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