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