Module Name:    src
Committed By:   kre
Date:           Sun May 28 14:14:22 UTC 2017

Modified Files:
        src/bin/sh: Makefile mkoptions.sh option.list

Log Message:
Redo mkoptions.sh .. much better this way, now fully automated
option sorting (no longer required option.list to be manually
sorted by long option name) and properly handles conditional
options.   Cleaner output format as well.

This allows option.list to be reordered to group related options
together ... also added more comments to it.


To generate a diff of this commit:
cvs rdiff -u -r1.108 -r1.109 src/bin/sh/Makefile
cvs rdiff -u -r1.1 -r1.2 src/bin/sh/mkoptions.sh src/bin/sh/option.list

Please note that diffs are not public domain; they are subject to the
copyright notices on the relevant files.

Modified files:

Index: src/bin/sh/Makefile
diff -u src/bin/sh/Makefile:1.108 src/bin/sh/Makefile:1.109
--- src/bin/sh/Makefile:1.108	Sun May 28 00:38:01 2017
+++ src/bin/sh/Makefile	Sun May 28 14:14:22 2017
@@ -1,4 +1,4 @@
-#	$NetBSD: Makefile,v 1.108 2017/05/28 00:38:01 kre Exp $
+#	$NetBSD: Makefile,v 1.109 2017/05/28 14:14:22 kre Exp $
 #	@(#)Makefile	8.4 (Berkeley) 5/5/95
 
 .include <bsd.own.mk>
@@ -20,6 +20,7 @@ DPADD+=	${LIBEDIT} ${LIBTERMINFO}
 # Environment for scripts executed during build.
 SCRIPT_ENV= \
 	AWK=${TOOL_AWK:Q} \
+	MKTEMP=${TOOL_MKTEMP:Q} \
 	SED=${TOOL_SED:Q}
 
 CPPFLAGS+=-DSHELL -I. -I${.CURDIR}

Index: src/bin/sh/mkoptions.sh
diff -u src/bin/sh/mkoptions.sh:1.1 src/bin/sh/mkoptions.sh:1.2
--- src/bin/sh/mkoptions.sh:1.1	Sun May 28 00:38:01 2017
+++ src/bin/sh/mkoptions.sh	Sun May 28 14:14:22 2017
@@ -1,6 +1,6 @@
 #! /bin/sh
 
-# $NetBSD: mkoptions.sh,v 1.1 2017/05/28 00:38:01 kre Exp $
+# $NetBSD: mkoptions.sh,v 1.2 2017/05/28 14:14:22 kre Exp $
 
 #
 # It would be more sensible to generate 2 .h files, one which
@@ -17,36 +17,53 @@ IFS=' 	'	# blank, tab (no newline)
 IF="$1"
 OF="${3+$3/}$2"
 
+E_FILE=$(${MKTEMP:-mktemp} -t MKO.E.$$)
+O_FILE=$(${MKTEMP:-mktemp} -t MKO.O.$$)
+trap 'rm -f "${E_FILE}" "${O_FILE}"' EXIT
+
+exec 5> "${E_FILE}"
+exec 6> "${O_FILE}"
+
 {
 	printf '/*\n * File automatically generated by %s.\n' "$0"
 	printf ' * Do not edit, do not add to cvs.\n'
 	printf ' */\n\n'
 
 	printf '#ifdef DEFINE_OPTIONS\n'
-	printf '#define DEF_OPT(a,b,c,d,e) { a, b, c, d, e },\n'
 	printf 'struct optent optlist[] = {\n'
-	printf '#else\n'
-	printf '#define DEF_OPT(a,b,c,d,e)\n'
-	printf '#endif\n\n'
 } >"${OF}"
 
 FIRST=true
-I=0
 
+${SED:-sed} <"${IF}"			\
+	-e '/^$/d'			\
+	-e '/^#/d'			\
+	-e '/^[ 	]*\//d'		\
+	-e '/^[ 	]*\*/d'		\
+	-e '/^[ 	]*;/d'			|
+sort -k2,2f -k2,2r < "${IF}"			|
 while read line
 do
 	# Look for comments in various styles, and ignore them
