diff options
author | dteske <dteske@FreeBSD.org> | 2013-06-02 22:04:39 +0000 |
---|---|---|
committer | dteske <dteske@FreeBSD.org> | 2013-06-02 22:04:39 +0000 |
commit | 9d0ce6189690b6d7b4012fe28c3ebb3617861595 (patch) | |
tree | 985784dcced67e039103a4f458fa4054164e6c11 /usr.sbin/bsdconfig/share | |
parent | e9cea3453922d1975e0825166772ffe465661dab (diff) | |
download | FreeBSD-src-9d0ce6189690b6d7b4012fe28c3ebb3617861595.zip FreeBSD-src-9d0ce6189690b6d7b4012fe28c3ebb3617861595.tar.gz |
Fix option processing from the library layer to address unexpected
behavior(s); e.g., `-Xd' versus `-dX' did not produce the same results.
The libraries common.subr and dialog.subr automatically process the
arguments passed to the program and enable/disable functionality without the
need to process the arguments within your program. For example, if "$@"
contains `-d', common.subr will see this and enable debugging regardless of
whether you process "$@" yourself or not (this automatic processing can
easily be disabled for custom scripts that don't want it; see the afore-
mentioned scripts for additional details).
NOTE: common.subr stores a copy of "$@" in $ARGV for convenient (and
repeated) processing by libraries such as dialog.subr which provide such
transparent functionality for the consuming script(s).
However, the libraries don't know if a program wants to accept `extra'
options. Flags are not really a problem, because the library can be
programmed to silently ignore unknown flags. The trouble comes into play
when the program wants to define an option that takes an argument.
For example:
bsdconfig -D logfile -X
In the above example, the library uses getopts to process $ARGV and if it
doesn't know that `-D' takes an argument, the option processing will
prematurely terminate on `logfile' (this is standard/correct behavior for
getopts but is undesired in our situation where we have partially off-loaded
main argument processing).
The problem is solved by allowing the program to define an extra set of
options to be included in each library's handling of $ARGV. Only options
that require arguments are truly necessary to be pre-specified in this new
manner.
Diffstat (limited to 'usr.sbin/bsdconfig/share')
-rw-r--r-- | usr.sbin/bsdconfig/share/common.subr | 31 | ||||
-rw-r--r-- | usr.sbin/bsdconfig/share/dialog.subr | 10 |
2 files changed, 35 insertions, 6 deletions
diff --git a/usr.sbin/bsdconfig/share/common.subr b/usr.sbin/bsdconfig/share/common.subr index 88594b5..b005ebe 100644 --- a/usr.sbin/bsdconfig/share/common.subr +++ b/usr.sbin/bsdconfig/share/common.subr @@ -77,6 +77,33 @@ export UNAME_R="$(uname -r)" # Release Level (i.e. X.Y-RELEASE) # GETOPTS_STDARGS="dD:" +# +# The getopts builtin will return 1 either when the end of "$@" or the first +# invalid flag is reached. This makes it impossible to determine if you've +# processed all the arguments or simply have hit an invalid flag. In the cases +# where we want to tolerate invalid flags (f_debug_init() for example), the +# following variable can be appended to your optstring argument to getopts, +# preventing it from prematurely returning 1 before the end of the arguments. +# +# NOTE: This assumes that all unknown flags are argument-less. +# +GETOPTS_ALLFLAGS="abcdefghijklmnopqrstuvwxyz" +GETOPTS_ALLFLAGS="${GETOPTS_ALLFLAGS}ABCDEFGHIJKLMNOPQRSTUVWXYZ" +GETOPTS_ALLFLAGS="${GETOPTS_ALLFLAGS}0123456789" + +# +# When we get included, f_debug_init() will fire (unless $DEBUG_SELF_INITIALIZE +# is set to disable automatic initialization) and process "$@" for a few global +# options such as `-d' and/or `-D file'. However, if your program takes custom +# flags that take arguments, this automatic processing may fail unexpectedly. +# +# The solution to this problem is to pre-define (before including this file) +# the following variable (which defaults to NULL) to indicate that there are +# extra flags that should be considered when performing automatic processing of +# globally persistent flags. +# +: ${GETOPTS_EXTRA:=} + ############################################################ FUNCTIONS # f_dprintf $fmt [ $opts ... ] @@ -114,11 +141,11 @@ f_debug_init() local OPTIND f_dprintf "f_debug_init: ARGV=[%s] GETOPTS_STDARGS=[%s]" \ "$ARGV" "$GETOPTS_STDARGS" - while getopts "$GETOPTS_STDARGS" flag > /dev/null; do + while getopts "$GETOPTS_STDARGS$GETOPTS_EXTRA$GETOPTS_ALLFLAGS" flag \ + > /dev/null; do case "$flag" in d) debug=1;; D) debugFile="$OPTARG";; - \?) continue;; esac done shift $(( $OPTIND - 1 )) diff --git a/usr.sbin/bsdconfig/share/dialog.subr b/usr.sbin/bsdconfig/share/dialog.subr index 2c81333..5fe309f 100644 --- a/usr.sbin/bsdconfig/share/dialog.subr +++ b/usr.sbin/bsdconfig/share/dialog.subr @@ -1970,18 +1970,20 @@ f_dialog_init() f_dprintf "f_dialog_init: ARGV=[%s] GETOPTS_STDARGS=[%s]" \ "$ARGV" "$GETOPTS_STDARGS" SECURE=$( set -- $ARGV - while getopts "$GETOPTS_STDARGS" flag > /dev/null; do + while getopts \ + "$GETOPTS_STDARGS$GETOPTS_EXTRA$GETOPTS_ALLFLAGS" \ + flag > /dev/null; do case "$flag" in S) echo 1;; - \?) continue;; esac done ) USE_XDIALOG=$( set -- $ARGV - while getopts $GETOPTS_STDARGS flag > /dev/null; do + while getopts \ + "$GETOPTS_STDARGS$GETOPTS_EXTRA$GETOPTS_ALLFLAGS" \ + flag > /dev/null; do case "$flag" in S|X) echo 1;; - \?) continue;; esac done ) |