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