From 8c0545c8a64331338ecc4abbfdc8b7c08115b4e8 Mon Sep 17 00:00:00 2001 From: Robert Pengelly Date: Sun, 23 Mar 2025 14:54:07 +0000 Subject: [PATCH] More BSD compatibility and bug fixes --- read.c | 92 ++++++++++++++++++++++++++++++++++++---------------------- 1 file changed, 57 insertions(+), 35 deletions(-) diff --git a/read.c b/read.c index eb6f953..7cb1c7f 100644 --- a/read.c +++ b/read.c @@ -155,10 +155,38 @@ static void remove_comment (char *line) { } +struct if_stack { + + struct if_stack *prev; + + int ignoring; + int prev_ignoring; + int has_else; + +}; + +static struct if_stack *cur_if_stack = 0; + +static void add_if_stack (void) { + + struct if_stack *if_stack = xmalloc (sizeof *if_stack); + + if (cur_if_stack) { + if_stack->prev_ignoring = cur_if_stack->ignoring; + } + + if_stack->prev = cur_if_stack; + cur_if_stack = if_stack; + +} + static int include_makefile (const char *filename, int bsd) { + struct if_stack *saved_if_stack = cur_if_stack; char *path, *new_name; + unsigned long i; + cur_if_stack = 0; if (bsd) { @@ -196,7 +224,10 @@ static int include_makefile (const char *filename, int bsd) { } if (!read_makefile (filename)) { + + cur_if_stack = saved_if_stack; return 0; + } for (i = 0; i < state->nb_include_paths; i++) { @@ -208,6 +239,8 @@ static int include_makefile (const char *filename, int bsd) { if (!read_makefile (new_name)) { + cur_if_stack = saved_if_stack; + free (new_name); return 0; @@ -222,31 +255,6 @@ static int include_makefile (const char *filename, int bsd) { } -struct if_stack { - - struct if_stack *prev; - - int ignoring; - int prev_ignoring; - int has_else; - -}; - -static struct if_stack *cur_if_stack = 0; - -static void add_if_stack (void) { - - struct if_stack *if_stack = xmalloc (sizeof *if_stack); - - if (cur_if_stack) { - if_stack->prev_ignoring = cur_if_stack->ignoring; - } - - if_stack->prev = cur_if_stack; - cur_if_stack = if_stack; - -} - static char *skip_whitespace (char *p) { while (isspace ((int) *p)) { @@ -323,9 +331,13 @@ static int read_lbuf (struct linebuf *lbuf, int set_default) { goto is_command; } - if (strncmp (p, "else", 4) == 0) { + if (strncmp (p, ".else", 5) == 0 || strncmp (p, "else", 4) == 0) { - p += 4; + if (*p == '.') { + p = skip_whitespace (p + 5); + } else { + p = skip_whitespace (p + 4); + } if (!cur_if_stack) { @@ -334,8 +346,6 @@ static int read_lbuf (struct linebuf *lbuf, int set_default) { } - p = skip_whitespace (p); - if (strncmp (p, "ifeq", 4) == 0 || strncmp (p, "ifneq", 5) == 0 || strncmp (p, "ifdef", 5) == 0 || strncmp (p, "ifndef", 6) == 0) { after_else = 1; } else { @@ -378,6 +388,11 @@ static int read_lbuf (struct linebuf *lbuf, int set_default) { if (isspace ((int) *p)) { p = skip_whitespace (p); + } else if (*p == '"' || *p == '\'') { + + fprintf (stderr, "%s: *** missing separator (%s must be followed by whitespace). Stop.\n", program_name, ifneq ? "ifneq" : "ifeq"); + exit (EXIT_FAILURE); + } if (*p == '"' || *p == '\'') { @@ -447,7 +462,6 @@ static int read_lbuf (struct linebuf *lbuf, int set_default) { } p = skip_whitespace (p); - fprintf (stderr, "%s\n", p); if (*p || !arg1 || !arg2) { fprintf (stderr, "%s: extraneous text after '%s' directive\n", program_name, ifneq ? "ifneq" : "ifeq"); @@ -483,11 +497,15 @@ static int read_lbuf (struct linebuf *lbuf, int set_default) { } - if (strncmp (p, "ifdef", 5) == 0 || strncmp (p, "ifndef", 6) == 0) { + if (strncmp (p, ".ifdef", 6) == 0 || strncmp (p, "ifdef", 5) == 0 || strncmp (p, ".ifndef", 7) == 0 || strncmp (p, "ifndef", 6) == 0) { struct variable *var; int ifndef; + if (*p == '.') { + p++; + } + if (strncmp (p, "ifdef", 5) == 0) { ifndef = 0; @@ -543,9 +561,13 @@ static int read_lbuf (struct linebuf *lbuf, int set_default) { } - if (strncmp (p, "endif", 5) == 0) { + if (strncmp (p, ".endif", 6) == 0 || strncmp (p, "endif", 5) == 0) { - p = skip_whitespace (p + 5); + if (*p == '.') { + p = skip_whitespace (p + 6); + } else { + p = skip_whitespace (p + 5); + } if (!cur_if_stack) { @@ -619,7 +641,7 @@ static int read_lbuf (struct linebuf *lbuf, int set_default) { } - if (strncmp (p, ".include", 8) == 0 || strncmp (p, "include", 7) == 0) { + if (strncmp (p, ".include", 8) == 0 || (strncmp (p, "include", 7) == 0 && (isspace ((int) p[7]) || p[7] == '\0'))) { int bsd = 0; @@ -741,7 +763,7 @@ static int read_lbuf (struct linebuf *lbuf, int set_default) { for (ns = filenames; ns; ns = ns->next) { - if ((ns->name[0] == '.') && (strchr (ns->name, '\\') == 0) && (strchr (ns->name, '/') == 0)) { + if ((ns->name[0] == '.') && strchr (ns->name, '\\') == 0 && strchr (ns->name, '/') == 0) { continue; } -- 2.34.1