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