summaryrefslogtreecommitdiffstats
path: root/usr.sbin
diff options
context:
space:
mode:
Diffstat (limited to 'usr.sbin')
-rwxr-xr-xusr.sbin/bsdconfig/bsdconfig15
-rw-r--r--usr.sbin/bsdconfig/share/common.subr31
-rw-r--r--usr.sbin/bsdconfig/share/dialog.subr10
3 files changed, 50 insertions, 6 deletions
diff --git a/usr.sbin/bsdconfig/bsdconfig b/usr.sbin/bsdconfig/bsdconfig
index 987d6f5..5c51228 100755
--- a/usr.sbin/bsdconfig/bsdconfig
+++ b/usr.sbin/bsdconfig/bsdconfig
@@ -29,6 +29,21 @@
#
############################################################ INCLUDES
+# When common.subr is included, it automatically scans "$@" for `-d' and/or
+# `-D file' arguments to conditionally enable debugging. Similarly, when
+# dialog.subr is included, it automatically scans "$@" for `-X' and/or `-S'.
+# To prevent this scanning from becoming confused by extra options, define
+# any/all extra arguments to use in the optstring to getopts when scanning
+# for dedicated options such as those described.
+#
+# NOTE: This needs to be declared before including `common.subr'.
+# NOTE: You really only need to list flags that require an argument as unknown
+# flags are silently accepted unless they take an argument (in which case
+# the following argument will terminate option processing unless it looks
+# like a flag).
+#
+GETOPTS_EXTRA="f:"
+
BSDCFG_SHARE="/usr/share/bsdconfig"
. $BSDCFG_SHARE/common.subr || exit 1
f_dprintf "%s: loading includes..." "$0"
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
)
OpenPOWER on IntegriCloud