Simplify the option value tokenization by doing it one place
[tig] / tig.c
diff --git a/tig.c b/tig.c
index 4e1ffcb..c270e16 100644 (file)
--- a/tig.c
+++ b/tig.c
 
 #include <curses.h>
 
-static void die(const char *err, ...);
+#if __GNUC__ >= 3
+#define __NORETURN __attribute__((__noreturn__))
+#else
+#define __NORETURN
+#endif
+
+static void __NORETURN die(const char *err, ...);
 static void report(const char *msg, ...);
 static int read_properties(FILE *pipe, const char *separators, int (*read)(char *, int, char *, int));
 static void set_nonblocking_input(bool loading);
@@ -47,6 +53,7 @@ static void load_help_page(void);
 
 #define SIZEOF_REF     256     /* Size of symbolic or SHA1 ID. */
 #define SIZEOF_CMD     1024    /* Size of command buffer. */
+#define SIZEOF_REVGRAPH        19      /* Size of revision ancestry graphics. */
 
 /* This color name can be used to refer to the default term colors. */
 #define COLOR_DEFAULT  (-1)
@@ -255,7 +262,8 @@ sq_quote(char buf[SIZEOF_CMD], size_t bufsize, const char *src)
        REQ_(SCREEN_RESIZE,     "Resize the screen"), \
        REQ_(SHOW_VERSION,      "Show version information"), \
        REQ_(STOP_LOADING,      "Stop all loading views"), \
-       REQ_(TOGGLE_LINENO,     "Toggle line numbers"),
+       REQ_(TOGGLE_LINENO,     "Toggle line numbers"), \
+       REQ_(TOGGLE_REV_GRAPH,  "Toggle revision graph visualization"),
 
 
 /* User action requests. */
@@ -309,6 +317,7 @@ VERSION " (" __DATE__ ")\n"
 
 /* Option and state variables. */
 static bool opt_line_number    = FALSE;
+static bool opt_rev_graph      = TRUE;
 static int opt_num_interval    = NUMBER_INTERVAL;
 static int opt_tab_size                = TABSIZE;
 static enum request opt_request = REQ_VIEW_MAIN;
@@ -415,7 +424,7 @@ parse_options(int argc, char *argv[])
                if (opt[0] && opt[0] != '-')
                        break;
 
-               die("unknown command '%s'", opt);
+               die("unknown option '%s'\n\n%s", opt, usage);
        }
 
        if (!isatty(STDIN_FILENO)) {
@@ -452,29 +461,9 @@ parse_options(int argc, char *argv[])
 }
 
 
-static struct int_map color_map[] = {
-#define COLOR_MAP(name) { #name, STRING_SIZE(#name), COLOR_##name }
-       COLOR_MAP(DEFAULT),
-       COLOR_MAP(BLACK),
-       COLOR_MAP(BLUE),
-       COLOR_MAP(CYAN),
-       COLOR_MAP(GREEN),
-       COLOR_MAP(MAGENTA),
-       COLOR_MAP(RED),
-       COLOR_MAP(WHITE),
-       COLOR_MAP(YELLOW),
-};
-
-static struct int_map attr_map[] = {
-#define ATTR_MAP(name) { #name, STRING_SIZE(#name), A_##name }
-       ATTR_MAP(NORMAL),
-       ATTR_MAP(BLINK),
-       ATTR_MAP(BOLD),
-       ATTR_MAP(DIM),
-       ATTR_MAP(REVERSE),
-       ATTR_MAP(STANDOUT),
-       ATTR_MAP(UNDERLINE),
-};
+/*
+ * Line-oriented content detection.
+ */
 
 #define LINE_INFO \
 LINE(DIFF_HEADER,  "diff --git ",      COLOR_YELLOW,   COLOR_DEFAULT,  0), \
