sysrc revision 241149
1134361Siedowse#!/bin/sh
2134361Siedowse#-
3134361Siedowse# Copyright (c) 2010-2012 Devin Teske
4134361Siedowse# All rights reserved.
5134361Siedowse#
6134361Siedowse# Redistribution and use in source and binary forms, with or without
7134361Siedowse# modification, are permitted provided that the following conditions
8134361Siedowse# are met:
9134361Siedowse# 1. Redistributions of source code must retain the above copyright
10134361Siedowse#    notice, this list of conditions and the following disclaimer.
11134361Siedowse# 2. Redistributions in binary form must reproduce the above copyright
12134361Siedowse#    notice, this list of conditions and the following disclaimer in the
13134361Siedowse#    documentation and/or other materials provided with the distribution.
14134361Siedowse#
15134361Siedowse# THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
16134361Siedowse# ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING BUT NOT LIMITED TO, THE
17134361Siedowse# IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
18134361Siedowse# ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
19134361Siedowse# FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
20134361Siedowse# DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
21134361Siedowse# OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
22134361Siedowse# HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
23134361Siedowse# LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
24134361Siedowse# OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
25134361Siedowse# SUCH DAMAGE.
26134361Siedowse#
27134361Siedowse# $FreeBSD: head/usr.sbin/sysrc/sysrc 241149 2012-10-03 02:32:47Z dteske $
28134361Siedowse#
29134361Siedowse############################################################ INCLUDES
30134361Siedowse
31134361SiedowseBSDCFG_SHARE="/usr/share/bsdconfig"
32134361Siedowse[ "$_COMMON_SUBR" ] || . $BSDCFG_SHARE/common.subr || exit 1
33134361Siedowse[ "$_SYSRC_SUBR"  ] || f_include $BSDCFG_SHARE/sysrc.subr
34134361Siedowse
35134361Siedowse############################################################ CONFIGURATION
36134361Siedowse
37134361Siedowse#
38134361Siedowse# Default verbosity.
39134361Siedowse#
40134361Siedowse: ${SYSRC_VERBOSE:=}
41134361Siedowse
42134361Siedowse############################################################ GLOBALS
43134361Siedowse
44134361Siedowse#
45134361Siedowse# Options
46134361Siedowse#
47134361SiedowseDELETE=
48134361SiedowseDESCRIBE=
49134361SiedowseIGNORE_UNKNOWNS=
50134361SiedowseJAIL=
51134361SiedowseQUIET=
52134361SiedowseROOTDIR=
53134361SiedowseSHOW_ALL=
54134361SiedowseSHOW_EQUALS=
55134361SiedowseSHOW_FILE=
56134361SiedowseSHOW_NAME=1
57134361SiedowseSHOW_VALUE=1
58134361Siedowse
59134361Siedowse############################################################ FUNCTIONS
60134361Siedowse
61134361Siedowse# die [ $fmt [ $opts ... ]]
62134361Siedowse#
63134361Siedowse# Optionally print a message to stderr before exiting with failure status.
64134361Siedowse#
65134361Siedowsedie()
66134361Siedowse{
67134361Siedowse	local fmt="$1"
68134361Siedowse	[ $# -gt 0 ] && shift 1
69134361Siedowse	[  "$fmt"  ] && f_err "$fmt\n" "$@"
70134361Siedowse
71134361Siedowse	exit $FAILURE
72134361Siedowse}
73134361Siedowse
74134361Siedowse# usage
75134361Siedowse#
76134361Siedowse# Prints a short syntax statement and exits.
77134361Siedowse#
78134361Siedowseusage()
79134361Siedowse{
80134361Siedowse	f_err "Usage: %s [OPTIONS] name[=value] ...\n" "$pgm"
81134361Siedowse	f_err "Try \`%s --help' for more information.\n" "$pgm"
82134361Siedowse	die
83134361Siedowse}
84134361Siedowse
85134361Siedowse# help
86134361Siedowse#
87134361Siedowse# Prints a full syntax statement and exits.
88134361Siedowse#
89134361Siedowsehelp()
90134361Siedowse{
91134361Siedowse	local optfmt="\t%-11s%s\n"
92134361Siedowse	local envfmt="\t%-17s%s\n"
93134361Siedowse
94134361Siedowse	f_err "Usage: %s [OPTIONS] name[=value] ...\n" "$pgm"
95134361Siedowse
96134361Siedowse	f_err "OPTIONS:\n"
97134361Siedowse	f_err "$optfmt" "-a" \
98134361Siedowse	      "Dump a list of all non-default configuration variables."
99134361Siedowse	f_err "$optfmt" "-A" \
100134361Siedowse	      "Dump a list of all configuration variables (incl. defaults)."
101134361Siedowse	f_err "$optfmt" "-d" \
102134361Siedowse	      "Print a description of the given variable."
103134361Siedowse	f_err "$optfmt" "-D" \
104134361Siedowse	      "Show default value(s) only (this is the same as setting"
105134361Siedowse	f_err "$optfmt" "" \
106134361Siedowse	      "RC_CONFS to NULL or passing \`-f' with a NULL file-argument)."
107134361Siedowse	f_err "$optfmt" "-e" \
108134361Siedowse	      "Print query results as \`var=value' (useful for producing"
109134361Siedowse	f_err "$optfmt" "" \
110134361Siedowse	      "output to be fed back in). Ignored if \`-n' is specified."
111134361Siedowse	f_err "$optfmt" "-f file" \
112134361Siedowse	      "Operate on the specified file(s) instead of rc_conf_files."
113134361Siedowse	f_err "$optfmt" "" \
114134361Siedowse	      "Can be specified multiple times for additional files."
115153504Smarcel	f_err "$optfmt" "-F" \
116134361Siedowse	      "Show only the last rc.conf(5) file each directive is in."
117134361Siedowse	f_err "$optfmt" "-h" \
118134361Siedowse	      "Print a short usage statement to stderr and exit."
119134361Siedowse	f_err "$optfmt" "--help" \
120134361Siedowse	      "Print this message to stderr and exit."
121134361Siedowse	f_err "$optfmt" "-i" \
122134361Siedowse	      "Ignore unknown variables."
123134361Siedowse	f_err "$optfmt" "-j jail" \
124134361Siedowse	      "The jid or name of the jail to operate within (overrides"
125134361Siedowse	f_err "$optfmt" "" \
126134361Siedowse	      "\`-R dir'; requires jexec(8))."
127134361Siedowse	f_err "$optfmt" "-n" \
128134361Siedowse	      "Show only variable values, not their names."
129134361Siedowse	f_err "$optfmt" "-N" \
130134361Siedowse	      "Show only variable names, not their values."
131134361Siedowse	f_err "$optfmt" "-q" \
132134361Siedowse	      "Quiet. Ignore previous \`-v' and/or SYSRC_VERBOSE."
133134361Siedowse	f_err "$optfmt" "-R dir" \
134134361Siedowse	      "Operate within the root directory \`dir' rather than \`/'."
135186826Sluigi	f_err "$optfmt" "-v" \
136134361Siedowse	      "Verbose. Print the pathname of the specific rc.conf(5)"
137134361Siedowse	f_err "$optfmt" "" \
138134361Siedowse	      "file where the directive was found."
139134361Siedowse	f_err "$optfmt" "-x" \
140134361Siedowse	      "Remove variable(s) from specified file(s)."
141134361Siedowse	f_err "\n"
142134361Siedowse
143134361Siedowse	f_err "ENVIRONMENT:\n"
144134361Siedowse	f_err "$envfmt" "RC_CONFS" \
145134361Siedowse	      "Override default rc_conf_files (even if set to NULL)."
146134361Siedowse	f_err "$envfmt" "RC_DEFAULTS" \
147134361Siedowse	      "Location of \`/etc/defaults/rc.conf' file."
148134361Siedowse	f_err "$envfmt" "SYSRC_VERBOSE" \
149134361Siedowse	      "Default verbosity. Set to non-NULL to enable."
150134361Siedowse
151134361Siedowse	die
152134361Siedowse}
153134361Siedowse
154134361Siedowse# jail_depend
155134361Siedowse#
156134361Siedowse# Dump dependencies such as language-file variables and include files to stdout
157134361Siedowse# to be piped-into sh(1) running via jexec(8)/chroot(8). As a security measure,
158134361Siedowse# this prevents existing language files and library files from being loaded in
159134361Siedowse# the jail. This also relaxes the requirement to have these files in every jail
160134361Siedowse# before sysrc can be used on said jail.
161134361Siedowse#
162134361Siedowsejail_depend()
163134361Siedowse{
164134361Siedowse	#
165134361Siedowse	# Indicate that we are jailed
166134361Siedowse	#
167134361Siedowse	echo export _SYSRC_JAILED=1
168134361Siedowse
169134361Siedowse	#
170134361Siedowse	# Print i18n language variables (their current values are sanitized
171134361Siedowse	# and re-printed for interpretation so that the i18n language files
172134361Siedowse	# do not need to exist within the jail).
173134361Siedowse	#
174134361Siedowse	local var val
175134361Siedowse	for var in \
176134361Siedowse		msg_cannot_create_permission_denied \
177134361Siedowse		msg_permission_denied \
178153504Smarcel		msg_previous_syntax_errors \
179134361Siedowse	; do
180134361Siedowse		val=$( eval echo \"\$$var\" |
181134361Siedowse			awk '{ gsub(/'\''/, "'\''\\'\'\''"); print }' )
182186826Sluigi		echo $var="'$val'"
183134361Siedowse	done
184134361Siedowse
185134361Siedowse	#
186134361Siedowse	# Print include dependencies
187134361Siedowse	#
188134361Siedowse	cat $BSDCFG_SHARE/common.subr
189134361Siedowse	cat $BSDCFG_SHARE/sysrc.subr
190134361Siedowse}
191134361Siedowse
192134361Siedowse############################################################ MAIN SOURCE
193134361Siedowse
194134361Siedowse#
195134361Siedowse# Perform sanity checks
196134361Siedowse#
197134361Siedowse[ $# -gt 0 ] || usage
198134361Siedowse
199134361Siedowse#
200134361Siedowse# Check for `--help' command-line option
201134361Siedowse#
202134361Siedowse( # Operate in sub-shell to protect $@ in parent
203134361Siedowse	while [ $# -gt 0 ]; do
204134361Siedowse		case "$1" in
205134361Siedowse		--help) exit 1;;
206134361Siedowse		-[fRj]) # These flags take an argument
207134361Siedowse			shift 1;;
208134361Siedowse		esac
209134361Siedowse		shift 1
210134361Siedowse	done
211134361Siedowse	exit 0
212134361Siedowse) || help
213134361Siedowse
214134361Siedowse#
215134361Siedowse# Process command-line flags
216134361Siedowse#
217134361Siedowsewhile getopts aAdDef:Fhij:nNqR:vxX flag; do
218134361Siedowse	case "$flag" in
219134361Siedowse	a) SHOW_ALL=${SHOW_ALL:-1};;
220134361Siedowse	A) SHOW_ALL=2;;
221134361Siedowse	d) DESCRIBE=1;;
222134361Siedowse	D) RC_CONFS=;;
223134361Siedowse	e) SHOW_EQUALS=1;;
224134361Siedowse	f) RC_CONFS="$RC_CONFS${RC_CONFS:+ }$OPTARG";;
225134361Siedowse	F) SHOW_FILE=1;;
226134361Siedowse	h) usage;;
227134361Siedowse	i) IGNORE_UNKNOWNS=1;;
228134361Siedowse	j) [ "$OPTARG" ] || die \
229134361Siedowse	   	"%s: Missing or null argument to \`-j' flag" "$pgm"
230134361Siedowse	   JAIL="$OPTARG";;
231134361Siedowse	n) SHOW_NAME=;;
232134361Siedowse	N) SHOW_VALUE=;;
233134361Siedowse	q) QUIET=1 SYSRC_VERBOSE=;;
234134361Siedowse	R) [ "$OPTARG" ] || die \
235134361Siedowse	   	"%s: Missing or null argument to \`-R' flag" "$pgm"
236134361Siedowse	   ROOTDIR="$OPTARG";;
237134361Siedowse	v) SYSRC_VERBOSE=1 QUIET=;;
238134361Siedowse	x) DELETE=${DELETE:-1};;
239134361Siedowse	X) DELETE=2;;
240134361Siedowse	\?) usage;;
241134361Siedowse	esac
242134450Siedowsedone
243134361Siedowseshift $(( $OPTIND - 1 ))
244134450Siedowse
245134361Siedowse#
246134361Siedowse# [More] Sanity checks (e.g., "sysrc --")
247134361Siedowse#
248134361Siedowse[ $# -eq 0 -a ! "$SHOW_ALL" ] && usage
249134361Siedowse
250134361Siedowse#
251134361Siedowse# Taint-check all rc.conf(5) files
252134361Siedowse#
253134361Siedowseerrmsg="$pgm: Exiting due to previous syntax errors"
254134361Siedowseif ( : ${RC_CONFS?} ) > /dev/null 2>&1; then
255134361Siedowse	( for i in $RC_CONFS; do
256134450Siedowse	  	[ -e "$i" ] || continue
257134361Siedowse	  	/bin/sh -n "$i" || exit $FAILURE
258134361Siedowse	  done
259134361Siedowse	  exit $SUCCESS
260134361Siedowse	) || die "$errmsg"
261134361Siedowseelse
262134361Siedowse	/bin/sh -n "$RC_DEFAULTS" || die "$errmsg"
263134361Siedowse	( . "$RC_DEFAULTS"
264134450Siedowse	  for i in $rc_conf_files; do
265134450Siedowse	  	[ -e "$i" ] || continue
266134450Siedowse	  	/bin/sh -n "$i" || exit $FAILURE
267134361Siedowse	  done
268134361Siedowse	  exit $SUCCESS
269134361Siedowse	) || die "$errmsg"
270134361Siedowsefi
271134361Siedowse
272134361Siedowse#
273134361Siedowse# Process `-x' (and secret `-X') command-line options
274134361Siedowse#
275134361Siedowseerrmsg="$pgm: \`-x' option incompatible with \`-a'/\`-A' options"
276134361Siedowseerrmsg="$errmsg (use \`-X' to override)"
277134361Siedowseif [ "$DELETE" -a "$SHOW_ALL" ]; then
278134361Siedowse	[ "$DELETE" = "2" ] || die "$errmsg"
279134450Siedowsefi
280134450Siedowse
281134361Siedowse#
282134361Siedowse# Process `-e', `-n', and `-N' command-line options
283134361Siedowse#
284134361SiedowseSEP=': '
285134361Siedowse[ "$SHOW_EQUALS" ] && SEP='="'
286134361Siedowse[ "$SHOW_NAME" ] || SHOW_EQUALS=
287134361Siedowse[ "$SYSRC_VERBOSE" = "0" ] && SYSRC_VERBOSE=
288134361Siedowseif [ ! "$SHOW_VALUE" ]; then
289134361Siedowse	SHOW_NAME=1
290134450Siedowse	SHOW_EQUALS=
291134450Siedowsefi
292134361Siedowse
293134361Siedowse#
294134361Siedowse# Process `-j jail' and `-R dir' command-line options
295134361Siedowse#
296134361Siedowseif [ "$JAIL" -o "$ROOTDIR" ]; then
297134361Siedowse	#
298134361Siedowse	# Reconstruct the arguments that we want to carry-over
299134361Siedowse	#
300134361Siedowse	args="
301134361Siedowse		${SYSRC_VERBOSE:+-v}
302134361Siedowse		${QUIET:+-q}
303134361Siedowse		$( [ "$DELETE" = "1" ] && echo \ -x )
304134361Siedowse		$( [ "$DELETE" = "2" ] && echo \ -X )
305134361Siedowse		$( [ "$SHOW_ALL" = "1" ] && echo \ -a )
306134361Siedowse		$( [ "$SHOW_ALL" = "2" ] && echo \ -A )
307134361Siedowse		${DESCRIBE:+-d}
308134361Siedowse		${SHOW_EQUALS:+-e}
309134361Siedowse		${IGNORE_UNKNOWNS:+-i}
310134361Siedowse		$( [ "$SHOW_NAME"  ] || echo \ -n )
311134361Siedowse		$( [ "$SHOW_VALUE" ] || echo \ -N )
312134361Siedowse		$( [ "$SHOW_FILE"  ] && echo \ -F )
313134361Siedowse	"
314134361Siedowse	if ( : ${RC_CONFS?} ) > /dev/null 2>&1; then
315134361Siedowse		args="$args -f '$RC_CONFS'"
316134361Siedowse	fi
317134361Siedowse	for arg in "$@"; do
318134361Siedowse		args="$args '$arg'"
319134361Siedowse	done
320134361Siedowse
321134361Siedowse	#
322134361Siedowse	# If both are supplied, `-j jail' supercedes `-R dir'
323134361Siedowse	#
324134361Siedowse	if [ "$JAIL" ]; then
325134361Siedowse		#
326134361Siedowse		# Re-execute ourselves with sh(1) via jexec(8)
327134361Siedowse		#
328134361Siedowse		( echo set -- $args
329134361Siedowse		  jail_depend
330134361Siedowse		  cat $0
331134361Siedowse		) | env - RC_DEFAULTS="$RC_DEFAULTS" \
332134361Siedowse		    	/usr/sbin/jexec "$JAIL" /bin/sh
333134361Siedowse		exit $?
334134361Siedowse	elif [ "$ROOTDIR" ]; then
335134361Siedowse		#
336134373Siedowse		# Make sure that the root directory specified is not to any
337154251Sjasone		# running jails.
338154251Sjasone		#
339134361Siedowse		# NOTE: To maintain backward compatibility with older jails on
340134361Siedowse		# older systems, we will not perform this check if either the
341134361Siedowse		# jls(1) or jexec(8) utilities are missing.
342134361Siedowse		#
343134361Siedowse		if f_have jexec && f_have jls; then
344134361Siedowse			jid="`jls jid path | \
345134361Siedowse			(
346251440Sdelphij				while read JID JROOT; do
347134361Siedowse					[ "$JROOT" = "$ROOTDIR" ] || continue
348134361Siedowse					echo $JID
349134361Siedowse				done
350134361Siedowse			)`"
351134361Siedowse
352134361Siedowse			#
353134361Siedowse			# If multiple running jails match the specified root
354134361Siedowse			# directory, exit with error.
355134361Siedowse			#
356134361Siedowse			if [ "$jid" -a "${jid%[$IFS]*}" != "$jid" ]; then
357134361Siedowse				die "%s: %s: %s" "$pgm" "$ROOTDIR" \
358134361Siedowse				    "$( echo "Multiple jails claim this" \
359134361Siedowse				             "directory as their root." \
360134361Siedowse				             "(use \`-j jail' instead)" )"
361134361Siedowse			fi
362134361Siedowse
363134361Siedowse			#
364134361Siedowse			# If only a single running jail matches the specified
365134361Siedowse			# root directory, implicitly use `-j jail'.
366134361Siedowse			#
367134361Siedowse			if [ "$jid" ]; then
368134361Siedowse				#
369134361Siedowse				# Re-execute outselves with sh(1) via jexec(8)
370134361Siedowse				#
371134361Siedowse				( echo set -- $args
372134361Siedowse				  jail_depend
373134361Siedowse				  cat $0
374134361Siedowse				) | env - RC_DEFAULTS="$RC_DEFAULTS" \
375134361Siedowse					/usr/sbin/jexec "$jid" /bin/sh
376134361Siedowse				exit $?
377134361Siedowse			fi
378134361Siedowse
379134373Siedowse			# Otherwise, fall through and allow chroot(8)
380134361Siedowse		fi
381134361Siedowse
382134361Siedowse		#
383134373Siedowse		# Re-execute ourselves with sh(1) via chroot(8)
384134361Siedowse		#
385134361Siedowse		( echo set -- $args
386134361Siedowse		  jail_depend
387134361Siedowse		  cat $0
388134361Siedowse		) | env - RC_DEFAULTS="$RC_DEFAULTS" \
389134361Siedowse		    	/usr/sbin/chroot "$ROOTDIR" /bin/sh
390134361Siedowse		exit $?
391134361Siedowse	fi
392134361Siedowsefi
393134361Siedowse
394134361Siedowse#
395134361Siedowse# Process `-a' or `-A' command-line options
396134361Siedowse#
397134361Siedowseif [ "$SHOW_ALL" ]; then
398134361Siedowse	#
399134361Siedowse	# Get a list of variables that are currently set in the rc.conf(5)
400134361Siedowse	# files (included `/etc/defaults/rc.conf') by performing a call to
401134361Siedowse	# source_rc_confs() in a clean environment.
402134361Siedowse	#
403134361Siedowse	( # Operate in a sub-shell to protect the parent environment
404134361Siedowse		#
405134361Siedowse		# Set which variables we want to preserve in the environment.
406134361Siedowse		# Append the pipe-character (|) to the list of internal field
407134361Siedowse		# separation (IFS) characters, allowing us to use the below
408134361Siedowse		# list both as an extended grep (-E) pattern and argument list
409134361Siedowse		# (required to first get f_clean_env() to preserve these in the
410134361Siedowse		# environment and then later to prune them from the list of
411134361Siedowse		# variables produced by set(1)).
412134361Siedowse		#
413134361Siedowse		IFS="$IFS|"
414134361Siedowse		EXCEPT="IFS|EXCEPT|PATH|RC_DEFAULTS|OPTIND|DESCRIBE|SEP"
415134361Siedowse		EXCEPT="$EXCEPT|DELETE|SHOW_ALL|SHOW_EQUALS|SHOW_NAME"
416134361Siedowse		EXCEPT="$EXCEPT|SHOW_VALUE|SHOW_FILE|SYSRC_VERBOSE|RC_CONFS"
417134361Siedowse		EXCEPT="$EXCEPT|pgm|SUCCESS|FAILURE"
418134361Siedowse		EXCEPT="$EXCEPT|f_sysrc_desc_awk|f_sysrc_delete_awk"
419134361Siedowse
420134361Siedowse		#
421134361Siedowse		# Clean the environment (except for our required variables)
422134361Siedowse		# and then source the required files.
423134361Siedowse		#
424134361Siedowse		f_clean_env --except $EXCEPT
425134361Siedowse		if [ -f "$RC_DEFAULTS" -a -r "$RC_DEFAULTS" ]; then
426134361Siedowse			. "$RC_DEFAULTS"
427134361Siedowse
428134361Siedowse			#
429134361Siedowse			# If passed `-a' (rather than `-A'), re-purge the
430134361Siedowse			# environment, removing the rc.conf(5) defaults.
431134361Siedowse			#
432134361Siedowse			[ "$SHOW_ALL" = "1" ] \
433134361Siedowse				&& f_clean_env --except rc_conf_files $EXCEPT
434134361Siedowse
435134361Siedowse			#
436134361Siedowse			# If `-f file' was passed, set $rc_conf_files to an
437134361Siedowse			# explicit value, modifying the default behavior of
438134361Siedowse			# source_rc_confs().
439134361Siedowse			#
440134361Siedowse			( : ${RC_CONFS?} ) > /dev/null 2>&1 &&
441134361Siedowse				rc_conf_files="$RC_CONFS"
442134361Siedowse
443134361Siedowse			source_rc_confs
444134361Siedowse
445134361Siedowse			#
446134361Siedowse			# If passed `-a' (rather than `-A'), remove
447134361Siedowse			# `rc_conf_files' unless it was defined somewhere
448134361Siedowse			# other than rc.conf(5) defaults.
449134361Siedowse			#
450134361Siedowse			[ "$SHOW_ALL" = "1" -a \
451134361Siedowse			  "$( f_sysrc_find rc_conf_files )" = "$RC_DEFAULTS" \
452134361Siedowse			] \
453134361Siedowse			&& unset rc_conf_files
454134361Siedowse		fi
455134361Siedowse
456134361Siedowse		for NAME in $( set |
457134361Siedowse			awk -F= '/^[[:alpha:]_][[:alnum:]_]*=/ {print $1}' |
458134361Siedowse			grep -Ev "^($EXCEPT)$"
459134361Siedowse		); do
460134361Siedowse			#
461134361Siedowse			# If enabled, describe rather than expand value
462134361Siedowse			#
463134361Siedowse			if [ "$DESCRIBE" ]; then
464134361Siedowse				echo "$NAME: $( f_sysrc_desc "$NAME" )"
465134361Siedowse				continue
466134361Siedowse			fi
467134361Siedowse
468134361Siedowse			#
469154251Sjasone			# If `-F' is passed, find it and move on
470134361Siedowse			#
471134361Siedowse			if [ "$SHOW_FILE" ]; then
472134361Siedowse				[ "$SHOW_NAME" ] && echo -n "$NAME: "
473134361Siedowse				f_sysrc_find "$NAME"
474134361Siedowse				continue
475134361Siedowse			fi
476154251Sjasone
477154251Sjasone			#
478134361Siedowse			# If `-X' is passed, delete the variables
479134361Siedowse			#
480134361Siedowse			if [ "$DELETE" = "2" ]; then
481134361Siedowse				f_sysrc_delete "$NAME"
482134361Siedowse				continue
483134361Siedowse			fi
484134361Siedowse
485134361Siedowse			[ "$SYSRC_VERBOSE" ] && \
486134361Siedowse				echo -n "$( f_sysrc_find "$NAME" ): "
487154251Sjasone
488154251Sjasone			#
489134361Siedowse			# If `-N' is passed, simplify the output
490134361Siedowse			#
491134361Siedowse			if [ ! "$SHOW_VALUE" ]; then
492134361Siedowse				echo "$NAME"
493134361Siedowse				continue
494134361Siedowse			fi
495134361Siedowse
496134361Siedowse			echo "${SHOW_NAME:+$NAME$SEP}$(
497134361Siedowse			      f_sysrc_get "$NAME" )${SHOW_EQUALS:+\"}"
498134361Siedowse
499134361Siedowse		done
500134361Siedowse	)
501134361Siedowse
502134361Siedowse	#
503134361Siedowse	# Ignore the remainder of positional arguments.
504134361Siedowse	#
505134361Siedowse	exit $SUCCESS
506134361Siedowsefi
507134361Siedowse
508134361Siedowse#
509134361Siedowse# Process command-line arguments
510134361Siedowse#
511134361Siedowsewhile [ $# -gt 0 ]; do
512134361Siedowse	NAME="${1%%=*}"
513134361Siedowse
514134361Siedowse	[ "$DESCRIBE" ] && \
515134361Siedowse		echo "$NAME: $( f_sysrc_desc "$NAME" )"
516134361Siedowse
517134361Siedowse	case "$1" in
518134361Siedowse	*=*)
519134361Siedowse		#
520134361Siedowse		# Like sysctl(8), if both `-d' AND "name=value" is passed,
521134361Siedowse		# first describe, then attempt to set
522134361Siedowse		#
523134361Siedowse
524134361Siedowse		if [ "$SYSRC_VERBOSE" ]; then
525134361Siedowse			file=$( f_sysrc_find "$NAME" )
526134361Siedowse			[ "$file" = "$RC_DEFAULTS" -o ! "$file" ] && \
527134361Siedowse				file=$( f_sysrc_get 'rc_conf_files%%[$IFS]*' )
528134361Siedowse			echo -n "$file: "
529134361Siedowse		fi
530134361Siedowse
531134361Siedowse		#
532134361Siedowse		# If `-x' or `-X' is passed, delete the variable and ignore the
533134361Siedowse		# desire to set some value
534134361Siedowse		#
535134361Siedowse		if [ "$DELETE" ]; then
536134361Siedowse			f_sysrc_delete "$NAME"
537134361Siedowse			shift 1
538134361Siedowse			continue
539134361Siedowse		fi
540134361Siedowse
541134361Siedowse		#
542134361Siedowse		# If `-N' is passed, simplify the output
543134361Siedowse		#
544134361Siedowse		if [ ! "$SHOW_VALUE" ]; then
545134361Siedowse			echo "$NAME"
546134361Siedowse			f_sysrc_set "$NAME" "${1#*}"
547134361Siedowse		else
548134361Siedowse			if [ "$SHOW_FILE" ]; then
549134361Siedowse				before=$( f_sysrc_find "$NAME" )
550134361Siedowse			else
551134361Siedowse				before=$( f_sysrc_get "$NAME" )
552134361Siedowse			fi
553134361Siedowse			if f_sysrc_set "$NAME" "${1#*=}"; then
554134361Siedowse				if [ "$SHOW_FILE" ]; then
555134361Siedowse					after=$( f_sysrc_find "$NAME" )
556134361Siedowse					echo -n "${SHOW_NAME:+$NAME$SEP}"
557134361Siedowse					echo -n "$before${SHOW_EQUALS:+\"}"
558134361Siedowse					echo " -> $after"
559134361Siedowse				else
560134361Siedowse					after=$( f_sysrc_get "$NAME" )
561134361Siedowse					echo -n "${SHOW_NAME:+$NAME$SEP}"
562134361Siedowse					echo "$before -> $after"
563134361Siedowse				fi
564134361Siedowse			fi
565134361Siedowse		fi
566134361Siedowse		;;
567134361Siedowse	*)
568134361Siedowse		if ! IGNORED="$( f_sysrc_get "$NAME?" )"; then
569134361Siedowse			[ "$IGNORE_UNKNOWNS" ] \
570134361Siedowse				|| echo "$pgm: unknown variable '$NAME'"
571134361Siedowse			shift 1
572134361Siedowse			continue
573134361Siedowse		fi
574134361Siedowse
575134361Siedowse		#
576134361Siedowse		# Like sysctl(8), when `-d' is passed,
577134361Siedowse		# desribe it rather than expanding it
578134361Siedowse		#
579134361Siedowse
580134361Siedowse		if [ "$DESCRIBE" ]; then
581134361Siedowse			shift 1
582134361Siedowse			continue
583134361Siedowse		fi
584134361Siedowse
585134361Siedowse		#
586134361Siedowse		# If `-x' or `-X' is passed, delete the variable
587134361Siedowse		#
588134361Siedowse		if [ "$DELETE" ]; then
589134361Siedowse			f_sysrc_delete "$NAME"
590134361Siedowse			shift 1
591134361Siedowse			continue
592134361Siedowse		fi
593134361Siedowse
594134361Siedowse		#
595134361Siedowse		# If `-F' is passed, find it and move on
596134361Siedowse		#
597134361Siedowse		if [ "$SHOW_FILE" ]; then
598134361Siedowse			[ "$SHOW_NAME" ] && echo -n "$NAME: "
599134361Siedowse			f_sysrc_find "$NAME"
600134361Siedowse			shift 1
601134361Siedowse			continue
602134361Siedowse		fi
603134361Siedowse
604134361Siedowse		[ "$SYSRC_VERBOSE" ] && \
605134361Siedowse			echo -n "$( f_sysrc_find "$NAME" ): "
606134361Siedowse
607		#
608		# If `-N' is passed, simplify the output
609		#
610		if [ ! "$SHOW_VALUE" ]; then
611			echo "$NAME"
612		else
613			echo "${SHOW_NAME:+$NAME$SEP}$(
614			      f_sysrc_get "$NAME" )${SHOW_EQUALS:+\"}"
615		fi
616	esac
617	shift 1
618done
619