summaryrefslogtreecommitdiffstats
path: root/usr.sbin/sysrc
diff options
context:
space:
mode:
authordteske <dteske@FreeBSD.org>2015-03-23 19:12:55 +0000
committerdteske <dteske@FreeBSD.org>2015-03-23 19:12:55 +0000
commitfb3af63da5f1d38669584e79f3488abc792abec5 (patch)
tree93d474a76c91f442e71ba4a5f5a140e1488add98 /usr.sbin/sysrc
parent3ce68975f29c670c2a6b0d08841ef28de5d3d3c7 (diff)
downloadFreeBSD-src-fb3af63da5f1d38669584e79f3488abc792abec5.zip
FreeBSD-src-fb3af63da5f1d38669584e79f3488abc792abec5.tar.gz
MFC revisions 274068, 274119, 279624:
r274068: Add key+=append syntax to sysrc(8) r279624: Add key-=remove syntax r274119: Add EXAMPLES-section entries for new syntax Reviewed by: shurd (r274068) Thanks to: seanc Relnotes: sysrc(8) now supports key+=append and key-=remove
Diffstat (limited to 'usr.sbin/sysrc')
-rw-r--r--usr.sbin/sysrc/sysrc78
-rw-r--r--usr.sbin/sysrc/sysrc.8134
2 files changed, 199 insertions, 13 deletions
diff --git a/usr.sbin/sysrc/sysrc b/usr.sbin/sysrc/sysrc
index f37a496..bcb2178 100644
--- a/usr.sbin/sysrc/sysrc
+++ b/usr.sbin/sysrc/sysrc
@@ -1,6 +1,6 @@
#!/bin/sh
#-
-# Copyright (c) 2010-2014 Devin Teske
+# Copyright (c) 2010-2015 Devin Teske
# All rights reserved.
#
# Redistribution and use in source and binary forms, with or without
@@ -40,7 +40,7 @@ BSDCFG_SHARE="/usr/share/bsdconfig"
#
# Version information
#
-SYSRC_VERSION="6.1 Jul-18,2014"
+SYSRC_VERSION="6.3 Mar-4,2015"
#
# Options
@@ -80,7 +80,7 @@ die()
#
usage()
{
- f_err "Usage: %s [OPTIONS] name[=value] ...\n" "$pgm"
+ f_err "Usage: %s [OPTIONS] name[[+]=value] ...\n" "$pgm"
f_err "Try \`%s --help' for more information.\n" "$pgm"
die
}
@@ -94,7 +94,7 @@ 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] name[[+|-]=value] ...\n" "$pgm"
f_err "OPTIONS:\n"
f_err "$optfmt" "-a" \
@@ -529,6 +529,12 @@ 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" )"
@@ -589,18 +595,70 @@ while [ $# -gt 0 ]; do
fi
#
- # If `-N' is passed, simplify the output
+ # Determine both `before' value and appropriate `new' value
#
- if [ ! "$SHOW_VALUE" ]; then
- echo "$NAME"
- f_sysrc_set "$NAME" "${1#*}"
- else
+ 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" )
+ ;;
+ *)
if [ "$SHOW_FILE" ]; then
before=$( f_sysrc_find "$NAME" )
else
before=$( f_sysrc_get "$NAME" )
fi
- if f_sysrc_set "$NAME" "${1#*=}"; then
+ 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
diff --git a/usr.sbin/sysrc/sysrc.8 b/usr.sbin/sysrc/sysrc.8
index ebe5651..31254d4 100644
--- a/usr.sbin/sysrc/sysrc.8
+++ b/usr.sbin/sysrc/sysrc.8
@@ -1,4 +1,4 @@
-.\" Copyright (c) 2011-2014 Devin Teske
+.\" Copyright (c) 2011-2015 Devin Teske
.\" All rights reserved.
.\"
.\" Redistribution and use in source and binary forms, with or without
@@ -24,7 +24,7 @@
.\"
.\" $FreeBSD$
.\"
-.Dd Jul 18, 2014
+.Dd March 4, 2015
.Dt SYSRC 8
.Os
.Sh NAME
@@ -35,7 +35,7 @@
.Op Fl cdDeFhinNqvx
.Op Fl f Ar file
.Op Fl j Ar jail | Fl R Ar dir
-.Ar name Ns Op = Ns Ar value
+.Ar name Ns Op Ns Oo +|- Oc Ns = Ns Ar value
.Ar ...
.Nm
.Op Fl cdDeFhinNqvx
@@ -135,6 +135,15 @@ It shares the `-e' and `-n' options
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
@@ -183,6 +192,115 @@ 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 -tag -width 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 :
+.Pp
+.Bl -tag -width indent+
+.It \
+.Nm
+cloned_interfaces+=gif0
+.El
+.Pp
+Take the following sequence for example:
+.Bl -tag -width 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 :
+.Pp
+.Bl -tag -width indent+
+.It \
+.Nm
+cloned_interfaces-=gif0
+.El
+.Pp
+Take the following sequence for example:
+.Bl -tag -width 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 :
@@ -243,6 +361,16 @@ Working on other files, such as
-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
OpenPOWER on IntegriCloud