@@ -516,11 +505,6 @@ LINE(MAIN_DELIM,   "",                     COLOR_MAGENTA,  COLOR_DEFAULT,  0), \
 LINE(MAIN_TAG,     "",                 COLOR_MAGENTA,  COLOR_DEFAULT,  A_BOLD), \
 LINE(MAIN_REF,     "",                 COLOR_CYAN,     COLOR_DEFAULT,  A_BOLD), \
 
-
-/*
- * Line-oriented content detection.
- */
-
 enum line_type {
 #define LINE(type, line, fg, bg, attr) \
        LINE_##type
@@ -620,55 +604,102 @@ 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)
+static struct int_map color_map[] = {
+#define COLOR_MAP(name) { #name, STRING_SIZE(#name), COLOR_##name }
+       COLOR_MAP(DEFAULT),
+       COLOR_MAP(BLACK),
+       COLOR_MAP(BLUE),
+       COLOR_MAP(CYAN),
+       COLOR_MAP(GREEN),
+       COLOR_MAP(MAGENTA),
+       COLOR_MAP(RED),
+       COLOR_MAP(WHITE),
+       COLOR_MAP(YELLOW),
+};
+
+#define set_color(color, name) \
+       set_from_int_map(color_map, ARRAY_SIZE(color_map), color, name, strlen(name))
+
+static struct int_map attr_map[] = {
+#define ATTR_MAP(name) { #name, STRING_SIZE(#name), A_##name }
+       ATTR_MAP(NORMAL),
+       ATTR_MAP(BLINK),
+       ATTR_MAP(BOLD),
+       ATTR_MAP(DIM),
+       ATTR_MAP(REVERSE),
+       ATTR_MAP(STANDOUT),
+       ATTR_MAP(UNDERLINE),
+};
 
-#define set_attribute(attr, name, namelen) \
-       set_from_int_map(attr_map, ARRAY_SIZE(attr_map), attr, name, namelen)
+#define set_attribute(attr, name) \
+       set_from_int_map(attr_map, ARRAY_SIZE(attr_map), attr, name, strlen(name))
 
 static int   config_lineno;
 static bool  config_errors;
 static char *config_msg;
 
+/* Reads
+ *
+ *     object fgcolor bgcolor [attr]
+ *
+ * from the value string. */
 static int
-set_option(char *opt, int optlen, char *value, int valuelen)
+set_option_color(int argc, char *argv[])
 {
-       /* Reads: "color" object fgcolor bgcolor [attr] */
-       if (!strcmp(opt, "color")) {
-               struct line_info *info;
-
-               value = chomp_string(value);
-               valuelen = strcspn(value, " \t");
-               info = get_line_info(value, valuelen);
-               if (!info) {
-                       config_msg = "Unknown color name";
-                       return ERR;
-               }
+       struct line_info *info;
 
-               value = chomp_string(value + valuelen);
-               valuelen = strcspn(value, " \t");
-               if (set_color(&info->fg, value, valuelen) == ERR) {
-                       config_msg = "Unknown color";
-                       return ERR;
-               }
+       if (argc != 3 && argc != 4) {
+               config_msg = "Wrong number of arguments given to color command";
+               return ERR;
+       }
 
-               value = chomp_string(value + valuelen);
-               valuelen = strcspn(value, " \t");
-               if (set_color(&info->bg, value, valuelen) == ERR) {
-                       config_msg = "Unknown color";
-                       return ERR;
-               }
+       info = get_line_info(argv[0], strlen(argv[0]));
+       if (!info) {
+               config_msg = "Unknown color name";
+               return ERR;
+       }
 
-               value = chomp_string(value + valuelen);
-               if (*value &&
-                   set_attribute(&info->attr, value, strlen(value)) == ERR) {
-                       config_msg = "Unknown attribute";
-                       return ERR;
-               }
+       if (set_color(&info->fg, argv[1]) == ERR) {
+               config_msg = "Unknown color";
+               return ERR;
+       }
 
-               return OK;
+       if (set_color(&info->bg, argv[2]) == ERR) {
+               config_msg = "Unknown color";
+               return ERR;
        }
 
+       if (argc == 4 && set_attribute(&info->attr, argv[3]) == ERR) {
+               config_msg = "Unknown attribute";
+               return ERR;
+       }
+
+       return OK;
+}
+
+static int
+set_option(char *opt, char *value)
+{
+       char *argv[16];
+       int valuelen;
+       int argc = 0;
+
+       /* Tokenize */
+       while (argc < ARRAY_SIZE(argv) && (valuelen = strcspn(value, " \t"))) {
+               argv[argc++] = value;
+
+               value += valuelen;
+               if (!*value)
+                       break;
+
+               *value++ = 0;
+               while (isspace(*value))
+                       value++;
+       }
+
+       if (!strcmp(opt, "color"))
+               return set_option_color(argc, argv);
+
        return ERR;
 }
 
@@ -694,7 +725,7 @@ read_option(char *opt, int optlen, char *value, int valuelen)
                value[valuelen] = 0;
        }
 
