Annotation of src/usr.bin/sed/main.c, Revision 1.34
1.34 ! krw 1: /* $OpenBSD: main.c,v 1.33 2016/07/14 08:31:18 semarie 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:
1.32 bentley 153: termwidth = 0;
154: if ((p = getenv("COLUMNS")) != NULL)
1.24 deraadt 155: termwidth = strtonum(p, 0, INT_MAX, NULL);
1.32 bentley 156: if (termwidth == 0 && ioctl(STDOUT_FILENO, TIOCGWINSZ, &win) == 0 &&
1.24 deraadt 157: win.ws_col > 0)
158: termwidth = win.ws_col;
159: if (termwidth == 0)
1.32 bentley 160: termwidth = 80;
1.25 deraadt 161:
162: if (inplace != NULL) {
1.33 semarie 163: if (pledge("stdio rpath wpath cpath fattr chown", NULL) == -1)
1.30 jca 164: error(FATAL, "pledge: %s", strerror(errno));
1.25 deraadt 165: } else {
1.27 deraadt 166: if (pledge("stdio rpath wpath cpath", NULL) == -1)
1.30 jca 167: error(FATAL, "pledge: %s", strerror(errno));
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))
1.29 mmcc 187: error(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)
1.29 mmcc 217: error(FATAL,
1.1 deraadt 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: }
1.34 ! krw 288:
! 289: return (NULL);
1.1 deraadt 290: }
291:
292: /*
293: * Like fgets, but go through the list of files chaining them together.
294: * Set len to the length of the line.
295: */
296: int
1.9 deraadt 297: mf_fgets(SPACE *sp, enum e_spflag spflag)
1.1 deraadt 298: {
1.19 jasper 299: struct stat sb;
1.1 deraadt 300: size_t len;
1.2 deraadt 301: char *p;
1.19 jasper 302: int c, fd;
303: static int firstfile;
304:
305: if (infile == NULL) {
306: /* stdin? */
307: if (files->fname == NULL) {
308: if (inplace != NULL)
1.29 mmcc 309: error(FATAL, "-i may not be used with stdin");
1.19 jasper 310: infile = stdin;
311: fname = "stdin";
312: outfile = stdout;
313: outfname = "stdout";
314: }
315:
316: firstfile = 1;
317: }
1.1 deraadt 318:
1.19 jasper 319: for (;;) {
320: if (infile != NULL && (c = getc(infile)) != EOF) {
321: (void)ungetc(c, infile);
322: break;
323: }
324: /* If we are here then either eof or no files are open yet */
325: if (infile == stdin) {
326: sp->len = 0;
327: return (0);
328: }
329: if (infile != NULL) {
330: fclose(infile);
331: if (*oldfname != '\0') {
332: if (rename(fname, oldfname) != 0) {
1.34 ! krw 333: warning("rename()");
1.19 jasper 334: unlink(tmpfname);
335: exit(1);
336: }
337: *oldfname = '\0';
1.1 deraadt 338: }
1.19 jasper 339: if (*tmpfname != '\0') {
340: if (outfile != NULL && outfile != stdout)
341: fclose(outfile);
342: outfile = NULL;
343: rename(tmpfname, fname);
344: *tmpfname = '\0';
1.1 deraadt 345: }
1.19 jasper 346: outfname = NULL;
347: }
348: if (firstfile == 0)
349: files = files->next;
350: else
351: firstfile = 0;
352: if (files == NULL) {
353: sp->len = 0;
354: return (0);
355: }
356: fname = files->fname;
357: if (inplace != NULL) {
358: if (lstat(fname, &sb) != 0)
1.30 jca 359: error(FATAL, "%s: %s", fname,
1.23 sthen 360: strerror(errno ? errno : EIO));
1.19 jasper 361: if (!S_ISREG(sb.st_mode))
1.29 mmcc 362: error(FATAL, "%s: %s %s", fname,
1.19 jasper 363: "in-place editing only",
364: "works for regular files");
365: if (*inplace != '\0') {
366: strlcpy(oldfname, fname,
367: sizeof(oldfname));
368: len = strlcat(oldfname, inplace,
369: sizeof(oldfname));
370: if (len > sizeof(oldfname))
1.29 mmcc 371: error(FATAL, "%s: name too long", fname);
1.1 deraadt 372: }
1.19 jasper 373: len = snprintf(tmpfname, sizeof(tmpfname), "%s/sedXXXXXXXXXX",
374: dirname(fname));
375: if (len >= sizeof(tmpfname))
1.29 mmcc 376: error(FATAL, "%s: name too long", fname);
1.19 jasper 377: if ((fd = mkstemp(tmpfname)) == -1)
1.31 tb 378: error(FATAL, "%s: %s", fname, strerror(errno));
1.19 jasper 379: if ((outfile = fdopen(fd, "w")) == NULL) {
380: unlink(tmpfname);
1.29 mmcc 381: error(FATAL, "%s", fname);
1.19 jasper 382: }
383: fchown(fileno(outfile), sb.st_uid, sb.st_gid);
384: fchmod(fileno(outfile), sb.st_mode & ALLPERMS);
385: outfname = tmpfname;
386: linenum = 0;
387: resetranges();
388: } else {
389: outfile = stdout;
390: outfname = "stdout";
391: }
392: if ((infile = fopen(fname, "r")) == NULL) {
1.34 ! krw 393: warning("%s", strerror(errno));
1.19 jasper 394: rval = 1;
395: continue;
1.1 deraadt 396: }
397: }
398:
399: /*
1.19 jasper 400: * We are here only when infile is open and we still have something
401: * to read from it.
402: *
1.1 deraadt 403: * Use fgetln so that we can handle essentially infinite input data.
404: * Can't use the pointer into the stdio buffer as the process space
405: * because the ungetc() can cause it to move.
406: */
1.19 jasper 407: p = fgetln(infile, &len);
408: if (ferror(infile))
1.29 mmcc 409: error(FATAL, "%s: %s", fname, strerror(errno ? errno : EIO));
1.19 jasper 410: if (len != 0 && p[len - 1] == '\n') {
411: sp->append_newline = 1;
412: len--;
413: } else if (!lastline()) {
414: sp->append_newline = 1;
415: } else {
416: sp->append_newline = 0;
417: }
1.1 deraadt 418: cspace(sp, p, len, spflag);
419:
420: linenum++;
1.19 jasper 421:
1.1 deraadt 422: return (1);
423: }
424:
425: /*
426: * Add a compilation unit to the linked list
427: */
428: static void
1.9 deraadt 429: add_compunit(enum e_cut type, char *s)
1.1 deraadt 430: {
431: struct s_compunit *cu;
432:
433: cu = xmalloc(sizeof(struct s_compunit));
434: cu->type = type;
435: cu->s = s;
436: cu->next = NULL;
437: *cu_nextp = cu;
438: cu_nextp = &cu->next;
439: }
440:
441: /*
442: * Add a file to the linked list
443: */
444: static void
1.9 deraadt 445: add_file(char *s)
1.1 deraadt 446: {
447: struct s_flist *fp;
448:
449: fp = xmalloc(sizeof(struct s_flist));
450: fp->next = NULL;
451: *fl_nextp = fp;
452: fp->fname = s;
453: fl_nextp = &fp->next;
1.19 jasper 454: }
455:
456:
457: static int
458: next_files_have_lines()
459: {
1.20 deraadt 460: struct s_flist *file;
461: FILE *file_fd;
462: int ch;
463:
464: file = files;
465: while ((file = file->next) != NULL) {
466: if ((file_fd = fopen(file->fname, "r")) == NULL)
467: continue;
1.19 jasper 468:
1.20 deraadt 469: if ((ch = getc(file_fd)) != EOF) {
470: /*
471: * This next file has content, therefore current
472: * file doesn't contains the last line.
473: */
474: ungetc(ch, file_fd);
475: fclose(file_fd);
476: return (1);
477: }
478: fclose(file_fd);
479: }
480: return (0);
1.19 jasper 481: }
482:
483: int
484: lastline(void)
485: {
486: int ch;
487:
488: if (feof(infile)) {
489: return !(
490: (inplace == NULL) &&
491: next_files_have_lines());
492: }
493: if ((ch = getc(infile)) == EOF) {
494: return !(
495: (inplace == NULL) &&
496: next_files_have_lines());
497: }
498: ungetc(ch, infile);
499: return (0);
1.1 deraadt 500: }