X-Git-Url: https://git.distorted.org.uk/~mdw/tig/blobdiff_plain/bec81df939d86351dbf008444799e759722df3b6..3df9850aa21a39e42ed4e720ad08c98610166a28:/tig.c diff --git a/tig.c b/tig.c index a3db54b..c7a7842 100644 --- a/tig.c +++ b/tig.c @@ -562,7 +562,7 @@ parse_options(int argc, char *argv[]) /** * FILES * ----- - * '~/.tig':: + * '~/.tigrc':: * User configuration file. See "<>" * section for examples. * @@ -574,7 +574,7 @@ parse_options(int argc, char *argv[]) * [[config-options]] * User Configuration file * ----------------------- - * You can permanently set an option by putting it in the `~/.tig` file. + * You can permanently set an option by putting it in the `~/.tigrc` file. * The file consists of a series of 'commands'. Each * line of the file may contain only one command. * @@ -845,30 +845,23 @@ struct line { }; +/* + * User config file handling. + */ + #define set_color(color, name, namelen) \ set_from_int_map(color_map, ARRAY_SIZE(color_map), color, name, namelen) #define set_attribute(attr, name, namelen) \ set_from_int_map(attr_map, ARRAY_SIZE(attr_map), attr, name, namelen) +static int config_lineno; +static bool config_errors; +static char *config_msg; + static int -read_option(char *opt, int optlen, char *value, int valuelen) +set_option(char *opt, int optlen, char *value, int valuelen) { - optlen = strcspn(opt, "#;"); - if (optlen == 0) - /* The whole line is a comment. */ - return OK; - - else if (opt[optlen] != 0) - /* Part of the option name is a comment, so the value part - * should be ignored. */ - valuelen = 0; - else - /* Else look for comment endings in the value. */ - valuelen = strcspn(value, "#;"); - - opt[optlen] = value[valuelen] = 0; - /* Reads: "color" object fgcolor bgcolor [attr] */ if (!strcmp(opt, "color")) { struct line_info *info; @@ -876,23 +869,31 @@ read_option(char *opt, int optlen, char *value, int valuelen) value = chomp_string(value); valuelen = strcspn(value, " \t"); info = get_line_info(value, valuelen); - if (!info) + if (!info) { + config_msg = "Unknown color name"; return ERR; + } value = chomp_string(value + valuelen); valuelen = strcspn(value, " \t"); - if (set_color(&info->fg, value, valuelen) == ERR) + if (set_color(&info->fg, value, valuelen) == ERR) { + config_msg = "Unknown color"; return ERR; + } value = chomp_string(value + valuelen); valuelen = strcspn(value, " \t"); - if (set_color(&info->bg, value, valuelen) == ERR) + if (set_color(&info->bg, value, valuelen) == ERR) { + config_msg = "Unknown color"; return ERR; + } value = chomp_string(value + valuelen); if (*value && - set_attribute(&info->attr, value, strlen(value)) == ERR) + set_attribute(&info->attr, value, strlen(value)) == ERR) { + config_msg = "Unknown attribute"; return ERR; + } return OK; } @@ -901,14 +902,49 @@ read_option(char *opt, int optlen, char *value, int valuelen) } static int +read_option(char *opt, int optlen, char *value, int valuelen) +{ + config_lineno++; + config_msg = "Internal error"; + + optlen = strcspn(opt, "#;"); + if (optlen == 0) { + /* The whole line is a commend or empty. */ + return OK; + + } else if (opt[optlen] != 0) { + /* Part of the option name is a comment, so the value part + * should be ignored. */ + valuelen = 0; + opt[optlen] = value[valuelen] = 0; + } else { + /* Else look for comment endings in the value. */ + valuelen = strcspn(value, "#;"); + value[valuelen] = 0; + } + + if (set_option(opt, optlen, value, valuelen) == ERR) { + fprintf(stderr, "Error on line %d, near '%.*s' option: %s\n", + config_lineno, optlen, opt, config_msg); + config_errors = TRUE; + } + + /* Always keep going if errors are encountered. */ + return OK; +} + +static int load_options(void) { char *home = getenv("HOME"); char buf[1024]; FILE *file; + config_lineno = 0; + config_errors = FALSE; + if (!home || - snprintf(buf, sizeof(buf), "%s/.tig", home) >= sizeof(buf)) + snprintf(buf, sizeof(buf), "%s/.tigrc", home) >= sizeof(buf)) return ERR; /* It's ok that the file doesn't exist. */ @@ -916,7 +952,11 @@ load_options(void) if (!file) return OK; - return read_properties(file, " \t", read_option); + if (read_properties(file, " \t", read_option) == ERR || + config_errors == TRUE) + fprintf(stderr, "Errors while loading %s.\n", buf); + + return OK; } @@ -2718,8 +2758,7 @@ read_properties(FILE *pipe, const char *separators, valuelen = 0; } - if (namelen) - state = read_property(name, namelen, value, valuelen); + state = read_property(name, namelen, value, valuelen); } if (state != ERR && ferror(pipe)) @@ -2976,6 +3015,8 @@ main(int argc, char *argv[]) * * - Locale support. * + * - Make '?' show a one page keybinding cheat sheet. + * * COPYRIGHT * --------- * Copyright (c) 2006 Jonas Fonseca