?RCS: $Id: Options.U 78389 2004-11-30 00:17:17Z manus $ ?RCS: ?RCS: Copyright (c) 1991-1993, Raphael Manfredi ?RCS: ?RCS: You may redistribute only under the terms of the Artistic Licence, ?RCS: as specified in the README file that comes with the distribution. ?RCS: You may reuse parts of this distribution only within the terms of ?RCS: that same Artistic Licence; a copy of which may be found at the root ?RCS: of the source tree for dist 3.0. ?RCS: ?RCS: $Log$ ?RCS: Revision 1.1 2004/11/30 00:17:18 manus ?RCS: Initial revision ?RCS: ?RCS: Revision 3.0.1.7 1997/02/28 15:08:15 ram ?RCS: patch61: optdef.sh now starts with a "startsh" ?RCS: patch61: moved some code from Head.U ?RCS: ?RCS: Revision 3.0.1.6 1995/09/25 09:14:46 ram ?RCS: patch59: protected option parsing code against 'echo -*' option failure ?RCS: ?RCS: Revision 3.0.1.5 1995/05/12 12:04:52 ram ?RCS: patch54: added -K option for experts ?RCS: ?RCS: Revision 3.0.1.4 1995/01/30 14:27:52 ram ?RCS: patch49: this unit now exports file optdef.sh, not a variable ?RCS: ?RCS: Revision 3.0.1.3 1995/01/11 15:19:00 ram ?RCS: patch45: new -O option allowing -D and -U to override config.sh setttings ?RCS: patch45: file optdef.sh is no longer removed after sourcing ?RCS: ?RCS: Revision 3.0.1.2 1994/10/29 15:58:06 ram ?RCS: patch36: ensure option definition file is removed before appending ?RCS: patch36: protect variable definitions with spaces in them ?RCS: ?RCS: Revision 3.0.1.1 1994/06/20 06:55:44 ram ?RCS: patch30: now uses new me symbol to tag error messages ?RCS: patch30: new -D and -U options to define/undef symbols (JHI) ?RCS: ?RCS: Revision 3.0 1993/08/18 12:05:14 ram ?RCS: Baseline for dist 3.0 netwide release. ?RCS: ?X: ?X: Command line parsing. It is really important that the variables used here ?X: be not listed in the MAKE line, or they will be saved in config.sh and ?X: loading this file to fetch default answers would clobber the values set ?X: herein. ?X: ?MAKE:Options: startsh ?MAKE: -pick wipe $@ %< ?V:reuseval alldone error realsilent silent extractsh fastread \ override knowitall: config_sh ?T:arg symbol ?F:./optdef.sh : produce awk script to parse command line options cat >options.awk <<'EOF' BEGIN { optstr = "dD:eEf:hKOrsSU:V"; # getopt-style specification len = length(optstr); for (i = 1; i <= len; i++) { c = substr(optstr, i, 1); ?X: some older awk's do not have the C ?: construct if (i < len) a = substr(optstr, i + 1, 1); else a = ""; if (a == ":") { arg[c] = 1; i++; } opt[c] = 1; } } { expect = 0; str = $0; if (substr(str, 1, 1) != "-") { printf("'%s'\n", str); next; } len = length($0); for (i = 2; i <= len; i++) { c = substr(str, i, 1); if (!opt[c]) { printf("-%s\n", substr(str, i)); next; } printf("-%s\n", c); if (arg[c]) { if (i < len) printf("'%s'\n", substr(str, i + 1)); else expect = 1; next; } } } END { if (expect) print "?"; } EOF : process the command line options ?X: Use "$@" to keep arguments with spaces in them from being split apart. ?X: For the same reason, awk will output quoted arguments and the final eval ?X: removes them and sets a proper $* array. An 'X' is prependend to each ?X: argument before being fed to echo to guard against 'echo -x', where -x ?X: would be understood as an echo option! It is removed before feeding awk. set X `for arg in "$@"; do echo "X$arg"; done | sed -e s/X// | awk -f options.awk` eval "set $*" shift rm -f options.awk : set up default values fastread='' reuseval=false config_sh='' alldone='' error='' silent='' extractsh='' override='' knowitall='' rm -f optdef.sh cat >optdef.sh <&4 redirection based on the value of the ?X: realsilent variable... -- RAM, 18/93/96 ?X: : option parsing while test $# -gt 0; do case "$1" in -d) shift; fastread=yes;; -e) shift; alldone=cont;; -f) shift cd .. if test -r "$1"; then config_sh="$1" else echo "$me: cannot read config file $1." >&2 error=true fi cd UU shift;; -h) shift; error=true;; -r) shift; reuseval=true;; -s) shift; silent=true; realsilent=true;; -E) shift; alldone=exit;; -K) shift; knowitall=true;; -O) shift; override=true;; -S) shift; silent=true; extractsh=true;; -D) shift case "$1" in *=) echo "$me: use '-U symbol=', not '-D symbol='." >&2 echo "$me: ignoring -D $1" >&2 ;; *=*) echo "$1" | \ sed -e "s/'/'\"'\"'/g" -e "s/=\(.*\)/='\1'/" >> optdef.sh;; *) echo "$1='define'" >> optdef.sh;; esac shift ;; -U) shift case "$1" in *=) echo "$1" >> optdef.sh;; *=*) echo "$me: use '-D symbol=val', not '-U symbol=val'." >&2 echo "$me: ignoring -U $1" >&2 ;; *) echo "$1='undef'" >> optdef.sh;; esac shift ;; -V) echo "$me generated by metaconfig PL." >&2 exit 0;; --) break;; -*) echo "$me: unknown option $1" >&2; shift; error=true;; *) break;; esac done case "$error" in true) cat >&2 <&1 case "$silent" in true) exec 1>/dev/null;; esac : run the defines and the undefines, if any, but leave the file out there... touch optdef.sh . ./optdef.sh