sysrc revision 290311
1294068Ssmh#!/bin/sh
2294068Ssmh#-
3294068Ssmh# Copyright (c) 2010-2015 Devin Teske
4294068Ssmh# All rights reserved.
5294068Ssmh#
6294068Ssmh# Redistribution and use in source and binary forms, with or without
7294068Ssmh# modification, are permitted provided that the following conditions
8294068Ssmh# are met:
9294068Ssmh# 1. Redistributions of source code must retain the above copyright
10294068Ssmh#    notice, this list of conditions and the following disclaimer.
11294068Ssmh# 2. Redistributions in binary form must reproduce the above copyright
12294068Ssmh#    notice, this list of conditions and the following disclaimer in the
13294068Ssmh#    documentation and/or other materials provided with the distribution.
14294068Ssmh#
15294068Ssmh# THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
16294068Ssmh# ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
17294068Ssmh# IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
18294068Ssmh# ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
19294068Ssmh# FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
20294068Ssmh# DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
21294068Ssmh# OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
22294068Ssmh# HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
23294068Ssmh# LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
24294068Ssmh# OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
25294068Ssmh# SUCH DAMAGE.
26294068Ssmh#
27294068Ssmh# $FreeBSD: stable/10/usr.sbin/sysrc/sysrc 290311 2015-11-02 22:55:25Z dteske $
28294068Ssmh#
29294068Ssmh############################################################ INCLUDES
30294068Ssmh
31294068Ssmh# Prevent `-d' from being interpreted as a debug flag by common.subr
32294068SsmhDEBUG_SELF_INITIALIZE=
33294068Ssmh
34294068SsmhBSDCFG_SHARE="/usr/share/bsdconfig"
35294068Ssmh[ "$_COMMON_SUBR" ] || . $BSDCFG_SHARE/common.subr || exit 1
36294068Ssmh[ "$_SYSRC_SUBR"  ] || f_include $BSDCFG_SHARE/sysrc.subr
37294068Ssmh
38294068Ssmh############################################################ GLOBALS
39294068Ssmh
40294068Ssmh#
41294068Ssmh# Version information
42294068Ssmh#
43294068SsmhSYSRC_VERSION="6.5 Sep-1,2015"
44294068Ssmh
45294068Ssmh#
46294068Ssmh# Options
47294068Ssmh#
48294068SsmhCHECK_ONLY=
49294068SsmhDELETE=
50294068SsmhDESCRIBE=
51294068SsmhIGNORE_UNKNOWNS=
52294068SsmhJAIL=
53294068SsmhQUIET=
54294068SsmhROOTDIR=
55294068SsmhSHOW_ALL=
56294265SsmhSHOW_EQUALS=
57294068SsmhSHOW_FILE=
58294265SsmhSHOW_NAME=1
59294068SsmhSHOW_VALUE=1
60294068SsmhVERBOSE=
61294068Ssmh
62294068Ssmh############################################################ FUNCTIONS
63294068Ssmh
64294068Ssmh# die [$fmt [$opts ...]]
65294068Ssmh#
66294068Ssmh# Optionally print a message to stderr before exiting with failure status.
67294068Ssmh#
68294068Ssmhdie()
69294068Ssmh{
70294068Ssmh	local fmt="$1"
71294068Ssmh	[ $# -gt 0 ] && shift 1
72294068Ssmh	[  "$fmt"  ] && f_err "$fmt\n" "$@"
73294068Ssmh
74294068Ssmh	exit $FAILURE
75294068Ssmh}
76294068Ssmh
77294068Ssmh# usage
78294068Ssmh#
79294068Ssmh# Prints a short syntax statement and exits.
80294068Ssmh#
81294068Ssmhusage()
82294068Ssmh{
83294068Ssmh	f_err "Usage: %s [OPTIONS] name[[+|-]=value] ...\n" "$pgm"
84294068Ssmh	f_err "Try \`%s --help' for more information.\n" "$pgm"
85294068Ssmh	die
86294068Ssmh}
87294068Ssmh
88294068Ssmh# help
89294068Ssmh#
90294068Ssmh# Prints a full syntax statement and exits.
91294068Ssmh#
92294068Ssmhhelp()
93294068Ssmh{
94295320Ssmh	local optfmt="\t%-11s%s\n"
95294068Ssmh	local envfmt="\t%-17s%s\n"
96294068Ssmh
97294068Ssmh	f_err "Usage: %s [OPTIONS] name[[+|-]=value] ...\n" "$pgm"
98294068Ssmh
99294068Ssmh	f_err "OPTIONS:\n"
100294068Ssmh	f_err "$optfmt" "-a" \
101294068Ssmh	      "Dump a list of all non-default configuration variables."
102294068Ssmh	f_err "$optfmt" "-A" \
103294068Ssmh	      "Dump a list of all configuration variables (incl. defaults)."
104294068Ssmh	f_err "$optfmt" "-c" \
105295320Ssmh	      "Check. Return success if set or no changes, else error."
106295320Ssmh	f_err "$optfmt" "-d" \
107295320Ssmh	      "Print a description of the given variable."
108295320Ssmh	f_err "$optfmt" "-D" \
109295320Ssmh	      "Show default value(s) only (this is the same as setting"
110295320Ssmh	f_err "$optfmt" "" \
111294068Ssmh	      "RC_CONFS to NULL or passing \`-f' with a NULL file-argument)."
112294068Ssmh	f_err "$optfmt" "-e" \
113294068Ssmh	      "Print query results as \`var=value' (useful for producing"
114295320Ssmh	f_err "$optfmt" "" \
115295320Ssmh	      "output to be fed back in). Ignored if \`-n' is specified."
116294068Ssmh	f_err "$optfmt" "-f file" \
117294068Ssmh	      "Operate on the specified file(s) instead of rc_conf_files."
118294068Ssmh	f_err "$optfmt" "" \
119295320Ssmh	      "Can be specified multiple times for additional files."
120295320Ssmh	f_err "$optfmt" "-F" \
121295320Ssmh	      "Show only the last rc.conf(5) file each directive is in."
122295320Ssmh	f_err "$optfmt" "-h" \
123295320Ssmh	      "Print a short usage statement to stderr and exit."
124295320Ssmh	f_err "$optfmt" "--help" \
125295320Ssmh	      "Print this message to stderr and exit."
126294068Ssmh	f_err "$optfmt" "-i" \
127294068Ssmh	      "Ignore unknown variables."
128294068Ssmh	f_err "$optfmt" "-j jail" \
129294068Ssmh	      "The jid or name of the jail to operate within (overrides"
130294068Ssmh	f_err "$optfmt" "" \
131295320Ssmh	      "\`-R dir'; requires jexec(8))."
132294068Ssmh	f_err "$optfmt" "-n" \
133294068Ssmh	      "Show only variable values, not their names."
134294068Ssmh	f_err "$optfmt" "-N" \
135294068Ssmh	      "Show only variable names, not their values."
136294068Ssmh	f_err "$optfmt" "-q" \
137294068Ssmh	      "Quiet. Disable verbose and hide certain errors."
138295356Ssmh	f_err "$optfmt" "-R dir" \
139295320Ssmh	      "Operate within the root directory \`dir' rather than \`/'."
140294068Ssmh	f_err "$optfmt" "-v" \
141294068Ssmh	      "Verbose. Print the pathname of the specific rc.conf(5)"
142294068Ssmh	f_err "$optfmt" "" \
143294068Ssmh	      "file where the directive was found."
144294068Ssmh	f_err "$optfmt" "--version" \
145294068Ssmh	      "Print version information to stdout and exit."
146294068Ssmh	f_err "$optfmt" "-x" \
147294068Ssmh	      "Remove variable(s) from specified file(s)."
148294068Ssmh	f_err "\n"
149294068Ssmh
150294068Ssmh	f_err "ENVIRONMENT:\n"
151294068Ssmh	f_err "$envfmt" "RC_CONFS" \
152294068Ssmh	      "Override default rc_conf_files (even if set to NULL)."
153294068Ssmh	f_err "$envfmt" "RC_DEFAULTS" \
154294068Ssmh	      "Location of \`/etc/defaults/rc.conf' file."
155294068Ssmh
156294068Ssmh	die
157294068Ssmh}
158294068Ssmh
159294068Ssmh# jail_depend
160294068Ssmh#
161294068Ssmh# Dump dependencies such as language-file variables and include files to stdout
162294068Ssmh# to be piped-into sh(1) running via jexec(8)/chroot(8). As a security measure,
163294068Ssmh# this prevents existing language files and library files from being loaded in
164294068Ssmh# the jail. This also relaxes the requirement to have these files in every jail
165294068Ssmh# before sysrc can be used on said jail.
166294068Ssmh#
167294068Ssmhjail_depend()
168294068Ssmh{
169294068Ssmh	#
170294068Ssmh	# Indicate that we are jailed
171294068Ssmh	#
172294068Ssmh	echo export _SYSRC_JAILED=1
173294068Ssmh
174294068Ssmh	#
175294068Ssmh	# Print i18n language variables (their current values are sanitized
176294068Ssmh	# and re-printed for interpretation so that the i18n language files
177294068Ssmh	# do not need to exist within the jail).
178294068Ssmh	#
179294068Ssmh	local var val
180294068Ssmh	for var in \
181295320Ssmh		msg_cannot_create_permission_denied \
182295320Ssmh		msg_permission_denied \
183295320Ssmh		msg_previous_syntax_errors \
184295320Ssmh	; do
185295320Ssmh		val=$( eval echo \"\$$var\" |
186295320Ssmh			awk '{ gsub(/'\''/, "'\''\\'\'\''"); print }' )
187295320Ssmh		echo $var="'$val'"
188294068Ssmh	done
189294068Ssmh
190294068Ssmh	#
191294068Ssmh	# Print include dependencies
192294068Ssmh	#
193294068Ssmh	echo DEBUG_SELF_INITIALIZE=
194295320Ssmh	cat $BSDCFG_SHARE/common.subr
195295320Ssmh	cat $BSDCFG_SHARE/sysrc.subr
196294068Ssmh}
197
198# escape $string [$var_to_set]
199#
200# Escape $string contents so that the contents can be properly encapsulated in
201# single-quotes (making for safe evaluation).
202#
203# NB: See `bsdconfig includes -dF escape' for relevant information/discussion.
204# NB: Abridged version of `f_shell_escape()' from bsdconfig(8) `strings.subr'.
205#
206escape()
207{
208	local __start="$1" __var_to_set="$2" __string=
209	while [ "$__start" ]; do
210		case "$__start" in *\'*)
211			__string="$__string${__start%%\'*}'\\''"
212			__start="${__start#*\'}" continue
213		esac
214		break
215	done
216	__string="$__string$__start"
217	if [ "$__var_to_set" ]; then
218		setvar "$__var_to_set" "$__string"
219	else
220		echo "$__string"
221	fi
222}
223
224############################################################ MAIN SOURCE
225
226#
227# Perform sanity checks
228#
229[ $# -gt 0 ] || usage # NOTREACHED
230
231#
232# Check for `--help' and `--version' command-line option
233#
234for arg in "$@"; do
235	case "$arg" in
236	--) break ;;
237	--help) help ;; # NOTREACHED
238	--version) # see GLOBALS
239		echo "$SYSRC_VERSION"
240		exit $FAILURE ;;
241	esac
242done
243unset arg
244
245#
246# Process command-line flags
247#
248while getopts aAcdDef:Fhij:nNqR:vxX flag; do
249	case "$flag" in
250	a) SHOW_ALL=${SHOW_ALL:-1} ;;
251	A) SHOW_ALL=2 ;;
252	c) CHECK_ONLY=1 ;;
253	d) DESCRIBE=1 ;;
254	D) RC_CONFS= ;;
255	e) SHOW_EQUALS=1 ;;
256	f) RC_CONFS="$RC_CONFS${RC_CONFS:+ }$OPTARG" ;;
257	F) SHOW_FILE=1 ;;
258	h) usage ;; # NOTREACHED
259	i) IGNORE_UNKNOWNS=1 ;;
260	j) [ "$OPTARG" ] || die \
261	   	"%s: Missing or null argument to \`-j' flag" "$pgm"
262	   JAIL="$OPTARG" ;;
263	n) SHOW_NAME= ;;
264	N) SHOW_VALUE= ;;
265	q) QUIET=1 VERBOSE= ;;
266	R) [ "$OPTARG" ] || die \
267	   	"%s: Missing or null argument to \`-R' flag" "$pgm"
268	   ROOTDIR="$OPTARG" ;;
269	v) VERBOSE=1 QUIET= ;;
270	x) DELETE=${DELETE:-1} ;;
271	X) DELETE=2 ;;
272	\?) usage ;; # NOTREACHED
273	esac
274done
275shift $(( $OPTIND - 1 ))
276
277#
278# [More] Sanity checks (e.g., "sysrc --")
279#
280[ $# -eq 0 -a ! "$SHOW_ALL" ] && usage # NOTREACHED
281
282#
283# Taint-check all rc.conf(5) files
284#
285errmsg="$pgm: Exiting due to previous syntax errors"
286if [ "${RC_CONFS+set}" ]; then
287	( for i in $RC_CONFS; do
288	  	[ -e "$i" ] || continue
289	  	/bin/sh -n "$i" || exit $FAILURE
290	  done
291	  exit $SUCCESS
292	) || die "$errmsg"
293else
294	/bin/sh -n "$RC_DEFAULTS" || die "$errmsg"
295	( . "$RC_DEFAULTS"
296	  for i in $rc_conf_files; do
297	  	[ -e "$i" ] || continue
298	  	/bin/sh -n "$i" || exit $FAILURE
299	  done
300	  exit $SUCCESS
301	) || die "$errmsg"
302fi
303
304#
305# Process `-x' (and secret `-X') command-line options
306#
307errmsg="$pgm: \`-x' option incompatible with \`-a'/\`-A' options"
308errmsg="$errmsg (use \`-X' to override)"
309if [ "$DELETE" -a "$SHOW_ALL" ]; then
310	[ "$DELETE" = "2" ] || die "$errmsg"
311fi
312
313#
314# Pre-flight for `-c' command-line option
315#
316[ "$CHECK_ONLY" -a "$SHOW_ALL" ] &&
317	die "$pgm: \`-c' option incompatible with \`-a'/\`-A' options"
318
319#
320# Process `-e', `-n', and `-N' command-line options
321#
322SEP=': '
323[ "$SHOW_FILE" ] && SHOW_EQUALS=
324[ "$SHOW_NAME" ] || SHOW_EQUALS=
325[ "$VERBOSE" = "0" ] && VERBOSE=
326if [ ! "$SHOW_VALUE" ]; then
327	SHOW_NAME=1
328	SHOW_EQUALS=
329fi
330[ "$SHOW_EQUALS" ] && SEP='="'
331
332#
333# Process `-j jail' and `-R dir' command-line options
334#
335if [ "$JAIL" -o "$ROOTDIR" ]; then
336	#
337	# Reconstruct the arguments that we want to carry-over
338	#
339	args="
340		${VERBOSE:+-v}
341		${QUIET:+-q}
342		$( [ "$DELETE" = "1" ] && echo \ -x )
343		$( [ "$DELETE" = "2" ] && echo \ -X )
344		$( [ "$SHOW_ALL" = "1" ] && echo \ -a )
345		$( [ "$SHOW_ALL" = "2" ] && echo \ -A )
346		${CHECK_ONLY:+-c}
347		${DESCRIBE:+-d}
348		${SHOW_EQUALS:+-e}
349		${IGNORE_UNKNOWNS:+-i}
350		$( [ "$SHOW_NAME"  ] || echo \ -n )
351		$( [ "$SHOW_VALUE" ] || echo \ -N )
352		$( [ "$SHOW_FILE"  ] && echo \ -F )
353	"
354	if [ "${RC_CONFS+set}" ]; then
355		escape "$RC_CONFS" _RC_CONFS
356		args="$args -f '$_RC_CONFS'"
357		unset _RC_CONFS
358	fi
359	for arg in "$@"; do
360		escape "$arg" arg
361		args="$args '$arg'"
362	done
363
364	#
365	# If both are supplied, `-j jail' supercedes `-R dir'
366	#
367	if [ "$JAIL" ]; then
368		#
369		# Re-execute ourselves 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 "$JAIL" /bin/sh
376		exit $?
377	elif [ "$ROOTDIR" ]; then
378		#
379		# Make sure that the root directory specified is not to any
380		# running jails.
381		#
382		# NOTE: To maintain backward compatibility with older jails on
383		# older systems, we will not perform this check if either the
384		# jls(1) or jexec(8) utilities are missing.
385		#
386		if f_have jexec && f_have jls; then
387			jid="`jls jid path | \
388			(
389				while read JID JROOT; do
390					[ "$JROOT" = "$ROOTDIR" ] || continue
391					echo $JID
392				done
393			)`"
394
395			#
396			# If multiple running jails match the specified root
397			# directory, exit with error.
398			#
399			if [ "$jid" -a "${jid%[$IFS]*}" != "$jid" ]; then
400				die "%s: %s: %s" "$pgm" "$ROOTDIR" \
401				    "$( echo "Multiple jails claim this" \
402				             "directory as their root." \
403				             "(use \`-j jail' instead)" )"
404			fi
405
406			#
407			# If only a single running jail matches the specified
408			# root directory, implicitly use `-j jail'.
409			#
410			if [ "$jid" ]; then
411				#
412				# Re-execute outselves with sh(1) via jexec(8)
413				#
414				( echo set -- $args
415				  jail_depend
416				  cat $0
417				) | env - RC_DEFAULTS="$RC_DEFAULTS" \
418					/usr/sbin/jexec "$jid" /bin/sh
419				exit $?
420			fi
421
422			# Otherwise, fall through and allow chroot(8)
423		fi
424
425		#
426		# Re-execute ourselves with sh(1) via chroot(8)
427		#
428		( echo set -- $args
429		  jail_depend
430		  cat $0
431		) | env - RC_DEFAULTS="$RC_DEFAULTS" \
432		    	/usr/sbin/chroot "$ROOTDIR" /bin/sh
433		exit $?
434	fi
435fi
436
437#
438# Process `-a' or `-A' command-line options
439#
440if [ "$SHOW_ALL" ]; then
441	#
442	# Get a list of variables that are currently set in the rc.conf(5)
443	# files (included `/etc/defaults/rc.conf') by performing a call to
444	# source_rc_confs() in a clean environment.
445	#
446	( # Operate in a sub-shell to protect the parent environment
447		#
448		# Set which variables we want to preserve in the environment.
449		# Append the pipe-character (|) to the list of internal field
450		# separation (IFS) characters, allowing us to use the below
451		# list both as an extended grep (-E) pattern and argument list
452		# (required to first get f_clean_env() to preserve these in the
453		# environment and then later to prune them from the list of
454		# variables produced by set(1)).
455		#
456		IFS="$IFS|"
457		EXCEPT="IFS|EXCEPT|PATH|RC_DEFAULTS|OPTIND|DESCRIBE|SEP"
458		EXCEPT="$EXCEPT|DELETE|SHOW_ALL|SHOW_EQUALS|SHOW_NAME"
459		EXCEPT="$EXCEPT|SHOW_VALUE|SHOW_FILE|VERBOSE|RC_CONFS"
460		EXCEPT="$EXCEPT|pgm|SUCCESS|FAILURE|CHECK_ONLY"
461		EXCEPT="$EXCEPT|f_sysrc_desc_awk|f_sysrc_delete_awk"
462
463		#
464		# Clean the environment (except for our required variables)
465		# and then source the required files.
466		#
467		f_clean_env --except $EXCEPT
468		if [ -f "$RC_DEFAULTS" -a -r "$RC_DEFAULTS" ]; then
469			. "$RC_DEFAULTS"
470
471			#
472			# If passed `-a' (rather than `-A'), re-purge the
473			# environment, removing the rc.conf(5) defaults.
474			#
475			[ "$SHOW_ALL" = "1" ] \
476				&& f_clean_env --except rc_conf_files $EXCEPT
477
478			#
479			# If `-f file' was passed, set $rc_conf_files to an
480			# explicit value, modifying the default behavior of
481			# source_rc_confs().
482			#
483			[ "${RC_CONFS+set}" ] && rc_conf_files="$RC_CONFS"
484
485			source_rc_confs
486
487			#
488			# If passed `-a' (rather than `-A'), remove
489			# `rc_conf_files' unless it was defined somewhere
490			# other than rc.conf(5) defaults.
491			#
492			[ "$SHOW_ALL" = "1" -a \
493			  "$( f_sysrc_find rc_conf_files )" = "$RC_DEFAULTS" \
494			] \
495			&& unset rc_conf_files
496		fi
497
498		for NAME in $( set |
499			awk -F= '/^[[:alpha:]_][[:alnum:]_]*=/ {print $1}' |
500			grep -Ev "^($EXCEPT)$"
501		); do
502			#
503			# If enabled, describe rather than expand value
504			#
505			if [ "$DESCRIBE" ]; then
506				echo "$NAME: $( f_sysrc_desc "$NAME" )"
507				continue
508			fi
509
510			#
511			# If `-F' is passed, find it and move on
512			#
513			if [ "$SHOW_FILE" ]; then
514				[ "$SHOW_NAME" ] && echo -n "$NAME: "
515				f_sysrc_find "$NAME"
516				continue
517			fi
518
519			#
520			# If `-X' is passed, delete the variables
521			#
522			if [ "$DELETE" = "2" ]; then
523				f_sysrc_delete "$NAME"
524				continue
525			fi
526
527			[ "$VERBOSE" ] && \
528				echo -n "$( f_sysrc_find "$NAME" ): "
529
530			#
531			# If `-N' is passed, simplify the output
532			#
533			if [ ! "$SHOW_VALUE" ]; then
534				echo "$NAME"
535				continue
536			fi
537
538			echo "${SHOW_NAME:+$NAME$SEP}$(
539			      f_sysrc_get "$NAME" )${SHOW_EQUALS:+\"}"
540
541		done
542	)
543
544	#
545	# Ignore the remainder of positional arguments.
546	#
547	exit $SUCCESS
548fi
549
550#
551# Process command-line arguments
552#
553status=$SUCCESS
554while [ $# -gt 0 ]; do
555	NAME="${1%%=*}"
556
557	case "$NAME" in
558	*+) mode=APPEND NAME="${NAME%+}" ;;
559	*-) mode=REMOVE NAME="${NAME%-}" ;;
560	 *) mode=ASSIGN
561	esac
562
563	[ "$DESCRIBE" ] && \
564		echo "$NAME: $( f_sysrc_desc "$NAME" )"
565
566	case "$1" in
567	*=*)
568		#
569		# Like sysctl(8), if both `-d' AND "name=value" is passed,
570		# first describe (done above), then attempt to set
571		#
572
573		# If verbose, prefix line with where the directive lives
574		if [ "$VERBOSE" -a ! "$CHECK_ONLY" ]; then
575			file=$( f_sysrc_find "$NAME" )
576			[ "$file" = "$RC_DEFAULTS" -o ! "$file" ] && \
577				file=$( f_sysrc_get 'rc_conf_files%%[$IFS]*' )
578			if [ "$SHOW_EQUALS" ]; then
579				echo -n ": $file; "
580			else
581				echo -n "$file: "
582			fi
583		fi
584
585		#
586		# If `-x' or `-X' is passed, delete the variable and ignore the
587		# desire to set some value
588		#
589		if [ "$DELETE" ]; then
590			f_sysrc_delete "$NAME" || status=$FAILURE
591			shift 1
592			continue
593		fi
594
595		#
596		# If `-c' is passed, simply compare and move on
597		#
598		if [ "$CHECK_ONLY" ]; then
599			if ! IGNORED=$( f_sysrc_get "$NAME?" ); then
600				status=$FAILURE
601				[ "$VERBOSE" ] &&
602					echo "$NAME: not currently set"
603				shift 1
604				continue
605			fi
606			value=$( f_sysrc_get "$NAME" )
607			if [ "$value" != "${1#*=}" ]; then
608				status=$FAILURE
609				if [ "$VERBOSE" ]; then
610					echo -n "$( f_sysrc_find "$NAME" ): "
611					echo -n "$NAME: would change from "
612					echo "\`$value' to \`${1#*=}'"
613				fi
614			elif [ "$VERBOSE" ]; then
615				echo -n "$( f_sysrc_find "$NAME" ): "
616				echo "$NAME: already set to \`$value'"
617			fi
618			shift 1
619			continue
620		fi
621
622		#
623		# Determine both `before' value and appropriate `new' value
624		#
625		case "$mode" in
626		APPEND)
627			before=$( f_sysrc_get "$NAME" )
628			add="${1#*=}"
629			delim="${add%"${add#?}"}" # first character
630			oldIFS="$IFS"
631			case "$delim" in
632			""|[$IFS]|[a-zA-Z0-9]) delim=" " ;;
633			*) IFS="$delim"
634			esac
635			new="$before"
636			for a in $add; do
637				[ "$a" ] || continue
638				skip=
639				for b in $before; do
640					[ "$b" = "$a" ] && skip=1 break
641				done
642				[ "$skip" ] || new="$new$delim$a"
643			done
644			new="${new#"$delim"}" IFS="$oldIFS"
645			unset add delim oldIFS a skip b
646			[ "$SHOW_FILE" ] && before=$( f_sysrc_find "$NAME" )
647			;;
648		REMOVE)
649			before=$( f_sysrc_get "$NAME" )
650			remove="${1#*=}"
651			delim="${remove%"${remove#?}"}" # first character
652			oldIFS="$IFS"
653			case "$delim" in
654			""|[$IFS]|[a-zA-Z0-9]) delim=" " ;;
655			*) IFS="$delim"
656			esac
657			new=
658			for b in $before; do
659				[ "$b" ] || continue
660				add=1
661				for r in $remove; do
662					[ "$r" = "$b" ] && add= break
663				done
664				[ "$add" ] && new="$new$delim$b"
665			done
666			new="${new#"$delim"}" IFS="$oldIFS"
667			unset remove delim oldIFS b add r
668			[ "$SHOW_FILE" ] && before=$( f_sysrc_find "$NAME" )
669			;;
670		*) # ASSIGN
671			if [ "$SHOW_FILE" ]; then
672				before=$( f_sysrc_find "$NAME" )
673			else
674				before=$( f_sysrc_get "$NAME" )
675			fi
676			new="${1#*=}"
677		esac
678
679		#
680		# If `-N' is passed, simplify the output
681		#
682		if [ ! "$SHOW_VALUE" ]; then
683			echo "$NAME"
684			f_sysrc_set "$NAME" "$new"
685		else
686			if f_sysrc_set "$NAME" "$new"; then
687				if [ "$SHOW_FILE" ]; then
688					after=$( f_sysrc_find "$NAME" )
689				else
690					after=$( f_sysrc_get "$NAME" )
691				fi
692				echo -n "${SHOW_NAME:+$NAME$SEP}"
693				echo -n "$before${SHOW_EQUALS:+\" #}"
694				echo -n " -> ${SHOW_EQUALS:+\"}$after"
695				echo "${SHOW_EQUALS:+\"}"
696			fi
697		fi
698		;;
699	*)
700		if ! IGNORED=$( f_sysrc_get "$NAME?" ); then
701			[ "$IGNORE_UNKNOWNS" -o "$QUIET" ] ||
702				echo "$pgm: unknown variable '$NAME'"
703			shift 1
704			status=$FAILURE
705			continue
706		fi
707
708		# The above check told us what we needed for `-c'
709		if [ "$CHECK_ONLY" ]; then
710			shift 1
711			continue
712		fi
713
714		#
715		# Like sysctl(8), when `-d' is passed, desribe it
716		# (already done above) rather than expanding it
717		#
718
719		if [ "$DESCRIBE" ]; then
720			shift 1
721			continue
722		fi
723
724		#
725		# If `-x' or `-X' is passed, delete the variable
726		#
727		if [ "$DELETE" ]; then
728			f_sysrc_delete "$NAME" || status=$FAILURE
729			shift 1
730			continue
731		fi
732
733		#
734		# If `-F' is passed, find it and move on
735		#
736		if [ "$SHOW_FILE" ]; then
737			[ "$SHOW_NAME" ] && echo -n "$NAME: "
738			f_sysrc_find "$NAME"
739			shift 1
740			continue
741		fi
742
743		if [ "$VERBOSE" ]; then
744			if [ "$SHOW_EQUALS" ]; then
745				echo -n ": $( f_sysrc_find "$NAME" ); "
746			else
747				echo -n "$( f_sysrc_find "$NAME" ): "
748			fi
749		fi
750
751		#
752		# If `-N' is passed, simplify the output
753		#
754		if [ ! "$SHOW_VALUE" ]; then
755			echo "$NAME"
756		else
757			echo "${SHOW_NAME:+$NAME$SEP}$(
758			      f_sysrc_get "$NAME" )${SHOW_EQUALS:+\"}"
759		fi
760	esac
761	shift 1
762done
763
764exit $status # $SUCCESS unless error occurred with either `-c' or `-x'
765
766################################################################################
767# END
768################################################################################
769