http-backend: respect CONTENT_LENGTH for receive-pack
[git] / builtin / help.c
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"
13
14 #ifndef DEFAULT_HELP_FORMAT
15 #define DEFAULT_HELP_FORMAT "man"
16 #endif
17
18 static struct man_viewer_list {
19         struct man_viewer_list *next;
20         char name[FLEX_ARRAY];
21 } *man_viewer_list;
22
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;
28
29 enum help_format {
30         HELP_FORMAT_NONE,
31         HELP_FORMAT_MAN,
32         HELP_FORMAT_INFO,
33         HELP_FORMAT_WEB
34 };
35
36 static const char *html_path;
37
38 static int show_all = 0;
39 static int show_guides = 0;
40 static int verbose;
41 static unsigned int colopts;
42 static enum help_format help_format = HELP_FORMAT_NONE;
43 static int exclude_guides;
44 static struct option builtin_help_options[] = {
45         OPT_BOOL('a', "all", &show_all, N_("print all available commands")),
46         OPT_HIDDEN_BOOL(0, "exclude-guides", &exclude_guides, N_("exclude guides")),
47         OPT_BOOL('g', "guides", &show_guides, N_("print list of useful guides")),
48         OPT_SET_INT('m', "man", &help_format, N_("show man page"), HELP_FORMAT_MAN),
49         OPT_SET_INT('w', "web", &help_format, N_("show manual in web browser"),
50                         HELP_FORMAT_WEB),
51         OPT_SET_INT('i', "info", &help_format, N_("show info page"),
52                         HELP_FORMAT_INFO),
53         OPT__VERBOSE(&verbose, N_("print command description")),
54         OPT_END(),
55 };
56
57 static const char * const builtin_help_usage[] = {
58         N_("git help [--all] [--guides] [--man | --web | --info] [<command>]"),
59         NULL
60 };
61
62 static enum help_format parse_help_format(const char *format)
63 {
64         if (!strcmp(format, "man"))
65                 return HELP_FORMAT_MAN;
66         if (!strcmp(format, "info"))
67                 return HELP_FORMAT_INFO;
68         if (!strcmp(format, "web") || !strcmp(format, "html"))
69                 return HELP_FORMAT_WEB;
70         die(_("unrecognized help format '%s'"), format);
71 }
72
73 static const char *get_man_viewer_info(const char *name)
74 {
75         struct man_viewer_info_list *viewer;
76
77         for (viewer = man_viewer_info_list; viewer; viewer = viewer->next)
78         {
79                 if (!strcasecmp(name, viewer->name))
80                         return viewer->info;
81         }
82         return NULL;
83 }
84
85 static int check_emacsclient_version(void)
86 {
87         struct strbuf buffer = STRBUF_INIT;
88         struct child_process ec_process = CHILD_PROCESS_INIT;
89         const char *argv_ec[] = { "emacsclient", "--version", NULL };
90         int version;
91
92         /* emacsclient prints its version number on stderr */
93         ec_process.argv = argv_ec;
94         ec_process.err = -1;
95         ec_process.stdout_to_stderr = 1;
96         if (start_command(&ec_process))
97                 return error(_("Failed to start emacsclient."));
98
99         strbuf_read(&buffer, ec_process.err, 20);
100         close(ec_process.err);
101
102         /*
103          * Don't bother checking return value, because "emacsclient --version"
104          * seems to always exits with code 1.
105          */
106         finish_command(&ec_process);
107
108         if (!starts_with(buffer.buf, "emacsclient")) {
109                 strbuf_release(&buffer);
110                 return error(_("Failed to parse emacsclient version."));
111         }
112
113         strbuf_remove(&buffer, 0, strlen("emacsclient"));
114         version = atoi(buffer.buf);
115
116         if (version < 22) {
117                 strbuf_release(&buffer);
118                 return error(_("emacsclient version '%d' too old (< 22)."),
119                         version);
120         }
121
122         strbuf_release(&buffer);
123         return 0;
124 }
125
126 static void exec_woman_emacs(const char *path, const char *page)
127 {
128         if (!check_emacsclient_version()) {
129                 /* This works only with emacsclient version >= 22. */
130                 struct strbuf man_page = STRBUF_INIT;
131
132                 if (!path)
133                         path = "emacsclient";
134                 strbuf_addf(&man_page, "(woman \"%s\")", page);
135                 execlp(path, "emacsclient", "-e", man_page.buf, (char *)NULL);
136                 warning_errno(_("failed to exec '%s'"), path);
137                 strbuf_release(&man_page);
138         }
139 }
140
141 static void exec_man_konqueror(const char *path, const char *page)
142 {
143         const char *display = getenv("DISPLAY");
144         if (display && *display) {
145                 struct strbuf man_page = STRBUF_INIT;
146                 const char *filename = "kfmclient";
147
148                 /* It's simpler to launch konqueror using kfmclient. */
149                 if (path) {
150                         size_t len;
151                         if (strip_suffix(path, "/konqueror", &len))
152                                 path = xstrfmt("%.*s/kfmclient", (int)len, path);
153                         filename = basename((char *)path);
154                 } else
155                         path = "kfmclient";
156                 strbuf_addf(&man_page, "man:%s(1)", page);
157                 execlp(path, filename, "newTab", man_page.buf, (char *)NULL);
158                 warning_errno(_("failed to exec '%s'"), path);
159                 strbuf_release(&man_page);
160         }
161 }
162
163 static void exec_man_man(const char *path, const char *page)
164 {
165         if (!path)
166                 path = "man";
167         execlp(path, "man", page, (char *)NULL);
168         warning_errno(_("failed to exec '%s'"), path);
169 }
170
171 static void exec_man_cmd(const char *cmd, const char *page)
172 {
173         struct strbuf shell_cmd = STRBUF_INIT;
174         strbuf_addf(&shell_cmd, "%s %s", cmd, page);
175         execl(SHELL_PATH, SHELL_PATH, "-c", shell_cmd.buf, (char *)NULL);
176         warning(_("failed to exec '%s'"), cmd);
177         strbuf_release(&shell_cmd);
178 }
179
180 static void add_man_viewer(const char *name)
181 {
182         struct man_viewer_list **p = &man_viewer_list;
183
184         while (*p)
185                 p = &((*p)->next);
186         FLEX_ALLOC_STR(*p, name, name);
187 }
188
189 static int supported_man_viewer(const char *name, size_t len)
190 {
191         return (!strncasecmp("man", name, len) ||
192                 !strncasecmp("woman", name, len) ||
193                 !strncasecmp("konqueror", name, len));
194 }
195
196 static void do_add_man_viewer_info(const char *name,
197                                    size_t len,
198                                    const char *value)
199 {
200         struct man_viewer_info_list *new_man_viewer;
201         FLEX_ALLOC_MEM(new_man_viewer, name, name, len);
202         new_man_viewer->info = xstrdup(value);
203         new_man_viewer->next = man_viewer_info_list;
204         man_viewer_info_list = new_man_viewer;
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, *subkey;
238         int namelen;
239
240         if (parse_config_key(var, "man", &name, &namelen, &subkey) < 0 || !name)
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, namelen, value);
247         }
248         if (!strcmp(subkey, "cmd")) {
249                 if (!value)
250                         return config_error_nonbool(var);
251                 return add_man_viewer_cmd(name, namelen, 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 (starts_with(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 (starts_with(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         if (is_builtin(s))
290                 return 1;
291
292         load_command_list("git-", &main_cmds, &other_cmds);
293         return is_in_cmdlist(&main_cmds, s) ||
294                 is_in_cmdlist(&other_cmds, s);
295 }
296
297 static const char *cmd_to_page(const char *git_cmd)
298 {
299         if (!git_cmd)
300                 return "git";
301         else if (starts_with(git_cmd, "git"))
302                 return git_cmd;
303         else if (is_git_command(git_cmd))
304                 return xstrfmt("git-%s", git_cmd);
305         else
306                 return xstrfmt("git%s", git_cmd);
307 }
308
309 static void setup_man_path(void)
310 {
311         struct strbuf new_path = STRBUF_INIT;
312         const char *old_path = getenv("MANPATH");
313         char *git_man_path = system_path(GIT_MAN_PATH);
314
315         /* We should always put ':' after our path. If there is no
316          * old_path, the ':' at the end will let 'man' to try
317          * system-wide paths after ours to find the manual page. If
318          * there is old_path, we need ':' as delimiter. */
319         strbuf_addstr(&new_path, git_man_path);
320         strbuf_addch(&new_path, ':');
321         if (old_path)
322                 strbuf_addstr(&new_path, old_path);
323
324         free(git_man_path);
325         setenv("MANPATH", new_path.buf, 1);
326
327         strbuf_release(&new_path);
328 }
329
330 static void exec_viewer(const char *name, const char *page)
331 {
332         const char *info = get_man_viewer_info(name);
333
334         if (!strcasecmp(name, "man"))
335                 exec_man_man(info, page);
336         else if (!strcasecmp(name, "woman"))
337                 exec_woman_emacs(info, page);
338         else if (!strcasecmp(name, "konqueror"))
339                 exec_man_konqueror(info, page);
340         else if (info)
341                 exec_man_cmd(info, page);
342         else
343                 warning(_("'%s': unknown man viewer."), name);
344 }
345
346 static void show_man_page(const char *git_cmd)
347 {
348         struct man_viewer_list *viewer;
349         const char *page = cmd_to_page(git_cmd);
350         const char *fallback = getenv("GIT_MAN_VIEWER");
351
352         setup_man_path();
353         for (viewer = man_viewer_list; viewer; viewer = viewer->next)
354         {
355                 exec_viewer(viewer->name, page); /* will return when unable */
356         }
357         if (fallback)
358                 exec_viewer(fallback, page);
359         exec_viewer("man", page);
360         die(_("no man viewer handled the request"));
361 }
362
363 static void show_info_page(const char *git_cmd)
364 {
365         const char *page = cmd_to_page(git_cmd);
366         setenv("INFOPATH", system_path(GIT_INFO_PATH), 1);
367         execlp("info", "info", "gitman", page, (char *)NULL);
368         die(_("no info viewer handled the request"));
369 }
370
371 static void get_html_page_path(struct strbuf *page_path, const char *page)
372 {
373         struct stat st;
374         char *to_free = NULL;
375
376         if (!html_path)
377                 html_path = to_free = system_path(GIT_HTML_PATH);
378
379         /* Check that we have a git documentation directory. */
380         if (!strstr(html_path, "://")) {
381                 if (stat(mkpath("%s/git.html", html_path), &st)
382                     || !S_ISREG(st.st_mode))
383                         die("'%s': not a documentation directory.", html_path);
384         }
385
386         strbuf_init(page_path, 0);
387         strbuf_addf(page_path, "%s/%s.html", html_path, page);
388         free(to_free);
389 }
390
391 static void open_html(const char *path)
392 {
393         execl_git_cmd("web--browse", "-c", "help.browser", path, (char *)NULL);
394 }
395
396 static void show_html_page(const char *git_cmd)
397 {
398         const char *page = cmd_to_page(git_cmd);
399         struct strbuf page_path; /* it leaks but we exec bellow */
400
401         get_html_page_path(&page_path, page);
402
403         open_html(page_path.buf);
404 }
405
406 static const char *check_git_cmd(const char* cmd)
407 {
408         char *alias;
409
410         if (is_git_command(cmd))
411                 return cmd;
412
413         alias = alias_lookup(cmd);
414         if (alias) {
415                 printf_ln(_("'%s' is aliased to '%s'"), cmd, alias);
416                 free(alias);
417                 exit(0);
418         }
419
420         if (exclude_guides)
421                 return help_unknown_cmd(cmd);
422
423         return cmd;
424 }
425
426 int cmd_help(int argc, const char **argv, const char *prefix)
427 {
428         int nongit;
429         enum help_format parsed_help_format;
430
431         argc = parse_options(argc, argv, prefix, builtin_help_options,
432                         builtin_help_usage, 0);
433         parsed_help_format = help_format;
434
435         if (show_all) {
436                 git_config(git_help_config, NULL);
437                 if (verbose) {
438                         setup_pager();
439                         list_all_cmds_help();
440                         return 0;
441                 }
442                 printf(_("usage: %s%s"), _(git_usage_string), "\n\n");
443                 load_command_list("git-", &main_cmds, &other_cmds);
444                 list_commands(colopts, &main_cmds, &other_cmds);
445         }
446
447         if (show_guides)
448                 list_common_guides_help();
449
450         if (show_all || show_guides) {
451                 printf("%s\n", _(git_more_info_string));
452                 /*
453                 * We're done. Ignore any remaining args
454                 */
455                 return 0;
456         }
457
458         if (!argv[0]) {
459                 printf(_("usage: %s%s"), _(git_usage_string), "\n\n");
460                 list_common_cmds_help();
461                 printf("\n%s\n", _(git_more_info_string));
462                 return 0;
463         }
464
465         setup_git_directory_gently(&nongit);
466         git_config(git_help_config, NULL);
467
468         if (parsed_help_format != HELP_FORMAT_NONE)
469                 help_format = parsed_help_format;
470         if (help_format == HELP_FORMAT_NONE)
471                 help_format = parse_help_format(DEFAULT_HELP_FORMAT);
472
473         argv[0] = check_git_cmd(argv[0]);
474
475         switch (help_format) {
476         case HELP_FORMAT_NONE:
477         case HELP_FORMAT_MAN:
478                 show_man_page(argv[0]);
479                 break;
480         case HELP_FORMAT_INFO:
481                 show_info_page(argv[0]);
482                 break;
483         case HELP_FORMAT_WEB:
484                 show_html_page(argv[0]);
485                 break;
486         }
487
488         return 0;
489 }