Skip site navigation (1)Skip section navigation (2)
Date:      Sat, 15 Jan 2011 00:52:53 -0800
From:      Devin Teske <dteske@vicor.com>
To:        Devin Teske <dteske@vicor.com>
Cc:        freebsd-rc@freebsd.org
Subject:   Re: sysrc(8) -- a sysctl(8)-like utility for managing rc.conf(5)
Message-ID:  <2D86857A-DDFA-4F37-B3A5-B7A6418EB327@vicor.com>
In-Reply-To: <11FDD7C3-4451-47BD-AE01-58952ACFF586@vicor.com>
References:  <1286925182.32724.18.camel@localhost.localdomain> <1286996709.32724.60.camel@localhost.localdomain> <1287448781.5713.3.camel@localhost.localdomain> <1287510629.25599.2.camel@localhost.localdomain> <D763F474-8F19-4C65-B23F-78C9B137A8FE@vicor.com> <1288746388.7362.4.camel@localhost.localdomain> <17B64023-A64A-40DA-9CBC-A601710AB5BB@vicor.com> <1288919368.7362.35.camel@localhost.localdomain> <11FDD7C3-4451-47BD-AE01-58952ACFF586@vicor.com>

next in thread | previous in thread | raw e-mail | index | archive | help

On Nov 8, 2010, at 11:23 PM, Devin Teske wrote:

>=20
> On Nov 4, 2010, at 6:09 PM, Devin Teske wrote:
>=20
>> And now... for the piece de resistance!
>>=20
>>> ...
>>=20
>> Woohoo... version 3.0!
>=20
> 3.1 now.

Announcing version 3.2 now.

Added `-x' to delete entries from rc.conf(5) files. Works as-expected =
with other arguments such as `-f file', `-n', `-N', `-v', `-a', `-A', =
etc.

I think this version is "feature complete" finally in that you can now =
fully manage your rc.conf(5) files without ever opening a text editor =
(period).

Enjoy!

And thanks again for Ross West for pushing me to finish this. (I had =
been putting off this last feature)

>>>>>>>> Direct links:
>>>>>>>> http://druidbsd.sf.net/download/sysrc.gz (download gzipped)
>>>>>>>> http://druidbsd.sf.net/download/sysrc.txt (view as text)
>=20
>=20
>> And here's that good-ol' unified patch to show what's changed...
>=20

--- sysrc.3_1_2	2010-12-09 13:46:20.000000000 -0800
+++ sysrc	2011-01-15 00:38:31.000000000 -0800
@@ -2,11 +2,11 @@
 # -*- tab-width:  4 -*- ;; Emacs
 # vi: set tabstop=3D4     :: Vi/ViM
 #
-# Revision: 3.1.2
-# Last Modified: December 9th, 2010
+# Revision: 3.2
+# Last Modified: January 15th, 2011
 ############################################################ COPYRIGHT
 #
-# (c)2010. Devin Teske. All Rights Reserved.
+# (c)2010-2011. Devin Teske. All Rights Reserved.
 #
 # Redistribution and use in source and binary forms, with or without
 # modification, are permitted provided that the following conditions
@@ -30,6 +30,8 @@
 # SUCH DAMAGE.
 #
 # AUTHOR      DATE      DESCRIPTION
+# dteske   2011.01.15   Make `-A' override `-a' despite =
order-of-appearance.
+# dteske   2011.01.15   Add `-x' to remove variables from file(s).
 # dteske   2010.12.09   Fix taint-checking to not die on non-existent =
files.
 # dteske   2010.11.09   Minor fixes to sysrc_set/sysrc_set_awk.
 # dteske   2010.11.08   Further significant performance enhancements.
@@ -59,11 +61,12 @@
 #   	-h         Print this message to stderr and exit.
 #   	-f file    Operate on the specified file(s) instead of =
rc_conf_files.
 #   	           Can be specified multiple times for additional files.
