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
36 * @(#) Copyright (c) 1991, 1993 The Regents of the University of California. All rights reserved.
37 * @(#)mkinit.c 8.2 (Berkeley) 5/4/95
38 * $FreeBSD: src/bin/sh/mkinit.c,v 1.14.2.1 2002/07/19 04:38:51 tjr Exp $
39 * $DragonFly: src/bin/sh/mkinit.c,v 1.2 2003/06/17 04:22:50 dillon Exp $
43 * This program scans all the source files for code to handle various
44 * special events and combines this code into one file. This (allegedly)
45 * improves the structure of the program since there is no need for
46 * anyone outside of a module to know that that module performs special
47 * operations on particular events.
49 * Usage: mkinit sourcefile...
53 #include <sys/cdefs.h>
54 #include <sys/types.h>
64 * OUTFILE is the name of the output file. Output is initially written
65 * to the file OUTTEMP, which is then moved to OUTFILE.
68 #define OUTFILE "init.c"
69 #define OUTTEMP "init.c.new"
73 * A text structure is basicly just a string that grows as more characters
74 * are added onto the end of it. It is implemented as a linked list of
75 * blocks of characters. The routines addstr and addchar append a string
76 * or a single character, respectively, to a text structure. Writetext
77 * writes the contents of a text structure to a file.
96 * There is one event structure for each event that mkinit handles.
100 char *name; /* name of event (e.g. INIT) */
101 char *routine; /* name of routine called on event */
102 char *comment; /* comment describing routine */
103 struct text code; /* code for handling event */
109 * This file was generated by the mkinit program.\n\
115 * Initialization code.\n\
120 * This routine is called when an error or an interrupt occurs in an\n\
121 * interactive shell and control is returned to the main command loop.\n\
124 char shellproc[] = "\
126 * This routine is called to initialize the shell to run a shell procedure.\n\
130 struct event event[] = {
131 {"INIT", "init", init},
132 {"RESET", "reset", reset},
133 {"SHELLPROC", "initshellproc", shellproc},
138 char *curfile; /* current file */
139 int linno; /* current line */
140 char *header_files[200]; /* list of header files */
141 struct text defines; /* #define statements */
142 struct text decls; /* declarations */
143 int amiddecls; /* for formatting */
146 void readfile(char *);
147 int match(char *, char *);
148 int gooddefine(char *);
149 void doevent(struct event *, FILE *, char *);
150 void doinclude(char *);
151 void dodecl(char *, FILE *);
153 void addstr(char *, struct text *);
154 void addchar(int, struct text *);
155 void writetext(struct text *, FILE *);
156 FILE *ckfopen(char *, char *);
158 char *savestr(char *);
161 #define equal(s1, s2) (strcmp(s1, s2) == 0)
164 main(int argc __unused, char *argv[])
168 header_files[0] = "\"shell.h\"";
169 header_files[1] = "\"mystring.h\"";
170 for (ap = argv + 1 ; *ap ; ap++)
173 rename(OUTTEMP, OUTFILE);
179 * Parse an input file.
183 readfile(char *fname)
189 fp = ckfopen(fname, "r");
193 while (fgets(line, sizeof line, fp) != NULL) {
195 for (ep = event ; ep->name ; ep++) {
196 if (line[0] == ep->name[0] && match(ep->name, line)) {
197 doevent(ep, fp, fname);
201 if (line[0] == 'I' && match("INCLUDE", line))
203 if (line[0] == 'M' && match("MKINIT", line))
205 if (line[0] == '#' && gooddefine(line)) {
208 static const char undef[] = "#undef ";
211 memcpy(line2, undef, sizeof(undef) - 1);
212 cp = line2 + sizeof(undef) - 1;
213 while(*cp && (*cp == ' ' || *cp == '\t'))
215 while(*cp && *cp != ' ' && *cp != '\t' && *cp != '\n')
217 *cp++ = '\n'; *cp = '\0';
218 addstr(line2, &defines);
219 addstr(line, &defines);
227 match(char *name, char *line)
236 if (*q != '{' && *q != ' ' && *q != '\t' && *q != '\n')
243 gooddefine(char *line)
247 if (! match("#define", line))
248 return 0; /* not a define */
250 while (*p == ' ' || *p == '\t')
252 while (*p != ' ' && *p != '\t') {
254 return 0; /* macro definition */
257 while (*p != '\n' && *p != '\0')
260 return 0; /* multi-line definition */
266 doevent(struct event *ep, FILE *fp, char *fname)
272 sprintf(line, "\n /* from %s: */\n", fname);
273 addstr(line, &ep->code);
274 addstr(" {\n", &ep->code);
277 if (fgets(line, sizeof line, fp) == NULL)
278 error("Unexpected EOF");
279 if (equal(line, "}\n"))
282 for (p = line ; *p == '\t' ; p++)
284 for ( ; *p == ' ' ; p++)
286 if (*p == '\n' || *p == '#')
288 while (indent >= 8) {
289 addchar('\t', &ep->code);
293 addchar(' ', &ep->code);
296 addstr(p, &ep->code);
298 addstr(" }\n", &ep->code);
303 doinclude(char *line)
309 for (p = line ; *p != '"' && *p != '<' && *p != '\0' ; p++);
311 error("Expecting '\"' or '<'");
313 while (*p != ' ' && *p != '\t' && *p != '\n')
315 if (p[-1] != '"' && p[-1] != '>')
316 error("Missing terminator");
319 /* name now contains the name of the include file */
320 for (pp = header_files ; *pp && ! equal(*pp, name) ; pp++);
327 dodecl(char *line1, FILE *fp)
332 if (strcmp(line1, "MKINIT\n") == 0) { /* start of struct/union decl */
333 addchar('\n', &decls);
336 if (fgets(line, sizeof line, fp) == NULL)
337 error("Unterminated structure declaration");
338 addstr(line, &decls);
339 } while (line[0] != '}');
343 addchar('\n', &decls);
345 for (p = line1 + 6 ; *p && strchr("=/\n", *p) == NULL; p++)
347 if (*p == '=') { /* eliminate initialization */
348 for (q = p ; *q && *q != ';' ; q++);
357 addstr("extern", &decls);
358 addstr(line1 + 6, &decls);
368 * Write the output to the file OUTTEMP.
378 fp = ckfopen(OUTTEMP, "w");
380 for (pp = header_files ; *pp ; pp++)
381 fprintf(fp, "#include %s\n", *pp);
383 writetext(&defines, fp);
385 writetext(&decls, fp);
386 for (ep = event ; ep->name ; ep++) {
388 fputs(ep->comment, fp);
389 fprintf(fp, "\nvoid\n%s() {\n", ep->routine);
390 writetext(&ep->code, fp);
398 * A text structure is simply a block of text that is kept in memory.
399 * Addstr appends a string to the text struct, and addchar appends a single
404 addstr(char *s, struct text *text)
407 if (--text->nleft < 0)
410 *text->nextc++ = *s++;
416 addchar(int c, struct text *text)
420 if (--text->nleft < 0) {
421 bp = ckmalloc(sizeof *bp);
422 if (text->start == NULL)
425 text->last->next = bp;
427 text->nextc = bp->text;
428 text->nleft = BLOCKSIZE - 1;
434 * Write the contents of a text structure to a file.
437 writetext(struct text *text, FILE *fp)
441 if (text->start != NULL) {
442 for (bp = text->start ; bp != text->last ; bp = bp->next)
443 fwrite(bp->text, sizeof (char), BLOCKSIZE, fp);
444 fwrite(bp->text, sizeof (char), BLOCKSIZE - text->nleft, fp);
449 ckfopen(char *file, char *mode)
453 if ((fp = fopen(file, mode)) == NULL) {
454 fprintf(stderr, "Can't open %s: %s\n", file, strerror(errno));
465 if ((p = malloc(nbytes)) == NULL)
466 error("Out of space");
475 p = ckmalloc(strlen(s) + 1);
484 fprintf(stderr, "%s:%d: ", curfile, linno);
485 fprintf(stderr, "%s\n", msg);