2 * Copyright (c) 1991, 1993
3 * The Regents of the University of California. All rights reserved.
5 * This code is derived from software contributed to Berkeley by
8 * Redistribution and use in source and binary forms, with or without
9 * modification, are permitted provided that the following conditions
11 * 1. Redistributions of source code must retain the above copyright
12 * notice, this list of conditions and the following disclaimer.
13 * 2. Redistributions in binary form must reproduce the above copyright
14 * notice, this list of conditions and the following disclaimer in the
15 * documentation and/or other materials provided with the distribution.
16 * 3. All advertising materials mentioning features or use of this software
17 * must display the following acknowledgement:
18 * This product includes software developed by the University of
19 * California, Berkeley and its contributors.
20 * 4. Neither the name of the University nor the names of its contributors
21 * may be used to endorse or promote products derived from this software
22 * without specific prior written permission.
24 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
25 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
26 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
27 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
28 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
29 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
30 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
31 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
32 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
33 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
38 static char const copyright[] =
39 "@(#) Copyright (c) 1991, 1993\n\
40 The Regents of the University of California. All rights reserved.\n";
45 static char sccsid[] = "@(#)mkinit.c 8.2 (Berkeley) 5/4/95";
47 static const char rcsid[] =
52 * This program scans all the source files for code to handle various
53 * special events and combines this code into one file. This (allegedly)
54 * improves the structure of the program since there is no need for
55 * anyone outside of a module to know that that module performs special
56 * operations on particular events.
58 * Usage: mkinit sourcefile...
62 #include <sys/cdefs.h>
63 #include <sys/types.h>
72 * OUTFILE is the name of the output file. Output is initially written
73 * to the file OUTTEMP, which is then moved to OUTFILE.
76 #define OUTFILE "init.c"
77 #define OUTTEMP "init.c.new"
81 * A text structure is basicly just a string that grows as more characters
82 * are added onto the end of it. It is implemented as a linked list of
83 * blocks of characters. The routines addstr and addchar append a string
84 * or a single character, respectively, to a text structure. Writetext
85 * writes the contents of a text structure to a file.
104 * There is one event structure for each event that mkinit handles.
108 char *name; /* name of event (e.g. INIT) */
109 char *routine; /* name of routine called on event */
110 char *comment; /* comment describing routine */
111 struct text code; /* code for handling event */
117 * This file was generated by the mkinit program.\n\
123 * Initialization code.\n\
128 * This routine is called when an error or an interrupt occurs in an\n\
129 * interactive shell and control is returned to the main command loop.\n\
132 char shellproc[] = "\
134 * This routine is called to initialize the shell to run a shell procedure.\n\
138 struct event event[] = {
139 {"INIT", "init", init},
140 {"RESET", "reset", reset},
141 {"SHELLPROC", "initshellproc", shellproc},
146 char *curfile; /* current file */
147 int linno; /* current line */
148 char *header_files[200]; /* list of header files */
149 struct text defines; /* #define statements */
150 struct text decls; /* declarations */
151 int amiddecls; /* for formatting */
154 void readfile __P((char *));
155 int match __P((char *, char *));
156 int gooddefine __P((char *));
157 void doevent __P((struct event *, FILE *, char *));
158 void doinclude __P((char *));
159 void dodecl __P((char *, FILE *));
160 void output __P((void));
161 void addstr __P((char *, struct text *));
162 void addchar __P((int, struct text *));
163 void writetext __P((struct text *, FILE *));
164 FILE *ckfopen __P((char *, char *));
165 void *ckmalloc __P((int));
166 char *savestr __P((char *));
167 void error __P((char *));
169 #define equal(s1, s2) (strcmp(s1, s2) == 0)
178 header_files[0] = "\"shell.h\"";
179 header_files[1] = "\"mystring.h\"";
180 for (ap = argv + 1 ; *ap ; ap++)
183 rename(OUTTEMP, OUTFILE);
189 * Parse an input file.
200 fp = ckfopen(fname, "r");
204 while (fgets(line, sizeof line, fp) != NULL) {
206 for (ep = event ; ep->name ; ep++) {
207 if (line[0] == ep->name[0] && match(ep->name, line)) {
208 doevent(ep, fp, fname);
212 if (line[0] == 'I' && match("INCLUDE", line))
214 if (line[0] == 'M' && match("MKINIT", line))
216 if (line[0] == '#' && gooddefine(line)) {
219 static const char undef[] = "#undef ";
222 memcpy(line2, undef, sizeof(undef) - 1);
223 cp = line2 + sizeof(undef) - 1;
224 while(*cp && (*cp == ' ' || *cp == '\t'))
226 while(*cp && *cp != ' ' && *cp != '\t' && *cp != '\n')
228 *cp++ = '\n'; *cp = '\0';
229 addstr(line2, &defines);
230 addstr(line, &defines);
249 if (*q != '{' && *q != ' ' && *q != '\t' && *q != '\n')
261 if (! match("#define", line))
262 return 0; /* not a define */
264 while (*p == ' ' || *p == '\t')
266 while (*p != ' ' && *p != '\t') {
268 return 0; /* macro definition */
271 while (*p != '\n' && *p != '\0')
274 return 0; /* multi-line definition */
280 doevent(ep, fp, fname)
289 sprintf(line, "\n /* from %s: */\n", fname);
290 addstr(line, &ep->code);
291 addstr(" {\n", &ep->code);
294 if (fgets(line, sizeof line, fp) == NULL)
295 error("Unexpected EOF");
296 if (equal(line, "}\n"))
299 for (p = line ; *p == '\t' ; p++)
301 for ( ; *p == ' ' ; p++)
303 if (*p == '\n' || *p == '#')
305 while (indent >= 8) {
306 addchar('\t', &ep->code);
310 addchar(' ', &ep->code);
313 addstr(p, &ep->code);
315 addstr(" }\n", &ep->code);
327 for (p = line ; *p != '"' && *p != '<' && *p != '\0' ; p++);
329 error("Expecting '\"' or '<'");
331 while (*p != ' ' && *p != '\t' && *p != '\n')
333 if (p[-1] != '"' && p[-1] != '>')
334 error("Missing terminator");
337 /* name now contains the name of the include file */
338 for (pp = header_files ; *pp && ! equal(*pp, name) ; pp++);
352 if (strcmp(line1, "MKINIT\n") == 0) { /* start of struct/union decl */
353 addchar('\n', &decls);
356 if (fgets(line, sizeof line, fp) == NULL)
357 error("Unterminated structure declaration");
358 addstr(line, &decls);
359 } while (line[0] != '}');
363 addchar('\n', &decls);
365 for (p = line1 + 6 ; *p && strchr("=/\n", *p) == NULL; p++)
367 if (*p == '=') { /* eliminate initialization */
368 for (q = p ; *q && *q != ';' ; q++);
377 addstr("extern", &decls);
378 addstr(line1 + 6, &decls);
388 * Write the output to the file OUTTEMP.
397 fp = ckfopen(OUTTEMP, "w");
399 for (pp = header_files ; *pp ; pp++)
400 fprintf(fp, "#include %s\n", *pp);
402 writetext(&defines, fp);
404 writetext(&decls, fp);
405 for (ep = event ; ep->name ; ep++) {
407 fputs(ep->comment, fp);
408 fprintf(fp, "\nvoid\n%s() {\n", ep->routine);
409 writetext(&ep->code, fp);
417 * A text structure is simply a block of text that is kept in memory.
418 * Addstr appends a string to the text struct, and addchar appends a single
428 if (--text->nleft < 0)
431 *text->nextc++ = *s++;
443 if (--text->nleft < 0) {
444 bp = ckmalloc(sizeof *bp);
445 if (text->start == NULL)
448 text->last->next = bp;
450 text->nextc = bp->text;
451 text->nleft = BLOCKSIZE - 1;
457 * Write the contents of a text structure to a file.
466 if (text->start != NULL) {
467 for (bp = text->start ; bp != text->last ; bp = bp->next)
468 fwrite(bp->text, sizeof (char), BLOCKSIZE, fp);
469 fwrite(bp->text, sizeof (char), BLOCKSIZE - text->nleft, fp);
480 if ((fp = fopen(file, mode)) == NULL) {
481 fprintf(stderr, "Can't open %s\n", file);
493 if ((p = malloc(nbytes)) == NULL)
494 error("Out of space");
504 p = ckmalloc(strlen(s) + 1);
514 fprintf(stderr, "%s:%d: ", curfile, linno);
515 fprintf(stderr, "%s\n", msg);