-#   	-a         Dump a list of non-default configuration variables.
+#   	-a         Dump a list of all non-default configuration =
variables.
 #   	-A         Dump a list of all configuration variables (incl. =
defaults).
+#   	-x         Remove variable(s) from specified file(s).
 #   	-d         Print a description of the given variable.
 #   	-e         Print query results as `var=3Dvalue' (useful for =
producing
-#   	           output to be fed back in). Ignored if -n is =
specified.
+#   	           output to be fed back in). Ignored if `-n' is =
specified.
 #   	-v         Verbose. Print the pathname of the specific =
rc.conf(5)
 #   	           file where the directive was found.
 #   	-i         Ignore unknown variables.
@@ -122,6 +125,7 @@
 #
 # Options
 #
+DELETE=3D
 DESCRIBE=3D
 IGNORE_UNKNOWNS=3D
 JAIL=3D
@@ -198,15 +202,17 @@
 	eprintf "$optfmt" "" \
 	        "Can be specified multiple times for additional files."
 	eprintf "$optfmt" "-a" \
-	        "Dump a list of non-default configuration variables."
+	        "Dump a list of all non-default configuration =
variables."
 	eprintf "$optfmt" "-A" \
 	        "Dump a list of all configuration variables (incl. =
defaults)."
+	eprintf "$optfmt" "-x" \
+	        "Remove variable(s) from specified file(s)."
 	eprintf "$optfmt" "-d" \
 	        "Print a description of the given variable."
 	eprintf "$optfmt" "-e" \
 	        "Print query results as \`var=3Dvalue' (useful for =
producing"
 	eprintf "$optfmt" "" \
-	        "output to be fed back in). Ignored if -n is specified."
+	        "output to be fed back in). Ignored if \`-n' is =
specified."
 	eprintf "$optfmt" "-v" \
 	        "Verbose. Print the pathname of the specific rc.conf(5)"
 	eprintf "$optfmt" "" \
@@ -647,6 +653,118 @@
 	awk -v varname=3D"$1" "$sysrc_desc_awk" < "$RC_DEFAULTS"
 }
