2
* Copyright (c) 1991, 1993
3
* The Regents of the University of California. All rights reserved.
4
* Copyright (c) 1997-2005
5
* Herbert Xu <herbert@gondor.apana.org.au>. All rights reserved.
7
* This code is derived from software contributed to Berkeley by
10
* Redistribution and use in source and binary forms, with or without
11
* modification, are permitted provided that the following conditions
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 University nor the names of its contributors
19
* may be used to endorse or promote products derived from this software
20
* without specific prior written permission.
22
* THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
23
* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
24
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
25
* ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
26
* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
27
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
28
* OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
29
* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
30
* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
31
* OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
36
* This program scans all the source files for code to handle various
37
* special events and combines this code into one file. This (allegedly)
38
* improves the structure of the program since there is no need for
39
* anyone outside of a module to know that that module performs special
40
* operations on particular events.
42
* Usage: mkinit sourcefile...
46
#include <sys/types.h>
55
* OUTFILE is the name of the output file. Output is initially written
56
* to the file OUTTEMP, which is then moved to OUTFILE.
59
#define OUTFILE "init.c"
60
#define OUTTEMP "init.c.new"
64
* A text structure is basicly just a string that grows as more characters
65
* are added onto the end of it. It is implemented as a linked list of
66
* blocks of characters. The routines addstr and addchar append a string
67
* or a single character, respectively, to a text structure. Writetext
68
* writes the contents of a text structure to a file.
87
* There is one event structure for each event that mkinit handles.
91
char *name; /* name of event (e.g. INIT) */
92
char *routine; /* name of routine called on event */
93
char *comment; /* comment describing routine */
94
struct text code; /* code for handling event */
100
* This file was generated by the mkinit program.\n\
106
* Initialization code.\n\
111
* This routine is called when an error or an interrupt occurs in an\n\
112
* interactive shell and control is returned to the main command loop.\n\
116
struct event event[] = {
117
{"INIT", "init", init},
118
{"RESET", "reset", reset},
123
char *curfile; /* current file */
124
int linno; /* current line */
125
char *header_files[200]; /* list of header files */
126
struct text defines; /* #define statements */
127
struct text decls; /* declarations */
128
int amiddecls; /* for formatting */
131
void readfile(char *);
132
int match(char *, char *);
133
int gooddefine(char *);
134
void doevent(struct event *, FILE *, char *);
135
void doinclude(char *);
136
void dodecl(char *, FILE *);
138
void addstr(char *, struct text *);
139
void addchar(int, struct text *);
140
void writetext(struct text *, FILE *);
141
FILE *ckfopen(char *, char *);
143
char *savestr(char *);
144
static void error(char *);
145
int main(int, char **);
147
#define equal(s1, s2) (strcmp(s1, s2) == 0)
150
main(int argc, char **argv)
154
header_files[0] = "\"shell.h\"";
155
header_files[1] = "\"mystring.h\"";
156
header_files[2] = "\"init.h\"";
157
for (ap = argv + 1 ; *ap ; ap++)
160
rename(OUTTEMP, OUTFILE);
167
* Parse an input file.
171
readfile(char *fname)
177
fp = ckfopen(fname, "r");
181
while (fgets(line, sizeof line, fp) != NULL) {
183
for (ep = event ; ep->name ; ep++) {
184
if (line[0] == ep->name[0] && match(ep->name, line)) {
185
doevent(ep, fp, fname);
189
if (line[0] == 'I' && match("INCLUDE", line))
191
if (line[0] == 'M' && match("MKINIT", line))
193
if (line[0] == '#' && gooddefine(line)) {
196
static const char undef[] = "#undef ";
199
memcpy(line2, undef, sizeof(undef) - 1);
200
cp = line2 + sizeof(undef) - 1;
201
while(*cp && (*cp == ' ' || *cp == '\t'))
203
while(*cp && *cp != ' ' && *cp != '\t' && *cp != '\n')
205
*cp++ = '\n'; *cp = '\0';
206
addstr(line2, &defines);
207
addstr(line, &defines);
215
match(char *name, char *line)
224
if (*q != '{' && *q != ' ' && *q != '\t' && *q != '\n')
231
gooddefine(char *line)
235
if (! match("#define", line))
236
return 0; /* not a define */
238
while (*p == ' ' || *p == '\t')
240
while (*p != ' ' && *p != '\t') {
242
return 0; /* macro definition */
245
while (*p != '\n' && *p != '\0')
248
return 0; /* multi-line definition */
254
doevent(struct event *ep, FILE *fp, char *fname)
260
sprintf(line, "\n /* from %s: */\n", fname);
261
addstr(line, &ep->code);
262
addstr(" {\n", &ep->code);
265
if (fgets(line, sizeof line, fp) == NULL)
266
error("Unexpected EOF");
267
if (equal(line, "}\n"))
270
for (p = line ; *p == '\t' ; p++)
272
for ( ; *p == ' ' ; p++)
274
if (*p == '\n' || *p == '#')
276
while (indent >= 8) {
277
addchar('\t', &ep->code);
281
addchar(' ', &ep->code);
284
addstr(p, &ep->code);
286
addstr(" }\n", &ep->code);
291
doinclude(char *line)
297
for (p = line ; *p != '"' && *p != '<' && *p != '\0' ; p++);
299
error("Expecting '\"' or '<'");
301
while (*p != ' ' && *p != '\t' && *p != '\n')
303
if (p[-1] != '"' && p[-1] != '>')
304
error("Missing terminator");
307
/* name now contains the name of the include file */
308
for (pp = header_files ; *pp && ! equal(*pp, name) ; pp++);
315
dodecl(char *line1, FILE *fp)
320
if (strcmp(line1, "MKINIT\n") == 0) { /* start of struct/union decl */
321
addchar('\n', &decls);
324
if (fgets(line, sizeof line, fp) == NULL)
325
error("Unterminated structure declaration");
326
addstr(line, &decls);
327
} while (line[0] != '}');
331
addchar('\n', &decls);
333
for (p = line1 + 6 ; *p && strchr("=/\n", *p) == NULL; p++)
335
if (*p == '=') { /* eliminate initialization */
336
for (q = p ; *q && *q != ';' ; q++);
345
addstr("extern", &decls);
346
addstr(line1 + 6, &decls);
356
* Write the output to the file OUTTEMP.
366
fp = ckfopen(OUTTEMP, "w");
368
for (pp = header_files ; *pp ; pp++)
369
fprintf(fp, "#include %s\n", *pp);
371
writetext(&defines, fp);
373
writetext(&decls, fp);
374
for (ep = event ; ep->name ; ep++) {
376
fputs(ep->comment, fp);
377
fprintf(fp, "\nvoid\n%s() {\n", ep->routine);
378
writetext(&ep->code, fp);
386
* A text structure is simply a block of text that is kept in memory.
387
* Addstr appends a string to the text struct, and addchar appends a single
392
addstr(char *s, struct text *text)
395
if (--text->nleft < 0)
398
*text->nextc++ = *s++;
404
addchar(int c, struct text *text)
408
if (--text->nleft < 0) {
409
bp = ckmalloc(sizeof *bp);
410
if (text->start == NULL)
413
text->last->next = bp;
415
text->nextc = bp->text;
416
text->nleft = BLOCKSIZE - 1;
422
* Write the contents of a text structure to a file.
425
writetext(struct text *text, FILE *fp)
429
if (text->start != NULL) {
430
for (bp = text->start ; bp != text->last ; bp = bp->next)
431
fwrite(bp->text, sizeof (char), BLOCKSIZE, fp);
432
fwrite(bp->text, sizeof (char), BLOCKSIZE - text->nleft, fp);
437
ckfopen(char *file, char *mode)
441
if ((fp = fopen(file, mode)) == NULL) {
442
fprintf(stderr, "Can't open %s\n", file);
453
if ((p = malloc(nbytes)) == NULL)
454
error("Out of space");
463
p = ckmalloc(strlen(s) + 1);
472
fprintf(stderr, "%s:%d: ", curfile, linno);
473
fprintf(stderr, "%s\n", msg);