Home | History | Annotate | Line # | Download | only in sh
mkinit.sh revision 1.3
      1  1.1  dsl #! /bin/sh
      2  1.3  dsl #	$NetBSD: mkinit.sh,v 1.3 2008/02/27 21:56:14 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.3  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.3  dsl 		ev="${nl}	/* from $src: */${nl}	{${nl}"
    103  1.3  dsl 		# Indent the text by an extra <tab>
    104  1.1  dsl 		while
    105  1.1  dsl 			read -r line
    106  1.1  dsl 			[ "$line" != "}" ]
    107  1.1  dsl 		do
    108  1.3  dsl 			[ -n "$line" -a "$line" = "${line###}" ] &&
    109  1.3  dsl 				line="	$line"
    110  1.1  dsl 			ev="${ev}${line}${nl}"
    111  1.1  dsl 		done
    112  1.3  dsl 		ev="${ev}	}${nl}"
    113  1.1  dsl 		eval event_$event=\"\$event_$event\$ev\"
    114  1.1  dsl 	done
    115  1.1  dsl done
    116  1.1  dsl 
    117  1.1  dsl exec >init.c.tmp
    118  1.1  dsl 
    119  1.1  dsl echo "/*"
    120  1.1  dsl echo " * This file was generated by the mkinit program."
    121  1.1  dsl echo " */"
    122  1.1  dsl echo
    123  1.1  dsl 
    124  1.1  dsl IFS=' '
    125  1.1  dsl for f in $includes; do
    126  1.1  dsl 	echo "#include $f"
    127  1.1  dsl done
    128  1.1  dsl 
    129  1.1  dsl echo
    130  1.1  dsl echo
    131  1.1  dsl echo
    132  1.1  dsl echo "$defines"
    133  1.1  dsl echo
    134  1.1  dsl echo "$decles"
    135  1.1  dsl echo
    136  1.1  dsl echo
    137  1.1  dsl echo "/*"
    138  1.1  dsl echo " * Initialization code."
    139  1.1  dsl echo " */"
    140  1.1  dsl echo
    141  1.1  dsl echo "void"
    142  1.3  dsl echo "init(void)"
    143  1.3  dsl echo "{"
    144  1.1  dsl echo "${event_init%$nl}"
    145  1.1  dsl echo "}"
    146  1.1  dsl echo
    147  1.1  dsl echo
    148  1.1  dsl echo
    149  1.1  dsl echo "/*"
    150  1.1  dsl echo " * This routine is called when an error or an interrupt occurs in an"
    151  1.1  dsl echo " * interactive shell and control is returned to the main command loop."
    152  1.1  dsl echo " */"
    153  1.1  dsl echo
    154  1.1  dsl echo "void"
    155  1.3  dsl echo "reset(void)"
    156  1.3  dsl echo "{"
    157  1.1  dsl echo "${event_reset%$nl}"
    158  1.1  dsl echo "}"
    159  1.1  dsl echo
    160  1.1  dsl echo
    161  1.1  dsl echo
    162  1.1  dsl echo "/*"
    163  1.1  dsl echo " * This routine is called to initialize the shell to run a shell procedure."
    164  1.1  dsl echo " */"
    165  1.1  dsl echo
    166  1.1  dsl echo "void"
    167  1.3  dsl echo "initshellproc(void)"
    168  1.3  dsl echo "{"
    169  1.1  dsl echo "${event_shellproc%$nl}"
    170  1.1  dsl echo "}"
    171  1.1  dsl 
    172  1.1  dsl exec >&-
    173  1.1  dsl mv init.c.tmp init.c
    174