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/types.h>
68 * OUTFILE is the name of the output file. Output is initially written
69 * to the file OUTTEMP, which is then moved to OUTFILE.
72 #define OUTFILE "init.c"
73 #define OUTTEMP "init.c.new"
77 * A text structure is basically just a string that grows as more characters
78 * are added onto the end of it. It is implemented as a linked list of
79 * blocks of characters. The routines addstr and addchar append a string
80 * or a single character, respectively, to a text structure. Writetext
81 * writes the contents of a text structure to a file.
100 * There is one event structure for each event that mkinit handles.
104 const char *name; /* name of event (e.g. INIT) */
105 const char *routine; /* name of routine called on event */
106 const char *comment; /* comment describing routine */
107 struct text code; /* code for handling event */
113 * This file was generated by the mkinit program.\n\
119 * Initialization code.\n\
124 * This routine is called when an error or an interrupt occurs in an\n\
125 * interactive shell and control is returned to the main command loop.\n\
129 struct event event[] = {
130 { "INIT", "init", init, { NULL, 0, NULL, NULL } },
131 { "RESET", "reset", reset, { NULL, 0, NULL, NULL } },
132 { NULL, NULL, NULL, { NULL, 0, NULL, NULL } }
136 const char *curfile; /* current file */
137 int linno; /* current line */
138 char *header_files[200]; /* list of header files */
139 struct text defines; /* #define statements */
140 struct text decls; /* declarations */
141 int amiddecls; /* for formatting */
144 void readfile(const char *);
145 int match(const char *, const char *);
146 int gooddefine(const char *);
147 void doevent(struct event *, FILE *, const char *);
148 void doinclude(char *);
149 void dodecl(char *, FILE *);
151 void addstr(const char *, struct text *);
152 void addchar(int, struct text *);
153 void writetext(struct text *, FILE *);
154 FILE *ckfopen(const char *, const char *);
155 void *ckmalloc(size_t);
156 char *savestr(const char *);
157 void error(const char *);
159 #define equal(s1, s2) (strcmp(s1, s2) == 0)
162 main(int argc __unused, char *argv[])
166 header_files[0] = savestr("\"shell.h\"");
167 header_files[1] = savestr("\"mystring.h\"");
168 header_files[2] = savestr("\"init.h\"");
169 for (ap = argv + 1 ; *ap ; ap++)
172 rename(OUTTEMP, OUTFILE);
178 * Parse an input file.
182 readfile(const char *fname)
188 fp = ckfopen(fname, "r");
192 while (fgets(line, sizeof line, fp) != NULL) {
194 for (ep = event ; ep->name ; ep++) {
195 if (line[0] == ep->name[0] && match(ep->name, line)) {
196 doevent(ep, fp, fname);
200 if (line[0] == 'I' && match("INCLUDE", line))
202 if (line[0] == 'M' && match("MKINIT", line))
204 if (line[0] == '#' && gooddefine(line)) {
207 static const char undef[] = "#undef ";
210 memcpy(line2, undef, sizeof(undef) - 1);
211 cp = line2 + sizeof(undef) - 1;
212 while(*cp && (*cp == ' ' || *cp == '\t'))
214 while(*cp && *cp != ' ' && *cp != '\t' && *cp != '\n')
216 *cp++ = '\n'; *cp = '\0';
217 addstr(line2, &defines);
218 addstr(line, &defines);
226 match(const char *name, const char *line)
235 if (*q != '{' && *q != ' ' && *q != '\t' && *q != '\n')
242 gooddefine(const char *line)
246 if (! match("#define", line))
247 return 0; /* not a define */
249 while (*p == ' ' || *p == '\t')
251 while (*p != ' ' && *p != '\t') {
253 return 0; /* macro definition */
256 while (*p != '\n' && *p != '\0')
259 return 0; /* multi-line definition */
265 doevent(struct event *ep, FILE *fp, const char *fname)
271 sprintf(line, "\n /* from %s: */\n", fname);
272 addstr(line, &ep->code);
273 addstr(" {\n", &ep->code);
276 if (fgets(line, sizeof line, fp) == NULL)
277 error("Unexpected EOF");
278 if (equal(line, "}\n"))
281 for (p = line ; *p == '\t' ; p++)
283 for ( ; *p == ' ' ; p++)
285 if (*p == '\n' || *p == '#')
287 while (indent >= 8) {
288 addchar('\t', &ep->code);
292 addchar(' ', &ep->code);
295 addstr(p, &ep->code);
297 addstr(" }\n", &ep->code);
302 doinclude(char *line)
308 for (p = line ; *p != '"' && *p != '<' && *p != '\0' ; p++);
310 error("Expecting '\"' or '<'");
312 while (*p != ' ' && *p != '\t' && *p != '\n')
314 if (p[-1] != '"' && p[-1] != '>')
315 error("Missing terminator");
318 /* name now contains the name of the include file */
319 for (pp = header_files ; *pp && ! equal(*pp, name) ; pp++);
326 dodecl(char *line1, FILE *fp)
331 if (strcmp(line1, "MKINIT\n") == 0) { /* start of struct/union decl */
332 addchar('\n', &decls);
335 if (fgets(line, sizeof line, fp) == NULL)
336 error("Unterminated structure declaration");
337 addstr(line, &decls);
338 } while (line[0] != '}');
342 addchar('\n', &decls);
344 for (p = line1 + 6 ; *p && strchr("=/\n", *p) == NULL; p++)
346 if (*p == '=') { /* eliminate initialization */
347 for (q = p ; *q && *q != ';' ; q++);
356 addstr("extern", &decls);
357 addstr(line1 + 6, &decls);
367 * Write the output to the file OUTTEMP.
377 fp = ckfopen(OUTTEMP, "w");
379 for (pp = header_files ; *pp ; pp++)
380 fprintf(fp, "#include %s\n", *pp);
382 writetext(&defines, fp);
384 writetext(&decls, fp);
385 for (ep = event ; ep->name ; ep++) {
387 fputs(ep->comment, fp);
388 fprintf(fp, "\nvoid\n%s(void)\n{\n", ep->routine);
389 writetext(&ep->code, fp);
397 * A text structure is simply a block of text that is kept in memory.
398 * Addstr appends a string to the text struct, and addchar appends a single
403 addstr(const char *s, struct text *text)
406 if (--text->nleft < 0)
409 *text->nextc++ = *s++;
415 addchar(int c, struct text *text)
419 if (--text->nleft < 0) {
420 bp = ckmalloc(sizeof *bp);
421 if (text->start == NULL)
424 text->last->next = bp;
426 text->nextc = bp->text;
427 text->nleft = BLOCKSIZE - 1;
433 * Write the contents of a text structure to a file.
436 writetext(struct text *text, FILE *fp)
440 if (text->start != NULL) {
441 for (bp = text->start ; bp != text->last ; bp = bp->next)
442 fwrite(bp->text, sizeof (char), BLOCKSIZE, fp);
443 fwrite(bp->text, sizeof (char), BLOCKSIZE - text->nleft, fp);
448 ckfopen(const char *file, const char *mode)
452 if ((fp = fopen(file, mode)) == NULL) {
453 fprintf(stderr, "Can't open %s: %s\n", file, strerror(errno));
460 ckmalloc(size_t nbytes)
464 if ((p = malloc(nbytes)) == NULL)
465 error("Out of space");
470 savestr(const char *s)
474 p = ckmalloc(strlen(s) + 1);
480 error(const char *msg)
483 fprintf(stderr, "%s:%d: ", curfile, linno);
484 fprintf(stderr, "%s\n", msg);