]>
Commit | Line | Data |
---|---|---|
3d78d1f1 | 1 | /* |
3d78d1f1 MV |
2 | * Builtin help command |
3 | */ | |
4 | #include "cache.h" | |
b2141fc1 | 5 | #include "config.h" |
3d78d1f1 MV |
6 | #include "builtin.h" |
7 | #include "exec_cmd.h" | |
3d78d1f1 MV |
8 | #include "parse-options.h" |
9 | #include "run-command.h" | |
dbfae689 | 10 | #include "column.h" |
3d78d1f1 MV |
11 | #include "help.h" |
12 | ||
1cc8af04 VR |
13 | #ifndef DEFAULT_HELP_FORMAT |
14 | #define DEFAULT_HELP_FORMAT "man" | |
15 | #endif | |
16 | ||
3d78d1f1 MV |
17 | static struct man_viewer_list { |
18 | struct man_viewer_list *next; | |
19 | char name[FLEX_ARRAY]; | |
20 | } *man_viewer_list; | |
21 | ||
22 | static struct man_viewer_info_list { | |
23 | struct man_viewer_info_list *next; | |
24 | const char *info; | |
25 | char name[FLEX_ARRAY]; | |
26 | } *man_viewer_info_list; | |
27 | ||
28 | enum help_format { | |
3caa8239 | 29 | HELP_FORMAT_NONE, |
3d78d1f1 MV |
30 | HELP_FORMAT_MAN, |
31 | HELP_FORMAT_INFO, | |
4b05548f | 32 | HELP_FORMAT_WEB |
3d78d1f1 MV |
33 | }; |
34 | ||
89a852ef CW |
35 | static const char *html_path; |
36 | ||
3d78d1f1 | 37 | static int show_all = 0; |
65f98358 | 38 | static int show_guides = 0; |
63eae83f | 39 | static int verbose; |
dbfae689 | 40 | static unsigned int colopts; |
3caa8239 | 41 | static enum help_format help_format = HELP_FORMAT_NONE; |
af74128f | 42 | static int exclude_guides; |
3d78d1f1 | 43 | static struct option builtin_help_options[] = { |
15f7d494 | 44 | OPT_BOOL('a', "all", &show_all, N_("print all available commands")), |
af74128f | 45 | OPT_HIDDEN_BOOL(0, "exclude-guides", &exclude_guides, N_("exclude guides")), |
65f98358 | 46 | OPT_BOOL('g', "guides", &show_guides, N_("print list of useful guides")), |
68918696 NTND |
47 | OPT_SET_INT('m', "man", &help_format, N_("show man page"), HELP_FORMAT_MAN), |
48 | OPT_SET_INT('w', "web", &help_format, N_("show manual in web browser"), | |
3d78d1f1 | 49 | HELP_FORMAT_WEB), |
68918696 | 50 | OPT_SET_INT('i', "info", &help_format, N_("show info page"), |
3d78d1f1 | 51 | HELP_FORMAT_INFO), |
63eae83f | 52 | OPT__VERBOSE(&verbose, N_("print command description")), |
3d78d1f1 MV |
53 | OPT_END(), |
54 | }; | |
55 | ||
56 | static const char * const builtin_help_usage[] = { | |
9c9b4f2f | 57 | N_("git help [--all] [--guides] [--man | --web | --info] [<command>]"), |
3d78d1f1 MV |
58 | NULL |
59 | }; | |
60 | ||
61 | static enum help_format parse_help_format(const char *format) | |
62 | { | |
63 | if (!strcmp(format, "man")) | |
64 | return HELP_FORMAT_MAN; | |
65 | if (!strcmp(format, "info")) | |
66 | return HELP_FORMAT_INFO; | |
67 | if (!strcmp(format, "web") || !strcmp(format, "html")) | |
68 | return HELP_FORMAT_WEB; | |
9665627d | 69 | die(_("unrecognized help format '%s'"), format); |
3d78d1f1 MV |
70 | } |
71 | ||
72 | static const char *get_man_viewer_info(const char *name) | |
73 | { | |
74 | struct man_viewer_info_list *viewer; | |
75 | ||
76 | for (viewer = man_viewer_info_list; viewer; viewer = viewer->next) | |
77 | { | |
78 | if (!strcasecmp(name, viewer->name)) | |
79 | return viewer->info; | |
80 | } | |
81 | return NULL; | |
82 | } | |
83 | ||
84 | static int check_emacsclient_version(void) | |
85 | { | |
86 | struct strbuf buffer = STRBUF_INIT; | |
d3180279 | 87 | struct child_process ec_process = CHILD_PROCESS_INIT; |
3d78d1f1 MV |
88 | const char *argv_ec[] = { "emacsclient", "--version", NULL }; |
89 | int version; | |
90 | ||
91 | /* emacsclient prints its version number on stderr */ | |
3d78d1f1 MV |
92 | ec_process.argv = argv_ec; |
93 | ec_process.err = -1; | |
94 | ec_process.stdout_to_stderr = 1; | |
4e2715fb | 95 | if (start_command(&ec_process)) |
9665627d | 96 | return error(_("Failed to start emacsclient.")); |
4e2715fb | 97 | |
3d78d1f1 MV |
98 | strbuf_read(&buffer, ec_process.err, 20); |
99 | close(ec_process.err); | |
100 | ||
101 | /* | |
102 | * Don't bother checking return value, because "emacsclient --version" | |
103 | * seems to always exits with code 1. | |
104 | */ | |
105 | finish_command(&ec_process); | |
106 | ||
59556548 | 107 | if (!starts_with(buffer.buf, "emacsclient")) { |
3d78d1f1 | 108 | strbuf_release(&buffer); |
9665627d | 109 | return error(_("Failed to parse emacsclient version.")); |
3d78d1f1 MV |
110 | } |
111 | ||
112 | strbuf_remove(&buffer, 0, strlen("emacsclient")); | |
113 | version = atoi(buffer.buf); | |
114 | ||
115 | if (version < 22) { | |
3d78d1f1 | 116 | strbuf_release(&buffer); |
9665627d | 117 | return error(_("emacsclient version '%d' too old (< 22)."), |
4e2715fb | 118 | version); |
3d78d1f1 MV |
119 | } |
120 | ||
121 | strbuf_release(&buffer); | |
122 | return 0; | |
123 | } | |
124 | ||
4b25d091 | 125 | static void exec_woman_emacs(const char *path, const char *page) |
3d78d1f1 MV |
126 | { |
127 | if (!check_emacsclient_version()) { | |
128 | /* This works only with emacsclient version >= 22. */ | |
129 | struct strbuf man_page = STRBUF_INIT; | |
130 | ||
131 | if (!path) | |
132 | path = "emacsclient"; | |
133 | strbuf_addf(&man_page, "(woman \"%s\")", page); | |
5d314759 | 134 | execlp(path, "emacsclient", "-e", man_page.buf, (char *)NULL); |
57477498 | 135 | warning_errno(_("failed to exec '%s'"), path); |
bad0e2c6 | 136 | strbuf_release(&man_page); |
3d78d1f1 MV |
137 | } |
138 | } | |
139 | ||
4b25d091 | 140 | static void exec_man_konqueror(const char *path, const char *page) |
3d78d1f1 MV |
141 | { |
142 | const char *display = getenv("DISPLAY"); | |
143 | if (display && *display) { | |
144 | struct strbuf man_page = STRBUF_INIT; | |
145 | const char *filename = "kfmclient"; | |
146 | ||
147 | /* It's simpler to launch konqueror using kfmclient. */ | |
148 | if (path) { | |
4c9ac3bf JK |
149 | size_t len; |
150 | if (strip_suffix(path, "/konqueror", &len)) | |
151 | path = xstrfmt("%.*s/kfmclient", (int)len, path); | |
152 | filename = basename((char *)path); | |
3d78d1f1 MV |
153 | } else |
154 | path = "kfmclient"; | |
155 | strbuf_addf(&man_page, "man:%s(1)", page); | |
5d314759 | 156 | execlp(path, filename, "newTab", man_page.buf, (char *)NULL); |
57477498 | 157 | warning_errno(_("failed to exec '%s'"), path); |
a981a9f0 | 158 | strbuf_release(&man_page); |
3d78d1f1 MV |
159 | } |
160 | } | |
161 | ||
4b25d091 | 162 | static void exec_man_man(const char *path, const char *page) |
3d78d1f1 MV |
163 | { |
164 | if (!path) | |
165 | path = "man"; | |
5d314759 | 166 | execlp(path, "man", page, (char *)NULL); |
57477498 | 167 | warning_errno(_("failed to exec '%s'"), path); |
3d78d1f1 MV |
168 | } |
169 | ||
170 | static void exec_man_cmd(const char *cmd, const char *page) | |
171 | { | |
172 | struct strbuf shell_cmd = STRBUF_INIT; | |
173 | strbuf_addf(&shell_cmd, "%s %s", cmd, page); | |
b680a86a | 174 | execl(SHELL_PATH, SHELL_PATH, "-c", shell_cmd.buf, (char *)NULL); |
57477498 | 175 | warning(_("failed to exec '%s'"), cmd); |
72462186 | 176 | strbuf_release(&shell_cmd); |
3d78d1f1 MV |
177 | } |
178 | ||
179 | static void add_man_viewer(const char *name) | |
180 | { | |
181 | struct man_viewer_list **p = &man_viewer_list; | |
3d78d1f1 MV |
182 | |
183 | while (*p) | |
184 | p = &((*p)->next); | |
96ffc06f | 185 | FLEX_ALLOC_STR(*p, name, name); |
3d78d1f1 MV |
186 | } |
187 | ||
188 | static int supported_man_viewer(const char *name, size_t len) | |
189 | { | |
190 | return (!strncasecmp("man", name, len) || | |
191 | !strncasecmp("woman", name, len) || | |
192 | !strncasecmp("konqueror", name, len)); | |
193 | } | |
194 | ||
195 | static void do_add_man_viewer_info(const char *name, | |
196 | size_t len, | |
197 | const char *value) | |
198 | { | |
8adda462 BW |
199 | struct man_viewer_info_list *new_man_viewer; |
200 | FLEX_ALLOC_MEM(new_man_viewer, name, name, len); | |
201 | new_man_viewer->info = xstrdup(value); | |
202 | new_man_viewer->next = man_viewer_info_list; | |
203 | man_viewer_info_list = new_man_viewer; | |
3d78d1f1 MV |
204 | } |
205 | ||
206 | static int add_man_viewer_path(const char *name, | |
207 | size_t len, | |
208 | const char *value) | |
209 | { | |
210 | if (supported_man_viewer(name, len)) | |
211 | do_add_man_viewer_info(name, len, value); | |
212 | else | |
9665627d NTND |
213 | warning(_("'%s': path for unsupported man viewer.\n" |
214 | "Please consider using 'man.<tool>.cmd' instead."), | |
3d78d1f1 MV |
215 | name); |
216 | ||
217 | return 0; | |
218 | } | |
219 | ||
220 | static int add_man_viewer_cmd(const char *name, | |
221 | size_t len, | |
222 | const char *value) | |
223 | { | |
224 | if (supported_man_viewer(name, len)) | |
9665627d NTND |
225 | warning(_("'%s': cmd for supported man viewer.\n" |
226 | "Please consider using 'man.<tool>.path' instead."), | |
3d78d1f1 MV |
227 | name); |
228 | else | |
229 | do_add_man_viewer_info(name, len, value); | |
230 | ||
231 | return 0; | |
232 | } | |
233 | ||
234 | static int add_man_viewer_info(const char *var, const char *value) | |
235 | { | |
4d5c6cef JK |
236 | const char *name, *subkey; |
237 | int namelen; | |
3d78d1f1 | 238 | |
4d5c6cef | 239 | if (parse_config_key(var, "man", &name, &namelen, &subkey) < 0 || !name) |
178b513e | 240 | return 0; |
3d78d1f1 | 241 | |
4d5c6cef | 242 | if (!strcmp(subkey, "path")) { |
3d78d1f1 MV |
243 | if (!value) |
244 | return config_error_nonbool(var); | |
4d5c6cef | 245 | return add_man_viewer_path(name, namelen, value); |
3d78d1f1 | 246 | } |
4d5c6cef | 247 | if (!strcmp(subkey, "cmd")) { |
3d78d1f1 MV |
248 | if (!value) |
249 | return config_error_nonbool(var); | |
4d5c6cef | 250 | return add_man_viewer_cmd(name, namelen, value); |
3d78d1f1 MV |
251 | } |
252 | ||
3d78d1f1 MV |
253 | return 0; |
254 | } | |
255 | ||
256 | static int git_help_config(const char *var, const char *value, void *cb) | |
257 | { | |
59556548 | 258 | if (starts_with(var, "column.")) |
dbfae689 | 259 | return git_column_config(var, value, "help", &colopts); |
3d78d1f1 MV |
260 | if (!strcmp(var, "help.format")) { |
261 | if (!value) | |
262 | return config_error_nonbool(var); | |
263 | help_format = parse_help_format(value); | |
264 | return 0; | |
265 | } | |
89a852ef CW |
266 | if (!strcmp(var, "help.htmlpath")) { |
267 | if (!value) | |
268 | return config_error_nonbool(var); | |
269 | html_path = xstrdup(value); | |
270 | return 0; | |
271 | } | |
3d78d1f1 MV |
272 | if (!strcmp(var, "man.viewer")) { |
273 | if (!value) | |
274 | return config_error_nonbool(var); | |
275 | add_man_viewer(value); | |
276 | return 0; | |
277 | } | |
59556548 | 278 | if (starts_with(var, "man.")) |
3d78d1f1 MV |
279 | return add_man_viewer_info(var, value); |
280 | ||
281 | return git_default_config(var, value, cb); | |
282 | } | |
283 | ||
c7371e99 | 284 | static struct cmdnames main_cmds, other_cmds; |
3d78d1f1 | 285 | |
3d78d1f1 MV |
286 | static int is_git_command(const char *s) |
287 | { | |
c6127fa3 SS |
288 | if (is_builtin(s)) |
289 | return 1; | |
290 | ||
a3c52634 | 291 | load_command_list("git-", &main_cmds, &other_cmds); |
3d78d1f1 MV |
292 | return is_in_cmdlist(&main_cmds, s) || |
293 | is_in_cmdlist(&other_cmds, s); | |
294 | } | |
295 | ||
3d78d1f1 MV |
296 | static const char *cmd_to_page(const char *git_cmd) |
297 | { | |
298 | if (!git_cmd) | |
299 | return "git"; | |
59556548 | 300 | else if (starts_with(git_cmd, "git")) |
3d78d1f1 MV |
301 | return git_cmd; |
302 | else if (is_git_command(git_cmd)) | |
acd47eec | 303 | return xstrfmt("git-%s", git_cmd); |
3d78d1f1 | 304 | else |
acd47eec | 305 | return xstrfmt("git%s", git_cmd); |
3d78d1f1 MV |
306 | } |
307 | ||
308 | static void setup_man_path(void) | |
309 | { | |
f285a2d7 | 310 | struct strbuf new_path = STRBUF_INIT; |
3d78d1f1 | 311 | const char *old_path = getenv("MANPATH"); |
59362e56 | 312 | char *git_man_path = system_path(GIT_MAN_PATH); |
3d78d1f1 | 313 | |
3d78d1f1 MV |
314 | /* We should always put ':' after our path. If there is no |
315 | * old_path, the ':' at the end will let 'man' to try | |
316 | * system-wide paths after ours to find the manual page. If | |
317 | * there is old_path, we need ':' as delimiter. */ | |
59362e56 | 318 | strbuf_addstr(&new_path, git_man_path); |
3d78d1f1 MV |
319 | strbuf_addch(&new_path, ':'); |
320 | if (old_path) | |
321 | strbuf_addstr(&new_path, old_path); | |
322 | ||
59362e56 | 323 | free(git_man_path); |
3d78d1f1 MV |
324 | setenv("MANPATH", new_path.buf, 1); |
325 | ||
326 | strbuf_release(&new_path); | |
327 | } | |
328 | ||
329 | static void exec_viewer(const char *name, const char *page) | |
330 | { | |
331 | const char *info = get_man_viewer_info(name); | |
332 | ||
333 | if (!strcasecmp(name, "man")) | |
334 | exec_man_man(info, page); | |
335 | else if (!strcasecmp(name, "woman")) | |
336 | exec_woman_emacs(info, page); | |
337 | else if (!strcasecmp(name, "konqueror")) | |
338 | exec_man_konqueror(info, page); | |
339 | else if (info) | |
340 | exec_man_cmd(info, page); | |
341 | else | |
9665627d | 342 | warning(_("'%s': unknown man viewer."), name); |
3d78d1f1 MV |
343 | } |
344 | ||
345 | static void show_man_page(const char *git_cmd) | |
346 | { | |
347 | struct man_viewer_list *viewer; | |
348 | const char *page = cmd_to_page(git_cmd); | |
5059a427 | 349 | const char *fallback = getenv("GIT_MAN_VIEWER"); |
3d78d1f1 MV |
350 | |
351 | setup_man_path(); | |
352 | for (viewer = man_viewer_list; viewer; viewer = viewer->next) | |
353 | { | |
354 | exec_viewer(viewer->name, page); /* will return when unable */ | |
355 | } | |
5059a427 RF |
356 | if (fallback) |
357 | exec_viewer(fallback, page); | |
3d78d1f1 | 358 | exec_viewer("man", page); |
9665627d | 359 | die(_("no man viewer handled the request")); |
3d78d1f1 MV |
360 | } |
361 | ||
362 | static void show_info_page(const char *git_cmd) | |
363 | { | |
364 | const char *page = cmd_to_page(git_cmd); | |
026fa0d5 | 365 | setenv("INFOPATH", system_path(GIT_INFO_PATH), 1); |
5d314759 | 366 | execlp("info", "info", "gitman", page, (char *)NULL); |
9665627d | 367 | die(_("no info viewer handled the request")); |
3d78d1f1 MV |
368 | } |
369 | ||
370 | static void get_html_page_path(struct strbuf *page_path, const char *page) | |
371 | { | |
372 | struct stat st; | |
59362e56 JH |
373 | char *to_free = NULL; |
374 | ||
89a852ef | 375 | if (!html_path) |
59362e56 | 376 | html_path = to_free = system_path(GIT_HTML_PATH); |
3d78d1f1 MV |
377 | |
378 | /* Check that we have a git documentation directory. */ | |
86272b4f CW |
379 | if (!strstr(html_path, "://")) { |
380 | if (stat(mkpath("%s/git.html", html_path), &st) | |
381 | || !S_ISREG(st.st_mode)) | |
382 | die("'%s': not a documentation directory.", html_path); | |
383 | } | |
3d78d1f1 MV |
384 | |
385 | strbuf_init(page_path, 0); | |
386 | strbuf_addf(page_path, "%s/%s.html", html_path, page); | |
59362e56 | 387 | free(to_free); |
3d78d1f1 MV |
388 | } |
389 | ||
2af202be | 390 | static void open_html(const char *path) |
3d78d1f1 | 391 | { |
5d314759 | 392 | execl_git_cmd("web--browse", "-c", "help.browser", path, (char *)NULL); |
3d78d1f1 | 393 | } |
3d78d1f1 MV |
394 | |
395 | static void show_html_page(const char *git_cmd) | |
396 | { | |
397 | const char *page = cmd_to_page(git_cmd); | |
398 | struct strbuf page_path; /* it leaks but we exec bellow */ | |
399 | ||
400 | get_html_page_path(&page_path, page); | |
401 | ||
402 | open_html(page_path.buf); | |
403 | } | |
404 | ||
af74128f RT |
405 | static const char *check_git_cmd(const char* cmd) |
406 | { | |
407 | char *alias; | |
408 | ||
409 | if (is_git_command(cmd)) | |
410 | return cmd; | |
411 | ||
412 | alias = alias_lookup(cmd); | |
413 | if (alias) { | |
b3a8076e | 414 | printf_ln(_("'%s' is aliased to '%s'"), cmd, alias); |
af74128f RT |
415 | free(alias); |
416 | exit(0); | |
417 | } | |
418 | ||
419 | if (exclude_guides) | |
420 | return help_unknown_cmd(cmd); | |
421 | ||
422 | return cmd; | |
423 | } | |
424 | ||
3d78d1f1 MV |
425 | int cmd_help(int argc, const char **argv, const char *prefix) |
426 | { | |
427 | int nongit; | |
3caa8239 | 428 | enum help_format parsed_help_format; |
3d78d1f1 | 429 | |
37782920 | 430 | argc = parse_options(argc, argv, prefix, builtin_help_options, |
3d78d1f1 | 431 | builtin_help_usage, 0); |
3caa8239 | 432 | parsed_help_format = help_format; |
3d78d1f1 MV |
433 | |
434 | if (show_all) { | |
dbfae689 | 435 | git_config(git_help_config, NULL); |
63eae83f NTND |
436 | if (verbose) { |
437 | setup_pager(); | |
438 | list_all_cmds_help(); | |
439 | return 0; | |
440 | } | |
9665627d | 441 | printf(_("usage: %s%s"), _(git_usage_string), "\n\n"); |
a3c52634 | 442 | load_command_list("git-", &main_cmds, &other_cmds); |
f4ed0af6 | 443 | list_commands(colopts, &main_cmds, &other_cmds); |
15f7d494 PO |
444 | } |
445 | ||
002b726a PO |
446 | if (show_guides) |
447 | list_common_guides_help(); | |
65f98358 PO |
448 | |
449 | if (show_all || show_guides) { | |
9665627d | 450 | printf("%s\n", _(git_more_info_string)); |
15f7d494 PO |
451 | /* |
452 | * We're done. Ignore any remaining args | |
453 | */ | |
3d78d1f1 MV |
454 | return 0; |
455 | } | |
456 | ||
457 | if (!argv[0]) { | |
9665627d | 458 | printf(_("usage: %s%s"), _(git_usage_string), "\n\n"); |
3d78d1f1 | 459 | list_common_cmds_help(); |
9665627d | 460 | printf("\n%s\n", _(git_more_info_string)); |
3d78d1f1 MV |
461 | return 0; |
462 | } | |
463 | ||
af6fbf9f DA |
464 | setup_git_directory_gently(&nongit); |
465 | git_config(git_help_config, NULL); | |
466 | ||
d0408c0c | 467 | if (parsed_help_format != HELP_FORMAT_NONE) |
3caa8239 | 468 | help_format = parsed_help_format; |
d0408c0c PT |
469 | if (help_format == HELP_FORMAT_NONE) |
470 | help_format = parse_help_format(DEFAULT_HELP_FORMAT); | |
3caa8239 | 471 | |
af74128f | 472 | argv[0] = check_git_cmd(argv[0]); |
3d78d1f1 MV |
473 | |
474 | switch (help_format) { | |
3caa8239 | 475 | case HELP_FORMAT_NONE: |
3d78d1f1 MV |
476 | case HELP_FORMAT_MAN: |
477 | show_man_page(argv[0]); | |
478 | break; | |
479 | case HELP_FORMAT_INFO: | |
480 | show_info_page(argv[0]); | |
481 | break; | |
482 | case HELP_FORMAT_WEB: | |
483 | show_html_page(argv[0]); | |
484 | break; | |
485 | } | |
486 | ||
487 | return 0; | |
488 | } |