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
32 * @(#) Copyright (c) 1991, 1993 The Regents of the University of California. All rights reserved.
33 * @(#)mkinit.c 8.2 (Berkeley) 5/4/95
34 * $FreeBSD: head/bin/sh/mkinit.c 245689 2013-01-20 12:44:50Z jilles $
38 * This program scans all the source files for code to handle various
39 * special events and combines this code into one file. This (allegedly)
40 * improves the structure of the program since there is no need for
41 * anyone outside of a module to know that that module performs special
42 * operations on particular events.
44 * Usage: mkinit sourcefile...
48 #include <sys/types.h>
58 * OUTFILE is the name of the output file. Output is initially written
59 * to the file OUTTEMP, which is then moved to OUTFILE.
62 #define OUTFILE "init.c"
63 #define OUTTEMP "init.c.new"
67 * A text structure is basically just a string that grows as more characters
68 * are added onto the end of it. It is implemented as a linked list of
69 * blocks of characters. The routines addstr and addchar append a string
70 * or a single character, respectively, to a text structure. Writetext
71 * writes the contents of a text structure to a file.
90 * There is one event structure for each event that mkinit handles.
94 const char *name; /* name of event (e.g. RESET) */
95 const char *routine; /* name of routine called on event */
96 const char *comment; /* comment describing routine */
97 struct text code; /* code for handling event */
103 * This file was generated by the mkinit program.\n\
109 * This routine is called when an error or an interrupt occurs in an\n\
110 * interactive shell and control is returned to the main command loop.\n\
114 struct event event[] = {
115 { "RESET", "reset", reset, { NULL, 0, NULL, NULL } },
116 { NULL, NULL, NULL, { NULL, 0, NULL, NULL } }
120 const char *curfile; /* current file */
121 int linno; /* current line */
122 const char *header_files[200]; /* list of header files */
123 struct text defines; /* #define statements */
124 struct text decls; /* declarations */
125 int amiddecls; /* for formatting */
128 void readfile(const char *);
129 int match(const char *, const char *);
130 int gooddefine(const char *);
131 void doevent(struct event *, FILE *, const char *);
132 void doinclude(char *);
133 void dodecl(char *, FILE *);
135 void addstr(const char *, struct text *);
136 void addchar(int, struct text *);
137 void writetext(struct text *, FILE *);
138 FILE *ckfopen(const char *, const char *);
139 void *ckmalloc(size_t);
140 char *savestr(const char *);
141 void error(const char *);
143 #define equal(s1, s2) (strcmp(s1, s2) == 0)
146 main(int argc __unused, char *argv[])
150 header_files[0] = savestr("\"shell.h\"");
151 header_files[1] = savestr("\"mystring.h\"");
152 header_files[2] = savestr("\"init.h\"");
153 for (ap = argv + 1 ; *ap ; ap++)
156 rename(OUTTEMP, OUTFILE);
162 * Parse an input file.
166 readfile(const char *fname)
172 fp = ckfopen(fname, "r");
176 while (fgets(line, sizeof line, fp) != NULL) {
178 for (ep = event ; ep->name ; ep++) {
179 if (line[0] == ep->name[0] && match(ep->name, line)) {
180 doevent(ep, fp, fname);
184 if (line[0] == 'I' && match("INCLUDE", line))
186 if (line[0] == 'M' && match("MKINIT", line))
188 if (line[0] == '#' && gooddefine(line)) {
191 static const char undef[] = "#undef ";
194 memcpy(line2, undef, sizeof(undef) - 1);
195 cp = line2 + sizeof(undef) - 1;
196 while(*cp && (*cp == ' ' || *cp == '\t'))
198 while(*cp && *cp != ' ' && *cp != '\t' && *cp != '\n')
200 *cp++ = '\n'; *cp = '\0';
201 addstr(line2, &defines);
202 addstr(line, &defines);
210 match(const char *name, const char *line)
219 if (*q != '{' && *q != ' ' && *q != '\t' && *q != '\n')
226 gooddefine(const char *line)
230 if (! match("#define", line))
231 return 0; /* not a define */
233 while (*p == ' ' || *p == '\t')
235 while (*p != ' ' && *p != '\t') {
237 return 0; /* macro definition */
240 while (*p != '\n' && *p != '\0')
243 return 0; /* multi-line definition */
249 doevent(struct event *ep, FILE *fp, const char *fname)
255 sprintf(line, "\n /* from %s: */\n", fname);
256 addstr(line, &ep->code);
257 addstr(" {\n", &ep->code);
260 if (fgets(line, sizeof line, fp) == NULL)
261 error("Unexpected EOF");
262 if (equal(line, "}\n"))
265 for (p = line ; *p == '\t' ; p++)
267 for ( ; *p == ' ' ; p++)
269 if (*p == '\n' || *p == '#')
271 while (indent >= 8) {
272 addchar('\t', &ep->code);
276 addchar(' ', &ep->code);
279 addstr(p, &ep->code);
281 addstr(" }\n", &ep->code);
286 doinclude(char *line)
292 for (p = line ; *p != '"' && *p != '<' && *p != '\0' ; p++);
294 error("Expecting '\"' or '<'");
296 while (*p != ' ' && *p != '\t' && *p != '\n')
298 if (p[-1] != '"' && p[-1] != '>')
299 error("Missing terminator");
302 /* name now contains the name of the include file */
303 for (pp = header_files ; *pp && ! equal(*pp, name) ; pp++);
310 dodecl(char *line1, FILE *fp)
315 if (strcmp(line1, "MKINIT\n") == 0) { /* start of struct/union decl */
316 addchar('\n', &decls);
319 if (fgets(line, sizeof line, fp) == NULL)
320 error("Unterminated structure declaration");
321 addstr(line, &decls);
322 } while (line[0] != '}');
326 addchar('\n', &decls);
328 for (p = line1 + 6 ; *p && strchr("=/\n", *p) == NULL; p++)
330 if (*p == '=') { /* eliminate initialization */
331 for (q = p ; *q && *q != ';' ; q++);
340 addstr("extern", &decls);
341 addstr(line1 + 6, &decls);
351 * Write the output to the file OUTTEMP.
358 const char * const *pp;
361 fp = ckfopen(OUTTEMP, "w");
363 for (pp = header_files ; *pp ; pp++)
364 fprintf(fp, "#include %s\n", *pp);
366 writetext(&defines, fp);
368 writetext(&decls, fp);
369 for (ep = event ; ep->name ; ep++) {
371 fputs(ep->comment, fp);
372 fprintf(fp, "\nvoid\n%s(void)\n{\n", ep->routine);
373 writetext(&ep->code, fp);
381 * A text structure is simply a block of text that is kept in memory.
382 * Addstr appends a string to the text struct, and addchar appends a single
387 addstr(const char *s, struct text *text)
390 if (--text->nleft < 0)
393 *text->nextc++ = *s++;
399 addchar(int c, struct text *text)
403 if (--text->nleft < 0) {
404 bp = ckmalloc(sizeof *bp);
405 if (text->start == NULL)
408 text->last->next = bp;
410 text->nextc = bp->text;
411 text->nleft = BLOCKSIZE - 1;
417 * Write the contents of a text structure to a file.
420 writetext(struct text *text, FILE *fp)
424 if (text->start != NULL) {
425 for (bp = text->start ; bp != text->last ; bp = bp->next)
426 fwrite(bp->text, sizeof (char), BLOCKSIZE, fp);
427 fwrite(bp->text, sizeof (char), BLOCKSIZE - text->nleft, fp);
432 ckfopen(const char *file, const char *mode)
436 if ((fp = fopen(file, mode)) == NULL) {
437 fprintf(stderr, "Can't open %s: %s\n", file, strerror(errno));
444 ckmalloc(size_t nbytes)
448 if ((p = malloc(nbytes)) == NULL)
449 error("Out of space");
454 savestr(const char *s)
458 p = ckmalloc(strlen(s) + 1);
464 error(const char *msg)
467 fprintf(stderr, "%s:%d: ", curfile, linno);
468 fprintf(stderr, "%s\n", msg);