xref: /illumos-gate/usr/src/cmd/sed/main.c (revision 7ffba875a0c7cf118aef7a2c9bfd00c3935e230a)
1 /*
2  * Copyright 2010 Nexenta Systems, Inc.  All rights reserved.
3  * Copyright (c) 1992 Diomidis Spinellis.
4  * Copyright (c) 1992, 1993
5  *	The Regents of the University of California.  All rights reserved.
6  *
7  * This code is derived from software contributed to Berkeley by
8  * Diomidis Spinellis of Imperial College, University of London.
9  *
10  * Redistribution and use in source and binary forms, with or without
11  * modification, are permitted provided that the following conditions
12  * are met:
13  * 1. Redistributions of source code must retain the above copyright
14  *    notice, this list of conditions and the following disclaimer.
15  * 2. Redistributions in binary form must reproduce the above copyright
16  *    notice, this list of conditions and the following disclaimer in the
17  *    documentation and/or other materials provided with the distribution.
18  * 4. Neither the name of the University nor the names of its contributors
19  *    may be used to endorse or promote products derived from this software
20  *    without specific prior written permission.
21  *
22  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
23  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
24  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
25  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
26  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
27  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
28  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
29  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
30  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
31  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
32  * SUCH DAMAGE.
33  */
34 
35 #include <sys/types.h>
36 #include <sys/mman.h>
37 #include <sys/param.h>
38 #include <sys/stat.h>
39 
40 #include <err.h>
41 #include <errno.h>
42 #include <fcntl.h>
43 #include <libgen.h>
44 #include <limits.h>
45 #include <locale.h>
46 #include <regex.h>
47 #include <stddef.h>
48 #include <stdio.h>
49 #include <stdlib.h>
50 #include <string.h>
51 #include <unistd.h>
52 #include <libintl.h>
53 
54 #include "defs.h"
55 #include "extern.h"
56 
57 /*
58  * Linked list of units (strings and files) to be compiled
59  */
60 struct s_compunit {
61 	struct s_compunit *next;
62 	enum e_cut {CU_FILE, CU_STRING} type;
63 	char *s;			/* Pointer to string or fname */
64 };
65 
66 /*
67  * Linked list pointer to compilation units and pointer to current
68  * next pointer.
69  */
70 static struct s_compunit *script, **cu_nextp = &script;
71 
72 /*
73  * Linked list of files to be processed
74  */
75 struct s_flist {
76 	char *fname;
77 	struct s_flist *next;
78 };
79 
80 /*
81  * Linked list pointer to files and pointer to current
82  * next pointer.
83  */
84 static struct s_flist *files, **fl_nextp = &files;
85 
86 FILE *infile;			/* Current input file */
87 FILE *outfile;			/* Current output file */
88 
89 int aflag, eflag, nflag;
90 int rflags = 0;
91 static int rval;		/* Exit status */
92 
93 static int ispan;		/* Whether inplace editing spans across files */
94 
95 /*
96  * Current file and line number; line numbers restart across compilation
97  * units, but span across input files.  The latter is optional if editing
98  * in place.
99  */
100 const char *fname;		/* File name. */
101 const char *outfname;		/* Output file name */
102 static char oldfname[PATH_MAX];	/* Old file name (for in-place editing) */
103 static char tmpfname[PATH_MAX];	/* Temporary file name (for in-place editing) */
104 static const char *inplace;	/* Inplace edit file extension. */
105 ulong_t linenum;
106 
107 static void add_compunit(enum e_cut, char *);
108 static void add_file(char *);
109 static void usage(void);
110 static char *getln(FILE *, size_t *);
111 
112 
113 int
114 main(int argc, char *argv[])
115 {
116 	int c, fflag;
117 	char *temp_arg;
118 
119 	(void) setlocale(LC_ALL, "");
120 
121 #ifndef TEXT_DOMAIN
122 #define	TEXT_DOMAIN	"SYS_TEST"
123 #endif
124 	(void) textdomain(TEXT_DOMAIN);
125 
126 	fflag = 0;
127 	inplace = NULL;
128 
129 	while ((c = getopt(argc, argv, "EI:ae:f:i:lnr")) != -1)
130 		switch (c) {
131 		case 'r':		/* Gnu sed compat */
132 		case 'E':
133 			rflags = REG_EXTENDED;
134 			break;
135 		case 'I':
136 			inplace = optarg;
137 			ispan = 1;	/* span across input files */
138 			break;
139 		case 'a':
140 			aflag = 1;
141 			break;
142 		case 'e':
143 			eflag = 1;
144 			if (asprintf(&temp_arg, "%s\n", optarg) <= 1)
145 				err(1, "asprintf");
146 			add_compunit(CU_STRING, temp_arg);
147 			break;
148 		case 'f':
149 			fflag = 1;
150 			add_compunit(CU_FILE, optarg);
151 			break;
152 		case 'i':
153 			inplace = optarg;
154 			ispan = 0;	/* don't span across input files */
155 			break;
156 		case 'l':
157 			/* On SunOS, setlinebuf "returns no useful value */
158 			(void) setlinebuf(stdout);
159 			break;
160 		case 'n':
161 			nflag = 1;
162 			break;
163 		default:
164 		case '?':
165 			usage();
166 		}
167 	argc -= optind;
168 	argv += optind;
169 
170 	/* First usage case; script is the first arg */
171 	if (!eflag && !fflag && *argv) {
172 		add_compunit(CU_STRING, *argv);
173 		argv++;
174 	}
175 
176 	compile();
177 
178 	/* Continue with first and start second usage */
179 	if (*argv)
180 		for (; *argv; argv++)
181 			add_file(*argv);
182 	else
183 		add_file(NULL);
184 	process();
185 	cfclose(prog, NULL);
186 	if (fclose(stdout))
187 		err(1, "stdout");
188 	return (rval);
189 }
190 
191 static void
192 usage(void)
193 {
194 	(void) fputs(_("usage: sed script [-Ealn] [-i extension] [file...]\n"
195 	    "       sed [-Ealn] [-i extension] [-e script]... "
196 	    "[-f script_file]... [file...]\n"),
197 	    stderr);
198 	exit(1);
199 }
200 
201 /*
202  * Like fgets, but go through the chain of compilation units chaining them
203  * together.  Empty strings and files are ignored.
204  */
205 char *
206 cu_fgets(char *buf, int n, int *more)
207 {
208 	static enum {ST_EOF, ST_FILE, ST_STRING} state = ST_EOF;
209 	static FILE *f;		/* Current open file */
210 	static char *s;		/* Current pointer inside string */
211 	static char string_ident[30];
212 	char *p;
213 
214 again:
215 	switch (state) {
216 	case ST_EOF:
217 		if (script == NULL) {
218 			if (more != NULL)
219 				*more = 0;
220 			return (NULL);
221 		}
222 		linenum = 0;
223 		switch (script->type) {
224 		case CU_FILE:
225 			if ((f = fopen(script->s, "r")) == NULL)
226 				err(1, "%s", script->s);
227 			fname = script->s;
228 			state = ST_FILE;
229 			goto again;
230 		case CU_STRING:
231 			if (((size_t)snprintf(string_ident,
232 			    sizeof (string_ident), "\"%s\"", script->s)) >=
233 			    sizeof (string_ident) - 1)
234 				(void) strcpy(string_ident +
235 				    sizeof (string_ident) - 6, " ...\"");
236 			fname = string_ident;
237 			s = script->s;
238 			state = ST_STRING;
239 			goto again;
240 		}
241 		/*NOTREACHED*/
242 
243 	case ST_FILE:
244 		if ((p = fgets(buf, n, f)) != NULL) {
245 			linenum++;
246 			if (linenum == 1 && buf[0] == '#' && buf[1] == 'n')
247 				nflag = 1;
248 			if (more != NULL)
249 				*more = !feof(f);
250 			return (p);
251 		}
252 		script = script->next;
253 		(void) fclose(f);
254 		state = ST_EOF;
255 		goto again;
256 	case ST_STRING:
257 		if (linenum == 0 && s[0] == '#' && s[1] == 'n')
258 			nflag = 1;
259 		p = buf;
260 		for (;;) {
261 			if (n-- <= 1) {
262 				*p = '\0';
263 				linenum++;
264 				if (more != NULL)
265 					*more = 1;
266 				return (buf);
267 			}
268 			switch (*s) {
269 			case '\0':
270 				state = ST_EOF;
271 				if (s == script->s) {
272 					script = script->next;
273 					goto again;
274 				} else {
275 					script = script->next;
276 					*p = '\0';
277 					linenum++;
278 					if (more != NULL)
279 						*more = 0;
280 					return (buf);
281 				}
282 			case '\n':
283 				*p++ = '\n';
284 				*p = '\0';
285 				s++;
286 				linenum++;
287 				if (more != NULL)
288 					*more = 0;
289 				return (buf);
290 			default:
291 				*p++ = *s++;
292 			}
293 		}
294 	}
295 	/* NOTREACHED */
296 	return (NULL);
297 }
298 
299 /*
300  * Like fgets, but go through the list of files chaining them together.
301  * Set len to the length of the line.
302  */
303 int
304 mf_fgets(SPACE *sp, enum e_spflag spflag)
305 {
306 	struct stat sb, nsb;
307 	size_t len;
308 	char *p;
309 	int c;
310 	static int firstfile;
311 
312 	if (infile == NULL) {
313 		/* stdin? */
314 		if (files->fname == NULL) {
315 			if (inplace != NULL)
316 				errx(1,
317 				    _("-I or -i may not be used with stdin"));
318 			infile = stdin;
319 			fname = "stdin";
320 			outfile = stdout;
321 			outfname = "stdout";
322 		}
323 		firstfile = 1;
324 	}
325 
326 	for (;;) {
327 		if (infile != NULL && (c = getc(infile)) != EOF) {
328 			(void) ungetc(c, infile);
329 			break;
330 		}
331 		/* If we are here then either eof or no files are open yet */
332 		if (infile == stdin) {
333 			sp->len = 0;
334 			return (0);
335 		}
336 		if (infile != NULL) {
337 			(void) fclose(infile);
338 			if (*oldfname != '\0') {
339 				/* if there was a backup file, remove it */
340 				(void) unlink(oldfname);
341 				/*
342 				 * Backup the original.  Note that hard links
343 				 * are not supported on all filesystems.
344 				 */
345 				if ((link(fname, oldfname) != 0) &&
346 				    (rename(fname, oldfname) != 0)) {
347 					warn("rename()");
348 					if (*tmpfname)
349 						(void) unlink(tmpfname);
350 					exit(1);
351 				}
352 				*oldfname = '\0';
353 			}
354 			if (*tmpfname != '\0') {
355 				if (outfile != NULL && outfile != stdout)
356 					if (fclose(outfile) != 0) {
357 						warn("fclose()");
358 						(void) unlink(tmpfname);
359 						exit(1);
360 					}
361 				outfile = NULL;
362 				if (rename(tmpfname, fname) != 0) {
363 					/* this should not happen really! */
364 					warn("rename()");
365 					(void) unlink(tmpfname);
366 					exit(1);
367 				}
368 				*tmpfname = '\0';
369 			}
370 			outfname = NULL;
371 		}
372 		if (firstfile == 0)
373 			files = files->next;
374 		else
375 			firstfile = 0;
376 		if (files == NULL) {
377 			sp->len = 0;
378 			return (0);
379 		}
380 		fname = files->fname;
381 		if (inplace != NULL) {
382 			char bn[PATH_MAX];
383 			char dn[PATH_MAX];
384 			(void) strlcpy(bn, fname, sizeof (bn));
385 			(void) strlcpy(dn, fname, sizeof (dn));
386 			if (lstat(fname, &sb) != 0)
387 				err(1, "%s", fname);
388 			if (!(sb.st_mode & S_IFREG))
389 				fatal(_("in-place editing only "
390 				    "works for regular files"));
391 			if (*inplace != '\0') {
392 				(void) strlcpy(oldfname, fname,
393 				    sizeof (oldfname));
394 				len = strlcat(oldfname, inplace,
395 				    sizeof (oldfname));
396 				if (len > sizeof (oldfname))
397 					fatal(_("name too long"));
398 			}
399 			len = snprintf(tmpfname, sizeof (tmpfname),
400 			    "%s/.!%ld!%s", dirname(dn), (long)getpid(),
401 			    basename(bn));
402 			if (len >= sizeof (tmpfname))
403 				fatal(_("name too long"));
404 			(void) unlink(tmpfname);
405 			if ((outfile = fopen(tmpfname, "w")) == NULL)
406 				err(1, "%s", fname);
407 			/*
408 			 * Some file systems don't support chown or
409 			 * chmod fully.  On those, the owner/group and
410 			 * permissions will already be set to what
411 			 * they need to be.
412 			 */
413 			if (fstat(fileno(outfile), &nsb) != 0) {
414 				warn("fstat()");
415 			}
416 			if (((sb.st_uid != nsb.st_uid) ||
417 			    (sb.st_gid != nsb.st_gid)) &&
418 			    (fchown(fileno(outfile), sb.st_uid, sb.st_gid)
419 			    != 0))
420 				warn("fchown()");
421 			if ((sb.st_mode != nsb.st_mode) &&
422 			    (fchmod(fileno(outfile), sb.st_mode & 07777) != 0))
423 				warn("fchmod()");
424 			outfname = tmpfname;
425 			if (!ispan) {
426 				linenum = 0;
427 				resetstate();
428 			}
429 		} else {
430 			outfile = stdout;
431 			outfname = "stdout";
432 		}
433 		if ((infile = fopen(fname, "r")) == NULL) {
434 			warn("%s", fname);
435 			rval = 1;
436 			continue;
437 		}
438 	}
439 	/*
440 	 * We are here only when infile is open and we still have something
441 	 * to read from it.
442 	 *
443 	 * Use fgetln so that we can handle essentially infinite input data.
444 	 * Can't use the pointer into the stdio buffer as the process space
445 	 * because the ungetc() can cause it to move.
446 	 */
447 	p = getln(infile, &len);
448 	if (ferror(infile))
449 		errx(1, "%s: %s", fname, strerror(errno ? errno : EIO));
450 	if (len != 0 && p[len - 1] == '\n')
451 		len--;
452 	cspace(sp, p, len, spflag);
453 
454 	linenum++;
455 
456 	return (1);
457 }
458 
459 /*
460  * Add a compilation unit to the linked list
461  */
462 static void
463 add_compunit(enum e_cut type, char *s)
464 {
465 	struct s_compunit *cu;
466 
467 	if ((cu = malloc(sizeof (struct s_compunit))) == NULL)
468 		err(1, "malloc");
469 	cu->type = type;
470 	cu->s = s;
471 	cu->next = NULL;
472 	*cu_nextp = cu;
473 	cu_nextp = &cu->next;
474 }
475 
476 /*
477  * Add a file to the linked list
478  */
479 static void
480 add_file(char *s)
481 {
482 	struct s_flist *fp;
483 
484 	if ((fp = malloc(sizeof (struct s_flist))) == NULL)
485 		err(1, "malloc");
486 	fp->next = NULL;
487 	*fl_nextp = fp;
488 	fp->fname = s;
489 	fl_nextp = &fp->next;
490 }
491 
492 int
493 lastline(void)
494 {
495 	int ch;
496 
497 	if (files->next != NULL && (inplace == NULL || ispan))
498 		return (0);
499 	if ((ch = getc(infile)) == EOF)
500 		return (1);
501 	(void) ungetc(ch, infile);
502 	return (0);
503 }
504 
505 char *
506 getln(FILE *in, size_t *lenp)
507 {
508 	static char	*buffer = NULL;
509 	static size_t	sz = 0;
510 
511 	size_t		len = 0;
512 
513 	for (;;) {
514 		if (sz <= (len + 1)) {
515 			char *nb;
516 			if ((nb = realloc(buffer, sz + LINE_MAX)) == NULL) {
517 				err(1, "realloc");
518 			}
519 			buffer = nb;
520 			sz += LINE_MAX;
521 		}
522 
523 		buffer[len] = 0;
524 
525 		if (fgets(buffer + len, sz - len, in) == NULL) {
526 			/* END OF FILE */
527 			*lenp = len;
528 			break;
529 		}
530 
531 		len += strlen(buffer + len);
532 
533 		if (buffer[len - 1] == '\n') {
534 			/* got the new line */
535 			*lenp = len;
536 			break;
537 		}
538 	}
539 
540 	return (buffer);
541 }
542