Make the r command filename obligatory, similar to what FreeBSD and NetBSD
authormartijn <martijn@openbsd.org>
Fri, 8 Dec 2017 18:41:59 +0000 (18:41 +0000)
committermartijn <martijn@openbsd.org>
Fri, 8 Dec 2017 18:41:59 +0000 (18:41 +0000)
do for several years.
While here make corresponding error message for missing read and write file
consistent between commands/flag, and shrink the the code of the w flag of
the s command by making it use the same code as the w command.

Prompted by a larger diff by kshe59 <at> zoho <dot> eu
OK millert@

usr.bin/sed/compile.c
usr.bin/sed/sed.1

index 0671805..f994049 100644 (file)
@@ -1,4 +1,4 @@
-/*     $OpenBSD: compile.c,v 1.42 2017/08/01 18:05:53 martijn Exp $    */
+/*     $OpenBSD: compile.c,v 1.43 2017/12/08 18:41:59 martijn Exp $    */
 
 /*-
  * Copyright (c) 1992 Diomidis Spinellis.
@@ -277,6 +277,8 @@ nonsel:             /* Now parse the command */
                        pledge_rpath = 1;
                        p++;
                        EATSPACE();
+                       if (*p == '\0')
+                               error(COMPILE, "filename expected");
                        cmd->t = duptoeol(p, "read command", NULL);
                        break;
                case BRANCH:                    /* b t */
@@ -539,7 +541,6 @@ compile_flags(char *p, struct s_subst *s)
 {
        int gn;                 /* True if we have seen g or n */
        long l;
-       char wfile[PATH_MAX], *q, *eq;
 
        s->n = 1;                               /* Default */
        s->p = 0;
@@ -577,32 +578,17 @@ compile_flags(char *p, struct s_subst *s)
                        continue;
                case 'w':
                        p++;
-#ifdef HISTORIC_PRACTICE
-                       if (*p != ' ') {
-                               warning("space missing before w wfile");
-                               return (p);
-                       }
-#endif
                        EATSPACE();
-                       q = wfile;
-                       eq = wfile + sizeof(wfile) - 1;
-                       while (*p) {
-                               if (*p == '\n')
-                                       break;
-                               if (q >= eq)
-                                       error(COMPILE, "wfile too long");
-                               *q++ = *p++;
-                       }
-                       *q = '\0';
-                       if (q == wfile)
-                               error(COMPILE, "no wfile specified");
-                       s->wfile = strdup(wfile);
+                       if (*p == '\0')
+                               error(COMPILE, "filename expected");
+                       s->wfile = duptoeol(p, "s command w flag", NULL);
+                       *p = '\0';
                        if (aflag)
                                pledge_wpath = 1;
-                       else if ((s->wfd = open(wfile,
+                       else if ((s->wfd = open(s->wfile,
                            O_WRONLY|O_APPEND|O_CREAT|O_TRUNC,
                            DEFFILEMODE)) == -1)
-                               error(FATAL, "%s: %s", wfile, strerror(errno));
+                               error(FATAL, "%s: %s", s->wfile, strerror(errno));
                        return (p);
                default:
                        error(COMPILE,
index aa76528..5e91484 100644 (file)
@@ -1,4 +1,4 @@
-.\"    $OpenBSD: sed.1,v 1.51 2017/12/07 09:52:26 martijn Exp $
+.\"    $OpenBSD: sed.1,v 1.52 2017/12/08 18:41:59 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: December 7 2017 $
+.Dd $Mdocdate: December 8 2017 $
 .Dt SED 1
 .Os
 .Sh NAME
@@ -255,7 +255,7 @@ as well as the
 flag to the
 .Ic s
 function,
-take an optional
+take a
 .Ar file
 parameter,
 which should be separated from the function or flag by whitespace.