1 /* $NetBSD: main.c,v 1.34 2015/03/12 12:40:41 christos Exp $ */
4 * Copyright (c) 2013 Johann 'Myrkraverk' Oskarsson.
5 * Copyright (c) 1992 Diomidis Spinellis.
6 * Copyright (c) 1992, 1993
7 * The Regents of the University of California. All rights reserved.
9 * This code is derived from software contributed to Berkeley by
10 * Diomidis Spinellis of Imperial College, University of London.
12 * Redistribution and use in source and binary forms, with or without
13 * modification, are permitted provided that the following conditions
15 * 1. Redistributions of source code must retain the above copyright
16 * notice, this list of conditions and the following disclaimer.
17 * 2. Redistributions in binary form must reproduce the above copyright
18 * notice, this list of conditions and the following disclaimer in the
19 * documentation and/or other materials provided with the distribution.
20 * 3. Neither the name of the University nor the names of its contributors
21 * may be used to endorse or promote products derived from this software
22 * without specific prior written permission.
24 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
25 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
26 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
27 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
28 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
29 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
30 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
31 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
32 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
33 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
37 #if HAVE_NBTOOL_CONFIG_H
38 #include "nbtool_config.h"
41 #include <sys/cdefs.h>
42 __RCSID("$NetBSD: main.c,v 1.34 2015/03/12 12:40:41 christos Exp $");
44 __FBSDID("$FreeBSD: head/usr.bin/sed/main.c 252231 2013-06-26 04:14:19Z pfg $");
48 __COPYRIGHT("@(#) Copyright (c) 1992, 1993\
49 The Regents of the University of California. All rights reserved.");
53 static const char sccsid
[] = "@(#)main.c 8.2 (Berkeley) 1/3/94";
56 #include <sys/types.h>
58 #include <sys/param.h>
79 * Linked list of units (strings and files) to be compiled
82 struct s_compunit
*next
;
83 enum e_cut
{CU_FILE
, CU_STRING
} type
;
84 char *s
; /* Pointer to string or fname */
88 * Linked list pointer to compilation units and pointer to current
91 static struct s_compunit
*script
, **cu_nextp
= &script
;
94 * Linked list of files to be processed
102 * Linked list pointer to files and pointer to current
105 static struct s_flist
*files
, **fl_nextp
= &files
;
107 FILE *infile
; /* Current input file */
108 FILE *outfile
; /* Current output file */
110 int aflag
, eflag
, nflag
;
112 static int rval
; /* Exit status */
114 static int ispan
; /* Whether inplace editing spans across files */
117 * Current file and line number; line numbers restart across compilation
118 * units, but span across input files. The latter is optional if editing
121 const char *fname
; /* File name. */
122 const char *outfname
; /* Output file name */
123 static char oldfname
[PATH_MAX
]; /* Old file name (for in-place editing) */
124 static char tmpfname
[PATH_MAX
]; /* Temporary file name (for in-place editing) */
125 static const char *inplace
; /* Inplace edit file extension. */
128 static void add_compunit(enum e_cut
, char *);
129 static void add_file(char *);
130 static void usage(void) __dead
;
133 main(int argc
, char *argv
[])
138 setprogname(argv
[0]);
139 (void) setlocale(LC_ALL
, "");
144 while ((c
= getopt(argc
, argv
, "EI::ae:f:i::lnru")) != -1)
146 case 'r': /* Gnu sed compat */
148 rflags
= REG_EXTENDED
;
151 inplace
= optarg
? optarg
: __UNCONST("");
152 ispan
= 1; /* span across input files */
159 temp_arg
= xmalloc(strlen(optarg
) + 2);
160 strcpy(temp_arg
, optarg
);
161 strcat(temp_arg
, "\n");
162 add_compunit(CU_STRING
, temp_arg
);
166 add_compunit(CU_FILE
, optarg
);
169 inplace
= optarg
? optarg
: __UNCONST("");
170 ispan
= 0; /* don't span across input files */
174 c
= setvbuf(stdout
, NULL
, _IOLBF
, 0);
176 c
= setlinebuf(stdout
);
179 warn("setting line buffered output failed");
186 c
= setvbuf(stdout
, NULL
, _IONBF
, 0);
192 warn("setting unbuffered output failed");
201 /* First usage case; script is the first arg */
202 if (!eflag
&& !fflag
&& *argv
) {
203 add_compunit(CU_STRING
, *argv
);
209 /* Continue with first and start second usage */
211 for (; *argv
; argv
++)
225 (void)fprintf(stderr
,
226 "Usage: %s [-aElnru] command [file ...]\n"
227 "\t%s [-aElnru] [-e command] [-f command_file] [-I[extension]]\n"
228 "\t [-i[extension]] [file ...]\n", getprogname(), getprogname());
233 * Like fgets, but go through the chain of compilation units chaining them
234 * together. Empty strings and files are ignored.
237 cu_fgets(char *buf
, int n
, int *more
)
239 static enum {ST_EOF
, ST_FILE
, ST_STRING
} state
= ST_EOF
;
240 static FILE *f
; /* Current open file */
241 static char *s
; /* Current pointer inside string */
242 static char string_ident
[30];
248 if (script
== NULL
) {
254 switch (script
->type
) {
256 if ((f
= fopen(script
->s
, "r")) == NULL
)
257 err(1, "%s", script
->s
);
262 if (((size_t)snprintf(string_ident
,
263 sizeof(string_ident
), "\"%s\"", script
->s
)) >=
264 sizeof(string_ident
) - 1)
265 (void)strcpy(string_ident
+
266 sizeof(string_ident
) - 6, " ...\"");
267 fname
= string_ident
;
273 if ((p
= fgets(buf
, n
, f
)) != NULL
) {
275 if (linenum
== 1 && buf
[0] == '#' && buf
[1] == 'n')
281 script
= script
->next
;
286 if (linenum
== 0 && s
[0] == '#' && s
[1] == 'n')
300 if (s
== script
->s
) {
301 script
= script
->next
;
304 script
= script
->next
;
329 * Like fgets, but go through the list of files chaining them together.
330 * Set len to the length of the line.
333 mf_fgets(SPACE
*sp
, enum e_spflag spflag
)
337 static char *p
= NULL
;
338 static size_t plen
= 0;
340 static int firstfile
;
342 if (infile
== NULL
) {
344 if (files
->fname
== NULL
) {
346 errx(1, "-I or -i may not be used with stdin");
356 if (infile
!= NULL
&& (c
= getc(infile
)) != EOF
) {
357 (void)ungetc(c
, infile
);
360 /* If we are here then either eof or no files are open yet */
361 if (infile
== stdin
) {
365 if (infile
!= NULL
) {
367 if (*oldfname
!= '\0') {
368 /* if there was a backup file, remove it */
371 * Backup the original. Note that hard links
372 * are not supported on all filesystems.
374 if ((link(fname
, oldfname
) != 0) &&
375 (rename(fname
, oldfname
) != 0)) {
383 if (*tmpfname
!= '\0') {
384 if (outfile
!= NULL
&& outfile
!= stdout
)
385 if (fclose(outfile
) != 0) {
391 if (rename(tmpfname
, fname
) != 0) {
392 /* this should not happen really! */
409 fname
= files
->fname
;
410 if (inplace
!= NULL
) {
411 if (lstat(fname
, &sb
) != 0)
413 if (!(sb
.st_mode
& S_IFREG
))
414 errx(1, "%s: %s %s", fname
,
415 "in-place editing only",
416 "works for regular files");
417 if (*inplace
!= '\0') {
418 strlcpy(oldfname
, fname
,
420 len
= strlcat(oldfname
, inplace
,
422 if (len
> sizeof(oldfname
))
423 errx(1, "%s: name too long", fname
);
425 char d_name
[PATH_MAX
], f_name
[PATH_MAX
];
426 (void)strlcpy(d_name
, fname
, sizeof(d_name
));
427 (void)strlcpy(f_name
, fname
, sizeof(f_name
));
428 len
= (size_t)snprintf(tmpfname
, sizeof(tmpfname
),
429 "%s/.!%ld!%s", dirname(d_name
), (long)getpid(),
431 if (len
>= sizeof(tmpfname
))
432 errx(1, "%s: name too long", fname
);
434 if (outfile
!= NULL
&& outfile
!= stdout
)
436 if ((outfile
= fopen(tmpfname
, "w")) == NULL
)
438 fchown(fileno(outfile
), sb
.st_uid
, sb
.st_gid
);
439 fchmod(fileno(outfile
), sb
.st_mode
& ALLPERMS
);
449 if ((infile
= fopen(fname
, "r")) == NULL
) {
456 * We are here only when infile is open and we still have something
459 * Use getline() so that we can handle essentially infinite input
460 * data. The p and plen are static so each invocation gives
461 * getline() the same buffer which is expanded as needed.
463 ssize_t slen
= getline(&p
, &plen
, infile
);
466 if (slen
!= 0 && p
[slen
- 1] == '\n')
468 cspace(sp
, p
, (size_t)slen
, spflag
);
476 * Add a compilation unit to the linked list
479 add_compunit(enum e_cut type
, char *s
)
481 struct s_compunit
*cu
;
483 cu
= xmalloc(sizeof(struct s_compunit
));
488 cu_nextp
= &cu
->next
;
492 * Add a file to the linked list
499 fp
= xmalloc(sizeof(struct s_flist
));
503 fl_nextp
= &fp
->next
;
511 if (files
->next
!= NULL
&& (inplace
== NULL
|| ispan
))
513 if ((ch
= getc(infile
)) == EOF
)