sysrc revision 241565
1235427Sdelphij#!/bin/sh
2235427Sdelphij#-
3235427Sdelphij# Copyright (c) 2010-2012 Devin Teske
4235427Sdelphij# All rights reserved.
5235427Sdelphij#
6235427Sdelphij# Redistribution and use in source and binary forms, with or without
7235427Sdelphij# modification, are permitted provided that the following conditions
8235427Sdelphij# are met:
9235427Sdelphij# 1. Redistributions of source code must retain the above copyright
10235427Sdelphij#    notice, this list of conditions and the following disclaimer.
11235427Sdelphij# 2. Redistributions in binary form must reproduce the above copyright
12235427Sdelphij#    notice, this list of conditions and the following disclaimer in the
13235427Sdelphij#    documentation and/or other materials provided with the distribution.
14235427Sdelphij#
15251158Sdelphij# THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
16235427Sdelphij# ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING BUT NOT LIMITED TO, THE
17235427Sdelphij# IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
18235427Sdelphij# ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
19235427Sdelphij# FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
20235427Sdelphij# DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
21235427Sdelphij# OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
22235427Sdelphij# HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
23235427Sdelphij# LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
24235427Sdelphij# OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
25235427Sdelphij# SUCH DAMAGE.
26235427Sdelphij#
27235427Sdelphij# $FreeBSD: head/usr.sbin/sysrc/sysrc 241565 2012-10-14 23:45:56Z dteske $
28235427Sdelphij#
29235427Sdelphij############################################################ INCLUDES
30235427Sdelphij
31235427SdelphijBSDCFG_SHARE="/usr/share/bsdconfig"
32235427Sdelphij[ "$_COMMON_SUBR" ] || . $BSDCFG_SHARE/common.subr || exit 1
33235427Sdelphij[ "$_SYSRC_SUBR"  ] || f_include $BSDCFG_SHARE/sysrc.subr
34235427Sdelphij
35235427Sdelphij############################################################ CONFIGURATION
36235427Sdelphij
37235427Sdelphij#
38235427Sdelphij# Default verbosity.
39235427Sdelphij#
40235427Sdelphij: ${SYSRC_VERBOSE:=}
41235427Sdelphij
42235427Sdelphij############################################################ GLOBALS
43235427Sdelphij
44235427Sdelphij#
45235427Sdelphij# Options
46235427Sdelphij#
47235427SdelphijDELETE=
48235427SdelphijDESCRIBE=
49251158SdelphijIGNORE_UNKNOWNS=
50235427SdelphijJAIL=
51235427SdelphijQUIET=
52235427SdelphijROOTDIR=
53235427SdelphijSHOW_ALL=
54235427SdelphijSHOW_EQUALS=
55235427SdelphijSHOW_FILE=
56235427SdelphijSHOW_NAME=1
57235427SdelphijSHOW_VALUE=1
58235427Sdelphij
59235427Sdelphij############################################################ FUNCTIONS
60235427Sdelphij
61235427Sdelphij# die [ $fmt [ $opts ... ]]
62235427Sdelphij#
63235427Sdelphij# Optionally print a message to stderr before exiting with failure status.
64235427Sdelphij#
65235427Sdelphijdie()
66235427Sdelphij{
67235427Sdelphij	local fmt="$1"
68235427Sdelphij	[ $# -gt 0 ] && shift 1
69235427Sdelphij	[  "$fmt"  ] && f_err "$fmt\n" "$@"
70235427Sdelphij
71235427Sdelphij	exit $FAILURE
72235427Sdelphij}
73235427Sdelphij
74235427Sdelphij# usage
75235427Sdelphij#
76235427Sdelphij# Prints a short syntax statement and exits.
77235427Sdelphij#
78235427Sdelphijusage()
79235427Sdelphij{
80235427Sdelphij	f_err "Usage: %s [OPTIONS] name[=value] ...\n" "$pgm"
81235427Sdelphij	f_err "Try \`%s --help' for more information.\n" "$pgm"
82235427Sdelphij	die
83235427Sdelphij}
84235427Sdelphij
85235427Sdelphij# help
86235427Sdelphij#
87235427Sdelphij# Prints a full syntax statement and exits.
88235427Sdelphij#
89235427Sdelphijhelp()
90235427Sdelphij{
91235427Sdelphij	local optfmt="\t%-11s%s\n"
92235427Sdelphij	local envfmt="\t%-17s%s\n"
93235427Sdelphij
94235427Sdelphij	f_err "Usage: %s [OPTIONS] name[=value] ...\n" "$pgm"
95235427Sdelphij
96235427Sdelphij	f_err "OPTIONS:\n"
97235427Sdelphij	f_err "$optfmt" "-a" \
98235427Sdelphij	      "Dump a list of all non-default configuration variables."
99235427Sdelphij	f_err "$optfmt" "-A" \
100235427Sdelphij	      "Dump a list of all configuration variables (incl. defaults)."
101235427Sdelphij	f_err "$optfmt" "-d" \
102235427Sdelphij	      "Print a description of the given variable."
103235427Sdelphij	f_err "$optfmt" "-D" \
104235427Sdelphij	      "Show default value(s) only (this is the same as setting"
105235427Sdelphij	f_err "$optfmt" "" \
106235427Sdelphij	      "RC_CONFS to NULL or passing \`-f' with a NULL file-argument)."
107235427Sdelphij	f_err "$optfmt" "-e" \
108235427Sdelphij	      "Print query results as \`var=value' (useful for producing"
109235427Sdelphij	f_err "$optfmt" "" \
110235427Sdelphij	      "output to be fed back in). Ignored if \`-n' is specified."
111235427Sdelphij	f_err "$optfmt" "-f file" \
112235427Sdelphij	      "Operate on the specified file(s) instead of rc_conf_files."
113235427Sdelphij	f_err "$optfmt" "" \
114235427Sdelphij	      "Can be specified multiple times for additional files."
115235427Sdelphij	f_err "$optfmt" "-F" \
116235427Sdelphij	      "Show only the last rc.conf(5) file each directive is in."
117235427Sdelphij	f_err "$optfmt" "-h" \
118235427Sdelphij	      "Print a short usage statement to stderr and exit."
119235427Sdelphij	f_err "$optfmt" "--help" \
120235427Sdelphij	      "Print this message to stderr and exit."
121235427Sdelphij	f_err "$optfmt" "-i" \
122235427Sdelphij	      "Ignore unknown variables."
123235427Sdelphij	f_err "$optfmt" "-j jail" \
124235427Sdelphij	      "The jid or name of the jail to operate within (overrides"
125235427Sdelphij	f_err "$optfmt" "" \
126235427Sdelphij	      "\`-R dir'; requires jexec(8))."
127235427Sdelphij	f_err "$optfmt" "-n" \
128235427Sdelphij	      "Show only variable values, not their names."
129235427Sdelphij	f_err "$optfmt" "-N" \
130235427Sdelphij	      "Show only variable names, not their values."
131235427Sdelphij	f_err "$optfmt" "-q" \
132235427Sdelphij	      "Quiet. Ignore previous \`-v' and/or SYSRC_VERBOSE."
133235427Sdelphij	f_err "$optfmt" "-R dir" \
134235427Sdelphij	      "Operate within the root directory \`dir' rather than \`/'."
135235427Sdelphij	f_err "$optfmt" "-v" \
136235427Sdelphij	      "Verbose. Print the pathname of the specific rc.conf(5)"
137235427Sdelphij	f_err "$optfmt" "" \
138235427Sdelphij	      "file where the directive was found."
139235427Sdelphij	f_err "$optfmt" "-x" \
140235427Sdelphij	      "Remove variable(s) from specified file(s)."
141235427Sdelphij	f_err "\n"
142235427Sdelphij
143235427Sdelphij	f_err "ENVIRONMENT:\n"
144235427Sdelphij	f_err "$envfmt" "RC_CONFS" \
145235427Sdelphij	      "Override default rc_conf_files (even if set to NULL)."
146235427Sdelphij	f_err "$envfmt" "RC_DEFAULTS" \
147235427Sdelphij	      "Location of \`/etc/defaults/rc.conf' file."
148235427Sdelphij	f_err "$envfmt" "SYSRC_VERBOSE" \
149235427Sdelphij	      "Default verbosity. Set to non-NULL to enable."
150235427Sdelphij
151235427Sdelphij	die
152235427Sdelphij}
153235427Sdelphij
154235427Sdelphij# jail_depend
155235427Sdelphij#
156235427Sdelphij# Dump dependencies such as language-file variables and include files to stdout
157235427Sdelphij# to be piped-into sh(1) running via jexec(8)/chroot(8). As a security measure,
158235427Sdelphij# this prevents existing language files and library files from being loaded in
159235427Sdelphij# the jail. This also relaxes the requirement to have these files in every jail
160235427Sdelphij# before sysrc can be used on said jail.
161235427Sdelphij#
162235427Sdelphijjail_depend()
163235427Sdelphij{
164235427Sdelphij	#
165235427Sdelphij	# Indicate that we are jailed
166235427Sdelphij	#
167235427Sdelphij	echo export _SYSRC_JAILED=1
168235427Sdelphij
169235427Sdelphij	#
170251158Sdelphij	# Print i18n language variables (their current values are sanitized
171235427Sdelphij	# and re-printed for interpretation so that the i18n language files
172235427Sdelphij	# do not need to exist within the jail).
173235427Sdelphij	#
174235427Sdelphij	local var val
175235427Sdelphij	for var in \
176235427Sdelphij		msg_cannot_create_permission_denied \
177235427Sdelphij		msg_permission_denied \
178235427Sdelphij		msg_previous_syntax_errors \
179235427Sdelphij	; do
180235427Sdelphij		val=$( eval echo \"\$$var\" |
181235427Sdelphij			awk '{ gsub(/'\''/, "'\''\\'\'\''"); print }' )
182235427Sdelphij		echo $var="'$val'"
183235427Sdelphij	done
184235427Sdelphij
185235427Sdelphij	#
186235427Sdelphij	# Print include dependencies
187235427Sdelphij	#
188235427Sdelphij	cat $BSDCFG_SHARE/common.subr
189235427Sdelphij	cat $BSDCFG_SHARE/sysrc.subr
190235427Sdelphij}
191235427Sdelphij
192235427Sdelphij############################################################ MAIN SOURCE
193235427Sdelphij
194235427Sdelphij#
195235427Sdelphij# Perform sanity checks
196235427Sdelphij#
197235427Sdelphij[ $# -gt 0 ] || usage
198235427Sdelphij
199235427Sdelphij#
200235427Sdelphij# Check for `--help' command-line option
201235427Sdelphij#
202235427Sdelphij( # Operate in sub-shell to protect $@ in parent
203235427Sdelphij	while [ $# -gt 0 ]; do
204251158Sdelphij		case "$1" in
205235427Sdelphij		--help) exit 1;;
206251158Sdelphij		-[fRj]) # These flags take an argument
207235427Sdelphij			shift 1;;
208235427Sdelphij		esac
209235427Sdelphij		shift 1
210235427Sdelphij	done
211235427Sdelphij	exit 0
212235427Sdelphij) || help
213235427Sdelphij
214235427Sdelphij#
215235427Sdelphij# Process command-line flags
216235427Sdelphij#
217235427Sdelphijwhile getopts aAdDef:Fhij:nNqR:vxX flag; do
218235427Sdelphij	case "$flag" in
219235427Sdelphij	a) SHOW_ALL=${SHOW_ALL:-1};;
220235427Sdelphij	A) SHOW_ALL=2;;
221235427Sdelphij	d) DESCRIBE=1;;
222251158Sdelphij	D) RC_CONFS=;;
223235427Sdelphij	e) SHOW_EQUALS=1;;
224251158Sdelphij	f) RC_CONFS="$RC_CONFS${RC_CONFS:+ }$OPTARG";;
225235427Sdelphij	F) SHOW_FILE=1;;
226235427Sdelphij	h) usage;;
227235427Sdelphij	i) IGNORE_UNKNOWNS=1;;
228235427Sdelphij	j) [ "$OPTARG" ] || die \
229235427Sdelphij	   	"%s: Missing or null argument to \`-j' flag" "$pgm"
230235427Sdelphij	   JAIL="$OPTARG";;
231235427Sdelphij	n) SHOW_NAME=;;
232235427Sdelphij	N) SHOW_VALUE=;;
233235427Sdelphij	q) QUIET=1 SYSRC_VERBOSE=;;
234235427Sdelphij	R) [ "$OPTARG" ] || die \
235235427Sdelphij	   	"%s: Missing or null argument to \`-R' flag" "$pgm"
236235427Sdelphij	   ROOTDIR="$OPTARG";;
237235427Sdelphij	v) SYSRC_VERBOSE=1 QUIET=;;
238235427Sdelphij	x) DELETE=${DELETE:-1};;
239235427Sdelphij	X) DELETE=2;;
240235427Sdelphij	\?) usage;;
241235427Sdelphij	esac
242235427Sdelphijdone
243235427Sdelphijshift $(( $OPTIND - 1 ))
244235427Sdelphij
245235427Sdelphij#
246235427Sdelphij# [More] Sanity checks (e.g., "sysrc --")
247235427Sdelphij#
248235427Sdelphij[ $# -eq 0 -a ! "$SHOW_ALL" ] && usage
249235427Sdelphij
250235427Sdelphij#
251235427Sdelphij# Taint-check all rc.conf(5) files
252235427Sdelphij#
253235427Sdelphijerrmsg="$pgm: Exiting due to previous syntax errors"
254235427Sdelphijif [ "${RC_CONFS+set}" ]; then
255235427Sdelphij	( for i in $RC_CONFS; do
256235427Sdelphij	  	[ -e "$i" ] || continue
257235427Sdelphij	  	/bin/sh -n "$i" || exit $FAILURE
258235427Sdelphij	  done
259235427Sdelphij	  exit $SUCCESS
260235427Sdelphij	) || die "$errmsg"
261235427Sdelphijelse
262235427Sdelphij	/bin/sh -n "$RC_DEFAULTS" || die "$errmsg"
263235427Sdelphij	( . "$RC_DEFAULTS"
264235427Sdelphij	  for i in $rc_conf_files; do
265235427Sdelphij	  	[ -e "$i" ] || continue
266235427Sdelphij	  	/bin/sh -n "$i" || exit $FAILURE
267235427Sdelphij	  done
268235427Sdelphij	  exit $SUCCESS
269235427Sdelphij	) || die "$errmsg"
270235427Sdelphijfi
271235427Sdelphij
272235427Sdelphij#
273235427Sdelphij# Process `-x' (and secret `-X') command-line options
274235427Sdelphij#
275235427Sdelphijerrmsg="$pgm: \`-x' option incompatible with \`-a'/\`-A' options"
276235427Sdelphijerrmsg="$errmsg (use \`-X' to override)"
277235427Sdelphijif [ "$DELETE" -a "$SHOW_ALL" ]; then
278235427Sdelphij	[ "$DELETE" = "2" ] || die "$errmsg"
279235427Sdelphijfi
280235427Sdelphij
281235427Sdelphij#
282235427Sdelphij# Process `-e', `-n', and `-N' command-line options
283235427Sdelphij#
284235427SdelphijSEP=': '
285235427Sdelphij[ "$SHOW_EQUALS" ] && SEP='="'
286235427Sdelphij[ "$SHOW_NAME" ] || SHOW_EQUALS=
287235427Sdelphij[ "$SYSRC_VERBOSE" = "0" ] && SYSRC_VERBOSE=
288235427Sdelphijif [ ! "$SHOW_VALUE" ]; then
289235427Sdelphij	SHOW_NAME=1
290235427Sdelphij	SHOW_EQUALS=
291235427Sdelphijfi
292235427Sdelphij
293235427Sdelphij#
294235427Sdelphij# Process `-j jail' and `-R dir' command-line options
295235427Sdelphij#
296235427Sdelphijif [ "$JAIL" -o "$ROOTDIR" ]; then
297235427Sdelphij	#
298235427Sdelphij	# Reconstruct the arguments that we want to carry-over
299235427Sdelphij	#
300235427Sdelphij	args="
301235427Sdelphij		${SYSRC_VERBOSE:+-v}
302235427Sdelphij		${QUIET:+-q}
303235427Sdelphij		$( [ "$DELETE" = "1" ] && echo \ -x )
304235427Sdelphij		$( [ "$DELETE" = "2" ] && echo \ -X )
305235427Sdelphij		$( [ "$SHOW_ALL" = "1" ] && echo \ -a )
306235427Sdelphij		$( [ "$SHOW_ALL" = "2" ] && echo \ -A )
307235427Sdelphij		${DESCRIBE:+-d}
308235427Sdelphij		${SHOW_EQUALS:+-e}
309235427Sdelphij		${IGNORE_UNKNOWNS:+-i}
310235427Sdelphij		$( [ "$SHOW_NAME"  ] || echo \ -n )
311235427Sdelphij		$( [ "$SHOW_VALUE" ] || echo \ -N )
312235427Sdelphij		$( [ "$SHOW_FILE"  ] && echo \ -F )
313235427Sdelphij	"
314235427Sdelphij	if [ "${RC_CONFS+set}" ]; then
315235427Sdelphij		args="$args -f '$RC_CONFS'"
316235427Sdelphij	fi
317235427Sdelphij	for arg in "$@"; do
318235427Sdelphij		args="$args '$arg'"
319235427Sdelphij	done
320235427Sdelphij
321235427Sdelphij	#
322235427Sdelphij	# If both are supplied, `-j jail' supercedes `-R dir'
323251158Sdelphij	#
324235427Sdelphij	if [ "$JAIL" ]; then
325235427Sdelphij		#
326235427Sdelphij		# Re-execute ourselves with sh(1) via jexec(8)
327235427Sdelphij		#
328235427Sdelphij		( echo set -- $args
329235427Sdelphij		  jail_depend
330235427Sdelphij		  cat $0
331235427Sdelphij		) | env - RC_DEFAULTS="$RC_DEFAULTS" \
332235427Sdelphij		    	/usr/sbin/jexec "$JAIL" /bin/sh
333235427Sdelphij		exit $?
334235427Sdelphij	elif [ "$ROOTDIR" ]; then
335235427Sdelphij		#
336235427Sdelphij		# Make sure that the root directory specified is not to any
337235427Sdelphij		# running jails.
338235427Sdelphij		#
339235427Sdelphij		# NOTE: To maintain backward compatibility with older jails on
340235427Sdelphij		# older systems, we will not perform this check if either the
341235427Sdelphij		# jls(1) or jexec(8) utilities are missing.
342235427Sdelphij		#
343235427Sdelphij		if f_have jexec && f_have jls; then
344235427Sdelphij			jid="`jls jid path | \
345235427Sdelphij			(
346235427Sdelphij				while read JID JROOT; do
347235427Sdelphij					[ "$JROOT" = "$ROOTDIR" ] || continue
348235427Sdelphij					echo $JID
349235427Sdelphij				done
350235427Sdelphij			)`"
351235427Sdelphij
352235427Sdelphij			#
353235427Sdelphij			# If multiple running jails match the specified root
354235427Sdelphij			# directory, exit with error.
355235427Sdelphij			#
356235427Sdelphij			if [ "$jid" -a "${jid%[$IFS]*}" != "$jid" ]; then
357235427Sdelphij				die "%s: %s: %s" "$pgm" "$ROOTDIR" \
358235427Sdelphij				    "$( echo "Multiple jails claim this" \
359235427Sdelphij				             "directory as their root." \
360235427Sdelphij				             "(use \`-j jail' instead)" )"
361235427Sdelphij			fi
362235427Sdelphij
363235427Sdelphij			#
364235427Sdelphij			# If only a single running jail matches the specified
365235427Sdelphij			# root directory, implicitly use `-j jail'.
366235427Sdelphij			#
367235427Sdelphij			if [ "$jid" ]; then
368235427Sdelphij				#
369				# Re-execute outselves with sh(1) via jexec(8)
370				#
371				( echo set -- $args
372				  jail_depend
373				  cat $0
374				) | env - RC_DEFAULTS="$RC_DEFAULTS" \
375					/usr/sbin/jexec "$jid" /bin/sh
376				exit $?
377			fi
378
379			# Otherwise, fall through and allow chroot(8)
380		fi
381
382		#
383		# Re-execute ourselves with sh(1) via chroot(8)
384		#
385		( echo set -- $args
386		  jail_depend
387		  cat $0
388		) | env - RC_DEFAULTS="$RC_DEFAULTS" \
389		    	/usr/sbin/chroot "$ROOTDIR" /bin/sh
390		exit $?
391	fi
392fi
393
394#
395# Process `-a' or `-A' command-line options
396#
397if [ "$SHOW_ALL" ]; then
398	#
399	# Get a list of variables that are currently set in the rc.conf(5)
400	# files (included `/etc/defaults/rc.conf') by performing a call to
401	# source_rc_confs() in a clean environment.
402	#
403	( # Operate in a sub-shell to protect the parent environment
404		#
405		# Set which variables we want to preserve in the environment.
406		# Append the pipe-character (|) to the list of internal field
407		# separation (IFS) characters, allowing us to use the below
408		# list both as an extended grep (-E) pattern and argument list
409		# (required to first get f_clean_env() to preserve these in the
410		# environment and then later to prune them from the list of
411		# variables produced by set(1)).
412		#
413		IFS="$IFS|"
414		EXCEPT="IFS|EXCEPT|PATH|RC_DEFAULTS|OPTIND|DESCRIBE|SEP"
415		EXCEPT="$EXCEPT|DELETE|SHOW_ALL|SHOW_EQUALS|SHOW_NAME"
416		EXCEPT="$EXCEPT|SHOW_VALUE|SHOW_FILE|SYSRC_VERBOSE|RC_CONFS"
417		EXCEPT="$EXCEPT|pgm|SUCCESS|FAILURE"
418		EXCEPT="$EXCEPT|f_sysrc_desc_awk|f_sysrc_delete_awk"
419
420		#
421		# Clean the environment (except for our required variables)
422		# and then source the required files.
423		#
424		f_clean_env --except $EXCEPT
425		if [ -f "$RC_DEFAULTS" -a -r "$RC_DEFAULTS" ]; then
426			. "$RC_DEFAULTS"
427
428			#
429			# If passed `-a' (rather than `-A'), re-purge the
430			# environment, removing the rc.conf(5) defaults.
431			#
432			[ "$SHOW_ALL" = "1" ] \
433				&& f_clean_env --except rc_conf_files $EXCEPT
434
435			#
436			# If `-f file' was passed, set $rc_conf_files to an
437			# explicit value, modifying the default behavior of
438			# source_rc_confs().
439			#
440			[ "${RC_CONFS+set}" ] && rc_conf_files="$RC_CONFS"
441
442			source_rc_confs
443
444			#
445			# If passed `-a' (rather than `-A'), remove
446			# `rc_conf_files' unless it was defined somewhere
447			# other than rc.conf(5) defaults.
448			#
449			[ "$SHOW_ALL" = "1" -a \
450			  "$( f_sysrc_find rc_conf_files )" = "$RC_DEFAULTS" \
451			] \
452			&& unset rc_conf_files
453		fi
454
455		for NAME in $( set |
456			awk -F= '/^[[:alpha:]_][[:alnum:]_]*=/ {print $1}' |
457			grep -Ev "^($EXCEPT)$"
458		); do
459			#
460			# If enabled, describe rather than expand value
461			#
462			if [ "$DESCRIBE" ]; then
463				echo "$NAME: $( f_sysrc_desc "$NAME" )"
464				continue
465			fi
466
467			#
468			# If `-F' is passed, find it and move on
469			#
470			if [ "$SHOW_FILE" ]; then
471				[ "$SHOW_NAME" ] && echo -n "$NAME: "
472				f_sysrc_find "$NAME"
473				continue
474			fi
475
476			#
477			# If `-X' is passed, delete the variables
478			#
479			if [ "$DELETE" = "2" ]; then
480				f_sysrc_delete "$NAME"
481				continue
482			fi
483
484			[ "$SYSRC_VERBOSE" ] && \
485				echo -n "$( f_sysrc_find "$NAME" ): "
486
487			#
488			# If `-N' is passed, simplify the output
489			#
490			if [ ! "$SHOW_VALUE" ]; then
491				echo "$NAME"
492				continue
493			fi
494
495			echo "${SHOW_NAME:+$NAME$SEP}$(
496			      f_sysrc_get "$NAME" )${SHOW_EQUALS:+\"}"
497
498		done
499	)
500
501	#
502	# Ignore the remainder of positional arguments.
503	#
504	exit $SUCCESS
505fi
506
507#
508# Process command-line arguments
509#
510while [ $# -gt 0 ]; do
511	NAME="${1%%=*}"
512
513	[ "$DESCRIBE" ] && \
514		echo "$NAME: $( f_sysrc_desc "$NAME" )"
515
516	case "$1" in
517	*=*)
518		#
519		# Like sysctl(8), if both `-d' AND "name=value" is passed,
520		# first describe, then attempt to set
521		#
522
523		if [ "$SYSRC_VERBOSE" ]; then
524			file=$( f_sysrc_find "$NAME" )
525			[ "$file" = "$RC_DEFAULTS" -o ! "$file" ] && \
526				file=$( f_sysrc_get 'rc_conf_files%%[$IFS]*' )
527			echo -n "$file: "
528		fi
529
530		#
531		# If `-x' or `-X' is passed, delete the variable and ignore the
532		# desire to set some value
533		#
534		if [ "$DELETE" ]; then
535			f_sysrc_delete "$NAME"
536			shift 1
537			continue
538		fi
539
540		#
541		# If `-N' is passed, simplify the output
542		#
543		if [ ! "$SHOW_VALUE" ]; then
544			echo "$NAME"
545			f_sysrc_set "$NAME" "${1#*}"
546		else
547			if [ "$SHOW_FILE" ]; then
548				before=$( f_sysrc_find "$NAME" )
549			else
550				before=$( f_sysrc_get "$NAME" )
551			fi
552			if f_sysrc_set "$NAME" "${1#*=}"; then
553				if [ "$SHOW_FILE" ]; then
554					after=$( f_sysrc_find "$NAME" )
555					echo -n "${SHOW_NAME:+$NAME$SEP}"
556					echo -n "$before${SHOW_EQUALS:+\"}"
557					echo " -> $after"
558				else
559					after=$( f_sysrc_get "$NAME" )
560					echo -n "${SHOW_NAME:+$NAME$SEP}"
561					echo "$before -> $after"
562				fi
563			fi
564		fi
565		;;
566	*)
567		if ! IGNORED="$( f_sysrc_get "$NAME?" )"; then
568			[ "$IGNORE_UNKNOWNS" ] \
569				|| echo "$pgm: unknown variable '$NAME'"
570			shift 1
571			continue
572		fi
573
574		#
575		# Like sysctl(8), when `-d' is passed,
576		# desribe it rather than expanding it
577		#
578
579		if [ "$DESCRIBE" ]; then
580			shift 1
581			continue
582		fi
583
584		#
585		# If `-x' or `-X' is passed, delete the variable
586		#
587		if [ "$DELETE" ]; then
588			f_sysrc_delete "$NAME"
589			shift 1
590			continue
591		fi
592
593		#
594		# If `-F' is passed, find it and move on
595		#
596		if [ "$SHOW_FILE" ]; then
597			[ "$SHOW_NAME" ] && echo -n "$NAME: "
598			f_sysrc_find "$NAME"
599			shift 1
600			continue
601		fi
602
603		[ "$SYSRC_VERBOSE" ] && \
604			echo -n "$( f_sysrc_find "$NAME" ): "
605
606		#
607		# If `-N' is passed, simplify the output
608		#
609		if [ ! "$SHOW_VALUE" ]; then
610			echo "$NAME"
611		else
612			echo "${SHOW_NAME:+$NAME$SEP}$(
613			      f_sysrc_get "$NAME" )${SHOW_EQUALS:+\"}"
614		fi
615	esac
616	shift 1
617done
618