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 * 3. All advertising materials mentioning features or use of this software
18 * must display the following acknowledgement:
19 * This product includes software developed by the University of
20 * California, Berkeley and its contributors.
21 * 4. Neither the name of the University nor the names of its contributors
22 * may be used to endorse or promote products derived from this software
23 * without specific prior written permission.
25 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
26 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
27 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
28 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
29 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
30 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
31 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
32 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
33 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
34 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
37 * @(#) Copyright (c) 1992, 1993 The Regents of the University of California. All rights reserved.
38 * @(#)main.c 8.2 (Berkeley) 1/3/94
39 * $FreeBSD: src/usr.bin/sed/main.c,v 1.9.2.7 2002/08/06 10:03:29 fanf Exp $
40 * $DragonFly: src/usr.bin/sed/main.c,v 1.2 2003/06/17 04:29:31 dillon Exp $
43 #include <sys/types.h>
45 #include <sys/param.h>
63 * Linked list of units (strings and files) to be compiled
66 struct s_compunit *next;
67 enum e_cut {CU_FILE, CU_STRING} type;
68 char *s; /* Pointer to string or fname */
72 * Linked list pointer to compilation units and pointer to current
75 static struct s_compunit *script, **cu_nextp = &script;
78 * Linked list of files to be processed
86 * Linked list pointer to files and pointer to current
89 static struct s_flist *files, **fl_nextp = &files;
91 static FILE *curfile; /* Current open file */
93 int aflag, eflag, nflag;
95 static int rval; /* Exit status */
98 * Current file and line number; line numbers restart across compilation
99 * units, but span across input files.
101 const char *fname; /* File name. */
102 const char *inplace; /* Inplace edit file extension. */
105 static void add_compunit(enum e_cut, char *);
106 static void add_file(char *);
107 static int inplace_edit(char **);
108 static void usage(void);
118 (void) setlocale(LC_ALL, "");
123 while ((c = getopt(argc, argv, "Eae:f:i:n")) != -1)
126 rflags = REG_EXTENDED;
133 if ((temp_arg = malloc(strlen(optarg) + 2)) == NULL)
135 strcpy(temp_arg, optarg);
136 strcat(temp_arg, "\n");
137 add_compunit(CU_STRING, temp_arg);
141 add_compunit(CU_FILE, optarg);
156 /* First usage case; script is the first arg */
157 if (!eflag && !fflag && *argv) {
158 add_compunit(CU_STRING, *argv);
164 /* Continue with first and start second usage */
166 for (; *argv; argv++)
180 (void)fprintf(stderr, "%s\n%s\n",
181 "usage: sed script [-Ean] [-i extension] [file ...]",
182 " sed [-an] [-i extension] [-e script] ... [-f script_file] ... [file ...]");
187 * Like fgets, but go through the chain of compilation units chaining them
188 * together. Empty strings and files are ignored.
191 cu_fgets(buf, n, more)
196 static enum {ST_EOF, ST_FILE, ST_STRING} state = ST_EOF;
197 static FILE *f; /* Current open file */
198 static char *s; /* Current pointer inside string */
199 static char string_ident[30];
205 if (script == NULL) {
211 switch (script->type) {
213 if ((f = fopen(script->s, "r")) == NULL)
214 err(1, "%s", script->s);
219 if ((snprintf(string_ident,
220 sizeof(string_ident), "\"%s\"", script->s)) >=
221 sizeof(string_ident) - 1)
222 (void)strcpy(string_ident +
223 sizeof(string_ident) - 6, " ...\"");
224 fname = string_ident;
230 if ((p = fgets(buf, n, f)) != NULL) {
232 if (linenum == 1 && buf[0] == '#' && buf[1] == 'n')
238 script = script->next;
243 if (linenum == 0 && s[0] == '#' && s[1] == 'n')
257 if (s == script->s) {
258 script = script->next;
261 script = script->next;
286 * Like fgets, but go through the list of files chaining them together.
287 * Set len to the length of the line.
292 enum e_spflag spflag;
297 static int firstfile;
299 if (curfile == NULL) {
301 if (files->fname == NULL) {
303 errx(1, "-i may not be used with stdin");
311 if (curfile != NULL && (c = getc(curfile)) != EOF) {
312 (void)ungetc(c, curfile);
315 /* If we are here then either eof or no files are open yet */
316 if (curfile == stdin) {
320 if (curfile != NULL) {
323 if (firstfile == 0) {
331 if (inplace != NULL) {
332 if (inplace_edit(&files->fname) == -1)
335 fname = files->fname;
336 if ((curfile = fopen(fname, "r")) == NULL) {
341 if (inplace != NULL && *inplace == '\0')
345 * We are here only when curfile is open and we still have something
348 * Use fgetln so that we can handle essentially infinite input data.
349 * Can't use the pointer into the stdio buffer as the process space
350 * because the ungetc() can cause it to move.
352 p = fgetln(curfile, &len);
354 errx(1, "%s: %s", fname, strerror(errno ? errno : EIO));
355 if (len != 0 && p[len - 1] == '\n')
357 cspace(sp, p, len, spflag);
365 * Add a compilation unit to the linked list
368 add_compunit(type, s)
372 struct s_compunit *cu;
374 if ((cu = malloc(sizeof(struct s_compunit))) == NULL)
380 cu_nextp = &cu->next;
384 * Add a file to the linked list
392 if ((fp = malloc(sizeof(struct s_flist))) == NULL)
397 fl_nextp = &fp->next;
401 * Modify a pointer to a filename for inplace editing and reopen stdout
404 inplace_edit(filename)
408 char backup[MAXPATHLEN];
410 if (lstat(*filename, &orig) == -1)
412 if ((orig.st_mode & S_IFREG) == 0) {
413 warnx("cannot inplace edit %s, not a regular file", *filename);
417 if (*inplace == '\0') {
419 * This is a bit of a hack: we use mkstemp() to avoid the
420 * mktemp() link-time warning, although mktemp() would fit in
421 * this context much better. We're only interested in getting
422 * a name for use in the rename(); there aren't any security
423 * issues here that don't already exist in relation to the
424 * original file and its directory.
427 strlcpy(backup, *filename, sizeof(backup));
428 strlcat(backup, ".XXXXXXXXXX", sizeof(backup));
429 fd = mkstemp(backup);
431 errx(1, "could not create backup of %s", *filename);
435 strlcpy(backup, *filename, sizeof(backup));
436 strlcat(backup, inplace, sizeof(backup));
439 if (rename(*filename, backup) == -1)
440 err(1, "rename(\"%s\", \"%s\")", *filename, backup);
441 if (freopen(*filename, "w", stdout) == NULL)
442 err(1, "open(\"%s\")", *filename);
443 if (fchmod(fileno(stdout), orig.st_mode) == -1)
444 err(1, "chmod(\"%s\")", *filename);
445 *filename = strdup(backup);
446 if (*filename == NULL)
456 if (files->next != NULL)
458 if ((ch = getc(curfile)) == EOF)