static void parse_git_color_option(enum line_type type, char *value) { struct line_info *info = get_line_info(type); const char *argv[SIZEOF_ARG]; int argc = 0; bool first_color = TRUE; int i; if (!argv_from_string(argv, &argc, value)) return; info->fg = COLOR_DEFAULT; info->bg = COLOR_DEFAULT; info->attr = 0; for (i = 0; i < argc; i++) { int attr = 0; if (set_attribute(&attr, argv[i])) { info->attr |= attr; } else if (set_color(&attr, argv[i])) { if (first_color) info->fg = attr; else info->bg = attr; first_color = FALSE; } } }
static struct line_info * parse_git_color_option(struct line_info *info, char *value) { const char *argv[SIZEOF_ARG]; int argc = 0; bool first_color = TRUE; int i; if (!argv_from_string(argv, &argc, value)) return NULL; info->fg = COLOR_DEFAULT; info->bg = COLOR_DEFAULT; info->attr = 0; for (i = 0; i < argc; i++) { int attr = 0; if (set_attribute(&attr, argv[i])) { info->attr |= attr; } else if (set_color(&attr, argv[i])) { if (first_color) info->fg = attr; else info->bg = attr; first_color = FALSE; } } return info; }
bool argv_from_env(const char **argv, const char *name) { char *env = argv ? getenv(name) : NULL; int argc = 0; if (env && *env) env = strdup(env); return !env || argv_from_string(argv, &argc, env); }
enum request open_prompt(struct view *view) { char *cmd = read_prompt(":"); const char *argv[SIZEOF_ARG] = { NULL }; int argc = 0; if (cmd && !argv_from_string(argv, &argc, cmd)) { report("Too many arguments"); return REQ_NONE; } return run_prompt_command(view, argv); }
static void set_repo_config_option(char *name, char *value, enum status_code (*cmd)(int, const char **)) { const char *argv[SIZEOF_ARG] = { name, "=" }; int argc = 1 + (cmd == option_set_command); enum status_code error; if (!argv_from_string(argv, &argc, value)) error = ERROR_TOO_MANY_OPTION_ARGUMENTS; else error = cmd(argc, argv); if (error != SUCCESS) warn("Option 'tig.%s': %s", name, get_status_message(error)); }
static void set_repo_config_option(char *name, char *value, enum status_code (*cmd)(int, const char **)) { const char *argv[SIZEOF_ARG] = { name, "=" }; int argc = 1 + (cmd == option_set_command); enum status_code code; if (!argv_from_string(argv, &argc, value)) code = error("Too many arguments"); else code = cmd(argc, argv); if (code != SUCCESS) warn("Option 'tig.%s': %s", name, get_status_message(code)); }
int load_options(void) { const char *tigrc_user = getenv("TIGRC_USER"); const char *tigrc_system = getenv("TIGRC_SYSTEM"); const char *tig_diff_opts = getenv("TIG_DIFF_OPTS"); const bool diff_opts_from_args = !!opt_diff_options; bool custom_tigrc_system = !!tigrc_system; opt_file_filter = TRUE; if (!find_option_info_by_value(&opt_diff_context)->seen) opt_diff_context = -3; if (!custom_tigrc_system) tigrc_system = SYSCONFDIR "/tigrc"; if (!*tigrc_system || (load_option_file(tigrc_system) == ERROR_FILE_DOES_NOT_EXIST && !custom_tigrc_system)) { struct config_state config = { "<built-in>", 0, FALSE }; struct io io; if (!io_from_string(&io, builtin_config)) die("Failed to get built-in config"); if (!io_load_span(&io, " \t", &config.lineno, read_option, &config) == ERR || config.errors == TRUE) die("Error in built-in config"); } if (!tigrc_user) tigrc_user = "******"; load_option_file(tigrc_user); if (!diff_opts_from_args && tig_diff_opts && *tig_diff_opts) { static const char *diff_opts[SIZEOF_ARG] = { NULL }; char buf[SIZEOF_STR]; int argc = 0; if (!string_format(buf, "%s", tig_diff_opts) || !argv_from_string(diff_opts, &argc, buf)) die("TIG_DIFF_OPTS contains too many arguments"); else if (!argv_copy(&opt_diff_options, diff_opts)) die("Failed to format TIG_DIFF_OPTS arguments"); } return OK; }
static int read_option(char *opt, size_t optlen, char *value, size_t valuelen, void *data) { struct config_state *config = data; enum status_code status = ERROR_NO_OPTION_VALUE; config->lineno++; /* Check for comment markers, since read_properties() will * only ensure opt and value are split at first " \t". */ optlen = strcspn(opt, "#"); if (optlen == 0) return OK; if (opt[optlen] == 0) { /* Look for comment endings in the value. */ size_t len = strcspn(value, "#"); const char *argv[SIZEOF_ARG]; int argc = 0; if (len < valuelen) { valuelen = len; value[valuelen] = 0; } if (!argv_from_string(argv, &argc, value)) status = error("Too many option arguments for %s", opt); else status = set_option(opt, argc, argv); } if (status != SUCCESS) { warn("%s:%d: %s", config->path, config->lineno, get_status_message(status)); config->errors = TRUE; } /* Always keep going if errors are encountered. */ return OK; }
enum status_code set_option(const char *opt, char *value) { const char *argv[SIZEOF_ARG]; int argc = 0; if (!argv_from_string(argv, &argc, value)) return ERROR_TOO_MANY_OPTION_ARGUMENTS; if (!strcmp(opt, "color")) return option_color_command(argc, argv); if (!strcmp(opt, "set")) return option_set_command(argc, argv); if (!strcmp(opt, "bind")) return option_bind_command(argc, argv); if (!strcmp(opt, "source")) return option_source_command(argc, argv); return ERROR_UNKNOWN_OPTION_COMMAND; }
static enum request run_prompt_command(struct view *view, char *cmd) { enum request request; if (cmd && string_isnumber(cmd)) { int lineno = view->pos.lineno + 1; if (parse_int(&lineno, cmd, 1, view->lines + 1) == SUCCESS) { select_view_line(view, lineno - 1); report_clear(); } else { report("Unable to parse '%s' as a line number", cmd); } } else if (cmd && iscommit(cmd)) { string_ncopy(view->env->search, cmd, strlen(cmd)); request = view_request(view, REQ_JUMP_COMMIT); if (request == REQ_JUMP_COMMIT) { report("Jumping to commits is not supported by the '%s' view", view->name); } } else if (cmd && strlen(cmd) == 1) { struct key_input input = { { cmd[0] } }; return get_keybinding(&view->ops->keymap, &input); } else if (cmd && cmd[0] == '!') { struct view *next = VIEW(REQ_VIEW_PAGER); const char *argv[SIZEOF_ARG]; int argc = 0; cmd++; /* When running random commands, initially show the * command in the title. However, it maybe later be * overwritten if a commit line is selected. */ string_ncopy(next->ref, cmd, strlen(cmd)); if (!argv_from_string(argv, &argc, cmd)) { report("Too many arguments"); } else if (!argv_format(view->env, &next->argv, argv, FALSE, TRUE)) { report("Argument formatting failed"); } else { next->dir = NULL; open_view(view, REQ_VIEW_PAGER, OPEN_PREPARED); } } else if (cmd) { request = get_request(cmd); if (request != REQ_UNKNOWN) return request; char *args = strchr(cmd, ' '); if (args) { *args++ = 0; if (set_option(cmd, args) == SUCCESS) { request = !view->unrefreshable ? REQ_REFRESH : REQ_SCREEN_REDRAW; if (!strcmp(cmd, "color")) init_colors(); } } return request; } return REQ_NONE; }