From c56690ef7b53452d0898b757de3ad1362c75f59d Mon Sep 17 00:00:00 2001 From: Archie Cobbs Date: Tue, 8 Dec 1998 21:29:22 +0000 Subject: [PATCH] Fix a new bug introduced by the previous bug fix --- usr.bin/sed/compile.c | 13 +++++++------ usr.bin/sed/extern.h | 2 +- usr.bin/sed/main.c | 18 +++++++++++++++--- 3 files changed, 23 insertions(+), 10 deletions(-) diff --git a/usr.bin/sed/compile.c b/usr.bin/sed/compile.c index bf0b56f165d4..1313fcaec0f0 100644 --- a/usr.bin/sed/compile.c +++ b/usr.bin/sed/compile.c @@ -40,7 +40,7 @@ static char sccsid[] = "@(#)compile.c 8.1 (Berkeley) 6/6/93"; #endif static const char rcsid[] = - "$Id: compile.c,v 1.10 1998/09/22 18:39:47 brian Exp $"; + "$Id: compile.c,v 1.11 1998/12/07 05:35:54 archie Exp $"; #endif /* not lint */ #include @@ -160,7 +160,7 @@ compile_stream(link) stack = 0; for (;;) { - if ((p = cu_fgets(lbuf, sizeof(lbuf))) == NULL) { + if ((p = cu_fgets(lbuf, sizeof(lbuf), NULL)) == NULL) { if (stack != 0) errx(1, "%lu: %s: unexpected EOF (pending }'s)", linenum, fname); @@ -456,6 +456,7 @@ compile_subst(p, s) static char lbuf[_POSIX2_LINE_MAX + 1]; int asize, ref, size; char c, *text, *op, *sp; + int more = 0; c = *p++; /* Terminator character */ if (c == '\0') @@ -483,8 +484,8 @@ compile_subst(p, s) } else if (*p == '&' || *p == '\\') *sp++ = '\\'; } else if (*p == c) { - if (*++p == '\0') { - if (cu_fgets(lbuf, sizeof(lbuf))) + if (*++p == '\0' && more) { + if (cu_fgets(lbuf, sizeof(lbuf), &more)) p = lbuf; } *sp++ = '\0'; @@ -503,7 +504,7 @@ compile_subst(p, s) asize *= 2; text = xrealloc(text, asize); } - } while (cu_fgets(p = lbuf, sizeof(lbuf))); + } while (cu_fgets(p = lbuf, sizeof(lbuf), &more)); errx(1, "%lu: %s: unterminated substitute in regular expression", linenum, fname); /* NOTREACHED */ @@ -636,7 +637,7 @@ compile_text() asize = 2 * _POSIX2_LINE_MAX + 1; text = xmalloc(asize); size = 0; - while (cu_fgets(lbuf, sizeof(lbuf))) { + while (cu_fgets(lbuf, sizeof(lbuf), NULL)) { op = s = text + size; p = lbuf; EATSPACE(); diff --git a/usr.bin/sed/extern.h b/usr.bin/sed/extern.h index a80f7c94dbce..584ac6f6d3af 100644 --- a/usr.bin/sed/extern.h +++ b/usr.bin/sed/extern.h @@ -50,7 +50,7 @@ extern char *fname; void cfclose __P((struct s_command *, struct s_command *)); void compile __P((void)); void cspace __P((SPACE *, char *, size_t, enum e_spflag)); -char *cu_fgets __P((char *, int)); +char *cu_fgets __P((char *, int, int *)); int mf_fgets __P((SPACE *, enum e_spflag)); void process __P((void)); char *strregerror __P((int, regex_t *)); diff --git a/usr.bin/sed/main.c b/usr.bin/sed/main.c index 6e1ef35c6916..353dedbc8e3d 100644 --- a/usr.bin/sed/main.c +++ b/usr.bin/sed/main.c @@ -46,7 +46,7 @@ static const char copyright[] = static char sccsid[] = "@(#)main.c 8.2 (Berkeley) 1/3/94"; #endif static const char rcsid[] = - "$Id$"; + "$Id: main.c,v 1.7 1997/08/11 07:21:03 charnier Exp $"; #endif /* not lint */ #include @@ -176,9 +176,10 @@ usage() * together. Empty strings and files are ignored. */ char * -cu_fgets(buf, n) +cu_fgets(buf, n, more) char *buf; int n; + int *more; { static enum {ST_EOF, ST_FILE, ST_STRING} state = ST_EOF; static FILE *f; /* Current open file */ @@ -189,8 +190,11 @@ cu_fgets(buf, n) again: switch (state) { case ST_EOF: - if (script == NULL) + if (script == NULL) { + if (more != NULL) + *more = 0; return (NULL); + } linenum = 0; switch (script->type) { case CU_FILE: @@ -215,6 +219,8 @@ cu_fgets(buf, n) linenum++; if (linenum == 1 && buf[0] == '#' && buf[1] == 'n') nflag = 1; + if (more != NULL) + *more = !feof(f); return (p); } script = script->next; @@ -229,6 +235,8 @@ cu_fgets(buf, n) if (n-- <= 1) { *p = '\0'; linenum++; + if (more != NULL) + *more = 1; return (buf); } switch (*s) { @@ -241,6 +249,8 @@ cu_fgets(buf, n) script = script->next; *p = '\0'; linenum++; + if (more != NULL) + *more = 0; return (buf); } case '\n': @@ -248,6 +258,8 @@ cu_fgets(buf, n) *p = '\0'; s++; linenum++; + if (more != NULL) + *more = 0; return (buf); default: *p++ = *s++;