main.c revision 1.21 1 1.21 tnn /* $NetBSD: main.c,v 1.21 2010/02/19 16:35:27 tnn Exp $ */
2 1.9 tls
3 1.1 alm /*-
4 1.5 cgd * Copyright (c) 1992, 1993
5 1.5 cgd * The Regents of the University of California. All rights reserved.
6 1.1 alm *
7 1.1 alm * This code is derived from software contributed to Berkeley by
8 1.1 alm * Diomidis Spinellis of Imperial College, University of London.
9 1.1 alm *
10 1.1 alm * Redistribution and use in source and binary forms, with or without
11 1.1 alm * modification, are permitted provided that the following conditions
12 1.1 alm * are met:
13 1.1 alm * 1. Redistributions of source code must retain the above copyright
14 1.1 alm * notice, this list of conditions and the following disclaimer.
15 1.1 alm * 2. Redistributions in binary form must reproduce the above copyright
16 1.1 alm * notice, this list of conditions and the following disclaimer in the
17 1.1 alm * documentation and/or other materials provided with the distribution.
18 1.14 agc * 3. Neither the name of the University nor the names of its contributors
19 1.14 agc * may be used to endorse or promote products derived from this software
20 1.14 agc * without specific prior written permission.
21 1.14 agc *
22 1.14 agc * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
23 1.14 agc * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
24 1.14 agc * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
25 1.14 agc * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
26 1.14 agc * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
27 1.14 agc * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
28 1.14 agc * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
29 1.14 agc * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
30 1.14 agc * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
31 1.14 agc * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
32 1.14 agc * SUCH DAMAGE.
33 1.14 agc */
34 1.14 agc
35 1.14 agc /*-
36 1.14 agc * Copyright (c) 1992 Diomidis Spinellis.
37 1.14 agc *
38 1.14 agc * This code is derived from software contributed to Berkeley by
39 1.14 agc * Diomidis Spinellis of Imperial College, University of London.
40 1.14 agc *
41 1.14 agc * Redistribution and use in source and binary forms, with or without
42 1.14 agc * modification, are permitted provided that the following conditions
43 1.14 agc * are met:
44 1.14 agc * 1. Redistributions of source code must retain the above copyright
45 1.14 agc * notice, this list of conditions and the following disclaimer.
46 1.14 agc * 2. Redistributions in binary form must reproduce the above copyright
47 1.14 agc * notice, this list of conditions and the following disclaimer in the
48 1.14 agc * documentation and/or other materials provided with the distribution.
49 1.1 alm * 3. All advertising materials mentioning features or use of this software
50 1.1 alm * must display the following acknowledgement:
51 1.1 alm * This product includes software developed by the University of
52 1.1 alm * California, Berkeley and its contributors.
53 1.1 alm * 4. Neither the name of the University nor the names of its contributors
54 1.1 alm * may be used to endorse or promote products derived from this software
55 1.1 alm * without specific prior written permission.
56 1.1 alm *
57 1.1 alm * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
58 1.1 alm * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
59 1.1 alm * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
60 1.1 alm * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
61 1.1 alm * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
62 1.1 alm * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
63 1.1 alm * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
64 1.1 alm * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
65 1.1 alm * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
66 1.1 alm * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
67 1.1 alm * SUCH DAMAGE.
68 1.1 alm */
69 1.1 alm
70 1.17 gdamore #if HAVE_NBTOOL_CONFIG_H
71 1.17 gdamore #include "nbtool_config.h"
72 1.17 gdamore #endif
73 1.17 gdamore
74 1.10 lukem #include <sys/cdefs.h>
75 1.1 alm #ifndef lint
76 1.18 lukem __COPYRIGHT("@(#) Copyright (c) 1992, 1993\
77 1.18 lukem The Regents of the University of California. All rights reserved.");
78 1.1 alm #endif /* not lint */
79 1.1 alm
80 1.1 alm #ifndef lint
81 1.10 lukem #if 0
82 1.10 lukem static char sccsid[] = "@(#)main.c 8.2 (Berkeley) 1/3/94";
83 1.10 lukem #else
84 1.21 tnn __RCSID("$NetBSD: main.c,v 1.21 2010/02/19 16:35:27 tnn Exp $");
85 1.10 lukem #endif
86 1.1 alm #endif /* not lint */
87 1.1 alm
88 1.1 alm #include <sys/types.h>
89 1.1 alm
90 1.1 alm #include <ctype.h>
91 1.1 alm #include <errno.h>
92 1.1 alm #include <fcntl.h>
93 1.21 tnn #include <limits.h>
94 1.1 alm #include <regex.h>
95 1.1 alm #include <stddef.h>
96 1.1 alm #include <stdio.h>
97 1.1 alm #include <stdlib.h>
98 1.1 alm #include <string.h>
99 1.1 alm #include <unistd.h>
100 1.1 alm
101 1.1 alm #include "defs.h"
102 1.1 alm #include "extern.h"
103 1.1 alm
104 1.1 alm /*
105 1.1 alm * Linked list of units (strings and files) to be compiled
106 1.1 alm */
107 1.1 alm struct s_compunit {
108 1.1 alm struct s_compunit *next;
109 1.1 alm enum e_cut {CU_FILE, CU_STRING} type;
110 1.1 alm char *s; /* Pointer to string or fname */
111 1.1 alm };
112 1.1 alm
113 1.1 alm /*
114 1.1 alm * Linked list pointer to compilation units and pointer to current
115 1.1 alm * next pointer.
116 1.1 alm */
117 1.1 alm static struct s_compunit *script, **cu_nextp = &script;
118 1.1 alm
119 1.1 alm /*
120 1.1 alm * Linked list of files to be processed
121 1.1 alm */
122 1.1 alm struct s_flist {
123 1.1 alm char *fname;
124 1.1 alm struct s_flist *next;
125 1.1 alm };
126 1.1 alm
127 1.1 alm /*
128 1.1 alm * Linked list pointer to files and pointer to current
129 1.1 alm * next pointer.
130 1.1 alm */
131 1.1 alm static struct s_flist *files, **fl_nextp = &files;
132 1.1 alm
133 1.11 atatat int aflag, eflag, nflag, ere;
134 1.1 alm
135 1.1 alm /*
136 1.1 alm * Current file and line number; line numbers restart across compilation
137 1.1 alm * units, but span across input files.
138 1.1 alm */
139 1.20 lukem const char *fname; /* File name. */
140 1.1 alm u_long linenum;
141 1.1 alm int lastline; /* TRUE on the last line of the last file */
142 1.1 alm
143 1.13 wiz static void add_compunit(enum e_cut, char *);
144 1.13 wiz static void add_file(char *);
145 1.13 wiz int main(int, char **);
146 1.1 alm
147 1.1 alm int
148 1.13 wiz main(int argc, char *argv[])
149 1.1 alm {
150 1.1 alm int c, fflag;
151 1.1 alm
152 1.15 grant setprogname(*argv);
153 1.1 alm fflag = 0;
154 1.19 perry while ((c = getopt(argc, argv, "ae:f:nrE")) != -1)
155 1.1 alm switch (c) {
156 1.1 alm case 'a':
157 1.1 alm aflag = 1;
158 1.1 alm break;
159 1.1 alm case 'e':
160 1.1 alm eflag = 1;
161 1.1 alm add_compunit(CU_STRING, optarg);
162 1.1 alm break;
163 1.1 alm case 'f':
164 1.1 alm fflag = 1;
165 1.1 alm add_compunit(CU_FILE, optarg);
166 1.1 alm break;
167 1.1 alm case 'n':
168 1.1 alm nflag = 1;
169 1.1 alm break;
170 1.19 perry case 'r':
171 1.11 atatat case 'E':
172 1.11 atatat ere = REG_EXTENDED;
173 1.11 atatat break;
174 1.1 alm default:
175 1.1 alm case '?':
176 1.1 alm (void)fprintf(stderr,
177 1.19 perry "usage:\t%s [-aEnr] script [file ...]\n\t%s [-aEnr] [-e script] ... [-f script_file] ... [file ...]\n",
178 1.15 grant getprogname(), getprogname());
179 1.1 alm exit(1);
180 1.1 alm }
181 1.1 alm argc -= optind;
182 1.1 alm argv += optind;
183 1.1 alm
184 1.1 alm /* First usage case; script is the first arg */
185 1.1 alm if (!eflag && !fflag && *argv) {
186 1.1 alm add_compunit(CU_STRING, *argv);
187 1.1 alm argv++;
188 1.1 alm }
189 1.1 alm
190 1.1 alm compile();
191 1.1 alm
192 1.1 alm /* Continue with first and start second usage */
193 1.1 alm if (*argv)
194 1.1 alm for (; *argv; argv++)
195 1.1 alm add_file(*argv);
196 1.1 alm else
197 1.1 alm add_file(NULL);
198 1.1 alm process();
199 1.1 alm cfclose(prog, NULL);
200 1.1 alm if (fclose(stdout))
201 1.1 alm err(FATAL, "stdout: %s", strerror(errno));
202 1.1 alm exit (0);
203 1.1 alm }
204 1.1 alm
205 1.1 alm /*
206 1.1 alm * Like fgets, but go through the chain of compilation units chaining them
207 1.1 alm * together. Empty strings and files are ignored.
208 1.1 alm */
209 1.1 alm char *
210 1.21 tnn cu_fgets(char **outbuf, size_t *outsize)
211 1.1 alm {
212 1.1 alm static enum {ST_EOF, ST_FILE, ST_STRING} state = ST_EOF;
213 1.1 alm static FILE *f; /* Current open file */
214 1.1 alm static char *s; /* Current pointer inside string */
215 1.1 alm static char string_ident[30];
216 1.21 tnn size_t len;
217 1.1 alm char *p;
218 1.1 alm
219 1.21 tnn if (*outbuf == NULL)
220 1.21 tnn *outsize = 0;
221 1.21 tnn
222 1.1 alm again:
223 1.1 alm switch (state) {
224 1.1 alm case ST_EOF:
225 1.1 alm if (script == NULL)
226 1.1 alm return (NULL);
227 1.1 alm linenum = 0;
228 1.1 alm switch (script->type) {
229 1.1 alm case CU_FILE:
230 1.1 alm if ((f = fopen(script->s, "r")) == NULL)
231 1.1 alm err(FATAL,
232 1.1 alm "%s: %s", script->s, strerror(errno));
233 1.1 alm fname = script->s;
234 1.1 alm state = ST_FILE;
235 1.1 alm goto again;
236 1.1 alm case CU_STRING:
237 1.1 alm if ((snprintf(string_ident,
238 1.1 alm sizeof(string_ident), "\"%s\"", script->s)) >=
239 1.20 lukem (int)(sizeof(string_ident) - 1))
240 1.1 alm (void)strcpy(string_ident +
241 1.1 alm sizeof(string_ident) - 6, " ...\"");
242 1.1 alm fname = string_ident;
243 1.1 alm s = script->s;
244 1.1 alm state = ST_STRING;
245 1.1 alm goto again;
246 1.1 alm }
247 1.1 alm case ST_FILE:
248 1.21 tnn if ((p = fgetln(f, &len)) != NULL) {
249 1.1 alm linenum++;
250 1.21 tnn if (len >= *outsize) {
251 1.21 tnn free(*outbuf);
252 1.21 tnn *outsize = ROUNDLEN(len + 1);
253 1.21 tnn *outbuf = xmalloc(*outsize);
254 1.21 tnn }
255 1.21 tnn memcpy(*outbuf, p, len);
256 1.21 tnn (*outbuf)[len] = '\0';
257 1.21 tnn if (linenum == 1 && p[0] == '#' && p[1] == 'n')
258 1.1 alm nflag = 1;
259 1.21 tnn return (*outbuf);
260 1.1 alm }
261 1.1 alm script = script->next;
262 1.1 alm (void)fclose(f);
263 1.1 alm state = ST_EOF;
264 1.1 alm goto again;
265 1.1 alm case ST_STRING:
266 1.1 alm if (linenum == 0 && s[0] == '#' && s[1] == 'n')
267 1.1 alm nflag = 1;
268 1.21 tnn p = *outbuf;
269 1.21 tnn len = *outsize;
270 1.1 alm for (;;) {
271 1.21 tnn if (len <= 1) {
272 1.21 tnn *outbuf = xrealloc(*outbuf,
273 1.21 tnn *outsize + _POSIX2_LINE_MAX);
274 1.21 tnn p = *outbuf + *outsize - len;
275 1.21 tnn len += _POSIX2_LINE_MAX;
276 1.21 tnn *outsize += _POSIX2_LINE_MAX;
277 1.1 alm }
278 1.1 alm switch (*s) {
279 1.1 alm case '\0':
280 1.1 alm state = ST_EOF;
281 1.1 alm if (s == script->s) {
282 1.1 alm script = script->next;
283 1.1 alm goto again;
284 1.1 alm } else {
285 1.1 alm script = script->next;
286 1.1 alm *p = '\0';
287 1.1 alm linenum++;
288 1.21 tnn return (*outbuf);
289 1.1 alm }
290 1.1 alm case '\n':
291 1.1 alm *p++ = '\n';
292 1.1 alm *p = '\0';
293 1.1 alm s++;
294 1.1 alm linenum++;
295 1.21 tnn return (*outbuf);
296 1.1 alm default:
297 1.1 alm *p++ = *s++;
298 1.21 tnn len--;
299 1.1 alm }
300 1.1 alm }
301 1.1 alm }
302 1.1 alm /* NOTREACHED */
303 1.10 lukem return (NULL);
304 1.1 alm }
305 1.1 alm
306 1.1 alm /*
307 1.1 alm * Like fgets, but go through the list of files chaining them together.
308 1.1 alm * Set len to the length of the line.
309 1.1 alm */
310 1.1 alm int
311 1.13 wiz mf_fgets(SPACE *sp, enum e_spflag spflag)
312 1.1 alm {
313 1.1 alm static FILE *f; /* Current open file */
314 1.1 alm size_t len;
315 1.8 mark char *p;
316 1.8 mark int c;
317 1.1 alm
318 1.1 alm if (f == NULL)
319 1.1 alm /* Advance to first non-empty file */
320 1.1 alm for (;;) {
321 1.1 alm if (files == NULL) {
322 1.1 alm lastline = 1;
323 1.1 alm return (0);
324 1.1 alm }
325 1.1 alm if (files->fname == NULL) {
326 1.1 alm f = stdin;
327 1.1 alm fname = "stdin";
328 1.1 alm } else {
329 1.1 alm fname = files->fname;
330 1.1 alm if ((f = fopen(fname, "r")) == NULL)
331 1.1 alm err(FATAL, "%s: %s",
332 1.1 alm fname, strerror(errno));
333 1.1 alm }
334 1.1 alm if ((c = getc(f)) != EOF) {
335 1.1 alm (void)ungetc(c, f);
336 1.1 alm break;
337 1.1 alm }
338 1.1 alm (void)fclose(f);
339 1.1 alm files = files->next;
340 1.1 alm }
341 1.1 alm
342 1.1 alm if (lastline) {
343 1.1 alm sp->len = 0;
344 1.1 alm return (0);
345 1.1 alm }
346 1.1 alm
347 1.1 alm /*
348 1.5 cgd * Use fgetln so that we can handle essentially infinite input data.
349 1.5 cgd * Can't use the pointer into the stdio buffer as the process space
350 1.5 cgd * because the ungetc() can cause it to move.
351 1.1 alm */
352 1.4 cgd p = fgetln(f, &len);
353 1.1 alm if (ferror(f))
354 1.1 alm err(FATAL, "%s: %s", fname, strerror(errno ? errno : EIO));
355 1.1 alm cspace(sp, p, len, spflag);
356 1.1 alm
357 1.1 alm linenum++;
358 1.1 alm /* Advance to next non-empty file */
359 1.1 alm while ((c = getc(f)) == EOF) {
360 1.1 alm (void)fclose(f);
361 1.1 alm files = files->next;
362 1.1 alm if (files == NULL) {
363 1.1 alm lastline = 1;
364 1.1 alm return (1);
365 1.1 alm }
366 1.1 alm if (files->fname == NULL) {
367 1.1 alm f = stdin;
368 1.1 alm fname = "stdin";
369 1.1 alm } else {
370 1.1 alm fname = files->fname;
371 1.1 alm if ((f = fopen(fname, "r")) == NULL)
372 1.1 alm err(FATAL, "%s: %s", fname, strerror(errno));
373 1.1 alm }
374 1.1 alm }
375 1.1 alm (void)ungetc(c, f);
376 1.1 alm return (1);
377 1.1 alm }
378 1.1 alm
379 1.1 alm /*
380 1.1 alm * Add a compilation unit to the linked list
381 1.1 alm */
382 1.1 alm static void
383 1.13 wiz add_compunit(enum e_cut type, char *s)
384 1.1 alm {
385 1.1 alm struct s_compunit *cu;
386 1.1 alm
387 1.1 alm cu = xmalloc(sizeof(struct s_compunit));
388 1.1 alm cu->type = type;
389 1.1 alm cu->s = s;
390 1.1 alm cu->next = NULL;
391 1.1 alm *cu_nextp = cu;
392 1.1 alm cu_nextp = &cu->next;
393 1.1 alm }
394 1.1 alm
395 1.1 alm /*
396 1.1 alm * Add a file to the linked list
397 1.1 alm */
398 1.1 alm static void
399 1.13 wiz add_file(char *s)
400 1.1 alm {
401 1.1 alm struct s_flist *fp;
402 1.1 alm
403 1.1 alm fp = xmalloc(sizeof(struct s_flist));
404 1.1 alm fp->next = NULL;
405 1.1 alm *fl_nextp = fp;
406 1.1 alm fp->fname = s;
407 1.1 alm fl_nextp = &fp->next;
408 1.1 alm }
409