make sed work on 8-bit objects
authorElan Amir <elan@ucbvax.Berkeley.EDU>
Wed, 14 Apr 1993 21:22:36 +0000 (13:22 -0800)
committerElan Amir <elan@ucbvax.Berkeley.EDU>
Wed, 14 Apr 1993 21:22:36 +0000 (13:22 -0800)
TK checking in for Elan Amir

SCCS-vsn: usr.bin/sed/defs.h 5.4
SCCS-vsn: usr.bin/sed/extern.h 5.6
SCCS-vsn: usr.bin/sed/misc.c 5.4
SCCS-vsn: usr.bin/sed/process.c 5.14

usr/src/usr.bin/sed/defs.h
usr/src/usr.bin/sed/extern.h
usr/src/usr.bin/sed/misc.c
usr/src/usr.bin/sed/process.c

index e0c9de2..bdc3d2d 100644 (file)
@@ -8,7 +8,7 @@
  *
  * %sccs.include.redist.c%
  *
  *
  * %sccs.include.redist.c%
  *
- *     @(#)defs.h      5.3 (Berkeley) %G%
+ *     @(#)defs.h      5.4 (Berkeley) %G%
  */
 
 /*
  */
 
 /*
@@ -48,7 +48,7 @@ struct s_subst {
 
 /*
  * An internally compiled command.
 
 /*
  * An internally compiled command.
- * Initialy, label references are stored in u.t, on a second pass they
+ * Initialy, label references are stored in t, on a second pass they
  * are updated to pointers.
  */
 struct s_command {
  * are updated to pointers.
  */
 struct s_command {
@@ -64,6 +64,7 @@ struct s_command {
        char code;                              /* Command code */
        u_int nonsel:1;                         /* True if ! */
        u_int inrange:1;                        /* True if in range */
        char code;                              /* Command code */
        u_int nonsel:1;                         /* True if ! */
        u_int inrange:1;                        /* True if in range */
+       u_int lused:1;                          /* True if label used. */
 };
 
 /*
 };
 
 /*
@@ -89,11 +90,11 @@ enum e_args {
 struct s_appends {
        enum {AP_STRING, AP_FILE} type;
        char *s;
 struct s_appends {
        enum {AP_STRING, AP_FILE} type;
        char *s;
+       size_t len;
 };
 
 enum e_spflag {
        APPEND,                                 /* Append to the contents. */
 };
 
 enum e_spflag {
        APPEND,                                 /* Append to the contents. */
-       APPENDNL,                               /* Append, with newline. */
        REPLACE,                                /* Replace the contents. */
 };
 
        REPLACE,                                /* Replace the contents. */
 };
 
index cc058f8..991b8b5 100644 (file)
@@ -8,7 +8,7 @@
  *
  * %sccs.include.redist.c%
  *
  *
  * %sccs.include.redist.c%
  *
- *     @(#)extern.h    5.5 (Berkeley) %G%
+ *     @(#)extern.h    5.6 (Berkeley) %G%
  */
 
 extern struct s_command *prog;
  */
 
 extern struct s_command *prog;
@@ -21,13 +21,14 @@ extern int lastline;
 extern int aflag, eflag, nflag;
 extern char *fname;
 
 extern int aflag, eflag, nflag;
 extern char *fname;
 
+void    cfclose __P((struct s_command *, struct s_command *));
 void    compile __P((void));
 void    compile __P((void));
+void    cspace __P((SPACE *, char *, size_t, enum e_spflag));
 char   *cu_fgets __P((char *, int));
 void    err __P((int, const char *, ...));
 int     mf_fgets __P((SPACE *, enum e_spflag));
 void    process __P((void));
 char   *cu_fgets __P((char *, int));
 void    err __P((int, const char *, ...));
 int     mf_fgets __P((SPACE *, enum e_spflag));
 void    process __P((void));
+char   *strnchr __P((const char *, int c, size_t));
 char   *strregerror __P((int, regex_t *));
 void   *xmalloc __P((u_int));
 void   *xrealloc __P((void *, u_int));
 char   *strregerror __P((int, regex_t *));
 void   *xmalloc __P((u_int));
 void   *xrealloc __P((void *, u_int));
-void    cfclose __P((struct s_command *, struct s_command *));
-void    cspace __P((SPACE *, char *, size_t, enum e_spflag));
index 40d58d7..10cdace 100644 (file)
@@ -10,7 +10,7 @@
  */
 
 #ifndef lint
  */
 
 #ifndef lint
-static char sccsid[] = "@(#)misc.c     5.3 (Berkeley) %G%";
+static char sccsid[] = "@(#)misc.c     5.4 (Berkeley) %G%";
 #endif /* not lint */
 
 #include <sys/types.h>
 #endif /* not lint */
 
 #include <sys/types.h>
@@ -54,6 +54,20 @@ xrealloc(p, size)
        return (p);
 }
 
        return (p);
 }
 
+char *
+strnchr(buf, c, len)
+       const char *buf;
+       int c;
+       size_t len;
+{
+       const char *cp;
+       
+       for (cp = buf; cp - buf < len; cp++)
+               if (*cp == c)
+                       return ((char *)cp);
+       return (NULL);
+}
+
 /*
  * Return a string for a regular expression error passed.  This is a overkill,
  * because of the silly semantics of regerror (we can never know the size of
 /*
  * Return a string for a regular expression error passed.  This is a overkill,
  * because of the silly semantics of regerror (we can never know the size of
index c4b8841..3c7c02d 100644 (file)
@@ -10,7 +10,7 @@
  */
 
 #ifndef lint
  */
 
 #ifndef lint
-static char sccsid[] = "@(#)process.c  5.13 (Berkeley) %G%";
+static char sccsid[] = "@(#)process.c  5.14 (Berkeley) %G%";
 #endif /* not lint */
 
 #include <sys/types.h>
 #endif /* not lint */
 
 #include <sys/types.h>
@@ -52,13 +52,11 @@ int appendnum;                      /* Size of appends array. */
 static int lastaddr;           /* Set by applies if last address of a range. */
 static int sdone;              /* If any substitutes since last line input. */
                                /* Iov structure for 'w' commands. */
 static int lastaddr;           /* Set by applies if last address of a range. */
 static int sdone;              /* If any substitutes since last line input. */
                                /* Iov structure for 'w' commands. */
-static struct iovec iov[2] = { NULL, 0, "\n", 1 };
-
 static regex_t *defpreg;
 size_t maxnsub;
 static regex_t *defpreg;
 size_t maxnsub;
-regmatch_t *match, startend;
+regmatch_t *match;
 
 
-#define OUT(s) { fwrite(s, sizeof(u_char), psl, stdout); putchar('\n'); }
+#define OUT(s) { fwrite(s, sizeof(u_char), psl, stdout); }
 
 void
 process()
 
 void
 process()
@@ -118,13 +116,13 @@ redirect:
                                cspace(&PS, hs, hsl, REPLACE);
                                break;
                        case 'G':
                                cspace(&PS, hs, hsl, REPLACE);
                                break;
                        case 'G':
-                               cspace(&PS, hs, hsl, APPENDNL);
+                               cspace(&PS, hs, hsl, 0);
                                break;
                        case 'h':
                                cspace(&HS, ps, psl, REPLACE);
                                break;
                        case 'H':
                                break;
                        case 'h':
                                cspace(&HS, ps, psl, REPLACE);
                                break;
                        case 'H':
-                               cspace(&HS, ps, psl, APPENDNL);
+                               cspace(&HS, ps, psl, 0);
                                break;
                        case 'i':
                                (void)printf("%s", cp->t);
                                break;
                        case 'i':
                                (void)printf("%s", cp->t);
@@ -145,7 +143,7 @@ redirect:
                                break;
                        case 'N':
                                flush_appends();
                                break;
                        case 'N':
                                flush_appends();
-                               if (!mf_fgets(&PS, APPENDNL)) {
+                               if (!mf_fgets(&PS, 0)) {
                                        if (!nflag && !pd)
                                                OUT(ps)
                                        exit(0);
                                        if (!nflag && !pd)
                                                OUT(ps)
                                        exit(0);
@@ -200,9 +198,7 @@ redirect:
                                    DEFFILEMODE)) == -1)
                                        err(FATAL, "%s: %s\n",
                                            cp->t, strerror(errno));
                                    DEFFILEMODE)) == -1)
                                        err(FATAL, "%s: %s\n",
                                            cp->t, strerror(errno));
