2 * Copyright 2020 OmniOS Community Edition (OmniOSce) Association.
3 * Copyright (c) 2013 Johann 'Myrkraverk' Oskarsson <johann@myrkraverk.com>
4 * Copyright (c) 2011 Gary Mills
5 * Copyright 2011 Nexenta Systems, Inc. All rights reserved.
6 * Copyright (c) 1992 Diomidis Spinellis.
7 * Copyright (c) 1992, 1993
8 * The Regents of the University of California. All rights reserved.
10 * This code is derived from software contributed to Berkeley by
11 * Diomidis Spinellis of Imperial College, University of London.
13 * Redistribution and use in source and binary forms, with or without
14 * modification, are permitted provided that the following conditions
16 * 1. Redistributions of source code must retain the above copyright
17 * notice, this list of conditions and the following disclaimer.
18 * 2. Redistributions in binary form must reproduce the above copyright
19 * notice, this list of conditions and the following disclaimer in the
20 * documentation and/or other materials provided with the distribution.
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
38 #include <sys/types.h>
40 #include <sys/param.h>
62 * Linked list of units (strings and files) to be compiled
65 struct s_compunit
*next
;
66 enum e_cut
{CU_FILE
, CU_STRING
} type
;
67 char *s
; /* Pointer to string or fname */
71 * Linked list pointer to compilation units and pointer to current
74 static struct s_compunit
*script
, **cu_nextp
= &script
;
77 * Linked list of files to be processed
85 * Linked list pointer to files and pointer to current
88 static struct s_flist
*files
, **fl_nextp
= &files
;
90 FILE *infile
; /* Current input file */
91 FILE *outfile
; /* Current output file */
93 int aflag
, eflag
, nflag
;
95 static int rval
; /* Exit status */
97 static int ispan
; /* Whether inplace editing spans across files */
100 * Current file and line number; line numbers restart across compilation
101 * units, but span across input files. The latter is optional if editing
104 const char *fname
; /* File name. */
105 const char *outfname
; /* Output file name */
106 static char oldfname
[PATH_MAX
]; /* Old file name (for in-place editing) */
107 static char tmpfname
[PATH_MAX
]; /* Temporary file name (for in-place editing) */
108 static const char *inplace
; /* Inplace edit file extension. */
111 static const struct option lopts
[] = {
112 {"in-place", optional_argument
, NULL
, 'i'},
116 static void add_compunit(enum e_cut
, char *);
117 static void add_file(char *);
118 static void usage(void);
122 main(int argc
, char *argv
[])
127 (void) setlocale(LC_ALL
, "");
130 #define TEXT_DOMAIN "SYS_TEST"
132 (void) textdomain(TEXT_DOMAIN
);
137 while ((c
= getopt_long(argc
, argv
, "EI::ae:f:i::lnr", lopts
, NULL
)) !=
140 case 'r': /* Gnu sed compat */
142 rflags
= REG_EXTENDED
;
149 ispan
= 1; /* span across input files */
156 if (asprintf(&temp_arg
, "%s\n", optarg
) < 1)
158 add_compunit(CU_STRING
, temp_arg
);
162 add_compunit(CU_FILE
, optarg
);
169 ispan
= 0; /* don't span across input files */
172 /* On SunOS, setlinebuf "returns no useful value */
173 (void) setlinebuf(stdout
);
185 /* First usage case; script is the first arg */
186 if (!eflag
&& !fflag
&& *argv
) {
187 add_compunit(CU_STRING
, *argv
);
193 /* Continue with first and start second usage */
195 for (; *argv
; argv
++)
209 (void) fputs(_("usage: sed script [-Ealn] [-i[extension]] [file...]\n"
210 " sed [-Ealn] [-i[extension]] [-e script]... "
211 "[-f script_file]... [file...]\n"),
217 * Like fgets, but go through the chain of compilation units chaining them
218 * together. Empty strings and files are ignored.
221 cu_fgets(char *buf
, int n
, int *more
)
223 static enum {ST_EOF
, ST_FILE
, ST_STRING
} state
= ST_EOF
;
224 static FILE *f
; /* Current open file */
225 static char *s
; /* Current pointer inside string */
226 static char string_ident
[30];
232 if (script
== NULL
) {
238 switch (script
->type
) {
240 if ((f
= fopen(script
->s
, "r")) == NULL
)
241 err(1, "%s", script
->s
);
246 if (((size_t)snprintf(string_ident
,
247 sizeof (string_ident
), "\"%s\"", script
->s
)) >=
248 sizeof (string_ident
) - 1)
249 (void) strcpy(string_ident
+
250 sizeof (string_ident
) - 6, " ...\"");
251 fname
= string_ident
;
256 errx(1, "BUG: Unknown script type: %d\n", script
->type
);
261 if ((p
= fgets(buf
, n
, f
)) != NULL
) {
263 if (linenum
== 1 && buf
[0] == '#' && buf
[1] == 'n')
269 script
= script
->next
;
274 if (linenum
== 0 && s
[0] == '#' && s
[1] == 'n')
288 if (s
== script
->s
) {
289 script
= script
->next
;
292 script
= script
->next
;
317 * Like fgets, but go through the list of files chaining them together.
318 * Set len to the length of the line.
321 mf_fgets(SPACE
*sp
, enum e_spflag spflag
)
325 static char *p
= NULL
;
326 static size_t plen
= 0;
328 static int firstfile
;
330 if (infile
== NULL
) {
332 if (files
->fname
== NULL
) {
335 _("-I or -i may not be used with stdin"));
345 if (infile
!= NULL
&& (c
= getc(infile
)) != EOF
) {
346 (void) ungetc(c
, infile
);
349 /* If we are here then either eof or no files are open yet */
350 if (infile
== stdin
) {
354 if (infile
!= NULL
) {
355 (void) fclose(infile
);
356 if (*oldfname
!= '\0') {
357 /* if there was a backup file, remove it */
358 (void) unlink(oldfname
);
360 * Backup the original. Note that hard links
361 * are not supported on all filesystems.
363 if ((link(fname
, oldfname
) != 0) &&
364 (rename(fname
, oldfname
) != 0)) {
367 (void) unlink(tmpfname
);
372 if (*tmpfname
!= '\0') {
373 if (outfile
!= NULL
&& outfile
!= stdout
)
374 if (fclose(outfile
) != 0) {
376 (void) unlink(tmpfname
);
380 if (rename(tmpfname
, fname
) != 0) {
381 /* this should not happen really! */
383 (void) unlink(tmpfname
);
398 fname
= files
->fname
;
399 if (inplace
!= NULL
) {
402 (void) strlcpy(bn
, fname
, sizeof (bn
));
403 (void) strlcpy(dn
, fname
, sizeof (dn
));
404 if (lstat(fname
, &sb
) != 0)
406 if (!(sb
.st_mode
& S_IFREG
))
407 fatal(_("in-place editing only "
408 "works for regular files"));
409 if (*inplace
!= '\0') {
410 (void) strlcpy(oldfname
, fname
,
412 len
= strlcat(oldfname
, inplace
,
414 if (len
> sizeof (oldfname
))
415 fatal(_("name too long"));
417 len
= snprintf(tmpfname
, sizeof (tmpfname
),
418 "%s/.!%ld!%s", dirname(dn
), (long)getpid(),
420 if (len
>= sizeof (tmpfname
))
421 fatal(_("name too long"));
422 (void) unlink(tmpfname
);
423 if ((outfile
= fopen(tmpfname
, "w")) == NULL
)
426 * Some file systems don't support chown or
427 * chmod fully. On those, the owner/group and
428 * permissions will already be set to what
431 if (fstat(fileno(outfile
), &nsb
) != 0) {
434 if (((sb
.st_uid
!= nsb
.st_uid
) ||
435 (sb
.st_gid
!= nsb
.st_gid
)) &&
436 (fchown(fileno(outfile
), sb
.st_uid
, sb
.st_gid
)
439 if ((sb
.st_mode
!= nsb
.st_mode
) &&
440 (fchmod(fileno(outfile
), sb
.st_mode
& 07777) != 0))
451 if ((infile
= fopen(fname
, "r")) == NULL
) {
458 * We are here only when infile is open and we still have something
461 * Use getline() so that we can handle essentially infinite
462 * input data. The p and plen are static so each invocation gives
463 * getline() the same buffer which is expanded as needed.
465 len
= getline(&p
, &plen
, infile
);
468 if (len
!= 0 && p
[len
- 1] == '\n')
470 cspace(sp
, p
, len
, spflag
);
478 * Add a compilation unit to the linked list
481 add_compunit(enum e_cut type
, char *s
)
483 struct s_compunit
*cu
;
485 if ((cu
= malloc(sizeof (struct s_compunit
))) == NULL
)
491 cu_nextp
= &cu
->next
;
495 * Add a file to the linked list
502 if ((fp
= malloc(sizeof (struct s_flist
))) == NULL
)
507 fl_nextp
= &fp
->next
;
515 if (feof(infile
) != 0 || (ch
= getc(infile
)) == EOF
) {
519 * Reached the end of the current input file.
520 * If there are no more that contain data, then this is the
523 if (inplace
!= NULL
&& ispan
== 0)
526 for (f
= files
->next
; f
!= NULL
; f
= f
->next
) {
529 if (stat(f
->fname
, &st
) == -1) {
530 /* Treat an error here as an empty file */
539 (void) ungetc(ch
, infile
);