diff options
author | yar <yar@FreeBSD.org> | 2007-04-21 01:21:36 +0000 |
---|---|---|
committer | yar <yar@FreeBSD.org> | 2007-04-21 01:21:36 +0000 |
commit | 65b331b4a19ecf3dc073e2363cb8294e986ded44 (patch) | |
tree | ca61296c60f091c362f574d1212cf29938a3745f /usr.bin/sed/main.c | |
parent | a1e73b1eafb356ff4adba96fd8099c15c11471cd (diff) | |
download | FreeBSD-src-65b331b4a19ecf3dc073e2363cb8294e986ded44.zip FreeBSD-src-65b331b4a19ecf3dc073e2363cb8294e986ded44.tar.gz |
Change the semantics of -i (in-place editing) so that it treats
each file independently from other files. The new semantics are
desired in the most of practical cases, e.g.: delete lines 5-9
from each file.
Keep the previous semantics of -i under a new option, -I, which
uses a single continuous address space covering all files to edit
in-place -- they are too cool to just drop them.
Add regression tests for -i and -I.
Approved by: dds
Compared with: GNU sed
Discussed on: -hackers
MFC after: 2 weeks
Diffstat (limited to 'usr.bin/sed/main.c')
-rw-r--r-- | usr.bin/sed/main.c | 20 |
1 files changed, 16 insertions, 4 deletions
diff --git a/usr.bin/sed/main.c b/usr.bin/sed/main.c index 1b78f80..e4e426f 100644 --- a/usr.bin/sed/main.c +++ b/usr.bin/sed/main.c @@ -101,9 +101,12 @@ int aflag, eflag, nflag; int rflags = 0; static int rval; /* Exit status */ +static int ispan; /* Whether inplace editing spans across files */ + /* * Current file and line number; line numbers restart across compilation - * units, but span across input files. + * units, but span across input files. The latter is optional if editing + * in place. */ const char *fname; /* File name. */ const char *outfname; /* Output file name */ @@ -127,11 +130,15 @@ main(int argc, char *argv[]) fflag = 0; inplace = NULL; - while ((c = getopt(argc, argv, "Eae:f:i:ln")) != -1) + while ((c = getopt(argc, argv, "EI:ae:f:i:ln")) != -1) switch (c) { case 'E': rflags = REG_EXTENDED; break; + case 'I': + inplace = optarg; + ispan = 1; /* span across input files */ + break; case 'a': aflag = 1; break; @@ -149,6 +156,7 @@ main(int argc, char *argv[]) break; case 'i': inplace = optarg; + ispan = 0; /* don't span across input files */ break; case 'l': if(setlinebuf(stdout) != 0) @@ -307,7 +315,7 @@ mf_fgets(SPACE *sp, enum e_spflag spflag) /* stdin? */ if (files->fname == NULL) { if (inplace != NULL) - errx(1, "-i may not be used with stdin"); + errx(1, "-I or -i may not be used with stdin"); infile = stdin; fname = "stdin"; outfile = stdout; @@ -380,6 +388,10 @@ mf_fgets(SPACE *sp, enum e_spflag spflag) fchown(fileno(outfile), sb.st_uid, sb.st_gid); fchmod(fileno(outfile), sb.st_mode & ALLPERMS); outfname = tmpfname; + if (!ispan) { + linenum = 0; + resetranges(); + } } else { outfile = stdout; outfname = "stdout"; @@ -448,7 +460,7 @@ lastline(void) { int ch; - if (files->next != NULL) + if (files->next != NULL && (inplace == NULL || ispan)) return (0); if ((ch = getc(infile)) == EOF) return (1); |