diff options
| author | rillig <rillig@NetBSD.org> | 2020-12-19 00:20:57 +0000 |
|---|---|---|
| committer | rillig <rillig@NetBSD.org> | 2020-12-19 00:20:57 +0000 |
| commit | c2cc4912bda33406769a23f885b67f0c32eeffb5 (patch) | |
| tree | 7338f293074f1b54a3370a96e5071ba58699de8e /usr.bin/make/parse.c | |
| parent | e139a7e9faf792ce8162de723bfcd75820be755f (diff) | |
make(1): extract ParseRawLine from ParseGetLine
Diffstat (limited to 'usr.bin/make/parse.c')
| -rw-r--r-- | usr.bin/make/parse.c | 131 |
1 files changed, 78 insertions, 53 deletions
diff --git a/usr.bin/make/parse.c b/usr.bin/make/parse.c index 0537b56d826..31627f6703a 100644 --- a/usr.bin/make/parse.c +++ b/usr.bin/make/parse.c @@ -1,4 +1,4 @@ -/* $NetBSD: parse.c,v 1.488 2020/12/19 00:02:34 rillig Exp $ */ +/* $NetBSD: parse.c,v 1.489 2020/12/19 00:20:57 rillig Exp $ */ /* * Copyright (c) 1988, 1989, 1990, 1993 @@ -117,7 +117,7 @@ #include "pathnames.h" /* "@(#)parse.c 8.3 (Berkeley) 3/19/94" */ -MAKE_RCSID("$NetBSD: parse.c,v 1.488 2020/12/19 00:02:34 rillig Exp $"); +MAKE_RCSID("$NetBSD: parse.c,v 1.489 2020/12/19 00:20:57 rillig Exp $"); /* types and constants */ @@ -2656,6 +2656,79 @@ ParseEOF(void) return TRUE; } +/* + * Parse a line, joining physical lines that end with backslash-newline. + * Do not unescape "\#", that's done by UnescapeBackslash. + */ +static Boolean +ParseRawLine(char **inout_line, char **out_ptr, char **out_line_end, + char **out_escaped, char **out_comment, char *inout_ch, + IFile *const cf) +{ + char *line = *inout_line; + char *ptr = line; + char *line_end = line; + char *escaped = NULL; + char *comment = NULL; + char ch = *inout_ch; + + for (;;) { + if (ptr == cf->buf_end) { + /* end of buffer */ + ch = '\0'; + break; + } + + ch = *ptr; + if (ch == '\0' || + (ch == '\\' && ptr + 1 < cf->buf_end && + ptr[1] == '\0')) { + Parse_Error(PARSE_FATAL, + "Zero byte read from file"); + return FALSE; + } + + /* + * Don't treat next character after '\' as special, + * remember first one. + */ + if (ch == '\\') { + if (escaped == NULL) + escaped = ptr; + if (ptr[1] == '\n') + cf->lineno++; + ptr += 2; + line_end = ptr; + continue; + } + + /* + * Remember the first '#' for comment stripping, + * unless the previous char was '[', as in the + * modifier ':[#]'. + */ + if (ch == '#' && comment == NULL && + !(ptr > line && ptr[-1] == '[')) + comment = line_end; + + ptr++; + if (ch == '\n') + break; + + /* We are not interested in trailing whitespace. */ + if (!ch_isspace(ch)) + line_end = ptr; + } + + *inout_line = line; + *out_ptr = ptr; + *out_line_end = line_end; + *out_escaped = escaped; + *out_comment = comment; + *inout_ch = ch; + return TRUE; +} + static void UnescapeBackslash(char *escaped, char *const line) { @@ -2733,57 +2806,9 @@ ParseGetLine(GetLineMode mode) for (;;) { cf->lineno++; line = cf->buf_ptr; - ptr = line; - line_end = line; - escaped = NULL; - comment = NULL; - for (;;) { - if (ptr == cf->buf_end) { - /* end of buffer */ - ch = '\0'; - break; - } - - ch = *ptr; - if (ch == '\0' || - (ch == '\\' && ptr + 1 < cf->buf_end && - ptr[1] == '\0')) { - Parse_Error(PARSE_FATAL, - "Zero byte read from file"); - return NULL; - } - - /* - * Don't treat next character after '\' as special, - * remember first one. - */ - if (ch == '\\') { - if (escaped == NULL) - escaped = ptr; - if (ptr[1] == '\n') - cf->lineno++; - ptr += 2; - line_end = ptr; - continue; - } - - /* - * Remember the first '#' for comment stripping, - * unless the previous char was '[', as in the - * modifier ':[#]'. - */ - if (ch == '#' && comment == NULL && - !(ptr > line && ptr[-1] == '[')) - comment = line_end; - - ptr++; - if (ch == '\n') - break; - - /* We are not interested in trailing whitespace. */ - if (!ch_isspace(ch)) - line_end = ptr; - } + if (!ParseRawLine(&line, &ptr, &line_end, &escaped, &comment, + &ch, cf)) + return NULL; /* Save next 'to be processed' location */ cf->buf_ptr = ptr; |
