xref: /netbsd-src/usr.bin/sed/main.c (revision b1c86f5f087524e68db12794ee9c3e3da1ab17a0)
1 /*	$NetBSD: main.c,v 1.21 2010/02/19 16:35:27 tnn Exp $	*/
2 
3 /*-
4  * Copyright (c) 1992, 1993
5  *	The Regents of the University of California.  All rights reserved.
6  *
7  * This code is derived from software contributed to Berkeley by
8  * Diomidis Spinellis of Imperial College, University of London.
9  *
10  * Redistribution and use in source and binary forms, with or without
11  * modification, are permitted provided that the following conditions
12  * are met:
13  * 1. Redistributions of source code must retain the above copyright
14  *    notice, this list of conditions and the following disclaimer.
15  * 2. Redistributions in binary form must reproduce the above copyright
16  *    notice, this list of conditions and the following disclaimer in the
17  *    documentation and/or other materials provided with the distribution.
18  * 3. Neither the name of the University nor the names of its contributors
19  *    may be used to endorse or promote products derived from this software
20  *    without specific prior written permission.
21  *
22  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
23  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
24  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
25  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
26  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
27  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
28  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
29  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
30  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
31  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
32  * SUCH DAMAGE.
33  */
34 
35 /*-
36  * Copyright (c) 1992 Diomidis Spinellis.
37  *
38  * This code is derived from software contributed to Berkeley by
39  * Diomidis Spinellis of Imperial College, University of London.
40  *
41  * Redistribution and use in source and binary forms, with or without
42  * modification, are permitted provided that the following conditions
43  * are met:
44  * 1. Redistributions of source code must retain the above copyright
45  *    notice, this list of conditions and the following disclaimer.
46  * 2. Redistributions in binary form must reproduce the above copyright
47  *    notice, this list of conditions and the following disclaimer in the
48  *    documentation and/or other materials provided with the distribution.
49  * 3. All advertising materials mentioning features or use of this software
50  *    must display the following acknowledgement:
51  *	This product includes software developed by the University of
52  *	California, Berkeley and its contributors.
53  * 4. Neither the name of the University nor the names of its contributors
54  *    may be used to endorse or promote products derived from this software
55  *    without specific prior written permission.
56  *
57  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
58  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
59  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
60  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
61  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
62  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
63  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
64  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
65  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
66  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
67  * SUCH DAMAGE.
68  */
69 
70 #if HAVE_NBTOOL_CONFIG_H
71 #include "nbtool_config.h"
72 #endif
73 
74 #include <sys/cdefs.h>
75 #ifndef lint
76 __COPYRIGHT("@(#) Copyright (c) 1992, 1993\
77  The Regents of the University of California.  All rights reserved.");
78 #endif /* not lint */
79 
80 #ifndef lint
81 #if 0
82 static char sccsid[] = "@(#)main.c	8.2 (Berkeley) 1/3/94";
83 #else
84 __RCSID("$NetBSD: main.c,v 1.21 2010/02/19 16:35:27 tnn Exp $");
85 #endif
86 #endif /* not lint */
87 
88 #include <sys/types.h>
89 
90 #include <ctype.h>
91 #include <errno.h>
92 #include <fcntl.h>
93 #include <limits.h>
94 #include <regex.h>
95 #include <stddef.h>
96 #include <stdio.h>
97 #include <stdlib.h>
98 #include <string.h>
99 #include <unistd.h>
100 
101 #include "defs.h"
102 #include "extern.h"
103 
104 /*
105  * Linked list of units (strings and files) to be compiled
106  */
107 struct s_compunit {
108 	struct s_compunit *next;
109 	enum e_cut {CU_FILE, CU_STRING} type;
110 	char *s;			/* Pointer to string or fname */
111 };
112 
113 /*
114  * Linked list pointer to compilation units and pointer to current
115  * next pointer.
116  */
117 static struct s_compunit *script, **cu_nextp = &script;
118 
119 /*
120  * Linked list of files to be processed
121  */
122 struct s_flist {
123 	char *fname;
124 	struct s_flist *next;
125 };
126 
127 /*
128  * Linked list pointer to files and pointer to current
129  * next pointer.
130  */
131 static struct s_flist *files, **fl_nextp = &files;
132 
133 int aflag, eflag, nflag, ere;
134 
135 /*
136  * Current file and line number; line numbers restart across compilation
137  * units, but span across input files.
138  */
139 const char *fname;			/* File name. */
140 u_long linenum;
141 int lastline;			/* TRUE on the last line of the last file */
142 
143 static void add_compunit(enum e_cut, char *);
144 static void add_file(char *);
145 int	main(int, char **);
146 
147 int
148 main(int argc, char *argv[])
149 {
150 	int c, fflag;
151 
152 	setprogname(*argv);
153 	fflag = 0;
154 	while ((c = getopt(argc, argv, "ae:f:nrE")) != -1)
155 		switch (c) {
156 		case 'a':
157 			aflag = 1;
158 			break;
159 		case 'e':
160 			eflag = 1;
161 			add_compunit(CU_STRING, optarg);
162 			break;
163 		case 'f':
164 			fflag = 1;
165 			add_compunit(CU_FILE, optarg);
166 			break;
167 		case 'n':
168 			nflag = 1;
169 			break;
170 		case 'r':
171 		case 'E':
172 			ere = REG_EXTENDED;
173 			break;
174 		default:
175 		case '?':
176 			(void)fprintf(stderr,
177 "usage:\t%s [-aEnr] script [file ...]\n\t%s [-aEnr] [-e script] ... [-f script_file] ... [file ...]\n",
178 			    getprogname(), getprogname());
179 			exit(1);
180 		}
181 	argc -= optind;
182 	argv += optind;
183 
184 	/* First usage case; script is the first arg */
185 	if (!eflag && !fflag && *argv) {
186 		add_compunit(CU_STRING, *argv);
187 		argv++;
188 	}
189 
190 	compile();
191 
192 	/* Continue with first and start second usage */
193 	if (*argv)
194 		for (; *argv; argv++)
195 			add_file(*argv);
196 	else
197 		add_file(NULL);
198 	process();
199 	cfclose(prog, NULL);
200 	if (fclose(stdout))
201 		err(FATAL, "stdout: %s", strerror(errno));
202 	exit (0);
203 }
204 
205 /*
206  * Like fgets, but go through the chain of compilation units chaining them
207  * together.  Empty strings and files are ignored.
208  */
209 char *
210 cu_fgets(char **outbuf, size_t *outsize)
211 {
212 	static enum {ST_EOF, ST_FILE, ST_STRING} state = ST_EOF;
213 	static FILE *f;		/* Current open file */
214 	static char *s;		/* Current pointer inside string */
215 	static char string_ident[30];
216 	size_t len;
217 	char *p;
218 
219 	if (*outbuf == NULL)
220 		*outsize = 0;
221 
222 again:
223 	switch (state) {
224 	case ST_EOF:
225 		if (script == NULL)
226 			return (NULL);
227 		linenum = 0;
228 		switch (script->type) {
229 		case CU_FILE:
230 			if ((f = fopen(script->s, "r")) == NULL)
231 				err(FATAL,
232 				    "%s: %s", script->s, strerror(errno));
233 			fname = script->s;
234 			state = ST_FILE;
235 			goto again;
236 		case CU_STRING:
237 			if ((snprintf(string_ident,
238 			    sizeof(string_ident), "\"%s\"", script->s)) >=
239 			    (int)(sizeof(string_ident) - 1))
240 				(void)strcpy(string_ident +
241 				    sizeof(string_ident) - 6, " ...\"");
242 			fname = string_ident;
243 			s = script->s;
244 			state = ST_STRING;
245 			goto again;
246 		}
247 	case ST_FILE:
248 		if ((p = fgetln(f, &len)) != NULL) {
249 			linenum++;
250 			if (len >= *outsize) {
251 				free(*outbuf);
252 				*outsize = ROUNDLEN(len + 1);
253 				*outbuf = xmalloc(*outsize);
254 			}
255 			memcpy(*outbuf, p, len);
256 			(*outbuf)[len] = '\0';
257 			if (linenum == 1 && p[0] == '#' && p[1] == 'n')
258 				nflag = 1;
259 			return (*outbuf);
260 		}
261 		script = script->next;
262 		(void)fclose(f);
263 		state = ST_EOF;
264 		goto again;
265 	case ST_STRING:
266 		if (linenum == 0 && s[0] == '#' && s[1] == 'n')
267 			nflag = 1;
268 		p = *outbuf;
269 		len = *outsize;
270 		for (;;) {
271 			if (len <= 1) {
272 				*outbuf = xrealloc(*outbuf,
273 				    *outsize + _POSIX2_LINE_MAX);
274 				p = *outbuf + *outsize - len;
275 				len += _POSIX2_LINE_MAX;
276 				*outsize += _POSIX2_LINE_MAX;
277 			}
278 			switch (*s) {
279 			case '\0':
280 				state = ST_EOF;
281 				if (s == script->s) {
282 					script = script->next;
283 					goto again;
284 				} else {
285 					script = script->next;
286 					*p = '\0';
287 					linenum++;
288 					return (*outbuf);
289 				}
290 			case '\n':
291 				*p++ = '\n';
292 				*p = '\0';
293 				s++;
294 				linenum++;
295 				return (*outbuf);
296 			default:
297 				*p++ = *s++;
298 				len--;
299 			}
300 		}
301 	}
302 	/* NOTREACHED */
303 	return (NULL);
304 }
305 
306 /*
307  * Like fgets, but go through the list of files chaining them together.
308  * Set len to the length of the line.
309  */
310 int
311 mf_fgets(SPACE *sp, enum e_spflag spflag)
312 {
313 	static FILE *f;		/* Current open file */
314 	size_t len;
315 	char *p;
316 	int c;
317 
318 	if (f == NULL)
319 		/* Advance to first non-empty file */
320 		for (;;) {
321 			if (files == NULL) {
322 				lastline = 1;
323 				return (0);
324 			}
325 			if (files->fname == NULL) {
326 				f = stdin;
327 				fname = "stdin";
328 			} else {
329 				fname = files->fname;
330 				if ((f = fopen(fname, "r")) == NULL)
331 					err(FATAL, "%s: %s",
332 					    fname, strerror(errno));
333 			}
334 			if ((c = getc(f)) != EOF) {
335 				(void)ungetc(c, f);
336 				break;
337 			}
338 			(void)fclose(f);
339 			files = files->next;
340 		}
341 
342 	if (lastline) {
343 		sp->len = 0;
344 		return (0);
345 	}
346 
347 	/*
348 	 * Use fgetln so that we can handle essentially infinite input data.
349 	 * Can't use the pointer into the stdio buffer as the process space
350 	 * because the ungetc() can cause it to move.
351 	 */
352 	p = fgetln(f, &len);
353 	if (ferror(f))
354 		err(FATAL, "%s: %s", fname, strerror(errno ? errno : EIO));
355 	cspace(sp, p, len, spflag);
356 
357 	linenum++;
358 	/* Advance to next non-empty file */
359 	while ((c = getc(f)) == EOF) {
360 		(void)fclose(f);
361 		files = files->next;
362 		if (files == NULL) {
363 			lastline = 1;
364 			return (1);
365 		}
366 		if (files->fname == NULL) {
367 			f = stdin;
368 			fname = "stdin";
369 		} else {
370 			fname = files->fname;
371 			if ((f = fopen(fname, "r")) == NULL)
372 				err(FATAL, "%s: %s", fname, strerror(errno));
373 		}
374 	}
375 	(void)ungetc(c, f);
376 	return (1);
377 }
378 
379 /*
380  * Add a compilation unit to the linked list
381  */
382 static void
383 add_compunit(enum e_cut type, char *s)
384 {
385 	struct s_compunit *cu;
386 
387 	cu = xmalloc(sizeof(struct s_compunit));
388 	cu->type = type;
389 	cu->s = s;
390 	cu->next = NULL;
391 	*cu_nextp = cu;
392 	cu_nextp = &cu->next;
393 }
394 
395 /*
396  * Add a file to the linked list
397  */
398 static void
399 add_file(char *s)
400 {
401 	struct s_flist *fp;
402 
403 	fp = xmalloc(sizeof(struct s_flist));
404 	fp->next = NULL;
405 	*fl_nextp = fp;
406 	fp->fname = s;
407 	fl_nextp = &fp->next;
408 }
409