Unnesting two ifs -- to put filenames, headerlines, and magic strings
on equal footing. git-svn-id: svn://svn.savannah.gnu.org/nano/trunk/nano@5724 35c25a1d-7b9e-4130-9fde-d3aeb78583b8master
parent
c5fedaa043
commit
4e8d2d6223
|
@ -1,6 +1,6 @@
|
||||||
2016-03-12 Benno Schulenberg <bensberg@justemail.net>
|
2016-03-12 Benno Schulenberg <bensberg@justemail.net>
|
||||||
* src/color.c (color_update): Set the syntax and regex pointers
|
* src/color.c (color_update): Set the syntax and regex pointers
|
||||||
just once, in a single place.
|
just once, in a single place. And unnest two 'if's.
|
||||||
|
|
||||||
2016-03-11 Benno Schulenberg <bensberg@justemail.net>
|
2016-03-11 Benno Schulenberg <bensberg@justemail.net>
|
||||||
* src/browser.c (do_browser): Fix compilation when configured with
|
* src/browser.c (do_browser): Fix compilation when configured with
|
||||||
|
|
95
src/color.c
95
src/color.c
|
@ -160,7 +160,7 @@ bool found_in_list(regexlisttype *head, const char *shibboleth)
|
||||||
return FALSE;
|
return FALSE;
|
||||||
}
|
}
|
||||||
|
|
||||||
/* Update the color information based on the current filename. */
|
/* Update the color information based on the current filename and content. */
|
||||||
void color_update(void)
|
void color_update(void)
|
||||||
{
|
{
|
||||||
syntaxtype *sint = NULL;
|
syntaxtype *sint = NULL;
|
||||||
|
@ -172,10 +172,9 @@ void color_update(void)
|
||||||
if (syntaxes == NULL)
|
if (syntaxes == NULL)
|
||||||
return;
|
return;
|
||||||
|
|
||||||
/* If we specified a syntax override string, use it. */
|
/* If we specified a syntax-override string, use it. */
|
||||||
if (syntaxstr != NULL) {
|
if (syntaxstr != NULL) {
|
||||||
/* If the syntax override is "none", it's the same as not having
|
/* An override of "none" is like having no syntax at all. */
|
||||||
* a syntax at all, so get out. */
|
|
||||||
if (strcmp(syntaxstr, "none") == 0)
|
if (strcmp(syntaxstr, "none") == 0)
|
||||||
return;
|
return;
|
||||||
|
|
||||||
|
@ -188,9 +187,8 @@ void color_update(void)
|
||||||
statusbar(_("Unknown syntax name: %s"), syntaxstr);
|
statusbar(_("Unknown syntax name: %s"), syntaxstr);
|
||||||
}
|
}
|
||||||
|
|
||||||
/* If we didn't specify a syntax override string, or if we did and
|
/* If no syntax-override string was specified, or it didn't match,
|
||||||
* there was no syntax by that name, get the syntax based on the
|
* try finding a syntax based on the filename (extension). */
|
||||||
* file extension, then try the headerline, and then try magic. */
|
|
||||||
if (sint == NULL) {
|
if (sint == NULL) {
|
||||||
char *currentdir = getcwd(NULL, PATH_MAX + 1);
|
char *currentdir = getcwd(NULL, PATH_MAX + 1);
|
||||||
char *joinednames = charalloc(PATH_MAX + 1);
|
char *joinednames = charalloc(PATH_MAX + 1);
|
||||||
|
@ -214,63 +212,62 @@ void color_update(void)
|
||||||
|
|
||||||
free(joinednames);
|
free(joinednames);
|
||||||
free(fullname);
|
free(fullname);
|
||||||
|
}
|
||||||
|
|
||||||
/* Check the headerline if the extension didn't match anything. */
|
/* If the filename didn't match anything, try the first line. */
|
||||||
if (sint == NULL) {
|
if (sint == NULL) {
|
||||||
#ifdef DEBUG
|
#ifdef DEBUG
|
||||||
fprintf(stderr, "No result from file extension, trying headerline...\n");
|
fprintf(stderr, "No result from file extension, trying headerline...\n");
|
||||||
#endif
|
#endif
|
||||||
for (sint = syntaxes; sint != NULL; sint = sint->next) {
|
for (sint = syntaxes; sint != NULL; sint = sint->next) {
|
||||||
if (found_in_list(sint->headers, openfile->fileage->data))
|
if (found_in_list(sint->headers, openfile->fileage->data))
|
||||||
break;
|
break;
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
|
||||||
#ifdef HAVE_LIBMAGIC
|
#ifdef HAVE_LIBMAGIC
|
||||||
/* Check magic if we don't have an answer yet. */
|
/* If we still don't have an answer, try using magic. */
|
||||||
if (sint == NULL) {
|
if (sint == NULL) {
|
||||||
struct stat fileinfo;
|
struct stat fileinfo;
|
||||||
magic_t cookie = NULL;
|
magic_t cookie = NULL;
|
||||||
const char *magicstring = NULL;
|
const char *magicstring = NULL;
|
||||||
#ifdef DEBUG
|
#ifdef DEBUG
|
||||||
fprintf(stderr, "No result from headerline either, trying libmagic...\n");
|
fprintf(stderr, "No result from headerline either, trying libmagic...\n");
|
||||||
#endif
|
#endif
|
||||||
if (stat(openfile->filename, &fileinfo) == 0) {
|
if (stat(openfile->filename, &fileinfo) == 0) {
|
||||||
/* Open the magic database and get a diagnosis of the file. */
|
/* Open the magic database and get a diagnosis of the file. */
|
||||||
cookie = magic_open(MAGIC_SYMLINK |
|
cookie = magic_open(MAGIC_SYMLINK |
|
||||||
#ifdef DEBUG
|
#ifdef DEBUG
|
||||||
MAGIC_DEBUG | MAGIC_CHECK |
|
MAGIC_DEBUG | MAGIC_CHECK |
|
||||||
#endif
|
#endif
|
||||||
MAGIC_ERROR);
|
MAGIC_ERROR);
|
||||||
if (cookie == NULL || magic_load(cookie, NULL) < 0)
|
if (cookie == NULL || magic_load(cookie, NULL) < 0)
|
||||||
statusbar(_("magic_load() failed: %s"), strerror(errno));
|
statusbar(_("magic_load() failed: %s"), strerror(errno));
|
||||||
else {
|
else {
|
||||||
magicstring = magic_file(cookie, openfile->filename);
|
magicstring = magic_file(cookie, openfile->filename);
|
||||||
if (magicstring == NULL) {
|
if (magicstring == NULL)
|
||||||
statusbar(_("magic_file(%s) failed: %s"),
|
statusbar(_("magic_file(%s) failed: %s"),
|
||||||
openfile->filename, magic_error(cookie));
|
openfile->filename, magic_error(cookie));
|
||||||
}
|
|
||||||
#ifdef DEBUG
|
#ifdef DEBUG
|
||||||
fprintf(stderr, "Returned magic string is: %s\n", magicstring);
|
fprintf(stderr, "Returned magic string is: %s\n", magicstring);
|
||||||
#endif
|
#endif
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/* Now try and find a syntax that matches the magicstring. */
|
|
||||||
if (magicstring != NULL) {
|
|
||||||
for (sint = syntaxes; sint != NULL; sint = sint->next) {
|
|
||||||
if (found_in_list(sint->magics, magicstring))
|
|
||||||
break;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
if (stat(openfile->filename, &fileinfo) == 0)
|
|
||||||
magic_close(cookie);
|
|
||||||
}
|
}
|
||||||
#endif /* HAVE_LIBMAGIC */
|
|
||||||
}
|
|
||||||
|
|
||||||
/* If we didn't find any syntax yet, see if there is a default one. */
|
/* Now try and find a syntax that matches the magic string. */
|
||||||
|
if (magicstring != NULL) {
|
||||||
|
for (sint = syntaxes; sint != NULL; sint = sint->next) {
|
||||||
|
if (found_in_list(sint->magics, magicstring))
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
if (stat(openfile->filename, &fileinfo) == 0)
|
||||||
|
magic_close(cookie);
|
||||||
|
}
|
||||||
|
#endif /* HAVE_LIBMAGIC */
|
||||||
|
|
||||||
|
/* If nothing at all matched, see if there is a default syntax. */
|
||||||
if (sint == NULL) {
|
if (sint == NULL) {
|
||||||
for (sint = syntaxes; sint != NULL; sint = sint->next) {
|
for (sint = syntaxes; sint != NULL; sint = sint->next) {
|
||||||
if (strcmp(sint->name, "default") == 0)
|
if (strcmp(sint->name, "default") == 0)
|
||||||
|
@ -281,8 +278,8 @@ void color_update(void)
|
||||||
openfile->syntax = sint;
|
openfile->syntax = sint;
|
||||||
openfile->colorstrings = (sint == NULL ? NULL : sint->color);
|
openfile->colorstrings = (sint == NULL ? NULL : sint->color);
|
||||||
|
|
||||||
/* If a syntax was found, compile its specified regexes, which have
|
/* If a syntax was found, compile its specified regexes (which have
|
||||||
* already been checked for validity when they were read in. */
|
* already been checked for validity when they were read in). */
|
||||||
for (ink = sint->color; ink != NULL; ink = ink->next) {
|
for (ink = sint->color; ink != NULL; ink = ink->next) {
|
||||||
if (ink->start == NULL) {
|
if (ink->start == NULL) {
|
||||||
ink->start = (regex_t *)nmalloc(sizeof(regex_t));
|
ink->start = (regex_t *)nmalloc(sizeof(regex_t));
|
||||||
|
|
Loading…
Reference in New Issue