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
This commit is contained in:
tjr 2002-06-22 01:42:26 +00:00
parent 66f8163870
commit 80b8872b68
2 changed files with 12 additions and 11 deletions

View File

@ -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++;

View File

@ -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;