main.c revision 1.10 1 /* $NetBSD: main.c,v 1.10 1997/10/19 23:05:14 lukem 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.10 1997/10/19 23:05:14 lukem 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;
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 __P((enum e_cut, char *));
109 static void add_file __P((char *));
110 int main __P((int, char **));
111
112 int
113 main(argc, argv)
114 int argc;
115 char *argv[];
116 {
117 int c, fflag;
118
119 fflag = 0;
120 while ((c = getopt(argc, argv, "ae:f:n")) != -1)
121 switch (c) {
122 case 'a':
123 aflag = 1;
124 break;
125 case 'e':
126 eflag = 1;
127 add_compunit(CU_STRING, optarg);
128 break;
129 case 'f':
130 fflag = 1;
131 add_compunit(CU_FILE, optarg);
132 break;
133 case 'n':
134 nflag = 1;
135 break;
136 default:
137 case '?':
138 (void)fprintf(stderr,
139 "usage:\tsed script [-an] [file ...]\n\tsed [-an] [-e script] ... [-f script_file] ... [file ...]\n");
140 exit(1);
141 }
142 argc -= optind;
143 argv += optind;
144
145 /* First usage case; script is the first arg */
146 if (!eflag && !fflag && *argv) {
147 add_compunit(CU_STRING, *argv);
148 argv++;
149 }
150
151 compile();
152
153 /* Continue with first and start second usage */
154 if (*argv)
155 for (; *argv; argv++)
156 add_file(*argv);
157 else
158 add_file(NULL);
159 process();
160 cfclose(prog, NULL);
161 if (fclose(stdout))
162 err(FATAL, "stdout: %s", strerror(errno));
163 exit (0);
164 }
165
166 /*
167 * Like fgets, but go through the chain of compilation units chaining them
168 * together. Empty strings and files are ignored.
169 */
170 char *
171 cu_fgets(buf, n)
172 char *buf;
173 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(sp, spflag)
260 SPACE *sp;
261 enum e_spflag spflag;
262 {
263 static FILE *f; /* Current open file */
264 size_t len;
265 char *p;
266 int c;
267
268 if (f == NULL)
269 /* Advance to first non-empty file */
270 for (;;) {
271 if (files == NULL) {
272 lastline = 1;
273 return (0);
274 }
275 if (files->fname == NULL) {
276 f = stdin;
277 fname = "stdin";
278 } else {
279 fname = files->fname;
280 if ((f = fopen(fname, "r")) == NULL)
281 err(FATAL, "%s: %s",
282 fname, strerror(errno));
283 }
284 if ((c = getc(f)) != EOF) {
285 (void)ungetc(c, f);
286 break;
287 }
288 (void)fclose(f);
289 files = files->next;
290 }
291
292 if (lastline) {
293 sp->len = 0;
294 return (0);
295 }
296
297 /*
298 * Use fgetln so that we can handle essentially infinite input data.
299 * Can't use the pointer into the stdio buffer as the process space
300 * because the ungetc() can cause it to move.
301 */
302 p = fgetln(f, &len);
303 if (ferror(f))
304 err(FATAL, "%s: %s", fname, strerror(errno ? errno : EIO));
305 cspace(sp, p, len, spflag);
306
307 linenum++;
308 /* Advance to next non-empty file */
309 while ((c = getc(f)) == EOF) {
310 (void)fclose(f);
311 files = files->next;
312 if (files == NULL) {
313 lastline = 1;
314 return (1);
315 }
316 if (files->fname == NULL) {
317 f = stdin;
318 fname = "stdin";
319 } else {
320 fname = files->fname;
321 if ((f = fopen(fname, "r")) == NULL)
322 err(FATAL, "%s: %s", fname, strerror(errno));
323 }
324 }
325 (void)ungetc(c, f);
326 return (1);
327 }
328
329 /*
330 * Add a compilation unit to the linked list
331 */
332 static void
333 add_compunit(type, s)
334 enum e_cut type;
335 char *s;
336 {
337 struct s_compunit *cu;
338
339 cu = xmalloc(sizeof(struct s_compunit));
340 cu->type = type;
341 cu->s = s;
342 cu->next = NULL;
343 *cu_nextp = cu;
344 cu_nextp = &cu->next;
345 }
346
347 /*
348 * Add a file to the linked list
349 */
350 static void
351 add_file(s)
352 char *s;
353 {
354 struct s_flist *fp;
355
356 fp = xmalloc(sizeof(struct s_flist));
357 fp->next = NULL;
358 *fl_nextp = fp;
359 fp->fname = s;
360 fl_nextp = &fp->next;
361 }
362