Windows ANSI - hook write()
[git/mingw/4msysgit/peterh.git] / help.c
blob2ed68389fed9c9c3aed64ac91a022486a444ea21
1 /*
2 * builtin-help.c
4 * Builtin help-related commands (help, usage, version)
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 "dir.h"
14 static struct man_viewer_list {
15 struct man_viewer_list *next;
16 char name[FLEX_ARRAY];
17 } *man_viewer_list;
19 static struct man_viewer_info_list {
20 struct man_viewer_info_list *next;
21 const char *info;
22 char name[FLEX_ARRAY];
23 } *man_viewer_info_list;
25 enum help_format {
26 HELP_FORMAT_MAN,
27 HELP_FORMAT_INFO,
28 HELP_FORMAT_WEB,
31 static int show_all = 0;
32 #ifdef __MINGW32__
33 static enum help_format help_format = HELP_FORMAT_WEB;
34 #else
35 static enum help_format help_format = HELP_FORMAT_MAN;
36 #endif
37 static struct option builtin_help_options[] = {
38 OPT_BOOLEAN('a', "all", &show_all, "print all available commands"),
39 OPT_SET_INT('m', "man", &help_format, "show man page", HELP_FORMAT_MAN),
40 OPT_SET_INT('w', "web", &help_format, "show manual in web browser",
41 HELP_FORMAT_WEB),
42 OPT_SET_INT('i', "info", &help_format, "show info page",
43 HELP_FORMAT_INFO),
44 OPT_END(),
47 static const char * const builtin_help_usage[] = {
48 "git-help [--all] [--man|--web|--info] [command]",
49 NULL
52 static enum help_format parse_help_format(const char *format)
54 if (!strcmp(format, "man"))
55 return HELP_FORMAT_MAN;
56 if (!strcmp(format, "info"))
57 return HELP_FORMAT_INFO;
58 if (!strcmp(format, "web") || !strcmp(format, "html"))
59 return HELP_FORMAT_WEB;
60 die("unrecognized help format '%s'", format);
63 static const char *get_man_viewer_info(const char *name)
65 struct man_viewer_info_list *viewer;
67 for (viewer = man_viewer_info_list; viewer; viewer = viewer->next)
69 if (!strcasecmp(name, viewer->name))
70 return viewer->info;
72 return NULL;
75 static int check_emacsclient_version(void)
77 struct strbuf buffer = STRBUF_INIT;
78 struct child_process ec_process;
79 const char *argv_ec[] = { "emacsclient", "--version", NULL };
80 int version;
82 /* emacsclient prints its version number on stderr */
83 memset(&ec_process, 0, sizeof(ec_process));
84 ec_process.argv = argv_ec;
85 ec_process.err = -1;
86 ec_process.stdout_to_stderr = 1;
87 if (start_command(&ec_process)) {
88 fprintf(stderr, "Failed to start emacsclient.\n");
89 return -1;
91 strbuf_read(&buffer, ec_process.err, 20);
92 close(ec_process.err);
95 * Don't bother checking return value, because "emacsclient --version"
96 * seems to always exits with code 1.
98 finish_command(&ec_process);
100 if (prefixcmp(buffer.buf, "emacsclient")) {
101 fprintf(stderr, "Failed to parse emacsclient version.\n");
102 strbuf_release(&buffer);
103 return -1;
106 strbuf_remove(&buffer, 0, strlen("emacsclient"));
107 version = atoi(buffer.buf);
109 if (version < 22) {
110 fprintf(stderr,
111 "emacsclient version '%d' too old (< 22).\n",
112 version);
113 strbuf_release(&buffer);
114 return -1;
117 strbuf_release(&buffer);
118 return 0;
121 static void exec_woman_emacs(const char* path, const char *page)
123 if (!check_emacsclient_version()) {
124 /* This works only with emacsclient version >= 22. */
125 struct strbuf man_page = STRBUF_INIT;
127 if (!path)
128 path = "emacsclient";
129 strbuf_addf(&man_page, "(woman \"%s\")", page);
130 execlp(path, "emacsclient", "-e", man_page.buf, NULL);
131 warning("failed to exec '%s': %s", path, strerror(errno));
135 static void exec_man_konqueror(const char* path, const char *page)
137 const char *display = getenv("DISPLAY");
138 if (display && *display) {
139 struct strbuf man_page = STRBUF_INIT;
140 const char *filename = "kfmclient";
142 /* It's simpler to launch konqueror using kfmclient. */
143 if (path) {
144 const char *file = strrchr(path, '/');
145 if (file && !strcmp(file + 1, "konqueror")) {
146 char *new = xstrdup(path);
147 char *dest = strrchr(new, '/');
149 /* strlen("konqueror") == strlen("kfmclient") */
150 strcpy(dest + 1, "kfmclient");
151 path = new;
153 if (file)
154 filename = file;
155 } else
156 path = "kfmclient";
157 strbuf_addf(&man_page, "man:%s(1)", page);
158 execlp(path, filename, "newTab", man_page.buf, NULL);
159 warning("failed to exec '%s': %s", path, strerror(errno));
163 static void exec_man_man(const char* path, const char *page)
165 if (!path)
166 path = "man";
167 execlp(path, "man", page, NULL);
168 warning("failed to exec '%s': %s", path, strerror(errno));
171 static void exec_man_cmd(const char *cmd, const char *page)
173 struct strbuf shell_cmd = STRBUF_INIT;
174 strbuf_addf(&shell_cmd, "%s %s", cmd, page);
175 execl("/bin/sh", "sh", "-c", shell_cmd.buf, NULL);
176 warning("failed to exec '%s': %s", cmd, strerror(errno));
179 static void add_man_viewer(const char *name)
181 struct man_viewer_list **p = &man_viewer_list;
182 size_t len = strlen(name);
184 while (*p)
185 p = &((*p)->next);
186 *p = xcalloc(1, (sizeof(**p) + len + 1));
187 strncpy((*p)->name, name, len);
190 static int supported_man_viewer(const char *name, size_t len)
192 return (!strncasecmp("man", name, len) ||
193 !strncasecmp("woman", name, len) ||
194 !strncasecmp("konqueror", name, len));
197 static void do_add_man_viewer_info(const char *name,
198 size_t len,
199 const char *value)
201 struct man_viewer_info_list *new = xcalloc(1, sizeof(*new) + len + 1);
203 strncpy(new->name, name, len);
204 new->info = xstrdup(value);
205 new->next = man_viewer_info_list;
206 man_viewer_info_list = new;
209 static int add_man_viewer_path(const char *name,
210 size_t len,
211 const char *value)
213 if (supported_man_viewer(name, len))
214 do_add_man_viewer_info(name, len, value);
215 else
216 warning("'%s': path for unsupported man viewer.\n"
217 "Please consider using 'man.<tool>.cmd' instead.",
218 name);
220 return 0;
223 static int add_man_viewer_cmd(const char *name,
224 size_t len,
225 const char *value)
227 if (supported_man_viewer(name, len))
228 warning("'%s': cmd for supported man viewer.\n"
229 "Please consider using 'man.<tool>.path' instead.",
230 name);
231 else
232 do_add_man_viewer_info(name, len, value);
234 return 0;
237 static int add_man_viewer_info(const char *var, const char *value)
239 const char *name = var + 4;
240 const char *subkey = strrchr(name, '.');
242 if (!subkey)
243 return error("Config with no key for man viewer: %s", name);
245 if (!strcmp(subkey, ".path")) {
246 if (!value)
247 return config_error_nonbool(var);
248 return add_man_viewer_path(name, subkey - name, value);
250 if (!strcmp(subkey, ".cmd")) {
251 if (!value)
252 return config_error_nonbool(var);
253 return add_man_viewer_cmd(name, subkey - name, value);
256 warning("'%s': unsupported man viewer sub key.", subkey);
257 return 0;
260 static int git_help_config(const char *var, const char *value, void *cb)
262 if (!strcmp(var, "help.format")) {
263 if (!value)
264 return config_error_nonbool(var);
265 help_format = parse_help_format(value);
266 return 0;
268 if (!strcmp(var, "man.viewer")) {
269 if (!value)
270 return config_error_nonbool(var);
271 add_man_viewer(value);
272 return 0;
274 if (!prefixcmp(var, "man."))
275 return add_man_viewer_info(var, value);
277 return git_default_config(var, value, cb);
280 /* most GUI terminals set COLUMNS (although some don't export it) */
281 static int term_columns(void)
283 char *col_string = getenv("COLUMNS");
284 int n_cols;
286 if (col_string && (n_cols = atoi(col_string)) > 0)
287 return n_cols;
289 #ifdef TIOCGWINSZ
291 struct winsize ws;
292 if (!ioctl(1, TIOCGWINSZ, &ws)) {
293 if (ws.ws_col)
294 return ws.ws_col;
297 #endif
299 return 80;
302 static inline void mput_char(char c, unsigned int num)
304 while(num--)
305 putchar(c);
308 static struct cmdnames {
309 int alloc;
310 int cnt;
311 struct cmdname {
312 size_t len;
313 char name[1];
314 } **names;
315 } main_cmds, other_cmds;
317 static void add_cmdname(struct cmdnames *cmds, const char *name, int len)
319 struct cmdname *ent = xmalloc(sizeof(*ent) + len);
321 ent->len = len;
322 memcpy(ent->name, name, len);
323 ent->name[len] = 0;
325 ALLOC_GROW(cmds->names, cmds->cnt + 1, cmds->alloc);
326 cmds->names[cmds->cnt++] = ent;
329 static int cmdname_compare(const void *a_, const void *b_)
331 struct cmdname *a = *(struct cmdname **)a_;
332 struct cmdname *b = *(struct cmdname **)b_;
333 return strcmp(a->name, b->name);
336 static void uniq(struct cmdnames *cmds)
338 int i, j;
340 if (!cmds->cnt)
341 return;
343 for (i = j = 1; i < cmds->cnt; i++)
344 if (strcmp(cmds->names[i]->name, cmds->names[i-1]->name))
345 cmds->names[j++] = cmds->names[i];
347 cmds->cnt = j;
350 static void exclude_cmds(struct cmdnames *cmds, struct cmdnames *excludes)
352 int ci, cj, ei;
353 int cmp;
355 ci = cj = ei = 0;
356 while (ci < cmds->cnt && ei < excludes->cnt) {
357 cmp = strcmp(cmds->names[ci]->name, excludes->names[ei]->name);
358 if (cmp < 0)
359 cmds->names[cj++] = cmds->names[ci++];
360 else if (cmp == 0)
361 ci++, ei++;
362 else if (cmp > 0)
363 ei++;
366 while (ci < cmds->cnt)
367 cmds->names[cj++] = cmds->names[ci++];
369 cmds->cnt = cj;
372 static void pretty_print_string_list(struct cmdnames *cmds, int longest)
374 int cols = 1, rows;
375 int space = longest + 1; /* min 1 SP between words */
376 int max_cols = term_columns() - 1; /* don't print *on* the edge */
377 int i, j;
379 if (space < max_cols)
380 cols = max_cols / space;
381 rows = (cmds->cnt + cols - 1) / cols;
383 for (i = 0; i < rows; i++) {
384 printf(" ");
386 for (j = 0; j < cols; j++) {
387 int n = j * rows + i;
388 int size = space;
389 if (n >= cmds->cnt)
390 break;
391 if (j == cols-1 || n + rows >= cmds->cnt)
392 size = 1;
393 printf("%-*s", size, cmds->names[n]->name);
395 putchar('\n');
399 static int is_executable(const char *name)
401 struct stat st;
403 if (stat(name, &st) || /* stat, not lstat */
404 !S_ISREG(st.st_mode))
405 return 0;
407 #ifdef __MINGW32__
408 /* cannot trust the executable bit, peek into the file instead */
409 char buf[3] = { 0 };
410 int n;
411 int fd = open(name, O_RDONLY);
412 st.st_mode &= ~S_IXUSR;
413 if (fd >= 0) {
414 n = read(fd, buf, 2);
415 if (n == 2)
416 /* DOS executables start with "MZ" */
417 if (!strcmp(buf, "#!") || !strcmp(buf, "MZ"))
418 st.st_mode |= S_IXUSR;
419 close(fd);
421 #endif
422 return st.st_mode & S_IXUSR;
425 static unsigned int list_commands_in_dir(struct cmdnames *cmds,
426 const char *path)
428 unsigned int longest = 0;
429 const char *prefix = "git-";
430 int prefix_len = strlen(prefix);
431 DIR *dir = opendir(path);
432 struct dirent *de;
434 if (!dir || chdir(path))
435 return 0;
437 while ((de = readdir(dir)) != NULL) {
438 int entlen;
440 if (prefixcmp(de->d_name, prefix))
441 continue;
443 if (!is_executable(de->d_name))
444 continue;
446 entlen = strlen(de->d_name) - prefix_len;
447 if (has_extension(de->d_name, ".exe"))
448 entlen -= 4;
450 if (longest < entlen)
451 longest = entlen;
453 add_cmdname(cmds, de->d_name + prefix_len, entlen);
455 closedir(dir);
457 return longest;
460 static unsigned int load_command_list(void)
462 unsigned int longest = 0;
463 unsigned int len;
464 const char *env_path = getenv("PATH");
465 char *paths, *path, *colon;
466 const char *exec_path = git_exec_path();
468 if (exec_path)
469 longest = list_commands_in_dir(&main_cmds, exec_path);
471 if (!env_path) {
472 fprintf(stderr, "PATH not set\n");
473 exit(1);
476 path = paths = xstrdup(env_path);
477 while (1) {
478 if ((colon = strchr(path, PATH_SEP)))
479 *colon = 0;
481 len = list_commands_in_dir(&other_cmds, path);
482 if (len > longest)
483 longest = len;
485 if (!colon)
486 break;
487 path = colon + 1;
489 free(paths);
491 qsort(main_cmds.names, main_cmds.cnt,
492 sizeof(*main_cmds.names), cmdname_compare);
493 uniq(&main_cmds);
495 qsort(other_cmds.names, other_cmds.cnt,
496 sizeof(*other_cmds.names), cmdname_compare);
497 uniq(&other_cmds);
498 exclude_cmds(&other_cmds, &main_cmds);
500 return longest;
503 static void list_commands(void)
505 unsigned int longest = load_command_list();
506 const char *exec_path = git_exec_path();
508 if (main_cmds.cnt) {
509 printf("available git commands in '%s'\n", exec_path);
510 printf("----------------------------");
511 mput_char('-', strlen(exec_path));
512 putchar('\n');
513 pretty_print_string_list(&main_cmds, longest);
514 putchar('\n');
517 if (other_cmds.cnt) {
518 printf("git commands available from elsewhere on your $PATH\n");
519 printf("---------------------------------------------------\n");
520 pretty_print_string_list(&other_cmds, longest);
521 putchar('\n');
525 void list_common_cmds_help(void)
527 int i, longest = 0;
529 for (i = 0; i < ARRAY_SIZE(common_cmds); i++) {
530 if (longest < strlen(common_cmds[i].name))
531 longest = strlen(common_cmds[i].name);
534 puts("The most commonly used git commands are:");
535 for (i = 0; i < ARRAY_SIZE(common_cmds); i++) {
536 printf(" %s ", common_cmds[i].name);
537 mput_char(' ', longest - strlen(common_cmds[i].name));
538 puts(common_cmds[i].help);
542 static int is_in_cmdlist(struct cmdnames *c, const char *s)
544 int i;
545 for (i = 0; i < c->cnt; i++)
546 if (!strcmp(s, c->names[i]->name))
547 return 1;
548 return 0;
551 static int is_git_command(const char *s)
553 load_command_list();
554 return is_in_cmdlist(&main_cmds, s) ||
555 is_in_cmdlist(&other_cmds, s);
558 static const char *cmd_to_page(const char *git_cmd)
560 if (!git_cmd)
561 return "git";
562 else if (!prefixcmp(git_cmd, "git"))
563 return git_cmd;
564 else {
565 int page_len = strlen(git_cmd) + 4;
566 char *p = xmalloc(page_len + 1);
567 strcpy(p, "git-");
568 strcpy(p + 4, git_cmd);
569 p[page_len] = 0;
570 return p;
574 static void setup_man_path(void)
576 struct strbuf new_path;
577 const char *old_path = getenv("MANPATH");
579 strbuf_init(&new_path, 0);
581 /* We should always put ':' after our path. If there is no
582 * old_path, the ':' at the end will let 'man' to try
583 * system-wide paths after ours to find the manual page. If
584 * there is old_path, we need ':' as delimiter. */
585 strbuf_addstr(&new_path, GIT_MAN_PATH);
586 strbuf_addch(&new_path, ':');
587 if (old_path)
588 strbuf_addstr(&new_path, old_path);
590 setenv("MANPATH", new_path.buf, 1);
592 strbuf_release(&new_path);
595 static void exec_viewer(const char *name, const char *page)
597 const char *info = get_man_viewer_info(name);
599 if (!strcasecmp(name, "man"))
600 exec_man_man(info, page);
601 else if (!strcasecmp(name, "woman"))
602 exec_woman_emacs(info, page);
603 else if (!strcasecmp(name, "konqueror"))
604 exec_man_konqueror(info, page);
605 else if (info)
606 exec_man_cmd(info, page);
607 else
608 warning("'%s': unknown man viewer.", name);
611 static void show_man_page(const char *git_cmd)
613 struct man_viewer_list *viewer;
614 const char *page = cmd_to_page(git_cmd);
616 setup_man_path();
617 for (viewer = man_viewer_list; viewer; viewer = viewer->next)
619 exec_viewer(viewer->name, page); /* will return when unable */
621 exec_viewer("man", page);
622 die("no man viewer handled the request");
625 static void show_info_page(const char *git_cmd)
627 const char *page = cmd_to_page(git_cmd);
628 setenv("INFOPATH", GIT_INFO_PATH, 1);
629 execlp("info", "info", "gitman", page, NULL);
632 static void get_html_page_path(struct strbuf *page_path, const char *page)
634 struct stat st;
636 /* Check that we have a git documentation directory. */
637 if (stat(GIT_HTML_PATH "/git.html", &st) || !S_ISREG(st.st_mode))
638 die("'%s': not a documentation directory.", GIT_HTML_PATH);
640 strbuf_init(page_path, 0);
641 strbuf_addf(page_path, GIT_HTML_PATH "/%s.html", page);
644 static void show_html_page(const char *git_cmd)
646 #ifdef __MINGW32__
647 const char* exec_path = git_exec_path();
648 char *htmlpath = make_native_separator(
649 mkpath("%s/../doc/git/html/%s.html"
650 , exec_path
651 , git_cmd)
653 if (!file_exists(htmlpath)) {
654 htmlpath = make_native_separator(
655 mkpath("%s/../doc/git/html/git-%s.html"
656 , exec_path
657 , git_cmd)
659 if (!file_exists(htmlpath)) {
660 fprintf(stderr, "Can't find HTML help for '%s'.\n"
661 , git_cmd);
662 exit(1);
665 printf("Launching default browser to display HTML help ...\n");
666 ShellExecute(NULL, "open", htmlpath, NULL, "\\", 0);
667 #else
668 const char *page = cmd_to_page(git_cmd);
669 struct strbuf page_path; /* it leaks but we exec bellow */
671 get_html_page_path(&page_path, page);
673 execl_git_cmd("web--browse", "-c", "help.browser", page_path.buf, NULL);
674 #endif
677 void help_unknown_cmd(const char *cmd)
679 fprintf(stderr, "git: '%s' is not a git-command. See 'git --help'.\n", cmd);
680 exit(1);
683 int cmd_version(int argc, const char **argv, const char *prefix)
685 printf("git version %s\n", git_version_string);
686 return 0;
689 int cmd_help(int argc, const char **argv, const char *prefix)
691 int nongit;
692 const char *alias;
694 setup_git_directory_gently(&nongit);
695 git_config(git_help_config, NULL);
697 argc = parse_options(argc, argv, builtin_help_options,
698 builtin_help_usage, 0);
700 if (show_all) {
701 printf("usage: %s\n\n", git_usage_string);
702 list_commands();
703 printf("%s\n", git_more_info_string);
704 return 0;
707 if (!argv[0]) {
708 printf("usage: %s\n\n", git_usage_string);
709 list_common_cmds_help();
710 printf("\n%s\n", git_more_info_string);
711 return 0;
714 alias = alias_lookup(argv[0]);
715 if (alias && !is_git_command(argv[0])) {
716 printf("`git %s' is aliased to `%s'\n", argv[0], alias);
717 return 0;
720 switch (help_format) {
721 case HELP_FORMAT_MAN:
722 show_man_page(argv[0]);
723 break;
724 case HELP_FORMAT_INFO:
725 show_info_page(argv[0]);
726 break;
727 case HELP_FORMAT_WEB:
728 show_html_page(argv[0]);
729 break;
732 return 0;