xref: /openbsd-src/usr.bin/sed/main.c (revision 8500990981f885cbe5e6a4958549cacc238b5ae6)
1 /*	$OpenBSD: main.c,v 1.9 2003/06/10 22:20:50 deraadt 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. Neither the name of the University nor the names of its contributors
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"; */
44 static char *rcsid = "$OpenBSD: main.c,v 1.9 2003/06/10 22:20:50 deraadt Exp $";
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 
101 static void add_compunit(enum e_cut, char *);
102 static void add_file(char *);
103 
104 int
105 main(int argc, char *argv[])
106 {
107 	int c, fflag;
108 
109 	fflag = 0;
110 	while ((c = getopt(argc, argv, "ae:f:n")) != -1)
111 		switch (c) {
112 		case 'a':
113 			aflag = 1;
114 			break;
115 		case 'e':
116 			eflag = 1;
117 			add_compunit(CU_STRING, optarg);
118 			break;
119 		case 'f':
120 			fflag = 1;
121 			add_compunit(CU_FILE, optarg);
122 			break;
123 		case 'n':
124 			nflag = 1;
125 			break;
126 		default:
127 		case '?':
128 			(void)fprintf(stderr,
129 "usage:\tsed script [-an] [file ...]\n\tsed [-an] [-e script] ... [-f script_file] ... [file ...]\n");
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 *
161 cu_fgets(char *buf, int n)
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)) >=
186 			    sizeof(string_ident))
187 				strlcpy(string_ident +
188 				    sizeof(string_ident) - 6, " ...\"", 5);
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
246 mf_fgets(SPACE *sp, enum e_spflag spflag)
247 {
248 	static FILE *f;		/* Current open file */
249 	size_t len;
250 	char *p;
251 	int c;
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
318 add_compunit(enum e_cut type, char *s)
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
334 add_file(char *s)
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 }
344