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