Annotation of src/usr.bin/sed/main.c, Revision 1.13
1.13 ! millert 1: /* $OpenBSD: main.c,v 1.12 2007/10/16 20:19:27 sobrado 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.13 ! millert 41: static const char rcsid[] = "$OpenBSD: main.c,v 1.12 2007/10/16 20:19:27 sobrado 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 > 0 && p[len-1] == '\n')
! 200: len--;
! 201: if (len >= *outlen) {
! 202: do {
! 203: *outlen *= 2;
! 204: } while (len >= *outlen);
! 205: free(*outbuf);
! 206: *outbuf = xmalloc(*outlen);
! 207: }
! 208: memcpy(*outbuf, p, len);
! 209: (*outbuf)[len] = '\0';
! 210: if (linenum == 1 && p[0] == '#' && p[1] == 'n')
1.1 deraadt 211: nflag = 1;
1.13 ! millert 212: return (*outbuf);
1.1 deraadt 213: }
214: script = script->next;
215: (void)fclose(f);
216: state = ST_EOF;
217: goto again;
218: case ST_STRING:
219: if (linenum == 0 && s[0] == '#' && s[1] == 'n')
220: nflag = 1;
1.13 ! millert 221: p = *outbuf;
! 222: len = *outlen;
1.1 deraadt 223: for (;;) {
1.13 ! millert 224: if (len-- <= 1) {
! 225: *outbuf = xrealloc(*outbuf, *outlen * 2);
! 226: p = *outbuf + *outlen - len - 1;
! 227: len += *outlen;
! 228: *outlen *= 2;
1.1 deraadt 229: }
230: switch (*s) {
231: case '\0':
232: state = ST_EOF;
233: if (s == script->s) {
234: script = script->next;
235: goto again;
236: } else {
237: script = script->next;
238: *p = '\0';
239: linenum++;
1.13 ! millert 240: return (*outbuf);
1.1 deraadt 241: }
242: case '\n':
243: *p++ = '\n';
244: *p = '\0';
245: s++;
246: linenum++;
1.13 ! millert 247: return (*outbuf);
1.1 deraadt 248: default:
249: *p++ = *s++;
250: }
251: }
252: }
253: /* NOTREACHED */
254: }
255:
256: /*
257: * Like fgets, but go through the list of files chaining them together.
258: * Set len to the length of the line.
259: */
260: int
1.9 deraadt 261: mf_fgets(SPACE *sp, enum e_spflag spflag)
1.1 deraadt 262: {
263: static FILE *f; /* Current open file */
264: size_t len;
1.2 deraadt 265: char *p;
266: int c;
1.1 deraadt 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
1.9 deraadt 333: add_compunit(enum e_cut type, char *s)
1.1 deraadt 334: {
335: struct s_compunit *cu;
336:
337: cu = xmalloc(sizeof(struct s_compunit));
338: cu->type = type;
339: cu->s = s;
340: cu->next = NULL;
341: *cu_nextp = cu;
342: cu_nextp = &cu->next;
343: }
344:
345: /*
346: * Add a file to the linked list
347: */
348: static void
1.9 deraadt 349: add_file(char *s)
1.1 deraadt 350: {
351: struct s_flist *fp;
352:
353: fp = xmalloc(sizeof(struct s_flist));
354: fp->next = NULL;
355: *fl_nextp = fp;
356: fp->fname = s;
357: fl_nextp = &fp->next;
358: }