Annotation of src/usr.bin/sed/main.c, Revision 1.26
1.26 ! deraadt 1: /* $OpenBSD: main.c,v 1.25 2015/10/03 00:58:59 deraadt Exp $ */
1.3 deraadt 2:
1.1 deraadt 3: /*-
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.
1.8 millert 19: * 3. Neither the name of the University nor the names of its contributors
1.1 deraadt 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>
1.24 deraadt 37: #include <sys/ioctl.h>
1.19 jasper 38: #include <sys/stat.h>
1.1 deraadt 39:
40: #include <ctype.h>
41: #include <errno.h>
42: #include <fcntl.h>
1.15 millert 43: #include <limits.h>
1.1 deraadt 44: #include <regex.h>
45: #include <stddef.h>
46: #include <stdio.h>
47: #include <stdlib.h>
48: #include <string.h>
49: #include <unistd.h>
1.19 jasper 50: #include <libgen.h>
1.1 deraadt 51:
52: #include "defs.h"
53: #include "extern.h"
54:
55: /*
56: * Linked list of units (strings and files) to be compiled
57: */
58: struct s_compunit {
59: struct s_compunit *next;
60: enum e_cut {CU_FILE, CU_STRING} type;
61: char *s; /* Pointer to string or fname */
62: };
63:
64: /*
65: * Linked list pointer to compilation units and pointer to current
66: * next pointer.
67: */
68: static struct s_compunit *script, **cu_nextp = &script;
69:
70: /*
71: * Linked list of files to be processed
72: */
73: struct s_flist {
74: char *fname;
75: struct s_flist *next;
76: };
77:
78: /*
79: * Linked list pointer to files and pointer to current
80: * next pointer.
81: */
82: static struct s_flist *files, **fl_nextp = &files;
83:
1.19 jasper 84: FILE *infile; /* Current input file */
85: FILE *outfile; /* Current output file */
86:
1.16 djm 87: int Eflag, aflag, eflag, nflag;
1.19 jasper 88: static int rval; /* Exit status */
1.1 deraadt 89:
90: /*
91: * Current file and line number; line numbers restart across compilation
1.19 jasper 92: * units, but span across input files. The latter is optional if editing
93: * in place.
1.1 deraadt 94: */
1.19 jasper 95: const char *fname; /* File name. */
96: const char *outfname; /* Output file name */
97: static char oldfname[PATH_MAX]; /* Old file name (for in-place editing) */
98: static char tmpfname[PATH_MAX]; /* Temporary file name (for in-place editing) */
99: char *inplace; /* Inplace edit file extension */
1.1 deraadt 100: u_long linenum;
101:
1.6 millert 102: static void add_compunit(enum e_cut, char *);
103: static void add_file(char *);
1.21 jasper 104: static int next_files_have_lines(void);
1.1 deraadt 105:
1.24 deraadt 106: int termwidth;
107:
1.1 deraadt 108: int
1.9 deraadt 109: main(int argc, char *argv[])
1.1 deraadt 110: {
1.24 deraadt 111: struct winsize win;
1.1 deraadt 112: int c, fflag;
1.24 deraadt 113: char *p;
1.1 deraadt 114:
115: fflag = 0;
1.19 jasper 116: inplace = NULL;
117: while ((c = getopt(argc, argv, "Eae:f:i::nru")) != -1)
1.1 deraadt 118: switch (c) {
1.16 djm 119: case 'E':
120: case 'r':
121: Eflag = 1;
122: break;
1.1 deraadt 123: case 'a':
124: aflag = 1;
125: break;
126: case 'e':
127: eflag = 1;
128: add_compunit(CU_STRING, optarg);
129: break;
130: case 'f':
131: fflag = 1;
132: add_compunit(CU_FILE, optarg);
133: break;
1.19 jasper 134: case 'i':
135: inplace = optarg ? optarg : "";
136: break;
1.1 deraadt 137: case 'n':
138: nflag = 1;
139: break;
1.11 ray 140: case 'u':
1.18 millert 141: setvbuf(stdout, NULL, _IOLBF, 0);
1.11 ray 142: break;
1.1 deraadt 143: default:
144: case '?':
145: (void)fprintf(stderr,
1.22 jmc 146: "usage: sed [-aEnru] [-i[extension]] command [file ...]\n"
147: " sed [-aEnru] [-e command] [-f command_file] [-i[extension]] [file ...]\n");
1.1 deraadt 148: exit(1);
149: }
150: argc -= optind;
151: argv += optind;
1.24 deraadt 152:
153: if ((p = getenv("COLUMNS")))
154: termwidth = strtonum(p, 0, INT_MAX, NULL);
155: if (termwidth == 0 &&
156: ioctl(STDOUT_FILENO, TIOCGWINSZ, &win) == 0 &&
157: win.ws_col > 0)
158: termwidth = win.ws_col;
159: if (termwidth == 0)
160: termwidth = 60;
1.25 deraadt 161:
162: if (inplace != NULL) {
1.26 ! deraadt 163: if (pledge("stdio wpath rpath cpath fattr", NULL) == -1)
! 164: err(1, "pledge");
1.25 deraadt 165: } else {
1.26 ! deraadt 166: if (pledge("stdio wpath rpath cpath", NULL) == -1)
! 167: err(1, "pledge");
1.25 deraadt 168: }
1.1 deraadt 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(FATAL, "stdout: %s", strerror(errno));
1.19 jasper 188: exit (rval);
1.1 deraadt 189: }
190:
191: /*
192: * Like fgets, but go through the chain of compilation units chaining them
193: * together. Empty strings and files are ignored.
194: */
195: char *
1.15 millert 196: cu_fgets(char **outbuf, size_t *outsize)
1.1 deraadt 197: {
198: static enum {ST_EOF, ST_FILE, ST_STRING} state = ST_EOF;
199: static FILE *f; /* Current open file */
200: static char *s; /* Current pointer inside string */
201: static char string_ident[30];
1.13 millert 202: size_t len;
1.1 deraadt 203: char *p;
204:
1.15 millert 205: if (*outbuf == NULL)
206: *outsize = 0;
207:
1.1 deraadt 208: again:
209: switch (state) {
210: case ST_EOF:
211: if (script == NULL)
212: return (NULL);
213: linenum = 0;
214: switch (script->type) {
215: case CU_FILE:
216: if ((f = fopen(script->s, "r")) == NULL)
217: err(FATAL,
218: "%s: %s", script->s, strerror(errno));
219: fname = script->s;
220: state = ST_FILE;
221: goto again;
222: case CU_STRING:
223: if ((snprintf(string_ident,
224: sizeof(string_ident), "\"%s\"", script->s)) >=
1.5 millert 225: sizeof(string_ident))
1.7 deraadt 226: strlcpy(string_ident +
227: sizeof(string_ident) - 6, " ...\"", 5);
1.1 deraadt 228: fname = string_ident;
229: s = script->s;
230: state = ST_STRING;
231: goto again;
232: }
233: case ST_FILE:
1.13 millert 234: if ((p = fgetln(f, &len)) != NULL) {
1.1 deraadt 235: linenum++;
1.15 millert 236: if (len >= *outsize) {
1.13 millert 237: free(*outbuf);
1.15 millert 238: *outsize = ROUNDLEN(len + 1);
239: *outbuf = xmalloc(*outsize);
1.13 millert 240: }
241: memcpy(*outbuf, p, len);
242: (*outbuf)[len] = '\0';
243: if (linenum == 1 && p[0] == '#' && p[1] == 'n')
1.1 deraadt 244: nflag = 1;
1.13 millert 245: return (*outbuf);
1.1 deraadt 246: }
247: script = script->next;
248: (void)fclose(f);
249: state = ST_EOF;
250: goto again;
251: case ST_STRING:
252: if (linenum == 0 && s[0] == '#' && s[1] == 'n')
253: nflag = 1;
1.13 millert 254: p = *outbuf;
1.15 millert 255: len = *outsize;
1.1 deraadt 256: for (;;) {
1.15 millert 257: if (len <= 1) {
258: *outbuf = xrealloc(*outbuf,
259: *outsize + _POSIX2_LINE_MAX);
260: p = *outbuf + *outsize - len;
261: len += _POSIX2_LINE_MAX;
262: *outsize += _POSIX2_LINE_MAX;
1.1 deraadt 263: }
264: switch (*s) {
265: case '\0':
266: state = ST_EOF;
267: if (s == script->s) {
268: script = script->next;
269: goto again;
270: } else {
271: script = script->next;
272: *p = '\0';
273: linenum++;
1.13 millert 274: return (*outbuf);
1.1 deraadt 275: }
276: case '\n':
277: *p++ = '\n';
278: *p = '\0';
279: s++;
280: linenum++;
1.13 millert 281: return (*outbuf);
1.1 deraadt 282: default:
283: *p++ = *s++;
1.15 millert 284: len--;
1.1 deraadt 285: }
286: }
287: }
288: /* NOTREACHED */
289: }
290:
291: /*
292: * Like fgets, but go through the list of files chaining them together.
293: * Set len to the length of the line.
294: */
295: int
1.9 deraadt 296: mf_fgets(SPACE *sp, enum e_spflag spflag)
1.1 deraadt 297: {
1.19 jasper 298: struct stat sb;
1.1 deraadt 299: size_t len;
1.2 deraadt 300: char *p;
1.19 jasper 301: int c, fd;
302: static int firstfile;
303:
304: if (infile == NULL) {
305: /* stdin? */
306: if (files->fname == NULL) {
307: if (inplace != NULL)
308: err(FATAL, "-i may not be used with stdin");
309: infile = stdin;
310: fname = "stdin";
311: outfile = stdout;
312: outfname = "stdout";
313: }
314:
315: firstfile = 1;
316: }
1.1 deraadt 317:
1.19 jasper 318: for (;;) {
319: if (infile != NULL && (c = getc(infile)) != EOF) {
320: (void)ungetc(c, infile);
321: break;
322: }
323: /* If we are here then either eof or no files are open yet */
324: if (infile == stdin) {
325: sp->len = 0;
326: return (0);
327: }
328: if (infile != NULL) {
329: fclose(infile);
330: if (*oldfname != '\0') {
331: if (rename(fname, oldfname) != 0) {
332: err(WARNING, "rename()");
333: unlink(tmpfname);
334: exit(1);
335: }
336: *oldfname = '\0';
1.1 deraadt 337: }
1.19 jasper 338: if (*tmpfname != '\0') {
339: if (outfile != NULL && outfile != stdout)
340: fclose(outfile);
341: outfile = NULL;
342: rename(tmpfname, fname);
343: *tmpfname = '\0';
1.1 deraadt 344: }
1.19 jasper 345: outfname = NULL;
346: }
347: if (firstfile == 0)
348: files = files->next;
349: else
350: firstfile = 0;
351: if (files == NULL) {
352: sp->len = 0;
353: return (0);
354: }
355: fname = files->fname;
356: if (inplace != NULL) {
357: if (lstat(fname, &sb) != 0)
1.23 sthen 358: err(1, "%s: %s", fname,
359: strerror(errno ? errno : EIO));
1.19 jasper 360: if (!S_ISREG(sb.st_mode))
361: err(FATAL, "%s: %s %s", fname,
362: "in-place editing only",
363: "works for regular files");
364: if (*inplace != '\0') {
365: strlcpy(oldfname, fname,
366: sizeof(oldfname));
367: len = strlcat(oldfname, inplace,
368: sizeof(oldfname));
369: if (len > sizeof(oldfname))
370: err(FATAL, "%s: name too long", fname);
1.1 deraadt 371: }
1.19 jasper 372: len = snprintf(tmpfname, sizeof(tmpfname), "%s/sedXXXXXXXXXX",
373: dirname(fname));
374: if (len >= sizeof(tmpfname))
375: err(FATAL, "%s: name too long", fname);
376: if ((fd = mkstemp(tmpfname)) == -1)
377: err(FATAL, "%s", fname);
378: if ((outfile = fdopen(fd, "w")) == NULL) {
379: unlink(tmpfname);
380: err(FATAL, "%s", fname);
381: }
382: fchown(fileno(outfile), sb.st_uid, sb.st_gid);
383: fchmod(fileno(outfile), sb.st_mode & ALLPERMS);
384: outfname = tmpfname;
385: linenum = 0;
386: resetranges();
387: } else {
388: outfile = stdout;
389: outfname = "stdout";
390: }
391: if ((infile = fopen(fname, "r")) == NULL) {
392: err(WARNING, "%s", fname);
393: rval = 1;
394: continue;
1.1 deraadt 395: }
396: }
397:
398: /*
1.19 jasper 399: * We are here only when infile is open and we still have something
400: * to read from it.
401: *
1.1 deraadt 402: * Use fgetln so that we can handle essentially infinite input data.
403: * Can't use the pointer into the stdio buffer as the process space
404: * because the ungetc() can cause it to move.
405: */
1.19 jasper 406: p = fgetln(infile, &len);
407: if (ferror(infile))
1.1 deraadt 408: err(FATAL, "%s: %s", fname, strerror(errno ? errno : EIO));
1.19 jasper 409: if (len != 0 && p[len - 1] == '\n') {
410: sp->append_newline = 1;
411: len--;
412: } else if (!lastline()) {
413: sp->append_newline = 1;
414: } else {
415: sp->append_newline = 0;
416: }
1.1 deraadt 417: cspace(sp, p, len, spflag);
418:
419: linenum++;
1.19 jasper 420:
1.1 deraadt 421: return (1);
422: }
423:
424: /*
425: * Add a compilation unit to the linked list
426: */
427: static void
1.9 deraadt 428: add_compunit(enum e_cut type, char *s)
1.1 deraadt 429: {
430: struct s_compunit *cu;
431:
432: cu = xmalloc(sizeof(struct s_compunit));
433: cu->type = type;
434: cu->s = s;
435: cu->next = NULL;
436: *cu_nextp = cu;
437: cu_nextp = &cu->next;
438: }
439:
440: /*
441: * Add a file to the linked list
442: */
443: static void
1.9 deraadt 444: add_file(char *s)
1.1 deraadt 445: {
446: struct s_flist *fp;
447:
448: fp = xmalloc(sizeof(struct s_flist));
449: fp->next = NULL;
450: *fl_nextp = fp;
451: fp->fname = s;
452: fl_nextp = &fp->next;
1.19 jasper 453: }
454:
455:
456: static int
457: next_files_have_lines()
458: {
1.20 deraadt 459: struct s_flist *file;
460: FILE *file_fd;
461: int ch;
462:
463: file = files;
464: while ((file = file->next) != NULL) {
465: if ((file_fd = fopen(file->fname, "r")) == NULL)
466: continue;
1.19 jasper 467:
1.20 deraadt 468: if ((ch = getc(file_fd)) != EOF) {
469: /*
470: * This next file has content, therefore current
471: * file doesn't contains the last line.
472: */
473: ungetc(ch, file_fd);
474: fclose(file_fd);
475: return (1);
476: }
477: fclose(file_fd);
478: }
479: return (0);
1.19 jasper 480: }
481:
482: int
483: lastline(void)
484: {
485: int ch;
486:
487: if (feof(infile)) {
488: return !(
489: (inplace == NULL) &&
490: next_files_have_lines());
491: }
492: if ((ch = getc(infile)) == EOF) {
493: return !(
494: (inplace == NULL) &&
495: next_files_have_lines());
496: }
497: ungetc(ch, infile);
498: return (0);
1.1 deraadt 499: }