Documentation: clarify that `-h` alone stands for `help`
[git/raj.git] / builtin / help.c
blob7739a5c1551426fd2302aaf5daa10e454d174195
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 "parse-options.h"
9 #include "run-command.h"
10 #include "column.h"
11 #include "help.h"
12 #include "alias.h"
14 #ifndef DEFAULT_HELP_FORMAT
15 #define DEFAULT_HELP_FORMAT "man"
16 #endif
18 static struct man_viewer_list {
19 struct man_viewer_list *next;
20 char name[FLEX_ARRAY];
21 } *man_viewer_list;
23 static struct man_viewer_info_list {
24 struct man_viewer_info_list *next;
25 const char *info;
26 char name[FLEX_ARRAY];
27 } *man_viewer_info_list;
29 enum help_format {
30 HELP_FORMAT_NONE,
31 HELP_FORMAT_MAN,
32 HELP_FORMAT_INFO,
33 HELP_FORMAT_WEB
36 static const char *html_path;
38 static int show_all = 0;
39 static int show_guides = 0;
40 static int show_config;
41 static int verbose = 1;
42 static unsigned int colopts;
43 static enum help_format help_format = HELP_FORMAT_NONE;
44 static int exclude_guides;
45 static struct option builtin_help_options[] = {
46 OPT_BOOL('a', "all", &show_all, N_("print all available commands")),
47 OPT_HIDDEN_BOOL(0, "exclude-guides", &exclude_guides, N_("exclude guides")),
48 OPT_BOOL('g', "guides", &show_guides, N_("print list of useful guides")),
49 OPT_BOOL('c', "config", &show_config, N_("print all configuration variable names")),
50 OPT_SET_INT_F(0, "config-for-completion", &show_config, "", 2, PARSE_OPT_HIDDEN),
51 OPT_SET_INT('m', "man", &help_format, N_("show man page"), HELP_FORMAT_MAN),
52 OPT_SET_INT('w', "web", &help_format, N_("show manual in web browser"),
53 HELP_FORMAT_WEB),
54 OPT_SET_INT('i', "info", &help_format, N_("show info page"),
55 HELP_FORMAT_INFO),
56 OPT__VERBOSE(&verbose, N_("print command description")),
57 OPT_END(),
60 static const char * const builtin_help_usage[] = {
61 N_("git help [--all] [--guides] [--man | --web | --info] [<command>]"),
62 NULL
65 static enum help_format parse_help_format(const char *format)
67 if (!strcmp(format, "man"))
68 return HELP_FORMAT_MAN;
69 if (!strcmp(format, "info"))
70 return HELP_FORMAT_INFO;
71 if (!strcmp(format, "web") || !strcmp(format, "html"))
72 return HELP_FORMAT_WEB;
73 die(_("unrecognized help format '%s'"), format);
76 static const char *get_man_viewer_info(const char *name)
78 struct man_viewer_info_list *viewer;
80 for (viewer = man_viewer_info_list; viewer; viewer = viewer->next)
82 if (!strcasecmp(name, viewer->name))
83 return viewer->info;
85 return NULL;
88 static int check_emacsclient_version(void)
90 struct strbuf buffer = STRBUF_INIT;
91 struct child_process ec_process = CHILD_PROCESS_INIT;
92 const char *argv_ec[] = { "emacsclient", "--version", NULL };
93 int version;
95 /* emacsclient prints its version number on stderr */
96 ec_process.argv = argv_ec;
97 ec_process.err = -1;
98 ec_process.stdout_to_stderr = 1;
99 if (start_command(&ec_process))
100 return error(_("Failed to start emacsclient."));
102 strbuf_read(&buffer, ec_process.err, 20);
103 close(ec_process.err);
106 * Don't bother checking return value, because "emacsclient --version"
107 * seems to always exits with code 1.
109 finish_command(&ec_process);
111 if (!starts_with(buffer.buf, "emacsclient")) {
112 strbuf_release(&buffer);
113 return error(_("Failed to parse emacsclient version."));
116 strbuf_remove(&buffer, 0, strlen("emacsclient"));
117 version = atoi(buffer.buf);
119 if (version < 22) {
120 strbuf_release(&buffer);
121 return error(_("emacsclient version '%d' too old (< 22)."),
122 version);
125 strbuf_release(&buffer);
126 return 0;
129 static void exec_woman_emacs(const char *path, const char *page)
131 if (!check_emacsclient_version()) {
132 /* This works only with emacsclient version >= 22. */
133 struct strbuf man_page = STRBUF_INIT;
135 if (!path)
136 path = "emacsclient";
137 strbuf_addf(&man_page, "(woman \"%s\")", page);
138 execlp(path, "emacsclient", "-e", man_page.buf, (char *)NULL);
139 warning_errno(_("failed to exec '%s'"), path);
140 strbuf_release(&man_page);
144 static void exec_man_konqueror(const char *path, const char *page)
146 const char *display = getenv("DISPLAY");
147 if (display && *display) {
148 struct strbuf man_page = STRBUF_INIT;
149 const char *filename = "kfmclient";
151 /* It's simpler to launch konqueror using kfmclient. */
152 if (path) {
153 size_t len;
154 if (strip_suffix(path, "/konqueror", &len))
155 path = xstrfmt("%.*s/kfmclient", (int)len, path);
156 filename = basename((char *)path);
157 } else
158 path = "kfmclient";
159 strbuf_addf(&man_page, "man:%s(1)", page);
160 execlp(path, filename, "newTab", man_page.buf, (char *)NULL);
161 warning_errno(_("failed to exec '%s'"), path);
162 strbuf_release(&man_page);
166 static void exec_man_man(const char *path, const char *page)
168 if (!path)
169 path = "man";
170 execlp(path, "man", page, (char *)NULL);
171 warning_errno(_("failed to exec '%s'"), path);
174 static void exec_man_cmd(const char *cmd, const char *page)
176 struct strbuf shell_cmd = STRBUF_INIT;
177 strbuf_addf(&shell_cmd, "%s %s", cmd, page);
178 execl(SHELL_PATH, SHELL_PATH, "-c", shell_cmd.buf, (char *)NULL);
179 warning(_("failed to exec '%s'"), cmd);
180 strbuf_release(&shell_cmd);
183 static void add_man_viewer(const char *name)
185 struct man_viewer_list **p = &man_viewer_list;
187 while (*p)
188 p = &((*p)->next);
189 FLEX_ALLOC_STR(*p, name, name);
192 static int supported_man_viewer(const char *name, size_t len)
194 return (!strncasecmp("man", name, len) ||
195 !strncasecmp("woman", name, len) ||
196 !strncasecmp("konqueror", name, len));
199 static void do_add_man_viewer_info(const char *name,
200 size_t len,
201 const char *value)
203 struct man_viewer_info_list *new_man_viewer;
204 FLEX_ALLOC_MEM(new_man_viewer, name, name, len);
205 new_man_viewer->info = xstrdup(value);
206 new_man_viewer->next = man_viewer_info_list;
207 man_viewer_info_list = new_man_viewer;
210 static int add_man_viewer_path(const char *name,
211 size_t len,
212 const char *value)
214 if (supported_man_viewer(name, len))
215 do_add_man_viewer_info(name, len, value);
216 else
217 warning(_("'%s': path for unsupported man viewer.\n"
218 "Please consider using 'man.<tool>.cmd' instead."),
219 name);
221 return 0;
224 static int add_man_viewer_cmd(const char *name,
225 size_t len,
226 const char *value)
228 if (supported_man_viewer(name, len))
229 warning(_("'%s': cmd for supported man viewer.\n"
230 "Please consider using 'man.<tool>.path' instead."),
231 name);
232 else
233 do_add_man_viewer_info(name, len, value);
235 return 0;
238 static int add_man_viewer_info(const char *var, const char *value)
240 const char *name, *subkey;
241 int namelen;
243 if (parse_config_key(var, "man", &name, &namelen, &subkey) < 0 || !name)
244 return 0;
246 if (!strcmp(subkey, "path")) {
247 if (!value)
248 return config_error_nonbool(var);
249 return add_man_viewer_path(name, namelen, value);
251 if (!strcmp(subkey, "cmd")) {
252 if (!value)
253 return config_error_nonbool(var);
254 return add_man_viewer_cmd(name, namelen, value);
257 return 0;
260 static int git_help_config(const char *var, const char *value, void *cb)
262 if (starts_with(var, "column."))
263 return git_column_config(var, value, "help", &colopts);
264 if (!strcmp(var, "help.format")) {
265 if (!value)
266 return config_error_nonbool(var);
267 help_format = parse_help_format(value);
268 return 0;
270 if (!strcmp(var, "help.htmlpath")) {
271 if (!value)
272 return config_error_nonbool(var);
273 html_path = xstrdup(value);
274 return 0;
276 if (!strcmp(var, "man.viewer")) {
277 if (!value)
278 return config_error_nonbool(var);
279 add_man_viewer(value);
280 return 0;
282 if (starts_with(var, "man."))
283 return add_man_viewer_info(var, value);
285 return git_default_config(var, value, cb);
288 static struct cmdnames main_cmds, other_cmds;
290 static int is_git_command(const char *s)
292 if (is_builtin(s))
293 return 1;
295 load_command_list("git-", &main_cmds, &other_cmds);
296 return is_in_cmdlist(&main_cmds, s) ||
297 is_in_cmdlist(&other_cmds, s);
300 static const char *cmd_to_page(const char *git_cmd)
302 if (!git_cmd)
303 return "git";
304 else if (starts_with(git_cmd, "git"))
305 return git_cmd;
306 else if (is_git_command(git_cmd))
307 return xstrfmt("git-%s", git_cmd);
308 else
309 return xstrfmt("git%s", git_cmd);
312 static void setup_man_path(void)
314 struct strbuf new_path = STRBUF_INIT;
315 const char *old_path = getenv("MANPATH");
316 char *git_man_path = system_path(GIT_MAN_PATH);
318 /* We should always put ':' after our path. If there is no
319 * old_path, the ':' at the end will let 'man' to try
320 * system-wide paths after ours to find the manual page. If
321 * there is old_path, we need ':' as delimiter. */
322 strbuf_addstr(&new_path, git_man_path);
323 strbuf_addch(&new_path, ':');
324 if (old_path)
325 strbuf_addstr(&new_path, old_path);
327 free(git_man_path);
328 setenv("MANPATH", new_path.buf, 1);
330 strbuf_release(&new_path);
333 static void exec_viewer(const char *name, const char *page)
335 const char *info = get_man_viewer_info(name);
337 if (!strcasecmp(name, "man"))
338 exec_man_man(info, page);
339 else if (!strcasecmp(name, "woman"))
340 exec_woman_emacs(info, page);
341 else if (!strcasecmp(name, "konqueror"))
342 exec_man_konqueror(info, page);
343 else if (info)
344 exec_man_cmd(info, page);
345 else
346 warning(_("'%s': unknown man viewer."), name);
349 static void show_man_page(const char *git_cmd)
351 struct man_viewer_list *viewer;
352 const char *page = cmd_to_page(git_cmd);
353 const char *fallback = getenv("GIT_MAN_VIEWER");
355 setup_man_path();
356 for (viewer = man_viewer_list; viewer; viewer = viewer->next)
358 exec_viewer(viewer->name, page); /* will return when unable */
360 if (fallback)
361 exec_viewer(fallback, page);
362 exec_viewer("man", page);
363 die(_("no man viewer handled the request"));
366 static void show_info_page(const char *git_cmd)
368 const char *page = cmd_to_page(git_cmd);
369 setenv("INFOPATH", system_path(GIT_INFO_PATH), 1);
370 execlp("info", "info", "gitman", page, (char *)NULL);
371 die(_("no info viewer handled the request"));
374 static void get_html_page_path(struct strbuf *page_path, const char *page)
376 struct stat st;
377 char *to_free = NULL;
379 if (!html_path)
380 html_path = to_free = system_path(GIT_HTML_PATH);
382 /* Check that we have a git documentation directory. */
383 if (!strstr(html_path, "://")) {
384 if (stat(mkpath("%s/git.html", html_path), &st)
385 || !S_ISREG(st.st_mode))
386 die("'%s': not a documentation directory.", html_path);
389 strbuf_init(page_path, 0);
390 strbuf_addf(page_path, "%s/%s.html", html_path, page);
391 free(to_free);
394 static void open_html(const char *path)
396 execl_git_cmd("web--browse", "-c", "help.browser", path, (char *)NULL);
399 static void show_html_page(const char *git_cmd)
401 const char *page = cmd_to_page(git_cmd);
402 struct strbuf page_path; /* it leaks but we exec bellow */
404 get_html_page_path(&page_path, page);
406 open_html(page_path.buf);
409 static const char *check_git_cmd(const char* cmd)
411 char *alias;
413 if (is_git_command(cmd))
414 return cmd;
416 alias = alias_lookup(cmd);
417 if (alias) {
418 const char **argv;
419 int count;
422 * handle_builtin() in git.c rewrites "git cmd --help"
423 * to "git help --exclude-guides cmd", so we can use
424 * exclude_guides to distinguish "git cmd --help" from
425 * "git help cmd". In the latter case, or if cmd is an
426 * alias for a shell command, just print the alias
427 * definition.
429 if (!exclude_guides || alias[0] == '!') {
430 printf_ln(_("'%s' is aliased to '%s'"), cmd, alias);
431 free(alias);
432 exit(0);
435 * Otherwise, we pretend that the command was "git
436 * word0 --help". We use split_cmdline() to get the
437 * first word of the alias, to ensure that we use the
438 * same rules as when the alias is actually
439 * used. split_cmdline() modifies alias in-place.
441 fprintf_ln(stderr, _("'%s' is aliased to '%s'"), cmd, alias);
442 count = split_cmdline(alias, &argv);
443 if (count < 0)
444 die(_("bad alias.%s string: %s"), cmd,
445 split_cmdline_strerror(count));
446 free(argv);
447 UNLEAK(alias);
448 return alias;
451 if (exclude_guides)
452 return help_unknown_cmd(cmd);
454 return cmd;
457 int cmd_help(int argc, const char **argv, const char *prefix)
459 int nongit;
460 enum help_format parsed_help_format;
462 argc = parse_options(argc, argv, prefix, builtin_help_options,
463 builtin_help_usage, 0);
464 parsed_help_format = help_format;
466 if (show_all) {
467 git_config(git_help_config, NULL);
468 if (verbose) {
469 setup_pager();
470 list_all_cmds_help();
471 return 0;
473 printf(_("usage: %s%s"), _(git_usage_string), "\n\n");
474 load_command_list("git-", &main_cmds, &other_cmds);
475 list_commands(colopts, &main_cmds, &other_cmds);
478 if (show_config) {
479 int for_human = show_config == 1;
481 if (!for_human) {
482 list_config_help(for_human);
483 return 0;
485 setup_pager();
486 list_config_help(for_human);
487 printf("\n%s\n", _("'git help config' for more information"));
488 return 0;
491 if (show_guides)
492 list_common_guides_help();
494 if (show_all || show_guides) {
495 printf("%s\n", _(git_more_info_string));
497 * We're done. Ignore any remaining args
499 return 0;
502 if (!argv[0]) {
503 printf(_("usage: %s%s"), _(git_usage_string), "\n\n");
504 list_common_cmds_help();
505 printf("\n%s\n", _(git_more_info_string));
506 return 0;
509 setup_git_directory_gently(&nongit);
510 git_config(git_help_config, NULL);
512 if (parsed_help_format != HELP_FORMAT_NONE)
513 help_format = parsed_help_format;
514 if (help_format == HELP_FORMAT_NONE)
515 help_format = parse_help_format(DEFAULT_HELP_FORMAT);
517 argv[0] = check_git_cmd(argv[0]);
519 switch (help_format) {
520 case HELP_FORMAT_NONE:
521 case HELP_FORMAT_MAN:
522 show_man_page(argv[0]);
523 break;
524 case HELP_FORMAT_INFO:
525 show_info_page(argv[0]);
526 break;
527 case HELP_FORMAT_WEB:
528 show_html_page(argv[0]);
529 break;
532 return 0;