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