xref: /freebsd/usr.bin/sed/main.c (revision 7aa383846770374466b1dcb2cefd71bde9acf463)
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  * 4. Neither the name of the University nor the names of its contributors
18  *    may be used to endorse or promote products derived from this software
19  *    without specific prior written permission.
20  *
21  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
22  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
23  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
24  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
25  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
26  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
27  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
28  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
29  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
30  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
31  * SUCH DAMAGE.
32  */
33 
34 #include <sys/cdefs.h>
35 __FBSDID("$FreeBSD$");
36 
37 #ifndef lint
38 static const char copyright[] =
39 "@(#) Copyright (c) 1992, 1993\n\
40 	The Regents of the University of California.  All rights reserved.\n";
41 #endif
42 
43 #ifndef lint
44 static const char sccsid[] = "@(#)main.c	8.2 (Berkeley) 1/3/94";
45 #endif
46 
47 #include <sys/types.h>
48 #include <sys/mman.h>
49 #include <sys/param.h>
50 #include <sys/stat.h>
51 
52 #include <err.h>
53 #include <errno.h>
54 #include <fcntl.h>
55 #include <libgen.h>
56 #include <limits.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 FILE *infile;			/* Current input file */
98 FILE *outfile;			/* Current output file */
99 
100 int aflag, eflag, nflag;
101 int rflags = 0;
102 static int rval;		/* Exit status */
103 
104 static int ispan;		/* Whether inplace editing spans across files */
105 
106 /*
107  * Current file and line number; line numbers restart across compilation
108  * units, but span across input files.  The latter is optional if editing
109  * in place.
110  */
111 const char *fname;		/* File name. */
112 const char *outfname;		/* Output file name */
113 static char oldfname[PATH_MAX];	/* Old file name (for in-place editing) */
114 static char tmpfname[PATH_MAX];	/* Temporary file name (for in-place editing) */
115 static const char *inplace;	/* Inplace edit file extension. */
116 u_long linenum;
117 
118 static void add_compunit(enum e_cut, char *);
119 static void add_file(char *);
120 static void usage(void);
121 
122 int
123 main(int argc, char *argv[])
124 {
125 	int c, fflag;
126 	char *temp_arg;
127 
128 	(void) setlocale(LC_ALL, "");
129 
130 	fflag = 0;
131 	inplace = NULL;
132 
133 	while ((c = getopt(argc, argv, "EI:ae:f:i:lnr")) != -1)
134 		switch (c) {
135 		case 'r':		/* Gnu sed compat */
136 		case 'E':
137 			rflags = REG_EXTENDED;
138 			break;
139 		case 'I':
140 			inplace = optarg;
141 			ispan = 1;	/* span across input files */
142 			break;
143 		case 'a':
144 			aflag = 1;
145 			break;
146 		case 'e':
147 			eflag = 1;
148 			if ((temp_arg = malloc(strlen(optarg) + 2)) == NULL)
149 				err(1, "malloc");
150 			strcpy(temp_arg, optarg);
151 			strcat(temp_arg, "\n");
152 			add_compunit(CU_STRING, temp_arg);
153 			break;
154 		case 'f':
155 			fflag = 1;
156 			add_compunit(CU_FILE, optarg);
157 			break;
158 		case 'i':
159 			inplace = optarg;
160 			ispan = 0;	/* don't span across input files */
161 			break;
162 		case 'l':
163 			if(setlinebuf(stdout) != 0)
164 				warnx("setlinebuf() failed");
165 			break;
166 		case 'n':
167 			nflag = 1;
168 			break;
169 		default:
170 		case '?':
171 			usage();
172 		}
173 	argc -= optind;
174 	argv += optind;
175 
176 	/* First usage case; script is the first arg */
177 	if (!eflag && !fflag && *argv) {
178 		add_compunit(CU_STRING, *argv);
179 		argv++;
180 	}
181 
182 	compile();
183 
184 	/* Continue with first and start second usage */
185 	if (*argv)
186 		for (; *argv; argv++)
187 			add_file(*argv);
188 	else
189 		add_file(NULL);
190 	process();
191 	cfclose(prog, NULL);
192 	if (fclose(stdout))
193 		err(1, "stdout");
194 	exit(rval);
195 }
196 
197 static void
198 usage(void)
199 {
200 	(void)fprintf(stderr, "%s\n%s\n",
201 		"usage: sed script [-Ealn] [-i extension] [file ...]",
202 		"       sed [-Ealn] [-i extension] [-e script] ... [-f script_file] ... [file ...]");
203 	exit(1);
204 }
205 
206 /*
207  * Like fgets, but go through the chain of compilation units chaining them
208  * together.  Empty strings and files are ignored.
209  */
210 char *
211 cu_fgets(char *buf, int n, int *more)
212 {
213 	static enum {ST_EOF, ST_FILE, ST_STRING} state = ST_EOF;
214 	static FILE *f;		/* Current open file */
215 	static char *s;		/* Current pointer inside string */
216 	static char string_ident[30];
217 	char *p;
218 
219 again:
220 	switch (state) {
221 	case ST_EOF:
222 		if (script == NULL) {
223 			if (more != NULL)
224 				*more = 0;
225 			return (NULL);
226 		}
227 		linenum = 0;
228 		switch (script->type) {
229 		case CU_FILE:
230 			if ((f = fopen(script->s, "r")) == NULL)
231 				err(1, "%s", script->s);
232 			fname = script->s;
233 			state = ST_FILE;
234 			goto again;
235 		case CU_STRING:
236 			if (((size_t)snprintf(string_ident,
237 			    sizeof(string_ident), "\"%s\"", script->s)) >=
238 			    sizeof(string_ident) - 1)
239 				(void)strcpy(string_ident +
240 				    sizeof(string_ident) - 6, " ...\"");
241 			fname = string_ident;
242 			s = script->s;
243 			state = ST_STRING;
244 			goto again;
245 		}
246 	case ST_FILE:
247 		if ((p = fgets(buf, n, f)) != NULL) {
248 			linenum++;
249 			if (linenum == 1 && buf[0] == '#' && buf[1] == 'n')
250 				nflag = 1;
251 			if (more != NULL)
252 				*more = !feof(f);
253 			return (p);
254 		}
255 		script = script->next;
256 		(void)fclose(f);
257 		state = ST_EOF;
258 		goto again;
259 	case ST_STRING:
260 		if (linenum == 0 && s[0] == '#' && s[1] == 'n')
261 			nflag = 1;
262 		p = buf;
263 		for (;;) {
264 			if (n-- <= 1) {
265 				*p = '\0';
266 				linenum++;
267 				if (more != NULL)
268 					*more = 1;
269 				return (buf);
270 			}
271 			switch (*s) {
272 			case '\0':
273 				state = ST_EOF;
274 				if (s == script->s) {
275 					script = script->next;
276 					goto again;
277 				} else {
278 					script = script->next;
279 					*p = '\0';
280 					linenum++;
281 					if (more != NULL)
282 						*more = 0;
283 					return (buf);
284 				}
285 			case '\n':
286 				*p++ = '\n';
287 				*p = '\0';
288 				s++;
289 				linenum++;
290 				if (more != NULL)
291 					*more = 0;
292 				return (buf);
293 			default:
294 				*p++ = *s++;
295 			}
296 		}
297 	}
298 	/* NOTREACHED */
299 	return (NULL);
300 }
301 
302 /*
303  * Like fgets, but go through the list of files chaining them together.
304  * Set len to the length of the line.
305  */
306 int
307 mf_fgets(SPACE *sp, enum e_spflag spflag)
308 {
309 	struct stat sb;
310 	size_t len;
311 	char *p;
312 	int c;
313 	static int firstfile;
314 
315 	if (infile == NULL) {
316 		/* stdin? */
317 		if (files->fname == NULL) {
318 			if (inplace != NULL)
319 				errx(1, "-I or -i may not be used with stdin");
320 			infile = stdin;
321 			fname = "stdin";
322 			outfile = stdout;
323 			outfname = "stdout";
324 		}
325 		firstfile = 1;
326 	}
327 
328 	for (;;) {
329 		if (infile != NULL && (c = getc(infile)) != EOF) {
330 			(void)ungetc(c, infile);
331 			break;
332 		}
333 		/* If we are here then either eof or no files are open yet */
334 		if (infile == stdin) {
335 			sp->len = 0;
336 			return (0);
337 		}
338 		if (infile != NULL) {
339 			fclose(infile);
340 			if (*oldfname != '\0') {
341 				if (rename(fname, oldfname) != 0) {
342 					warn("rename()");
343 					unlink(tmpfname);
344 					exit(1);
345 				}
346 				*oldfname = '\0';
347 			}
348 			if (*tmpfname != '\0') {
349 				if (outfile != NULL && outfile != stdout)
350 					fclose(outfile);
351 				outfile = NULL;
352 				rename(tmpfname, fname);
353 				*tmpfname = '\0';
354 			}
355 			outfname = NULL;
356 		}
357 		if (firstfile == 0)
358 			files = files->next;
359 		else
360 			firstfile = 0;
361 		if (files == NULL) {
362 			sp->len = 0;
363 			return (0);
364 		}
365 		fname = files->fname;
366 		if (inplace != NULL) {
367 			if (lstat(fname, &sb) != 0)
368 				err(1, "%s", fname);
369 			if (!(sb.st_mode & S_IFREG))
370 				errx(1, "%s: %s %s", fname,
371 				    "in-place editing only",
372 				    "works for regular files");
373 			if (*inplace != '\0') {
374 				strlcpy(oldfname, fname,
375 				    sizeof(oldfname));
376 				len = strlcat(oldfname, inplace,
377 				    sizeof(oldfname));
378 				if (len > sizeof(oldfname))
379 					errx(1, "%s: name too long", fname);
380 			}
381 			len = snprintf(tmpfname, sizeof(tmpfname),
382 			    "%s/.!%ld!%s", dirname(fname), (long)getpid(),
383 			    basename(fname));
384 			if (len >= sizeof(tmpfname))
385 				errx(1, "%s: name too long", fname);
386 			unlink(tmpfname);
387 			if ((outfile = fopen(tmpfname, "w")) == NULL)
388 				err(1, "%s", fname);
389 			fchown(fileno(outfile), sb.st_uid, sb.st_gid);
390 			fchmod(fileno(outfile), sb.st_mode & ALLPERMS);
391 			outfname = tmpfname;
392 			if (!ispan) {
393 				linenum = 0;
394 				resetstate();
395 			}
396 		} else {
397 			outfile = stdout;
398 			outfname = "stdout";
399 		}
400 		if ((infile = fopen(fname, "r")) == NULL) {
401 			warn("%s", fname);
402 			rval = 1;
403 			continue;
404 		}
405 	}
406 	/*
407 	 * We are here only when infile is open and we still have something
408 	 * to read from it.
409 	 *
410 	 * Use fgetln so that we can handle essentially infinite input data.
411 	 * Can't use the pointer into the stdio buffer as the process space
412 	 * because the ungetc() can cause it to move.
413 	 */
414 	p = fgetln(infile, &len);
415 	if (ferror(infile))
416 		errx(1, "%s: %s", fname, strerror(errno ? errno : EIO));
417 	if (len != 0 && p[len - 1] == '\n')
418 		len--;
419 	cspace(sp, p, len, spflag);
420 
421 	linenum++;
422 
423 	return (1);
424 }
425 
426 /*
427  * Add a compilation unit to the linked list
428  */
429 static void
430 add_compunit(enum e_cut type, char *s)
431 {
432 	struct s_compunit *cu;
433 
434 	if ((cu = malloc(sizeof(struct s_compunit))) == NULL)
435 		err(1, "malloc");
436 	cu->type = type;
437 	cu->s = s;
438 	cu->next = NULL;
439 	*cu_nextp = cu;
440 	cu_nextp = &cu->next;
441 }
442 
443 /*
444  * Add a file to the linked list
445  */
446 static void
447 add_file(char *s)
448 {
449 	struct s_flist *fp;
450 
451 	if ((fp = malloc(sizeof(struct s_flist))) == NULL)
452 		err(1, "malloc");
453 	fp->next = NULL;
454 	*fl_nextp = fp;
455 	fp->fname = s;
456 	fl_nextp = &fp->next;
457 }
458 
459 int
460 lastline(void)
461 {
462 	int ch;
463 
464 	if (files->next != NULL && (inplace == NULL || ispan))
465 		return (0);
466 	if ((ch = getc(infile)) == EOF)
467 		return (1);
468 	ungetc(ch, infile);
469 	return (0);
470 }
471