2 * Copyright (c) 2013 Johann 'Myrkraverk' Oskarsson.
3 * Copyright (c) 1992 Diomidis Spinellis.
4 * Copyright (c) 1992, 1993
5 * The Regents of the University of California. All rights reserved.
7 * This code is derived from software contributed to Berkeley by
8 * Diomidis Spinellis of Imperial College, University of London.
10 * Redistribution and use in source and binary forms, with or without
11 * modification, are permitted provided that the following conditions
13 * 1. Redistributions of source code must retain the above copyright
14 * notice, this list of conditions and the following disclaimer.
15 * 2. Redistributions in binary form must reproduce the above copyright
16 * notice, this list of conditions and the following disclaimer in the
17 * documentation and/or other materials provided with the distribution.
18 * 4. Neither the name of the University nor the names of its contributors
19 * may be used to endorse or promote products derived from this software
20 * without specific prior written permission.
22 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
23 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
24 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
25 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
26 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
27 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
28 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
29 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
30 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
31 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
35 #include <sys/cdefs.h>
36 __FBSDID("$FreeBSD$");
39 static const char copyright[] =
40 "@(#) Copyright (c) 1992, 1993\n\
41 The Regents of the University of California. All rights reserved.\n";
45 static const char sccsid[] = "@(#)main.c 8.2 (Berkeley) 1/3/94";
48 #include <sys/types.h>
50 #include <sys/param.h>
71 * Linked list of units (strings and files) to be compiled
74 struct s_compunit *next;
75 enum e_cut {CU_FILE, CU_STRING} type;
76 char *s; /* Pointer to string or fname */
80 * Linked list pointer to compilation units and pointer to current
83 static struct s_compunit *script, **cu_nextp = &script;
86 * Linked list of files to be processed
94 * Linked list pointer to files and pointer to current
97 static struct s_flist *files, **fl_nextp = &files;
99 FILE *infile; /* Current input file */
100 FILE *outfile; /* Current output file */
102 int aflag, eflag, nflag;
104 static int rval; /* Exit status */
106 static int ispan; /* Whether inplace editing spans across files */
109 * Current file and line number; line numbers restart across compilation
110 * units, but span across input files. The latter is optional if editing
113 const char *fname; /* File name. */
114 const char *outfname; /* Output file name */
115 static char oldfname[PATH_MAX]; /* Old file name (for in-place editing) */
116 static char tmpfname[PATH_MAX]; /* Temporary file name (for in-place editing) */
117 static const char *inplace; /* Inplace edit file extension. */
120 static void add_compunit(enum e_cut, char *);
121 static void add_file(char *);
122 static void usage(void);
125 main(int argc, char *argv[])
130 (void) setlocale(LC_ALL, "");
135 while ((c = getopt(argc, argv, "EI:ae:f:i:lnr")) != -1)
137 case 'r': /* Gnu sed compat */
139 rflags = REG_EXTENDED;
143 ispan = 1; /* span across input files */
150 if ((temp_arg = malloc(strlen(optarg) + 2)) == NULL)
152 strcpy(temp_arg, optarg);
153 strcat(temp_arg, "\n");
154 add_compunit(CU_STRING, temp_arg);
158 add_compunit(CU_FILE, optarg);
162 ispan = 0; /* don't span across input files */
165 if(setlinebuf(stdout) != 0)
166 warnx("setlinebuf() failed");
178 /* First usage case; script is the first arg */
179 if (!eflag && !fflag && *argv) {
180 add_compunit(CU_STRING, *argv);
186 /* Continue with first and start second usage */
188 for (; *argv; argv++)
202 (void)fprintf(stderr, "%s\n%s\n",
203 "usage: sed script [-Ealn] [-i extension] [file ...]",
204 " sed [-Ealn] [-i extension] [-e script] ... [-f script_file] ... [file ...]");
209 * Like fgets, but go through the chain of compilation units chaining them
210 * together. Empty strings and files are ignored.
213 cu_fgets(char *buf, int n, int *more)
215 static enum {ST_EOF, ST_FILE, ST_STRING} state = ST_EOF;
216 static FILE *f; /* Current open file */
217 static char *s; /* Current pointer inside string */
218 static char string_ident[30];
224 if (script == NULL) {
230 switch (script->type) {
232 if ((f = fopen(script->s, "r")) == NULL)
233 err(1, "%s", script->s);
238 if (((size_t)snprintf(string_ident,
239 sizeof(string_ident), "\"%s\"", script->s)) >=
240 sizeof(string_ident) - 1)
241 (void)strcpy(string_ident +
242 sizeof(string_ident) - 6, " ...\"");
243 fname = string_ident;
249 if ((p = fgets(buf, n, f)) != NULL) {
251 if (linenum == 1 && buf[0] == '#' && buf[1] == 'n')
257 script = script->next;
262 if (linenum == 0 && s[0] == '#' && s[1] == 'n')
276 if (s == script->s) {
277 script = script->next;
280 script = script->next;
305 * Like fgets, but go through the list of files chaining them together.
306 * Set len to the length of the line.
309 mf_fgets(SPACE *sp, enum e_spflag spflag)
313 static char *p = NULL;
314 static size_t plen = 0;
316 static int firstfile;
318 if (infile == NULL) {
320 if (files->fname == NULL) {
322 errx(1, "-I or -i may not be used with stdin");
332 if (infile != NULL && (c = getc(infile)) != EOF) {
333 (void)ungetc(c, infile);
336 /* If we are here then either eof or no files are open yet */
337 if (infile == stdin) {
341 if (infile != NULL) {
343 if (*oldfname != '\0') {
344 /* if there was a backup file, remove it */
347 * Backup the original. Note that hard links
348 * are not supported on all filesystems.
350 if ((link(fname, oldfname) != 0) &&
351 (rename(fname, oldfname) != 0)) {
359 if (*tmpfname != '\0') {
360 if (outfile != NULL && outfile != stdout)
361 if (fclose(outfile) != 0) {
367 if (rename(tmpfname, fname) != 0) {
368 /* this should not happen really! */
385 fname = files->fname;
386 if (inplace != NULL) {
387 if (lstat(fname, &sb) != 0)
389 if (!(sb.st_mode & S_IFREG))
390 errx(1, "%s: %s %s", fname,
391 "in-place editing only",
392 "works for regular files");
393 if (*inplace != '\0') {
394 strlcpy(oldfname, fname,
396 len = strlcat(oldfname, inplace,
398 if (len > sizeof(oldfname))
399 errx(1, "%s: name too long", fname);
401 len = snprintf(tmpfname, sizeof(tmpfname),
402 "%s/.!%ld!%s", dirname(fname), (long)getpid(),
404 if (len >= sizeof(tmpfname))
405 errx(1, "%s: name too long", fname);
407 if (outfile != NULL && outfile != stdout)
409 if ((outfile = fopen(tmpfname, "w")) == NULL)
411 fchown(fileno(outfile), sb.st_uid, sb.st_gid);
412 fchmod(fileno(outfile), sb.st_mode & ALLPERMS);
422 if ((infile = fopen(fname, "r")) == NULL) {
429 * We are here only when infile is open and we still have something
432 * Use getline() so that we can handle essentially infinite input
433 * data. The p and plen are static so each invocation gives
434 * getline() the same buffer which is expanded as needed.
436 len = getline(&p, &plen, infile);
439 if (len != 0 && p[len - 1] == '\n')
441 cspace(sp, p, len, spflag);
449 * Add a compilation unit to the linked list
452 add_compunit(enum e_cut type, char *s)
454 struct s_compunit *cu;
456 if ((cu = malloc(sizeof(struct s_compunit))) == NULL)
462 cu_nextp = &cu->next;
466 * Add a file to the linked list
473 if ((fp = malloc(sizeof(struct s_flist))) == NULL)
478 fl_nextp = &fp->next;
486 if (files->next != NULL && (inplace == NULL || ispan))
488 if ((ch = getc(infile)) == EOF)