2 * Copyright 2010 Nexenta Systems, Inc. All rights reserved.
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/types.h>
37 #include <sys/param.h>
58 * Linked list of units (strings and files) to be compiled
61 struct s_compunit
*next
;
62 enum e_cut
{CU_FILE
, CU_STRING
} type
;
63 char *s
; /* Pointer to string or fname */
67 * Linked list pointer to compilation units and pointer to current
70 static struct s_compunit
*script
, **cu_nextp
= &script
;
73 * Linked list of files to be processed
81 * Linked list pointer to files and pointer to current
84 static struct s_flist
*files
, **fl_nextp
= &files
;
86 FILE *infile
; /* Current input file */
87 FILE *outfile
; /* Current output file */
89 int aflag
, eflag
, nflag
;
91 static int rval
; /* Exit status */
93 static int ispan
; /* Whether inplace editing spans across files */
96 * Current file and line number; line numbers restart across compilation
97 * units, but span across input files. The latter is optional if editing
100 const char *fname
; /* File name. */
101 const char *outfname
; /* Output file name */
102 static char oldfname
[PATH_MAX
]; /* Old file name (for in-place editing) */
103 static char tmpfname
[PATH_MAX
]; /* Temporary file name (for in-place editing) */
104 static const char *inplace
; /* Inplace edit file extension. */
107 static void add_compunit(enum e_cut
, char *);
108 static void add_file(char *);
109 static void usage(void);
110 static char *getln(FILE *, size_t *);
114 main(int argc
, char *argv
[])
119 (void) setlocale(LC_ALL
, "");
122 #define TEXT_DOMAIN "SYS_TEST"
124 (void) textdomain(TEXT_DOMAIN
);
129 while ((c
= getopt(argc
, argv
, "EI:ae:f:i:lnr")) != -1)
131 case 'r': /* Gnu sed compat */
133 rflags
= REG_EXTENDED
;
137 ispan
= 1; /* span across input files */
144 if (asprintf(&temp_arg
, "%s\n", optarg
) <= 1)
146 add_compunit(CU_STRING
, temp_arg
);
150 add_compunit(CU_FILE
, optarg
);
154 ispan
= 0; /* don't span across input files */
157 /* On SunOS, setlinebuf "returns no useful value */
158 (void) setlinebuf(stdout
);
170 /* First usage case; script is the first arg */
171 if (!eflag
&& !fflag
&& *argv
) {
172 add_compunit(CU_STRING
, *argv
);
178 /* Continue with first and start second usage */
180 for (; *argv
; argv
++)
194 (void) fputs(_("usage: sed script [-Ealn] [-i extension] [file ...]\n"
195 " sed [-Ealn] [-i extension] [-e script] ... "
196 "[-f script_file] ... [file ...]"),
202 * Like fgets, but go through the chain of compilation units chaining them
203 * together. Empty strings and files are ignored.
206 cu_fgets(char *buf
, int n
, int *more
)
208 static enum {ST_EOF
, ST_FILE
, ST_STRING
} state
= ST_EOF
;
209 static FILE *f
; /* Current open file */
210 static char *s
; /* Current pointer inside string */
211 static char string_ident
[30];
217 if (script
== NULL
) {
223 switch (script
->type
) {
225 if ((f
= fopen(script
->s
, "r")) == NULL
)
226 err(1, "%s", script
->s
);
231 if (((size_t)snprintf(string_ident
,
232 sizeof (string_ident
), "\"%s\"", script
->s
)) >=
233 sizeof (string_ident
) - 1)
234 (void) strcpy(string_ident
+
235 sizeof (string_ident
) - 6, " ...\"");
236 fname
= string_ident
;
244 if ((p
= fgets(buf
, n
, f
)) != NULL
) {
246 if (linenum
== 1 && buf
[0] == '#' && buf
[1] == 'n')
252 script
= script
->next
;
257 if (linenum
== 0 && s
[0] == '#' && s
[1] == 'n')
271 if (s
== script
->s
) {
272 script
= script
->next
;
275 script
= script
->next
;
300 * Like fgets, but go through the list of files chaining them together.
301 * Set len to the length of the line.
304 mf_fgets(SPACE
*sp
, enum e_spflag spflag
)
310 static int firstfile
;
312 if (infile
== NULL
) {
314 if (files
->fname
== NULL
) {
317 _("-I or -i may not be used with stdin"));
327 if (infile
!= NULL
&& (c
= getc(infile
)) != EOF
) {
328 (void) ungetc(c
, infile
);
331 /* If we are here then either eof or no files are open yet */
332 if (infile
== stdin
) {
336 if (infile
!= NULL
) {
337 (void) fclose(infile
);
338 if (*oldfname
!= '\0') {
339 /* if there was a backup file, remove it */
340 (void) unlink(oldfname
);
341 if (link(fname
, oldfname
) != 0) {
343 (void) unlink(tmpfname
);
348 if (*tmpfname
!= '\0') {
349 if (outfile
!= NULL
&& outfile
!= stdout
)
350 if (fclose(outfile
) != 0) {
352 (void) unlink(tmpfname
);
356 if (rename(tmpfname
, fname
) != 0) {
357 /* this should not happen really! */
359 (void) unlink(tmpfname
);
374 fname
= files
->fname
;
375 if (inplace
!= NULL
) {
378 (void) strlcpy(bn
, fname
, sizeof (bn
));
379 (void) strlcpy(dn
, fname
, sizeof (dn
));
380 if (lstat(fname
, &sb
) != 0)
382 if (!(sb
.st_mode
& S_IFREG
))
383 fatal(_("in-place editing only "
384 "works for regular files"));
385 if (*inplace
!= '\0') {
386 (void) strlcpy(oldfname
, fname
,
388 len
= strlcat(oldfname
, inplace
,
390 if (len
> sizeof (oldfname
))
391 fatal(_("name too long"));
393 len
= snprintf(tmpfname
, sizeof (tmpfname
),
394 "%s/.!%ld!%s", dirname(dn
), (long)getpid(),
396 if (len
>= sizeof (tmpfname
))
397 fatal(_("name too long"));
398 (void) unlink(tmpfname
);
399 if ((outfile
= fopen(tmpfname
, "w")) == NULL
)
401 if (fchown(fileno(outfile
), sb
.st_uid
, sb
.st_gid
) != 0)
403 if (fchmod(fileno(outfile
), sb
.st_mode
& 07777) != 0)
414 if ((infile
= fopen(fname
, "r")) == NULL
) {
421 * We are here only when infile is open and we still have something
424 * Use fgetln so that we can handle essentially infinite input data.
425 * Can't use the pointer into the stdio buffer as the process space
426 * because the ungetc() can cause it to move.
428 p
= getln(infile
, &len
);
430 errx(1, "%s: %s", fname
, strerror(errno
? errno
: EIO
));
431 if (len
!= 0 && p
[len
- 1] == '\n')
433 cspace(sp
, p
, len
, spflag
);
441 * Add a compilation unit to the linked list
444 add_compunit(enum e_cut type
, char *s
)
446 struct s_compunit
*cu
;
448 if ((cu
= malloc(sizeof (struct s_compunit
))) == NULL
)
454 cu_nextp
= &cu
->next
;
458 * Add a file to the linked list
465 if ((fp
= malloc(sizeof (struct s_flist
))) == NULL
)
470 fl_nextp
= &fp
->next
;
478 if (files
->next
!= NULL
&& (inplace
== NULL
|| ispan
))
480 if ((ch
= getc(infile
)) == EOF
)
482 (void) ungetc(ch
, infile
);
487 getln(FILE *in
, size_t *lenp
)
489 static char *buffer
= NULL
;
490 static size_t sz
= 0;
495 if (sz
<= (len
+ 1)) {
497 if ((nb
= realloc(buffer
, sz
+ LINE_MAX
)) == NULL
) {
506 if (fgets(buffer
+ len
, sz
- len
, in
) == NULL
) {
512 len
+= strlen(buffer
+ len
);
514 if (buffer
[len
- 1] == '\n') {
515 /* got the new line */