--- sysrc-3.2.2.txt 2012-01-03 17:17:52.000000000 +0000 +++ sysrc-4.0.txt 2012-08-22 23:49:25.647444381 +0000 @@ -2,8 +2,8 @@ # -*- tab-width: 4 -*- ;; Emacs # vi: set tabstop=4 :: Vi/ViM # -# Revision: 3.2.2 -# Last Modified: January 3rd, 2012 +# Revision: 4.0 +# Last Modified: August 22nd, 2012 ############################################################ COPYRIGHT # # (c)2010-2012. Devin Teske. All Rights Reserved. @@ -30,6 +30,11 @@ # SUCH DAMAGE. # # AUTHOR DATE DESCRIPTION +# dteske 2012.08.22 Minor changes in-preparation of import to HEAD. +# dteske 2012.08.21 Shorten default usage statement and add `--help'. +# dteske 2012.08.21 Allow `-f file' or RC_CONFS to be NULL (show defaults). +# dteske 2012.08.21 Add `-q' flag to be quiet even if SYSRC_VERBOSE is set. +# dteske 2012.08.21 Add `-F' flag for querying which file something is in. # dteske 2012.01.03 Minor bug fix to sysrc_delete (s/chmod/chown/). # dteske 2011.12.30 Minor bug fix for FreeBSD 9.0-CURRENT. # dteske 2011.01.15 Make `-A' override `-a' despite order-of-appearance. @@ -60,25 +65,29 @@ # sysrc [OPTIONS] name[=value] ... # # OPTIONS: -# -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 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. -# -v Verbose. Print the pathname of the specific rc.conf(5) -# file where the directive was found. +# -f file Operate on the specified file(s) instead of rc_conf_files. +# Can be specified multiple times for additional files. +# -F Show only the last rc.conf(5) file each directive is in. +# -h Print a short usage statement to stderr and exit. +# --help Print this message to stderr and exit. # -i Ignore unknown variables. +# -j jail The jid or name of the jail to operate within (overrides +# `-R dir'; requires jexec(8)). # -n Show only variable values, not their names. # -N Show only variable names, not their values. +# -q Quiet. Ignore previous `-v' and/or SYSRC_VERBOSE. # -R dir Operate within the root directory `dir' rather than `/'. -# -j jail The jid or name of the jail to operate within (overrides -# `-R dir'; requires jexec(8)). +# -v Verbose. Print the pathname of the specific rc.conf(5) +# file where the directive was found. +# -x Remove variable(s) from specified file(s). # # ENVIRONMENT: +# RC_CONFS Override default rc_conf_files (even if set to NULL). # RC_DEFAULTS Location of `/etc/defaults/rc.conf' file. # SYSRC_VERBOSE Default verbosity. Set to non-NULL to enable. # @@ -124,7 +133,7 @@ FAILURE=1 # # Program name # -progname="${0##*/}" +pgm="${0##*/}" # # Options @@ -133,45 +142,49 @@ DELETE= DESCRIBE= IGNORE_UNKNOWNS= JAIL= -RC_CONFS= +QUIET= ROOTDIR= SHOW_ALL= SHOW_EQUALS= +SHOW_FILE= SHOW_NAME=1 SHOW_VALUE=1 +# +# Non-i18n error strings +# +msg_cannot_create_permission_denied="%s: cannot create %s: Permission denied" +msg_permission_denied="%s: %s: Permission denied" +msg_previous_syntax_errors="%s: Not overwriting \`%s' due to previous syntax errors" + ############################################################ FUNCTIONS -# have $anything +# f_quietly $command [ $arguments ... ] # -# A wrapper to the `type' built-in. Returns true if argument is a valid shell -# built-in, keyword, or externally-tracked binary, otherwise false. +# Run a command quietly (quell any output to stdout or stderr) # -have() +f_quietly() { - type "$@" > /dev/null 2>&1 + "$@" > /dev/null 2>&1 } -# fprintf $fd $fmt [ $opts ... ] +# f_have $anything ... # -# Like printf, except allows you to print to a specific file-descriptor. Useful -# for printing to stderr (fd=2) or some other known file-descriptor. +# A wrapper to the `type' built-in. Returns true if argument is a valid shell +# built-in, keyword, or externally-tracked binary, otherwise false. # -fprintf() +f_have() { - local fd=$1 - [ $# -gt 1 ] || return $FAILURE - shift 1 - printf "$@" >&$fd + f_quietly type "$@" } -# eprintf $fmt [ $opts ... ] +# f_err $fmt [ $opts ... ] # # Print a message to stderr (fd=2). # -eprintf() +f_err() { - fprintf 2 "$@" + printf "$@" >&2 } # die [ $fmt [ $opts ... ]] @@ -182,7 +195,7 @@ die() { local fmt="$1" [ $# -gt 0 ] && shift 1 - [ "$fmt" ] && eprintf "$fmt\n" "$@" + [ "$fmt" ] && f_err "$fmt\n" "$@" exit $FAILURE } @@ -193,58 +206,77 @@ die() # usage() { + f_err "Usage: %s [OPTIONS] name[=value] ...\n" "$pgm" + f_err "Try \`%s --help' for more information.\n" "$pgm" + die +} + +# help +# +# Prints a full syntax statement and exits. +# +help() +{ local optfmt="\t%-11s%s\n" local envfmt="\t%-17s%s\n" - eprintf "Usage: %s [OPTIONS] name[=value] ...\n" "$progname" + f_err "Usage: %s [OPTIONS] name[=value] ...\n" "$pgm" - eprintf "OPTIONS:\n" - eprintf "$optfmt" "-h" \ - "Print this message to stderr and exit." - eprintf "$optfmt" "-f file" \ - "Operate on the specified file(s) instead of rc_conf_files." - eprintf "$optfmt" "" \ - "Can be specified multiple times for additional files." - eprintf "$optfmt" "-a" \ - "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." - eprintf "$optfmt" "-v" \ - "Verbose. Print the pathname of the specific rc.conf(5)" - eprintf "$optfmt" "" \ - "file where the directive was found." - eprintf "$optfmt" "-i" \ - "Ignore unknown variables." - eprintf "$optfmt" "-n" \ - "Show only variable values, not their names." - eprintf "$optfmt" "-N" \ - "Show only variable names, not their values." - eprintf "$optfmt" "-R dir" \ - "Operate within the root directory \`dir' rather than \`/'." - eprintf "$optfmt" "-j jail" \ - "The jid or name of the jail to operate within (overrides" - eprintf "$optfmt" "" \ - "\`-R dir'; requires jexec(8))." - eprintf "\n" - - eprintf "ENVIRONMENT:\n" - eprintf "$envfmt" "RC_DEFAULTS" \ - "Location of \`/etc/defaults/rc.conf' file." - eprintf "$envfmt" "SYSRC_VERBOSE" \ - "Default verbosity. Set to non-NULL to enable." + f_err "OPTIONS:\n" + f_err "$optfmt" "-a" \ + "Dump a list of all non-default configuration variables." + f_err "$optfmt" "-A" \ + "Dump a list of all configuration variables (incl. defaults)." + f_err "$optfmt" "-d" \ + "Print a description of the given variable." + f_err "$optfmt" "-e" \ + "Print query results as \`var=value' (useful for producing" + f_err "$optfmt" "" \ + "output to be fed back in). Ignored if \`-n' is specified." + f_err "$optfmt" "-f file" \ + "Operate on the specified file(s) instead of rc_conf_files." + f_err "$optfmt" "" \ + "Can be specified multiple times for additional files." + f_err "$optfmt" "-F" \ + "Show only the last rc.conf(5) file each directive is in." + f_err "$optfmt" "-h" \ + "Print a short usage statement to stderr and exit." + f_err "$optfmt" "--help" \ + "Print this message to stderr and exit." + f_err "$optfmt" "-i" \ + "Ignore unknown variables." + f_err "$optfmt" "-j jail" \ + "The jid or name of the jail to operate within (overrides" + f_err "$optfmt" "" \ + "\`-R dir'; requires jexec(8))." + f_err "$optfmt" "-n" \ + "Show only variable values, not their names." + f_err "$optfmt" "-N" \ + "Show only variable names, not their values." + f_err "$optfmt" "-q" \ + "Quiet. Ignore previous \`-v' and/or SYSRC_VERBOSE." + f_err "$optfmt" "-R dir" \ + "Operate within the root directory \`dir' rather than \`/'." + f_err "$optfmt" "-v" \ + "Verbose. Print the pathname of the specific rc.conf(5)" + f_err "$optfmt" "" \ + "file where the directive was found." + f_err "$optfmt" "-x" \ + "Remove variable(s) from specified file(s)." + f_err "\n" + + f_err "ENVIRONMENT:\n" + f_err "$envfmt" "RC_CONFS" \ + "Override default rc_conf_files (even if set to NULL)." + f_err "$envfmt" "RC_DEFAULTS" \ + "Location of \`/etc/defaults/rc.conf' file." + f_err "$envfmt" "SYSRC_VERBOSE" \ + "Default verbosity. Set to non-NULL to enable." die } -# clean_env [ --except $varname ... ] +# f_clean_env [ --except $varname ... ] # # Unset all environment variables in the current scope. An optional list of # arguments can be passed, indicating which variables to avoid unsetting; the @@ -255,7 +287,7 @@ usage() # $PATH expansion in, because this will blow $PATH away. This is best used # within a sub-shell block "(...)" or "$(...)" or "`...`". # -clean_env() +f_clean_env() { local var arg except= @@ -294,7 +326,7 @@ clean_env() done } -# sysrc_get $varname +# f_sysrc_get $varname # # Get a system configuration setting from the collection of system- # configuration files (in order: /etc/defaults/rc.conf /etc/rc.conf @@ -305,7 +337,7 @@ clean_env() # return the hostname up to (but not including) the first `.' (see sh(1), # "Parameter Expansion" for more information on additional formats). # -sysrc_get() +f_sysrc_get() { # Sanity check [ -f "$RC_DEFAULTS" -a -r "$RC_DEFAULTS" ] || return $FAILURE @@ -325,10 +357,13 @@ sysrc_get() # Clear the environment of all variables, preventing the # expansion of normals such as `PS1', `TERM', etc. # - clean_env --except IFS RC_CONFS RC_DEFAULTS + f_clean_env --except IFS RC_CONFS RC_DEFAULTS SUCCESS . "$RC_DEFAULTS" > /dev/null 2>&1 + unset RC_DEFAULTS + # no longer needed + # # If the query is for `rc_conf_files' then store the value that # we inherited from sourcing RC_DEFAULTS (above) so that we may @@ -343,7 +378,14 @@ sysrc_get() # If `-f file' was passed, set $rc_conf_files to an explicit # value, modifying the default behavior of source_rc_confs(). # - [ "$RC_CONFS" ] && rc_conf_files="$RC_CONFS" + ( : ${RC_CONFS?} ) > /dev/null 2>&1 + if [ $? -eq ${SUCCESS:-0} ]; then + rc_conf_files="$RC_CONFS" + _rc_confs_set=1 + fi + + unset SUCCESS + # no longer needed source_rc_confs > /dev/null 2>&1 @@ -356,18 +398,19 @@ sysrc_get() # instead to query the value from the file(s) specified). # if [ "$1" = "rc_conf_files" -a \ - "$RC_CONFS" != "" -a \ + "$_rc_confs_set" -a \ "$rc_conf_files" = "$RC_CONFS" \ ]; then rc_conf_files="$_rc_conf_files" unset _rc_conf_files + unset _rc_confs_set fi unset RC_CONFS # no longer needed # - # This must be the last functional line for both the sub-shell, + # This must be the last functional line for both the sub-shell # and the function to preserve the return status from formats # such as "${varname?}" and "${varname:?}" (see "Parameter # Expansion" in sh(1) for more information). @@ -376,7 +419,7 @@ sysrc_get() ) } -# sysrc_find $varname +# f_sysrc_find $varname # # Find which file holds the effective last-assignment to a given variable # within the rc.conf(5) file(s). @@ -385,11 +428,10 @@ sysrc_get() # the filename it was found in and then returns success. Otherwise output is # NULL and the function returns with error status. # -sysrc_find() +f_sysrc_find() { local varname="$1" - local regex="^[[:space:]]*$varname=" - local rc_conf_files="$( sysrc_get rc_conf_files )" + local rc_conf_files="$( f_sysrc_get rc_conf_files )" local conf_files= local file @@ -400,7 +442,7 @@ sysrc_find() # If `-f file' was passed, set $rc_conf_files to an explicit # value, modifying the default behavior of source_rc_confs(). # - [ "$RC_CONFS" ] && rc_conf_files="$RC_CONFS" + ( : ${RC_CONFS?} ) > /dev/null 2>&1 && rc_conf_files="$RC_CONFS" # # Reverse the order of files in rc_conf_files (the boot process sources @@ -419,6 +461,17 @@ sysrc_find() conf_files="$conf_files${conf_files:+ }$RC_DEFAULTS" # + # Sanitize the variable name into a regular expression to use + # + local regex="^[[:space:]]*" + varname=$( echo "$varname" | awk '{ + sub(/[^[:alnum:]_].*/, "") + print + exit # no multi-line arguments please + }' ) + regex="$regex$varname=" + + # # Find which file matches assignment to the given variable name. # for file in $conf_files; do @@ -432,7 +485,7 @@ sysrc_find() return $FAILURE # Not found } -# sysrc_set $varname $new_value +# f_sysrc_set $varname $new_value # # Change a setting in the system configuration files (edits the files in-place # to change the value in the last assignment to the variable). If the variable @@ -442,7 +495,7 @@ sysrc_find() # 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_set_awk=' +f_sysrc_set_awk=' # Variables that should be defined on the invocation line: # -v varname="varname" # -v new_value="new_value" @@ -501,7 +554,7 @@ BEGIN { } END { exit retval } ' -sysrc_set() +f_sysrc_set() { local varname="$1" new_value="$2" @@ -512,7 +565,7 @@ sysrc_set() # Find which rc.conf(5) file contains the last-assignment # local not_found= - local file="$( sysrc_find "$varname" )" + local file="$( f_sysrc_find "$varname" )" if [ "$file" = "$RC_DEFAULTS" -o ! "$file" ]; then # # We either got a null response (not found) or the variable @@ -526,10 +579,10 @@ sysrc_set() # If `-f file' was passed, use $RC_CONFS # rather than $rc_conf_files. # - if [ "$RC_CONFS" ]; then + if ( : ${RC_CONFS?} ) > /dev/null 2>&1; then file="${RC_CONFS%%[$IFS]*}" else - file=$( sysrc_get 'rc_conf_files%%[$IFS]*' ) + file=$( f_sysrc_get 'rc_conf_files%%[$IFS]*' ) fi fi @@ -538,22 +591,22 @@ sysrc_set() # if [ "$not_found" ]; then echo "$varname=\"$new_value\"" >> "$file" - return $SUCCESS + return $? fi # # Perform sanity checks. # if [ ! -w "$file" ]; then - eprintf "\n%s: cannot create %s: Permission denied\n" \ - "$progname" "$file" + f_err "$msg_cannot_create_permission_denied\n" \ + "$pgm" "$file" return $FAILURE fi # # Create a new temporary file to write to. # - local tmpfile="$( mktemp -t "$progname" )" + local tmpfile="$( mktemp -t "$pgm" )" [ "$tmpfile" ] || return $FAILURE # @@ -562,14 +615,18 @@ sysrc_set() # temporary file over the destination, the destination will inherit the # permissions from the temporary file). # - chmod "$( stat -f '%#Lp' "$file" )" "$tmpfile" 2> /dev/null + local mode + mode=$( stat -f '%#Lp' "$file" 2> /dev/null ) + f_quietly chmod "${mode:-0644}" "$tmpfile" # - # Fixup ownerhsip. The destination file _is_ writable (we tested + # Fixup ownership. The destination file _is_ writable (we tested # earlier above). However, this will fail if we don't have sufficient # permissions (so we throw stderr into the bit-bucket). # - chown "$( stat -f '%u:%g' "$file" )" "$tmpfile" 2> /dev/null + local owner + owner=$( stat -f '%u:%g' "$file" 2> /dev/null ) + f_quietly chown "${owner:-root:wheel}" "$tmpfile" # # Operate on the matching file, replacing only the last occurrence. @@ -577,7 +634,7 @@ sysrc_set() local new_contents retval new_contents=$( tail -r $file 2> /dev/null ) new_contents=$( echo "$new_contents" | awk -v varname="$varname" \ - -v new_value="$new_value" "$sysrc_set_awk" ) + -v new_value="$new_value" "$f_sysrc_set_awk" ) retval=$? # @@ -592,8 +649,7 @@ sysrc_set() # Taint-check our results. # if ! /bin/sh -n "$tmpfile"; then - eprintf "%s: Not overwriting \`%s' due to %s\n" \ - "$progname" "$file" "previous syntax errors" + f_err "$msg_previous_syntax_errors\n" "$pgm" "$file" rm -f "$tmpfile" return $FAILURE fi @@ -604,7 +660,7 @@ sysrc_set() mv "$tmpfile" "$file" } -# sysrc_desc $varname +# f_sysrc_desc $varname # # Attempts to return the comments associated with varname from the rc.conf(5) # defaults file `/etc/defaults/rc.conf' (or whatever RC_DEFAULTS points to). @@ -615,7 +671,7 @@ sysrc_set() # 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_desc_awk=' +f_sysrc_desc_awk=' # Variables that should be defined on the invocation line: # -v varname="varname" # @@ -652,12 +708,12 @@ END { exit ! found } ' -sysrc_desc() +f_sysrc_desc() { - awk -v varname="$1" "$sysrc_desc_awk" < "$RC_DEFAULTS" + awk -v varname="$1" "$f_sysrc_desc_awk" < "$RC_DEFAULTS" } -# sysrc_delete $varname +# f_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 @@ -667,7 +723,7 @@ sysrc_desc() # 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=' +f_sysrc_delete_awk=' # Variables that should be defined on the invocation line: # -v varname="varname" # @@ -683,7 +739,7 @@ BEGIN { } END { exit ! found } ' -sysrc_delete() +f_sysrc_delete() { local varname="$1" local file @@ -694,25 +750,36 @@ sysrc_delete() # # Operate on each of the specified files # - for file in ${RC_CONFS:-$( sysrc_get rc_conf_files )}; do + local files + if ( : ${RC_CONFS?} ) > /dev/null 2>&1; then + files="$RC_CONFS" + else + files=$( f_sysrc_get rc_conf_files ) + fi + for file in $files; do + [ -e "$file" ] || continue + # # Create a new temporary file to write to. # - local tmpfile="$( mktemp -t "$progname" )" + local tmpfile="$( mktemp -t "$pgm" )" [ "$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 - chown "$( stat -f '%u:%g' "$file" )" "$tmpfile" 2> /dev/null + local mode owner + mode=$( stat -f '%#Lp' "$file" 2> /dev/null ) + owner=$( stat -f '%u:%g' "$file" 2> /dev/null ) + f_quietly chmod "${mode:-0644}" "$tmpfile" + f_quietly chown "${owner:-root:wheel}" "$tmpfile" # # Operate on the file, removing all occurrences, saving the # output in our temporary file. # - awk -v varname="$varname" "$sysrc_delete_awk" "$file" \ + awk -v varname="$varname" "$f_sysrc_delete_awk" "$file" \ > "$tmpfile" if [ $? -ne $SUCCESS ]; then # The file didn't contain any assignments @@ -724,42 +791,19 @@ sysrc_delete() # Taint-check our results. # if ! /bin/sh -n "$tmpfile"; then - eprintf "%s: Not overwriting \`%s' due to %s\n" \ - "$progname" "$file" "previous syntax errors" + f_err "$msg_previous_syntax_errors\n" \ + "$pgm" "$file" rm -f "$tmpfile" - continue + return $FAILURE fi # # Perform sanity checks # if [ ! -w "$file" ]; then - eprintf "%s: %s: Permission denied\n" \ - "$progname" "$file" + f_err "$msg_permission_denied\n" "$pgm" "$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:+\"}" + return $FAILURE fi # @@ -777,52 +821,81 @@ sysrc_delete() [ $# -gt 0 ] || usage # -# Process command-line options +# Check for `--help' command-line option # -while getopts hf:aAxXdevinNR:j: flag; do +( # Operate in sub-shell to protect $@ in parent + while [ $# -gt 0 ]; do + case "$1" in + --help) exit 1;; + -[fRj]) # These flags take an argument + shift 1;; + esac + shift 1 + done + exit 0 +) || help + +# +# Process command-line flags +# +while getopts hf:FaAxXdevinNR:j:q 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";; + f) RC_CONFS="$RC_CONFS${RC_CONFS:+ }$OPTARG";; + F) SHOW_FILE=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;; + v) SYSRC_VERBOSE=1 QUIET=;; i) IGNORE_UNKNOWNS=1;; n) SHOW_NAME=;; N) SHOW_VALUE=;; R) [ "$OPTARG" ] || die \ - "%s: Missing or null argument to \`-R' flag" "$progname" + "%s: Missing or null argument to \`-R' flag" "$pgm" ROOTDIR="$OPTARG";; j) [ "$OPTARG" ] || die \ - "%s: Missing or null argument to \`-j' flag" "$progname" + "%s: Missing or null argument to \`-j' flag" "$pgm" JAIL="$OPTARG";; + q) QUIET=1 SYSRC_VERBOSE=;; \?) usage;; esac done shift $(( $OPTIND - 1 )) # +# [More] Sanity checks (e.g., "sysrc --") +# +[ $# -eq 0 -a ! "$SHOW_ALL" ] && usage + +# # Taint-check all rc.conf(5) files # -errmsg="$progname: Exiting due to previous syntax errors" -/bin/sh -n "$RC_DEFAULTS" || die "$errmsg" -( . "$RC_DEFAULTS" - for i in ${RC_CONFS:-$rc_conf_files}; do - [ -e "$i" ] || continue - /bin/sh -n "$i" || exit $FAILURE - done - exit $SUCCESS -) || die "$errmsg" +errmsg="$pgm: Exiting due to previous syntax errors" +if ( : ${RC_CONFS?} ) > /dev/null 2>&1; then + ( for i in $RC_CONFS; do + [ -e "$i" ] || continue + /bin/sh -n "$i" || exit $FAILURE + done + exit $SUCCESS + ) || die "$errmsg" +else + /bin/sh -n "$RC_DEFAULTS" || die "$errmsg" + ( . "$RC_DEFAULTS" + for i in $rc_conf_files; do + [ -e "$i" ] || continue + /bin/sh -n "$i" || exit $FAILURE + done + exit $SUCCESS + ) || die "$errmsg" +fi # # Process `-x' (and secret `-X') command-line options # -errmsg="$progname: \`-x' option incompatible with \`-a'/\`-A' options" +errmsg="$pgm: \`-x' option incompatible with \`-a'/\`-A' options" errmsg="$errmsg (use \`-X' to override)" if [ "$DELETE" -a "$SHOW_ALL" ]; then [ "$DELETE" = "2" ] || die "$errmsg" @@ -849,7 +922,7 @@ if [ "$JAIL" -o "$ROOTDIR" ]; then # args=" ${SYSRC_VERBOSE:+-v} - ${RC_CONFS:+-f'$RC_CONFS'} + ${QUIET:+-q} $( [ "$DELETE" = "1" ] && echo \ -x ) $( [ "$DELETE" = "2" ] && echo \ -X ) $( [ "$SHOW_ALL" = "1" ] && echo \ -a ) @@ -859,7 +932,11 @@ if [ "$JAIL" -o "$ROOTDIR" ]; then ${IGNORE_UNKNOWNS:+-i} $( [ "$SHOW_NAME" ] || echo \ -n ) $( [ "$SHOW_VALUE" ] || echo \ -N ) + $( [ "$SHOW_FILE" ] && echo \ -F ) " + if ( : ${RC_CONFS?} ) > /dev/null 2>&1; then + args="$args -f '$RC_CONFS'" + fi for arg in "$@"; do args="$args '$arg'" done @@ -872,6 +949,7 @@ if [ "$JAIL" -o "$ROOTDIR" ]; then # Re-execute ourselves with sh(1) via jexec(8) # ( echo set -- $args + jail_depend cat $0 ) | env - RC_DEFAULTS="$RC_DEFAULTS" \ /usr/sbin/jexec "$JAIL" /bin/sh @@ -885,7 +963,7 @@ if [ "$JAIL" -o "$ROOTDIR" ]; then # older systems, we will not perform this check if either the # jls(1) or jexec(8) utilities are missing. # - if have jexec && have jls; then + if f_have jexec && f_have jls; then jid="`jls jid path | \ ( while read JID JROOT; do @@ -899,10 +977,10 @@ if [ "$JAIL" -o "$ROOTDIR" ]; then # directory, exit with error. # if [ "$jid" -a "${jid%[$IFS]*}" != "$jid" ]; then - die "%s: %s: %s" "$progname" "$ROOTDIR" \ - "$( echo "Multiple jails claim this" \ - "directory as their root." \ - "(use \`-j jail' instead)" )" + die "%s: %s: %s" "$pgm" "$ROOTDIR" \ + "$( echo "Multiple jails claim this" \ + "directory as their root." \ + "(use \`-j jail' instead)" )" fi # @@ -914,6 +992,7 @@ if [ "$JAIL" -o "$ROOTDIR" ]; then # Re-execute outselves with sh(1) via jexec(8) # ( echo set -- $args + jail_depend cat $0 ) | env - RC_DEFAULTS="$RC_DEFAULTS" \ /usr/sbin/jexec "$jid" /bin/sh @@ -927,6 +1006,7 @@ if [ "$JAIL" -o "$ROOTDIR" ]; then # Re-execute ourselves with sh(1) via chroot(8) # ( echo set -- $args + jail_depend cat $0 ) | env - RC_DEFAULTS="$RC_DEFAULTS" \ /usr/sbin/chroot "$ROOTDIR" /bin/sh @@ -949,22 +1029,22 @@ if [ "$SHOW_ALL" ]; then # Append the pipe-character (|) to the list of internal field # separation (IFS) characters, allowing us to use the below # list both as an extended grep (-E) pattern and argument list - # (required to first get clean_env() to preserve these in the + # (required to first get f_clean_env() to preserve these in the # environment and then later to prune them from the list of # variables produced by set(1)). # IFS="$IFS|" EXCEPT="IFS|EXCEPT|PATH|RC_DEFAULTS|OPTIND|DESCRIBE|SEP" 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" + EXCEPT="$EXCEPT|SHOW_VALUE|SHOW_FILE|SYSRC_VERBOSE|RC_CONFS" + EXCEPT="$EXCEPT|pgm|SUCCESS|FAILURE" + EXCEPT="$EXCEPT|f_sysrc_desc_awk|f_sysrc_delete_awk" # # Clean the environment (except for our required variables) # and then source the required files. # - clean_env --except $EXCEPT + f_clean_env --except $EXCEPT if [ -f "$RC_DEFAULTS" -a -r "$RC_DEFAULTS" ]; then . "$RC_DEFAULTS" @@ -973,14 +1053,15 @@ if [ "$SHOW_ALL" ]; then # environment, removing the rc.conf(5) defaults. # [ "$SHOW_ALL" = "1" ] \ - && clean_env --except rc_conf_files $EXCEPT + && f_clean_env --except rc_conf_files $EXCEPT # # If `-f file' was passed, set $rc_conf_files to an # explicit value, modifying the default behavior of # source_rc_confs(). # - [ "$RC_CONFS" ] && rc_conf_files="$RC_CONFS" + ( : ${RC_CONFS?} ) > /dev/null 2>&1 && + rc_conf_files="$RC_CONFS" source_rc_confs @@ -990,20 +1071,29 @@ if [ "$SHOW_ALL" ]; then # other than rc.conf(5) defaults. # [ "$SHOW_ALL" = "1" -a \ - "$( sysrc_find rc_conf_files )" = "$RC_DEFAULTS" \ + "$( f_sysrc_find rc_conf_files )" = "$RC_DEFAULTS" \ ] \ && unset rc_conf_files fi - for NAME in $( set | awk -F= \ - '/^[[:alpha:]_][[:alnum:]_]*=/ {print $1}' \ - | grep -Ev "^($EXCEPT)$" + for NAME in $( set | + awk -F= '/^[[:alpha:]_][[:alnum:]_]*=/ {print $1}' | + grep -Ev "^($EXCEPT)$" ); do # # If enabled, describe rather than expand value # if [ "$DESCRIBE" ]; then - echo "$NAME: $( sysrc_desc "$NAME" )" + echo "$NAME: $( f_sysrc_desc "$NAME" )" + continue + fi + + # + # If `-F' is passed, find it and move on + # + if [ "$SHOW_FILE" ]; then + [ "$SHOW_NAME" ] && echo -n "$NAME: " + f_sysrc_find "$NAME" continue fi @@ -1011,12 +1101,12 @@ if [ "$SHOW_ALL" ]; then # If `-X' is passed, delete the variables # if [ "$DELETE" = "2" ]; then - sysrc_delete "$NAME" + f_sysrc_delete "$NAME" continue fi [ "$SYSRC_VERBOSE" ] && \ - echo -n "$( sysrc_find "$NAME" ): " + echo -n "$( f_sysrc_find "$NAME" ): " # # If `-N' is passed, simplify the output @@ -1027,7 +1117,7 @@ if [ "$SHOW_ALL" ]; then fi echo "${SHOW_NAME:+$NAME$SEP}$( - sysrc_get "$NAME" )${SHOW_EQUALS:+\"}" + f_sysrc_get "$NAME" )${SHOW_EQUALS:+\"}" done ) @@ -1045,7 +1135,7 @@ while [ $# -gt 0 ]; do NAME="${1%%=*}" [ "$DESCRIBE" ] && \ - echo "$NAME: $( sysrc_desc "$NAME" )" + echo "$NAME: $( f_sysrc_desc "$NAME" )" case "$1" in *=*) @@ -1055,9 +1145,9 @@ while [ $# -gt 0 ]; do # if [ "$SYSRC_VERBOSE" ]; then - file=$( sysrc_find "$NAME" ) + file=$( f_sysrc_find "$NAME" ) [ "$file" = "$RC_DEFAULTS" -o ! "$file" ] && \ - file=$( sysrc_get "rc_conf_files%%[$IFS]*" ) + file=$( f_sysrc_get 'rc_conf_files%%[$IFS]*' ) echo -n "$file: " fi @@ -1066,7 +1156,7 @@ while [ $# -gt 0 ]; do # desire to set some value # if [ "$DELETE" ]; then - sysrc_delete "$NAME" + f_sysrc_delete "$NAME" shift 1 continue fi @@ -1076,20 +1166,31 @@ while [ $# -gt 0 ]; do # if [ ! "$SHOW_VALUE" ]; then echo "$NAME" - sysrc_set "$NAME" "${1#*}" + f_sysrc_set "$NAME" "${1#*}" else - before=$( sysrc_get "$NAME" ) - if sysrc_set "$NAME" "${1#*=}"; then - echo -n "${SHOW_NAME:+$NAME$SEP}" - echo -n "$before${SHOW_EQUALS:+\"}" - echo " -> $( sysrc_get "$NAME" )" + if [ "$SHOW_FILE" ]; then + before=$( f_sysrc_find "$NAME" ) + else + before=$( f_sysrc_get "$NAME" ) + fi + if f_sysrc_set "$NAME" "${1#*=}"; then + if [ "$SHOW_FILE" ]; then + after=$( f_sysrc_find "$NAME" ) + echo -n "${SHOW_NAME:+$NAME$SEP}" + echo -n "$before${SHOW_EQUALS:+\"}" + echo " -> $after" + else + after=$( f_sysrc_get "$NAME" ) + echo -n "${SHOW_NAME:+$NAME$SEP}" + echo "$before -> $after" + fi fi fi ;; *) - if ! IGNORED="$( sysrc_get "$NAME?" )"; then + if ! IGNORED="$( f_sysrc_get "$NAME?" )"; then [ "$IGNORE_UNKNOWNS" ] \ - || echo "$progname: unknown variable '$NAME'" + || echo "$pgm: unknown variable '$NAME'" shift 1 continue fi @@ -1108,13 +1209,23 @@ while [ $# -gt 0 ]; do # If `-x' or `-X' is passed, delete the variable # if [ "$DELETE" ]; then - sysrc_delete "$NAME" + f_sysrc_delete "$NAME" + shift 1 + continue + fi + + # + # If `-F' is passed, find it and move on + # + if [ "$SHOW_FILE" ]; then + [ "$SHOW_NAME" ] && echo -n "$NAME: " + f_sysrc_find "$NAME" shift 1 continue fi [ "$SYSRC_VERBOSE" ] && \ - echo -n "$( sysrc_find "$NAME" ): " + echo -n "$( f_sysrc_find "$NAME" ): " # # If `-N' is passed, simplify the output @@ -1123,7 +1234,7 @@ while [ $# -gt 0 ]; do echo "$NAME" else echo "${SHOW_NAME:+$NAME$SEP}$( - sysrc_get "$NAME" )${SHOW_EQUALS:+\"}" + f_sysrc_get "$NAME" )${SHOW_EQUALS:+\"}" fi esac shift 1