[Midnightbsd-cvs] src [9892] trunk/usr.sbin/sysrc: update sysrc to version 7

laffer1 at midnightbsd.org laffer1 at midnightbsd.org
Thu May 24 15:01:33 EDT 2018


Revision: 9892
          http://svnweb.midnightbsd.org/src/?rev=9892
Author:   laffer1
Date:     2018-05-24 15:01:32 -0400 (Thu, 24 May 2018)
Log Message:
-----------
update sysrc to version 7

Modified Paths:
--------------
    trunk/usr.sbin/sysrc/Makefile

Added Paths:
-----------
    trunk/usr.sbin/sysrc/sysrc
    trunk/usr.sbin/sysrc/sysrc.8

Removed Paths:
-------------
    trunk/usr.sbin/sysrc/sysrc.sh

Modified: trunk/usr.sbin/sysrc/Makefile
===================================================================
--- trunk/usr.sbin/sysrc/Makefile	2018-05-24 18:53:26 UTC (rev 9891)
+++ trunk/usr.sbin/sysrc/Makefile	2018-05-24 19:01:32 UTC (rev 9892)
@@ -1,7 +1,7 @@
 # $MidnightBSD$
 
-SCRIPTS=sysrc.sh
-NO_MAN=
+SCRIPTS= sysrc
 
+MAN= sysrc.8
+
 .include <bsd.prog.mk>
-

