Merge branch 'master' of git://repo.or.cz/alt-git
[git/mingw.git] / help.c
blobe3f94452f3a1ad5acc35a521f90c21c5944f2325
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"
13 static struct man_viewer_list {
14 void (*exec)(const char *);
15 struct man_viewer_list *next;
16 } *man_viewer_list;
18 enum help_format {
19 HELP_FORMAT_MAN,
20 HELP_FORMAT_INFO,
21 HELP_FORMAT_WEB,
24 static int show_all = 0;
25 static enum help_format help_format = HELP_FORMAT_MAN;
26 static struct option builtin_help_options[] = {
27 OPT_BOOLEAN('a', "all", &show_all, "print all available commands"),
28 OPT_SET_INT('m', "man", &help_format, "show man page", HELP_FORMAT_MAN),
29 OPT_SET_INT('w', "web", &help_format, "show manual in web browser",
30 HELP_FORMAT_WEB),
31 OPT_SET_INT('i', "info", &help_format, "show info page",
32 HELP_FORMAT_INFO),
35 static const char * const builtin_help_usage[] = {
36 "git-help [--all] [--man|--web|--info] [command]",
37 NULL
40 static enum help_format parse_help_format(const char *format)
42 if (!strcmp(format, "man"))
43 return HELP_FORMAT_MAN;
44 if (!strcmp(format, "info"))
45 return HELP_FORMAT_INFO;
46 if (!strcmp(format, "web") || !strcmp(format, "html"))
47 return HELP_FORMAT_WEB;
48 die("unrecognized help format '%s'", format);
51 static int check_emacsclient_version(void)
53 struct strbuf buffer = STRBUF_INIT;
54 struct child_process ec_process;
55 const char *argv_ec[] = { "emacsclient", "--version", NULL };
56 int version;
58 /* emacsclient prints its version number on stderr */
59 memset(&ec_process, 0, sizeof(ec_process));
60 ec_process.argv = argv_ec;
61 ec_process.err = -1;
62 ec_process.stdout_to_stderr = 1;
63 if (start_command(&ec_process)) {
64 fprintf(stderr, "Failed to start emacsclient.\n");
65 return -1;
67 strbuf_read(&buffer, ec_process.err, 20);
68 close(ec_process.err);
71 * Don't bother checking return value, because "emacsclient --version"
72 * seems to always exits with code 1.
74 finish_command(&ec_process);
76 if (prefixcmp(buffer.buf, "emacsclient")) {
77 fprintf(stderr, "Failed to parse emacsclient version.\n");
78 strbuf_release(&buffer);
79 return -1;
82 strbuf_remove(&buffer, 0, strlen("emacsclient"));
83 version = atoi(buffer.buf);
85 if (version < 22) {
86 fprintf(stderr,
87 "emacsclient version '%d' too old (< 22).\n",
88 version);
89 strbuf_release(&buffer);
90 return -1;
93 strbuf_release(&buffer);
94 return 0;
97 static void exec_woman_emacs(const char *page)
99 if (!check_emacsclient_version()) {
100 /* This works only with emacsclient version >= 22. */
101 struct strbuf man_page = STRBUF_INIT;
102 strbuf_addf(&man_page, "(woman \"%s\")", page);
103 execlp("emacsclient", "emacsclient", "-e", man_page.buf, NULL);
107 static void exec_man_konqueror(const char *page)
109 const char *display = getenv("DISPLAY");
110 if (display && *display) {
111 struct strbuf man_page = STRBUF_INIT;
112 strbuf_addf(&man_page, "man:%s(1)", page);
113 execlp("kfmclient", "kfmclient", "newTab", man_page.buf, NULL);
117 static void exec_man_man(const char *page)
119 execlp("man", "man", page, NULL);
122 static void do_add_man_viewer(void (*exec)(const char *))
124 struct man_viewer_list **p = &man_viewer_list;
126 while (*p)
127 p = &((*p)->next);
128 *p = xmalloc(sizeof(**p));
129 (*p)->next = NULL;
130 (*p)->exec = exec;
133 static int add_man_viewer(const char *value)
135 if (!strcasecmp(value, "man"))
136 do_add_man_viewer(exec_man_man);
137 else if (!strcasecmp(value, "woman"))
138 do_add_man_viewer(exec_woman_emacs);
139 else if (!strcasecmp(value, "konqueror"))
140 do_add_man_viewer(exec_man_konqueror);
141 else
142 warning("'%s': unsupported man viewer.", value);
144 return 0;
147 static int git_help_config(const char *var, const char *value)
149 if (!strcmp(var, "help.format")) {
150 if (!value)
151 return config_error_nonbool(var);
152 help_format = parse_help_format(value);
153 return 0;
155 if (!strcmp(var, "man.viewer")) {
156 if (!value)
157 return config_error_nonbool(var);
158 return add_man_viewer(value);
160 return git_default_config(var, value);
163 /* most GUI terminals set COLUMNS (although some don't export it) */
164 static int term_columns(void)
166 char *col_string = getenv("COLUMNS");
167 int n_cols;
169 if (col_string && (n_cols = atoi(col_string)) > 0)
170 return n_cols;
172 #ifdef TIOCGWINSZ
174 struct winsize ws;
175 if (!ioctl(1, TIOCGWINSZ, &ws)) {
176 if (ws.ws_col)
177 return ws.ws_col;
180 #endif
182 return 80;
185 static inline void mput_char(char c, unsigned int num)
187 while(num--)
188 putchar(c);
191 static struct cmdnames {
192 int alloc;
193 int cnt;
194 struct cmdname {
195 size_t len;
196 char name[1];
197 } **names;
198 } main_cmds, other_cmds;
200 static void add_cmdname(struct cmdnames *cmds, const char *name, int len)
202 struct cmdname *ent = xmalloc(sizeof(*ent) + len);
204 ent->len = len;
205 memcpy(ent->name, name, len);
206 ent->name[len] = 0;
208 ALLOC_GROW(cmds->names, cmds->cnt + 1, cmds->alloc);
209 cmds->names[cmds->cnt++] = ent;
212 static int cmdname_compare(const void *a_, const void *b_)
214 struct cmdname *a = *(struct cmdname **)a_;
215 struct cmdname *b = *(struct cmdname **)b_;
216 return strcmp(a->name, b->name);
219 static void uniq(struct cmdnames *cmds)
221 int i, j;
223 if (!cmds->cnt)
224 return;
226 for (i = j = 1; i < cmds->cnt; i++)
227 if (strcmp(cmds->names[i]->name, cmds->names[i-1]->name))
228 cmds->names[j++] = cmds->names[i];
230 cmds->cnt = j;
233 static void exclude_cmds(struct cmdnames *cmds, struct cmdnames *excludes)
235 int ci, cj, ei;
236 int cmp;
238 ci = cj = ei = 0;
239 while (ci < cmds->cnt && ei < excludes->cnt) {
240 cmp = strcmp(cmds->names[ci]->name, excludes->names[ei]->name);
241 if (cmp < 0)
242 cmds->names[cj++] = cmds->names[ci++];
243 else if (cmp == 0)
244 ci++, ei++;
245 else if (cmp > 0)
246 ei++;
249 while (ci < cmds->cnt)
250 cmds->names[cj++] = cmds->names[ci++];
252 cmds->cnt = cj;
255 static void pretty_print_string_list(struct cmdnames *cmds, int longest)
257 int cols = 1, rows;
258 int space = longest + 1; /* min 1 SP between words */
259 int max_cols = term_columns() - 1; /* don't print *on* the edge */
260 int i, j;
262 if (space < max_cols)
263 cols = max_cols / space;
264 rows = (cmds->cnt + cols - 1) / cols;
266 for (i = 0; i < rows; i++) {
267 printf(" ");
269 for (j = 0; j < cols; j++) {
270 int n = j * rows + i;
271 int size = space;
272 if (n >= cmds->cnt)
273 break;
274 if (j == cols-1 || n + rows >= cmds->cnt)
275 size = 1;
276 printf("%-*s", size, cmds->names[n]->name);
278 putchar('\n');
282 static int is_executable(const char *name)
284 struct stat st;
286 if (stat(name, &st) || /* stat, not lstat */
287 !S_ISREG(st.st_mode))
288 return 0;
290 #ifdef __MINGW32__
291 /* cannot trust the executable bit, peek into the file instead */
292 char buf[3] = { 0 };
293 int n;
294 int fd = open(name, O_RDONLY);
295 st.st_mode &= ~S_IXUSR;
296 if (fd >= 0) {
297 n = read(fd, buf, 2);
298 if (n == 2)
299 /* DOS executables start with "MZ" */
300 if (!strcmp(buf, "#!") || !strcmp(buf, "MZ"))
301 st.st_mode |= S_IXUSR;
302 close(fd);
304 #endif
305 return st.st_mode & S_IXUSR;
308 static unsigned int list_commands_in_dir(struct cmdnames *cmds,
309 const char *path)
311 unsigned int longest = 0;
312 const char *prefix = "git-";
313 int prefix_len = strlen(prefix);
314 DIR *dir = opendir(path);
315 struct dirent *de;
317 if (!dir || chdir(path))
318 return 0;
320 while ((de = readdir(dir)) != NULL) {
321 int entlen;
323 if (prefixcmp(de->d_name, prefix))
324 continue;
326 if (!is_executable(de->d_name))
327 continue;
329 entlen = strlen(de->d_name) - prefix_len;
330 if (has_extension(de->d_name, ".exe"))
331 entlen -= 4;
333 if (longest < entlen)
334 longest = entlen;
336 add_cmdname(cmds, de->d_name + prefix_len, entlen);
338 closedir(dir);
340 return longest;
343 static unsigned int load_command_list(void)
345 unsigned int longest = 0;
346 unsigned int len;
347 const char *env_path = getenv("PATH");
348 char *paths, *path, *colon;
349 const char *exec_path = git_exec_path();
351 if (exec_path)
352 longest = list_commands_in_dir(&main_cmds, exec_path);
354 if (!env_path) {
355 fprintf(stderr, "PATH not set\n");
356 exit(1);
359 path = paths = xstrdup(env_path);
360 while (1) {
361 if ((colon = strchr(path, PATH_SEP)))
362 *colon = 0;
364 len = list_commands_in_dir(&other_cmds, path);
365 if (len > longest)
366 longest = len;
368 if (!colon)
369 break;
370 path = colon + 1;
372 free(paths);
374 qsort(main_cmds.names, main_cmds.cnt,
375 sizeof(*main_cmds.names), cmdname_compare);
376 uniq(&main_cmds);
378 qsort(other_cmds.names, other_cmds.cnt,
379 sizeof(*other_cmds.names), cmdname_compare);
380 uniq(&other_cmds);
381 exclude_cmds(&other_cmds, &main_cmds);
383 return longest;
386 static void list_commands(void)
388 unsigned int longest = load_command_list();
389 const char *exec_path = git_exec_path();
391 if (main_cmds.cnt) {
392 printf("available git commands in '%s'\n", exec_path);
393 printf("----------------------------");
394 mput_char('-', strlen(exec_path));
395 putchar('\n');
396 pretty_print_string_list(&main_cmds, longest);
397 putchar('\n');
400 if (other_cmds.cnt) {
401 printf("git commands available from elsewhere on your $PATH\n");
402 printf("---------------------------------------------------\n");
403 pretty_print_string_list(&other_cmds, longest);
404 putchar('\n');
408 void list_common_cmds_help(void)
410 int i, longest = 0;
412 for (i = 0; i < ARRAY_SIZE(common_cmds); i++) {
413 if (longest < strlen(common_cmds[i].name))
414 longest = strlen(common_cmds[i].name);
417 puts("The most commonly used git commands are:");
418 for (i = 0; i < ARRAY_SIZE(common_cmds); i++) {
419 printf(" %s ", common_cmds[i].name);
420 mput_char(' ', longest - strlen(common_cmds[i].name));
421 puts(common_cmds[i].help);
425 static int is_in_cmdlist(struct cmdnames *c, const char *s)
427 int i;
428 for (i = 0; i < c->cnt; i++)
429 if (!strcmp(s, c->names[i]->name))
430 return 1;
431 return 0;
434 static int is_git_command(const char *s)
436 load_command_list();
437 return is_in_cmdlist(&main_cmds, s) ||
438 is_in_cmdlist(&other_cmds, s);
441 static const char *cmd_to_page(const char *git_cmd)
443 if (!git_cmd)
444 return "git";
445 else if (!prefixcmp(git_cmd, "git"))
446 return git_cmd;
447 else {
448 int page_len = strlen(git_cmd) + 4;
449 char *p = xmalloc(page_len + 1);
450 strcpy(p, "git-");
451 strcpy(p + 4, git_cmd);
452 p[page_len] = 0;
453 return p;
457 static void setup_man_path(void)
459 struct strbuf new_path;
460 const char *old_path = getenv("MANPATH");
462 strbuf_init(&new_path, 0);
464 /* We should always put ':' after our path. If there is no
465 * old_path, the ':' at the end will let 'man' to try
466 * system-wide paths after ours to find the manual page. If
467 * there is old_path, we need ':' as delimiter. */
468 strbuf_addstr(&new_path, GIT_MAN_PATH);
469 strbuf_addch(&new_path, ':');
470 if (old_path)
471 strbuf_addstr(&new_path, old_path);
473 setenv("MANPATH", new_path.buf, 1);
475 strbuf_release(&new_path);
478 static void show_man_page(const char *git_cmd)
480 struct man_viewer_list *viewer;
481 const char *page = cmd_to_page(git_cmd);
483 setup_man_path();
484 for (viewer = man_viewer_list; viewer; viewer = viewer->next)
486 viewer->exec(page); /* will return when unable */
488 exec_man_man(page);
489 die("no man viewer handled the request");
492 static void show_info_page(const char *git_cmd)
494 const char *page = cmd_to_page(git_cmd);
495 setenv("INFOPATH", GIT_INFO_PATH, 1);
496 execlp("info", "info", "gitman", page, NULL);
499 static void get_html_page_path(struct strbuf *page_path, const char *page)
501 struct stat st;
503 /* Check that we have a git documentation directory. */
504 if (stat(GIT_HTML_PATH "/git.html", &st) || !S_ISREG(st.st_mode))
505 die("'%s': not a documentation directory.", GIT_HTML_PATH);
507 strbuf_init(page_path, 0);
508 strbuf_addf(page_path, GIT_HTML_PATH "/%s.html", page);
511 static void show_html_page(const char *git_cmd)
513 const char *page = cmd_to_page(git_cmd);
514 struct strbuf page_path; /* it leaks but we exec bellow */
516 get_html_page_path(&page_path, page);
518 execl_git_cmd("web--browse", "-c", "help.browser", page_path.buf, NULL);
521 void help_unknown_cmd(const char *cmd)
523 fprintf(stderr, "git: '%s' is not a git-command. See 'git --help'.\n", cmd);
524 exit(1);
527 int cmd_version(int argc, const char **argv, const char *prefix)
529 printf("git version %s\n", git_version_string);
530 return 0;
533 int cmd_help(int argc, const char **argv, const char *prefix)
535 int nongit;
536 const char *alias;
538 setup_git_directory_gently(&nongit);
539 git_config(git_help_config);
541 argc = parse_options(argc, argv, builtin_help_options,
542 builtin_help_usage, 0);
544 if (show_all) {
545 printf("usage: %s\n\n", git_usage_string);
546 list_commands();
547 return 0;
550 if (!argv[0]) {
551 printf("usage: %s\n\n", git_usage_string);
552 list_common_cmds_help();
553 return 0;
556 alias = alias_lookup(argv[0]);
557 if (alias && !is_git_command(argv[0])) {
558 printf("`git %s' is aliased to `%s'\n", argv[0], alias);
559 return 0;
562 switch (help_format) {
563 case HELP_FORMAT_MAN:
564 show_man_page(argv[0]);
565 break;
566 case HELP_FORMAT_INFO:
567 show_info_page(argv[0]);
568 break;
569 case HELP_FORMAT_WEB:
570 show_html_page(argv[0]);
571 break;
574 return 0;