From 1fba31e7b50cfe3a204c7e7473df07a5684cbef7 Mon Sep 17 00:00:00 2001 From: Benno Schulenberg Date: Sun, 28 Feb 2016 15:16:27 +0000 Subject: [PATCH] Using the now correct parsing of header regexes also for parsing magic regexes. This fixes Savannah bug #47292. git-svn-id: svn://svn.savannah.gnu.org/nano/trunk/nano@5692 35c25a1d-7b9e-4130-9fde-d3aeb78583b8 --- ChangeLog | 3 ++ src/rcfile.c | 93 ++++++++++++---------------------------------------- 2 files changed, 24 insertions(+), 72 deletions(-) diff --git a/ChangeLog b/ChangeLog index 00426b33..7f416196 100644 --- a/ChangeLog +++ b/ChangeLog @@ -1,6 +1,9 @@ 2016-02-28 Benno Schulenberg * src/rcfile.c (parse_header_exp): Don't continue when something is wrong -- skip the rest of the line. This fixes Savannah bug #47289. + * src/rcfile.c (parse_header_exp, parse_magic_exp, grab_and_store): + Use the now correct parsing of header regexes also for parsing magic + regexes. This fixes Savannah bug #47292 and saves 50 lines of code. 2016-02-26 Benno Schulenberg * doc/man/nanorc.5, doc/texinfo/nano.texi, doc/syntax/nanorc.nanorc, diff --git a/src/rcfile.c b/src/rcfile.c index 53e221de..de48ad6f 100644 --- a/src/rcfile.c +++ b/src/rcfile.c @@ -855,24 +855,25 @@ bool parse_color_names(char *combostr, short *fg, short *bg, bool *bright) return TRUE; } -/* Parse the header-line regexes that may influence the choice of syntax. */ -void parse_header_exp(char *ptr) -{ - regexlisttype *endheader = NULL; - assert(ptr != NULL); +/* Read regex strings enclosed in double quotes from the line pointed at + * by ptr, and store them quoteless in the passed storage place. */ +void grab_and_store(char *ptr, const char *kind, regexlisttype **storage) +{ + regexlisttype *lastthing = NULL; if (syntaxes == NULL) { rcfile_error( - N_("Cannot add a header regex without a syntax command")); + N_("A '%s' command requires a preceding 'syntax' command"), kind); return; } if (*ptr == '\0') { - rcfile_error(N_("Missing regex string")); + rcfile_error(N_("Missing regex string after '%s' command"), kind); return; } + /* Now load the regexes into their part of the struct. */ while (*ptr != '\0') { const char *regexstring; regexlisttype *newheader; @@ -893,84 +894,32 @@ void parse_header_exp(char *ptr) newheader = (regexlisttype *)nmalloc(sizeof(regexlisttype)); /* Save the regex string if it's valid. */ - if (nregcomp(regexstring, 0)) { + if (nregcomp(regexstring, REG_NOSUB)) { newheader->full_regex = mallocstrcpy(NULL, regexstring); newheader->rgx = NULL; - if (endheader == NULL) - endsyntax->headers = newheader; + if (lastthing == NULL) + *storage = newheader; else - endheader->next = newheader; - endheader = newheader; - endheader->next = NULL; + lastthing->next = newheader; + lastthing = newheader; + lastthing->next = NULL; } else free(newheader); } } +/* Parse the header-line regexes that may influence the choice of syntax. */ +void parse_header_exp(char *ptr) +{ + grab_and_store(ptr, "header", &endsyntax->headers); +} + #ifdef HAVE_LIBMAGIC /* Parse the magic regexes that may influence the choice of syntax. */ void parse_magic_exp(char *ptr) { - regexlisttype *endmagic = NULL; - - assert(ptr != NULL); - - if (syntaxes == NULL) { - rcfile_error( - N_("Cannot add a magic string regex without a syntax command")); - return; - } - - if (*ptr == '\0') { - rcfile_error(N_("Missing magic string name")); - return; - } - - if (*ptr != '"') { - rcfile_error( - N_("Regex strings must begin and end with a \" character")); - return; - } - -#ifdef DEBUG - fprintf(stderr, "Starting a magic type: \"%s\"\n", ptr); -#endif - - /* Now load the magic regexes into their part of the struct. */ - while (*ptr != '\0') { - const char *regexstring; - regexlisttype *newmagic; - - while (*ptr != '"' && *ptr != '\0') - ptr++; - - if (*ptr == '\0') - return; - - ptr++; - - regexstring = ptr; - ptr = parse_next_regex(ptr); - if (ptr == NULL) - break; - - newmagic = (regexlisttype *)nmalloc(sizeof(regexlisttype)); - - /* Save the regex string if it's valid. */ - if (nregcomp(regexstring, REG_NOSUB)) { - newmagic->full_regex = mallocstrcpy(NULL, regexstring); - newmagic->rgx = NULL; - - if (endmagic == NULL) - endsyntax->magics = newmagic; - else - endmagic->next = newmagic; - endmagic = newmagic; - endmagic->next = NULL; - } else - free(newmagic); - } + grab_and_store(ptr, "magic", &endsyntax->magics); } #endif /* HAVE_LIBMAGIC */ -- 2.39.5