sysrc(8) -- a sysctl(8)-like utility for managing rc.conf(5)

Devin Teske dteske at vicor.com
Sat Jan 15 09:12:13 UTC 2011


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

> 
> On Nov 4, 2010, at 6:09 PM, Devin Teske wrote:
> 
>> And now... for the piece de resistance!
>> 
>>> ...
>> 
>> Woohoo... version 3.0!
> 
> 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)
> 
> 
>> And here's that good-ol' unified patch to show what's changed...
> 

--- 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=4     :: 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=value' (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=
 DESCRIBE=
 IGNORE_UNKNOWNS=
 JAIL=
@@ -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=value' (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="$1" "$sysrc_desc_awk" < "$RC_DEFAULTS"
 }
 
+# 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='
+# Variables that should be defined on the invocation line:
+# 	-v varname="varname"
+#
+BEGIN {
+	regex = "^[[:space:]]*"varname"="
+	found = 0
+}
+{
+	if ( $0 ~ regex )
+		found = 1
+	else
+		print
+}
+END { exit ! found }
+'
+sysrc_delete()
+{
+	local varname="$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="$( 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="$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.
+		# 
+		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
 
 #
@@ -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="$RC_CONFS${RC_CONFS:+ }$OPTARG";;
-	a) SHOW_ALL=1;;
+	a) SHOW_ALL=${SHOW_ALL:-1};;
 	A) SHOW_ALL=2;;
+	x) DELETE=${DELETE:-1};;
+	X) DELETE=2;;
 	d) DESCRIBE=1;;
 	e) SHOW_EQUALS=1;;
 	v) SYSRC_VERBOSE=1;;
@@ -696,6 +816,15 @@
 ) || die "$errmsg"
 
 #
+# Process `-x' (and secret `-X') command-line options
+#
+errmsg="$progname: \`-x' option incompatible with \`-a'/\`-A' options"
+errmsg="$errmsg (use \`-X' to override)"
+if [ "$DELETE" -a "$SHOW_ALL" ]; then
+	[ "$DELETE" = "2" ] || die "$errmsg"
+fi
+
+#
 # Process `-e', `-n', and `-N' command-line options
 #
 SEP=': '
@@ -717,6 +846,8 @@
 	args="
 		${SYSRC_VERBOSE:+-v}
 		${RC_CONFS:+-f'$RC_CONFS'}
+		$( [ "$DELETE" = "1" ] && echo \ -x )
+		$( [ "$DELETE" = "2" ] && echo \ -X )
 		$( [ "$SHOW_ALL" = "1" ] && echo \ -a )
 		$( [ "$SHOW_ALL" = "2" ] && echo \ -A )
 		${DESCRIBE:+-d}
@@ -820,8 +951,10 @@
 		#
 		IFS="$IFS|"
 		EXCEPT="IFS|EXCEPT|PATH|RC_DEFAULTS|OPTIND|DESCRIBE|SEP"
-		EXCEPT="$EXCEPT|SHOW_ALL|SHOW_EQUALS|SHOW_NAME|SHOW_VALUE"
-		EXCEPT="$EXCEPT|SYSRC_VERBOSE|RC_CONFS|sysrc_desc_awk"
+		EXCEPT="$EXCEPT|DELETE|SHOW_ALL|SHOW_EQUALS|SHOW_NAME"
+		EXCEPT="$EXCEPT|SHOW_VALUE|SYSRC_VERBOSE|RC_CONFS"
+		EXCEPT="$EXCEPT|progname|sysrc_desc_awk|sysrc_delete_awk"
+		EXCEPT="$EXCEPT|SUCCESS|FAILURE"
 
 		#
 		# Clean the environment (except for our required variables)
@@ -870,6 +1003,14 @@
 				continue
 			fi
 
+			#
+			# If `-X' is passed, delete the variables
+			#
+			if [ "$DELETE" = "2" ]; then
+				sysrc_delete "$NAME"
+				continue
+			fi
+
 			[ "$SYSRC_VERBOSE" ] && \
 				echo -n "$( sysrc_find "$NAME" ): "
 
@@ -883,6 +1024,7 @@
 
 			echo "${SHOW_NAME:+$NAME$SEP}$(
 			      sysrc_get "$NAME" )${SHOW_EQUALS:+\"}"
+
 		done
 	)
 
@@ -916,6 +1058,16 @@
 		fi
 
 		#
+		# 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
 
+		#
+		# 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" ): "
 


--
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 at 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+ 
------END GEEK CODE BLOCK------
http://www.geekcode.com/

-> END TRANSMISSION <-



More information about the freebsd-rc mailing list