1 /* $NetBSD: main.c,v 1.19 2008/09/16 13:32:04 perry Exp $ */
4 * Copyright (c) 1992, 1993
5 * The Regents of the University of California. All rights reserved.
7 * This code is derived from software contributed to Berkeley by
8 * Diomidis Spinellis of Imperial College, University of London.
10 * Redistribution and use in source and binary forms, with or without
11 * modification, are permitted provided that the following conditions
13 * 1. Redistributions of source code must retain the above copyright
14 * notice, this list of conditions and the following disclaimer.
15 * 2. Redistributions in binary form must reproduce the above copyright
16 * notice, this list of conditions and the following disclaimer in the
17 * documentation and/or other materials provided with the distribution.
18 * 3. Neither the name of the University nor the names of its contributors
19 * may be used to endorse or promote products derived from this software
20 * without specific prior written permission.
22 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
23 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
24 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
25 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
26 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
27 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
28 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
29 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
30 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
31 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
36 * Copyright (c) 1992 Diomidis Spinellis.
38 * This code is derived from software contributed to Berkeley by
39 * Diomidis Spinellis of Imperial College, University of London.
41 * Redistribution and use in source and binary forms, with or without
42 * modification, are permitted provided that the following conditions
44 * 1. Redistributions of source code must retain the above copyright
45 * notice, this list of conditions and the following disclaimer.
46 * 2. Redistributions in binary form must reproduce the above copyright
47 * notice, this list of conditions and the following disclaimer in the
48 * documentation and/or other materials provided with the distribution.
49 * 3. All advertising materials mentioning features or use of this software
50 * must display the following acknowledgement:
51 * This product includes software developed by the University of
52 * California, Berkeley and its contributors.
53 * 4. Neither the name of the University nor the names of its contributors
54 * may be used to endorse or promote products derived from this software
55 * without specific prior written permission.
57 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
58 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
59 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
60 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
61 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
62 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
63 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
64 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
65 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
66 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
70 #if HAVE_NBTOOL_CONFIG_H
71 #include "nbtool_config.h"
74 #include <sys/cdefs.h>
76 __COPYRIGHT("@(#) Copyright (c) 1992, 1993\
77 The Regents of the University of California. All rights reserved.");
82 static char sccsid
[] = "@(#)main.c 8.2 (Berkeley) 1/3/94";
84 __RCSID("$NetBSD: main.c,v 1.19 2008/09/16 13:32:04 perry Exp $");
88 #include <sys/types.h>
104 * Linked list of units (strings and files) to be compiled
107 struct s_compunit
*next
;
108 enum e_cut
{CU_FILE
, CU_STRING
} type
;
109 char *s
; /* Pointer to string or fname */
113 * Linked list pointer to compilation units and pointer to current
116 static struct s_compunit
*script
, **cu_nextp
= &script
;
119 * Linked list of files to be processed
123 struct s_flist
*next
;
127 * Linked list pointer to files and pointer to current
130 static struct s_flist
*files
, **fl_nextp
= &files
;
132 int aflag
, eflag
, nflag
, ere
;
135 * Current file and line number; line numbers restart across compilation
136 * units, but span across input files.
138 const char *fname
; /* File name. */
140 int lastline
; /* TRUE on the last line of the last file */
142 static void add_compunit(enum e_cut
, char *);
143 static void add_file(char *);
144 int main(int, char **);
147 main(int argc
, char *argv
[])
153 while ((c
= getopt(argc
, argv
, "ae:f:nrE")) != -1)
160 add_compunit(CU_STRING
, optarg
);
164 add_compunit(CU_FILE
, optarg
);
175 (void)fprintf(stderr
,
176 "usage:\t%s [-aEnr] script [file ...]\n\t%s [-aEnr] [-e script] ... [-f script_file] ... [file ...]\n",
177 getprogname(), getprogname());
183 /* First usage case; script is the first arg */
184 if (!eflag
&& !fflag
&& *argv
) {
185 add_compunit(CU_STRING
, *argv
);
191 /* Continue with first and start second usage */
193 for (; *argv
; argv
++)
200 err(FATAL
, "stdout: %s", strerror(errno
));
205 * Like fgets, but go through the chain of compilation units chaining them
206 * together. Empty strings and files are ignored.
209 cu_fgets(char *buf
, int n
)
211 static enum {ST_EOF
, ST_FILE
, ST_STRING
} state
= ST_EOF
;
212 static FILE *f
; /* Current open file */
213 static char *s
; /* Current pointer inside string */
214 static char string_ident
[30];
223 switch (script
->type
) {
225 if ((f
= fopen(script
->s
, "r")) == NULL
)
227 "%s: %s", script
->s
, strerror(errno
));
232 if ((snprintf(string_ident
,
233 sizeof(string_ident
), "\"%s\"", script
->s
)) >=
234 (int)(sizeof(string_ident
) - 1))
235 (void)strcpy(string_ident
+
236 sizeof(string_ident
) - 6, " ...\"");
237 fname
= string_ident
;
243 if ((p
= fgets(buf
, n
, f
)) != NULL
) {
245 if (linenum
== 1 && buf
[0] == '#' && buf
[1] == 'n')
249 script
= script
->next
;
254 if (linenum
== 0 && s
[0] == '#' && s
[1] == 'n')
266 if (s
== script
->s
) {
267 script
= script
->next
;
270 script
= script
->next
;
291 * Like fgets, but go through the list of files chaining them together.
292 * Set len to the length of the line.
295 mf_fgets(SPACE
*sp
, enum e_spflag spflag
)
297 static FILE *f
; /* Current open file */
303 /* Advance to first non-empty file */
309 if (files
->fname
== NULL
) {
313 fname
= files
->fname
;
314 if ((f
= fopen(fname
, "r")) == NULL
)
316 fname
, strerror(errno
));
318 if ((c
= getc(f
)) != EOF
) {
332 * Use fgetln so that we can handle essentially infinite input data.
333 * Can't use the pointer into the stdio buffer as the process space
334 * because the ungetc() can cause it to move.
338 err(FATAL
, "%s: %s", fname
, strerror(errno
? errno
: EIO
));
339 cspace(sp
, p
, len
, spflag
);
342 /* Advance to next non-empty file */
343 while ((c
= getc(f
)) == EOF
) {
350 if (files
->fname
== NULL
) {
354 fname
= files
->fname
;
355 if ((f
= fopen(fname
, "r")) == NULL
)
356 err(FATAL
, "%s: %s", fname
, strerror(errno
));
364 * Add a compilation unit to the linked list
367 add_compunit(enum e_cut type
, char *s
)
369 struct s_compunit
*cu
;
371 cu
= xmalloc(sizeof(struct s_compunit
));
376 cu_nextp
= &cu
->next
;
380 * Add a file to the linked list
387 fp
= xmalloc(sizeof(struct s_flist
));
391 fl_nextp
= &fp
->next
;