7 #include "parse-options.h"
8 #include "run-command.h"
12 #ifndef DEFAULT_HELP_FORMAT
13 #define DEFAULT_HELP_FORMAT "man"
16 static struct man_viewer_list
{
17 struct man_viewer_list
*next
;
18 char name
[FLEX_ARRAY
];
21 static struct man_viewer_info_list
{
22 struct man_viewer_info_list
*next
;
24 char name
[FLEX_ARRAY
];
25 } *man_viewer_info_list
;
34 static const char *html_path
;
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"),
44 OPT_SET_INT('i', "info", &help_format
, N_("show info page"),
49 static const char * const builtin_help_usage
[] = {
50 N_("git help [--all] [--man|--web|--info] [command]"),
54 static enum help_format
parse_help_format(const char *format
)
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
);
65 static const char *get_man_viewer_info(const char *name
)
67 struct man_viewer_info_list
*viewer
;
69 for (viewer
= man_viewer_info_list
; viewer
; viewer
= viewer
->next
)
71 if (!strcasecmp(name
, viewer
->name
))
77 static int check_emacsclient_version(void)
79 struct strbuf buffer
= STRBUF_INIT
;
80 struct child_process ec_process
;
81 const char *argv_ec
[] = { "emacsclient", "--version", NULL
};
84 /* emacsclient prints its version number on stderr */
85 memset(&ec_process
, 0, sizeof(ec_process
));
86 ec_process
.argv
= argv_ec
;
88 ec_process
.stdout_to_stderr
= 1;
89 if (start_command(&ec_process
))
90 return error(_("Failed to start emacsclient."));
92 strbuf_read(&buffer
, ec_process
.err
, 20);
93 close(ec_process
.err
);
96 * Don't bother checking return value, because "emacsclient --version"
97 * seems to always exits with code 1.
99 finish_command(&ec_process
);
101 if (prefixcmp(buffer
.buf
, "emacsclient")) {
102 strbuf_release(&buffer
);
103 return error(_("Failed to parse emacsclient version."));
106 strbuf_remove(&buffer
, 0, strlen("emacsclient"));
107 version
= atoi(buffer
.buf
);
110 strbuf_release(&buffer
);
111 return error(_("emacsclient version '%d' too old (< 22)."),
115 strbuf_release(&buffer
);
119 static void exec_woman_emacs(const char *path
, const char *page
)
121 if (!check_emacsclient_version()) {
122 /* This works only with emacsclient version >= 22. */
123 struct strbuf man_page
= STRBUF_INIT
;
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
));
133 static void exec_man_konqueror(const char *path
, const char *page
)
135 const char *display
= getenv("DISPLAY");
136 if (display
&& *display
) {
137 struct strbuf man_page
= STRBUF_INIT
;
138 const char *filename
= "kfmclient";
140 /* It's simpler to launch konqueror using kfmclient. */
142 const char *file
= strrchr(path
, '/');
143 if (file
&& !strcmp(file
+ 1, "konqueror")) {
144 char *new = xstrdup(path
);
145 char *dest
= strrchr(new, '/');
147 /* strlen("konqueror") == strlen("kfmclient") */
148 strcpy(dest
+ 1, "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
));
161 static void exec_man_man(const char *path
, const char *page
)
165 execlp(path
, "man", page
, (char *)NULL
);
166 warning(_("failed to exec '%s': %s"), path
, strerror(errno
));
169 static void exec_man_cmd(const char *cmd
, const char *page
)
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
));
177 static void add_man_viewer(const char *name
)
179 struct man_viewer_list
**p
= &man_viewer_list
;
180 size_t len
= strlen(name
);
184 *p
= xcalloc(1, (sizeof(**p
) + len
+ 1));
185 strncpy((*p
)->name
, name
, len
);
188 static int supported_man_viewer(const char *name
, size_t len
)
190 return (!strncasecmp("man", name
, len
) ||
191 !strncasecmp("woman", name
, len
) ||
192 !strncasecmp("konqueror", name
, len
));
195 static void do_add_man_viewer_info(const char *name
,
199 struct man_viewer_info_list
*new = xcalloc(1, sizeof(*new) + len
+ 1);
201 strncpy(new->name
, name
, len
);
202 new->info
= xstrdup(value
);
203 new->next
= man_viewer_info_list
;
204 man_viewer_info_list
= new;
207 static int add_man_viewer_path(const char *name
,
211 if (supported_man_viewer(name
, len
))
212 do_add_man_viewer_info(name
, len
, value
);
214 warning(_("'%s': path for unsupported man viewer.\n"
215 "Please consider using 'man.<tool>.cmd' instead."),
221 static int add_man_viewer_cmd(const char *name
,
225 if (supported_man_viewer(name
, len
))
226 warning(_("'%s': cmd for supported man viewer.\n"
227 "Please consider using 'man.<tool>.path' instead."),
230 do_add_man_viewer_info(name
, len
, value
);
235 static int add_man_viewer_info(const char *var
, const char *value
)
237 const char *name
= var
+ 4;
238 const char *subkey
= strrchr(name
, '.');
243 if (!strcmp(subkey
, ".path")) {
245 return config_error_nonbool(var
);
246 return add_man_viewer_path(name
, subkey
- name
, value
);
248 if (!strcmp(subkey
, ".cmd")) {
250 return config_error_nonbool(var
);
251 return add_man_viewer_cmd(name
, subkey
- name
, value
);
257 static int git_help_config(const char *var
, const char *value
, void *cb
)
259 if (!prefixcmp(var
, "column."))
260 return git_column_config(var
, value
, "help", &colopts
);
261 if (!strcmp(var
, "help.format")) {
263 return config_error_nonbool(var
);
264 help_format
= parse_help_format(value
);
267 if (!strcmp(var
, "help.htmlpath")) {
269 return config_error_nonbool(var
);
270 html_path
= xstrdup(value
);
273 if (!strcmp(var
, "man.viewer")) {
275 return config_error_nonbool(var
);
276 add_man_viewer(value
);
279 if (!prefixcmp(var
, "man."))
280 return add_man_viewer_info(var
, value
);
282 return git_default_config(var
, value
, cb
);
285 static struct cmdnames main_cmds
, other_cmds
;
287 static int is_git_command(const char *s
)
289 return is_in_cmdlist(&main_cmds
, s
) ||
290 is_in_cmdlist(&other_cmds
, s
);
293 static const char *prepend(const char *prefix
, const char *cmd
)
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
);
303 static const char *cmd_to_page(const char *git_cmd
)
307 else if (!prefixcmp(git_cmd
, "git"))
309 else if (is_git_command(git_cmd
))
310 return prepend("git-", git_cmd
);
312 return prepend("git", git_cmd
);
315 static void setup_man_path(void)
317 struct strbuf new_path
= STRBUF_INIT
;
318 const char *old_path
= getenv("MANPATH");
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
, ':');
327 strbuf_addstr(&new_path
, old_path
);
329 setenv("MANPATH", new_path
.buf
, 1);
331 strbuf_release(&new_path
);
334 static void exec_viewer(const char *name
, const char *page
)
336 const char *info
= get_man_viewer_info(name
);
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
);
345 exec_man_cmd(info
, page
);
347 warning(_("'%s': unknown man viewer."), name
);
350 static void show_man_page(const char *git_cmd
)
352 struct man_viewer_list
*viewer
;
353 const char *page
= cmd_to_page(git_cmd
);
354 const char *fallback
= getenv("GIT_MAN_VIEWER");
357 for (viewer
= man_viewer_list
; viewer
; viewer
= viewer
->next
)
359 exec_viewer(viewer
->name
, page
); /* will return when unable */
362 exec_viewer(fallback
, page
);
363 exec_viewer("man", page
);
364 die(_("no man viewer handled the request"));
367 static void show_info_page(const char *git_cmd
)
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"));
375 static void get_html_page_path(struct strbuf
*page_path
, const char *page
)
379 html_path
= system_path(GIT_HTML_PATH
);
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
);
388 strbuf_init(page_path
, 0);
389 strbuf_addf(page_path
, "%s/%s.html", html_path
, page
);
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
398 static void open_html(const char *path
)
400 execl_git_cmd("web--browse", "-c", "help.browser", path
, (char *)NULL
);
404 static void show_html_page(const char *git_cmd
)
406 const char *page
= cmd_to_page(git_cmd
);
407 struct strbuf page_path
; /* it leaks but we exec bellow */
409 get_html_page_path(&page_path
, page
);
411 open_html(page_path
.buf
);
414 int cmd_help(int argc
, const char **argv
, const char *prefix
)
418 enum help_format parsed_help_format
;
419 load_command_list("git-", &main_cmds
, &other_cmds
);
421 argc
= parse_options(argc
, argv
, prefix
, builtin_help_options
,
422 builtin_help_usage
, 0);
423 parsed_help_format
= help_format
;
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
));
434 printf(_("usage: %s%s"), _(git_usage_string
), "\n\n");
435 list_common_cmds_help();
436 printf("\n%s\n", _(git_more_info_string
));
440 setup_git_directory_gently(&nongit
);
441 git_config(git_help_config
, NULL
);
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
);
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
);
454 switch (help_format
) {
455 case HELP_FORMAT_NONE
:
456 case HELP_FORMAT_MAN
:
457 show_man_page(argv
[0]);
459 case HELP_FORMAT_INFO
:
460 show_info_page(argv
[0]);
462 case HELP_FORMAT_WEB
:
463 show_html_page(argv
[0]);