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