=20
+# sysrc_delete $varname
+#
+# Remove a setting from the system configuration files (edits files =
in-place).
+# Deletes all assignments to the given variable in all config files. If =
the
+# `-f file' option is passed, the removal is restricted to only those =
files
+# specified, otherwise the system collection of rc_conf_files is used.
+#
+# This function is a two-parter. Below is the awk(1) portion of the =
function,
+# afterward is the sh(1) function which utilizes the below awk script.
+#
+sysrc_delete_awk=3D'
+# Variables that should be defined on the invocation line:
+# 	-v varname=3D"varname"
+#
+BEGIN {
+	regex =3D "^[[:space:]]*"varname"=3D"
+	found =3D 0
+}
+{
+	if ( $0 ~ regex )
+		found =3D 1
+	else
+		print
+}
+END { exit ! found }
+'
+sysrc_delete()
+{
+	local varname=3D"$1"
+	local file
+
+	# Check arguments
+	[ "$varname" ] || return $FAILURE
+
+	#
+	# Operate on each of the specified files
+	#
+	for file in ${RC_CONFS:-$( sysrc_get rc_conf_files )}; do
+		#
+		# Create a new temporary file to write to.
+		#
+		local tmpfile=3D"$( mktemp -t "$progname" )"
+		[ "$tmpfile" ] || return $FAILURE
+
+		#
+		# Fixup permissions and ownership (mktemp(1) defaults to =
0600
+		# permissions) to instead match the destination file.
+		#
+		chmod "$( stat -f '%#Lp' "$file" )" "$tmpfile" 2> =
/dev/null
+		chmod "$( stat -f '%u:%g' "$file" )" "$tmpfile" 2> =
/dev/null
+
+		#
+		# Operate on the file, removing all occurrences, saving =
the
+		# output in our temporary file.
+		#
+		awk -v varname=3D"$varname" "$sysrc_delete_awk" "$file" =
\
+			> "$tmpfile"
+		if [ $? -ne $SUCCESS ]; then
+			# The file didn't contain any assignments
+			rm -f "$tmpfile"
+			continue
+		fi
+
+		#
+		# Taint-check our results.
+		#
+		if ! /bin/sh -n "$tmpfile"; then
+			eprintf "%s: Not overwriting \`%s' due to %s\n" =
\
+			        "$progname" "$file" "previous syntax =
errors"
+			rm -f "$tmpfile"
+			continue
+		fi
+
+		#
+		# Perform sanity checks
+		#
+		if [ ! -w "$file" ]; then
+			eprintf "%s: %s: Permission denied\n" \
+			        "$progname" "$file"
+			rm -f "$tmpfile"
+			continue
+		fi
+
+		#
+		# If verbose, now's the time to show it.
+		#=20
+		if [ "$SYSRC_VERBOSE" ]; then
+			echo -n "$file: "
+
+			#
+			# If `-N' is passed, simplify the output
+			#
+			if [ ! "$SHOW_VALUE" ]; then
+				echo "$NAME"
+				continue
+			fi
+
+			echo -n "${SHOW_NAME:+$NAME$SEP}"
+			( # Operate in sub-shell to protect parent =
environment
+				. "$file" 2> /dev/null
+				eval echo -n '"${'"$NAME"'}"' 2> =
/dev/null
+			)
+			echo "${SHOW_EQUALS:+\"}"
+		fi
+
+		#
+		# Finally, move the temporary file into place.
+		#
+		mv "$tmpfile" "$file"
+	done
+}
+
 ############################################################ MAIN =
SOURCE
=20
 #
@@ -657,14 +775,16 @@
 #
 # Process command-line options
 #
-while getopts hf:aAdevinNR:j: flag; do
+while getopts hf:aAxXdevinNR:j: flag; do
 	case "$flag" in
 	h) usage;;
 	f) [ "$OPTARG" ] || die \
 	   	"%s: Missing or null argument to \`-f' flag" "$progname"
 	   RC_CONFS=3D"$RC_CONFS${RC_CONFS:+ }$OPTARG";;
-	a) SHOW_ALL=3D1;;
+	a) SHOW_ALL=3D${SHOW_ALL:-1};;
 	A) SHOW_ALL=3D2;;
+	x) DELETE=3D${DELETE:-1};;
+	X) DELETE=3D2;;
 	d) DESCRIBE=3D1;;
 	e) SHOW_EQUALS=3D1;;
 	v) SYSRC_VERBOSE=3D1;;
@@ -696,6 +816,15 @@
 ) || die "$errmsg"
=20
 #
+# Process `-x' (and secret `-X') command-line options
+#
+errmsg=3D"$progname: \`-x' option incompatible with \`-a'/\`-A' =
options"
+errmsg=3D"$errmsg (use \`-X' to override)"
+if [ "$DELETE" -a "$SHOW_ALL" ]; then
+	[ "$DELETE" =3D "2" ] || die "$errmsg"
+fi
+
+#
 # Process `-e', `-n', and `-N' command-line options
 #
 SEP=3D': '
@@ -717,6 +846,8 @@
 	args=3D"
 		${SYSRC_VERBOSE:+-v}
 		${RC_CONFS:+-f'$RC_CONFS'}
+		$( [ "$DELETE" =3D "1" ] && echo \ -x )
+		$( [ "$DELETE" =3D "2" ] && echo \ -X )
 		$( [ "$SHOW_ALL" =3D "1" ] && echo \ -a )
 		$( [ "$SHOW_ALL" =3D "2" ] && echo \ -A )
 		${DESCRIBE:+-d}
