mkinit.sh revision 1.9 1 1.1 dsl #! /bin/sh
2 1.9 kre # $NetBSD: mkinit.sh,v 1.9 2018/10/18 04:24:43 kre 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.1 dsl nl='
34 1.1 dsl '
35 1.2 dsl openparen='('
36 1.9 kre
37 1.9 kre # shells have bugs (including older NetBSD sh) in how \ is
38 1.9 kre # used in pattern matching. So work out what the shell
39 1.9 kre # running this script expects. We could also just use a
40 1.9 kre # literal \ in the pattern, which would need to be quoted
41 1.9 kre # of course, but then we'd run into a whole host of potential
42 1.9 kre # other shell bugs (both with the quoting in the pattern, and
43 1.9 kre # with the matching that follows if that works as inended).
44 1.9 kre # Far easier, and more reliable, is to just work out what works,
45 1.9 kre # and then use it, which more or less mandates using a variable...
46 1.9 kre backslash='\\'
47 1.9 kre var='abc\' # dummy test case.
48 1.9 kre if [ "$var" = "${var%$backslash}" ]
49 1.9 kre then
50 1.9 kre # buggy sh, try the broken way
51 1.9 kre backslash='\'
52 1.9 kre if [ "$var" = "${var%$backslash}" ]
53 1.9 kre then
54 1.9 kre printf >&2 "$0: %s\n" 'No pattern match with \ (broken shell)'
55 1.9 kre exit 1
56 1.9 kre fi
57 1.9 kre fi
58 1.9 kre # We know we can detect the presence of a trailing \, which is all we need.
59 1.9 kre # Now to confirm we will not generate false matches.
60 1.9 kre var='abc'
61 1.9 kre if [ "$var" != "${var%$backslash}" ]
62 1.9 kre then
63 1.9 kre printf >&2 "$0: %s\n" 'Bogus pattern match with \ (broken shell)'
64 1.9 kre exit 1
65 1.9 kre fi
66 1.9 kre unset var
67 1.1 dsl
68 1.1 dsl includes=' "shell.h" "mystring.h" "init.h" '
69 1.1 dsl defines=
70 1.1 dsl decles=
71 1.1 dsl event_init=
72 1.1 dsl event_reset=
73 1.1 dsl event_shellproc=
74 1.1 dsl
75 1.1 dsl for src in $srcs; do
76 1.1 dsl exec <$src
77 1.1 dsl decnl="$nl"
78 1.1 dsl while IFS=; read -r line; do
79 1.1 dsl [ "$line" = x ]
80 1.1 dsl case "$line " in
81 1.1 dsl INIT["{ "]* ) event=init;;
82 1.1 dsl RESET["{ "]* ) event=reset;;
83 1.1 dsl SHELLPROC["{ "]* ) event=shellproc;;
84 1.1 dsl INCLUDE[\ \ ]* )
85 1.1 dsl IFS=' '
86 1.1 dsl set -- $line
87 1.1 dsl # ignore duplicates
88 1.7 christos [ "${includes}" != "${includes% $2 *}" ] && continue
89 1.1 dsl includes="$includes$2 "
90 1.1 dsl continue
91 1.1 dsl ;;
92 1.1 dsl MKINIT\ )
93 1.1 dsl # struct declaration
94 1.1 dsl decles="$decles$nl"
95 1.1 dsl while
96 1.1 dsl read -r line
97 1.1 dsl decles="${decles}${line}${nl}"
98 1.1 dsl [ "$line" != "};" ]
99 1.1 dsl do
100 1.1 dsl :
101 1.1 dsl done
102 1.1 dsl decnl="$nl"
103 1.1 dsl continue
104 1.1 dsl ;;
105 1.1 dsl MKINIT["{ "]* )
106 1.1 dsl # strip initialiser
107 1.1 dsl def=${line#MKINIT}
108 1.1 dsl comment="${def#*;}"
109 1.1 dsl def="${def%;$comment}"
110 1.1 dsl def="${def%%=*}"
111 1.1 dsl def="${def% }"
112 1.1 dsl decles="${decles}${decnl}extern${def};${comment}${nl}"
113 1.1 dsl decnl=
114 1.1 dsl continue
115 1.1 dsl ;;
116 1.1 dsl \#define[\ \ ]* )
117 1.1 dsl IFS=' '
118 1.1 dsl set -- $line
119 1.1 dsl # Ignore those with arguments
120 1.2 dsl [ "$2" = "${2##*$openparen}" ] || continue
121 1.1 dsl # and multiline definitions
122 1.2 dsl [ "$line" = "${line%$backslash}" ] || continue
123 1.3 dsl defines="${defines}#undef $2${nl}${line}${nl}"
124 1.1 dsl continue
125 1.1 dsl ;;
126 1.1 dsl * ) continue;;
127 1.1 dsl esac
128 1.1 dsl # code for events
129 1.3 dsl ev="${nl} /* from $src: */${nl} {${nl}"
130 1.3 dsl # Indent the text by an extra <tab>
131 1.1 dsl while
132 1.1 dsl read -r line
133 1.1 dsl [ "$line" != "}" ]
134 1.1 dsl do
135 1.8 kre case "$line" in
136 1.8 kre ('') ;;
137 1.8 kre ('#'*) ;;
138 1.8 kre (*) line=" $line";;
139 1.8 kre esac
140 1.1 dsl ev="${ev}${line}${nl}"
141 1.1 dsl done
142 1.3 dsl ev="${ev} }${nl}"
143 1.1 dsl eval event_$event=\"\$event_$event\$ev\"
144 1.1 dsl done
145 1.1 dsl done
146 1.1 dsl
147 1.1 dsl exec >init.c.tmp
148 1.1 dsl
149 1.1 dsl echo "/*"
150 1.1 dsl echo " * This file was generated by the mkinit program."
151 1.1 dsl echo " */"
152 1.1 dsl echo
153 1.1 dsl
154 1.1 dsl IFS=' '
155 1.1 dsl for f in $includes; do
156 1.1 dsl echo "#include $f"
157 1.1 dsl done
158 1.1 dsl
159 1.1 dsl echo
160 1.1 dsl echo
161 1.1 dsl echo
162 1.1 dsl echo "$defines"
163 1.1 dsl echo
164 1.1 dsl echo "$decles"
165 1.1 dsl echo
166 1.1 dsl echo
167 1.1 dsl echo "/*"
168 1.1 dsl echo " * Initialization code."
169 1.1 dsl echo " */"
170 1.1 dsl echo
171 1.1 dsl echo "void"
172 1.3 dsl echo "init(void)"
173 1.3 dsl echo "{"
174 1.5 apb echo "${event_init}"
175 1.1 dsl echo "}"
176 1.1 dsl echo
177 1.1 dsl echo
178 1.1 dsl echo
179 1.1 dsl echo "/*"
180 1.1 dsl echo " * This routine is called when an error or an interrupt occurs in an"
181 1.1 dsl echo " * interactive shell and control is returned to the main command loop."
182 1.1 dsl echo " */"
183 1.1 dsl echo
184 1.1 dsl echo "void"
185 1.3 dsl echo "reset(void)"
186 1.3 dsl echo "{"
187 1.5 apb echo "${event_reset}"
188 1.1 dsl echo "}"
189 1.1 dsl echo
190 1.1 dsl echo
191 1.1 dsl echo
192 1.1 dsl echo "/*"
193 1.1 dsl echo " * This routine is called to initialize the shell to run a shell procedure."
194 1.1 dsl echo " */"
195 1.1 dsl echo
196 1.1 dsl echo "void"
197 1.3 dsl echo "initshellproc(void)"
198 1.3 dsl echo "{"
199 1.5 apb echo "${event_shellproc}"
200 1.1 dsl echo "}"
201 1.1 dsl
202 1.1 dsl exec >&-
203 1.1 dsl mv init.c.tmp init.c
204