Home | History | Annotate | Line # | Download | only in sh
mkinit.sh revision 1.1
      1 #! /bin/sh
      2 #	$NetBSD: mkinit.sh,v 1.1 2004/01/17 11:47:31 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 
     39 includes=' "shell.h" "mystring.h" "init.h" '
     40 defines=
     41 decles=
     42 event_init=
     43 event_reset=
     44 event_shellproc=
     45 
     46 for src in $srcs; do
     47 	exec <$src
     48 	decnl="$nl"
     49 	while IFS=; read -r line; do
     50 		[ "$line" = x ]
     51 		case "$line " in
     52 		INIT["{ 	"]* ) event=init;;
     53 		RESET["{ 	"]* ) event=reset;;
     54 		SHELLPROC["{ 	"]* ) event=shellproc;;
     55 		INCLUDE[\ \	]* )
     56 			IFS=' 	'
     57 			set -- $line
     58 			# ignore duplicates
     59 			[ "${includes}" != "${includes%* $2 }" ] && continue
     60 			includes="$includes$2 "
     61 			continue
     62 			;;
     63 		MKINIT\  )
     64 			# struct declaration
     65 			decles="$decles$nl"
     66 			while
     67 				read -r line
     68 				decles="${decles}${line}${nl}"
     69 				[ "$line" != "};" ]
     70 			do
     71 				:
     72 			done
     73 			decnl="$nl"
     74 			continue
     75 			;;
     76 		MKINIT["{ 	"]* )
     77 			# strip initialiser
     78 			def=${line#MKINIT}
     79 			comment="${def#*;}"
     80 			def="${def%;$comment}"
     81 			def="${def%%=*}"
     82 			def="${def% }"
     83 			decles="${decles}${decnl}extern${def};${comment}${nl}"
     84 			decnl=
     85 			continue
     86 			;;
     87 		\#define[\ \	]* )
     88 			IFS=' 	'
     89 			set -- $line
     90 			# Ignore those with arguments
     91 			[ "$2" = "${2##*(}" ] || continue
     92 			# and multiline definitions
     93 			[ "$line" = "${line%\\}" ] || continue
     94 			defines="${defines}#undef  $2${nl}${line}${nl}"
     95 			continue
     96 			;;
     97 		* ) continue;;
     98 		esac
     99 		# code for events
    100 		ev="${nl}      /* from $src: */${nl}      {${nl}"
    101 		while
    102 			read -r line
    103 			[ "$line" != "}" ]
    104 		do
    105 			# The C program indented by an extra 6 chars using
    106 			# tabs then spaces. I need to compare the output :-(
    107 			indent=6
    108 			while
    109 				l=${line#	}
    110 				[ "$l" != "$line" ]
    111 			do
    112 				indent=$(($indent + 8))
    113 				line="$l"
    114 			done
    115 			while
    116 				l=${line# }
    117 				[ "$l" != "$line" ]
    118 			do
    119 				indent=$(($indent + 1))
    120 				line="$l"
    121 			done
    122 			[ -z "$line" -o "$line" != "${line###}" ] && indent=0
    123 			while
    124 				[ $indent -ge 8 ]
    125 			do
    126 				ev="$ev	"
    127 				indent="$(($indent - 8))"
    128 			done
    129 			while
    130 				[ $indent -gt 0 ]
    131 			do
    132 				ev="$ev "
    133 				indent="$(($indent - 1))"
    134 			done
    135 			ev="${ev}${line}${nl}"
    136 		done
    137 		ev="${ev}      }${nl}"
    138 		eval event_$event=\"\$event_$event\$ev\"
    139 	done
    140 done
    141 
    142 exec >init.c.tmp
    143 
    144 echo "/*"
    145 echo " * This file was generated by the mkinit program."
    146 echo " */"
    147 echo
    148 
    149 IFS=' '
    150 for f in $includes; do
    151 	echo "#include $f"
    152 done
    153 
    154 echo
    155 echo
    156 echo
    157 echo "$defines"
    158 echo
    159 echo "$decles"
    160 echo
    161 echo
    162 echo "/*"
    163 echo " * Initialization code."
    164 echo " */"
    165 echo
    166 echo "void"
    167 echo "init() {"
    168 echo "${event_init%$nl}"
    169 echo "}"
    170 echo
    171 echo
    172 echo
    173 echo "/*"
    174 echo " * This routine is called when an error or an interrupt occurs in an"
    175 echo " * interactive shell and control is returned to the main command loop."
    176 echo " */"
    177 echo
    178 echo "void"
    179 echo "reset() {"
    180 echo "${event_reset%$nl}"
    181 echo "}"
    182 echo
    183 echo
    184 echo
    185 echo "/*"
    186 echo " * This routine is called to initialize the shell to run a shell procedure."
    187 echo " */"
    188 echo
    189 echo "void"
    190 echo "initshellproc() {"
    191 echo "${event_shellproc%$nl}"
    192 echo "}"
    193 
    194 exec >&-
    195 mv init.c.tmp init.c
    196