Added: trunk/usr.sbin/sysrc/sysrc
===================================================================
--- trunk/usr.sbin/sysrc/sysrc	                        (rev 0)
+++ trunk/usr.sbin/sysrc/sysrc	2018-05-24 19:01:32 UTC (rev 9892)
@@ -0,0 +1,930 @@
+#!/bin/sh
+#-
+# Copyright (c) 2010-2015 Devin Teske
+# All rights reserved.
+#
+# Redistribution and use in source and binary forms, with or without
+# modification, are permitted provided that the following conditions
+# are met:
+# 1. Redistributions of source code must retain the above copyright
+#    notice, this list of conditions and the following disclaimer.
+# 2. Redistributions in binary form must reproduce the above copyright
+#    notice, this list of conditions and the following disclaimer in the
+#    documentation and/or other materials provided with the distribution.
+#
+# THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
+# ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
+# IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
+# ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
+# FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
+# DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
+# OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
+# HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
+# LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
+# OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
+# SUCH DAMAGE.
+#
+# $MidnightBSD$
+# $FreeBSD: stable/10/usr.sbin/sysrc/sysrc 292833 2015-12-28 17:50:31Z dteske $
+#
+############################################################ INCLUDES
+
+# Prevent `-d' from being interpreted as a debug flag by common.subr
+DEBUG_SELF_INITIALIZE=
+
+BSDCFG_SHARE="/usr/share/bsdconfig"
+[ "$_COMMON_SUBR" ] || . $BSDCFG_SHARE/common.subr || exit 1
+[ "$_SYSRC_SUBR"  ] || f_include $BSDCFG_SHARE/sysrc.subr
+
+############################################################ GLOBALS
+
+#
+# Version information
+#
+SYSRC_VERSION="7.0 Sep-13,2015"
+
+#
+# Options
+#
+CHECK_ONLY=
+DEFAULT=
+DELETE=
+DESCRIBE=
+EXISTING_ONLY=
+IGNORE_UNKNOWNS=
+JAIL=
+LIST_SERVICE_CONFS=
+LIST_CONFS=
+QUIET=
+ROOTDIR=
+SERVICE=
+SHOW_ALL=
+SHOW_EQUALS=
+SHOW_FILE=
+SHOW_NAME=1
+SHOW_VALUE=1
+VERBOSE=
+
+############################################################ FUNCTIONS
+
+# die [$fmt [$opts ...]]
+#
+# Optionally print a message to stderr before exiting with failure status.
+#
+die()
+{
+	local fmt="$1"
+	[ $# -gt 0 ] && shift 1
+	[  "$fmt"  ] && f_err "$fmt\n" "$@"
+
+	exit $FAILURE
+}
+
+# usage
+#
+# Prints a short syntax statement and exits.
+#
+usage()
+{
+	f_err "Usage: %s [OPTIONS] %s\n" "$pgm" \
+		"{name[[+|-]=value] ... | -a | -A | -l | -L [name ...]}"
+	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"
+
+	f_err "Usage: %s [OPTIONS] name[[+|-]=value] ...\n" "$pgm"
+	f_err "Usage: %s [OPTIONS] -a | -A\n" "$pgm"
+	f_err "Usage: %s [OPTIONS] -l | -L [name ...]\n" "$pgm"
+
+	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" "-c" \
+	      "Check. Return success if set or no changes, else error."
+	f_err "$optfmt" "-d" \
+	      "Print a description of the given variable."
+	f_err "$optfmt" "-D" \
+	      "Show default value(s) only (this is the same as setting"
+	f_err "$optfmt" "" \
+	      "RC_CONFS to NULL or passing \`-f' with a NULL file-argument)."
+	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" "-E" \
+	      "Existing files only with \`-[lL]' or when changing a setting."
+	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" "-l" \
+	      "List configuration files used at startup on stdout and exit."
+	f_err "$optfmt" "-L" \
+	      "List all configuration files including rc.conf.d entries."
+	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. Disable verbose and hide certain errors."
+	f_err "$optfmt" "-s name" \
+	      "Process additional \`rc.conf.d' entries for service name."
+	f_err "$optfmt" "" \
+	      "Ignored if \`-f file' is given."
+	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" "--version" \
+	      "Print version information to stdout and exit."
+	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."
+
+	die
+}
+
+# jail_depend
+#
+# Dump dependencies such as language-file variables and include files to stdout
+# to be piped-into sh(1) running via jexec(8)/chroot(8). As a security measure,
+# this prevents existing language files and library files from being loaded in
+# the jail. This also relaxes the requirement to have these files in every jail
+# before sysrc can be used on said jail.
+#
+jail_depend()
+{
+	#
+	# Indicate that we are jailed
+	#
+	echo export _SYSRC_JAILED=1
+
+	#
+	# Print i18n language variables (their current values are sanitized
+	# and re-printed for interpretation so that the i18n language files
+	# do not need to exist within the jail).
+	#
+	local var val
+	for var in \
+		msg_cannot_create_permission_denied \
+		msg_permission_denied \
+		msg_previous_syntax_errors \
+	; do
+		val=$( eval echo \"\$$var\" |
+			awk '{ gsub(/'\''/, "'\''\\'\'\''"); print }' )
+		echo $var="'$val'"
+	done
+
+	#
+	# Print include dependencies
+	#
+	echo DEBUG_SELF_INITIALIZE=
+	cat $BSDCFG_SHARE/common.subr
+	cat $BSDCFG_SHARE/sysrc.subr
+}
+
+# escape $string [$var_to_set]
+#
+# Escape $string contents so that the contents can be properly encapsulated in
+# single-quotes (making for safe evaluation).
+#
+# NB: See `bsdconfig includes -dF escape' for relevant information/discussion.
+# NB: Abridged version of `f_shell_escape()' from bsdconfig(8) `strings.subr'.
+#
+escape()
+{
+	local __start="$1" __var_to_set="$2" __string=
+	while [ "$__start" ]; do
+		case "$__start" in *\'*)
+			__string="$__string${__start%%\'*}'\\''"
+			__start="${__start#*\'}" continue
+		esac
+		break
+	done
+	__string="$__string$__start"
+	if [ "$__var_to_set" ]; then
+		setvar "$__var_to_set" "$__string"
+	else
+		echo "$__string"
+	fi
+}
+
+############################################################ MAIN SOURCE
+
+#
+# Perform sanity checks
+#
+[ $# -gt 0 ] || usage # NOTREACHED
+
+#
+# Check for `--help' and `--version' command-line option
+#
+for arg in "$@"; do
+	case "$arg" in
+	--) break ;;
+	--help) help ;; # NOTREACHED
+	--version) # see GLOBALS
+		echo "$SYSRC_VERSION"
+		exit $FAILURE ;;
+	esac
+done
+unset arg
+
+#
+# Process command-line flags
+#
+while getopts aAcdDeEf:Fhij:lLnNqR:s:vxX flag; do
+	case "$flag" in
+	a) SHOW_ALL=${SHOW_ALL:-1} ;;
+	A) SHOW_ALL=2 ;;
+	c) CHECK_ONLY=1 ;;
+	d) DESCRIBE=1 ;;
+	D) DEFAULT=1 RC_CONFS= ;;
+	e) SHOW_EQUALS=1 ;;
+	E) EXISTING_ONLY=1 ;;
+	f) DEFAULT= RC_CONFS="$RC_CONFS${RC_CONFS:+ }$OPTARG" ;;
+	F) SHOW_FILE=1 ;;
+	h) usage ;; # NOTREACHED
+	i) IGNORE_UNKNOWNS=1 ;;
+	j) [ "$OPTARG" ] ||
+		die "%s: Missing or null argument to \`-j' flag" "$pgm"
+	   JAIL="$OPTARG" ;;
+	l) LIST_CONFS=1 ;;
+	L) LIST_SERVICE_CONFS=1 ;;
+	n) SHOW_NAME= ;;
+	N) SHOW_VALUE= ;;
+	q) QUIET=1 VERBOSE= ;;
+	R) [ "$OPTARG" ] ||
+		die "%s: Missing or null argument to \`-R' flag" "$pgm"
+	   ROOTDIR="$OPTARG" ;;
+	s) [ "$OPTARG" ] ||
+		die "%s: Missing or null argument to \`-s' flag" "$pgm"
+	   SERVICE="$OPTARG" ;;
+	v) VERBOSE=1 QUIET= ;;
+	x) DELETE=${DELETE:-1} ;;
+	X) DELETE=2 ;;
+	\?) usage ;; # NOTREACHED
+	esac
+done
+shift $(( $OPTIND - 1 ))
+
+#
+# Process `-L' flag
+#
+if [ "$LIST_SERVICE_CONFS" ]; then
+	list= 
+
+	#
+	# List rc_conf_files if no service names given
+	#
+	files=
+	[ $# -eq 0 ] && files=$( f_sysrc_get rc_conf_files )
+	for file in $files; do
+		if [ "$EXISTING_ONLY" ]; then
+			[ -e "$file" -a ! -d "$file" ] || continue
+		fi
+		case "$list" in
+		"$file"|*" $file"|"$file "*|*" $file "*) continue ;;
+		esac
+		list="$list $file"
+	done
+	list="${list# }"
+	if [ $# -eq 0 ]; then
+		if [ "$VERBOSE" ]; then
+			echo rc_conf_files: $list
+		elif [ "$SHOW_EQUALS" ]; then
+			echo "rc_conf_files=\"$list\""
+		fi
+	fi
+
+	#
+	# List rc.conf.d entries
+	#
+	retval=$SUCCESS
+	for service in ${*:-$( service -l )}; do
+		slist=
+		f_sysrc_service_configs $service files || retval=$? continue
+		for file in $files; do
+			if [ "$EXISTING_ONLY" ]; then
+				[ -e "$file" -a ! -d "$file" ] || continue
+			fi
+			if [ ! "$VERBOSE" -a ! "$SHOW_EQUALS" ]; then
+				case "$list" in
+				"$file"|*" $file"|"$file "*|*" $file "*)
+					continue ;;
+				esac
+			fi
+			slist="$slist $file"
+		done
+		slist="${slist# }"
+		if [ $# -gt 0 ]; then
+			[ "$slist" ] || retval=$?
+		fi
+		if [ "$VERBOSE" ]; then
+			[ "$slist" ] && echo "$service: $slist"
+			continue
+		elif [ "$SHOW_EQUALS" ]; then
+			[ "$slist" ] && echo "$service=\"$slist\""
+			continue
+		fi
+		list="$list${slist:+ }$slist"
+	done
+	if [ ! "$VERBOSE" -a ! "$SHOW_EQUALS" ]; then
+		if [ $# -eq 0 -o ! "$QUIET" ]; then
+			list="${list# }"
+			[ "$list" ] && echo $list
+		fi
+	fi
+
+	exit $retval
+fi
+
+#
+# Process `-s name' argument
+#
+if [ "$SERVICE" -a ! "${RC_CONFS+set}" ]; then
+	if f_sysrc_service_configs "$SERVICE" RC_CONFS; then
+		rc_conf_files=$( f_sysrc_get rc_conf_files )
+		RC_CONFS="$rc_conf_files${RC_CONFS:+ }$RC_CONFS"
+		unset rc_conf_files
+	else
+		unset RC_CONFS
+	fi
+fi
+
+#
+# Process `-E' option flag
+#
+if [ "$EXISTING_ONLY" ]; then
+	#
+	# To get f_sysrc_*() to ignore missing rc_conf_files, we have to use
+	# RC_CONFS to override the unpreened value. If RC_CONFS already has a
+	# value (`-D', `-f file', `-s name', or inherited from parent), use it.
+	# Otherwise, include filtered contents of rc_conf_files.
+	# 
+	RC_CONFS=$(
+		if [ "${RC_CONFS+set}" ]; then
+			set -- $RC_CONFS
+		else
+			set -- $( f_sysrc_get rc_conf_files )
+		fi
+		while [ $# -gt 0 ]; do
+			[ -f "$1" ] && echo -n " $1"
+			shift
+		done
+	)
+	RC_CONFS="${RC_CONFS# }"
+fi
+
+#
+# Process `-l' option flag
+#
+if [ "$LIST_CONFS" ]; then
+	[ $# -eq 0 ] || usage
+	if [ "$DEFAULT" ]; then
+		echo "$RC_DEFAULTS"
+	elif [ "${RC_CONFS+set}" ]; then
+		echo "$RC_CONFS"
+	else
+		f_sysrc_get rc_conf_files
+	fi
+	exit $SUCCESS
+fi
+
+#
+# [More] Sanity checks (e.g., "sysrc --")
+#
+[ $# -eq 0 -a ! "$SHOW_ALL" ] && usage # NOTREACHED
+
+#
+# Taint-check all rc.conf(5) files
+#
+errmsg="$pgm: Exiting due to previous syntax errors"
+if [ "${RC_CONFS+set}" ]; 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="$pgm: \`-x' option incompatible with \`-a'/\`-A' options"
+errmsg="$errmsg (use \`-X' to override)"
+if [ "$DELETE" -a "$SHOW_ALL" ]; then
+	[ "$DELETE" = "2" ] || die "$errmsg"
+fi
+
+#
+# Pre-flight for `-c' command-line option
+#
+[ "$CHECK_ONLY" -a "$SHOW_ALL" ] &&
+	die "$pgm: \`-c' option incompatible with \`-a'/\`-A' options"
+
+#
+# Process `-e', `-n', and `-N' command-line options
+#
+SEP=': '
+[ "$SHOW_FILE" ] && SHOW_EQUALS=
+[ "$SHOW_NAME" ] || SHOW_EQUALS=
+[ "$VERBOSE" = "0" ] && VERBOSE=
+if [ ! "$SHOW_VALUE" ]; then
+	SHOW_NAME=1
+	SHOW_EQUALS=
+fi
+[ "$SHOW_EQUALS" ] && SEP='="'
+
+#
+# Process `-j jail' and `-R dir' command-line options
+#
+if [ "$JAIL" -o "$ROOTDIR" ]; then
+	#
+	# Reconstruct the arguments that we want to carry-over
+	#
+	args="
+		${VERBOSE:+-v}
+		${QUIET:+-q}
+		$( [ "$DELETE" = "1" ] && echo \ -x )
+		$( [ "$DELETE" = "2" ] && echo \ -X )
+		$( [ "$SHOW_ALL" = "1" ] && echo \ -a )
+		$( [ "$SHOW_ALL" = "2" ] && echo \ -A )
+		${CHECK_ONLY:+-c}
+		${DEFAULT:+-D}
+		${EXISTING_ONLY:+-E}
+		${LIST_CONFS:+-l}
+		${LIST_SERVICE_CONFS:+-L}
+		${DESCRIBE:+-d}
+		${SHOW_EQUALS:+-e}
+		${IGNORE_UNKNOWNS:+-i}
+		$( [ "$SHOW_NAME"  ] || echo \ -n )
+		$( [ "$SHOW_VALUE" ] || echo \ -N )
+		$( [ "$SHOW_FILE"  ] && echo \ -F )
+	"
+	if [ "$SERVICE" ]; then
+		escape "$SERVICE" _SERVICE
+		args="$args -s '$_SERVICE'"
+		unset _SERVICE
+	fi
+	if [ "${RC_CONFS+set}" ]; then
+		escape "$RC_CONFS" _RC_CONFS
+		args="$args -f '$_RC_CONFS'"
+		unset _RC_CONFS
+	fi
+	for arg in "$@"; do
+		escape "$arg" arg
+		args="$args '$arg'"
+	done
+
+	#
+	# If both are supplied, `-j jail' supercedes `-R dir'
+	#
+	if [ "$JAIL" ]; 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
+		exit $?
+	elif [ "$ROOTDIR" ]; then
+		#
+		# Make sure that the root directory specified is not to any
+		# running jails.
+		#
+		# NOTE: To maintain backward compatibility with older jails on
+		# older systems, we will not perform this check if either the
+		# jls(1) or jexec(8) utilities are missing.
+		#
+		if f_have jexec && f_have jls; then
+			jid=$( jls jid path |
+				while read JID JROOT; do
+					[ "$JROOT" = "$ROOTDIR" ] || continue
+					echo $JID
+				done
+			)
+
+			#
+			# If multiple running jails match the specified root
+			# directory, exit with error.
+			#
+			if [ "$jid" -a "${jid%[$IFS]*}" != "$jid" ]; then
+				die "%s: %s: %s" "$pgm" "$ROOTDIR" \
+				    "$( echo "Multiple jails claim this" \
+				             "directory as their root." \
+				             "(use \`-j jail' instead)" )"
+			fi
+
+			#
+			# If only a single running jail matches the specified
+			# root directory, implicitly use `-j jail'.
+			#
+			if [ "$jid" ]; 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
+				exit $?
+			fi
+
+			# Otherwise, fall through and allow chroot(8)
+		fi
+
+		#
+		# 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
+		exit $?
+	fi
+fi
+
+#
+# Process `-a' or `-A' command-line options
+#
+if [ "$SHOW_ALL" ]; then
+	#
+	# Get a list of variables that are currently set in the rc.conf(5)
+	# files (included `/etc/defaults/rc.conf') by performing a call to
+	# source_rc_confs() in a clean environment.
+	#
+	( # Operate in a sub-shell to protect the parent environment
+		#
+		# Set which variables we want to preserve in the environment.
+		# 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 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|DEFAULT"
+		EXCEPT="$EXCEPT|SHOW_VALUE|SHOW_FILE|VERBOSE|RC_CONFS|SERVICE"
+		EXCEPT="$EXCEPT|pgm|SUCCESS|FAILURE|CHECK_ONLY|EXISTING_ONLY"
+		EXCEPT="$EXCEPT|LIST_CONFS|LIST_SERVICE_CONFS"
+		EXCEPT="$EXCEPT|f_sysrc_desc_awk|f_sysrc_delete_awk"
+
+		#
+		# Clean the environment (except for our required variables)
+		# and then source the required files.
+		#
+		f_clean_env --except $EXCEPT
+		if [ -f "$RC_DEFAULTS" -a -r "$RC_DEFAULTS" ]; then
+			. "$RC_DEFAULTS"
+
+			#
+			# If passed `-a' (rather than `-A'), re-purge the
+			# environment, removing the rc.conf(5) defaults.
+			#
+			[ "$SHOW_ALL" = "1" ] &&
+				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().
+			#
+			if [ "${RC_CONFS+set}" ]; then
+				[ "$SHOW_ALL" = "1" -a "$SERVICE" -a \
+					! "$DEFAULT" ] || rc_conf_files=
+				rc_conf_files="$rc_conf_files $RC_CONFS"
+				rc_conf_files="${rc_conf_files# }"
+				rc_conf_files="${rc_conf_files% }"
+			fi
+
+			source_rc_confs
+
+			#
+			# If passed `-a' (rather than `-A'), remove
+			# `rc_conf_files' unless it was defined somewhere
+			# other than rc.conf(5) defaults.
+			#
+			[ "$SHOW_ALL" = "1" -a \
+			  "$( 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)$"
+		); do
+			#
+			# If enabled, describe rather than expand value
+			#
+			if [ "$DESCRIBE" ]; then
+				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
+
+			#
+			# If `-X' is passed, delete the variables
+			#
+			if [ "$DELETE" = "2" ]; then
+				f_sysrc_delete "$NAME"
+				continue
+			fi
+
+			[ "$VERBOSE" ] &&
+				echo -n "$( f_sysrc_find "$NAME" ): "
+
+			#
+			# If `-N' is passed, simplify the output
+			#
+			if [ ! "$SHOW_VALUE" ]; then
+				echo "$NAME"
+				continue
+			fi
+
+			echo "${SHOW_NAME:+$NAME$SEP}$(
+			      f_sysrc_get "$NAME" )${SHOW_EQUALS:+\"}"
+
+		done
+	)
+
+	#
+	# Ignore the remainder of positional arguments.
+	#
+	exit $SUCCESS
+fi
+
+#
+# Process command-line arguments
+#
+status=$SUCCESS
+while [ $# -gt 0 ]; do
+	NAME="${1%%=*}"
+
+	case "$NAME" in
+	*+) mode=APPEND NAME="${NAME%+}" ;;
+	*-) mode=REMOVE NAME="${NAME%-}" ;;
+	 *) mode=ASSIGN
+	esac
+
+	[ "$DESCRIBE" ] &&
+		echo "$NAME: $( f_sysrc_desc "$NAME" )"
+
+	case "$1" in
+	*=*)
+		#
+		# Like sysctl(8), if both `-d' AND "name=value" is passed,
+		# first describe (done above), then attempt to set
+		#
+
+		# If verbose, prefix line with where the directive lives
+		if [ "$VERBOSE" -a ! "$CHECK_ONLY" ]; then
+			file=$( f_sysrc_find "$NAME" )
+			[ "$file" = "$RC_DEFAULTS" -o ! "$file" ] &&
+				file=$( f_sysrc_get 'rc_conf_files%%[$IFS]*' )
+			if [ "$SHOW_EQUALS" ]; then
+				echo -n ": $file; "
+			else
+				echo -n "$file: "
+			fi
+		fi
+
+		#
+		# If `-x' or `-X' is passed, delete the variable and ignore the
+		# desire to set some value
+		#
+		if [ "$DELETE" ]; then
+			f_sysrc_delete "$NAME" || status=$FAILURE
+			shift 1
+			continue
+		fi
+
+		#
+		# If `-c' is passed, simply compare and move on
+		#
+		if [ "$CHECK_ONLY" ]; then
+			if ! IGNORED=$( f_sysrc_get "$NAME?" ); then
+				status=$FAILURE
+				[ "$VERBOSE" ] &&
+					echo "$NAME: not currently set"
+				shift 1
+				continue
+			fi
+			value=$( f_sysrc_get "$NAME" )
+			if [ "$value" != "${1#*=}" ]; then
+				status=$FAILURE
+				if [ "$VERBOSE" ]; then
+					echo -n "$( f_sysrc_find "$NAME" ): "
+					echo -n "$NAME: would change from "
+					echo "\`$value' to \`${1#*=}'"
+				fi
+			elif [ "$VERBOSE" ]; then
+				echo -n "$( f_sysrc_find "$NAME" ): "
+				echo "$NAME: already set to \`$value'"
+			fi
+			shift 1
+			continue
+		fi
+
+		#
+		# Determine both `before' value and appropriate `new' value
+		#
+		case "$mode" in
+		APPEND)
+			before=$( f_sysrc_get "$NAME" )
+			add="${1#*=}"
+			delim="${add%"${add#?}"}" # first character
+			oldIFS="$IFS"
+			case "$delim" in
+			""|[$IFS]|[a-zA-Z0-9]) delim=" " ;;
+			*) IFS="$delim"
+			esac
+			new="$before"
+			for a in $add; do
+				[ "$a" ] || continue
+				skip=
+				for b in $before; do
+					[ "$b" = "$a" ] && skip=1 break
+				done
+				[ "$skip" ] || new="$new$delim$a"
+			done
+			new="${new#"$delim"}" IFS="$oldIFS"
+			unset add delim oldIFS a skip b
+			[ "$SHOW_FILE" ] && before=$( f_sysrc_find "$NAME" )
+			;;
+		REMOVE)
+			before=$( f_sysrc_get "$NAME" )
+			remove="${1#*=}"
+			delim="${remove%"${remove#?}"}" # first character
+			oldIFS="$IFS"
+			case "$delim" in
+			""|[$IFS]|[a-zA-Z0-9]) delim=" " ;;
+			*) IFS="$delim"
+			esac
+			new=
+			for b in $before; do
+				[ "$b" ] || continue
+				add=1
+				for r in $remove; do
+					[ "$r" = "$b" ] && add= break
+				done
+				[ "$add" ] && new="$new$delim$b"
+			done
+			new="${new#"$delim"}" IFS="$oldIFS"
+			unset remove delim oldIFS b add r
+			[ "$SHOW_FILE" ] && before=$( f_sysrc_find "$NAME" )
+			;;
+		*) # ASSIGN
+			if [ "$SHOW_FILE" ]; then
+				before=$( f_sysrc_find "$NAME" )
+			else
+				before=$( f_sysrc_get "$NAME" )
+			fi
+			new="${1#*=}"
+		esac
+
+		#
+		# If `-N' is passed, simplify the output
+		#
+		if [ ! "$SHOW_VALUE" ]; then
+			echo "$NAME"
+			f_sysrc_set "$NAME" "$new"
+		else
+			if f_sysrc_set "$NAME" "$new"; then
+				if [ "$SHOW_FILE" ]; then
+					after=$( f_sysrc_find "$NAME" )
+				else
+					after=$( f_sysrc_get "$NAME" )
+				fi
+				echo -n "${SHOW_NAME:+$NAME$SEP}"
+				echo -n "$before${SHOW_EQUALS:+\" #}"
+				echo -n " -> ${SHOW_EQUALS:+\"}$after"
+				echo "${SHOW_EQUALS:+\"}"
+			fi
+		fi
+		;;
+	*)
+		if ! IGNORED=$( f_sysrc_get "$NAME?" ); then
+			[ "$IGNORE_UNKNOWNS" -o "$QUIET" ] ||
+				echo "$pgm: unknown variable '$NAME'"
+			shift 1
+			status=$FAILURE
+			continue
+		fi
+
+		# The above check told us what we needed for `-c'
+		if [ "$CHECK_ONLY" ]; then
+			shift 1
+			continue
+		fi
+
+		#
+		# Like sysctl(8), when `-d' is passed, desribe it
+		# (already done above) rather than expanding it
+		#
+
+		if [ "$DESCRIBE" ]; then
+			shift 1
+			continue
+		fi
+
+		#
+		# If `-x' or `-X' is passed, delete the variable
+		#
+		if [ "$DELETE" ]; then
+			f_sysrc_delete "$NAME" || status=$FAILURE
+			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
+
+		if [ "$VERBOSE" ]; then
+			if [ "$SHOW_EQUALS" ]; then
+				echo -n ": $( f_sysrc_find "$NAME" ); "
+			else
+				echo -n "$( f_sysrc_find "$NAME" ): "
+			fi
+		fi
+
+		#
+		# If `-N' is passed, simplify the output
+		#
+		if [ ! "$SHOW_VALUE" ]; then
+			echo "$NAME"
+		else
+			echo "${SHOW_NAME:+$NAME$SEP}$(
+			      f_sysrc_get "$NAME" )${SHOW_EQUALS:+\"}"
+		fi
+	esac
+	shift 1
+done
+
+exit $status # $SUCCESS unless error occurred with either `-c' or `-x'
+
+################################################################################
+# END
+################################################################################


Property changes on: trunk/usr.sbin/sysrc/sysrc
___________________________________________________________________
Added: svn:keywords
## -0,0 +1 ##
+MidnightBSD=%H
\ No newline at end of property
Added: trunk/usr.sbin/sysrc/sysrc.8
===================================================================
--- trunk/usr.sbin/sysrc/sysrc.8	                        (rev 0)
+++ trunk/usr.sbin/sysrc/sysrc.8	2018-05-24 19:01:32 UTC (rev 9892)
@@ -0,0 +1,478 @@
+.\" Copyright (c) 2011-2015 Devin Teske
+.\" All rights reserved.
+.\"
+.\" Redistribution and use in source and binary forms, with or without
+.\" modification, are permitted provided that the following conditions
+.\" are met:
+.\" 1. Redistributions of source code must retain the above copyright
+.\"    notice, this list of conditions and the following disclaimer.
+.\" 2. Redistributions in binary form must reproduce the above copyright
+.\"    notice, this list of conditions and the following disclaimer in the
+.\"    documentation and/or other materials provided with the distribution.
+.\"
+.\" THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
+.\" ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
+.\" IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
+.\" ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
+.\" FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
+.\" DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
+.\" OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
+.\" HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
+.\" LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
+.\" OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
+.\" SUCH DAMAGE.
+.\"
+.\" $MidnightBSD$
+.\" $FreeBSD: stable/10/usr.sbin/sysrc/sysrc.8 292833 2015-12-28 17:50:31Z dteske $
+.\"
+.Dd September 12, 2015
+.Dt SYSRC 8
+.Os
+.Sh NAME
+.Nm sysrc
+.Nd safely edit system rc files
+.Sh SYNOPSIS
+.Nm
+.Op Fl cdDeEFhinNqvx
+.Op Fl s Ar name
+.Op Fl f Ar file
+.Op Fl j Ar jail | Fl R Ar dir
+.Ar name Ns Op Ns Oo +|- Oc Ns = Ns Ar value
+.Ar ...
+.Nm
+.Op Fl cdDeEFhinNqvx
+.Op Fl s Ar name
+.Op Fl f Ar file
+.Op Fl j Ar jail | Fl R Ar dir
+.Fl a | A
+.Nm
+.Op Fl E
+.Op Fl s Ar name
+.Op Fl f Ar file
+.Fl l
+.Nm
+.Op Fl eEqv
+.Fl L
+.Op Ar name ...
+.Sh DESCRIPTION
+The
+.Nm
+utility retrieves
+.Xr rc.conf 5
+variables from the collection of system rc files and allows processes with
+appropriate privilege to change values in a safe and effective manner.
+.Pp
+The following options are available:
+.Bl -tag -width indent+
+.It Fl a
+Dump a list of all non-default configuration variables.
+.It Fl A
+Dump a list of all configuration variables
+.Pq incl. defaults .
+.It Fl c
+Check only.
+For querying, return success if all requested variables are set
+.Pq even if NULL ,
+otherwise return error status.
+For assignments, return success if no changes are required, otherwise failure.
+If verbose
+.Pq see Dq Fl v
+prints a message stating whether variables are set and/or changes are required.
+.It Fl d
+Print a description of the given variable.
+.It Fl D
+Show default value(s) only (this is the same as setting RC_CONFS to NULL or
+passing `-f' with a NULL file-argument).
+.It Fl e
+Print query results as
+.Xr sh 1
+compatible syntax
+.Pq for example, Ql var=value .
+Ignored if either
+.Ql Fl n
+or
+.Ql Fl F
+is specified.
+.It Fl E
+When given
+.Sq Fl l
+or
+.Sq Fl L
+to list configuration files, only list those that exist.
+When changing a setting, prefer to modify existing files.
+.It Fl f Ar file
+Operate on the specified file(s) instead of the files obtained by reading the
+.Sq rc_conf_files
+entry in the
+.Ev RC_DEFAULTS
+file.
+This option can be specified multiple times for additional files.
+.It Fl F
+Show only the last
+.Xr rc.conf 5
+file each directive is in.
+.It Fl h
+Print a short usage message to stderr and exit.
+.It Fl -help
+Print a full usage statement to stderr and exit.
+.It Fl i
+Ignore unknown variables.
+.It Fl j Ar jail
+The
+.Ar jid
+or name of the
+.Ar jail
+to operate within
+.Pq overrides So Fl R Ar dir Sc ; requires Xr jexec 8 .
+.It Fl l
+List configuration files used at startup on stdout and exit.
+.It Fl L
+List all configuration files including rc.conf.d entries on stdout and exit.
+Can be combined with
+.Sq Fl v
+or
+.Sq Fl e
+to show service names.
+.Nm
+exits with success if all named services are installed, failure otherwise.
+.It Fl n
+Show only variable values, not their names.
+.It Fl N
+Show only variable names, not their values.
+.It Fl q
+Quiet.
+Disable verbose and hide certain errors.
+When combined with
+.Sq Fl L
+and one or more
+.Li Ar name
+arguments, provide only exit status and no output.
+.It Fl R Ar dir
+Operate within the root directory
+.Sq Ar dir
+rather than
+.Sq / .
+.It Fl s Ar name
+If an
+.Li rc.d
+script of
+.Ar name
+exists
+.Po
+in
+.Dq /etc/rc.d
+or
+.Li local_startup
+directories
+.Pc ,
+process its
+.Dq rc.conf.d
+entries as potential overrides to
+.Sq rc_conf_files .
+See
+.Xr rc.subr 8
+for additional information on
+.Dq rc.conf.d .
+Can be combined with
+.Sq Fl l
+to list configuration files used by service at startup.
+.It Fl v
+Verbose.
+Print the pathname of the specific
+.Xr rc.conf 5
+file where the directive was found.
+.It Fl -version
+Print version information to stdout and exit.
+.It Fl x
+Remove variable(s) from specified file(s).
+.El
+.Pp
+This utility has a similar syntax to
+.Xr sysctl 8 .
+It shares the `-e' and `-n' options
+.Pq detailed above
+and also has the same
+.Ql name[=value]
+syntax for making queries/assignments.
+In addition
+.Pq but unlike Xr sysctl 8 ,
+.Ql name+=value
+is supported for adding items to values
+.Pq see APPENDING VALUES
+and
+.Ql name-=value
+is supported for removing items from values
+.Pq see SUBTRACTING VALUES .
+.Pp
+However, while
+.Xr sysctl 8
+serves to query/modify MIBs in the entrant kernel,
+.Nm
+instead works on values in the system
+.Xr rc.conf 5
+configuration files.
+.Pp
+The list of system configuration files is configured in the file
+.Ql /etc/defaults/rc.conf
+within the variable
+.Ql rc_conf_files ,
+which by-default contains a space-separated list of pathnames.
+On all FreeBSD
+systems, this defaults to the value "/etc/rc.conf /etc/rc.conf.local".
+Each
+pathname is sourced in-order upon startup.
+It is in the same fashion that
+.Nm
+sources the configuration files before returning the value of the given
+variable.
+.Pp
+When supplied a variable name,
+.Nm
+will return the value of the variable.
+If the variable does not appear in any
+of the configured
+.Ql rc_conf_files ,
+an error is printed and error status is returned.
+.Pp
+When changing values of a given variable, it does not matter if the variable
+appears in any of the
+.Ql rc_conf_files
+or not.
+If the variable does not appear in any of the files, it is appended to
+the end of the first pathname in the
+.Ql rc_conf_files
+variable.
+Otherwise,
+.Nm
+will replace only the last-occurrence in the last-file found to contain the
+variable.
+This gets the value to take effect next boot without heavily
+modifying these integral files (yet taking care not to allow the file to
+grow unwieldy should
+.Nm
+be called repeatedly).
+.Sh APPENDING VALUES
+When using the
+.Ql key+=value
+syntax to add items to existing values,
+the first character of the value is taken as the delimiter separating items
+.Pq usually Qo \  Qc or Qo , Qc .
+For example, in the following statement:
+.Bl -item -offset indent
+.It
+.Nm
+cloned_interfaces+=" gif0"
+.El
+.Pp
+the first character is a space, informing
+.Nm
+that existing values are to be considered separated by whitespace.
+If
+.Ql gif0
+is not found in the existing value for
+.Va cloned_interfaces ,
+it is added
+.Pq with delimiter only if existing value is non-NULL .
+.Pp
+For convenience, if the first character is alpha-numeric
+.Pq letters A-Z, a-z, or numbers 0-9 ,
+.Nm
+uses the default setting of whitespace as separator.
+For example, the above and below statements are equivalent since
+.Dq gif0
+starts with an alpha-numeric character
+.Pq the letter Li g :
+.Bl -item -offset indent
+.It
+.Nm
+cloned_interfaces+=gif0
+.El
+.Pp
+Take the following sequence for example:
+.Bl -item -offset indent
+.It
+.Nm
+cloned_interfaces= # start with NULL
+.It
+.Nm
+cloned_interfaces+=gif0
+.Dl # NULL -> `gif0' Pq NB: no preceding delimiter
+.It
+.Nm
+cloned_interfaces+=gif0 # no change
+.It
+.Nm
+cloned_interfaces+="tun0 gif0"
+.Dl # `gif0' -> `gif0 tun0' Pq NB: no duplication
+.El
+.Pp
+.Nm
+prevents the same value from being added if already there.
+.Sh SUBTRACTING VALUES
+When using the
+.Ql key-=value
+syntax to remove items from existing values,
+the first character of the value is taken as the delimiter separating items
+.Pq usually Qo \  Qc or Qo , Qc .
+For example, in the following statement:
+.Pp
+.Dl Nm cloned_interfaces-=" gif0"
+.Pp
+the first character is a space, informing
+.Nm
+that existing values are to be considered separated by whitespace.
+If
+.Ql gif0
+is found in the existing value for
+.Va cloned_interfaces ,
+it is removed
+.Pq extra delimiters removed .
+.Pp
+For convenience, if the first character is alpha-numeric
+.Pq letters A-Z, a-z, or numbers 0-9 ,
+.Nm
+uses the default setting of whitespace as separator.
+For example, the above and below statements are equivalent since
+.Dq gif0
+starts with an alpha-numeric character
+.Pq the letter Li g :
+.Bl -item -offset indent
+.It
+.Nm
+cloned_interfaces-=gif0
+.El
+.Pp
+Take the following sequence for example:
+.Bl -item -offset indent
+.It
+.Nm
+foo="bar baz" # start
+.It
+.Nm
+foo-=bar # `bar baz' -> `baz'
+.It
+.Nm
+foo-=baz # `baz' -> NULL
+.El
+.Pp
+.Nm
+removes all occurrences of all items provided
+and collapses extra delimiters between items.
+.Sh ENVIRONMENT
+The following environment variables are referenced by
+.Nm :
+.Bl -tag -width ".Ev RC_DEFAULTS"
+.It Ev RC_CONFS
+Override default
+.Ql rc_conf_files
+.Pq even if set to NULL .
+.It Ev RC_DEFAULTS
+Location of
+.Ql /etc/defaults/rc.conf
+file.
+.El
+.Sh DEPENDENCIES
+The following standard commands are required by
+.Nm :
+.Pp
+.Xr awk 1 ,
+.Xr cat 1 ,
+.Xr chmod 1 ,
+.Xr env 1 ,
+.Xr grep 1 ,
+.Xr jls 1 ,
+.Xr mktemp 1 ,
+.Xr mv 1 ,
+.Xr rm 1 ,
+.Xr sh 1 ,
+.Xr stat 1 ,
+.Xr tail 1 ,
+.Xr chown 8
+and
+.Xr jexec 8 .
+.Sh FILES
+.Bl -tag -width ".Pa /etc/defaults/rc.conf" -compact
+.It Pa /etc/defaults/rc.conf
+.It Pa /etc/rc.conf
+.It Pa /etc/rc.conf.local
+.It Pa /etc/rc.conf.d/name
+.It Pa /etc/rc.conf.d/name/*
+.It Pa /usr/local/etc/rc.conf.d/name
+.It Pa /usr/local/etc/rc.conf.d/name/*
+.El
+.Sh EXAMPLES
+Below are some simple examples of how
+.Nm
+can be used to query certain values from the
+.Xr rc.conf 5
+collection of system configuration files:
+.Pp
+.Nm
+sshd_enable
+.Dl returns the value of $sshd_enable, usually YES or NO .
+.Pp
+.Nm
+defaultrouter
+.Dl returns IP address of default router Pq if configured .
+.Pp
+Working on other files, such as
+.Xr crontab 5 :
+.Pp
+.Nm
+-f /etc/crontab MAILTO
+.Dl returns the value of the MAILTO setting Pq if configured .
+.Pp
+Appending to existing values:
+.Pp
+.Nm
+\&cloned_interfaces+=gif0
+.Dl appends Qo gif0 Qc to $cloned_interfaces Pq see APPENDING VALUES .
+.Pp
+.Nm
+\&cloned_interfaces-=gif0
+.Dl removes Qo gif0 Qc from $cloned_interfaces Pq see SUBTRACTING VALUES .
+.Pp
+In addition to the above syntax,
+.Nm
+also supports inline
+.Xr sh 1
+PARAMETER expansion for changing the way values are reported, shown below:
+.Pp
+.Nm
+\&'hostname%%.*'
+.Dl returns $hostname up to (but not including) first `.' .
+.Pp
+.Nm
+\&'network_interfaces%%[$IFS]*'
+.Dl returns first word of $network_interfaces .
+.Pp
+.Nm
+\&'ntpdate_flags##*[$IFS]'
+.Dl returns last word of $ntpdate_flags (time server address) .
+.Pp
+.Nm
+usbd_flags-"default"
+.Dl returns $usbd_flags or "default" if unset or NULL .
+.Pp
+.Nm
+cloned_interfaces+"alternate"
+.Dl returns "alternate" if $cloned_interfaces is set .
+.Sh SEE ALSO
+.Xr jls 1 ,
+.Xr rc.conf 5 ,
+.Xr rc.subr 8 ,
+.Xr jail 8 ,
+.Xr jexec 8 ,
+.Xr rc 8 ,
+.Xr sysctl 8
+.Sh HISTORY
+A
+.Nm
+utility first appeared in
+.Fx 9.2 .
+.Sh AUTHORS
+.An Devin Teske Aq dteske at FreeBSD.org
+.Sh THANKS TO
+Brandon Gooch, Garrett Cooper, Julian Elischer, Pawel Jakub Dawidek,
+Cyrille Lefevre, Ross West, Stefan Esser, Marco Steinbach, Jilles Tjoelker,
+Allan Jude, and Lars Engels for suggestions, help, and testing.


Property changes on: trunk/usr.sbin/sysrc/sysrc.8
___________________________________________________________________
Added: svn:eol-style
## -0,0 +1 ##
+native
\ No newline at end of property
Added: svn:keywords
## -0,0 +1 ##
+MidnightBSD=%H
\ No newline at end of property
Added: svn:mime-type
## -0,0 +1 ##
+text/plain
\ No newline at end of property
Deleted: trunk/usr.sbin/sysrc/sysrc.sh
===================================================================
--- trunk/usr.sbin/sysrc/sysrc.sh	2018-05-24 18:53:26 UTC (rev 9891)
+++ trunk/usr.sbin/sysrc/sysrc.sh	2018-05-24 19:01:32 UTC (rev 9892)
@@ -1,1126 +0,0 @@
-#!/bin/sh
-# -*- tab-width:  4 -*- ;; Emacs
-# vi: set tabstop=4     :: Vi/ViM
-#
-# Revision: 3.2
-# Last Modified: January 15th, 2011
-############################################################ COPYRIGHT
-#
-# (c)2010-2011. Devin Teske. All Rights Reserved.
-#
-# Redistribution and use in source and binary forms, with or without
-# modification, are permitted provided that the following conditions
-# are met:
-# 1. Redistributions of source code must retain the above copyright
-#    notice, this list of conditions and the following disclaimer.
-# 2. Redistributions in binary form must reproduce the above copyright
-#    notice, this list of conditions and the following disclaimer in the
-#    documentation and/or other materials provided with the distribution.
-#
-# THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
-# ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING BUT NOT LIMITED TO, THE
-# IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
-# ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
-# FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
-# DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
-# OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
-# HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
-# LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
-# OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
-# SUCH DAMAGE.
-#
-# AUTHOR      DATE      DESCRIPTION
-# dteske   2011.01.15   Make `-A' override `-a' despite order-of-appearance.
-# dteske   2011.01.15   Add `-x' to remove variables from file(s).
-# dteske   2010.12.09   Fix taint-checking to not die on non-existent files.
-# dteske   2010.11.09   Minor fixes to sysrc_set/sysrc_set_awk.
-# dteske   2010.11.08   Further significant performance enhancements.
-# dteske   2010.11.04   Add taint checking.
-# dteske   2010.11.04   Comments.
-# dteske   2010.11.04   Significant performance enhancements.
-# dteske   2010.11.02   Fix quotes when replacing null assignment.
-# dteske   2010.11.02   Preserve leading/trailing whitespace in sysrc_set().
-# dteske   2010.11.02   Deprecate lrev() in favor of tail(1)'s `-r' flag.
-# dteske   2010.11.02   Map `-R dir' to `-j jail' if `dir' maps to a single
-#                       running jail (or produce an error if `dir' maps to
-#                       many running jails).
-# dteske   2010.10.20   Make `-j jail' and `-R dir' more secure
-# dteske   2010.10.19   Add `-j jail' for operating on jails (see jexec(8)).
-# dteske   2010.10.18   Add `-R dir' for operating in different root-dir.
-# dteske   2010.10.13   Allow `-f file' multiple times.
-# dteske   2010.10.12   Updates per freebsd-hackers thread.
-# dteske   2010.09.29   Initial version.
-#
-############################################################ INFORMATION
-#
-# Command Usage:
-#
-#   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.
-#   	-i         Ignore unknown variables.
-#   	-n         Show only variable values, not their names.
-#   	-N         Show only variable names, not their values.
-#   	-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)).
-# 
-#   ENVIRONMENT:
-#   	RC_DEFAULTS      Location of `/etc/defaults/rc.conf' file.
-#   	SYSRC_VERBOSE    Default verbosity. Set to non-NULL to enable.
-#
-#
-# Dependencies (sorted alphabetically):
-#
-#    awk(1)    cat(1)     chmod(1)   chown(8)    chroot(8)   env(1)
-#    grep(1)   jexec(8)   jls(8)*    mktemp(1)   mv(1)       rm(1)
-#    sh(1)     stat(1)    tail(1)
-#
-# *optional
-#
-# Special Thanks to (in order of appearance):
-# 	Brandon Gooch <jamesbrandongooch at gmail.com> - name suggestion
-# 	Garrett Cooper <gcooper at freebsd.org> - structure/mktemp suggestions
-# 	Julian Elischer <julian at freebsd.org> - jail suggestions
-# 	Pawel Jakub Dawidek <pjd at freebsd.org> - security suggestions
-# 	Cyrille Lefevre <cyrille.lefevre-lists at laposte.net> - awk suggestions
-# 	Ross West <westr at connection.ca> - taint-checking bug report
-#
-############################################################ CONFIGURATION
-
-#
-# Default verbosity.
-#
-: ${SYSRC_VERBOSE:=}
-
-#
-# Default location of the rc.conf(5) defaults configuration file.
-#
-: ${RC_DEFAULTS:="/etc/defaults/rc.conf"}
-
-############################################################ GLOBALS
-
-#
-# Global exit status variables
-#
-SUCCESS=0
-FAILURE=1
-
-#
-# Program name
-#
-progname="${0##*/}"
-
-#
-# Options
-#
-DELETE=
-DESCRIBE=
-IGNORE_UNKNOWNS=
-JAIL=
-RC_CONFS=
-ROOTDIR=
-SHOW_ALL=
-SHOW_EQUALS=
-SHOW_NAME=1
-SHOW_VALUE=1
-
-############################################################ FUNCTIONS
-
-# have $anything
-#
-# A wrapper to the `type' built-in. Returns true if argument is a valid shell
-# built-in, keyword, or externally-tracked binary, otherwise false.
-#
-have()
-{
-	type "$@" > /dev/null 2>&1
-}
-
-# fprintf $fd $fmt [ $opts ... ]
-#
-# 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.
-#
-fprintf()
-{
-	local fd=$1
-	[ $# -gt 1 ] || return $FAILURE
-	shift 1
-	printf "$@" >&$fd
-}
-
-# eprintf $fmt [ $opts ... ]
-#
-# Print a message to stderr (fd=2).
-#
-eprintf()
-{
-	fprintf 2 "$@"
-}
-
-# die [ $fmt [ $opts ... ]]
-#
-# Optionally print a message to stderr before exiting with failure status.
-#
-die()
-{
-	local fmt="$1"
-	[ $# -gt 0 ] && shift 1
-	[  "$fmt"  ] && eprintf "$fmt\n" "$@"
-
-	exit $FAILURE
-}
-
-# usage
-#
-# Prints a short syntax statement and exits.
-#
-usage()
-{
-	local optfmt="\t%-11s%s\n"
-	local envfmt="\t%-17s%s\n"
-
-	eprintf "Usage: %s [OPTIONS] name[=value] ...\n" "$progname"
-
-	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."
-
-	die
-}
-
-# 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
-# `--except' is required to enable the exclusion-list as the remainder of
-# positional arguments.
-#
-# Be careful not to call this in a shell that you still expect to perform
-# $PATH expansion in, because this will blow $PATH away. This is best used
-# within a sub-shell block "(...)" or "$(...)" or "`...`".
-#
-clean_env()
-{
-	local var arg except=
-
-	#
-	# Should we process an exclusion-list?
-	#
-	if [ "$1" = "--except" ]; then
-		except=1
-		shift 1
-	fi
-
-	#
-	# Loop over a list of variable names from set(1) built-in.
-	#
-	for var in $( set | awk -F= \
-		'/^[[:alpha:]_][[:alnum:]_]*=/ {print $1}' \
-		| grep -v '^except$'
-	); do
-		#
-		# In POSIX bourne-shell, attempting to unset(1) OPTIND results
-		# in "unset: Illegal number:" and causes abrupt termination.
-		#
-		[ "$var" = OPTIND ] && continue
-
-		#
-		# Process the exclusion-list?
-		#
-		if [ "$except" ]; then
-			for arg in "$@" ""; do
-				[ "$var" = "$arg" ] && break
-			done
-			[ "$arg" ] && continue
-		fi
-
-		unset "$var"
-	done
-}
-
-# sysrc_get $varname
-#
-# Get a system configuration setting from the collection of system-
-# configuration files (in order: /etc/defaults/rc.conf /etc/rc.conf
-# and /etc/rc.conf).
-#
-# NOTE: Additional shell parameter-expansion formats are supported. For
-# example, passing an argument of "hostname%%.*" (properly quoted) will
-# return the hostname up to (but not including) the first `.' (see sh(1),
-# "Parameter Expansion" for more information on additional formats).
-#
-sysrc_get()
-{
-	# Sanity check
-	[ -f "$RC_DEFAULTS" -a -r "$RC_DEFAULTS" ] || return $FAILURE
-
-	# Taint-check variable name
-	case "$1" in
-	[0-9]*)
-		# Don't expand possible positional parameters
-		return $FAILURE;;
-	*)
-		[ "$1" ] || return $FAILURE
-	esac
-
-	( # Execute within sub-shell to protect parent environment
-
-		#
-		# Clear the environment of all variables, preventing the
-		# expansion of normals such as `PS1', `TERM', etc.
-		#
-		clean_env --except RC_CONFS RC_DEFAULTS
-
-		. "$RC_DEFAULTS" > /dev/null 2>&1
-
-		#
-		# If the query is for `rc_conf_files' then store the value that
-		# we inherited from sourcing RC_DEFAULTS (above) so that we may
-		# conditionally restore this value after source_rc_confs in the
-		# event that RC_CONFS does not customize the value.
-		#
-		if [ "$1" = "rc_conf_files" ]; then
-			_rc_conf_files="$rc_conf_files"
-		fi
-
-		#
-		# 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"
-
-		source_rc_confs > /dev/null 2>&1
-
-		#
-		# If the query was for `rc_conf_files' AND after calling
-		# source_rc_confs the value has not changed, then we should
-		# restore the value to the one inherited from RC_DEFAULTS
-		# before performing the final query (preventing us from
-		# returning what was passed in via `-f' when the intent was
-		# instead to query the value from the file(s) specified).
-		#
-		if [ "$1" = "rc_conf_files" -a \
-		     "$RC_CONFS" != "" -a \
-		     "$rc_conf_files" = "$RC_CONFS" \
-		]; then
-			rc_conf_files="$_rc_conf_files"
-			unset _rc_conf_files
-		fi
-
-		unset RC_CONFS
-			# no longer needed
-
-		#
-		# 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).
-		#
-		eval echo '"${'"$1"'}"' 2> /dev/null
-	)
-}
-
-# sysrc_find $varname
-#
-# Find which file holds the effective last-assignment to a given variable
-# within the rc.conf(5) file(s).
-#
-# If the variable is found in any of the rc.conf(5) files, the function prints
-# the filename it was found in and then returns success. Otherwise output is
-# NULL and the function returns with error status.
-#
-sysrc_find()
-{
-	local varname="$1"
-	local regex="^[[:space:]]*$varname="
-	local rc_conf_files="$( sysrc_get rc_conf_files )"
-	local conf_files=
-	local file
-
-	# Check parameters
-	[ "$varname" ] || return $FAILURE
-
-	#
-	# 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"
-
-	#
-	# Reverse the order of files in rc_conf_files (the boot process sources
-	# these in order, so we will search them in reverse-order to find the
-	# last-assignment -- the one that ultimately effects the environment).
-	#
-	for file in $rc_conf_files; do
-		conf_files="$file${conf_files:+ }$conf_files"
-	done
-
-	#
-	# Append the defaults file (since directives in the defaults file
-	# indeed affect the boot process, we'll want to know when a directive
-	# is found there).
-	#
-	conf_files="$conf_files${conf_files:+ }$RC_DEFAULTS"
-
-	#
-	# Find which file matches assignment to the given variable name.
-	#
-	for file in $conf_files; do
-		[ -f "$file" -a -r "$file" ] || continue
-		if grep -Eq "$regex" $file; then
-			echo $file
-			return $SUCCESS
-		fi
-	done
-
-	return $FAILURE # Not found
-}
-
-# 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
-# does not appear in the source file, it is appended to the end of the primary
-# system configuration file `/etc/rc.conf'.
-#
-# 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='
-# Variables that should be defined on the invocation line:
-# 	-v varname="varname"
-# 	-v new_value="new_value"
-#
-BEGIN {
-	regex = "^[[:space:]]*"varname"="
-	found = retval = 0
-}
-{
-	# If already found... just spew
-	if ( found ) { print; next }
-
-	# Does this line match an assignment to our variable?
-	if ( ! match($0, regex) ) { print; next }
-
-	# Save important match information
-	found = 1
-	matchlen = RSTART + RLENGTH - 1
-
-	# Store the value text for later munging
-	value = substr($0, matchlen + 1, length($0) - matchlen)
-
-	# Store the first character of the value
-	t1 = t2 = substr(value, 0, 1)
-
-	# Assignment w/ back-ticks, expression, or misc.
-	# We ignore these since we did not generate them
-	#
-	if ( t1 ~ /[`$\\]/ ) { retval = 1; print; next }
-
-	# Assignment w/ single-quoted value
-	else if ( t1 == "'\''" ) {
-		sub(/^'\''[^'\'']*/, "", value)
-		if ( length(value) == 0 ) t2 = ""
-		sub(/^'\''/, "", value)
-	}
-
-	# Assignment w/ double-quoted value
-	else if ( t1 == "\"" ) {
-		sub(/^"(.*\\\\+")*[^"]*/, "", value)
-		if ( length(value) == 0 ) t2 = ""
-		sub(/^"/, "", value)
-	}
-
-	# Assignment w/ non-quoted value
-	else if ( t1 ~ /[^[:space:];]/ ) {
-		t1 = t2 = "\""
-		sub(/^[^[:space:]]*/, "", value)
-	}
-
-	# Null-assignment
-	else if ( t1 ~ /[[:space:];]/ ) { t1 = t2 = "\"" }
-
-	printf "%s%c%s%c%s\n", substr($0, 0, matchlen), \
-		t1, new_value, t2, value
-}
-END { exit retval }
-'
-sysrc_set()
-{
-	local varname="$1" new_value="$2"
-
-	# Check arguments
-	[ "$varname" ] || return $FAILURE
-
-	#
-	# Find which rc.conf(5) file contains the last-assignment
-	#
-	local not_found=
-	local file="$( sysrc_find "$varname" )"
-	if [ "$file" = "$RC_DEFAULTS" -o ! "$file" ]; then
-		#
-		# We either got a null response (not found) or the variable
-		# was only found in the rc.conf(5) defaults. In either case,
-		# let's instead modify the first file from $rc_conf_files.
-		#
-
-		not_found=1
-
-		#
-		# If `-f file' was passed, use $RC_CONFS
-		# rather than $rc_conf_files.
-		#
-		if [ "$RC_CONFS" ]; then
-			file="${RC_CONFS%%[$IFS]*}"
-		else
-			file=$( sysrc_get "rc_conf_files%%[$IFS]*" )
-		fi
-	fi
-
-	#
-	# If not found, append new value to last file and return.
-	#
-	if [ "$not_found" ]; then
-		echo "$varname=\"$new_value\"" >> "$file"
-		return $SUCCESS
-	fi
-
-	#
-	# Perform sanity checks.
-	#
-	if [ ! -w "$file" ]; then
-		eprintf "\n%s: cannot create %s: Permission denied\n" \
-		        "$progname" "$file"
-		return $FAILURE
-	fi
-
-	#
-	# Create a new temporary file to write to.
-	#
-	local tmpfile="$( mktemp -t "$progname" )"
-	[ "$tmpfile" ] || return $FAILURE
-
-	#
-	# Fixup permissions (else we're in for a surprise, as mktemp(1) creates
-	# the temporary file with 0600 permissions, and if we simply mv(1) the
-	# temporary file over the destination, the destination will inherit the
-	# permissions from the temporary file).
-	#
-	chmod "$( stat -f '%#Lp' "$file" )" "$tmpfile" 2> /dev/null
-
-	#
-	# Fixup ownerhsip. 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
-
-	#
-	# Operate on the matching file, replacing only the last occurrence.
-	#
-	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" )
-	retval=$?
-
-	#
-	# Write the temporary file contents.
-	#
-	echo "$new_contents" | tail -r > "$tmpfile" || return $FAILURE
-	if [ $retval -ne $SUCCESS ]; then
-		echo "$varname=\"$new_value\"" >> "$tmpfile"
-	fi
-
-	#
-	# Taint-check our results.
-	#
-	if ! /bin/sh -n "$tmpfile"; then
-		eprintf "%s: Not overwriting \`%s' due to %s\n" \
-		        "$progname" "$file" "previous syntax errors"
-		rm -f "$tmpfile"
-		return $FAILURE
-	fi
-
-	#
-	# Finally, move the temporary file into place.
-	#
-	mv "$tmpfile" "$file"
-}
-
-# 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).
-#
-# Multi-line comments are joined together. Results are NULL if no description
-# could be found.
-#
-# 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='
-# Variables that should be defined on the invocation line:
-# 	-v varname="varname"
-#
-BEGIN {
-	regex = "^[[:space:]]*"varname"="
-	found = 0
-	buffer = ""
-}
-{
-	if ( ! found )
-	{
-		if ( ! match($0, regex) ) next
-
-		found = 1
-		sub(/^[^#]*(#[[:space:]]*)?/, "")
-		buffer = $0
-		next
-	}
-
-	if ( !/^[[:space:]]*#/ ||
-	      /^[[:space:]]*[[:alpha:]_][[:alnum:]_]*=/ ||
-	      /^[[:space:]]*#[[:alpha:]_][[:alnum:]_]*=/ ||
-	      /^[[:space:]]*$/ ) exit
-
-	sub(/(.*#)*[[:space:]]*/, "")
-	buffer = buffer" "$0
-}
-END {
-	# Clean up the buffer
-	sub(/^[[:space:]]*/, "", buffer)
-	sub(/[[:space:]]*$/, "", buffer)
-
-	print buffer
-	exit ! found
-}
-'
-sysrc_desc()
-{
-	awk -v varname="$1" "$sysrc_desc_awk" < "$RC_DEFAULTS"
-}
-
-# sysrc_delete $varname
-#
-# Remove a setting from the system configuration files (edits files in-place).
-# Deletes all assignments to the given variable in all config files. If the
-# `-f file' option is passed, the removal is restricted to only those files
-# specified, otherwise the system collection of rc_conf_files is used.
-#
-# This function is a two-parter. Below is the awk(1) portion of the function,
-# afterward is the sh(1) function which utilizes the below awk script.
-#
-sysrc_delete_awk='
-# Variables that should be defined on the invocation line:
-# 	-v varname="varname"
-#
-BEGIN {
-	regex = "^[[:space:]]*"varname"="
-	found = 0
-}
-{
-	if ( $0 ~ regex )
-		found = 1
-	else
-		print
-}
-END { exit ! found }
-'
-sysrc_delete()
-{
-	local varname="$1"
-	local file
-
-	# Check arguments
-	[ "$varname" ] || return $FAILURE
-
-	#
-	# Operate on each of the specified files
-	#
-	for file in ${RC_CONFS:-$( sysrc_get rc_conf_files )}; do
-		#
-		# Create a new temporary file to write to.
-		#
-		local tmpfile="$( mktemp -t "$progname" )"
-		[ "$tmpfile" ] || return $FAILURE
-
-		#
-		# Fixup permissions and ownership (mktemp(1) defaults to 0600
-		# permissions) to instead match the destination file.
-		#
-		chmod "$( stat -f '%#Lp' "$file" )" "$tmpfile" 2> /dev/null
-		chmod "$( stat -f '%u:%g' "$file" )" "$tmpfile" 2> /dev/null
-
-		#
-		# Operate on the file, removing all occurrences, saving the
-		# output in our temporary file.
-		#
-		awk -v varname="$varname" "$sysrc_delete_awk" "$file" \
-			> "$tmpfile"
-		if [ $? -ne $SUCCESS ]; then
-			# The file didn't contain any assignments
-			rm -f "$tmpfile"
-			continue
-		fi
-
-		#
-		# Taint-check our results.
-		#
-		if ! /bin/sh -n "$tmpfile"; then
-			eprintf "%s: Not overwriting \`%s' due to %s\n" \
-			        "$progname" "$file" "previous syntax errors"
-			rm -f "$tmpfile"
-			continue
-		fi
-
-		#
-		# Perform sanity checks
-		#
-		if [ ! -w "$file" ]; then
-			eprintf "%s: %s: Permission denied\n" \
-			        "$progname" "$file"
-			rm -f "$tmpfile"
-			continue
-		fi
-
-		#
-		# If verbose, now's the time to show it.
-		# 
-		if [ "$SYSRC_VERBOSE" ]; then
-			echo -n "$file: "
-
-			#
-			# If `-N' is passed, simplify the output
-			#
-			if [ ! "$SHOW_VALUE" ]; then
-				echo "$NAME"
-				continue
-			fi
-
-			echo -n "${SHOW_NAME:+$NAME$SEP}"
-			( # Operate in sub-shell to protect parent environment
-				. "$file" 2> /dev/null
-				eval echo -n '"${'"$NAME"'}"' 2> /dev/null
-			)
-			echo "${SHOW_EQUALS:+\"}"
-		fi
-
-		#
-		# Finally, move the temporary file into place.
-		#
-		mv "$tmpfile" "$file"
-	done
-}
-
-############################################################ MAIN SOURCE
-
-#
-# Perform sanity checks
-#
-[ $# -gt 0 ] || usage
-
-#
-# Process command-line options
-#
-while getopts hf:aAxXdevinNR:j: flag; do
-	case "$flag" in
-	h) usage;;
-	f) [ "$OPTARG" ] || die \
-	   	"%s: Missing or null argument to \`-f' flag" "$progname"
-	   RC_CONFS="$RC_CONFS${RC_CONFS:+ }$OPTARG";;
-	a) SHOW_ALL=${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;;
-	i) IGNORE_UNKNOWNS=1;;
-	n) SHOW_NAME=;;
-	N) SHOW_VALUE=;;
-	R) [ "$OPTARG" ] || die \
-	   	"%s: Missing or null argument to \`-R' flag" "$progname"
-	   ROOTDIR="$OPTARG";;
-	j) [ "$OPTARG" ] || die \
-	   	"%s: Missing or null argument to \`-j' flag" "$progname"
-	   JAIL="$OPTARG";;
-	\?) usage;;
-	esac
-done
-shift $(( $OPTIND - 1 ))
-
-#
-# 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"
-
-#
-# Process `-x' (and secret `-X') command-line options
-#
-errmsg="$progname: \`-x' option incompatible with \`-a'/\`-A' options"
-errmsg="$errmsg (use \`-X' to override)"
-if [ "$DELETE" -a "$SHOW_ALL" ]; then
-	[ "$DELETE" = "2" ] || die "$errmsg"
-fi
-
-#
-# Process `-e', `-n', and `-N' command-line options
-#
-SEP=': '
-[ "$SHOW_EQUALS" ] && SEP='="'
-[ "$SHOW_NAME" ] || SHOW_EQUALS=
-[ "$SYSRC_VERBOSE" = "0" ] && SYSRC_VERBOSE=
-if [ ! "$SHOW_VALUE" ]; then
-	SHOW_NAME=1
-	SHOW_EQUALS=
-fi
-
-#
-# Process `-j jail' and `-R dir' command-line options
-#
-if [ "$JAIL" -o "$ROOTDIR" ]; then
-	#
-	# Reconstruct the arguments that we want to carry-over
-	#
-	args="
-		${SYSRC_VERBOSE:+-v}
-		${RC_CONFS:+-f'$RC_CONFS'}
-		$( [ "$DELETE" = "1" ] && echo \ -x )
-		$( [ "$DELETE" = "2" ] && echo \ -X )
-		$( [ "$SHOW_ALL" = "1" ] && echo \ -a )
-		$( [ "$SHOW_ALL" = "2" ] && echo \ -A )
-		${DESCRIBE:+-d}
-		${SHOW_EQUALS:+-e}
-		${IGNORE_UNKNOWNS:+-i}
-		$( [ "$SHOW_NAME"  ] || echo \ -n )
-		$( [ "$SHOW_VALUE" ] || echo \ -N )
-	"
-	for arg in "$@"; do
-		args="$args '$arg'"
-	done
-
-	#
-	# If both are supplied, `-j jail' supercedes `-R dir'
-	#
-	if [ "$JAIL" ]; then
-		#
-		# Re-execute ourselves with sh(1) via jexec(8)
-		#
-		( echo set -- $args
-		  cat $0
-		) | env - RC_DEFAULTS="$RC_DEFAULTS" \
-		    	/usr/sbin/jexec "$JAIL" /bin/sh
-		exit $?
-	elif [ "$ROOTDIR" ]; then
-		#
-		# Make sure that the root directory specified is not to any
-		# running jails.
-		#
-		# NOTE: To maintain backward compatibility with older jails on
-		# older systems, we will not perform this check if either the
-		# jls(8) or jexec(8) utilities are missing.
-		#
-		if have jexec && have jls; then
-			jid="`jls jid path | \
-			(
-				while read JID JROOT; do
-					[ "$JROOT" = "$ROOTDIR" ] || continue
-					echo $JID
-				done
-			)`"
-
-			#
-			# If multiple running jails match the specified root
-			# 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)" )"
-			fi
-
-			#
-			# If only a single running jail matches the specified
-			# root directory, implicitly use `-j jail'.
-			#
-			if [ "$jid" ]; then
-				#
-				# Re-execute outselves with sh(1) via jexec(8)
-				#
-				( echo set -- $args
-				  cat $0
-				) | env - RC_DEFAULTS="$RC_DEFAULTS" \
-					/usr/sbin/jexec "$jid" /bin/sh
-				exit $?
-			fi
-
-			# Otherwise, fall through and allow chroot(8)
-		fi
-
-		#
-		# Re-execute ourselves with sh(1) via chroot(8)
-		#
-		( echo set -- $args
-		  cat $0
-		) | env - RC_DEFAULTS="$RC_DEFAULTS" \
-		    	/usr/sbin/chroot "$ROOTDIR" /bin/sh
-		exit $?
-	fi
-fi
-
-#
-# Process `-a' or `-A' command-line options
-#
-if [ "$SHOW_ALL" ]; then
-	#
-	# Get a list of variables that are currently set in the rc.conf(5)
-	# files (included `/etc/defaults/rc.conf') by performing a call to
-	# source_rc_confs() in a clean environment.
-	#
-	( # Operate in a sub-shell to protect the parent environment
-		#
-		# Set which variables we want to preserve in the environment.
-		# 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
-		# 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"
-
-		#
-		# Clean the environment (except for our required variables)
-		# and then source the required files.
-		#
-		clean_env --except $EXCEPT
-		if [ -f "$RC_DEFAULTS" -a -r "$RC_DEFAULTS" ]; then
-			. "$RC_DEFAULTS"
-
-			#
-			# If passed `-a' (rather than `-A'), re-purge the
-			# environment, removing the rc.conf(5) defaults.
-			#
-			[ "$SHOW_ALL" = "1" ] \
-				&& 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"
-
-			source_rc_confs
-
-			#
-			# If passed `-a' (rather than `-A'), remove
-			# `rc_conf_files' unless it was defined somewhere
-			# other than rc.conf(5) defaults.
-			#
-			[ "$SHOW_ALL" = "1" -a \
-			  "$( 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)$"
-		); do
-			#
-			# If enabled, describe rather than expand value
-			#
-			if [ "$DESCRIBE" ]; then
-				echo "$NAME: $( sysrc_desc "$NAME" )"
-				continue
-			fi
-
-			#
-			# If `-X' is passed, delete the variables
-			#
-			if [ "$DELETE" = "2" ]; then
-				sysrc_delete "$NAME"
-				continue
-			fi
-
-			[ "$SYSRC_VERBOSE" ] && \
-				echo -n "$( sysrc_find "$NAME" ): "
-
-			#
-			# If `-N' is passed, simplify the output
-			#
-			if [ ! "$SHOW_VALUE" ]; then
-				echo "$NAME"
-				continue
-			fi
-
-			echo "${SHOW_NAME:+$NAME$SEP}$(
-			      sysrc_get "$NAME" )${SHOW_EQUALS:+\"}"
-
-		done
-	)
-
-	#
-	# Ignore the remainder of positional arguments.
-	#
-	exit $SUCCESS
-fi
-
-#
-# Process command-line arguments
-#
-while [ $# -gt 0 ]; do
-	NAME="${1%%=*}"
-
-	[ "$DESCRIBE" ] && \
-		echo "$NAME: $( sysrc_desc "$NAME" )"
-
-	case "$1" in
-	*=*)
-		#
-		# Like sysctl(8), if both `-d' AND "name=value" is passed,
-		# first describe, then attempt to set
-		#
-
-		if [ "$SYSRC_VERBOSE" ]; then
-			file=$( sysrc_find "$NAME" )
-			[ "$file" = "$RC_DEFAULTS" -o ! "$file" ] && \
-				file=$( sysrc_get "rc_conf_files%%[$IFS]*" )
-			echo -n "$file: "
-		fi
-
-		#
-		# If `-x' or `-X' is passed, delete the variable and ignore the
-		# desire to set some value
-		#
-		if [ "$DELETE" ]; then
-			sysrc_delete "$NAME"
-			shift 1
-			continue
-		fi
-
-		#
-		# If `-N' is passed, simplify the output
-		#
-		if [ ! "$SHOW_VALUE" ]; then
-			echo "$NAME"
-			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" )"
-			fi
-		fi
-		;;
-	*)
-		if ! IGNORED="$( sysrc_get "$NAME?" )"; then
-			[ "$IGNORE_UNKNOWNS" ] \
-				|| echo "$progname: unknown variable '$NAME'"
-			shift 1
-			continue
-		fi
-
-		#
-		# Like sysctl(8), when `-d' is passed,
-		# desribe it rather than expanding it
-		#
-
-		if [ "$DESCRIBE" ]; then
-			shift 1
-			continue
-		fi
-
-		#
-		# If `-x' or `-X' is passed, delete the variable
-		#
-		if [ "$DELETE" ]; then
-			sysrc_delete "$NAME"
-			shift 1
-			continue
-		fi
-
-		[ "$SYSRC_VERBOSE" ] && \
-			echo -n "$( sysrc_find "$NAME" ): "
-
-		#
-		# If `-N' is passed, simplify the output
-		#
-		if [ ! "$SHOW_VALUE" ]; then
-			echo "$NAME"
-		else
-			echo "${SHOW_NAME:+$NAME$SEP}$(
-			      sysrc_get "$NAME" )${SHOW_EQUALS:+\"}"
-		fi
-	esac
-	shift 1
-done



More information about the Midnightbsd-cvs mailing list