2 * Copyright (c) 1992 Diomidis Spinellis.
3 * Copyright (c) 1992, 1993
4 * The Regents of the University of California. All rights reserved.
6 * This code is derived from software contributed to Berkeley by
7 * Diomidis Spinellis of Imperial College, University of London.
9 * Redistribution and use in source and binary forms, with or without
10 * modification, are permitted provided that the following conditions
12 * 1. Redistributions of source code must retain the above copyright
13 * notice, this list of conditions and the following disclaimer.
14 * 2. Redistributions in binary form must reproduce the above copyright
15 * notice, this list of conditions and the following disclaimer in the
16 * documentation and/or other materials provided with the distribution.
17 * 4. Neither the name of the University nor the names of its contributors
18 * may be used to endorse or promote products derived from this software
19 * without specific prior written permission.
21 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
22 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
23 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
24 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
25 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
26 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
27 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
28 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
29 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
30 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
33 * @(#) Copyright (c) 1992, 1993 The Regents of the University of California. All rights reserved.
34 * @(#)main.c 8.2 (Berkeley) 1/3/94
35 * $FreeBSD: src/usr.bin/sed/main.c,v 1.41 2008/02/09 09:12:02 dwmalone Exp $
36 * $DragonFly: src/usr.bin/sed/main.c,v 1.4 2008/04/08 13:23:38 swildner Exp $
39 #include <sys/types.h>
41 #include <sys/param.h>
61 * Linked list of units (strings and files) to be compiled
64 struct s_compunit *next;
65 enum e_cut {CU_FILE, CU_STRING} type;
66 char *s; /* Pointer to string or fname */
70 * Linked list pointer to compilation units and pointer to current
73 static struct s_compunit *script, **cu_nextp = &script;
76 * Linked list of files to be processed
84 * Linked list pointer to files and pointer to current
87 static struct s_flist *files, **fl_nextp = &files;
89 FILE *infile; /* Current input file */
90 FILE *outfile; /* Current output file */
92 int aflag, eflag, nflag;
94 static int rval; /* Exit status */
96 static int ispan; /* Whether inplace editing spans across files */
99 * Current file and line number; line numbers restart across compilation
100 * units, but span across input files. The latter is optional if editing
103 const char *fname; /* File name. */
104 const char *outfname; /* Output file name */
105 static char oldfname[PATH_MAX]; /* Old file name (for in-place editing) */
106 static char tmpfname[PATH_MAX]; /* Temporary file name (for in-place editing) */
107 static const char *inplace; /* Inplace edit file extension. */
110 static void add_compunit(enum e_cut, char *);
111 static void add_file(char *);
112 static void usage(void);
115 main(int argc, char *argv[])
120 (void) setlocale(LC_ALL, "");
125 while ((c = getopt(argc, argv, "EI:ae:f:i:ln")) != -1)
128 rflags = REG_EXTENDED;
132 ispan = 1; /* span across input files */
139 if ((temp_arg = malloc(strlen(optarg) + 2)) == NULL)
141 strcpy(temp_arg, optarg);
142 strcat(temp_arg, "\n");
143 add_compunit(CU_STRING, temp_arg);
147 add_compunit(CU_FILE, optarg);
151 ispan = 0; /* don't span across input files */
154 if(setlinebuf(stdout) != 0)
155 warnx("setlinebuf() failed");
167 /* First usage case; script is the first arg */
168 if (!eflag && !fflag && *argv) {
169 add_compunit(CU_STRING, *argv);
175 /* Continue with first and start second usage */
177 for (; *argv; argv++)
191 (void)fprintf(stderr, "%s\n%s\n",
192 "usage: sed script [-Ealn] [-i extension] [file ...]",
193 " sed [-Ealn] [-i extension] [-e script] ... [-f script_file] ... [file ...]");
198 * Like fgets, but go through the chain of compilation units chaining them
199 * together. Empty strings and files are ignored.
202 cu_fgets(char *buf, int n, int *more)
204 static enum {ST_EOF, ST_FILE, ST_STRING} state = ST_EOF;
205 static FILE *f; /* Current open file */
206 static char *s; /* Current pointer inside string */
207 static char string_ident[30];
213 if (script == NULL) {
219 switch (script->type) {
221 if ((f = fopen(script->s, "r")) == NULL)
222 err(1, "%s", script->s);
227 if (((size_t)snprintf(string_ident,
228 sizeof(string_ident), "\"%s\"", script->s)) >=
229 sizeof(string_ident) - 1)
230 (void)strcpy(string_ident +
231 sizeof(string_ident) - 6, " ...\"");
232 fname = string_ident;
238 if ((p = fgets(buf, n, f)) != NULL) {
240 if (linenum == 1 && buf[0] == '#' && buf[1] == 'n')
246 script = script->next;
251 if (linenum == 0 && s[0] == '#' && s[1] == 'n')
265 if (s == script->s) {
266 script = script->next;
269 script = script->next;
294 * Like fgets, but go through the list of files chaining them together.
295 * Set len to the length of the line.
298 mf_fgets(SPACE *sp, enum e_spflag spflag)
304 static int firstfile;
306 if (infile == NULL) {
308 if (files->fname == NULL) {
310 errx(1, "-I or -i may not be used with stdin");
320 if (infile != NULL && (c = getc(infile)) != EOF) {
321 (void)ungetc(c, infile);
324 /* If we are here then either eof or no files are open yet */
325 if (infile == stdin) {
329 if (infile != NULL) {
331 if (*oldfname != '\0') {
332 if (rename(fname, oldfname) != 0) {
339 if (*tmpfname != '\0') {
340 if (outfile != NULL && outfile != stdout)
343 rename(tmpfname, fname);
356 fname = files->fname;
357 if (inplace != NULL) {
358 if (lstat(fname, &sb) != 0)
360 if (!(sb.st_mode & S_IFREG))
361 errx(1, "%s: %s %s", fname,
362 "in-place editing only",
363 "works for regular files");
364 if (*inplace != '\0') {
365 strlcpy(oldfname, fname,
367 len = strlcat(oldfname, inplace,
369 if (len > sizeof(oldfname))
370 errx(1, "%s: name too long", fname);
372 len = snprintf(tmpfname, sizeof(tmpfname),
373 "%s/.!%ld!%s", dirname(fname), (long)getpid(),
375 if (len >= sizeof(tmpfname))
376 errx(1, "%s: name too long", fname);
378 if ((outfile = fopen(tmpfname, "w")) == NULL)
380 fchown(fileno(outfile), sb.st_uid, sb.st_gid);
381 fchmod(fileno(outfile), sb.st_mode & ALLPERMS);
391 if ((infile = fopen(fname, "r")) == NULL) {
398 * We are here only when infile is open and we still have something
401 * Use fgetln so that we can handle essentially infinite input data.
402 * Can't use the pointer into the stdio buffer as the process space
403 * because the ungetc() can cause it to move.
405 p = fgetln(infile, &len);
407 errx(1, "%s: %s", fname, strerror(errno ? errno : EIO));
408 if (len != 0 && p[len - 1] == '\n')
410 cspace(sp, p, len, spflag);
418 * Add a compilation unit to the linked list
421 add_compunit(enum e_cut type, char *s)
423 struct s_compunit *cu;
425 if ((cu = malloc(sizeof(struct s_compunit))) == NULL)
431 cu_nextp = &cu->next;
435 * Add a file to the linked list
442 if ((fp = malloc(sizeof(struct s_flist))) == NULL)
447 fl_nextp = &fp->next;
455 if (files->next != NULL && (inplace == NULL || ispan))
457 if ((ch = getc(infile)) == EOF)