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, { NULL, 0, NULL, NULL } },
137 { "RESET", "reset", reset, { NULL, 0, NULL, NULL } },
138 { "SHELLPROC", "initshellproc", shellproc, { NULL, 0, NULL, NULL } },
139 { NULL, NULL, NULL, { NULL, 0, NULL, NULL } }
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 header_files[2] = "\"init.h\"";
176 for (ap = argv + 1 ; *ap ; ap++)
179 rename(OUTTEMP, OUTFILE);
185 * Parse an input file.
189 readfile(char *fname)
195 fp = ckfopen(fname, "r");
199 while (fgets(line, sizeof line, fp) != NULL) {
201 for (ep = event ; ep->name ; ep++) {
202 if (line[0] == ep->name[0] && match(ep->name, line)) {
203 doevent(ep, fp, fname);
207 if (line[0] == 'I' && match("INCLUDE", line))
209 if (line[0] == 'M' && match("MKINIT", line))
211 if (line[0] == '#' && gooddefine(line)) {
214 static const char undef[] = "#undef ";
217 memcpy(line2, undef, sizeof(undef) - 1);
218 cp = line2 + sizeof(undef) - 1;
219 while(*cp && (*cp == ' ' || *cp == '\t'))
221 while(*cp && *cp != ' ' && *cp != '\t' && *cp != '\n')
223 *cp++ = '\n'; *cp = '\0';
224 addstr(line2, &defines);
225 addstr(line, &defines);
233 match(char *name, char *line)
242 if (*q != '{' && *q != ' ' && *q != '\t' && *q != '\n')
249 gooddefine(char *line)
253 if (! match("#define", line))
254 return 0; /* not a define */
256 while (*p == ' ' || *p == '\t')
258 while (*p != ' ' && *p != '\t') {
260 return 0; /* macro definition */
263 while (*p != '\n' && *p != '\0')
266 return 0; /* multi-line definition */
272 doevent(struct event *ep, FILE *fp, char *fname)
278 sprintf(line, "\n /* from %s: */\n", fname);
279 addstr(line, &ep->code);
280 addstr(" {\n", &ep->code);
283 if (fgets(line, sizeof line, fp) == NULL)
284 error("Unexpected EOF");
285 if (equal(line, "}\n"))
288 for (p = line ; *p == '\t' ; p++)
290 for ( ; *p == ' ' ; p++)
292 if (*p == '\n' || *p == '#')
294 while (indent >= 8) {
295 addchar('\t', &ep->code);
299 addchar(' ', &ep->code);
302 addstr(p, &ep->code);
304 addstr(" }\n", &ep->code);
309 doinclude(char *line)
315 for (p = line ; *p != '"' && *p != '<' && *p != '\0' ; p++);
317 error("Expecting '\"' or '<'");
319 while (*p != ' ' && *p != '\t' && *p != '\n')
321 if (p[-1] != '"' && p[-1] != '>')
322 error("Missing terminator");
325 /* name now contains the name of the include file */
326 for (pp = header_files ; *pp && ! equal(*pp, name) ; pp++);
333 dodecl(char *line1, FILE *fp)
338 if (strcmp(line1, "MKINIT\n") == 0) { /* start of struct/union decl */
339 addchar('\n', &decls);
342 if (fgets(line, sizeof line, fp) == NULL)
343 error("Unterminated structure declaration");
344 addstr(line, &decls);
345 } while (line[0] != '}');
349 addchar('\n', &decls);
351 for (p = line1 + 6 ; *p && strchr("=/\n", *p) == NULL; p++)
353 if (*p == '=') { /* eliminate initialization */
354 for (q = p ; *q && *q != ';' ; q++);
363 addstr("extern", &decls);
364 addstr(line1 + 6, &decls);
374 * Write the output to the file OUTTEMP.
384 fp = ckfopen(OUTTEMP, "w");
386 for (pp = header_files ; *pp ; pp++)
387 fprintf(fp, "#include %s\n", *pp);
389 writetext(&defines, fp);
391 writetext(&decls, fp);
392 for (ep = event ; ep->name ; ep++) {
394 fputs(ep->comment, fp);
395 fprintf(fp, "\nvoid\n%s(void)\n{\n", ep->routine);
396 writetext(&ep->code, fp);
404 * A text structure is simply a block of text that is kept in memory.
405 * Addstr appends a string to the text struct, and addchar appends a single
410 addstr(char *s, struct text *text)
413 if (--text->nleft < 0)
416 *text->nextc++ = *s++;
422 addchar(int c, struct text *text)
426 if (--text->nleft < 0) {
427 bp = ckmalloc(sizeof *bp);
428 if (text->start == NULL)
431 text->last->next = bp;
433 text->nextc = bp->text;
434 text->nleft = BLOCKSIZE - 1;
440 * Write the contents of a text structure to a file.
443 writetext(struct text *text, FILE *fp)
447 if (text->start != NULL) {
448 for (bp = text->start ; bp != text->last ; bp = bp->next)
449 fwrite(bp->text, sizeof (char), BLOCKSIZE, fp);
450 fwrite(bp->text, sizeof (char), BLOCKSIZE - text->nleft, fp);
455 ckfopen(char *file, char *mode)
459 if ((fp = fopen(file, mode)) == NULL) {
460 fprintf(stderr, "Can't open %s: %s\n", file, strerror(errno));
471 if ((p = malloc(nbytes)) == NULL)
472 error("Out of space");
481 p = ckmalloc(strlen(s) + 1);
490 fprintf(stderr, "%s:%d: ", curfile, linno);
491 fprintf(stderr, "%s\n", msg);