@@ -820,8 +951,10 @@
 		#
 		IFS=3D"$IFS|"
 		EXCEPT=3D"IFS|EXCEPT|PATH|RC_DEFAULTS|OPTIND|DESCRIBE|SEP"=

-		=
EXCEPT=3D"$EXCEPT|SHOW_ALL|SHOW_EQUALS|SHOW_NAME|SHOW_VALUE"
-		EXCEPT=3D"$EXCEPT|SYSRC_VERBOSE|RC_CONFS|sysrc_desc_awk"
+		EXCEPT=3D"$EXCEPT|DELETE|SHOW_ALL|SHOW_EQUALS|SHOW_NAME"
+		EXCEPT=3D"$EXCEPT|SHOW_VALUE|SYSRC_VERBOSE|RC_CONFS"
+		=
EXCEPT=3D"$EXCEPT|progname|sysrc_desc_awk|sysrc_delete_awk"
+		EXCEPT=3D"$EXCEPT|SUCCESS|FAILURE"
=20
 		#
 		# Clean the environment (except for our required =
variables)
@@ -870,6 +1003,14 @@
 				continue
 			fi
=20
+			#
+			# If `-X' is passed, delete the variables
+			#
+			if [ "$DELETE" =3D "2" ]; then
+				sysrc_delete "$NAME"
+				continue
+			fi
+
 			[ "$SYSRC_VERBOSE" ] && \
 				echo -n "$( sysrc_find "$NAME" ): "
=20
@@ -883,6 +1024,7 @@
=20
 			echo "${SHOW_NAME:+$NAME$SEP}$(
 			      sysrc_get "$NAME" )${SHOW_EQUALS:+\"}"
+
 		done
 	)
=20
@@ -916,6 +1058,16 @@
 		fi
=20
 		#
+		# If `-x' or `-X' is passed, delete the variable and =
ignore the
+		# desire to set some value
+		#
+		if [ "$DELETE" ]; then
+			sysrc_delete "$NAME"
+			shift 1
+			continue
+		fi
+
+		#
 		# If `-N' is passed, simplify the output
 		#
 		if [ ! "$SHOW_VALUE" ]; then
@@ -948,6 +1100,15 @@
 			continue
 		fi
=20
+		#
+		# If `-x' or `-X' is passed, delete the variable
+		#
+		if [ "$DELETE" ]; then
+			sysrc_delete "$NAME"
+			shift 1
+			continue
+		fi
+
 		[ "$SYSRC_VERBOSE" ] && \
 			echo -n "$( sysrc_find "$NAME" ): "
=20


--
Cheers,
Devin Teske

-> CONTACT INFORMATION <-
Business Solutions Consultant II
FIS - fisglobal.com
510-735-5650 Mobile
510-621-2038 Office
510-621-2020 Office Fax
909-477-4578 Home/Fax
devin.teske@fisglobal.com

-> LEGAL DISCLAIMER <-
This message  contains confidential  and proprietary  information
of the sender,  and is intended only for the person(s) to whom it
is addressed. Any use, distribution, copying or disclosure by any
other person  is strictly prohibited.  If you have  received this
message in error,  please notify  the e-mail sender  immediately,
and delete the original message without making a copy.

-> FUN STUFF <-
-----BEGIN GEEK CODE BLOCK-----
Version 3.1
GAT/CS d(+) s: a- C++(++++) UB++++$ P++(++++) L++(++++) !E--- W++ N? o? =
K- w O
M+ V- PS+ PE Y+ PGP- t(+) 5? X+(++) R>++ tv(+) b+(++) DI+(++) D(+) G+>++ =
e>+ h
r>++ y+=20
------END GEEK CODE BLOCK------
http://www.geekcode.com/

-> END TRANSMISSION <-




Want to link to this message? Use this URL: <https://mail-archive.FreeBSD.org/cgi/mid.cgi?2D86857A-DDFA-4F37-B3A5-B7A6418EB327>