Home | History | Annotate | Line # | Download | only in sh
mkinit.sh revision 1.9
      1 #! /bin/sh
      2 #	$NetBSD: mkinit.sh,v 1.9 2018/10/18 04:24:43 kre Exp $
      3 
      4 # Copyright (c) 2003 The NetBSD Foundation, Inc.
      5 # All rights reserved.
      6 #
      7 # This code is derived from software contributed to The NetBSD Foundation
      8 # by David Laight.
      9 #
     10 # Redistribution and use in source and binary forms, with or without
     11 # modification, are permitted provided that the following conditions
     12 # are met:
     13 # 1. Redistributions of source code must retain the above copyright
     14 #    notice, this list of conditions and the following disclaimer.
     15 # 2. Redistributions in binary form must reproduce the above copyright
     16 #    notice, this list of conditions and the following disclaimer in the
     17 #    documentation and/or other materials provided with the distribution.
     18 #
     19 # THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
     20 # ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
     21 # TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
     22 # PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
     23 # BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
     24 # CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
     25 # SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
     26 # INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
     27 # CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
     28 # ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
     29 # POSSIBILITY OF SUCH DAMAGE.
     30 
     31 srcs="$*"
     32 
     33 nl='
     34 '
     35 openparen='('
     36 
     37 # shells have bugs (including older NetBSD sh) in how \ is
     38 # used in pattern matching.   So work out what the shell
     39 # running this script expects.   We could also just use a
     40 # literal \ in the pattern, which would need to be quoted
     41 # of course, but then we'd run into a whole host of potential
     42 # other shell bugs (both with the quoting in the pattern, and
     43 # with the matching that follows if that works as inended).
     44 # Far easier, and more reliable, is to just work out what works,
     45 # and then use it, which more or less mandates using a variable...
     46 backslash='\\'
     47 var='abc\'			# dummy test case.
     48 if [ "$var" = "${var%$backslash}" ]
     49 then
     50 	# buggy sh, try the broken way
     51 	backslash='\'
     52 	if [ "$var" = "${var%$backslash}" ]
     53 	then
     54 		printf >&2 "$0: %s\n" 'No pattern match with \ (broken shell)'
     55 		exit 1
     56 	fi
     57 fi
     58 # We know we can detect the presence of a trailing \, which is all we need.
     59 # Now to confirm we will not generate false matches.
     60 var='abc'
     61 if [ "$var" != "${var%$backslash}" ]
     62 then
     63 	printf >&2 "$0: %s\n" 'Bogus pattern match with \ (broken shell)'
     64 	exit 1
     65 fi
     66 unset var
     67 
     68 includes=' "shell.h" "mystring.h" "init.h" '
     69 defines=
     70 decles=
     71 event_init=
     72 event_reset=
     73 event_shellproc=
     74 
     75 for src in $srcs; do
     76 	exec <$src
     77 	decnl="$nl"
     78 	while IFS=; read -r line; do
     79 		[ "$line" = x ]
     80 		case "$line " in
     81 		INIT["{ 	"]* ) event=init;;
     82 		RESET["{ 	"]* ) event=reset;;
     83 		SHELLPROC["{ 	"]* ) event=shellproc;;
     84 		INCLUDE[\ \	]* )
     85 			IFS=' 	'
     86 			set -- $line
     87 			# ignore duplicates
     88 			[ "${includes}" != "${includes% $2 *}" ] && continue
     89 			includes="$includes$2 "
     90 			continue
     91 			;;
     92 		MKINIT\  )
     93 			# struct declaration
     94 			decles="$decles$nl"
     95 			while
     96 				read -r line
     97 				decles="${decles}${line}${nl}"
     98 				[ "$line" != "};" ]
     99 			do
    100 				:
    101 			done
    102 			decnl="$nl"
    103 			continue
    104 			;;
    105 		MKINIT["{ 	"]* )
    106 			# strip initialiser
    107 			def=${line#MKINIT}
    108 			comment="${def#*;}"
    109 			def="${def%;$comment}"
    110 			def="${def%%=*}"
    111 			def="${def% }"
    112 			decles="${decles}${decnl}extern${def};${comment}${nl}"
    113 			decnl=
    114 			continue
    115 			;;
    116 		\#define[\ \	]* )
    117 			IFS=' 	'
    118 			set -- $line
    119 			# Ignore those with arguments
    120 			[ "$2" = "${2##*$openparen}" ] || continue
    121 			# and multiline definitions
    122 			[ "$line" = "${line%$backslash}" ] || continue
    123 			defines="${defines}#undef	$2${nl}${line}${nl}"
    124 			continue
    125 			;;
    126 		* ) continue;;
    127 		esac
    128 		# code for events
    129 		ev="${nl}	/* from $src: */${nl}	{${nl}"
    130 		# Indent the text by an extra <tab>
    131 		while
    132 			read -r line
    133 			[ "$line" != "}" ]
    134 		do
    135 			case "$line" in
    136 			('')	;;
    137 			('#'*)	;;
    138 			(*)	line="	$line";;
    139 			esac
    140 			ev="${ev}${line}${nl}"
    141 		done
    142 		ev="${ev}	}${nl}"
    143 		eval event_$event=\"\$event_$event\$ev\"
    144 	done
    145 done
    146 
    147 exec >init.c.tmp
    148 
    149 echo "/*"
    150 echo " * This file was generated by the mkinit program."
    151 echo " */"
    152 echo
    153 
    154 IFS=' '
    155 for f in $includes; do
    156 	echo "#include $f"
    157 done
    158 
    159 echo
    160 echo
    161 echo
    162 echo "$defines"
    163 echo
    164 echo "$decles"
    165 echo
    166 echo
    167 echo "/*"
    168 echo " * Initialization code."
    169 echo " */"
    170 echo
    171 echo "void"
    172 echo "init(void)"
    173 echo "{"
    174 echo "${event_init}"
    175 echo "}"
    176 echo
    177 echo
    178 echo
    179 echo "/*"
    180 echo " * This routine is called when an error or an interrupt occurs in an"
    181 echo " * interactive shell and control is returned to the main command loop."
    182 echo " */"
    183 echo
    184 echo "void"
    185 echo "reset(void)"
    186 echo "{"
    187 echo "${event_reset}"
    188 echo "}"
    189 echo
    190 echo
    191 echo
    192 echo "/*"
    193 echo " * This routine is called to initialize the shell to run a shell procedure."
    194 echo " */"
    195 echo
    196 echo "void"
    197 echo "initshellproc(void)"
    198 echo "{"
    199 echo "${event_shellproc}"
    200 echo "}"
    201 
    202 exec >&-
    203 mv init.c.tmp init.c
    204