From 265d3245af26672f1839bdfc87e1c1198546ee78 Mon Sep 17 00:00:00 2001 From: Benno Schulenberg Date: Mon, 7 Oct 2019 09:10:06 +0200 Subject: [PATCH] rcfile: process extensions to file-matching commands straightaway When 'extendsyntax' is used with a 'header' or 'magic' command, it must be processed immediately. It is pointless to store the command, because when then it is processed (when the syntax gets used), it is too late to have any effect. This fixes https://savannah.gnu.org/bugs/?56997. With-help-from: Brand Huntsman Bug existed since version 4.3, commit cba9d8d0. --- src/rcfile.c | 21 +++++++++++++++++++-- 1 file changed, 19 insertions(+), 2 deletions(-) diff --git a/src/rcfile.c b/src/rcfile.c index 257f617a..32b10f12 100644 --- a/src/rcfile.c +++ b/src/rcfile.c @@ -1001,6 +1001,7 @@ void parse_rcfile(FILE *rcstream, bool just_syntax, bool intros_only) while ((len = getline(&buffer, &size, rcstream)) > 0) { char *ptr, *keyword, *option; + bool drop_open = FALSE; int set = 0; size_t i; @@ -1032,6 +1033,7 @@ void parse_rcfile(FILE *rcstream, bool just_syntax, bool intros_only) if (!just_syntax && strcasecmp(keyword, "extendsyntax") == 0) { augmentstruct *newitem, *extra; char *syntaxname = ptr; + char *argument; syntaxtype *sint; check_for_nonempty_syntax(); @@ -1047,12 +1049,23 @@ void parse_rcfile(FILE *rcstream, bool just_syntax, bool intros_only) continue; } + argument = strdup(ptr); + keyword = ptr; + ptr = parse_next_word(ptr); + + /* File-matching commands need to be processed immediately; + * other commands are stored for possible later processing. */ + if (strcmp(keyword, "header") == 0 || strcmp(keyword, "magic") == 0) { + free(argument); + live_syntax = sint; + opensyntax = TRUE; + drop_open = TRUE; + } else { newitem = nmalloc(sizeof(augmentstruct));; - /* Store the content of an 'extendsyntax', for later parsing. */ newitem->filename = strdup(nanorc); newitem->lineno = lineno; - newitem->data = strdup(ptr); + newitem->data = argument; newitem->next = NULL; if (sint->augmentations != NULL) { @@ -1064,6 +1077,7 @@ void parse_rcfile(FILE *rcstream, bool just_syntax, bool intros_only) sint->augmentations = newitem; continue; + } } /* Try to parse the keyword. */ @@ -1120,6 +1134,9 @@ void parse_rcfile(FILE *rcstream, bool just_syntax, bool intros_only) else if (intros_only) jot_error(N_("Command \"%s\" not understood"), keyword); + if (drop_open) + opensyntax = FALSE; + if (set == 0) continue;