diff options
author | 2018-11-14 10:59:33 +0000 | |
---|---|---|
committer | 2018-11-14 10:59:33 +0000 | |
commit | 442efec3da5e5dc19d5771789ef01b92d773183a (patch) | |
tree | da6d5aa48278dc7bfcbb8aa560f8beab5156adcf | |
parent | Add back part of the changes to takeover the firmware framebuffer on (diff) | |
download | wireguard-openbsd-442efec3da5e5dc19d5771789ef01b92d773183a.tar.xz wireguard-openbsd-442efec3da5e5dc19d5771789ef01b92d773183a.zip |
Make sed's -i flag more compatible with what gsed does.
- Reset the hold-space in between files
- quit the editor as soon as a 'q' command is found
- Make sure the temp-file is written back to the original file if we quit
the editor
temp-file not written back issue found by Time Chase.
Lots of feedback from millert@ and schwarze@
OK millert@
-rw-r--r-- | usr.bin/sed/extern.h | 5 | ||||
-rw-r--r-- | usr.bin/sed/main.c | 48 | ||||
-rw-r--r-- | usr.bin/sed/process.c | 8 | ||||
-rw-r--r-- | usr.bin/sed/sed.1 | 9 |
4 files changed, 42 insertions, 28 deletions
diff --git a/usr.bin/sed/extern.h b/usr.bin/sed/extern.h index 77ed8ef1c5d..772f0bab3ef 100644 --- a/usr.bin/sed/extern.h +++ b/usr.bin/sed/extern.h @@ -1,4 +1,4 @@ -/* $OpenBSD: extern.h,v 1.13 2017/08/01 18:05:53 martijn Exp $ */ +/* $OpenBSD: extern.h,v 1.14 2018/11/14 10:59:33 martijn Exp $ */ /*- * Copyright (c) 1992 Diomidis Spinellis. * Copyright (c) 1992, 1993 @@ -53,8 +53,9 @@ __dead void error(int, const char *, ...); void warning(const char *, ...); int mf_fgets(SPACE *, enum e_spflag); int lastline(void); +void finish_file(void); void process(void); -void resetranges(void); +void resetstate(void); char *strregerror(int, regex_t *); void *xmalloc(size_t); void *xreallocarray(void *, size_t, size_t); diff --git a/usr.bin/sed/main.c b/usr.bin/sed/main.c index 2ef2ec1013f..60535638cd4 100644 --- a/usr.bin/sed/main.c +++ b/usr.bin/sed/main.c @@ -1,4 +1,4 @@ -/* $OpenBSD: main.c,v 1.37 2018/07/11 06:57:18 martijn Exp $ */ +/* $OpenBSD: main.c,v 1.38 2018/11/14 10:59:33 martijn Exp $ */ /*- * Copyright (c) 1992 Diomidis Spinellis. @@ -310,6 +310,30 @@ again: return (NULL); } +void +finish_file(void) +{ + if (infile != NULL) { + fclose(infile); + if (*oldfname != '\0') { + if (rename(fname, oldfname) != 0) { + warning("rename()"); + unlink(tmpfname); + exit(1); + } + *oldfname = '\0'; + } + if (*tmpfname != '\0') { + if (outfile != NULL && outfile != stdout) + fclose(outfile); + outfile = NULL; + rename(tmpfname, fname); + *tmpfname = '\0'; + } + outfname = NULL; + } +} + /* * Like fgets, but go through the list of files chaining them together. * Set len to the length of the line. @@ -347,25 +371,7 @@ mf_fgets(SPACE *sp, enum e_spflag spflag) sp->len = 0; return (0); } - if (infile != NULL) { - fclose(infile); - if (*oldfname != '\0') { - if (rename(fname, oldfname) != 0) { - warning("rename()"); - unlink(tmpfname); - exit(1); - } - *oldfname = '\0'; - } - if (*tmpfname != '\0') { - if (outfile != NULL && outfile != stdout) - fclose(outfile); - outfile = NULL; - rename(tmpfname, fname); - *tmpfname = '\0'; - } - outfname = NULL; - } + finish_file(); if (firstfile == 0) files = files->next; else @@ -405,7 +411,7 @@ mf_fgets(SPACE *sp, enum e_spflag spflag) fchmod(fileno(outfile), sb.st_mode & ALLPERMS); outfname = tmpfname; linenum = 0; - resetranges(); + resetstate(); } else { outfile = stdout; outfname = "stdout"; diff --git a/usr.bin/sed/process.c b/usr.bin/sed/process.c index 654cf758ab8..cc31fcb57a3 100644 --- a/usr.bin/sed/process.c +++ b/usr.bin/sed/process.c @@ -1,4 +1,4 @@ -/* $OpenBSD: process.c,v 1.33 2017/12/13 16:06:34 millert Exp $ */ +/* $OpenBSD: process.c,v 1.34 2018/11/14 10:59:33 martijn Exp $ */ /*- * Copyright (c) 1992 Diomidis Spinellis. @@ -196,6 +196,7 @@ redirect: if (!nflag && !pd) OUT(); flush_appends(); + finish_file(); exit(0); case 'r': if (appendx >= appendnum) { @@ -312,10 +313,13 @@ applies(struct s_command *cp) * Reset all inrange markers. */ void -resetranges(void) +resetstate(void) { struct s_command *cp; + free(HS.back); + memset(&HS, 0, sizeof(HS)); + for (cp = prog; cp; cp = cp->code == '{' ? cp->u.c : cp->next) if (cp->a2) cp->inrange = 0; diff --git a/usr.bin/sed/sed.1 b/usr.bin/sed/sed.1 index af3a4cdea08..226735865e7 100644 --- a/usr.bin/sed/sed.1 +++ b/usr.bin/sed/sed.1 @@ -1,4 +1,4 @@ -.\" $OpenBSD: sed.1,v 1.56 2018/07/11 06:47:38 martijn Exp $ +.\" $OpenBSD: sed.1,v 1.57 2018/11/14 10:59:33 martijn Exp $ .\" .\" Copyright (c) 1992, 1993 .\" The Regents of the University of California. All rights reserved. @@ -32,7 +32,7 @@ .\" .\" from: @(#)sed.1 8.2 (Berkeley) 12/30/93 .\" -.Dd $Mdocdate: July 11 2018 $ +.Dd $Mdocdate: November 14 2018 $ .Dt SED 1 .Os .Sh NAME @@ -106,6 +106,9 @@ It is not recommended to give a zero length .Ar extension when in place editing files, as it risks corruption or partial content in situations where disk space is exhausted, etc. +In +.Fl i +mode, the hold space, line numbers, and ranges are reset between files. .It Fl r An alias for .Fl E , @@ -392,7 +395,7 @@ Write the pattern space to standard output. Write the pattern space, up to the first newline character, to the standard output. .It [1addr] Ns Ic q -Branch to the end of the script and quit without starting a new cycle. +Branch to the end of the script and quit without starting a new cycle or file. .It [1addr] Ns Ic r Ar file Copy the contents of .Ar file |