Home | History | Annotate | Line # | Download | only in sh
mkinit.sh revision 1.2
      1  1.1  dsl #! /bin/sh
      2  1.2  dsl #	$NetBSD: mkinit.sh,v 1.2 2004/06/15 23:09:54 dsl Exp $
      3  1.1  dsl 
      4  1.1  dsl # Copyright (c) 2003 The NetBSD Foundation, Inc.
      5  1.1  dsl # All rights reserved.
      6  1.1  dsl #
      7  1.1  dsl # This code is derived from software contributed to The NetBSD Foundation
      8  1.1  dsl # by David Laight.
      9  1.1  dsl #
     10  1.1  dsl # Redistribution and use in source and binary forms, with or without
     11  1.1  dsl # modification, are permitted provided that the following conditions
     12  1.1  dsl # are met:
     13  1.1  dsl # 1. Redistributions of source code must retain the above copyright
     14  1.1  dsl #    notice, this list of conditions and the following disclaimer.
     15  1.1  dsl # 2. Redistributions in binary form must reproduce the above copyright
     16  1.1  dsl #    notice, this list of conditions and the following disclaimer in the
     17  1.1  dsl #    documentation and/or other materials provided with the distribution.
     18  1.1  dsl # 3. Neither the name of The NetBSD Foundation nor the names of its
     19  1.1  dsl #    contributors may be used to endorse or promote products derived
     20  1.1  dsl #    from this software without specific prior written permission.
     21  1.1  dsl #
     22  1.1  dsl # THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
     23  1.1  dsl # ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
     24  1.1  dsl # TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
     25  1.1  dsl # PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
     26  1.1  dsl # BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
     27  1.1  dsl # CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
     28  1.1  dsl # SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
     29  1.1  dsl # INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
     30  1.1  dsl # CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
     31  1.1  dsl # ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
     32  1.1  dsl # POSSIBILITY OF SUCH DAMAGE.
     33  1.1  dsl 
     34  1.1  dsl srcs="$*"
     35  1.1  dsl 
     36  1.1  dsl nl='
     37  1.1  dsl '
     38  1.2  dsl openparen='('
     39  1.2  dsl backslash='\'
     40  1.1  dsl 
     41  1.1  dsl includes=' "shell.h" "mystring.h" "init.h" '
     42  1.1  dsl defines=
     43  1.1  dsl decles=
     44  1.1  dsl event_init=
     45  1.1  dsl event_reset=
     46  1.1  dsl event_shellproc=
     47  1.1  dsl 
     48  1.1  dsl for src in $srcs; do
     49  1.1  dsl 	exec <$src
     50  1.1  dsl 	decnl="$nl"
     51  1.1  dsl 	while IFS=; read -r line; do
     52  1.1  dsl 		[ "$line" = x ]
     53  1.1  dsl 		case "$line " in
     54  1.1  dsl 		INIT["{ 	"]* ) event=init;;
     55  1.1  dsl 		RESET["{ 	"]* ) event=reset;;
     56  1.1  dsl 		SHELLPROC["{ 	"]* ) event=shellproc;;
     57  1.1  dsl 		INCLUDE[\ \	]* )
     58  1.1  dsl 			IFS=' 	'
     59  1.1  dsl 			set -- $line
     60  1.1  dsl 			# ignore duplicates
     61  1.1  dsl 			[ "${includes}" != "${includes%* $2 }" ] && continue
     62  1.1  dsl 			includes="$includes$2 "
     63  1.1  dsl 			continue
     64  1.1  dsl 			;;
     65  1.1  dsl 		MKINIT\  )
     66  1.1  dsl 			# struct declaration
     67  1.1  dsl 			decles="$decles$nl"
     68  1.1  dsl 			while
     69  1.1  dsl 				read -r line
     70  1.1  dsl 				decles="${decles}${line}${nl}"
     71  1.1  dsl 				[ "$line" != "};" ]
     72  1.1  dsl 			do
     73  1.1  dsl 				:
     74  1.1  dsl 			done
     75  1.1  dsl 			decnl="$nl"
     76  1.1  dsl 			continue
     77  1.1  dsl 			;;
     78  1.1  dsl 		MKINIT["{ 	"]* )
     79  1.1  dsl 			# strip initialiser
     80  1.1  dsl 			def=${line#MKINIT}
     81  1.1  dsl 			comment="${def#*;}"
     82  1.1  dsl 			def="${def%;$comment}"
     83  1.1  dsl 			def="${def%%=*}"
     84  1.1  dsl 			def="${def% }"
     85  1.1  dsl 			decles="${decles}${decnl}extern${def};${comment}${nl}"
     86  1.1  dsl 			decnl=
     87  1.1  dsl 			continue
     88  1.1  dsl 			;;
     89  1.1  dsl 		\#define[\ \	]* )
     90  1.1  dsl 			IFS=' 	'
     91  1.1  dsl 			set -- $line
     92  1.1  dsl 			# Ignore those with arguments
     93  1.2  dsl 			[ "$2" = "${2##*$openparen}" ] || continue
     94  1.1  dsl 			# and multiline definitions
     95  1.2  dsl 			[ "$line" = "${line%$backslash}" ] || continue
     96  1.1  dsl 			defines="${defines}#undef  $2${nl}${line}${nl}"
     97  1.1  dsl 			continue
     98  1.1  dsl 			;;
     99  1.1  dsl 		* ) continue;;
    100  1.1  dsl 		esac
    101  1.1  dsl 		# code for events
    102  1.1  dsl 		ev="${nl}      /* from $src: */${nl}      {${nl}"
    103  1.1  dsl 		while
    104  1.1  dsl 			read -r line
    105  1.1  dsl 			[ "$line" != "}" ]
    106  1.1  dsl 		do
    107  1.1  dsl 			# The C program indented by an extra 6 chars using
    108  1.1  dsl 			# tabs then spaces. I need to compare the output :-(
    109  1.1  dsl 			indent=6
    110  1.1  dsl 			while
    111  1.1  dsl 				l=${line#	}
    112  1.1  dsl 				[ "$l" != "$line" ]
    113  1.1  dsl 			do
    114  1.1  dsl 				indent=$(($indent + 8))
    115  1.1  dsl 				line="$l"
    116  1.1  dsl 			done
    117  1.1  dsl 			while
    118  1.1  dsl 				l=${line# }
    119  1.1  dsl 				[ "$l" != "$line" ]
    120  1.1  dsl 			do
    121  1.1  dsl 				indent=$(($indent + 1))
    122  1.1  dsl 				line="$l"
    123  1.1  dsl 			done
    124  1.1  dsl 			[ -z "$line" -o "$line" != "${line###}" ] && indent=0
    125  1.1  dsl 			while
    126  1.1  dsl 				[ $indent -ge 8 ]
    127  1.1  dsl 			do
    128  1.1  dsl 				ev="$ev	"
    129  1.1  dsl 				indent="$(($indent - 8))"
    130  1.1  dsl 			done
    131  1.1  dsl 			while
    132  1.1  dsl 				[ $indent -gt 0 ]
    133  1.1  dsl 			do
    134  1.1  dsl 				ev="$ev "
    135  1.1  dsl 				indent="$(($indent - 1))"
    136  1.1  dsl 			done
    137  1.1  dsl 			ev="${ev}${line}${nl}"
    138  1.1  dsl 		done
    139  1.1  dsl 		ev="${ev}      }${nl}"
    140  1.1  dsl 		eval event_$event=\"\$event_$event\$ev\"
    141  1.1  dsl 	done
    142  1.1  dsl done
    143  1.1  dsl 
    144  1.1  dsl exec >init.c.tmp
    145  1.1  dsl 
    146  1.1  dsl echo "/*"
    147  1.1  dsl echo " * This file was generated by the mkinit program."
    148  1.1  dsl echo " */"
    149  1.1  dsl echo
    150  1.1  dsl 
    151  1.1  dsl IFS=' '
    152  1.1  dsl for f in $includes; do
    153  1.1  dsl 	echo "#include $f"
    154  1.1  dsl done
    155  1.1  dsl 
    156  1.1  dsl echo
    157  1.1  dsl echo
    158  1.1  dsl echo
    159  1.1  dsl echo "$defines"
    160  1.1  dsl echo
    161  1.1  dsl echo "$decles"
    162  1.1  dsl echo
    163  1.1  dsl echo
    164  1.1  dsl echo "/*"
    165  1.1  dsl echo " * Initialization code."
    166  1.1  dsl echo " */"
    167  1.1  dsl echo
    168  1.1  dsl echo "void"
    169  1.1  dsl echo "init() {"
    170  1.1  dsl echo "${event_init%$nl}"
    171  1.1  dsl echo "}"
    172  1.1  dsl echo
    173  1.1  dsl echo
    174  1.1  dsl echo
    175  1.1  dsl echo "/*"
    176  1.1  dsl echo " * This routine is called when an error or an interrupt occurs in an"
    177  1.1  dsl echo " * interactive shell and control is returned to the main command loop."
    178  1.1  dsl echo " */"
    179  1.1  dsl echo
    180  1.1  dsl echo "void"
    181  1.1  dsl echo "reset() {"
    182  1.1  dsl echo "${event_reset%$nl}"
    183  1.1  dsl echo "}"
    184  1.1  dsl echo
    185  1.1  dsl echo
    186  1.1  dsl echo
    187  1.1  dsl echo "/*"
    188  1.1  dsl echo " * This routine is called to initialize the shell to run a shell procedure."
    189  1.1  dsl echo " */"
    190  1.1  dsl echo
    191  1.1  dsl echo "void"
    192  1.1  dsl echo "initshellproc() {"
    193  1.1  dsl echo "${event_shellproc%$nl}"
    194  1.1  dsl echo "}"
    195  1.1  dsl 
    196  1.1  dsl exec >&-
    197  1.1  dsl mv init.c.tmp init.c
    198