1 /* $NetBSD: main.c,v 1.21 2010/02/19 16:35:27 tnn 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.21 2010/02/19 16:35:27 tnn Exp $");
88 #include <sys/types.h>
105 * Linked list of units (strings and files) to be compiled
108 struct s_compunit
*next
;
109 enum e_cut
{CU_FILE
, CU_STRING
} type
;
110 char *s
; /* Pointer to string or fname */
114 * Linked list pointer to compilation units and pointer to current
117 static struct s_compunit
*script
, **cu_nextp
= &script
;
120 * Linked list of files to be processed
124 struct s_flist
*next
;
128 * Linked list pointer to files and pointer to current
131 static struct s_flist
*files
, **fl_nextp
= &files
;
133 int aflag
, eflag
, nflag
, ere
;
136 * Current file and line number; line numbers restart across compilation
137 * units, but span across input files.
139 const char *fname
; /* File name. */
141 int lastline
; /* TRUE on the last line of the last file */
143 static void add_compunit(enum e_cut
, char *);
144 static void add_file(char *);
145 int main(int, char **);
148 main(int argc
, char *argv
[])
154 while ((c
= getopt(argc
, argv
, "ae:f:nrE")) != -1)
161 add_compunit(CU_STRING
, optarg
);
165 add_compunit(CU_FILE
, optarg
);
176 (void)fprintf(stderr
,
177 "usage:\t%s [-aEnr] script [file ...]\n\t%s [-aEnr] [-e script] ... [-f script_file] ... [file ...]\n",
178 getprogname(), getprogname());
184 /* First usage case; script is the first arg */
185 if (!eflag
&& !fflag
&& *argv
) {
186 add_compunit(CU_STRING
, *argv
);
192 /* Continue with first and start second usage */
194 for (; *argv
; argv
++)
201 err(FATAL
, "stdout: %s", strerror(errno
));
206 * Like fgets, but go through the chain of compilation units chaining them
207 * together. Empty strings and files are ignored.
210 cu_fgets(char **outbuf
, size_t *outsize
)
212 static enum {ST_EOF
, ST_FILE
, ST_STRING
} state
= ST_EOF
;
213 static FILE *f
; /* Current open file */
214 static char *s
; /* Current pointer inside string */
215 static char string_ident
[30];
228 switch (script
->type
) {
230 if ((f
= fopen(script
->s
, "r")) == NULL
)
232 "%s: %s", script
->s
, strerror(errno
));
237 if ((snprintf(string_ident
,
238 sizeof(string_ident
), "\"%s\"", script
->s
)) >=
239 (int)(sizeof(string_ident
) - 1))
240 (void)strcpy(string_ident
+
241 sizeof(string_ident
) - 6, " ...\"");
242 fname
= string_ident
;
248 if ((p
= fgetln(f
, &len
)) != NULL
) {
250 if (len
>= *outsize
) {
252 *outsize
= ROUNDLEN(len
+ 1);
253 *outbuf
= xmalloc(*outsize
);
255 memcpy(*outbuf
, p
, len
);
256 (*outbuf
)[len
] = '\0';
257 if (linenum
== 1 && p
[0] == '#' && p
[1] == 'n')
261 script
= script
->next
;
266 if (linenum
== 0 && s
[0] == '#' && s
[1] == 'n')
272 *outbuf
= xrealloc(*outbuf
,
273 *outsize
+ _POSIX2_LINE_MAX
);
274 p
= *outbuf
+ *outsize
- len
;
275 len
+= _POSIX2_LINE_MAX
;
276 *outsize
+= _POSIX2_LINE_MAX
;
281 if (s
== script
->s
) {
282 script
= script
->next
;
285 script
= script
->next
;
307 * Like fgets, but go through the list of files chaining them together.
308 * Set len to the length of the line.
311 mf_fgets(SPACE
*sp
, enum e_spflag spflag
)
313 static FILE *f
; /* Current open file */
319 /* Advance to first non-empty file */
325 if (files
->fname
== NULL
) {
329 fname
= files
->fname
;
330 if ((f
= fopen(fname
, "r")) == NULL
)
332 fname
, strerror(errno
));
334 if ((c
= getc(f
)) != EOF
) {
348 * Use fgetln so that we can handle essentially infinite input data.
349 * Can't use the pointer into the stdio buffer as the process space
350 * because the ungetc() can cause it to move.
354 err(FATAL
, "%s: %s", fname
, strerror(errno
? errno
: EIO
));
355 cspace(sp
, p
, len
, spflag
);
358 /* Advance to next non-empty file */
359 while ((c
= getc(f
)) == EOF
) {
366 if (files
->fname
== NULL
) {
370 fname
= files
->fname
;
371 if ((f
= fopen(fname
, "r")) == NULL
)
372 err(FATAL
, "%s: %s", fname
, strerror(errno
));
380 * Add a compilation unit to the linked list
383 add_compunit(enum e_cut type
, char *s
)
385 struct s_compunit
*cu
;
387 cu
= xmalloc(sizeof(struct s_compunit
));
392 cu_nextp
= &cu
->next
;
396 * Add a file to the linked list
403 fp
= xmalloc(sizeof(struct s_flist
));
407 fl_nextp
= &fp
->next
;