-	# preprocessor statements are simply output verbatim
-	# but use them only first or last. one #ifdef/#endif at end is OK
+	# (these should generally be already removed by sed above)
 
 	case "${line}" in
 	'')	continue;;
 	/*)	continue;;
 	\**)	continue;;
 	\;*)	continue;;
-	\#*)	printf '%s\n\n' "${line}" >&4; continue;;
+	\#*)	continue;;
 	esac
 
+	case "${line}" in
+	*'#if'*)
+		COND="${line#*#}"
+		COND="#${COND%%#*}"
+		;;
+	*)
+		COND=
+		;;
+	esac
 	set -- ${line%%[ 	]#*}
 
 	var="$1" name="$2"
@@ -59,7 +76,6 @@ do
 	esac
 
 	case "${name}" in
-#	=)	name=${var};;		# probably not a good idea
 	?) 	set -- ${var} '' $name $3 $4; name= ;;
 	esac
 
@@ -76,57 +92,90 @@ do
 
 	case "${set}" in
 	-)	set= ;;
-	[01])	dflt="${set}"; set= ;;
+	[01] | [Oo][Nn] | [Oo][Ff][Ff])	dflt="${set}"; set= ;;
 	''|?)	;;
 	*)	printf >&2 'set "%s": Not a character\n' "${set}"; continue;;
 	esac
 
+	case "${dflt}" in
+	'')		;;
+	[Oo][Nn])	dflt=1;;
+	[Oo][Ff][Ff])	dflt=0;;
+	[01])		;;
+	*)	printf >&2 'default "%s" invalid, use 0 off 1 on\n'; continue;;
+	esac
+
+	# validation complete, now to generate output
+
+	if [ -n "${COND}" ]
+	then
+		printf '%s\n' "${COND}" >&4
+		printf '%s\n' "${COND}" >&5
+		printf '%s\n' "${COND}" >&6
+	fi
+
+	printf '\t_SH_OPT_%s,\n' "${var}" >&5
 
 	if [ -n "${name}" ]
 	then
-		printf '    DEF_OPT("%s", ' "${name}" >&4
+		printf '    { "%s", ' "${name}"	>&4
 	else
-		printf '    DEF_OPT(0, ' >&4
+		printf '    { 0, '		>&4
 	fi
 
 	if [ -n "${chr}" ]
 	then
-		printf "'%s', " "${chr}" >&4
+		printf "'%s', " "${chr}"	>&4
 	else
-		printf '0, ' >&4
+		chr=
+		printf '0, '			>&4
 	fi
 
 	if [ -n "${set}" ]
 	then
-		printf "'%s', 0, " "${set}" >&4
+		printf "'%s', 0, " "${set}"	>&4
 	else
-		printf '0, 0, ' >&4
+		printf '0, 0, '			>&4
 	fi
 
 	if [ -n "${dflt}" ]
 	then
-		printf '%s )\n' "${dflt}" >&4
+		printf '%s },\n' "${dflt}"	>&4
 	else
-		printf '0 )\n' >&4
+		printf '0 },\n'			>&4
 	fi
 
-	printf '#define	%s	optlist[%d].val\n\n' "${var}" "${I}" >&4
-	I=$((I + 1))
+	printf '#define %s\toptlist[_SH_OPT_%s].val\n' "${var}" "${var}" >&6
+
+	if [ -n "${COND}" ]
+	then
+		printf '#endif\n' >&4
+		printf '#endif\n' >&5
+		printf '#endif\n' >&6
+	fi
 
 	test -z "${chr}" && continue
 
-	printf '%s %d\n' "${chr}" $((I - 1))
+	printf '%s _SH_OPT_%s %s\n' "${chr}" "${var}" "${COND}"
 
-done < "$IF" 4>>"${OF}" | sort -t' ' -k1,1f -k1,1r | while read chr index
+done 4>>"${OF}" | sort -t' ' -k1,1f -k1,1r | while read chr index COND
 do
 	if $FIRST
 	then
+		printf '    { 0, 0, 0, 0, 0 }\n};\n'
+		printf '#endif\n\n'
+
+		printf 'enum shell_opt_names {\n'
+		cat "${E_FILE}"
+		printf '};\n\n'
+
 		printf '#ifdef DEFINE_OPTIONS\n'
-		printf '    { 0, 0, 0, 0, 0 }\n};\n\n'
 		printf 'const unsigned char optorder[] = {\n'
 		FIRST=false
 	fi
+	[ -n "${COND}" ] && printf '%s\n' "${COND}"
 	printf '\t%s,\n' "${index}"
+	[ -n "${COND}" ] && printf '#endif\n'
 
 done >>"${OF}"
 
@@ -141,7 +190,9 @@ done >>"${OF}"
 	printf 'extern int sizeof_optlist;\n'
 	printf 'extern const unsigned char optorder[];\n'
 	printf 'extern const int option_flags;\n'
-	printf '\n#endif\n'
+	printf '\n#endif\n\n'
+
+	cat "${O_FILE}"
 } >> "${OF}"
 
 exit 0
Index: src/bin/sh/option.list
diff -u src/bin/sh/option.list:1.1 src/bin/sh/option.list:1.2
--- src/bin/sh/option.list:1.1	Sun May 28 00:38:01 2017
+++ src/bin/sh/option.list	Sun May 28 14:14:22 2017
@@ -1,13 +1,16 @@
-/* $NetBSD: option.list,v 1.1 2017/05/28 00:38:01 kre Exp $ */
+/* $NetBSD: option.list,v 1.2 2017/05/28 14:14:22 kre Exp $ */
 
 /*
  * define the shell's settable options
+ *
+ *	new options can be defined by adding them here,
+ *	but they do nothing untilcode to implement them
+ *	is added (using the "var name" field)
  */
 
 /*
  * format is up to 5 columns... (followed by anything)
  * end of line comments can be introduced by ' #' (space/tab hash) to eol.
- * proprocessor directoves can be (kind of) interspersed as required
  *
  * The columns are:
  *	1. internal shell "var name"	(required)
@@ -19,39 +22,54 @@
  *		if neither long nor short name, line is ignored
  *	4. option set short name (name of option equiv class)
  *		if '-' or absent then no class
- *	5. efault value of option
+ *	5. default value of option
  *		if absent, default is 0
- *		only 0 or 1 possible (0==off 1==on)
+ *		only 0 or 1 possible (0==off 1==on) ("on" and "off" can be used)
+ *
+ * Data may be followed by any C preprocessor #if expression (incl the #if..)
+ * (including #ifdef #ifndef) to conditionalise output for that option.
+ * The #if expression continues until \n or next following '#'
  */
 
