1 #! /bin/sh 2 # $NetBSD: mkinit.sh,v 1.2 2004/06/15 23:09:54 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 while 104 read -r line 105 [ "$line" != "}" ] 106 do 107 # The C program indented by an extra 6 chars using 108 # tabs then spaces. I need to compare the output :-( 109 indent=6 110 while 111 l=${line# } 112 [ "$l" != "$line" ] 113 do 114 indent=$(($indent + 8)) 115 line="$l" 116 done 117 while 118 l=${line# } 119 [ "$l" != "$line" ] 120 do 121 indent=$(($indent + 1)) 122 line="$l" 123 done 124 [ -z "$line" -o "$line" != "${line###}" ] && indent=0 125 while 126 [ $indent -ge 8 ] 127 do 128 ev="$ev " 129 indent="$(($indent - 8))" 130 done 131 while 132 [ $indent -gt 0 ] 133 do 134 ev="$ev " 135 indent="$(($indent - 1))" 136 done 137 ev="${ev}${line}${nl}" 138 done 139 ev="${ev} }${nl}" 140 eval event_$event=\"\$event_$event\$ev\" 141 done 142 done 143 144 exec >init.c.tmp 145 146 echo "/*" 147 echo " * This file was generated by the mkinit program." 148 echo " */" 149 echo 150 151 IFS=' ' 152 for f in $includes; do 153 echo "#include $f" 154 done 155 156 echo 157 echo 158 echo 159 echo "$defines" 160 echo 161 echo "$decles" 162 echo 163 echo 164 echo "/*" 165 echo " * Initialization code." 166 echo " */" 167 echo 168 echo "void" 169 echo "init() {" 170 echo "${event_init%$nl}" 171 echo "}" 172 echo 173 echo 174 echo 175 echo "/*" 176 echo " * This routine is called when an error or an interrupt occurs in an" 177 echo " * interactive shell and control is returned to the main command loop." 178 echo " */" 179 echo 180 echo "void" 181 echo "reset() {" 182 echo "${event_reset%$nl}" 183 echo "}" 184 echo 185 echo 186 echo 187 echo "/*" 188 echo " * This routine is called to initialize the shell to run a shell procedure." 189 echo " */" 190 echo 191 echo "void" 192 echo "initshellproc() {" 193 echo "${event_shellproc%$nl}" 194 echo "}" 195 196 exec >&- 197 mv init.c.tmp init.c 198