xref: /openbsd-src/usr.bin/sed/main.c (revision d13be5d47e4149db2549a9828e244d59dbc43f15)
1 /*	$OpenBSD: main.c,v 1.17 2009/10/27 23:59:43 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 #include <sys/types.h>
37 
38 #include <ctype.h>
39 #include <errno.h>
40 #include <fcntl.h>
41 #include <limits.h>
42 #include <regex.h>
43 #include <stddef.h>
44 #include <stdio.h>
45 #include <stdlib.h>
46 #include <string.h>
47 #include <unistd.h>
48 
49 #include "defs.h"
50 #include "extern.h"
51 
52 /*
53  * Linked list of units (strings and files) to be compiled
54  */
55 struct s_compunit {
56 	struct s_compunit *next;
57 	enum e_cut {CU_FILE, CU_STRING} type;
58 	char *s;			/* Pointer to string or fname */
59 };
60 
61 /*
62  * Linked list pointer to compilation units and pointer to current
63  * next pointer.
64  */
65 static struct s_compunit *script, **cu_nextp = &script;
66 
67 /*
68  * Linked list of files to be processed
69  */
70 struct s_flist {
71 	char *fname;
72 	struct s_flist *next;
73 };
74 
75 /*
76  * Linked list pointer to files and pointer to current
77  * next pointer.
78  */
79 static struct s_flist *files, **fl_nextp = &files;
80 
81 int Eflag, aflag, eflag, nflag;
82 
83 /*
84  * Current file and line number; line numbers restart across compilation
85  * units, but span across input files.
86  */
87 char *fname;			/* File name. */
88 u_long linenum;
89 int lastline;			/* TRUE on the last line of the last file */
90 
91 static void add_compunit(enum e_cut, char *);
92 static void add_file(char *);
93 
94 int
95 main(int argc, char *argv[])
96 {
97 	int c, fflag;
98 
99 	fflag = 0;
100 	while ((c = getopt(argc, argv, "Eae:f:nru")) != -1)
101 		switch (c) {
102 		case 'E':
103 		case 'r':
104 			Eflag = 1;
105 			break;
106 		case 'a':
107 			aflag = 1;
108 			break;
109 		case 'e':
110 			eflag = 1;
111 			add_compunit(CU_STRING, optarg);
112 			break;
113 		case 'f':
114 			fflag = 1;
115 			add_compunit(CU_FILE, optarg);
116 			break;
117 		case 'n':
118 			nflag = 1;
119 			break;
120 		case 'u':
121 			setlinebuf(stdout);
122 			break;
123 		default:
124 		case '?':
125 			(void)fprintf(stderr,
126 			    "usage: sed [-aEnru] command [file ...]\n"
127 			    "       sed [-aEnru] [-e command] [-f command_file] [file ...]\n");
128 			exit(1);
129 		}
130 	argc -= optind;
131 	argv += optind;
132 
133 	/* First usage case; script is the first arg */
134 	if (!eflag && !fflag && *argv) {
135 		add_compunit(CU_STRING, *argv);
136 		argv++;
137 	}
138 
139 	compile();
140 
141 	/* Continue with first and start second usage */
142 	if (*argv)
143 		for (; *argv; argv++)
144 			add_file(*argv);
145 	else
146 		add_file(NULL);
147 	process();
148 	cfclose(prog, NULL);
149 	if (fclose(stdout))
150 		err(FATAL, "stdout: %s", strerror(errno));
151 	exit (0);
152 }
153 
154 /*
155  * Like fgets, but go through the chain of compilation units chaining them
156  * together.  Empty strings and files are ignored.
157  */
158 char *
159 cu_fgets(char **outbuf, size_t *outsize)
160 {
161 	static enum {ST_EOF, ST_FILE, ST_STRING} state = ST_EOF;
162 	static FILE *f;		/* Current open file */
163 	static char *s;		/* Current pointer inside string */
164 	static char string_ident[30];
165 	size_t len;
166 	char *p;
167 
168 	if (*outbuf == NULL)
169 		*outsize = 0;
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)) >=
188 			    sizeof(string_ident))
189 				strlcpy(string_ident +
190 				    sizeof(string_ident) - 6, " ...\"", 5);
191 			fname = string_ident;
192 			s = script->s;
193 			state = ST_STRING;
194 			goto again;
195 		}
196 	case ST_FILE:
197 		if ((p = fgetln(f, &len)) != NULL) {
198 			linenum++;
199 			if (len >= *outsize) {
200 				free(*outbuf);
201 				*outsize = ROUNDLEN(len + 1);
202 				*outbuf = xmalloc(*outsize);
203 			}
204 			memcpy(*outbuf, p, len);
205 			(*outbuf)[len] = '\0';
206 			if (linenum == 1 && p[0] == '#' && p[1] == 'n')
207 				nflag = 1;
208 			return (*outbuf);
209 		}
210 		script = script->next;
211 		(void)fclose(f);
212 		state = ST_EOF;
213 		goto again;
214 	case ST_STRING:
215 		if (linenum == 0 && s[0] == '#' && s[1] == 'n')
216 			nflag = 1;
217 		p = *outbuf;
218 		len = *outsize;
219 		for (;;) {
220 			if (len <= 1) {
221 				*outbuf = xrealloc(*outbuf,
222 				    *outsize + _POSIX2_LINE_MAX);
223 				p = *outbuf + *outsize - len;
224 				len += _POSIX2_LINE_MAX;
225 				*outsize += _POSIX2_LINE_MAX;
226 			}
227 			switch (*s) {
228 			case '\0':
229 				state = ST_EOF;
230 				if (s == script->s) {
231 					script = script->next;
232 					goto again;
233 				} else {
234 					script = script->next;
235 					*p = '\0';
236 					linenum++;
237 					return (*outbuf);
238 				}
239 			case '\n':
240 				*p++ = '\n';
241 				*p = '\0';
242 				s++;
243 				linenum++;
244 				return (*outbuf);
245 			default:
246 				*p++ = *s++;
247 				len--;
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
259 mf_fgets(SPACE *sp, enum e_spflag spflag)
260 {
261 	static FILE *f;		/* Current open file */
262 	size_t len;
263 	char *p;
264 	int c;
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
331 add_compunit(enum e_cut type, char *s)
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
347 add_file(char *s)
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 }
357