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