xref: /openbsd/usr.bin/sed/main.c (revision 404b540a)
1 /*	$OpenBSD: main.c,v 1.16 2009/08/07 03:30:56 djm 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.16 2009/08/07 03:30:56 djm 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 Eflag, 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, "Eae:f:nru")) != -1)
109 		switch (c) {
110 		case 'E':
111 		case 'r':
112 			Eflag = 1;
113 			break;
114 		case 'a':
115 			aflag = 1;
116 			break;
117 		case 'e':
118 			eflag = 1;
119 			add_compunit(CU_STRING, optarg);
120 			break;
121 		case 'f':
122 			fflag = 1;
123 			add_compunit(CU_FILE, optarg);
124 			break;
125 		case 'n':
126 			nflag = 1;
127 			break;
128 		case 'u':
129 			setlinebuf(stdout);
130 			break;
131 		default:
132 		case '?':
133 			(void)fprintf(stderr,
134 			    "usage: sed [-aEnru] command [file ...]\n"
135 			    "       sed [-aEnru] [-e command] [-f command_file] [file ...]\n");
136 			exit(1);
137 		}
138 	argc -= optind;
139 	argv += optind;
140 
141 	/* First usage case; script is the first arg */
142 	if (!eflag && !fflag && *argv) {
143 		add_compunit(CU_STRING, *argv);
144 		argv++;
145 	}
146 
147 	compile();
148 
149 	/* Continue with first and start second usage */
150 	if (*argv)
151 		for (; *argv; argv++)
152 			add_file(*argv);
153 	else
154 		add_file(NULL);
155 	process();
156 	cfclose(prog, NULL);
157 	if (fclose(stdout))
158 		err(FATAL, "stdout: %s", strerror(errno));
159 	exit (0);
160 }
161 
162 /*
163  * Like fgets, but go through the chain of compilation units chaining them
164  * together.  Empty strings and files are ignored.
165  */
166 char *
167 cu_fgets(char **outbuf, size_t *outsize)
168 {
169 	static enum {ST_EOF, ST_FILE, ST_STRING} state = ST_EOF;
170 	static FILE *f;		/* Current open file */
171 	static char *s;		/* Current pointer inside string */
172 	static char string_ident[30];
173 	size_t len;
174 	char *p;
175 
176 	if (*outbuf == NULL)
177 		*outsize = 0;
178 
179 again:
180 	switch (state) {
181 	case ST_EOF:
182 		if (script == NULL)
183 			return (NULL);
184 		linenum = 0;
185 		switch (script->type) {
186 		case CU_FILE:
187 			if ((f = fopen(script->s, "r")) == NULL)
188 				err(FATAL,
189 				    "%s: %s", script->s, strerror(errno));
190 			fname = script->s;
191 			state = ST_FILE;
192 			goto again;
193 		case CU_STRING:
194 			if ((snprintf(string_ident,
195 			    sizeof(string_ident), "\"%s\"", script->s)) >=
196 			    sizeof(string_ident))
197 				strlcpy(string_ident +
198 				    sizeof(string_ident) - 6, " ...\"", 5);
199 			fname = string_ident;
200 			s = script->s;
201 			state = ST_STRING;
202 			goto again;
203 		}
204 	case ST_FILE:
205 		if ((p = fgetln(f, &len)) != NULL) {
206 			linenum++;
207 			if (len >= *outsize) {
208 				free(*outbuf);
209 				*outsize = ROUNDLEN(len + 1);
210 				*outbuf = xmalloc(*outsize);
211 			}
212 			memcpy(*outbuf, p, len);
213 			(*outbuf)[len] = '\0';
214 			if (linenum == 1 && p[0] == '#' && p[1] == 'n')
215 				nflag = 1;
216 			return (*outbuf);
217 		}
218 		script = script->next;
219 		(void)fclose(f);
220 		state = ST_EOF;
221 		goto again;
222 	case ST_STRING:
223 		if (linenum == 0 && s[0] == '#' && s[1] == 'n')
224 			nflag = 1;
225 		p = *outbuf;
226 		len = *outsize;
227 		for (;;) {
228 			if (len <= 1) {
229 				*outbuf = xrealloc(*outbuf,
230 				    *outsize + _POSIX2_LINE_MAX);
231 				p = *outbuf + *outsize - len;
232 				len += _POSIX2_LINE_MAX;
233 				*outsize += _POSIX2_LINE_MAX;
234 			}
235 			switch (*s) {
236 			case '\0':
237 				state = ST_EOF;
238 				if (s == script->s) {
239 					script = script->next;
240 					goto again;
241 				} else {
242 					script = script->next;
243 					*p = '\0';
244 					linenum++;
245 					return (*outbuf);
246 				}
247 			case '\n':
248 				*p++ = '\n';
249 				*p = '\0';
250 				s++;
251 				linenum++;
252 				return (*outbuf);
253 			default:
254 				*p++ = *s++;
255 				len--;
256 			}
257 		}
258 	}
259 	/* NOTREACHED */
260 }
261 
262 /*
263  * Like fgets, but go through the list of files chaining them together.
264  * Set len to the length of the line.
265  */
266 int
267 mf_fgets(SPACE *sp, enum e_spflag spflag)
268 {
269 	static FILE *f;		/* Current open file */
270 	size_t len;
271 	char *p;
272 	int c;
273 
274 	if (f == NULL)
275 		/* Advance to first non-empty file */
276 		for (;;) {
277 			if (files == NULL) {
278 				lastline = 1;
279 				return (0);
280 			}
281 			if (files->fname == NULL) {
282 				f = stdin;
283 				fname = "stdin";
284 			} else {
285 				fname = files->fname;
286 				if ((f = fopen(fname, "r")) == NULL)
287 					err(FATAL, "%s: %s",
288 					    fname, strerror(errno));
289 			}
290 			if ((c = getc(f)) != EOF) {
291 				(void)ungetc(c, f);
292 				break;
293 			}
294 			(void)fclose(f);
295 			files = files->next;
296 		}
297 
298 	if (lastline) {
299 		sp->len = 0;
300 		return (0);
301 	}
302 
303 	/*
304 	 * Use fgetln so that we can handle essentially infinite input data.
305 	 * Can't use the pointer into the stdio buffer as the process space
306 	 * because the ungetc() can cause it to move.
307 	 */
308 	p = fgetln(f, &len);
309 	if (ferror(f))
310 		err(FATAL, "%s: %s", fname, strerror(errno ? errno : EIO));
311 	cspace(sp, p, len, spflag);
312 
313 	linenum++;
314 	/* Advance to next non-empty file */
315 	while ((c = getc(f)) == EOF) {
316 		(void)fclose(f);
317 		files = files->next;
318 		if (files == NULL) {
319 			lastline = 1;
320 			return (1);
321 		}
322 		if (files->fname == NULL) {
323 			f = stdin;
324 			fname = "stdin";
325 		} else {
326 			fname = files->fname;
327 			if ((f = fopen(fname, "r")) == NULL)
328 				err(FATAL, "%s: %s", fname, strerror(errno));
329 		}
330 	}
331 	(void)ungetc(c, f);
332 	return (1);
333 }
334 
335 /*
336  * Add a compilation unit to the linked list
337  */
338 static void
339 add_compunit(enum e_cut type, char *s)
340 {
341 	struct s_compunit *cu;
342 
343 	cu = xmalloc(sizeof(struct s_compunit));
344 	cu->type = type;
345 	cu->s = s;
346 	cu->next = NULL;
347 	*cu_nextp = cu;
348 	cu_nextp = &cu->next;
349 }
350 
351 /*
352  * Add a file to the linked list
353  */
354 static void
355 add_file(char *s)
356 {
357 	struct s_flist *fp;
358 
359 	fp = xmalloc(sizeof(struct s_flist));
360 	fp->next = NULL;
361 	*fl_nextp = fp;
362 	fp->fname = s;
363 	fl_nextp = &fp->next;
364 }
365