xref: /freebsd/usr.bin/sed/main.c (revision 0de89efe5c443f213c7ea28773ef2dc6cf3af2ed)
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$";
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)
180 	char *buf;
181 	int n;
182 {
183 	static enum {ST_EOF, ST_FILE, ST_STRING} state = ST_EOF;
184 	static FILE *f;		/* Current open file */
185 	static char *s;		/* Current pointer inside string */
186 	static char string_ident[30];
187 	char *p;
188 
189 again:
190 	switch (state) {
191 	case ST_EOF:
192 		if (script == NULL)
193 			return (NULL);
194 		linenum = 0;
195 		switch (script->type) {
196 		case CU_FILE:
197 			if ((f = fopen(script->s, "r")) == NULL)
198 				err(1, "%s", script->s);
199 			fname = script->s;
200 			state = ST_FILE;
201 			goto again;
202 		case CU_STRING:
203 			if ((snprintf(string_ident,
204 			    sizeof(string_ident), "\"%s\"", script->s)) >=
205 			    sizeof(string_ident) - 1)
206 				(void)strcpy(string_ident +
207 				    sizeof(string_ident) - 6, " ...\"");
208 			fname = string_ident;
209 			s = script->s;
210 			state = ST_STRING;
211 			goto again;
212 		}
213 	case ST_FILE:
214 		if ((p = fgets(buf, n, f)) != NULL) {
215 			linenum++;
216 			if (linenum == 1 && buf[0] == '#' && buf[1] == 'n')
217 				nflag = 1;
218 			return (p);
219 		}
220 		script = script->next;
221 		(void)fclose(f);
222 		state = ST_EOF;
223 		goto again;
224 	case ST_STRING:
225 		if (linenum == 0 && s[0] == '#' && s[1] == 'n')
226 			nflag = 1;
227 		p = buf;
228 		for (;;) {
229 			if (n-- <= 1) {
230 				*p = '\0';
231 				linenum++;
232 				return (buf);
233 			}
234 			switch (*s) {
235 			case '\0':
236 				state = ST_EOF;
237 				if (s == script->s) {
238 					script = script->next;
239 					goto again;
240 				} else {
241 					script = script->next;
242 					*p = '\0';
243 					linenum++;
244 					return (buf);
245 				}
246 			case '\n':
247 				*p++ = '\n';
248 				*p = '\0';
249 				s++;
250 				linenum++;
251 				return (buf);
252 			default:
253 				*p++ = *s++;
254 			}
255 		}
256 	}
257 	/* NOTREACHED */
258 	return (NULL);
259 }
260 
261 /*
262  * Like fgets, but go through the list of files chaining them together.
263  * Set len to the length of the line.
264  */
265 int
266 mf_fgets(sp, spflag)
267 	SPACE *sp;
268 	enum e_spflag spflag;
269 {
270 	static FILE *f;		/* Current open file */
271 	size_t len;
272 	char *p;
273 	int c;
274 
275 	if (f == NULL)
276 		/* Advance to first non-empty file */
277 		for (;;) {
278 			if (files == NULL) {
279 				lastline = 1;
280 				return (0);
281 			}
282 			if (files->fname == NULL) {
283 				f = stdin;
284 				fname = "stdin";
285 			} else {
286 				fname = files->fname;
287 				if ((f = fopen(fname, "r")) == NULL)
288 					err(1, "%s", fname);
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 		errx(1, "%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(1, "%s", fname);
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(type, s)
340 	enum e_cut type;
341 	char *s;
342 {
343 	struct s_compunit *cu;
344 
345 	cu = xmalloc(sizeof(struct s_compunit));
346 	cu->type = type;
347 	cu->s = s;
348 	cu->next = NULL;
349 	*cu_nextp = cu;
350 	cu_nextp = &cu->next;
351 }
352 
353 /*
354  * Add a file to the linked list
355  */
356 static void
357 add_file(s)
358 	char *s;
359 {
360 	struct s_flist *fp;
361 
362 	fp = xmalloc(sizeof(struct s_flist));
363 	fp->next = NULL;
364 	*fl_nextp = fp;
365 	fp->fname = s;
366 	fl_nextp = &fp->next;
367 }
368