From 80b8872b68e77010ee7a970c3ab9d029daf6af5b Mon Sep 17 00:00:00 2001 From: tjr Date: Sat, 22 Jun 2002 01:42:26 +0000 Subject: [PATCH] Don't store newlines at the end of each line in the hold/pattern spaces, instead add the newline when the pattern space is printed. Make the `G' and `H' commands add a newline to the space before the data, remove bogus addition of newline from `x' command. PR: 29790, 38195 --- usr.bin/sed/main.c | 2 ++ usr.bin/sed/process.c | 21 ++++++++++----------- 2 files changed, 12 insertions(+), 11 deletions(-) diff --git a/usr.bin/sed/main.c b/usr.bin/sed/main.c index 853003f99fdf..e64cfe7dda1b 100644 --- a/usr.bin/sed/main.c +++ b/usr.bin/sed/main.c @@ -362,6 +362,8 @@ mf_fgets(sp, spflag) p = fgetln(f, &len); if (ferror(f)) errx(1, "%s: %s", fname, strerror(errno ? errno : EIO)); + if (len != 0 && p[len - 1] == '\n') + len--; cspace(sp, p, len, spflag); linenum++; diff --git a/usr.bin/sed/process.c b/usr.bin/sed/process.c index 9816b1b2bd0c..6c1463171cc8 100644 --- a/usr.bin/sed/process.c +++ b/usr.bin/sed/process.c @@ -86,7 +86,7 @@ static regex_t *defpreg; size_t maxnsub; regmatch_t *match; -#define OUT(s) { fwrite(s, sizeof(u_char), psl, stdout); } +#define OUT(s) { fwrite(s, sizeof(u_char), psl, stdout); putchar('\n'); } void process() @@ -149,14 +149,14 @@ process() cspace(&PS, hs, hsl, REPLACE); break; case 'G': - if (hs == NULL) - cspace(&HS, "\n", 1, REPLACE); + cspace(&PS, "\n", 1, 0); cspace(&PS, hs, hsl, 0); break; case 'h': cspace(&HS, ps, psl, REPLACE); break; case 'H': + cspace(&HS, "\n", 1, 0); cspace(&HS, ps, psl, 0); break; case 'i': @@ -175,6 +175,7 @@ process() break; case 'N': flush_appends(); + cspace(&PS, "\n", 1, 0); if (!mf_fgets(&PS, 0)) { if (!nflag && !pd) OUT(ps) @@ -192,7 +193,7 @@ process() if (psl != 0 && (p = memchr(ps, '\n', psl - 1)) != NULL) { oldpsl = psl; - psl = (p + 1) - ps; + psl = p - ps; } OUT(ps) if (p != NULL) @@ -231,12 +232,11 @@ process() O_WRONLY|O_APPEND|O_CREAT|O_TRUNC, DEFFILEMODE)) == -1) err(1, "%s", cp->t); - if (write(cp->u.fd, ps, psl) != psl) + if (write(cp->u.fd, ps, psl) != psl || + write(cp->u.fd, "\n", 1) != 1) err(1, "%s", cp->t); break; case 'x': - if (hs == NULL) - cspace(&HS, "\n", 1, REPLACE); tspace = PS; PS = HS; HS = tspace; @@ -419,7 +419,8 @@ 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(1, "%s", cp->u.s->wfile); - if (write(cp->u.s->wfd, ps, psl) != psl) + if (write(cp->u.s->wfd, ps, psl) != psl || + write(cp->u.s->wfd, "\n", 1) != 1) err(1, "%s", cp->u.s->wfile); } return (1); @@ -523,9 +524,7 @@ regexec_e(preg, string, eflags, nomatch, slen) } else defpreg = preg; - /* Set anchors, discounting trailing newline (if any). */ - if (slen > 0 && string[slen - 1] == '\n') - slen--; + /* Set anchors */ match[0].rm_so = 0; match[0].rm_eo = slen;