]> CyberLeo.Net >> Repos - FreeBSD/FreeBSD.git/blob - bin/sh/mkinit.c
This commit was generated by cvs2svn to compensate for changes in r146899,
[FreeBSD/FreeBSD.git] / bin / sh / mkinit.c
1 /*-
2  * Copyright (c) 1991, 1993
3  *      The Regents of the University of California.  All rights reserved.
4  *
5  * This code is derived from software contributed to Berkeley by
6  * Kenneth Almquist.
7  *
8  * Redistribution and use in source and binary forms, with or without
9  * modification, are permitted provided that the following conditions
10  * are met:
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.
19  *
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
30  * SUCH DAMAGE.
31  */
32
33 #ifndef lint
34 static char const copyright[] =
35 "@(#) Copyright (c) 1991, 1993\n\
36         The Regents of the University of California.  All rights reserved.\n";
37 #endif /* not lint */
38
39 #ifndef lint
40 #if 0
41 static char sccsid[] = "@(#)mkinit.c    8.2 (Berkeley) 5/4/95";
42 #endif
43 #endif /* not lint */
44 #include <sys/cdefs.h>
45 __FBSDID("$FreeBSD$");
46
47 /*
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.
53  *
54  * Usage:  mkinit sourcefile...
55  */
56
57
58 #include <sys/cdefs.h>
59 #include <sys/types.h>
60 #include <stdio.h>
61 #include <stdlib.h>
62 #include <string.h>
63 #include <fcntl.h>
64 #include <unistd.h>
65 #include <errno.h>
66
67
68 /*
69  * OUTFILE is the name of the output file.  Output is initially written
70  * to the file OUTTEMP, which is then moved to OUTFILE.
71  */
72
73 #define OUTFILE "init.c"
74 #define OUTTEMP "init.c.new"
75
76
77 /*
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.
83  */
84
85 #define BLOCKSIZE 512
86
87 struct text {
88         char *nextc;
89         int nleft;
90         struct block *start;
91         struct block *last;
92 };
93
94 struct block {
95         struct block *next;
96         char text[BLOCKSIZE];
97 };
98
99
100 /*
101  * There is one event structure for each event that mkinit handles.
102  */
103
104 struct event {
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 */
109 };
110
111
112 char writer[] = "\
113 /*\n\
114  * This file was generated by the mkinit program.\n\
115  */\n\
116 \n";
117
118 char init[] = "\
119 /*\n\
120  * Initialization code.\n\
121  */\n";
122
123 char reset[] = "\
124 /*\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\
127  */\n";
128
129 char shellproc[] = "\
130 /*\n\
131  * This routine is called to initialize the shell to run a shell procedure.\n\
132  */\n";
133
134
135 struct event event[] = {
136         {"INIT", "init", init},
137         {"RESET", "reset", reset},
138         {"SHELLPROC", "initshellproc", shellproc},
139         {NULL, NULL}
140 };
141
142
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 */
149
150
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 *);
157 void output(void);
158 void addstr(char *, struct text *);
159 void addchar(int, struct text *);
160 void writetext(struct text *, FILE *);
161 FILE *ckfopen(char *, char *);
162 void *ckmalloc(int);
163 char *savestr(char *);
164 void error(char *);
165
166 #define equal(s1, s2)   (strcmp(s1, s2) == 0)
167
168 int
169 main(int argc __unused, char *argv[])
170 {
171         char **ap;
172
173         header_files[0] = "\"shell.h\"";
174         header_files[1] = "\"mystring.h\"";
175         for (ap = argv + 1 ; *ap ; ap++)
176                 readfile(*ap);
177         output();
178         rename(OUTTEMP, OUTFILE);
179         exit(0);
180 }
181
182
183 /*
184  * Parse an input file.
185  */
186
187 void
188 readfile(char *fname)
189 {
190         FILE *fp;
191         char line[1024];
192         struct event *ep;
193
194         fp = ckfopen(fname, "r");
195         curfile = fname;
196         linno = 0;
197         amiddecls = 0;
198         while (fgets(line, sizeof line, fp) != NULL) {
199                 linno++;
200                 for (ep = event ; ep->name ; ep++) {
201                         if (line[0] == ep->name[0] && match(ep->name, line)) {
202                                 doevent(ep, fp, fname);
203                                 break;
204                         }
205                 }
206                 if (line[0] == 'I' && match("INCLUDE", line))
207                         doinclude(line);
208                 if (line[0] == 'M' && match("MKINIT", line))
209                         dodecl(line, fp);
210                 if (line[0] == '#' && gooddefine(line)) {
211                         char *cp;
212                         char line2[1024];
213                         static const char undef[] = "#undef ";
214
215                         strcpy(line2, line);
216                         memcpy(line2, undef, sizeof(undef) - 1);
217                         cp = line2 + sizeof(undef) - 1;
218                         while(*cp && (*cp == ' ' || *cp == '\t'))
219                                 cp++;
220                         while(*cp && *cp != ' ' && *cp != '\t' && *cp != '\n')
221                                 cp++;
222                         *cp++ = '\n'; *cp = '\0';
223                         addstr(line2, &defines);
224                         addstr(line, &defines);
225                 }
226         }
227         fclose(fp);
228 }
229
230
231 int
232 match(char *name, char *line)
233 {
234         char *p, *q;
235
236         p = name, q = line;
237         while (*p) {
238                 if (*p++ != *q++)
239                         return 0;
240         }
241         if (*q != '{' && *q != ' ' && *q != '\t' && *q != '\n')
242                 return 0;
243         return 1;
244 }
245
246
247 int
248 gooddefine(char *line)
249 {
250         char *p;
251
252         if (! match("#define", line))
253                 return 0;                       /* not a define */
254         p = line + 7;
255         while (*p == ' ' || *p == '\t')
256                 p++;
257         while (*p != ' ' && *p != '\t') {
258                 if (*p == '(')
259                         return 0;               /* macro definition */
260                 p++;
261         }
262         while (*p != '\n' && *p != '\0')
263                 p++;
264         if (p[-1] == '\\')
265                 return 0;                       /* multi-line definition */
266         return 1;
267 }
268
269
270 void
271 doevent(struct event *ep, FILE *fp, char *fname)
272 {
273         char line[1024];
274         int indent;
275         char *p;
276
277         sprintf(line, "\n      /* from %s: */\n", fname);
278         addstr(line, &ep->code);
279         addstr("      {\n", &ep->code);
280         for (;;) {
281                 linno++;
282                 if (fgets(line, sizeof line, fp) == NULL)
283                         error("Unexpected EOF");
284                 if (equal(line, "}\n"))
285                         break;
286                 indent = 6;
287                 for (p = line ; *p == '\t' ; p++)
288                         indent += 8;
289                 for ( ; *p == ' ' ; p++)
290                         indent++;
291                 if (*p == '\n' || *p == '#')
292                         indent = 0;
293                 while (indent >= 8) {
294                         addchar('\t', &ep->code);
295                         indent -= 8;
296                 }
297                 while (indent > 0) {
298                         addchar(' ', &ep->code);
299                         indent--;
300                 }
301                 addstr(p, &ep->code);
302         }
303         addstr("      }\n", &ep->code);
304 }
305
306
307 void
308 doinclude(char *line)
309 {
310         char *p;
311         char *name;
312         char **pp;
313
314         for (p = line ; *p != '"' && *p != '<' && *p != '\0' ; p++);
315         if (*p == '\0')
316                 error("Expecting '\"' or '<'");
317         name = p;
318         while (*p != ' ' && *p != '\t' && *p != '\n')
319                 p++;
320         if (p[-1] != '"' && p[-1] != '>')
321                 error("Missing terminator");
322         *p = '\0';
323
324         /* name now contains the name of the include file */
325         for (pp = header_files ; *pp && ! equal(*pp, name) ; pp++);
326         if (*pp == NULL)
327                 *pp = savestr(name);
328 }
329
330
331 void
332 dodecl(char *line1, FILE *fp)
333 {
334         char line[1024];
335         char *p, *q;
336
337         if (strcmp(line1, "MKINIT\n") == 0) { /* start of struct/union decl */
338                 addchar('\n', &decls);
339                 do {
340                         linno++;
341                         if (fgets(line, sizeof line, fp) == NULL)
342                                 error("Unterminated structure declaration");
343                         addstr(line, &decls);
344                 } while (line[0] != '}');
345                 amiddecls = 0;
346         } else {
347                 if (! amiddecls)
348                         addchar('\n', &decls);
349                 q = NULL;
350                 for (p = line1 + 6 ; *p && strchr("=/\n", *p) == NULL; p++)
351                         continue;
352                 if (*p == '=') {                /* eliminate initialization */
353                         for (q = p ; *q && *q != ';' ; q++);
354                         if (*q == '\0')
355                                 q = NULL;
356                         else {
357                                 while (p[-1] == ' ')
358                                         p--;
359                                 *p = '\0';
360                         }
361                 }
362                 addstr("extern", &decls);
363                 addstr(line1 + 6, &decls);
364                 if (q != NULL)
365                         addstr(q, &decls);
366                 amiddecls = 1;
367         }
368 }
369
370
371
372 /*
373  * Write the output to the file OUTTEMP.
374  */
375
376 void
377 output(void)
378 {
379         FILE *fp;
380         char **pp;
381         struct event *ep;
382
383         fp = ckfopen(OUTTEMP, "w");
384         fputs(writer, fp);
385         for (pp = header_files ; *pp ; pp++)
386                 fprintf(fp, "#include %s\n", *pp);
387         fputs("\n\n\n", fp);
388         writetext(&defines, fp);
389         fputs("\n\n", fp);
390         writetext(&decls, fp);
391         for (ep = event ; ep->name ; ep++) {
392                 fputs("\n\n\n", fp);
393                 fputs(ep->comment, fp);
394                 fprintf(fp, "\nvoid\n%s() {\n", ep->routine);
395                 writetext(&ep->code, fp);
396                 fprintf(fp, "}\n");
397         }
398         fclose(fp);
399 }
400
401
402 /*
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
405  * character.
406  */
407
408 void
409 addstr(char *s, struct text *text)
410 {
411         while (*s) {
412                 if (--text->nleft < 0)
413                         addchar(*s++, text);
414                 else
415                         *text->nextc++ = *s++;
416         }
417 }
418
419
420 void
421 addchar(int c, struct text *text)
422 {
423         struct block *bp;
424
425         if (--text->nleft < 0) {
426                 bp = ckmalloc(sizeof *bp);
427                 if (text->start == NULL)
428                         text->start = bp;
429                 else
430                         text->last->next = bp;
431                 text->last = bp;
432                 text->nextc = bp->text;
433                 text->nleft = BLOCKSIZE - 1;
434         }
435         *text->nextc++ = c;
436 }
437
438 /*
439  * Write the contents of a text structure to a file.
440  */
441 void
442 writetext(struct text *text, FILE *fp)
443 {
444         struct block *bp;
445
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);
450         }
451 }
452
453 FILE *
454 ckfopen(char *file, char *mode)
455 {
456         FILE *fp;
457
458         if ((fp = fopen(file, mode)) == NULL) {
459                 fprintf(stderr, "Can't open %s: %s\n", file, strerror(errno));
460                 exit(2);
461         }
462         return fp;
463 }
464
465 void *
466 ckmalloc(int nbytes)
467 {
468         char *p;
469
470         if ((p = malloc(nbytes)) == NULL)
471                 error("Out of space");
472         return p;
473 }
474
475 char *
476 savestr(char *s)
477 {
478         char *p;
479
480         p = ckmalloc(strlen(s) + 1);
481         strcpy(p, s);
482         return p;
483 }
484
485 void
486 error(char *msg)
487 {
488         if (curfile != NULL)
489                 fprintf(stderr, "%s:%d: ", curfile, linno);
490         fprintf(stderr, "%s\n", msg);
491         exit(2);
492 }