l10n: sv.po: Update Swedish translation (4674t0f0u)
[git.git] / builtin / help.c
blobe5590d7787c50af121db0ba7e6221e89c6785df1
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"
14 #ifndef DEFAULT_HELP_FORMAT
15 #define DEFAULT_HELP_FORMAT "man"
16 #endif
18 static struct man_viewer_list {
19 struct man_viewer_list *next;
20 char name[FLEX_ARRAY];
21 } *man_viewer_list;
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;
29 enum help_format {
30 HELP_FORMAT_NONE,
31 HELP_FORMAT_MAN,
32 HELP_FORMAT_INFO,
33 HELP_FORMAT_WEB
36 static const char *html_path;
38 static int show_all = 0;
39 static int show_guides = 0;
40 static int show_config;
41 static int verbose = 1;
42 static unsigned int colopts;
43 static enum help_format help_format = HELP_FORMAT_NONE;
44 static int exclude_guides;
45 static struct option builtin_help_options[] = {
46 OPT_BOOL('a', "all", &show_all, N_("print all available commands")),
47 OPT_HIDDEN_BOOL(0, "exclude-guides", &exclude_guides, N_("exclude guides")),
48 OPT_BOOL('g', "guides", &show_guides, N_("print list of useful guides")),
49 OPT_BOOL('c', "config", &show_config, N_("print all configuration variable names")),
50 OPT_SET_INT_F(0, "config-for-completion", &show_config, "", 2, PARSE_OPT_HIDDEN),
51 OPT_SET_INT('m', "man", &help_format, N_("show man page"), HELP_FORMAT_MAN),
52 OPT_SET_INT('w', "web", &help_format, N_("show manual in web browser"),
53 HELP_FORMAT_WEB),
54 OPT_SET_INT('i', "info", &help_format, N_("show info page"),
55 HELP_FORMAT_INFO),
56 OPT__VERBOSE(&verbose, N_("print command description")),
57 OPT_END(),
60 static const char * const builtin_help_usage[] = {
61 N_("git help [--all] [--guides] [--man | --web | --info] [<command>]"),
62 NULL
65 static enum help_format parse_help_format(const char *format)
67 if (!strcmp(format, "man"))
68 return HELP_FORMAT_MAN;
69 if (!strcmp(format, "info"))
70 return HELP_FORMAT_INFO;
71 if (!strcmp(format, "web") || !strcmp(format, "html"))
72 return HELP_FORMAT_WEB;
74 * Please update _git_config() in git-completion.bash when you
75 * add new help formats.
77 die(_("unrecognized help format '%s'"), format);
80 static const char *get_man_viewer_info(const char *name)
82 struct man_viewer_info_list *viewer;
84 for (viewer = man_viewer_info_list; viewer; viewer = viewer->next)
86 if (!strcasecmp(name, viewer->name))
87 return viewer->info;
89 return NULL;
92 static int check_emacsclient_version(void)
94 struct strbuf buffer = STRBUF_INIT;
95 struct child_process ec_process = CHILD_PROCESS_INIT;
96 const char *argv_ec[] = { "emacsclient", "--version", NULL };
97 int version;
99 /* emacsclient prints its version number on stderr */
100 ec_process.argv = argv_ec;
101 ec_process.err = -1;
102 ec_process.stdout_to_stderr = 1;
103 if (start_command(&ec_process))
104 return error(_("Failed to start emacsclient."));
106 strbuf_read(&buffer, ec_process.err, 20);
107 close(ec_process.err);
110 * Don't bother checking return value, because "emacsclient --version"
111 * seems to always exits with code 1.
113 finish_command(&ec_process);
115 if (!starts_with(buffer.buf, "emacsclient")) {
116 strbuf_release(&buffer);
117 return error(_("Failed to parse emacsclient version."));
120 strbuf_remove(&buffer, 0, strlen("emacsclient"));
121 version = atoi(buffer.buf);
123 if (version < 22) {
124 strbuf_release(&buffer);
125 return error(_("emacsclient version '%d' too old (< 22)."),
126 version);
129 strbuf_release(&buffer);
130 return 0;
133 static void exec_woman_emacs(const char *path, const char *page)
135 if (!check_emacsclient_version()) {
136 /* This works only with emacsclient version >= 22. */
137 struct strbuf man_page = STRBUF_INIT;
139 if (!path)
140 path = "emacsclient";
141 strbuf_addf(&man_page, "(woman \"%s\")", page);
142 execlp(path, "emacsclient", "-e", man_page.buf, (char *)NULL);
143 warning_errno(_("failed to exec '%s'"), path);
144 strbuf_release(&man_page);
148 static void exec_man_konqueror(const char *path, const char *page)
150 const char *display = getenv("DISPLAY");
151 if (display && *display) {
152 struct strbuf man_page = STRBUF_INIT;
153 const char *filename = "kfmclient";
155 /* It's simpler to launch konqueror using kfmclient. */
156 if (path) {
157 size_t len;
158 if (strip_suffix(path, "/konqueror", &len))
159 path = xstrfmt("%.*s/kfmclient", (int)len, path);
160 filename = basename((char *)path);
161 } else
162 path = "kfmclient";
163 strbuf_addf(&man_page, "man:%s(1)", page);
164 execlp(path, filename, "newTab", man_page.buf, (char *)NULL);
165 warning_errno(_("failed to exec '%s'"), path);
166 strbuf_release(&man_page);
170 static void exec_man_man(const char *path, const char *page)
172 if (!path)
173 path = "man";
174 execlp(path, "man", page, (char *)NULL);
175 warning_errno(_("failed to exec '%s'"), path);
178 static void exec_man_cmd(const char *cmd, const char *page)
180 struct strbuf shell_cmd = STRBUF_INIT;
181 strbuf_addf(&shell_cmd, "%s %s", cmd, page);
182 execl(SHELL_PATH, SHELL_PATH, "-c", shell_cmd.buf, (char *)NULL);
183 warning(_("failed to exec '%s'"), cmd);
184 strbuf_release(&shell_cmd);
187 static void add_man_viewer(const char *name)
189 struct man_viewer_list **p = &man_viewer_list;
191 while (*p)
192 p = &((*p)->next);
193 FLEX_ALLOC_STR(*p, name, name);
196 static int supported_man_viewer(const char *name, size_t len)
198 return (!strncasecmp("man", name, len) ||
199 !strncasecmp("woman", name, len) ||
200 !strncasecmp("konqueror", name, len));
203 static void do_add_man_viewer_info(const char *name,
204 size_t len,
205 const char *value)
207 struct man_viewer_info_list *new_man_viewer;
208 FLEX_ALLOC_MEM(new_man_viewer, name, name, len);
209 new_man_viewer->info = xstrdup(value);
210 new_man_viewer->next = man_viewer_info_list;
211 man_viewer_info_list = new_man_viewer;
214 static int add_man_viewer_path(const char *name,
215 size_t len,
216 const char *value)
218 if (supported_man_viewer(name, len))
219 do_add_man_viewer_info(name, len, value);
220 else
221 warning(_("'%s': path for unsupported man viewer.\n"
222 "Please consider using 'man.<tool>.cmd' instead."),
223 name);
225 return 0;
228 static int add_man_viewer_cmd(const char *name,
229 size_t len,
230 const char *value)
232 if (supported_man_viewer(name, len))
233 warning(_("'%s': cmd for supported man viewer.\n"
234 "Please consider using 'man.<tool>.path' instead."),
235 name);
236 else
237 do_add_man_viewer_info(name, len, value);
239 return 0;
242 static int add_man_viewer_info(const char *var, const char *value)
244 const char *name, *subkey;
245 int namelen;
247 if (parse_config_key(var, "man", &name, &namelen, &subkey) < 0 || !name)
248 return 0;
250 if (!strcmp(subkey, "path")) {
251 if (!value)
252 return config_error_nonbool(var);
253 return add_man_viewer_path(name, namelen, value);
255 if (!strcmp(subkey, "cmd")) {
256 if (!value)
257 return config_error_nonbool(var);
258 return add_man_viewer_cmd(name, namelen, value);
261 return 0;
264 static int git_help_config(const char *var, const char *value, void *cb)
266 if (starts_with(var, "column."))
267 return git_column_config(var, value, "help", &colopts);
268 if (!strcmp(var, "help.format")) {
269 if (!value)
270 return config_error_nonbool(var);
271 help_format = parse_help_format(value);
272 return 0;
274 if (!strcmp(var, "help.htmlpath")) {
275 if (!value)
276 return config_error_nonbool(var);
277 html_path = xstrdup(value);
278 return 0;
280 if (!strcmp(var, "man.viewer")) {
281 if (!value)
282 return config_error_nonbool(var);
283 add_man_viewer(value);
284 return 0;
286 if (starts_with(var, "man."))
287 return add_man_viewer_info(var, value);
289 return git_default_config(var, value, cb);
292 static struct cmdnames main_cmds, other_cmds;
294 static int is_git_command(const char *s)
296 if (is_builtin(s))
297 return 1;
299 load_command_list("git-", &main_cmds, &other_cmds);
300 return is_in_cmdlist(&main_cmds, s) ||
301 is_in_cmdlist(&other_cmds, s);
304 static const char *cmd_to_page(const char *git_cmd)
306 if (!git_cmd)
307 return "git";
308 else if (starts_with(git_cmd, "git"))
309 return git_cmd;
310 else if (is_git_command(git_cmd))
311 return xstrfmt("git-%s", git_cmd);
312 else
313 return xstrfmt("git%s", git_cmd);
316 static void setup_man_path(void)
318 struct strbuf new_path = STRBUF_INIT;
319 const char *old_path = getenv("MANPATH");
320 char *git_man_path = system_path(GIT_MAN_PATH);
322 /* We should always put ':' after our path. If there is no
323 * old_path, the ':' at the end will let 'man' to try
324 * system-wide paths after ours to find the manual page. If
325 * there is old_path, we need ':' as delimiter. */
326 strbuf_addstr(&new_path, git_man_path);
327 strbuf_addch(&new_path, ':');
328 if (old_path)
329 strbuf_addstr(&new_path, old_path);
331 free(git_man_path);
332 setenv("MANPATH", new_path.buf, 1);
334 strbuf_release(&new_path);
337 static void exec_viewer(const char *name, const char *page)
339 const char *info = get_man_viewer_info(name);
341 if (!strcasecmp(name, "man"))
342 exec_man_man(info, page);
343 else if (!strcasecmp(name, "woman"))
344 exec_woman_emacs(info, page);
345 else if (!strcasecmp(name, "konqueror"))
346 exec_man_konqueror(info, page);
347 else if (info)
348 exec_man_cmd(info, page);
349 else
350 warning(_("'%s': unknown man viewer."), name);
353 static void show_man_page(const char *git_cmd)
355 struct man_viewer_list *viewer;
356 const char *page = cmd_to_page(git_cmd);
357 const char *fallback = getenv("GIT_MAN_VIEWER");
359 setup_man_path();
360 for (viewer = man_viewer_list; viewer; viewer = viewer->next)
362 exec_viewer(viewer->name, page); /* will return when unable */
364 if (fallback)
365 exec_viewer(fallback, page);
366 exec_viewer("man", page);
367 die(_("no man viewer handled the request"));
370 static void show_info_page(const char *git_cmd)
372 const char *page = cmd_to_page(git_cmd);
373 setenv("INFOPATH", system_path(GIT_INFO_PATH), 1);
374 execlp("info", "info", "gitman", page, (char *)NULL);
375 die(_("no info viewer handled the request"));
378 static void get_html_page_path(struct strbuf *page_path, const char *page)
380 struct stat st;
381 char *to_free = NULL;
383 if (!html_path)
384 html_path = to_free = system_path(GIT_HTML_PATH);
386 /* Check that we have a git documentation directory. */
387 if (!strstr(html_path, "://")) {
388 if (stat(mkpath("%s/git.html", html_path), &st)
389 || !S_ISREG(st.st_mode))
390 die("'%s': not a documentation directory.", html_path);
393 strbuf_init(page_path, 0);
394 strbuf_addf(page_path, "%s/%s.html", html_path, page);
395 free(to_free);
398 static void open_html(const char *path)
400 execl_git_cmd("web--browse", "-c", "help.browser", path, (char *)NULL);
403 static void show_html_page(const char *git_cmd)
405 const char *page = cmd_to_page(git_cmd);
406 struct strbuf page_path; /* it leaks but we exec bellow */
408 get_html_page_path(&page_path, page);
410 open_html(page_path.buf);
413 static const char *check_git_cmd(const char* cmd)
415 char *alias;
417 if (is_git_command(cmd))
418 return cmd;
420 alias = alias_lookup(cmd);
421 if (alias) {
422 const char **argv;
423 int count;
426 * handle_builtin() in git.c rewrites "git cmd --help"
427 * to "git help --exclude-guides cmd", so we can use
428 * exclude_guides to distinguish "git cmd --help" from
429 * "git help cmd". In the latter case, or if cmd is an
430 * alias for a shell command, just print the alias
431 * definition.
433 if (!exclude_guides || alias[0] == '!') {
434 printf_ln(_("'%s' is aliased to '%s'"), cmd, alias);
435 free(alias);
436 exit(0);
439 * Otherwise, we pretend that the command was "git
440 * word0 --help". We use split_cmdline() to get the
441 * first word of the alias, to ensure that we use the
442 * same rules as when the alias is actually
443 * used. split_cmdline() modifies alias in-place.
445 fprintf_ln(stderr, _("'%s' is aliased to '%s'"), cmd, alias);
446 count = split_cmdline(alias, &argv);
447 if (count < 0)
448 die(_("bad alias.%s string: %s"), cmd,
449 split_cmdline_strerror(count));
450 free(argv);
451 UNLEAK(alias);
452 return alias;
455 if (exclude_guides)
456 return help_unknown_cmd(cmd);
458 return cmd;
461 int cmd_help(int argc, const char **argv, const char *prefix)
463 int nongit;
464 enum help_format parsed_help_format;
466 argc = parse_options(argc, argv, prefix, builtin_help_options,
467 builtin_help_usage, 0);
468 parsed_help_format = help_format;
470 if (show_all) {
471 git_config(git_help_config, NULL);
472 if (verbose) {
473 setup_pager();
474 list_all_cmds_help();
475 return 0;
477 printf(_("usage: %s%s"), _(git_usage_string), "\n\n");
478 load_command_list("git-", &main_cmds, &other_cmds);
479 list_commands(colopts, &main_cmds, &other_cmds);
482 if (show_config) {
483 int for_human = show_config == 1;
485 if (!for_human) {
486 list_config_help(for_human);
487 return 0;
489 setup_pager();
490 list_config_help(for_human);
491 printf("\n%s\n", _("'git help config' for more information"));
492 return 0;
495 if (show_guides)
496 list_common_guides_help();
498 if (show_all || show_guides) {
499 printf("%s\n", _(git_more_info_string));
501 * We're done. Ignore any remaining args
503 return 0;
506 if (!argv[0]) {
507 printf(_("usage: %s%s"), _(git_usage_string), "\n\n");
508 list_common_cmds_help();
509 printf("\n%s\n", _(git_more_info_string));
510 return 0;
513 setup_git_directory_gently(&nongit);
514 git_config(git_help_config, NULL);
516 if (parsed_help_format != HELP_FORMAT_NONE)
517 help_format = parsed_help_format;
518 if (help_format == HELP_FORMAT_NONE)
519 help_format = parse_help_format(DEFAULT_HELP_FORMAT);
521 argv[0] = check_git_cmd(argv[0]);
523 switch (help_format) {
524 case HELP_FORMAT_NONE:
525 case HELP_FORMAT_MAN:
526 show_man_page(argv[0]);
527 break;
528 case HELP_FORMAT_INFO:
529 show_info_page(argv[0]);
530 break;
531 case HELP_FORMAT_WEB:
532 show_html_page(argv[0]);
533 break;
536 return 0;