-/*
- * The order of the lines below gives the order they are listed by set -o
- * Options labelled '[U]' are not (yet, maybe ever) implemented.
- */
+// the POSIX defined options
 aflag	allexport	a		# export all variables
-cdprint	cdprint				# always print result of a cd
-Eflag	emacs		E V		# enable emacs style editing
 eflag	errexit		e		# exit on command error ($? != 0)
-usefork	fork		F		# use fork(2) instead of vfork(2)
-Iflag	ignoreeof	I		# do not exit interactive shell on EOF
-iflag	interactive	i		# interactive shell
 mflag	monitor		m		# enable job control
 Cflag	noclobber	C		# do not overwrite files when using >
 nflag	noexec		n		# do not execue commands
 fflag	noglob		f		# no pathname expansion
+uflag	nounset		u		# expanding unset var is an error
+vflag	verbose		v		# echo commands as read
+xflag	xtrace		x		# trace command execution
+
+// the long name (ignoreeof) is standard, the I flag is not
+Iflag	ignoreeof	I		# do not exit interactive shell on EOF
+
+// defined but not really implemented by the shell (yet) - they do nothing
+bflag	notify		b		# [U] report bg job completion
 nolog	nolog				# [U] no func definitions in history
+// 'h' is standard, long name (trackall) is not
+hflag	trackall	h		# [U] locate cmds in funcs during defn
+
+// 's' is standard for command line, not as 'set' option, nor 'stdin' name
+sflag	stdin		s		# read from standard input
+// minusc		c		# command line option only.
+// --			o		# handled differently...
+
+// non-standard options -- 'i' is just a state, not an option in standard.
+iflag	interactive	i		# interactive shell
+cdprint	cdprint				# always print result of a cd
+usefork	fork		F		# use fork(2) instead of vfork(2)
 pflag	nopriv		p		# preserve privs if set[ug]id
-bflag	notify		b		# [U] report bg job completion
-uflag	nounset		u		# expanding unset var is an error
 posix	posix				# be closer to POSIX compat
 qflag	quietprofile	q		# disable -v/-x in startup files
-sflag	stdin		s		# read from standard input
-tabcomplete	tabcomplete		# make <tab> cause filename expansion
-hflag	trackall	h		# [U] locate cmds in funcs during defn
-vflag	verbose		v		# echo commands as read
+
+// editline/history related options ("vi" is standard, 'V' and others are not)
+// only one of vi/emacs can be set, hence the "set" definition, value
+// of that can be any char (not used for a different set)
 Vflag	vi		V V		# enable vi style editing
-xflag	xtrace		x		# trace command execution
+Eflag	emacs		E V		# enable emacs style editing
+tabcomplete	tabcomplete		# make <tab> cause filename expansion
 
-#ifdef DEBUG
-debug	debug				# enable internal shell debugging
-#endif
+// internal debug option (not usually included in the shell)
+debug	debug		#ifdef DEBUG	# enable internal shell debugging

Reply via email to