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