Emit more informative error messages when loading ~/.tig
authorJonas Fonseca <fonseca@diku.dk>
Sun, 28 May 2006 18:59:31 +0000 (20:59 +0200)
committerJonas Fonseca <fonseca@antimatter.localdomain>
Sun, 28 May 2006 18:59:31 +0000 (20:59 +0200)
tig.c

diff --git a/tig.c b/tig.c
index 982c1cc..121c5db 100644 (file)
--- a/tig.c
+++ b/tig.c
@@ -855,25 +855,13 @@ struct line {
 #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;
-               opt[optlen] = value[valuelen] = 0;
-       } else {
-               /* Else look for comment endings in the value. */
-               valuelen = strcspn(value, "#;");
-               value[valuelen] = 0;
-       }
-
        /* Reads: "color" object fgcolor bgcolor [attr] */
        if (!strcmp(opt, "color")) {
                struct line_info *info;
@@ -881,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;
        }
@@ -906,12 +902,47 @@ 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))
                return ERR;
@@ -921,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;
 }
 
 
@@ -2723,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))