xref: /openbsd-src/usr.bin/sed/main.c (revision a28daedfc357b214be5c701aa8ba8adb29a7f1c2)
1 /*	$OpenBSD: main.c,v 1.15 2008/10/16 16:34:32 millert 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 const char copyright[] =
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"; */
41 static const char rcsid[] = "$OpenBSD: main.c,v 1.15 2008/10/16 16:34:32 millert Exp $";
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 <limits.h>
50 #include <regex.h>
51 #include <stddef.h>
52 #include <stdio.h>
53 #include <stdlib.h>
54 #include <string.h>
55 #include <unistd.h>
56 
57 #include "defs.h"
58 #include "extern.h"
59 
60 /*
61  * Linked list of units (strings and files) to be compiled
62  */
63 struct s_compunit {
64 	struct s_compunit *next;
65 	enum e_cut {CU_FILE, CU_STRING} type;
66 	char *s;			/* Pointer to string or fname */
67 };
68 
69 /*
70  * Linked list pointer to compilation units and pointer to current
71  * next pointer.
72  */
73 static struct s_compunit *script, **cu_nextp = &script;
74 
75 /*
76  * Linked list of files to be processed
77  */
78 struct s_flist {
79 	char *fname;
80 	struct s_flist *next;
81 };
82 
83 /*
84  * Linked list pointer to files and pointer to current
85  * next pointer.
86  */
87 static struct s_flist *files, **fl_nextp = &files;
88 
89 int aflag, eflag, nflag;
90 
91 /*
92  * Current file and line number; line numbers restart across compilation
93  * units, but span across input files.
94  */
95 char *fname;			/* File name. */
96 u_long linenum;
97 int lastline;			/* TRUE on the last line of the last file */
98 
99 static void add_compunit(enum e_cut, char *);
100 static void add_file(char *);
101 
102 int
103 main(int argc, char *argv[])
104 {
105 	int c, fflag;
106 
107 	fflag = 0;
108 	while ((c = getopt(argc, argv, "ae:f:nu")) != -1)
109 		switch (c) {
110 		case 'a':
111 			aflag = 1;
112 			break;
113 		case 'e':
114 			eflag = 1;
115 			add_compunit(CU_STRING, optarg);
116 			break;
117 		case 'f':
118 			fflag = 1;
119 			add_compunit(CU_FILE, optarg);
120 			break;
121 		case 'n':
122 			nflag = 1;
123 			break;
124 		case 'u':
125 			setlinebuf(stdout);
126 			break;
127 		default:
128 		case '?':
129 			(void)fprintf(stderr,
130 			    "usage: sed [-anu] command [file ...]\n"
131 			    "       sed [-anu] [-e command] [-f command_file] [file ...]\n");
132 			exit(1);
133 		}
134 	argc -= optind;
135 	argv += optind;
136 
137 	/* First usage case; script is the first arg */
138 	if (!eflag && !fflag && *argv) {
139 		add_compunit(CU_STRING, *argv);
140 		argv++;
141 	}
142 
143 	compile();
144 
145 	/* Continue with first and start second usage */
146 	if (*argv)
147 		for (; *argv; argv++)
148 			add_file(*argv);
149 	else
150 		add_file(NULL);
151 	process();
152 	cfclose(prog, NULL);
153 	if (fclose(stdout))
154 		err(FATAL, "stdout: %s", strerror(errno));
155 	exit (0);
156 }
157 
158 /*
159  * Like fgets, but go through the chain of compilation units chaining them
160  * together.  Empty strings and files are ignored.
161  */
162 char *
163 cu_fgets(char **outbuf, size_t *outsize)
164 {
165 	static enum {ST_EOF, ST_FILE, ST_STRING} state = ST_EOF;
166 	static FILE *f;		/* Current open file */
167 	static char *s;		/* Current pointer inside string */
168 	static char string_ident[30];
169 	size_t len;
170 	char *p;
171 
172 	if (*outbuf == NULL)
173 		*outsize = 0;
174 
175 again:
176 	switch (state) {
177 	case ST_EOF:
178 		if (script == NULL)
179 			return (NULL);
180 		linenum = 0;
181 		switch (script->type) {
182 		case CU_FILE:
183 			if ((f = fopen(script->s, "r")) == NULL)
184 				err(FATAL,
185 				    "%s: %s", script->s, strerror(errno));
186 			fname = script->s;
187 			state = ST_FILE;
188 			goto again;
189 		case CU_STRING:
190 			if ((snprintf(string_ident,
191 			    sizeof(string_ident), "\"%s\"", script->s)) >=
192 			    sizeof(string_ident))
193 				strlcpy(string_ident +
194 				    sizeof(string_ident) - 6, " ...\"", 5);
195 			fname = string_ident;
196 			s = script->s;
197 			state = ST_STRING;
198 			goto again;
199 		}
200 	case ST_FILE:
201 		if ((p = fgetln(f, &len)) != NULL) {
202 			linenum++;
203 			if (len >= *outsize) {
204 				free(*outbuf);
205 				*outsize = ROUNDLEN(len + 1);
206 				*outbuf = xmalloc(*outsize);
207 			}
208 			memcpy(*outbuf, p, len);
209 			(*outbuf)[len] = '\0';
210 			if (linenum == 1 && p[0] == '#' && p[1] == 'n')
211 				nflag = 1;
212 			return (*outbuf);
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;
221 		p = *outbuf;
222 		len = *outsize;
223 		for (;;) {
224 			if (len <= 1) {
225 				*outbuf = xrealloc(*outbuf,
226 				    *outsize + _POSIX2_LINE_MAX);
227 				p = *outbuf + *outsize - len;
228 				len += _POSIX2_LINE_MAX;
229 				*outsize += _POSIX2_LINE_MAX;
230 			}
231 			switch (*s) {
232 			case '\0':
233 				state = ST_EOF;
234 				if (s == script->s) {
235 					script = script->next;
236 					goto again;
237 				} else {
238 					script = script->next;
239 					*p = '\0';
240 					linenum++;
241 					return (*outbuf);
242 				}
243 			case '\n':
244 				*p++ = '\n';
245 				*p = '\0';
246 				s++;
247 				linenum++;
248 				return (*outbuf);
249 			default:
250 				*p++ = *s++;
251 				len--;
252 			}
253 		}
254 	}
255 	/* NOTREACHED */
256 }
257 
258 /*
259  * Like fgets, but go through the list of files chaining them together.
260  * Set len to the length of the line.
261  */
262 int
263 mf_fgets(SPACE *sp, enum e_spflag spflag)
264 {
265 	static FILE *f;		/* Current open file */
266 	size_t len;
267 	char *p;
268 	int c;
269 
270 	if (f == NULL)
271 		/* Advance to first non-empty file */
272 		for (;;) {
273 			if (files == NULL) {
274 				lastline = 1;
275 				return (0);
276 			}
277 			if (files->fname == NULL) {
278 				f = stdin;
279 				fname = "stdin";
280 			} else {
281 				fname = files->fname;
282 				if ((f = fopen(fname, "r")) == NULL)
283 					err(FATAL, "%s: %s",
284 					    fname, strerror(errno));
285 			}
286 			if ((c = getc(f)) != EOF) {
287 				(void)ungetc(c, f);
288 				break;
289 			}
290 			(void)fclose(f);
291 			files = files->next;
292 		}
293 
294 	if (lastline) {
295 		sp->len = 0;
296 		return (0);
297 	}
298 
299 	/*
300 	 * Use fgetln so that we can handle essentially infinite input data.
301 	 * Can't use the pointer into the stdio buffer as the process space
302 	 * because the ungetc() can cause it to move.
303 	 */
304 	p = fgetln(f, &len);
305 	if (ferror(f))
306 		err(FATAL, "%s: %s", fname, strerror(errno ? errno : EIO));
307 	cspace(sp, p, len, spflag);
308 
309 	linenum++;
310 	/* Advance to next non-empty file */
311 	while ((c = getc(f)) == EOF) {
312 		(void)fclose(f);
313 		files = files->next;
314 		if (files == NULL) {
315 			lastline = 1;
316 			return (1);
317 		}
318 		if (files->fname == NULL) {
319 			f = stdin;
320 			fname = "stdin";
321 		} else {
322 			fname = files->fname;
323 			if ((f = fopen(fname, "r")) == NULL)
324 				err(FATAL, "%s: %s", fname, strerror(errno));
325 		}
326 	}
327 	(void)ungetc(c, f);
328 	return (1);
329 }
330 
331 /*
332  * Add a compilation unit to the linked list
333  */
334 static void
335 add_compunit(enum e_cut type, char *s)
336 {
337 	struct s_compunit *cu;
338 
339 	cu = xmalloc(sizeof(struct s_compunit));
340 	cu->type = type;
341 	cu->s = s;
342 	cu->next = NULL;
343 	*cu_nextp = cu;
344 	cu_nextp = &cu->next;
345 }
346 
347 /*
348  * Add a file to the linked list
349  */
350 static void
351 add_file(char *s)
352 {
353 	struct s_flist *fp;
354 
355 	fp = xmalloc(sizeof(struct s_flist));
356 	fp->next = NULL;
357 	*fl_nextp = fp;
358 	fp->fname = s;
359 	fl_nextp = &fp->next;
360 }
361