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