git-remote-mediawiki: properly deal with invalid remote revisions
[git/jnareb-git.git] / builtin / help.c
blobefea4f55e173a5ff2d7e1748316d6a94266e915a
1 /*
2 * builtin-help.c
4 * Builtin help command
5 */
6 #include "cache.h"
7 #include "builtin.h"
8 #include "exec_cmd.h"
9 #include "common-cmds.h"
10 #include "parse-options.h"
11 #include "run-command.h"
12 #include "column.h"
13 #include "help.h"
15 #ifndef DEFAULT_HELP_FORMAT
16 #define DEFAULT_HELP_FORMAT "man"
17 #endif
19 static struct man_viewer_list {
20 struct man_viewer_list *next;
21 char name[FLEX_ARRAY];
22 } *man_viewer_list;
24 static struct man_viewer_info_list {
25 struct man_viewer_info_list *next;
26 const char *info;
27 char name[FLEX_ARRAY];
28 } *man_viewer_info_list;
30 enum help_format {
31 HELP_FORMAT_NONE,
32 HELP_FORMAT_MAN,
33 HELP_FORMAT_INFO,
34 HELP_FORMAT_WEB
37 static const char *html_path;
39 static int show_all = 0;
40 static unsigned int colopts;
41 static enum help_format help_format = HELP_FORMAT_NONE;
42 static struct option builtin_help_options[] = {
43 OPT_BOOLEAN('a', "all", &show_all, "print all available commands"),
44 OPT_SET_INT('m', "man", &help_format, "show man page", HELP_FORMAT_MAN),
45 OPT_SET_INT('w', "web", &help_format, "show manual in web browser",
46 HELP_FORMAT_WEB),
47 OPT_SET_INT('i', "info", &help_format, "show info page",
48 HELP_FORMAT_INFO),
49 OPT_END(),
52 static const char * const builtin_help_usage[] = {
53 "git help [--all] [--man|--web|--info] [command]",
54 NULL
57 static enum help_format parse_help_format(const char *format)
59 if (!strcmp(format, "man"))
60 return HELP_FORMAT_MAN;
61 if (!strcmp(format, "info"))
62 return HELP_FORMAT_INFO;
63 if (!strcmp(format, "web") || !strcmp(format, "html"))
64 return HELP_FORMAT_WEB;
65 die(_("unrecognized help format '%s'"), format);
68 static const char *get_man_viewer_info(const char *name)
70 struct man_viewer_info_list *viewer;
72 for (viewer = man_viewer_info_list; viewer; viewer = viewer->next)
74 if (!strcasecmp(name, viewer->name))
75 return viewer->info;
77 return NULL;
80 static int check_emacsclient_version(void)
82 struct strbuf buffer = STRBUF_INIT;
83 struct child_process ec_process;
84 const char *argv_ec[] = { "emacsclient", "--version", NULL };
85 int version;
87 /* emacsclient prints its version number on stderr */
88 memset(&ec_process, 0, sizeof(ec_process));
89 ec_process.argv = argv_ec;
90 ec_process.err = -1;
91 ec_process.stdout_to_stderr = 1;
92 if (start_command(&ec_process))
93 return error(_("Failed to start emacsclient."));
95 strbuf_read(&buffer, ec_process.err, 20);
96 close(ec_process.err);
99 * Don't bother checking return value, because "emacsclient --version"
100 * seems to always exits with code 1.
102 finish_command(&ec_process);
104 if (prefixcmp(buffer.buf, "emacsclient")) {
105 strbuf_release(&buffer);
106 return error(_("Failed to parse emacsclient version."));
109 strbuf_remove(&buffer, 0, strlen("emacsclient"));
110 version = atoi(buffer.buf);
112 if (version < 22) {
113 strbuf_release(&buffer);
114 return error(_("emacsclient version '%d' too old (< 22)."),
115 version);
118 strbuf_release(&buffer);
119 return 0;
122 static void exec_woman_emacs(const char *path, const char *page)
124 if (!check_emacsclient_version()) {
125 /* This works only with emacsclient version >= 22. */
126 struct strbuf man_page = STRBUF_INIT;
128 if (!path)
129 path = "emacsclient";
130 strbuf_addf(&man_page, "(woman \"%s\")", page);
131 execlp(path, "emacsclient", "-e", man_page.buf, (char *)NULL);
132 warning(_("failed to exec '%s': %s"), path, strerror(errno));
136 static void exec_man_konqueror(const char *path, const char *page)
138 const char *display = getenv("DISPLAY");
139 if (display && *display) {
140 struct strbuf man_page = STRBUF_INIT;
141 const char *filename = "kfmclient";
143 /* It's simpler to launch konqueror using kfmclient. */
144 if (path) {
145 const char *file = strrchr(path, '/');
146 if (file && !strcmp(file + 1, "konqueror")) {
147 char *new = xstrdup(path);
148 char *dest = strrchr(new, '/');
150 /* strlen("konqueror") == strlen("kfmclient") */
151 strcpy(dest + 1, "kfmclient");
152 path = new;
154 if (file)
155 filename = file;
156 } else
157 path = "kfmclient";
158 strbuf_addf(&man_page, "man:%s(1)", page);
159 execlp(path, filename, "newTab", man_page.buf, (char *)NULL);
160 warning(_("failed to exec '%s': %s"), path, strerror(errno));
164 static void exec_man_man(const char *path, const char *page)
166 if (!path)
167 path = "man";
168 execlp(path, "man", page, (char *)NULL);
169 warning(_("failed to exec '%s': %s"), path, strerror(errno));
172 static void exec_man_cmd(const char *cmd, const char *page)
174 struct strbuf shell_cmd = STRBUF_INIT;
175 strbuf_addf(&shell_cmd, "%s %s", cmd, page);
176 execl("/bin/sh", "sh", "-c", shell_cmd.buf, (char *)NULL);
177 warning(_("failed to exec '%s': %s"), cmd, strerror(errno));
180 static void add_man_viewer(const char *name)
182 struct man_viewer_list **p = &man_viewer_list;
183 size_t len = strlen(name);
185 while (*p)
186 p = &((*p)->next);
187 *p = xcalloc(1, (sizeof(**p) + len + 1));
188 strncpy((*p)->name, name, len);
191 static int supported_man_viewer(const char *name, size_t len)
193 return (!strncasecmp("man", name, len) ||
194 !strncasecmp("woman", name, len) ||
195 !strncasecmp("konqueror", name, len));
198 static void do_add_man_viewer_info(const char *name,
199 size_t len,
200 const char *value)
202 struct man_viewer_info_list *new = xcalloc(1, sizeof(*new) + len + 1);
204 strncpy(new->name, name, len);
205 new->info = xstrdup(value);
206 new->next = man_viewer_info_list;
207 man_viewer_info_list = new;
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 = var + 4;
241 const char *subkey = strrchr(name, '.');
243 if (!subkey)
244 return 0;
246 if (!strcmp(subkey, ".path")) {
247 if (!value)
248 return config_error_nonbool(var);
249 return add_man_viewer_path(name, subkey - name, value);
251 if (!strcmp(subkey, ".cmd")) {
252 if (!value)
253 return config_error_nonbool(var);
254 return add_man_viewer_cmd(name, subkey - name, value);
257 return 0;
260 static int git_help_config(const char *var, const char *value, void *cb)
262 if (!prefixcmp(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 (!prefixcmp(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 void list_common_cmds_help(void)
292 int i, longest = 0;
294 for (i = 0; i < ARRAY_SIZE(common_cmds); i++) {
295 if (longest < strlen(common_cmds[i].name))
296 longest = strlen(common_cmds[i].name);
299 puts(_("The most commonly used git commands are:"));
300 for (i = 0; i < ARRAY_SIZE(common_cmds); i++) {
301 printf(" %s ", common_cmds[i].name);
302 mput_char(' ', longest - strlen(common_cmds[i].name));
303 puts(_(common_cmds[i].help));
307 static int is_git_command(const char *s)
309 return is_in_cmdlist(&main_cmds, s) ||
310 is_in_cmdlist(&other_cmds, s);
313 static const char *prepend(const char *prefix, const char *cmd)
315 size_t pre_len = strlen(prefix);
316 size_t cmd_len = strlen(cmd);
317 char *p = xmalloc(pre_len + cmd_len + 1);
318 memcpy(p, prefix, pre_len);
319 strcpy(p + pre_len, cmd);
320 return p;
323 static const char *cmd_to_page(const char *git_cmd)
325 if (!git_cmd)
326 return "git";
327 else if (!prefixcmp(git_cmd, "git"))
328 return git_cmd;
329 else if (is_git_command(git_cmd))
330 return prepend("git-", git_cmd);
331 else
332 return prepend("git", git_cmd);
335 static void setup_man_path(void)
337 struct strbuf new_path = STRBUF_INIT;
338 const char *old_path = getenv("MANPATH");
340 /* We should always put ':' after our path. If there is no
341 * old_path, the ':' at the end will let 'man' to try
342 * system-wide paths after ours to find the manual page. If
343 * there is old_path, we need ':' as delimiter. */
344 strbuf_addstr(&new_path, system_path(GIT_MAN_PATH));
345 strbuf_addch(&new_path, ':');
346 if (old_path)
347 strbuf_addstr(&new_path, old_path);
349 setenv("MANPATH", new_path.buf, 1);
351 strbuf_release(&new_path);
354 static void exec_viewer(const char *name, const char *page)
356 const char *info = get_man_viewer_info(name);
358 if (!strcasecmp(name, "man"))
359 exec_man_man(info, page);
360 else if (!strcasecmp(name, "woman"))
361 exec_woman_emacs(info, page);
362 else if (!strcasecmp(name, "konqueror"))
363 exec_man_konqueror(info, page);
364 else if (info)
365 exec_man_cmd(info, page);
366 else
367 warning(_("'%s': unknown man viewer."), name);
370 static void show_man_page(const char *git_cmd)
372 struct man_viewer_list *viewer;
373 const char *page = cmd_to_page(git_cmd);
374 const char *fallback = getenv("GIT_MAN_VIEWER");
376 setup_man_path();
377 for (viewer = man_viewer_list; viewer; viewer = viewer->next)
379 exec_viewer(viewer->name, page); /* will return when unable */
381 if (fallback)
382 exec_viewer(fallback, page);
383 exec_viewer("man", page);
384 die(_("no man viewer handled the request"));
387 static void show_info_page(const char *git_cmd)
389 const char *page = cmd_to_page(git_cmd);
390 setenv("INFOPATH", system_path(GIT_INFO_PATH), 1);
391 execlp("info", "info", "gitman", page, (char *)NULL);
392 die(_("no info viewer handled the request"));
395 static void get_html_page_path(struct strbuf *page_path, const char *page)
397 struct stat st;
398 if (!html_path)
399 html_path = system_path(GIT_HTML_PATH);
401 /* Check that we have a git documentation directory. */
402 if (!strstr(html_path, "://")) {
403 if (stat(mkpath("%s/git.html", html_path), &st)
404 || !S_ISREG(st.st_mode))
405 die("'%s': not a documentation directory.", html_path);
408 strbuf_init(page_path, 0);
409 strbuf_addf(page_path, "%s/%s.html", html_path, page);
413 * If open_html is not defined in a platform-specific way (see for
414 * example compat/mingw.h), we use the script web--browse to display
415 * HTML.
417 #ifndef open_html
418 static void open_html(const char *path)
420 execl_git_cmd("web--browse", "-c", "help.browser", path, (char *)NULL);
422 #endif
424 static void show_html_page(const char *git_cmd)
426 const char *page = cmd_to_page(git_cmd);
427 struct strbuf page_path; /* it leaks but we exec bellow */
429 get_html_page_path(&page_path, page);
431 open_html(page_path.buf);
434 int cmd_help(int argc, const char **argv, const char *prefix)
436 int nongit;
437 const char *alias;
438 enum help_format parsed_help_format;
439 load_command_list("git-", &main_cmds, &other_cmds);
441 argc = parse_options(argc, argv, prefix, builtin_help_options,
442 builtin_help_usage, 0);
443 parsed_help_format = help_format;
445 if (show_all) {
446 git_config(git_help_config, NULL);
447 printf(_("usage: %s%s"), _(git_usage_string), "\n\n");
448 list_commands(colopts, &main_cmds, &other_cmds);
449 printf("%s\n", _(git_more_info_string));
450 return 0;
453 if (!argv[0]) {
454 printf(_("usage: %s%s"), _(git_usage_string), "\n\n");
455 list_common_cmds_help();
456 printf("\n%s\n", _(git_more_info_string));
457 return 0;
460 setup_git_directory_gently(&nongit);
461 git_config(git_help_config, NULL);
463 if (parsed_help_format != HELP_FORMAT_NONE)
464 help_format = parsed_help_format;
465 if (help_format == HELP_FORMAT_NONE)
466 help_format = parse_help_format(DEFAULT_HELP_FORMAT);
468 alias = alias_lookup(argv[0]);
469 if (alias && !is_git_command(argv[0])) {
470 printf_ln(_("`git %s' is aliased to `%s'"), argv[0], alias);
471 return 0;
474 switch (help_format) {
475 case HELP_FORMAT_NONE:
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;
487 return 0;