-                               iov[0].iov_base = ps;
-                               iov[0].iov_len = psl;
-                               if (writev(cp->u.fd, iov, 2) != psl + 1)
+                               if (write(cp->u.fd, ps, psl) != psl)
                                        err(FATAL, "%s: %s\n",
                                            cp->t, strerror(errno));
                                break;
                                        err(FATAL, "%s: %s\n",
                                            cp->t, strerror(errno));
                                break;
@@ -216,7 +212,7 @@ redirect:
                        case 'y':
                                if (pd)
                                        break;
                        case 'y':
                                if (pd)
                                        break;
-                               for (p = ps, len = psl; len--; ++p)
+                               for (p = ps, len = psl; --len; ++p)
                                        *p = cp->u.y[*p];
                                break;
                        case ':':
                                        *p = cp->u.y[*p];
                                break;
                        case ':':
@@ -369,9 +365,7 @@ substitute(cp)
                if (cp->u.s->wfd == -1 && (cp->u.s->wfd = open(cp->u.s->wfile,
                    O_WRONLY|O_APPEND|O_CREAT|O_TRUNC, DEFFILEMODE)) == -1)
                        err(FATAL, "%s: %s\n", cp->u.s->wfile, strerror(errno));
                if (cp->u.s->wfd == -1 && (cp->u.s->wfd = open(cp->u.s->wfile,
                    O_WRONLY|O_APPEND|O_CREAT|O_TRUNC, DEFFILEMODE)) == -1)
                        err(FATAL, "%s: %s\n", cp->u.s->wfile, strerror(errno));
