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 * 4. Neither the name of the University nor the names of its contributors
17 * may be used to endorse or promote products derived from this software
18 * without specific prior written permission.
20 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
21 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
22 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
23 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
24 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
25 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
26 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
27 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
28 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
29 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
34 static char const copyright[] =
35 "@(#) Copyright (c) 1991, 1993\n\
36 The Regents of the University of California. All rights reserved.\n";
41 static char sccsid[] = "@(#)mkinit.c 8.2 (Berkeley) 5/4/95";
44 #include <sys/cdefs.h>
45 __FBSDID("$FreeBSD$");
48 * This program scans all the source files for code to handle various
49 * special events and combines this code into one file. This (allegedly)
50 * improves the structure of the program since there is no need for
51 * anyone outside of a module to know that that module performs special
52 * operations on particular events.
54 * Usage: mkinit sourcefile...
58 #include <sys/cdefs.h>
59 #include <sys/types.h>
69 * OUTFILE is the name of the output file. Output is initially written
70 * to the file OUTTEMP, which is then moved to OUTFILE.
73 #define OUTFILE "init.c"
74 #define OUTTEMP "init.c.new"
78 * A text structure is basicly just a string that grows as more characters
79 * are added onto the end of it. It is implemented as a linked list of
80 * blocks of characters. The routines addstr and addchar append a string
81 * or a single character, respectively, to a text structure. Writetext
82 * writes the contents of a text structure to a file.
101 * There is one event structure for each event that mkinit handles.
105 char *name; /* name of event (e.g. INIT) */
106 char *routine; /* name of routine called on event */
107 char *comment; /* comment describing routine */
108 struct text code; /* code for handling event */
114 * This file was generated by the mkinit program.\n\
120 * Initialization code.\n\
125 * This routine is called when an error or an interrupt occurs in an\n\
126 * interactive shell and control is returned to the main command loop.\n\
129 char shellproc[] = "\
131 * This routine is called to initialize the shell to run a shell procedure.\n\
135 struct event event[] = {
136 {"INIT", "init", init},
137 {"RESET", "reset", reset},
138 {"SHELLPROC", "initshellproc", shellproc},
143 char *curfile; /* current file */
144 int linno; /* current line */
145 char *header_files[200]; /* list of header files */
146 struct text defines; /* #define statements */
147 struct text decls; /* declarations */
148 int amiddecls; /* for formatting */
151 void readfile(char *);
152 int match(char *, char *);
153 int gooddefine(char *);
154 void doevent(struct event *, FILE *, char *);
155 void doinclude(char *);
156 void dodecl(char *, FILE *);
158 void addstr(char *, struct text *);
159 void addchar(int, struct text *);
160 void writetext(struct text *, FILE *);
161 FILE *ckfopen(char *, char *);
163 char *savestr(char *);
166 #define equal(s1, s2) (strcmp(s1, s2) == 0)
169 main(int argc __unused, char *argv[])
173 header_files[0] = "\"shell.h\"";
174 header_files[1] = "\"mystring.h\"";
175 for (ap = argv + 1 ; *ap ; ap++)
178 rename(OUTTEMP, OUTFILE);
184 * Parse an input file.
188 readfile(char *fname)
194 fp = ckfopen(fname, "r");
198 while (fgets(line, sizeof line, fp) != NULL) {
200 for (ep = event ; ep->name ; ep++) {
201 if (line[0] == ep->name[0] && match(ep->name, line)) {
202 doevent(ep, fp, fname);
206 if (line[0] == 'I' && match("INCLUDE", line))
208 if (line[0] == 'M' && match("MKINIT", line))
210 if (line[0] == '#' && gooddefine(line)) {
213 static const char undef[] = "#undef ";
216 memcpy(line2, undef, sizeof(undef) - 1);
217 cp = line2 + sizeof(undef) - 1;
218 while(*cp && (*cp == ' ' || *cp == '\t'))
220 while(*cp && *cp != ' ' && *cp != '\t' && *cp != '\n')
222 *cp++ = '\n'; *cp = '\0';
223 addstr(line2, &defines);
224 addstr(line, &defines);
232 match(char *name, char *line)
241 if (*q != '{' && *q != ' ' && *q != '\t' && *q != '\n')
248 gooddefine(char *line)
252 if (! match("#define", line))
253 return 0; /* not a define */
255 while (*p == ' ' || *p == '\t')
257 while (*p != ' ' && *p != '\t') {
259 return 0; /* macro definition */
262 while (*p != '\n' && *p != '\0')
265 return 0; /* multi-line definition */
271 doevent(struct event *ep, FILE *fp, char *fname)
277 sprintf(line, "\n /* from %s: */\n", fname);
278 addstr(line, &ep->code);
279 addstr(" {\n", &ep->code);
282 if (fgets(line, sizeof line, fp) == NULL)
283 error("Unexpected EOF");
284 if (equal(line, "}\n"))
287 for (p = line ; *p == '\t' ; p++)
289 for ( ; *p == ' ' ; p++)
291 if (*p == '\n' || *p == '#')
293 while (indent >= 8) {
294 addchar('\t', &ep->code);
298 addchar(' ', &ep->code);
301 addstr(p, &ep->code);
303 addstr(" }\n", &ep->code);
308 doinclude(char *line)
314 for (p = line ; *p != '"' && *p != '<' && *p != '\0' ; p++);
316 error("Expecting '\"' or '<'");
318 while (*p != ' ' && *p != '\t' && *p != '\n')
320 if (p[-1] != '"' && p[-1] != '>')
321 error("Missing terminator");
324 /* name now contains the name of the include file */
325 for (pp = header_files ; *pp && ! equal(*pp, name) ; pp++);
332 dodecl(char *line1, FILE *fp)
337 if (strcmp(line1, "MKINIT\n") == 0) { /* start of struct/union decl */
338 addchar('\n', &decls);
341 if (fgets(line, sizeof line, fp) == NULL)
342 error("Unterminated structure declaration");
343 addstr(line, &decls);
344 } while (line[0] != '}');
348 addchar('\n', &decls);
350 for (p = line1 + 6 ; *p && strchr("=/\n", *p) == NULL; p++)
352 if (*p == '=') { /* eliminate initialization */
353 for (q = p ; *q && *q != ';' ; q++);
362 addstr("extern", &decls);
363 addstr(line1 + 6, &decls);
373 * Write the output to the file OUTTEMP.
383 fp = ckfopen(OUTTEMP, "w");
385 for (pp = header_files ; *pp ; pp++)
386 fprintf(fp, "#include %s\n", *pp);
388 writetext(&defines, fp);
390 writetext(&decls, fp);
391 for (ep = event ; ep->name ; ep++) {
393 fputs(ep->comment, fp);
394 fprintf(fp, "\nvoid\n%s() {\n", ep->routine);
395 writetext(&ep->code, fp);
403 * A text structure is simply a block of text that is kept in memory.
404 * Addstr appends a string to the text struct, and addchar appends a single
409 addstr(char *s, struct text *text)
412 if (--text->nleft < 0)
415 *text->nextc++ = *s++;
421 addchar(int c, struct text *text)
425 if (--text->nleft < 0) {
426 bp = ckmalloc(sizeof *bp);
427 if (text->start == NULL)
430 text->last->next = bp;
432 text->nextc = bp->text;
433 text->nleft = BLOCKSIZE - 1;
439 * Write the contents of a text structure to a file.
442 writetext(struct text *text, FILE *fp)
446 if (text->start != NULL) {
447 for (bp = text->start ; bp != text->last ; bp = bp->next)
448 fwrite(bp->text, sizeof (char), BLOCKSIZE, fp);
449 fwrite(bp->text, sizeof (char), BLOCKSIZE - text->nleft, fp);
454 ckfopen(char *file, char *mode)
458 if ((fp = fopen(file, mode)) == NULL) {
459 fprintf(stderr, "Can't open %s: %s\n", file, strerror(errno));
470 if ((p = malloc(nbytes)) == NULL)
471 error("Out of space");
480 p = ckmalloc(strlen(s) + 1);
489 fprintf(stderr, "%s:%d: ", curfile, linno);
490 fprintf(stderr, "%s\n", msg);