notes.c: use designated initializers for clarity
[git/debian.git] / builtin / help.c
blob87333a02ec40adb4e2b2b613b840c36f066f4e52
1 /*
2 * Builtin help command
3 */
4 #include "cache.h"
5 #include "config.h"
6 #include "builtin.h"
7 #include "exec-cmd.h"
8 #include "gettext.h"
9 #include "parse-options.h"
10 #include "run-command.h"
11 #include "config-list.h"
12 #include "help.h"
13 #include "alias.h"
14 #include "setup.h"
16 #ifndef DEFAULT_HELP_FORMAT
17 #define DEFAULT_HELP_FORMAT "man"
18 #endif
20 static struct man_viewer_list {
21 struct man_viewer_list *next;
22 char name[FLEX_ARRAY];
23 } *man_viewer_list;
25 static struct man_viewer_info_list {
26 struct man_viewer_info_list *next;
27 const char *info;
28 char name[FLEX_ARRAY];
29 } *man_viewer_info_list;
31 enum help_format {
32 HELP_FORMAT_NONE,
33 HELP_FORMAT_MAN,
34 HELP_FORMAT_INFO,
35 HELP_FORMAT_WEB
38 enum show_config_type {
39 SHOW_CONFIG_HUMAN,
40 SHOW_CONFIG_VARS,
41 SHOW_CONFIG_SECTIONS,
44 static enum help_action {
45 HELP_ACTION_ALL = 1,
46 HELP_ACTION_GUIDES,
47 HELP_ACTION_CONFIG,
48 HELP_ACTION_USER_INTERFACES,
49 HELP_ACTION_DEVELOPER_INTERFACES,
50 HELP_ACTION_CONFIG_FOR_COMPLETION,
51 HELP_ACTION_CONFIG_SECTIONS_FOR_COMPLETION,
52 } cmd_mode;
54 static const char *html_path;
55 static int verbose = 1;
56 static enum help_format help_format = HELP_FORMAT_NONE;
57 static int exclude_guides;
58 static int show_external_commands = -1;
59 static int show_aliases = -1;
60 static struct option builtin_help_options[] = {
61 OPT_CMDMODE('a', "all", &cmd_mode, N_("print all available commands"),
62 HELP_ACTION_ALL),
63 OPT_BOOL(0, "external-commands", &show_external_commands,
64 N_("show external commands in --all")),
65 OPT_BOOL(0, "aliases", &show_aliases, N_("show aliases in --all")),
66 OPT_HIDDEN_BOOL(0, "exclude-guides", &exclude_guides, N_("exclude guides")),
67 OPT_SET_INT('m', "man", &help_format, N_("show man page"), HELP_FORMAT_MAN),
68 OPT_SET_INT('w', "web", &help_format, N_("show manual in web browser"),
69 HELP_FORMAT_WEB),
70 OPT_SET_INT('i', "info", &help_format, N_("show info page"),
71 HELP_FORMAT_INFO),
72 OPT__VERBOSE(&verbose, N_("print command description")),
74 OPT_CMDMODE('g', "guides", &cmd_mode, N_("print list of useful guides"),
75 HELP_ACTION_GUIDES),
76 OPT_CMDMODE(0, "user-interfaces", &cmd_mode,
77 N_("print list of user-facing repository, command and file interfaces"),
78 HELP_ACTION_USER_INTERFACES),
79 OPT_CMDMODE(0, "developer-interfaces", &cmd_mode,
80 N_("print list of file formats, protocols and other developer interfaces"),
81 HELP_ACTION_DEVELOPER_INTERFACES),
82 OPT_CMDMODE('c', "config", &cmd_mode, N_("print all configuration variable names"),
83 HELP_ACTION_CONFIG),
84 OPT_CMDMODE_F(0, "config-for-completion", &cmd_mode, "",
85 HELP_ACTION_CONFIG_FOR_COMPLETION, PARSE_OPT_HIDDEN),
86 OPT_CMDMODE_F(0, "config-sections-for-completion", &cmd_mode, "",
87 HELP_ACTION_CONFIG_SECTIONS_FOR_COMPLETION, PARSE_OPT_HIDDEN),
89 OPT_END(),
92 static const char * const builtin_help_usage[] = {
93 "git help [-a|--all] [--[no-]verbose] [--[no-]external-commands] [--[no-]aliases]",
94 N_("git help [[-i|--info] [-m|--man] [-w|--web]] [<command>|<doc>]"),
95 "git help [-g|--guides]",
96 "git help [-c|--config]",
97 "git help [--user-interfaces]",
98 "git help [--developer-interfaces]",
99 NULL
102 struct slot_expansion {
103 const char *prefix;
104 const char *placeholder;
105 void (*fn)(struct string_list *list, const char *prefix);
106 int found;
109 static void list_config_help(enum show_config_type type)
111 struct slot_expansion slot_expansions[] = {
112 { "advice", "*", list_config_advices },
113 { "color.branch", "<slot>", list_config_color_branch_slots },
114 { "color.decorate", "<slot>", list_config_color_decorate_slots },
115 { "color.diff", "<slot>", list_config_color_diff_slots },
116 { "color.grep", "<slot>", list_config_color_grep_slots },
117 { "color.interactive", "<slot>", list_config_color_interactive_slots },
118 { "color.remote", "<slot>", list_config_color_sideband_slots },
119 { "color.status", "<slot>", list_config_color_status_slots },
120 { "fsck", "<msg-id>", list_config_fsck_msg_ids },
121 { "receive.fsck", "<msg-id>", list_config_fsck_msg_ids },
122 { NULL, NULL, NULL }
124 const char **p;
125 struct slot_expansion *e;
126 struct string_list keys = STRING_LIST_INIT_DUP;
127 struct string_list keys_uniq = STRING_LIST_INIT_DUP;
128 struct string_list_item *item;
129 int i;
131 for (p = config_name_list; *p; p++) {
132 const char *var = *p;
133 struct strbuf sb = STRBUF_INIT;
135 for (e = slot_expansions; e->prefix; e++) {
137 strbuf_reset(&sb);
138 strbuf_addf(&sb, "%s.%s", e->prefix, e->placeholder);
139 if (!strcasecmp(var, sb.buf)) {
140 e->fn(&keys, e->prefix);
141 e->found++;
142 break;
145 strbuf_release(&sb);
146 if (!e->prefix)
147 string_list_append(&keys, var);
150 for (e = slot_expansions; e->prefix; e++)
151 if (!e->found)
152 BUG("slot_expansion %s.%s is not used",
153 e->prefix, e->placeholder);
155 string_list_sort(&keys);
156 for (i = 0; i < keys.nr; i++) {
157 const char *var = keys.items[i].string;
158 const char *wildcard, *tag, *cut;
159 const char *dot = NULL;
160 struct strbuf sb = STRBUF_INIT;
162 switch (type) {
163 case SHOW_CONFIG_HUMAN:
164 puts(var);
165 continue;
166 case SHOW_CONFIG_SECTIONS:
167 dot = strchr(var, '.');
168 break;
169 case SHOW_CONFIG_VARS:
170 break;
172 wildcard = strchr(var, '*');
173 tag = strchr(var, '<');
175 if (!dot && !wildcard && !tag) {
176 string_list_append(&keys_uniq, var);
177 continue;
180 if (dot)
181 cut = dot;
182 else if (wildcard && !tag)
183 cut = wildcard;
184 else if (!wildcard && tag)
185 cut = tag;
186 else
187 cut = wildcard < tag ? wildcard : tag;
189 strbuf_add(&sb, var, cut - var);
190 string_list_append(&keys_uniq, sb.buf);
191 strbuf_release(&sb);
194 string_list_clear(&keys, 0);
195 string_list_remove_duplicates(&keys_uniq, 0);
196 for_each_string_list_item(item, &keys_uniq)
197 puts(item->string);
198 string_list_clear(&keys_uniq, 0);
201 static enum help_format parse_help_format(const char *format)
203 if (!strcmp(format, "man"))
204 return HELP_FORMAT_MAN;
205 if (!strcmp(format, "info"))
206 return HELP_FORMAT_INFO;
207 if (!strcmp(format, "web") || !strcmp(format, "html"))
208 return HELP_FORMAT_WEB;
210 * Please update _git_config() in git-completion.bash when you
211 * add new help formats.
213 die(_("unrecognized help format '%s'"), format);
216 static const char *get_man_viewer_info(const char *name)
218 struct man_viewer_info_list *viewer;
220 for (viewer = man_viewer_info_list; viewer; viewer = viewer->next)
222 if (!strcasecmp(name, viewer->name))
223 return viewer->info;
225 return NULL;
228 static int check_emacsclient_version(void)
230 struct strbuf buffer = STRBUF_INIT;
231 struct child_process ec_process = CHILD_PROCESS_INIT;
232 int version;
234 /* emacsclient prints its version number on stderr */
235 strvec_pushl(&ec_process.args, "emacsclient", "--version", NULL);
236 ec_process.err = -1;
237 ec_process.stdout_to_stderr = 1;
238 if (start_command(&ec_process))
239 return error(_("Failed to start emacsclient."));
241 strbuf_read(&buffer, ec_process.err, 20);
242 close(ec_process.err);
245 * Don't bother checking return value, because "emacsclient --version"
246 * seems to always exits with code 1.
248 finish_command(&ec_process);
250 if (!starts_with(buffer.buf, "emacsclient")) {
251 strbuf_release(&buffer);
252 return error(_("Failed to parse emacsclient version."));
255 strbuf_remove(&buffer, 0, strlen("emacsclient"));
256 version = atoi(buffer.buf);
258 if (version < 22) {
259 strbuf_release(&buffer);
260 return error(_("emacsclient version '%d' too old (< 22)."),
261 version);
264 strbuf_release(&buffer);
265 return 0;
268 static void exec_woman_emacs(const char *path, const char *page)
270 if (!check_emacsclient_version()) {
271 /* This works only with emacsclient version >= 22. */
272 struct strbuf man_page = STRBUF_INIT;
274 if (!path)
275 path = "emacsclient";
276 strbuf_addf(&man_page, "(woman \"%s\")", page);
277 execlp(path, "emacsclient", "-e", man_page.buf, (char *)NULL);
278 warning_errno(_("failed to exec '%s'"), path);
279 strbuf_release(&man_page);
283 static void exec_man_konqueror(const char *path, const char *page)
285 const char *display = getenv("DISPLAY");
286 if (display && *display) {
287 struct strbuf man_page = STRBUF_INIT;
288 const char *filename = "kfmclient";
290 /* It's simpler to launch konqueror using kfmclient. */
291 if (path) {
292 size_t len;
293 if (strip_suffix(path, "/konqueror", &len))
294 path = xstrfmt("%.*s/kfmclient", (int)len, path);
295 filename = basename((char *)path);
296 } else
297 path = "kfmclient";
298 strbuf_addf(&man_page, "man:%s(1)", page);
299 execlp(path, filename, "newTab", man_page.buf, (char *)NULL);
300 warning_errno(_("failed to exec '%s'"), path);
301 strbuf_release(&man_page);
305 static void exec_man_man(const char *path, const char *page)
307 if (!path)
308 path = "man";
309 execlp(path, "man", page, (char *)NULL);
310 warning_errno(_("failed to exec '%s'"), path);
313 static void exec_man_cmd(const char *cmd, const char *page)
315 struct strbuf shell_cmd = STRBUF_INIT;
316 strbuf_addf(&shell_cmd, "%s %s", cmd, page);
317 execl(SHELL_PATH, SHELL_PATH, "-c", shell_cmd.buf, (char *)NULL);
318 warning(_("failed to exec '%s'"), cmd);
319 strbuf_release(&shell_cmd);
322 static void add_man_viewer(const char *name)
324 struct man_viewer_list **p = &man_viewer_list;
326 while (*p)
327 p = &((*p)->next);
328 FLEX_ALLOC_STR(*p, name, name);
331 static int supported_man_viewer(const char *name, size_t len)
333 return (!strncasecmp("man", name, len) ||
334 !strncasecmp("woman", name, len) ||
335 !strncasecmp("konqueror", name, len));
338 static void do_add_man_viewer_info(const char *name,
339 size_t len,
340 const char *value)
342 struct man_viewer_info_list *new_man_viewer;
343 FLEX_ALLOC_MEM(new_man_viewer, name, name, len);
344 new_man_viewer->info = xstrdup(value);
345 new_man_viewer->next = man_viewer_info_list;
346 man_viewer_info_list = new_man_viewer;
349 static int add_man_viewer_path(const char *name,
350 size_t len,
351 const char *value)
353 if (supported_man_viewer(name, len))
354 do_add_man_viewer_info(name, len, value);
355 else
356 warning(_("'%s': path for unsupported man viewer.\n"
357 "Please consider using 'man.<tool>.cmd' instead."),
358 name);
360 return 0;
363 static int add_man_viewer_cmd(const char *name,
364 size_t len,
365 const char *value)
367 if (supported_man_viewer(name, len))
368 warning(_("'%s': cmd for supported man viewer.\n"
369 "Please consider using 'man.<tool>.path' instead."),
370 name);
371 else
372 do_add_man_viewer_info(name, len, value);
374 return 0;
377 static int add_man_viewer_info(const char *var, const char *value)
379 const char *name, *subkey;
380 size_t namelen;
382 if (parse_config_key(var, "man", &name, &namelen, &subkey) < 0 || !name)
383 return 0;
385 if (!strcmp(subkey, "path")) {
386 if (!value)
387 return config_error_nonbool(var);
388 return add_man_viewer_path(name, namelen, value);
390 if (!strcmp(subkey, "cmd")) {
391 if (!value)
392 return config_error_nonbool(var);
393 return add_man_viewer_cmd(name, namelen, value);
396 return 0;
399 static int git_help_config(const char *var, const char *value, void *cb)
401 if (!strcmp(var, "help.format")) {
402 if (!value)
403 return config_error_nonbool(var);
404 help_format = parse_help_format(value);
405 return 0;
407 if (!strcmp(var, "help.htmlpath")) {
408 if (!value)
409 return config_error_nonbool(var);
410 html_path = xstrdup(value);
411 return 0;
413 if (!strcmp(var, "man.viewer")) {
414 if (!value)
415 return config_error_nonbool(var);
416 add_man_viewer(value);
417 return 0;
419 if (starts_with(var, "man."))
420 return add_man_viewer_info(var, value);
422 return git_default_config(var, value, cb);
425 static struct cmdnames main_cmds, other_cmds;
427 static int is_git_command(const char *s)
429 if (is_builtin(s))
430 return 1;
432 load_command_list("git-", &main_cmds, &other_cmds);
433 return is_in_cmdlist(&main_cmds, s) ||
434 is_in_cmdlist(&other_cmds, s);
437 static const char *cmd_to_page(const char *git_cmd)
439 if (!git_cmd)
440 return "git";
441 else if (starts_with(git_cmd, "git"))
442 return git_cmd;
443 else if (is_git_command(git_cmd))
444 return xstrfmt("git-%s", git_cmd);
445 else if (!strcmp("scalar", git_cmd))
446 return xstrdup(git_cmd);
447 else
448 return xstrfmt("git%s", git_cmd);
451 static void setup_man_path(void)
453 struct strbuf new_path = STRBUF_INIT;
454 const char *old_path = getenv("MANPATH");
455 char *git_man_path = system_path(GIT_MAN_PATH);
457 /* We should always put ':' after our path. If there is no
458 * old_path, the ':' at the end will let 'man' to try
459 * system-wide paths after ours to find the manual page. If
460 * there is old_path, we need ':' as delimiter. */
461 strbuf_addstr(&new_path, git_man_path);
462 strbuf_addch(&new_path, ':');
463 if (old_path)
464 strbuf_addstr(&new_path, old_path);
466 free(git_man_path);
467 setenv("MANPATH", new_path.buf, 1);
469 strbuf_release(&new_path);
472 static void exec_viewer(const char *name, const char *page)
474 const char *info = get_man_viewer_info(name);
476 if (!strcasecmp(name, "man"))
477 exec_man_man(info, page);
478 else if (!strcasecmp(name, "woman"))
479 exec_woman_emacs(info, page);
480 else if (!strcasecmp(name, "konqueror"))
481 exec_man_konqueror(info, page);
482 else if (info)
483 exec_man_cmd(info, page);
484 else
485 warning(_("'%s': unknown man viewer."), name);
488 static void show_man_page(const char *page)
490 struct man_viewer_list *viewer;
491 const char *fallback = getenv("GIT_MAN_VIEWER");
493 setup_man_path();
494 for (viewer = man_viewer_list; viewer; viewer = viewer->next)
496 exec_viewer(viewer->name, page); /* will return when unable */
498 if (fallback)
499 exec_viewer(fallback, page);
500 exec_viewer("man", page);
501 die(_("no man viewer handled the request"));
504 static void show_info_page(const char *page)
506 setenv("INFOPATH", system_path(GIT_INFO_PATH), 1);
507 execlp("info", "info", "gitman", page, (char *)NULL);
508 die(_("no info viewer handled the request"));
511 static void get_html_page_path(struct strbuf *page_path, const char *page)
513 struct stat st;
514 char *to_free = NULL;
516 if (!html_path)
517 html_path = to_free = system_path(GIT_HTML_PATH);
520 * Check that the page we're looking for exists.
522 if (!strstr(html_path, "://")) {
523 if (stat(mkpath("%s/%s.html", html_path, page), &st)
524 || !S_ISREG(st.st_mode))
525 die("'%s/%s.html': documentation file not found.",
526 html_path, page);
529 strbuf_init(page_path, 0);
530 strbuf_addf(page_path, "%s/%s.html", html_path, page);
531 free(to_free);
534 static void open_html(const char *path)
536 execl_git_cmd("web--browse", "-c", "help.browser", path, (char *)NULL);
539 static void show_html_page(const char *page)
541 struct strbuf page_path; /* it leaks but we exec bellow */
543 get_html_page_path(&page_path, page);
545 open_html(page_path.buf);
548 static const char *check_git_cmd(const char* cmd)
550 char *alias;
552 if (is_git_command(cmd))
553 return cmd;
555 alias = alias_lookup(cmd);
556 if (alias) {
557 const char **argv;
558 int count;
561 * handle_builtin() in git.c rewrites "git cmd --help"
562 * to "git help --exclude-guides cmd", so we can use
563 * exclude_guides to distinguish "git cmd --help" from
564 * "git help cmd". In the latter case, or if cmd is an
565 * alias for a shell command, just print the alias
566 * definition.
568 if (!exclude_guides || alias[0] == '!') {
569 printf_ln(_("'%s' is aliased to '%s'"), cmd, alias);
570 free(alias);
571 exit(0);
574 * Otherwise, we pretend that the command was "git
575 * word0 --help". We use split_cmdline() to get the
576 * first word of the alias, to ensure that we use the
577 * same rules as when the alias is actually
578 * used. split_cmdline() modifies alias in-place.
580 fprintf_ln(stderr, _("'%s' is aliased to '%s'"), cmd, alias);
581 count = split_cmdline(alias, &argv);
582 if (count < 0)
583 die(_("bad alias.%s string: %s"), cmd,
584 split_cmdline_strerror(count));
585 free(argv);
586 UNLEAK(alias);
587 return alias;
590 if (exclude_guides)
591 return help_unknown_cmd(cmd);
593 return cmd;
596 static void no_help_format(const char *opt_mode, enum help_format fmt)
598 const char *opt_fmt;
600 switch (fmt) {
601 case HELP_FORMAT_NONE:
602 return;
603 case HELP_FORMAT_MAN:
604 opt_fmt = "--man";
605 break;
606 case HELP_FORMAT_INFO:
607 opt_fmt = "--info";
608 break;
609 case HELP_FORMAT_WEB:
610 opt_fmt = "--web";
611 break;
612 default:
613 BUG("unreachable");
616 usage_msg_optf(_("options '%s' and '%s' cannot be used together"),
617 builtin_help_usage, builtin_help_options, opt_mode,
618 opt_fmt);
621 static void opt_mode_usage(int argc, const char *opt_mode,
622 enum help_format fmt)
624 if (argc)
625 usage_msg_optf(_("the '%s' option doesn't take any non-option arguments"),
626 builtin_help_usage, builtin_help_options,
627 opt_mode);
629 no_help_format(opt_mode, fmt);
632 int cmd_help(int argc, const char **argv, const char *prefix)
634 int nongit;
635 enum help_format parsed_help_format;
636 const char *page;
638 argc = parse_options(argc, argv, prefix, builtin_help_options,
639 builtin_help_usage, 0);
640 parsed_help_format = help_format;
642 if (cmd_mode != HELP_ACTION_ALL &&
643 (show_external_commands >= 0 ||
644 show_aliases >= 0))
645 usage_msg_opt(_("the '--no-[external-commands|aliases]' options can only be used with '--all'"),
646 builtin_help_usage, builtin_help_options);
648 switch (cmd_mode) {
649 case HELP_ACTION_ALL:
650 opt_mode_usage(argc, "--all", help_format);
651 if (verbose) {
652 setup_pager();
653 list_all_cmds_help(show_external_commands,
654 show_aliases);
655 return 0;
657 printf(_("usage: %s%s"), _(git_usage_string), "\n\n");
658 load_command_list("git-", &main_cmds, &other_cmds);
659 list_commands(&main_cmds, &other_cmds);
660 printf("%s\n", _(git_more_info_string));
661 break;
662 case HELP_ACTION_GUIDES:
663 opt_mode_usage(argc, "--guides", help_format);
664 list_guides_help();
665 printf("%s\n", _(git_more_info_string));
666 return 0;
667 case HELP_ACTION_CONFIG_FOR_COMPLETION:
668 opt_mode_usage(argc, "--config-for-completion", help_format);
669 list_config_help(SHOW_CONFIG_VARS);
670 return 0;
671 case HELP_ACTION_USER_INTERFACES:
672 opt_mode_usage(argc, "--user-interfaces", help_format);
673 list_user_interfaces_help();
674 return 0;
675 case HELP_ACTION_DEVELOPER_INTERFACES:
676 opt_mode_usage(argc, "--developer-interfaces", help_format);
677 list_developer_interfaces_help();
678 return 0;
679 case HELP_ACTION_CONFIG_SECTIONS_FOR_COMPLETION:
680 opt_mode_usage(argc, "--config-sections-for-completion",
681 help_format);
682 list_config_help(SHOW_CONFIG_SECTIONS);
683 return 0;
684 case HELP_ACTION_CONFIG:
685 opt_mode_usage(argc, "--config", help_format);
686 setup_pager();
687 list_config_help(SHOW_CONFIG_HUMAN);
688 printf("\n%s\n", _("'git help config' for more information"));
689 return 0;
692 if (!argv[0]) {
693 printf(_("usage: %s%s"), _(git_usage_string), "\n\n");
694 list_common_cmds_help();
695 printf("\n%s\n", _(git_more_info_string));
696 return 0;
699 setup_git_directory_gently(&nongit);
700 git_config(git_help_config, NULL);
702 if (parsed_help_format != HELP_FORMAT_NONE)
703 help_format = parsed_help_format;
704 if (help_format == HELP_FORMAT_NONE)
705 help_format = parse_help_format(DEFAULT_HELP_FORMAT);
707 argv[0] = check_git_cmd(argv[0]);
709 page = cmd_to_page(argv[0]);
710 switch (help_format) {
711 case HELP_FORMAT_NONE:
712 case HELP_FORMAT_MAN:
713 show_man_page(page);
714 break;
715 case HELP_FORMAT_INFO:
716 show_info_page(page);
717 break;
718 case HELP_FORMAT_WEB:
719 show_html_page(page);
720 break;
723 return 0;