-               iov[0].iov_base = ps;
-               iov[0].iov_len = psl;   
-               if (writev(cp->u.s->wfd, iov, 2) != psl + 1)
+               if (write(cp->u.s->wfd, ps, psl) != psl)
                        err(FATAL, "%s: %s\n", cp->u.s->wfile, strerror(errno));
        }
        return (1);
                        err(FATAL, "%s: %s\n", cp->u.s->wfile, strerror(errno));
        }
        return (1);
@@ -405,8 +399,7 @@ flush_appends()
                         */
                        if ((f = fopen(appends[i].s, "r")) == NULL)
                                break;
                         */
                        if ((f = fopen(appends[i].s, "r")) == NULL)
                                break;
-                       while (count = fread(buf, sizeof(char), sizeof(buf), 
-                           f))
+                       while (count = fread(buf, sizeof(char), sizeof(buf), f))
                                (void)fwrite(buf, sizeof(char), count, stdout);
                        (void)fclose(f);
                        break;
                                (void)fwrite(buf, sizeof(char), count, stdout);
                        (void)fclose(f);
                        break;
@@ -449,7 +442,7 @@ lputs(s)
                                (void)putchar("\\abfnrtv"[p - escapes]);
                                count += 2;
                        } else {
                                (void)putchar("\\abfnrtv"[p - escapes]);
                                count += 2;
                        } else {
-                               (void)printf("%03o", (u_char)*s);
+                               (void)printf("%03o", *(u_char *)s);
                                count += 4;
                        }
                }
                                count += 4;
                        }
                }
@@ -469,22 +462,17 @@ regexec_e(preg, string, eflags, nomatch, slen)
 {
        int eval;
        
 {
        int eval;
        
-       /* So we can work with binary files */
-       startend.rm_so = 0;
-       startend.rm_eo = slen;
-       match[0] = startend;
-       
-
-       eflags |= REG_STARTEND; 
-       
        if (preg == NULL) {
                if (defpreg == NULL)
                        err(FATAL, "first RE may not be empty");
        } else
                defpreg = preg;
 
        if (preg == NULL) {
                if (defpreg == NULL)
                        err(FATAL, "first RE may not be empty");
        } else
                defpreg = preg;
 
+       match[0].rm_so = 0;
+       match[0].rm_eo = slen - 1;      /* Length minus trailing newline. */
+       
        eval = regexec(defpreg, string,
        eval = regexec(defpreg, string,
-           nomatch ? 0 : maxnsub + 1, match, eflags);
+           nomatch ? 0 : maxnsub + 1, match, eflags | REG_STARTEND);
        switch(eval) {
        case 0:
                return (1);
        switch(eval) {
        case 0:
                return (1);
@@ -554,20 +542,14 @@ cspace(sp, p, len, spflag)
 {
        size_t tlen;
 
 {
        size_t tlen;
 
-       /*
-        * Make sure SPACE has enough memory and ramp up quickly.  Appends
-        * need two extra bytes, one for the newline, one for a terminating
-        * NULL.
-        */
-       tlen = sp->len + len + (spflag == APPENDNL ? 2 : 1);
+       /* Make sure SPACE has enough memory and ramp up quickly. */
+       tlen = sp->len + len + 1;
        if (tlen > sp->blen) {
                sp->blen = tlen + 1024;
                sp->space = sp->back = xrealloc(sp->back, sp->blen);
        }
 
        if (tlen > sp->blen) {
                sp->blen = tlen + 1024;
                sp->space = sp->back = xrealloc(sp->back, sp->blen);
        }
 
-       if (spflag == APPENDNL)
-               sp->space[sp->len++] = '\n';
-       else if (spflag == REPLACE)
+       if (spflag == REPLACE)
                sp->len = 0;
 
        memmove(sp->space + sp->len, p, len);
                sp->len = 0;
 
        memmove(sp->space + sp->len, p, len);