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.3 2003/10/04 20:36:50 hmp 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);
111 main(int argc
, char **argv
)
116 (void) setlocale(LC_ALL
, "");
121 while ((c
= getopt(argc
, argv
, "Eae:f:i:n")) != -1)
124 rflags
= REG_EXTENDED
;
131 if ((temp_arg
= malloc(strlen(optarg
) + 2)) == NULL
)
133 strcpy(temp_arg
, optarg
);
134 strcat(temp_arg
, "\n");
135 add_compunit(CU_STRING
, temp_arg
);
139 add_compunit(CU_FILE
, optarg
);
154 /* First usage case; script is the first arg */
155 if (!eflag
&& !fflag
&& *argv
) {
156 add_compunit(CU_STRING
, *argv
);
162 /* Continue with first and start second usage */
164 for (; *argv
; argv
++)
178 (void)fprintf(stderr
, "%s\n%s\n",
179 "usage: sed script [-Ean] [-i extension] [file ...]",
180 " sed [-an] [-i extension] [-e script] ... [-f script_file] ... [file ...]");
185 * Like fgets, but go through the chain of compilation units chaining them
186 * together. Empty strings and files are ignored.
189 cu_fgets(char *buf
, int n
, int *more
)
191 static enum {ST_EOF
, ST_FILE
, ST_STRING
} state
= ST_EOF
;
192 static FILE *f
; /* Current open file */
193 static char *s
; /* Current pointer inside string */
194 static char string_ident
[30];
200 if (script
== NULL
) {
206 switch (script
->type
) {
208 if ((f
= fopen(script
->s
, "r")) == NULL
)
209 err(1, "%s", script
->s
);
214 if ((snprintf(string_ident
,
215 sizeof(string_ident
), "\"%s\"", script
->s
)) >=
216 sizeof(string_ident
) - 1)
217 (void)strcpy(string_ident
+
218 sizeof(string_ident
) - 6, " ...\"");
219 fname
= string_ident
;
225 if ((p
= fgets(buf
, n
, f
)) != NULL
) {
227 if (linenum
== 1 && buf
[0] == '#' && buf
[1] == 'n')
233 script
= script
->next
;
238 if (linenum
== 0 && s
[0] == '#' && s
[1] == 'n')
252 if (s
== script
->s
) {
253 script
= script
->next
;
256 script
= script
->next
;
281 * Like fgets, but go through the list of files chaining them together.
282 * Set len to the length of the line.
285 mf_fgets(SPACE
*sp
, enum e_spflag spflag
)
290 static int firstfile
;
292 if (curfile
== NULL
) {
294 if (files
->fname
== NULL
) {
296 errx(1, "-i may not be used with stdin");
304 if (curfile
!= NULL
&& (c
= getc(curfile
)) != EOF
) {
305 (void)ungetc(c
, curfile
);
308 /* If we are here then either eof or no files are open yet */
309 if (curfile
== stdin
) {
313 if (curfile
!= NULL
) {
316 if (firstfile
== 0) {
324 if (inplace
!= NULL
) {
325 if (inplace_edit(&files
->fname
) == -1)
328 fname
= files
->fname
;
329 if ((curfile
= fopen(fname
, "r")) == NULL
) {
334 if (inplace
!= NULL
&& *inplace
== '\0')
338 * We are here only when curfile is open and we still have something
341 * Use fgetln so that we can handle essentially infinite input data.
342 * Can't use the pointer into the stdio buffer as the process space
343 * because the ungetc() can cause it to move.
345 p
= fgetln(curfile
, &len
);
347 errx(1, "%s: %s", fname
, strerror(errno
? errno
: EIO
));
348 if (len
!= 0 && p
[len
- 1] == '\n')
350 cspace(sp
, p
, len
, spflag
);
358 * Add a compilation unit to the linked list
361 add_compunit(enum e_cut type
, char *s
)
363 struct s_compunit
*cu
;
365 if ((cu
= malloc(sizeof(struct s_compunit
))) == NULL
)
371 cu_nextp
= &cu
->next
;
375 * Add a file to the linked list
382 if ((fp
= malloc(sizeof(struct s_flist
))) == NULL
)
387 fl_nextp
= &fp
->next
;
391 * Modify a pointer to a filename for inplace editing and reopen stdout
394 inplace_edit(char **filename
)
397 char backup
[MAXPATHLEN
];
399 if (lstat(*filename
, &orig
) == -1)
401 if ((orig
.st_mode
& S_IFREG
) == 0) {
402 warnx("cannot inplace edit %s, not a regular file", *filename
);
406 if (*inplace
== '\0') {
408 * This is a bit of a hack: we use mkstemp() to avoid the
409 * mktemp() link-time warning, although mktemp() would fit in
410 * this context much better. We're only interested in getting
411 * a name for use in the rename(); there aren't any security
412 * issues here that don't already exist in relation to the
413 * original file and its directory.
416 strlcpy(backup
, *filename
, sizeof(backup
));
417 strlcat(backup
, ".XXXXXXXXXX", sizeof(backup
));
418 fd
= mkstemp(backup
);
420 errx(1, "could not create backup of %s", *filename
);
424 strlcpy(backup
, *filename
, sizeof(backup
));
425 strlcat(backup
, inplace
, sizeof(backup
));
428 if (rename(*filename
, backup
) == -1)
429 err(1, "rename(\"%s\", \"%s\")", *filename
, backup
);
430 if (freopen(*filename
, "w", stdout
) == NULL
)
431 err(1, "open(\"%s\")", *filename
);
432 if (fchmod(fileno(stdout
), orig
.st_mode
) == -1)
433 err(1, "chmod(\"%s\")", *filename
);
434 *filename
= strdup(backup
);
435 if (*filename
== NULL
)
445 if (files
->next
!= NULL
)
447 if ((ch
= getc(curfile
)) == EOF
)