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