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 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:ln")) != -1)
136 rflags = REG_EXTENDED;
140 ispan = 1; /* span across input files */
147 if ((temp_arg = malloc(strlen(optarg) + 2)) == NULL)
149 strcpy(temp_arg, optarg);
150 strcat(temp_arg, "\n");
151 add_compunit(CU_STRING, temp_arg);
155 add_compunit(CU_FILE, optarg);
159 ispan = 0; /* don't span across input files */
162 if(setlinebuf(stdout) != 0)
163 warnx("setlinebuf() failed");
175 /* First usage case; script is the first arg */
176 if (!eflag && !fflag && *argv) {
177 add_compunit(CU_STRING, *argv);
183 /* Continue with first and start second usage */
185 for (; *argv; argv++)
199 (void)fprintf(stderr, "%s\n%s\n",
200 "usage: sed script [-Ealn] [-i extension] [file ...]",
201 " sed [-Ealn] [-i extension] [-e script] ... [-f script_file] ... [file ...]");
206 * Like fgets, but go through the chain of compilation units chaining them
207 * together. Empty strings and files are ignored.
210 cu_fgets(char *buf, int n, int *more)
212 static enum {ST_EOF, ST_FILE, ST_STRING} state = ST_EOF;
213 static FILE *f; /* Current open file */
214 static char *s; /* Current pointer inside string */
215 static char string_ident[30];
221 if (script == NULL) {
227 switch (script->type) {
229 if ((f = fopen(script->s, "r")) == NULL)
230 err(1, "%s", script->s);
235 if ((snprintf(string_ident,
236 sizeof(string_ident), "\"%s\"", script->s)) >=
237 sizeof(string_ident) - 1)
238 (void)strcpy(string_ident +
239 sizeof(string_ident) - 6, " ...\"");
240 fname = string_ident;
246 if ((p = fgets(buf, n, f)) != NULL) {
248 if (linenum == 1 && buf[0] == '#' && buf[1] == 'n')
254 script = script->next;
259 if (linenum == 0 && s[0] == '#' && s[1] == 'n')
273 if (s == script->s) {
274 script = script->next;
277 script = script->next;
302 * Like fgets, but go through the list of files chaining them together.
303 * Set len to the length of the line.
306 mf_fgets(SPACE *sp, enum e_spflag spflag)
312 static int firstfile;
314 if (infile == NULL) {
316 if (files->fname == NULL) {
318 errx(1, "-I or -i may not be used with stdin");
328 if (infile != NULL && (c = getc(infile)) != EOF) {
329 (void)ungetc(c, infile);
332 /* If we are here then either eof or no files are open yet */
333 if (infile == stdin) {
337 if (infile != NULL) {
339 if (*oldfname != '\0') {
340 if (rename(fname, oldfname) != 0) {
347 if (*tmpfname != '\0') {
348 if (outfile != NULL && outfile != stdout)
351 rename(tmpfname, fname);
364 fname = files->fname;
365 if (inplace != NULL) {
366 if (lstat(fname, &sb) != 0)
368 if (!(sb.st_mode & S_IFREG))
369 errx(1, "%s: %s %s", fname,
370 "in-place editing only",
371 "works for regular files");
372 if (*inplace != '\0') {
373 strlcpy(oldfname, fname,
375 len = strlcat(oldfname, inplace,
377 if (len > sizeof(oldfname))
378 errx(1, "%s: name too long", fname);
380 len = snprintf(tmpfname, sizeof(tmpfname),
381 "%s/.!%ld!%s", dirname(fname), (long)getpid(),
383 if (len >= sizeof(tmpfname))
384 errx(1, "%s: name too long", fname);
386 if ((outfile = fopen(tmpfname, "w")) == NULL)
388 fchown(fileno(outfile), sb.st_uid, sb.st_gid);
389 fchmod(fileno(outfile), sb.st_mode & ALLPERMS);
399 if ((infile = fopen(fname, "r")) == NULL) {
406 * We are here only when infile is open and we still have something
409 * Use fgetln so that we can handle essentially infinite input data.
410 * Can't use the pointer into the stdio buffer as the process space
411 * because the ungetc() can cause it to move.
413 p = fgetln(infile, &len);
415 errx(1, "%s: %s", fname, strerror(errno ? errno : EIO));
416 if (len != 0 && p[len - 1] == '\n')
418 cspace(sp, p, len, spflag);
426 * Add a compilation unit to the linked list
429 add_compunit(enum e_cut type, char *s)
431 struct s_compunit *cu;
433 if ((cu = malloc(sizeof(struct s_compunit))) == NULL)
439 cu_nextp = &cu->next;
443 * Add a file to the linked list
450 if ((fp = malloc(sizeof(struct s_flist))) == NULL)
455 fl_nextp = &fp->next;
463 if (files->next != NULL && (inplace == NULL || ispan))
465 if ((ch = getc(infile)) == EOF)