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