On Tue, 14 Aug 2018 10:43:30 +0200, Martijn van Duren wrote:
> The diff below fixes this. Note that I took special care to make a
> distinction between in place and normal for the 'q' command.
> When running normally the files are concatenated, so we should quit
> immediately. When running in in place mode I reckon the script
> should be treated as if it were to run for every file individually,
> since the line numbers are also reset in between files.
I'm not convinced this is the right approach. If there is a quit
command, the script should quit without further processing.
We could do something like this instead which matches what GNU sed
does more closely. Since the -i flag is a GNU invention I think
it is worthwhile trying to behave similarly.
- todd
Index: usr.bin/sed/extern.h
===================================================================
RCS file: /cvs/src/usr.bin/sed/extern.h,v
retrieving revision 1.13
diff -u -p -u -r1.13 extern.h
--- usr.bin/sed/extern.h 1 Aug 2017 18:05:53 -0000 1.13
+++ usr.bin/sed/extern.h 14 Aug 2018 12:01:59 -0000
@@ -53,6 +53,7 @@ __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);
char *strregerror(int, regex_t *);
Index: usr.bin/sed/main.c
===================================================================
RCS file: /cvs/src/usr.bin/sed/main.c,v
retrieving revision 1.37
diff -u -p -u -r1.37 main.c
--- usr.bin/sed/main.c 11 Jul 2018 06:57:18 -0000 1.37
+++ usr.bin/sed/main.c 14 Aug 2018 12:01:10 -0000
@@ -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
Index: usr.bin/sed/process.c
===================================================================
RCS file: /cvs/src/usr.bin/sed/process.c,v
retrieving revision 1.33
diff -u -p -u -r1.33 process.c
--- usr.bin/sed/process.c 13 Dec 2017 16:06:34 -0000 1.33
+++ usr.bin/sed/process.c 14 Aug 2018 12:03:24 -0000
@@ -196,6 +196,7 @@ redirect:
if (!nflag && !pd)
OUT();
flush_appends();
+ finish_file();
exit(0);
case 'r':
if (appendx >= appendnum) {