Annotation of src/usr.bin/sed/main.c, Revision 1.22
1.22 ! jmc 1: /* $OpenBSD: main.c,v 1.21 2015/07/17 21:54:26 jasper 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)
336: err(1, "%s", fname);
337: if (!S_ISREG(sb.st_mode))
338: err(FATAL, "%s: %s %s", fname,
339: "in-place editing only",
340: "works for regular files");
341: if (*inplace != '\0') {
342: strlcpy(oldfname, fname,
343: sizeof(oldfname));
344: len = strlcat(oldfname, inplace,
345: sizeof(oldfname));
346: if (len > sizeof(oldfname))
347: err(FATAL, "%s: name too long", fname);
1.1 deraadt 348: }
1.19 jasper 349: len = snprintf(tmpfname, sizeof(tmpfname), "%s/sedXXXXXXXXXX",
350: dirname(fname));
351: if (len >= sizeof(tmpfname))
352: err(FATAL, "%s: name too long", fname);
353: if ((fd = mkstemp(tmpfname)) == -1)
354: err(FATAL, "%s", fname);
355: if ((outfile = fdopen(fd, "w")) == NULL) {
356: unlink(tmpfname);
357: err(FATAL, "%s", fname);
358: }
359: fchown(fileno(outfile), sb.st_uid, sb.st_gid);
360: fchmod(fileno(outfile), sb.st_mode & ALLPERMS);
361: outfname = tmpfname;
362: linenum = 0;
363: resetranges();
364: } else {
365: outfile = stdout;
366: outfname = "stdout";
367: }
368: if ((infile = fopen(fname, "r")) == NULL) {
369: err(WARNING, "%s", fname);
370: rval = 1;
371: continue;
1.1 deraadt 372: }
373: }
374:
375: /*
1.19 jasper 376: * We are here only when infile is open and we still have something
377: * to read from it.
378: *
1.1 deraadt 379: * Use fgetln so that we can handle essentially infinite input data.
380: * Can't use the pointer into the stdio buffer as the process space
381: * because the ungetc() can cause it to move.
382: */
1.19 jasper 383: p = fgetln(infile, &len);
384: if (ferror(infile))
1.1 deraadt 385: err(FATAL, "%s: %s", fname, strerror(errno ? errno : EIO));
1.19 jasper 386: if (len != 0 && p[len - 1] == '\n') {
387: sp->append_newline = 1;
388: len--;
389: } else if (!lastline()) {
390: sp->append_newline = 1;
391: } else {
392: sp->append_newline = 0;
393: }
1.1 deraadt 394: cspace(sp, p, len, spflag);
395:
396: linenum++;
1.19 jasper 397:
1.1 deraadt 398: return (1);
399: }
400:
401: /*
402: * Add a compilation unit to the linked list
403: */
404: static void
1.9 deraadt 405: add_compunit(enum e_cut type, char *s)
1.1 deraadt 406: {
407: struct s_compunit *cu;
408:
409: cu = xmalloc(sizeof(struct s_compunit));
410: cu->type = type;
411: cu->s = s;
412: cu->next = NULL;
413: *cu_nextp = cu;
414: cu_nextp = &cu->next;
415: }
416:
417: /*
418: * Add a file to the linked list
419: */
420: static void
1.9 deraadt 421: add_file(char *s)
1.1 deraadt 422: {
423: struct s_flist *fp;
424:
425: fp = xmalloc(sizeof(struct s_flist));
426: fp->next = NULL;
427: *fl_nextp = fp;
428: fp->fname = s;
429: fl_nextp = &fp->next;
1.19 jasper 430: }
431:
432:
433: static int
434: next_files_have_lines()
435: {
1.20 deraadt 436: struct s_flist *file;
437: FILE *file_fd;
438: int ch;
439:
440: file = files;
441: while ((file = file->next) != NULL) {
442: if ((file_fd = fopen(file->fname, "r")) == NULL)
443: continue;
1.19 jasper 444:
1.20 deraadt 445: if ((ch = getc(file_fd)) != EOF) {
446: /*
447: * This next file has content, therefore current
448: * file doesn't contains the last line.
449: */
450: ungetc(ch, file_fd);
451: fclose(file_fd);
452: return (1);
453: }
454: fclose(file_fd);
455: }
456: return (0);
1.19 jasper 457: }
458:
459: int
460: lastline(void)
461: {
462: int ch;
463:
464: if (feof(infile)) {
465: return !(
466: (inplace == NULL) &&
467: next_files_have_lines());
468: }
469: if ((ch = getc(infile)) == EOF) {
470: return !(
471: (inplace == NULL) &&
472: next_files_have_lines());
473: }
474: ungetc(ch, infile);
475: return (0);
1.1 deraadt 476: }