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