-       if (set_option(opt, optlen, value, valuelen) == ERR) {
+       if (set_option(opt, value) == ERR) {
                fprintf(stderr, "Error on line %d, near '%.*s' option: %s\n",
                        config_lineno, optlen, opt, config_msg);
                config_errors = TRUE;
@@ -865,8 +896,9 @@ update_view_title(struct view *view)
                wprintw(view->title, "[%s]", view->name);
 
        if (view->lines || view->pipe) {
+               unsigned int view_lines = view->offset + view->height;
                unsigned int lines = view->lines
-                                  ? (view->lineno + 1) * 100 / view->lines
+                                  ? MIN(view_lines, view->lines) * 100 / view->lines
                                   : 0;
 
                wprintw(view->title, " - %s %d of %d (%d%%)",
@@ -1341,8 +1373,11 @@ open_view(struct view *prev, enum request request, enum open_flags flags)
                return;
        }
 
-       if ((reload || strcmp(view->vid, view->id)) &&
-           !begin_update(view)) {
+       if (view == VIEW(REQ_VIEW_HELP)) {
+               load_help_page();
+
+       } else if ((reload || strcmp(view->vid, view->id)) &&
+                  !begin_update(view)) {
                report("Failed to load %s view", view->name);
                return;
        }
@@ -1382,9 +1417,6 @@ open_view(struct view *prev, enum request request, enum open_flags flags)
                view->parent = prev;
        }
 
-       if (view == VIEW(REQ_VIEW_HELP))
-               load_help_page();
-
        if (view->pipe && view->lines == 0) {
                /* Clear the old view and let the incremental updating refill
                 * the screen. */
@@ -1482,6 +1514,11 @@ view_driver(struct view *view, enum request request)
                redraw_display();
                break;
 
+       case REQ_TOGGLE_REV_GRAPH:
+               opt_rev_graph = !opt_rev_graph;
+               redraw_display();
+               break;
+
        case REQ_PROMPT:
                /* Always reload^Wrerun commands from the prompt. */
                open_view(view, opt_request, OPEN_RELOAD);
@@ -1713,11 +1750,13 @@ static struct view_ops pager_ops = {
  */
 
 struct commit {
-       char id[41];            /* SHA1 ID. */
-       char title[75];         /* The first line of the commit message. */
-       char author[75];        /* The author of the commit. */
-       struct tm time;         /* Date from the author ident. */
-       struct ref **refs;      /* Repository references; tags & branch heads. */
+       char id[41];                    /* SHA1 ID. */
+       char title[75];                 /* First line of the commit message. */
+       char author[75];                /* Author of the commit. */
+       struct tm time;                 /* Date from the author ident. */
+       struct ref **refs;              /* Repository references. */
+       chtype graph[SIZEOF_REVGRAPH];  /* Ancestry chain graphics. */
+       size_t graph_size;              /* The width of the graph array. */
 };
 
 static bool
@@ -1780,9 +1819,19 @@ main_draw(struct view *view, struct line *line, unsigned int lineno)
        if (type != LINE_CURSOR)
                wattrset(view->win, A_NORMAL);
 
-       mvwaddch(view->win, lineno, col, ACS_LTEE);
-       wmove(view->win, lineno, col + 2);
-       col += 2;
+       if (opt_rev_graph && commit->graph_size) {
+               size_t i;
+
+               wmove(view->win, lineno, col);
+               /* Using waddch() instead of waddnstr() ensures that
+                * they'll be rendered correctly for the cursor line. */
+               for (i = 0; i < commit->graph_size; i++)
+                       waddch(view->win, commit->graph[i]);
+
+               col += commit->graph_size + 1;
+       }
+
+       wmove(view->win, lineno, col);
 
        if (commit->refs) {
                size_t i = 0;
@@ -1838,6 +1887,7 @@ main_read(struct view *view, char *line)
                view->line[view->lines++].data = commit;
                string_copy(commit->id, line);
                commit->refs = get_refs(commit->id);
+               commit->graph[commit->graph_size++] = ACS_LTEE;
                break;
 
        case LINE_AUTHOR:
@@ -1972,6 +2022,7 @@ static struct keymap keymap[] = {
        { 'v',          REQ_SHOW_VERSION },
        { 'r',          REQ_SCREEN_REDRAW },
        { 'n',          REQ_TOGGLE_LINENO },
+       { 'g',          REQ_TOGGLE_REV_GRAPH},
        { ':',          REQ_PROMPT },
 
        /* wgetch() with nodelay() enabled returns ERR when there's no input. */
@@ -2420,25 +2471,22 @@ read_ref(char *id, int idlen, char *name, int namelen)
 {
        struct ref *ref;
        bool tag = FALSE;
-       bool tag_commit = FALSE;
 
-       /* Commits referenced by tags has "^{}" appended. */
-       if (name[namelen - 1] == '}') {
+       if (!strncmp(name, "refs/tags/", STRING_SIZE("refs/tags/"))) {
+               /* Commits referenced by tags has "^{}" appended. */
+               if (name[namelen - 1] != '}')
+                       return OK;
+
                while (namelen > 0 && name[namelen] != '^')
                        namelen--;
-               if (namelen > 0)
-                       tag_commit = TRUE;
-               name[namelen] = 0;
-       }
 
-       if (!strncmp(name, "refs/tags/", STRING_SIZE("refs/tags/"))) {
-               if (!tag_commit)
-                       return OK;
-               name += STRING_SIZE("refs/tags/");
                tag = TRUE;
+               namelen -= STRING_SIZE("refs/tags/");
+               name    += STRING_SIZE("refs/tags/");
 
        } else if (!strncmp(name, "refs/heads/", STRING_SIZE("refs/heads/"))) {
-               name += STRING_SIZE("refs/heads/");
+               namelen -= STRING_SIZE("refs/heads/");
+               name    += STRING_SIZE("refs/heads/");
 
        } else if (!strcmp(name, "HEAD")) {
                return OK;
@@ -2449,10 +2497,12 @@ read_ref(char *id, int idlen, char *name, int namelen)
                return ERR;
 
        ref = &refs[refs_size++];
-       ref->name = strdup(name);
+       ref->name = malloc(namelen + 1);
        if (!ref->name)
                return ERR;
 
+       strncpy(ref->name, name, namelen);
+       ref->name[namelen] = 0;
        ref->tag = tag;
        string_copy(ref->id, id);
 
@@ -2529,12 +2579,6 @@ read_properties(FILE *pipe, const char *separators,
  * Main
  */
 
-#if __GNUC__ >= 3
-#define __NORETURN __attribute__((__noreturn__))
-#else
-#define __NORETURN
-#endif
-
 static void __NORETURN
 quit(int sig)
 {