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
34 #include <sys/cdefs.h>
35 __FBSDID("$FreeBSD$");
38 static const char copyright[] =
39 "@(#) Copyright (c) 1992, 1993\n\
40 The Regents of the University of California. All rights reserved.\n";
44 static const char sccsid[] = "@(#)main.c 8.2 (Berkeley) 1/3/94";
47 #include <sys/types.h>
49 #include <sys/param.h>
69 * Linked list of units (strings and files) to be compiled
72 struct s_compunit *next;
73 enum e_cut {CU_FILE, CU_STRING} type;
74 char *s; /* Pointer to string or fname */
78 * Linked list pointer to compilation units and pointer to current
81 static struct s_compunit *script, **cu_nextp = &script;
84 * Linked list of files to be processed
92 * Linked list pointer to files and pointer to current
95 static struct s_flist *files, **fl_nextp = &files;
97 FILE *infile; /* Current input file */
98 FILE *outfile; /* Current output file */
100 int aflag, eflag, nflag;
102 static int rval; /* Exit status */
104 static int ispan; /* Whether inplace editing spans across files */
107 * Current file and line number; line numbers restart across compilation
108 * units, but span across input files. The latter is optional if editing
111 const char *fname; /* File name. */
112 const char *outfname; /* Output file name */
113 static char oldfname[PATH_MAX]; /* Old file name (for in-place editing) */
114 static char tmpfname[PATH_MAX]; /* Temporary file name (for in-place editing) */
115 static const char *inplace; /* Inplace edit file extension. */
118 static void add_compunit(enum e_cut, char *);
119 static void add_file(char *);
120 static void usage(void);
123 main(int argc, char *argv[])
128 (void) setlocale(LC_ALL, "");
133 while ((c = getopt(argc, argv, "EI:ae:f:i:lnr")) != -1)
135 case 'r': /* Gnu sed compat */
137 rflags = REG_EXTENDED;
141 ispan = 1; /* span across input files */
148 if ((temp_arg = malloc(strlen(optarg) + 2)) == NULL)
150 strcpy(temp_arg, optarg);
151 strcat(temp_arg, "\n");
152 add_compunit(CU_STRING, temp_arg);
156 add_compunit(CU_FILE, optarg);
160 ispan = 0; /* don't span across input files */
163 if(setlinebuf(stdout) != 0)
164 warnx("setlinebuf() failed");
176 /* First usage case; script is the first arg */
177 if (!eflag && !fflag && *argv) {
178 add_compunit(CU_STRING, *argv);
184 /* Continue with first and start second usage */
186 for (; *argv; argv++)
200 (void)fprintf(stderr, "%s\n%s\n",
201 "usage: sed script [-Ealn] [-i extension] [file ...]",
202 " sed [-Ealn] [-i extension] [-e script] ... [-f script_file] ... [file ...]");
207 * Like fgets, but go through the chain of compilation units chaining them
208 * together. Empty strings and files are ignored.
211 cu_fgets(char *buf, int n, int *more)
213 static enum {ST_EOF, ST_FILE, ST_STRING} state = ST_EOF;
214 static FILE *f; /* Current open file */
215 static char *s; /* Current pointer inside string */
216 static char string_ident[30];
222 if (script == NULL) {
228 switch (script->type) {
230 if ((f = fopen(script->s, "r")) == NULL)
231 err(1, "%s", script->s);
236 if (((size_t)snprintf(string_ident,
237 sizeof(string_ident), "\"%s\"", script->s)) >=
238 sizeof(string_ident) - 1)
239 (void)strcpy(string_ident +
240 sizeof(string_ident) - 6, " ...\"");
241 fname = string_ident;
247 if ((p = fgets(buf, n, f)) != NULL) {
249 if (linenum == 1 && buf[0] == '#' && buf[1] == 'n')
255 script = script->next;
260 if (linenum == 0 && s[0] == '#' && s[1] == 'n')
274 if (s == script->s) {
275 script = script->next;
278 script = script->next;
303 * Like fgets, but go through the list of files chaining them together.
304 * Set len to the length of the line.
307 mf_fgets(SPACE *sp, enum e_spflag spflag)
313 static int firstfile;
315 if (infile == NULL) {
317 if (files->fname == NULL) {
319 errx(1, "-I or -i may not be used with stdin");
329 if (infile != NULL && (c = getc(infile)) != EOF) {
330 (void)ungetc(c, infile);
333 /* If we are here then either eof or no files are open yet */
334 if (infile == stdin) {
338 if (infile != NULL) {
340 if (*oldfname != '\0') {
341 /* if there was a backup file, remove it */
344 * Backup the original. Note that hard links
345 * are not supported on all filesystems.
347 if ((link(fname, oldfname) != 0) &&
348 (rename(fname, oldfname) != 0)) {
356 if (*tmpfname != '\0') {
357 if (outfile != NULL && outfile != stdout)
358 if (fclose(outfile) != 0) {
364 if (rename(tmpfname, fname) != 0) {
365 /* this should not happen really! */
382 fname = files->fname;
383 if (inplace != NULL) {
384 if (lstat(fname, &sb) != 0)
386 if (!(sb.st_mode & S_IFREG))
387 errx(1, "%s: %s %s", fname,
388 "in-place editing only",
389 "works for regular files");
390 if (*inplace != '\0') {
391 strlcpy(oldfname, fname,
393 len = strlcat(oldfname, inplace,
395 if (len > sizeof(oldfname))
396 errx(1, "%s: name too long", fname);
398 len = snprintf(tmpfname, sizeof(tmpfname),
399 "%s/.!%ld!%s", dirname(fname), (long)getpid(),
401 if (len >= sizeof(tmpfname))
402 errx(1, "%s: name too long", fname);
404 if ((outfile = fopen(tmpfname, "w")) == NULL)
406 fchown(fileno(outfile), sb.st_uid, sb.st_gid);
407 fchmod(fileno(outfile), sb.st_mode & ALLPERMS);
417 if ((infile = fopen(fname, "r")) == NULL) {
424 * We are here only when infile is open and we still have something
427 * Use fgetln so that we can handle essentially infinite input data.
428 * Can't use the pointer into the stdio buffer as the process space
429 * because the ungetc() can cause it to move.
431 p = fgetln(infile, &len);
433 errx(1, "%s: %s", fname, strerror(errno ? errno : EIO));
434 if (len != 0 && p[len - 1] == '\n')
436 cspace(sp, p, len, spflag);
444 * Add a compilation unit to the linked list
447 add_compunit(enum e_cut type, char *s)
449 struct s_compunit *cu;
451 if ((cu = malloc(sizeof(struct s_compunit))) == NULL)
457 cu_nextp = &cu->next;
461 * Add a file to the linked list
468 if ((fp = malloc(sizeof(struct s_flist))) == NULL)
473 fl_nextp = &fp->next;
481 if (files->next != NULL && (inplace == NULL || ispan))
483 if ((ch = getc(infile)) == EOF)