Home | History | Annotate | Line # | Download | only in sh
mkinit.sh revision 1.3
      1 #! /bin/sh
      2 #	$NetBSD: mkinit.sh,v 1.3 2008/02/27 21:56:14 dsl 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 # 3. Neither the name of The NetBSD Foundation nor the names of its
     19 #    contributors may be used to endorse or promote products derived
     20 #    from this software without specific prior written permission.
     21 #
     22 # THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
     23 # ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
     24 # TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
     25 # PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
     26 # BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
     27 # CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
     28 # SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
     29 # INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
     30 # CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
     31 # ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
     32 # POSSIBILITY OF SUCH DAMAGE.
     33 
     34 srcs="$*"
     35 
     36 nl='
     37 '
     38 openparen='('
     39 backslash='\'
     40 
     41 includes=' "shell.h" "mystring.h" "init.h" '
     42 defines=
     43 decles=
     44 event_init=
     45 event_reset=
     46 event_shellproc=
     47 
     48 for src in $srcs; do
     49 	exec <$src
     50 	decnl="$nl"
     51 	while IFS=; read -r line; do
     52 		[ "$line" = x ]
     53 		case "$line " in
     54 		INIT["{ 	"]* ) event=init;;
     55 		RESET["{ 	"]* ) event=reset;;
     56 		SHELLPROC["{ 	"]* ) event=shellproc;;
     57 		INCLUDE[\ \	]* )
     58 			IFS=' 	'
     59 			set -- $line
     60 			# ignore duplicates
     61 			[ "${includes}" != "${includes%* $2 }" ] && continue
     62 			includes="$includes$2 "
     63 			continue
     64 			;;
     65 		MKINIT\  )
     66 			# struct declaration
     67 			decles="$decles$nl"
     68 			while
     69 				read -r line
     70 				decles="${decles}${line}${nl}"
     71 				[ "$line" != "};" ]
     72 			do
     73 				:
     74 			done
     75 			decnl="$nl"
     76 			continue
     77 			;;
     78 		MKINIT["{ 	"]* )
     79 			# strip initialiser
     80 			def=${line#MKINIT}
     81 			comment="${def#*;}"
     82 			def="${def%;$comment}"
     83 			def="${def%%=*}"
     84 			def="${def% }"
     85 			decles="${decles}${decnl}extern${def};${comment}${nl}"
     86 			decnl=
     87 			continue
     88 			;;
     89 		\#define[\ \	]* )
     90 			IFS=' 	'
     91 			set -- $line
     92 			# Ignore those with arguments
     93 			[ "$2" = "${2##*$openparen}" ] || continue
     94 			# and multiline definitions
     95 			[ "$line" = "${line%$backslash}" ] || continue
     96 			defines="${defines}#undef	$2${nl}${line}${nl}"
     97 			continue
     98 			;;
     99 		* ) continue;;
    100 		esac
    101 		# code for events
    102 		ev="${nl}	/* from $src: */${nl}	{${nl}"
    103 		# Indent the text by an extra <tab>
    104 		while
    105 			read -r line
    106 			[ "$line" != "}" ]
    107 		do
    108 			[ -n "$line" -a "$line" = "${line###}" ] &&
    109 				line="	$line"
    110 			ev="${ev}${line}${nl}"
    111 		done
    112 		ev="${ev}	}${nl}"
    113 		eval event_$event=\"\$event_$event\$ev\"
    114 	done
    115 done
    116 
    117 exec >init.c.tmp
    118 
    119 echo "/*"
    120 echo " * This file was generated by the mkinit program."
    121 echo " */"
    122 echo
    123 
    124 IFS=' '
    125 for f in $includes; do
    126 	echo "#include $f"
    127 done
    128 
    129 echo
    130 echo
    131 echo
    132 echo "$defines"
    133 echo
    134 echo "$decles"
    135 echo
    136 echo
    137 echo "/*"
    138 echo " * Initialization code."
    139 echo " */"
    140 echo
    141 echo "void"
    142 echo "init(void)"
    143 echo "{"
    144 echo "${event_init%$nl}"
    145 echo "}"
    146 echo
    147 echo
    148 echo
    149 echo "/*"
    150 echo " * This routine is called when an error or an interrupt occurs in an"
    151 echo " * interactive shell and control is returned to the main command loop."
    152 echo " */"
    153 echo
    154 echo "void"
    155 echo "reset(void)"
    156 echo "{"
    157 echo "${event_reset%$nl}"
    158 echo "}"
    159 echo
    160 echo
    161 echo
    162 echo "/*"
    163 echo " * This routine is called to initialize the shell to run a shell procedure."
    164 echo " */"
    165 echo
    166 echo "void"
    167 echo "initshellproc(void)"
    168 echo "{"
    169 echo "${event_shellproc%$nl}"
    170 echo "}"
    171 
    172 exec >&-
    173 mv init.c.tmp init.c
    174