Annotation of src/usr.bin/sed/main.c, Revision 1.14
1.14 ! millert 1: /* $OpenBSD: main.c,v 1.13 2008/10/08 17:26:47 millert 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.14 ! millert 41: static const char rcsid[] = "$OpenBSD: main.c,v 1.13 2008/10/08 17:26:47 millert 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.12 sobrado 129: "usage: sed [-anu] command [file ...]\n"
130: " sed [-anu] [-e command] [-f command_file] [file ...]\n");
1.1 deraadt 131: exit(1);
132: }
133: argc -= optind;
134: argv += optind;
135:
136: /* First usage case; script is the first arg */
137: if (!eflag && !fflag && *argv) {
138: add_compunit(CU_STRING, *argv);
139: argv++;
140: }
141:
142: compile();
143:
144: /* Continue with first and start second usage */
145: if (*argv)
146: for (; *argv; argv++)
147: add_file(*argv);
148: else
149: add_file(NULL);
150: process();
151: cfclose(prog, NULL);
152: if (fclose(stdout))
153: err(FATAL, "stdout: %s", strerror(errno));
154: exit (0);
155: }
156:
157: /*
158: * Like fgets, but go through the chain of compilation units chaining them
159: * together. Empty strings and files are ignored.
160: */
161: char *
1.13 millert 162: cu_fgets(char **outbuf, size_t *outlen)
1.1 deraadt 163: {
164: static enum {ST_EOF, ST_FILE, ST_STRING} state = ST_EOF;
165: static FILE *f; /* Current open file */
166: static char *s; /* Current pointer inside string */
167: static char string_ident[30];
1.13 millert 168: size_t len;
1.1 deraadt 169: char *p;
170:
171: again:
172: switch (state) {
173: case ST_EOF:
174: if (script == NULL)
175: return (NULL);
176: linenum = 0;
177: switch (script->type) {
178: case CU_FILE:
179: if ((f = fopen(script->s, "r")) == NULL)
180: err(FATAL,
181: "%s: %s", script->s, strerror(errno));
182: fname = script->s;
183: state = ST_FILE;
184: goto again;
185: case CU_STRING:
186: if ((snprintf(string_ident,
187: sizeof(string_ident), "\"%s\"", script->s)) >=
1.5 millert 188: sizeof(string_ident))
1.7 deraadt 189: strlcpy(string_ident +
190: sizeof(string_ident) - 6, " ...\"", 5);
1.1 deraadt 191: fname = string_ident;
192: s = script->s;
193: state = ST_STRING;
194: goto again;
195: }
196: case ST_FILE:
1.13 millert 197: if ((p = fgetln(f, &len)) != NULL) {
1.1 deraadt 198: linenum++;
1.13 millert 199: if (len >= *outlen) {
200: do {
201: *outlen *= 2;
202: } while (len >= *outlen);
203: free(*outbuf);
204: *outbuf = xmalloc(*outlen);
205: }
206: memcpy(*outbuf, p, len);
207: (*outbuf)[len] = '\0';
208: if (linenum == 1 && p[0] == '#' && p[1] == 'n')
1.1 deraadt 209: nflag = 1;
1.13 millert 210: return (*outbuf);
1.1 deraadt 211: }
212: script = script->next;
213: (void)fclose(f);
214: state = ST_EOF;
215: goto again;
216: case ST_STRING:
217: if (linenum == 0 && s[0] == '#' && s[1] == 'n')
218: nflag = 1;
1.13 millert 219: p = *outbuf;
220: len = *outlen;
1.1 deraadt 221: for (;;) {
1.13 millert 222: if (len-- <= 1) {
223: *outbuf = xrealloc(*outbuf, *outlen * 2);
224: p = *outbuf + *outlen - len - 1;
225: len += *outlen;
226: *outlen *= 2;
1.1 deraadt 227: }
228: switch (*s) {
229: case '\0':
230: state = ST_EOF;
231: if (s == script->s) {
232: script = script->next;
233: goto again;
234: } else {
235: script = script->next;
236: *p = '\0';
237: linenum++;
1.13 millert 238: return (*outbuf);
1.1 deraadt 239: }
240: case '\n':
241: *p++ = '\n';
242: *p = '\0';
243: s++;
244: linenum++;
1.13 millert 245: return (*outbuf);
1.1 deraadt 246: default:
247: *p++ = *s++;
248: }
249: }
250: }
251: /* NOTREACHED */
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
1.9 deraadt 259: mf_fgets(SPACE *sp, enum e_spflag spflag)
1.1 deraadt 260: {
261: static FILE *f; /* Current open file */
262: size_t len;
1.2 deraadt 263: char *p;
264: int c;
1.1 deraadt 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
1.9 deraadt 331: add_compunit(enum e_cut type, char *s)
1.1 deraadt 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
1.9 deraadt 347: add_file(char *s)
1.1 deraadt 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: }