fix "builtin-*" references to be "builtin/*"
[git] / builtin / help.c
1 /*
2  * Builtin help command
3  */
4 #include "cache.h"
5 #include "builtin.h"
6 #include "exec_cmd.h"
7 #include "parse-options.h"
8 #include "run-command.h"
9 #include "column.h"
10 #include "help.h"
11
12 #ifndef DEFAULT_HELP_FORMAT
13 #define DEFAULT_HELP_FORMAT "man"
14 #endif
15
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 {
28         HELP_FORMAT_NONE,
29         HELP_FORMAT_MAN,
30         HELP_FORMAT_INFO,
31         HELP_FORMAT_WEB
32 };
33
34 static const char *html_path;
35
36 static int show_all = 0;
37 static unsigned int colopts;
38 static enum help_format help_format = HELP_FORMAT_NONE;
39 static struct option builtin_help_options[] = {
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"),
43                         HELP_FORMAT_WEB),
44         OPT_SET_INT('i', "info", &help_format, N_("show info page"),
45                         HELP_FORMAT_INFO),
46         OPT_END(),
47 };
48
49 static const char * const builtin_help_usage[] = {
50         N_("git help [--all] [--man|--web|--info] [command]"),
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;
62         die(_("unrecognized help format '%s'"), format);
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;
89         if (start_command(&ec_process))
90                 return error(_("Failed to start emacsclient."));
91
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")) {
102                 strbuf_release(&buffer);
103                 return error(_("Failed to parse emacsclient version."));
104         }
105
106         strbuf_remove(&buffer, 0, strlen("emacsclient"));
107         version = atoi(buffer.buf);
108
109         if (version < 22) {
110                 strbuf_release(&buffer);
111                 return error(_("emacsclient version '%d' too old (< 22)."),
112                         version);
113         }
114
115         strbuf_release(&buffer);
116         return 0;
117 }
118
119 static void exec_woman_emacs(const char *path, const char *page)
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);
128                 execlp(path, "emacsclient", "-e", man_page.buf, (char *)NULL);
129                 warning(_("failed to exec '%s': %s"), path, strerror(errno));
130         }
131 }
132
133 static void exec_man_konqueror(const char *path, const char *page)
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);
156                 execlp(path, filename, "newTab", man_page.buf, (char *)NULL);
157                 warning(_("failed to exec '%s': %s"), path, strerror(errno));
158         }
159 }
160
161 static void exec_man_man(const char *path, const char *page)
162 {
163         if (!path)
164                 path = "man";
165         execlp(path, "man", page, (char *)NULL);
166         warning(_("failed to exec '%s': %s"), path, strerror(errno));
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);
173         execl("/bin/sh", "sh", "-c", shell_cmd.buf, (char *)NULL);
174         warning(_("failed to exec '%s': %s"), cmd, strerror(errno));
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
214                 warning(_("'%s': path for unsupported man viewer.\n"
215                           "Please consider using 'man.<tool>.cmd' instead."),
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))
226                 warning(_("'%s': cmd for supported man viewer.\n"
227                           "Please consider using 'man.<tool>.path' instead."),
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)
241                 return 0;
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
254         return 0;
255 }
256
257 static int git_help_config(const char *var, const char *value, void *cb)
258 {
259         if (!prefixcmp(var, "column."))
260                 return git_column_config(var, value, "help", &colopts);
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         }
267         if (!strcmp(var, "help.htmlpath")) {
268                 if (!value)
269                         return config_error_nonbool(var);
270                 html_path = xstrdup(value);
271                 return 0;
272         }
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
285 static struct cmdnames main_cmds, other_cmds;
286
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 {
317         struct strbuf new_path = STRBUF_INIT;
318         const char *old_path = getenv("MANPATH");
319
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. */
324         strbuf_addstr(&new_path, system_path(GIT_MAN_PATH));
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
347                 warning(_("'%s': unknown man viewer."), name);
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);
354         const char *fallback = getenv("GIT_MAN_VIEWER");
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         }
361         if (fallback)
362                 exec_viewer(fallback, page);
363         exec_viewer("man", page);
364         die(_("no man viewer handled the request"));
365 }
366
367 static void show_info_page(const char *git_cmd)
368 {
369         const char *page = cmd_to_page(git_cmd);
370         setenv("INFOPATH", system_path(GIT_INFO_PATH), 1);
371         execlp("info", "info", "gitman", page, (char *)NULL);
372         die(_("no info viewer handled the request"));
373 }
374
375 static void get_html_page_path(struct strbuf *page_path, const char *page)
376 {
377         struct stat st;
378         if (!html_path)
379                 html_path = system_path(GIT_HTML_PATH);
380
381         /* Check that we have a git documentation directory. */
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         }
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
398 static void open_html(const char *path)
399 {
400         execl_git_cmd("web--browse", "-c", "help.browser", path, (char *)NULL);
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;
418         enum help_format parsed_help_format;
419         load_command_list("git-", &main_cmds, &other_cmds);
420
421         argc = parse_options(argc, argv, prefix, builtin_help_options,
422                         builtin_help_usage, 0);
423         parsed_help_format = help_format;
424
425         if (show_all) {
426                 git_config(git_help_config, NULL);
427                 printf(_("usage: %s%s"), _(git_usage_string), "\n\n");
428                 list_commands(colopts, &main_cmds, &other_cmds);
429                 printf("%s\n", _(git_more_info_string));
430                 return 0;
431         }
432
433         if (!argv[0]) {
434                 printf(_("usage: %s%s"), _(git_usage_string), "\n\n");
435                 list_common_cmds_help();
436                 printf("\n%s\n", _(git_more_info_string));
437                 return 0;
438         }
439
440         setup_git_directory_gently(&nongit);
441         git_config(git_help_config, NULL);
442
443         if (parsed_help_format != HELP_FORMAT_NONE)
444                 help_format = parsed_help_format;
445         if (help_format == HELP_FORMAT_NONE)
446                 help_format = parse_help_format(DEFAULT_HELP_FORMAT);
447
448         alias = alias_lookup(argv[0]);
449         if (alias && !is_git_command(argv[0])) {
450                 printf_ln(_("`git %s' is aliased to `%s'"), argv[0], alias);
451                 return 0;
452         }
453
454         switch (help_format) {
455         case HELP_FORMAT_NONE:
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 }