Home | History | Annotate | Line # | Download | only in sh
mkinit.sh revision 1.7.16.1
      1       1.1       dsl #! /bin/sh
      2  1.7.16.1  christos #	$NetBSD: mkinit.sh,v 1.7.16.1 2019/06/10 21:41:04 christos 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 #
     19       1.1       dsl # THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
     20       1.1       dsl # ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
     21       1.1       dsl # TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
     22       1.1       dsl # PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
     23       1.1       dsl # BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
     24       1.1       dsl # CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
     25       1.1       dsl # SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
     26       1.1       dsl # INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
     27       1.1       dsl # CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
     28       1.1       dsl # ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
     29       1.1       dsl # POSSIBILITY OF SUCH DAMAGE.
     30       1.1       dsl 
     31       1.1       dsl srcs="$*"
     32       1.1       dsl 
     33  1.7.16.1  christos # use of echo in this script is broken
     34  1.7.16.1  christos 
     35  1.7.16.1  christos # some echo versions will expand \n in the args, which breaks C
     36  1.7.16.1  christos # Note: this script is a HOST_PROG ... it must run in the
     37  1.7.16.1  christos # build host's environment, with its shell.
     38  1.7.16.1  christos 
     39  1.7.16.1  christos # Fortunately, use of echo here is also trivially simplistic,
     40  1.7.16.1  christos # we can easily replace all uses with ...
     41  1.7.16.1  christos 
     42  1.7.16.1  christos echo()
     43  1.7.16.1  christos {
     44  1.7.16.1  christos 	printf '%s\n' "$1"
     45  1.7.16.1  christos }
     46  1.7.16.1  christos 
     47  1.7.16.1  christos # CAUTION: for anyone modifying this script.... use printf
     48  1.7.16.1  christos # rather than echo to output anything at all... then
     49  1.7.16.1  christos # you will avoid being bitten by the simplicity of this function.
     50  1.7.16.1  christos # This was done this way rather than wholesale replacement
     51  1.7.16.1  christos # to avoid unnecessary code churn.
     52  1.7.16.1  christos 
     53  1.7.16.1  christos 
     54       1.1       dsl nl='
     55       1.1       dsl '
     56       1.2       dsl openparen='('
     57  1.7.16.1  christos 
     58  1.7.16.1  christos # shells have bugs (including older NetBSD sh) in how \ is
     59  1.7.16.1  christos # used in pattern matching.   So work out what the shell
     60  1.7.16.1  christos # running this script expects.   We could also just use a
     61  1.7.16.1  christos # literal \ in the pattern, which would need to be quoted
     62  1.7.16.1  christos # of course, but then we'd run into a whole host of potential
     63  1.7.16.1  christos # other shell bugs (both with the quoting in the pattern, and
     64  1.7.16.1  christos # with the matching that follows if that works as inended).
     65  1.7.16.1  christos # Far easier, and more reliable, is to just work out what works,
     66  1.7.16.1  christos # and then use it, which more or less mandates using a variable...
     67  1.7.16.1  christos backslash='\\'
     68  1.7.16.1  christos var='abc\'			# dummy test case.
     69  1.7.16.1  christos if [ "$var" = "${var%$backslash}" ]
     70  1.7.16.1  christos then
     71  1.7.16.1  christos 	# buggy sh, try the broken way
     72  1.7.16.1  christos 	backslash='\'
     73  1.7.16.1  christos 	if [ "$var" = "${var%$backslash}" ]
     74  1.7.16.1  christos 	then
     75  1.7.16.1  christos 		printf >&2 "$0: %s\n" 'No pattern match with \ (broken shell)'
     76  1.7.16.1  christos 		exit 1
     77  1.7.16.1  christos 	fi
     78  1.7.16.1  christos fi
     79  1.7.16.1  christos # We know we can detect the presence of a trailing \, which is all we need.
     80  1.7.16.1  christos # Now to confirm we will not generate false matches.
     81  1.7.16.1  christos var='abc'
     82  1.7.16.1  christos if [ "$var" != "${var%$backslash}" ]
     83  1.7.16.1  christos then
     84  1.7.16.1  christos 	printf >&2 "$0: %s\n" 'Bogus pattern match with \ (broken shell)'
     85  1.7.16.1  christos 	exit 1
     86  1.7.16.1  christos fi
     87  1.7.16.1  christos unset var
     88       1.1       dsl 
     89       1.1       dsl includes=' "shell.h" "mystring.h" "init.h" '
     90       1.1       dsl defines=
     91       1.1       dsl decles=
     92       1.1       dsl event_init=
     93       1.1       dsl event_reset=
     94       1.1       dsl event_shellproc=
     95       1.1       dsl 
     96       1.1       dsl for src in $srcs; do
     97       1.1       dsl 	exec <$src
     98       1.1       dsl 	decnl="$nl"
     99       1.1       dsl 	while IFS=; read -r line; do
    100       1.1       dsl 		[ "$line" = x ]
    101       1.1       dsl 		case "$line " in
    102       1.1       dsl 		INIT["{ 	"]* ) event=init;;
    103       1.1       dsl 		RESET["{ 	"]* ) event=reset;;
    104       1.1       dsl 		SHELLPROC["{ 	"]* ) event=shellproc;;
    105       1.1       dsl 		INCLUDE[\ \	]* )
    106       1.1       dsl 			IFS=' 	'
    107       1.1       dsl 			set -- $line
    108       1.1       dsl 			# ignore duplicates
    109       1.7  christos 			[ "${includes}" != "${includes% $2 *}" ] && continue
    110       1.1       dsl 			includes="$includes$2 "
    111       1.1       dsl 			continue
    112       1.1       dsl 			;;
    113       1.1       dsl 		MKINIT\  )
    114       1.1       dsl 			# struct declaration
    115       1.1       dsl 			decles="$decles$nl"
    116       1.1       dsl 			while
    117       1.1       dsl 				read -r line
    118       1.1       dsl 				decles="${decles}${line}${nl}"
    119       1.1       dsl 				[ "$line" != "};" ]
    120       1.1       dsl 			do
    121       1.1       dsl 				:
    122       1.1       dsl 			done
    123       1.1       dsl 			decnl="$nl"
    124       1.1       dsl 			continue
    125       1.1       dsl 			;;
    126       1.1       dsl 		MKINIT["{ 	"]* )
    127       1.1       dsl 			# strip initialiser
    128       1.1       dsl 			def=${line#MKINIT}
    129       1.1       dsl 			comment="${def#*;}"
    130       1.1       dsl 			def="${def%;$comment}"
    131       1.1       dsl 			def="${def%%=*}"
    132       1.1       dsl 			def="${def% }"
    133       1.1       dsl 			decles="${decles}${decnl}extern${def};${comment}${nl}"
    134       1.1       dsl 			decnl=
    135       1.1       dsl 			continue
    136       1.1       dsl 			;;
    137       1.1       dsl 		\#define[\ \	]* )
    138       1.1       dsl 			IFS=' 	'
    139       1.1       dsl 			set -- $line
    140       1.1       dsl 			# Ignore those with arguments
    141       1.2       dsl 			[ "$2" = "${2##*$openparen}" ] || continue
    142       1.1       dsl 			# and multiline definitions
    143       1.2       dsl 			[ "$line" = "${line%$backslash}" ] || continue
    144       1.3       dsl 			defines="${defines}#undef	$2${nl}${line}${nl}"
    145       1.1       dsl 			continue
    146       1.1       dsl 			;;
    147       1.1       dsl 		* ) continue;;
    148       1.1       dsl 		esac
    149       1.1       dsl 		# code for events
    150       1.3       dsl 		ev="${nl}	/* from $src: */${nl}	{${nl}"
    151       1.3       dsl 		# Indent the text by an extra <tab>
    152       1.1       dsl 		while
    153       1.1       dsl 			read -r line
    154       1.1       dsl 			[ "$line" != "}" ]
    155       1.1       dsl 		do
    156  1.7.16.1  christos 			case "$line" in
    157  1.7.16.1  christos 			('')	;;
    158  1.7.16.1  christos 			('#'*)	;;
    159  1.7.16.1  christos 			(*)	line="	$line";;
    160  1.7.16.1  christos 			esac
    161       1.1       dsl 			ev="${ev}${line}${nl}"
    162       1.1       dsl 		done
    163       1.3       dsl 		ev="${ev}	}${nl}"
    164       1.1       dsl 		eval event_$event=\"\$event_$event\$ev\"
    165       1.1       dsl 	done
    166       1.1       dsl done
    167       1.1       dsl 
    168       1.1       dsl exec >init.c.tmp
    169       1.1       dsl 
    170       1.1       dsl echo "/*"
    171       1.1       dsl echo " * This file was generated by the mkinit program."
    172       1.1       dsl echo " */"
    173       1.1       dsl echo
    174       1.1       dsl 
    175       1.1       dsl IFS=' '
    176       1.1       dsl for f in $includes; do
    177       1.1       dsl 	echo "#include $f"
    178       1.1       dsl done
    179       1.1       dsl 
    180       1.1       dsl echo
    181       1.1       dsl echo
    182       1.1       dsl echo
    183       1.1       dsl echo "$defines"
    184       1.1       dsl echo
    185       1.1       dsl echo "$decles"
    186       1.1       dsl echo
    187       1.1       dsl echo
    188       1.1       dsl echo "/*"
    189       1.1       dsl echo " * Initialization code."
    190       1.1       dsl echo " */"
    191       1.1       dsl echo
    192       1.1       dsl echo "void"
    193       1.3       dsl echo "init(void)"
    194       1.3       dsl echo "{"
    195       1.5       apb echo "${event_init}"
    196       1.1       dsl echo "}"
    197       1.1       dsl echo
    198       1.1       dsl echo
    199       1.1       dsl echo
    200       1.1       dsl echo "/*"
    201       1.1       dsl echo " * This routine is called when an error or an interrupt occurs in an"
    202       1.1       dsl echo " * interactive shell and control is returned to the main command loop."
    203       1.1       dsl echo " */"
    204       1.1       dsl echo
    205       1.1       dsl echo "void"
    206       1.3       dsl echo "reset(void)"
    207       1.3       dsl echo "{"
    208       1.5       apb echo "${event_reset}"
    209       1.1       dsl echo "}"
    210       1.1       dsl echo
    211       1.1       dsl echo
    212       1.1       dsl echo
    213       1.1       dsl echo "/*"
    214       1.1       dsl echo " * This routine is called to initialize the shell to run a shell procedure."
    215       1.1       dsl echo " */"
    216       1.1       dsl echo
    217       1.1       dsl echo "void"
    218       1.3       dsl echo "initshellproc(void)"
    219       1.3       dsl echo "{"
    220       1.5       apb echo "${event_shellproc}"
    221       1.1       dsl echo "}"
    222       1.1       dsl 
    223       1.1       dsl exec >&-
    224       1.1       dsl mv init.c.tmp init.c
    225