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