xref: /freebsd/usr.bin/sed/main.c (revision aa0a1e58)
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 there was a backup file, remove it */
342 				unlink(oldfname);
343 				/*
344 				 * Backup the original.  Note that hard links
345 				 * are not supported on all filesystems.
346 				 */
347 				if ((link(fname, oldfname) != 0) &&
348 				   (rename(fname, oldfname) != 0)) {
349 					warn("rename()");
350 					if (*tmpfname)
351 						unlink(tmpfname);
352 					exit(1);
353 				}
354 				*oldfname = '\0';
355 			}
356 			if (*tmpfname != '\0') {
357 				if (outfile != NULL && outfile != stdout)
358 					if (fclose(outfile) != 0) {
359 						warn("fclose()");
360 						unlink(tmpfname);
361 						exit(1);
362 					}
363 				outfile = NULL;
364 				if (rename(tmpfname, fname) != 0) {
365 					/* this should not happen really! */
366 					warn("rename()");
367 					unlink(tmpfname);
368 					exit(1);
369 				}
370 				*tmpfname = '\0';
371 			}
372 			outfname = NULL;
373 		}
374 		if (firstfile == 0)
375 			files = files->next;
376 		else
377 			firstfile = 0;
378 		if (files == NULL) {
379 			sp->len = 0;
380 			return (0);
381 		}
382 		fname = files->fname;
383 		if (inplace != NULL) {
384 			if (lstat(fname, &sb) != 0)
385 				err(1, "%s", fname);
386 			if (!(sb.st_mode & S_IFREG))
387 				errx(1, "%s: %s %s", fname,
388 				    "in-place editing only",
389 				    "works for regular files");
390 			if (*inplace != '\0') {
391 				strlcpy(oldfname, fname,
392 				    sizeof(oldfname));
393 				len = strlcat(oldfname, inplace,
394 				    sizeof(oldfname));
395 				if (len > sizeof(oldfname))
396 					errx(1, "%s: name too long", fname);
397 			}
398 			len = snprintf(tmpfname, sizeof(tmpfname),
399 			    "%s/.!%ld!%s", dirname(fname), (long)getpid(),
400 			    basename(fname));
401 			if (len >= sizeof(tmpfname))
402 				errx(1, "%s: name too long", fname);
403 			unlink(tmpfname);
404 			if ((outfile = fopen(tmpfname, "w")) == NULL)
405 				err(1, "%s", fname);
406 			fchown(fileno(outfile), sb.st_uid, sb.st_gid);
407 			fchmod(fileno(outfile), sb.st_mode & ALLPERMS);
408 			outfname = tmpfname;
409 			if (!ispan) {
410 				linenum = 0;
411 				resetstate();
412 			}
413 		} else {
414 			outfile = stdout;
415 			outfname = "stdout";
416 		}
417 		if ((infile = fopen(fname, "r")) == NULL) {
418 			warn("%s", fname);
419 			rval = 1;
420 			continue;
421 		}
422 	}
423 	/*
424 	 * We are here only when infile is open and we still have something
425 	 * to read from it.
426 	 *
427 	 * Use fgetln so that we can handle essentially infinite input data.
428 	 * Can't use the pointer into the stdio buffer as the process space
429 	 * because the ungetc() can cause it to move.
430 	 */
431 	p = fgetln(infile, &len);
432 	if (ferror(infile))
433 		errx(1, "%s: %s", fname, strerror(errno ? errno : EIO));
434 	if (len != 0 && p[len - 1] == '\n')
435 		len--;
436 	cspace(sp, p, len, spflag);
437 
438 	linenum++;
439 
440 	return (1);
441 }
442 
443 /*
444  * Add a compilation unit to the linked list
445  */
446 static void
447 add_compunit(enum e_cut type, char *s)
448 {
449 	struct s_compunit *cu;
450 
451 	if ((cu = malloc(sizeof(struct s_compunit))) == NULL)
452 		err(1, "malloc");
453 	cu->type = type;
454 	cu->s = s;
455 	cu->next = NULL;
456 	*cu_nextp = cu;
457 	cu_nextp = &cu->next;
458 }
459 
460 /*
461  * Add a file to the linked list
462  */
463 static void
464 add_file(char *s)
465 {
466 	struct s_flist *fp;
467 
468 	if ((fp = malloc(sizeof(struct s_flist))) == NULL)
469 		err(1, "malloc");
470 	fp->next = NULL;
471 	*fl_nextp = fp;
472 	fp->fname = s;
473 	fl_nextp = &fp->next;
474 }
475 
476 int
477 lastline(void)
478 {
479 	int ch;
480 
481 	if (files->next != NULL && (inplace == NULL || ispan))
482 		return (0);
483 	if ((ch = getc(infile)) == EOF)
484 		return (1);
485 	ungetc(ch, infile);
486 	return (0);
487 }
488