/* Compile the regular expression regex to see if it's valid. Return * TRUE if it is, or FALSE otherwise. */ bool nregcomp(const char *regex, int eflags) { regex_t preg; const char *r = fixbounds(regex); int rc = regcomp(&preg, r, REG_EXTENDED | eflags); if (rc != 0) { size_t len = regerror(rc, &preg, NULL, 0); char *str = charalloc(len); regerror(rc, &preg, str, len); rcfile_error(N_("Bad regex \"%s\": %s"), r, str); free(str); } regfree(&preg); return (rc == 0); }
/* Update the color information based on the current filename. */ void color_update(void) { syntaxtype *tmpsyntax; syntaxtype *defsyntax = NULL; colortype *tmpcolor, *defcolor = NULL; assert(openfile != NULL); openfile->syntax = NULL; openfile->colorstrings = NULL; /* If we specified a syntax override string, use it. */ if (syntaxstr != NULL) { /* If the syntax override is "none", it's the same as not having * a syntax at all, so get out. */ if (strcmp(syntaxstr, "none") == 0) return; for (tmpsyntax = syntaxes; tmpsyntax != NULL; tmpsyntax = tmpsyntax->next) { if (strcmp(tmpsyntax->desc, syntaxstr) == 0) { openfile->syntax = tmpsyntax; openfile->colorstrings = tmpsyntax->color; } if (openfile->colorstrings != NULL) break; } } /* If we didn't specify a syntax override string, or if we did and * there was no syntax by that name, get the syntax based on the * file extension, and then look in the header. */ if (openfile->colorstrings == NULL) { for (tmpsyntax = syntaxes; tmpsyntax != NULL; tmpsyntax = tmpsyntax->next) { exttype *e; /* If this is the default syntax, it has no associated * extensions, which we've checked for elsewhere. Skip over * it here, but keep track of its color regexes. */ if (strcmp(tmpsyntax->desc, "default") == 0) { defsyntax = tmpsyntax; defcolor = tmpsyntax->color; continue; } for (e = tmpsyntax->extensions; e != NULL; e = e->next) { bool not_compiled = (e->ext == NULL); /* e->ext_regex has already been checked for validity * elsewhere. Compile its specified regex if we haven't * already. */ if (not_compiled) { e->ext = (regex_t *)nmalloc(sizeof(regex_t)); regcomp(e->ext, fixbounds(e->ext_regex), REG_EXTENDED); } /* Set colorstrings if we matched the extension * regex. */ if (regexec(e->ext, openfile->filename, 0, NULL, 0) == 0) { openfile->syntax = tmpsyntax; openfile->colorstrings = tmpsyntax->color; } if (openfile->colorstrings != NULL) break; /* Decompile e->ext_regex's specified regex if we aren't * going to use it. */ if (not_compiled) { regfree(e->ext); free(e->ext); e->ext = NULL; } } } /* If we haven't matched anything yet, try the headers */ if (openfile->colorstrings == NULL) { #ifdef DEBUG fprintf(stderr, "No match for file extensions, looking at headers...\n"); #endif for (tmpsyntax = syntaxes; tmpsyntax != NULL; tmpsyntax = tmpsyntax->next) { exttype *e; for (e = tmpsyntax->headers; e != NULL; e = e->next) { bool not_compiled = (e->ext == NULL); /* e->ext_regex has already been checked for validity * elsewhere. Compile its specified regex if we haven't * already. */ if (not_compiled) { e->ext = (regex_t *)nmalloc(sizeof(regex_t)); regcomp(e->ext, fixbounds(e->ext_regex), REG_EXTENDED); } /* Set colorstrings if we matched the extension * regex. */ #ifdef DEBUG fprintf(stderr, "Comparing header regex \"%s\" to fileage \"%s\"...\n", e->ext_regex, openfile->fileage->data); #endif if (regexec(e->ext, openfile->fileage->data, 0, NULL, 0) == 0) { openfile->syntax = tmpsyntax; openfile->colorstrings = tmpsyntax->color; } if (openfile->colorstrings != NULL) break; /* Decompile e->ext_regex's specified regex if we aren't * going to use it. */ if (not_compiled) { regfree(e->ext); free(e->ext); e->ext = NULL; } } } } } /* If we didn't get a syntax based on the file extension, and we * have a default syntax, use it. */ if (openfile->colorstrings == NULL && defcolor != NULL) { openfile->syntax = defsyntax; openfile->colorstrings = defcolor; } for (tmpcolor = openfile->colorstrings; tmpcolor != NULL; tmpcolor = tmpcolor->next) { /* tmpcolor->start_regex and tmpcolor->end_regex have already * been checked for validity elsewhere. Compile their specified * regexes if we haven't already. */ if (tmpcolor->start == NULL) { tmpcolor->start = (regex_t *)nmalloc(sizeof(regex_t)); regcomp(tmpcolor->start, fixbounds(tmpcolor->start_regex), REG_EXTENDED | (tmpcolor->icase ? REG_ICASE : 0)); } if (tmpcolor->end_regex != NULL && tmpcolor->end == NULL) { tmpcolor->end = (regex_t *)nmalloc(sizeof(regex_t)); regcomp(tmpcolor->end, fixbounds(tmpcolor->end_regex), REG_EXTENDED | (tmpcolor->icase ? REG_ICASE : 0)); } } }