Move name hashing functions into a file of its own
[git/git-p4.git] / help.c
blob10298fb0a197f18008783214de11e424fbadb77d
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),
33 OPT_END(),
36 static const char * const builtin_help_usage[] = {
37 "git-help [--all] [--man|--web|--info] [command]",
38 NULL
41 static enum help_format parse_help_format(const char *format)
43 if (!strcmp(format, "man"))
44 return HELP_FORMAT_MAN;
45 if (!strcmp(format, "info"))
46 return HELP_FORMAT_INFO;
47 if (!strcmp(format, "web") || !strcmp(format, "html"))
48 return HELP_FORMAT_WEB;
49 die("unrecognized help format '%s'", format);
52 static int check_emacsclient_version(void)
54 struct strbuf buffer = STRBUF_INIT;
55 struct child_process ec_process;
56 const char *argv_ec[] = { "emacsclient", "--version", NULL };
57 int version;
59 /* emacsclient prints its version number on stderr */
60 memset(&ec_process, 0, sizeof(ec_process));
61 ec_process.argv = argv_ec;
62 ec_process.err = -1;
63 ec_process.stdout_to_stderr = 1;
64 if (start_command(&ec_process)) {
65 fprintf(stderr, "Failed to start emacsclient.\n");
66 return -1;
68 strbuf_read(&buffer, ec_process.err, 20);
69 close(ec_process.err);
72 * Don't bother checking return value, because "emacsclient --version"
73 * seems to always exits with code 1.
75 finish_command(&ec_process);
77 if (prefixcmp(buffer.buf, "emacsclient")) {
78 fprintf(stderr, "Failed to parse emacsclient version.\n");
79 strbuf_release(&buffer);
80 return -1;
83 strbuf_remove(&buffer, 0, strlen("emacsclient"));
84 version = atoi(buffer.buf);
86 if (version < 22) {
87 fprintf(stderr,
88 "emacsclient version '%d' too old (< 22).\n",
89 version);
90 strbuf_release(&buffer);
91 return -1;
94 strbuf_release(&buffer);
95 return 0;
98 static void exec_woman_emacs(const char *page)
100 if (!check_emacsclient_version()) {
101 /* This works only with emacsclient version >= 22. */
102 struct strbuf man_page = STRBUF_INIT;
103 strbuf_addf(&man_page, "(woman \"%s\")", page);
104 execlp("emacsclient", "emacsclient", "-e", man_page.buf, NULL);
108 static void exec_man_konqueror(const char *page)
110 const char *display = getenv("DISPLAY");
111 if (display && *display) {
112 struct strbuf man_page = STRBUF_INIT;
113 strbuf_addf(&man_page, "man:%s(1)", page);
114 execlp("kfmclient", "kfmclient", "newTab", man_page.buf, NULL);
118 static void exec_man_man(const char *page)
120 execlp("man", "man", page, NULL);
123 static void do_add_man_viewer(void (*exec)(const char *))
125 struct man_viewer_list **p = &man_viewer_list;
127 while (*p)
128 p = &((*p)->next);
129 *p = xmalloc(sizeof(**p));
130 (*p)->next = NULL;
131 (*p)->exec = exec;
134 static int add_man_viewer(const char *value)
136 if (!strcasecmp(value, "man"))
137 do_add_man_viewer(exec_man_man);
138 else if (!strcasecmp(value, "woman"))
139 do_add_man_viewer(exec_woman_emacs);
140 else if (!strcasecmp(value, "konqueror"))
141 do_add_man_viewer(exec_man_konqueror);
142 else
143 warning("'%s': unsupported man viewer.", value);
145 return 0;
148 static int git_help_config(const char *var, const char *value)
150 if (!strcmp(var, "help.format")) {
151 if (!value)
152 return config_error_nonbool(var);
153 help_format = parse_help_format(value);
154 return 0;
156 if (!strcmp(var, "man.viewer")) {
157 if (!value)
158 return config_error_nonbool(var);
159 return add_man_viewer(value);
161 return git_default_config(var, value);
164 /* most GUI terminals set COLUMNS (although some don't export it) */
165 static int term_columns(void)
167 char *col_string = getenv("COLUMNS");
168 int n_cols;
170 if (col_string && (n_cols = atoi(col_string)) > 0)
171 return n_cols;
173 #ifdef TIOCGWINSZ
175 struct winsize ws;
176 if (!ioctl(1, TIOCGWINSZ, &ws)) {
177 if (ws.ws_col)
178 return ws.ws_col;
181 #endif
183 return 80;
186 static inline void mput_char(char c, unsigned int num)
188 while(num--)
189 putchar(c);
192 static struct cmdnames {
193 int alloc;
194 int cnt;
195 struct cmdname {
196 size_t len;
197 char name[1];
198 } **names;
199 } main_cmds, other_cmds;
201 static void add_cmdname(struct cmdnames *cmds, const char *name, int len)
203 struct cmdname *ent = xmalloc(sizeof(*ent) + len);
205 ent->len = len;
206 memcpy(ent->name, name, len);
207 ent->name[len] = 0;
209 ALLOC_GROW(cmds->names, cmds->cnt + 1, cmds->alloc);
210 cmds->names[cmds->cnt++] = ent;
213 static int cmdname_compare(const void *a_, const void *b_)
215 struct cmdname *a = *(struct cmdname **)a_;
216 struct cmdname *b = *(struct cmdname **)b_;
217 return strcmp(a->name, b->name);
220 static void uniq(struct cmdnames *cmds)
222 int i, j;
224 if (!cmds->cnt)
225 return;
227 for (i = j = 1; i < cmds->cnt; i++)
228 if (strcmp(cmds->names[i]->name, cmds->names[i-1]->name))
229 cmds->names[j++] = cmds->names[i];
231 cmds->cnt = j;
234 static void exclude_cmds(struct cmdnames *cmds, struct cmdnames *excludes)
236 int ci, cj, ei;
237 int cmp;
239 ci = cj = ei = 0;
240 while (ci < cmds->cnt && ei < excludes->cnt) {
241 cmp = strcmp(cmds->names[ci]->name, excludes->names[ei]->name);
242 if (cmp < 0)
243 cmds->names[cj++] = cmds->names[ci++];
244 else if (cmp == 0)
245 ci++, ei++;
246 else if (cmp > 0)
247 ei++;
250 while (ci < cmds->cnt)
251 cmds->names[cj++] = cmds->names[ci++];
253 cmds->cnt = cj;
256 static void pretty_print_string_list(struct cmdnames *cmds, int longest)
258 int cols = 1, rows;
259 int space = longest + 1; /* min 1 SP between words */
260 int max_cols = term_columns() - 1; /* don't print *on* the edge */
261 int i, j;
263 if (space < max_cols)
264 cols = max_cols / space;
265 rows = (cmds->cnt + cols - 1) / cols;
267 for (i = 0; i < rows; i++) {
268 printf(" ");
270 for (j = 0; j < cols; j++) {
271 int n = j * rows + i;
272 int size = space;
273 if (n >= cmds->cnt)
274 break;
275 if (j == cols-1 || n + rows >= cmds->cnt)
276 size = 1;
277 printf("%-*s", size, cmds->names[n]->name);
279 putchar('\n');
283 static unsigned int list_commands_in_dir(struct cmdnames *cmds,
284 const char *path)
286 unsigned int longest = 0;
287 const char *prefix = "git-";
288 int prefix_len = strlen(prefix);
289 DIR *dir = opendir(path);
290 struct dirent *de;
292 if (!dir || chdir(path))
293 return 0;
295 while ((de = readdir(dir)) != NULL) {
296 struct stat st;
297 int entlen;
299 if (prefixcmp(de->d_name, prefix))
300 continue;
302 if (stat(de->d_name, &st) || /* stat, not lstat */
303 !S_ISREG(st.st_mode) ||
304 !(st.st_mode & S_IXUSR))
305 continue;
307 entlen = strlen(de->d_name) - prefix_len;
308 if (has_extension(de->d_name, ".exe"))
309 entlen -= 4;
311 if (longest < entlen)
312 longest = entlen;
314 add_cmdname(cmds, de->d_name + prefix_len, entlen);
316 closedir(dir);
318 return longest;
321 static unsigned int load_command_list(void)
323 unsigned int longest = 0;
324 unsigned int len;
325 const char *env_path = getenv("PATH");
326 char *paths, *path, *colon;
327 const char *exec_path = git_exec_path();
329 if (exec_path)
330 longest = list_commands_in_dir(&main_cmds, exec_path);
332 if (!env_path) {
333 fprintf(stderr, "PATH not set\n");
334 exit(1);
337 path = paths = xstrdup(env_path);
338 while (1) {
339 if ((colon = strchr(path, ':')))
340 *colon = 0;
342 len = list_commands_in_dir(&other_cmds, path);
343 if (len > longest)
344 longest = len;
346 if (!colon)
347 break;
348 path = colon + 1;
350 free(paths);
352 qsort(main_cmds.names, main_cmds.cnt,
353 sizeof(*main_cmds.names), cmdname_compare);
354 uniq(&main_cmds);
356 qsort(other_cmds.names, other_cmds.cnt,
357 sizeof(*other_cmds.names), cmdname_compare);
358 uniq(&other_cmds);
359 exclude_cmds(&other_cmds, &main_cmds);
361 return longest;
364 static void list_commands(void)
366 unsigned int longest = load_command_list();
367 const char *exec_path = git_exec_path();
369 if (main_cmds.cnt) {
370 printf("available git commands in '%s'\n", exec_path);
371 printf("----------------------------");
372 mput_char('-', strlen(exec_path));
373 putchar('\n');
374 pretty_print_string_list(&main_cmds, longest);
375 putchar('\n');
378 if (other_cmds.cnt) {
379 printf("git commands available from elsewhere on your $PATH\n");
380 printf("---------------------------------------------------\n");
381 pretty_print_string_list(&other_cmds, longest);
382 putchar('\n');
386 void list_common_cmds_help(void)
388 int i, longest = 0;
390 for (i = 0; i < ARRAY_SIZE(common_cmds); i++) {
391 if (longest < strlen(common_cmds[i].name))
392 longest = strlen(common_cmds[i].name);
395 puts("The most commonly used git commands are:");
396 for (i = 0; i < ARRAY_SIZE(common_cmds); i++) {
397 printf(" %s ", common_cmds[i].name);
398 mput_char(' ', longest - strlen(common_cmds[i].name));
399 puts(common_cmds[i].help);
403 static int is_in_cmdlist(struct cmdnames *c, const char *s)
405 int i;
406 for (i = 0; i < c->cnt; i++)
407 if (!strcmp(s, c->names[i]->name))
408 return 1;
409 return 0;
412 static int is_git_command(const char *s)
414 load_command_list();
415 return is_in_cmdlist(&main_cmds, s) ||
416 is_in_cmdlist(&other_cmds, s);
419 static const char *cmd_to_page(const char *git_cmd)
421 if (!git_cmd)
422 return "git";
423 else if (!prefixcmp(git_cmd, "git"))
424 return git_cmd;
425 else {
426 int page_len = strlen(git_cmd) + 4;
427 char *p = xmalloc(page_len + 1);
428 strcpy(p, "git-");
429 strcpy(p + 4, git_cmd);
430 p[page_len] = 0;
431 return p;
435 static void setup_man_path(void)
437 struct strbuf new_path;
438 const char *old_path = getenv("MANPATH");
440 strbuf_init(&new_path, 0);
442 /* We should always put ':' after our path. If there is no
443 * old_path, the ':' at the end will let 'man' to try
444 * system-wide paths after ours to find the manual page. If
445 * there is old_path, we need ':' as delimiter. */
446 strbuf_addstr(&new_path, GIT_MAN_PATH);
447 strbuf_addch(&new_path, ':');
448 if (old_path)
449 strbuf_addstr(&new_path, old_path);
451 setenv("MANPATH", new_path.buf, 1);
453 strbuf_release(&new_path);
456 static void show_man_page(const char *git_cmd)
458 struct man_viewer_list *viewer;
459 const char *page = cmd_to_page(git_cmd);
461 setup_man_path();
462 for (viewer = man_viewer_list; viewer; viewer = viewer->next)
464 viewer->exec(page); /* will return when unable */
466 exec_man_man(page);
467 die("no man viewer handled the request");
470 static void show_info_page(const char *git_cmd)
472 const char *page = cmd_to_page(git_cmd);
473 setenv("INFOPATH", GIT_INFO_PATH, 1);
474 execlp("info", "info", "gitman", page, NULL);
477 static void get_html_page_path(struct strbuf *page_path, const char *page)
479 struct stat st;
481 /* Check that we have a git documentation directory. */
482 if (stat(GIT_HTML_PATH "/git.html", &st) || !S_ISREG(st.st_mode))
483 die("'%s': not a documentation directory.", GIT_HTML_PATH);
485 strbuf_init(page_path, 0);
486 strbuf_addf(page_path, GIT_HTML_PATH "/%s.html", page);
489 static void show_html_page(const char *git_cmd)
491 const char *page = cmd_to_page(git_cmd);
492 struct strbuf page_path; /* it leaks but we exec bellow */
494 get_html_page_path(&page_path, page);
496 execl_git_cmd("web--browse", "-c", "help.browser", page_path.buf, NULL);
499 void help_unknown_cmd(const char *cmd)
501 fprintf(stderr, "git: '%s' is not a git-command. See 'git --help'.\n", cmd);
502 exit(1);
505 int cmd_version(int argc, const char **argv, const char *prefix)
507 printf("git version %s\n", git_version_string);
508 return 0;
511 int cmd_help(int argc, const char **argv, const char *prefix)
513 int nongit;
514 const char *alias;
516 setup_git_directory_gently(&nongit);
517 git_config(git_help_config);
519 argc = parse_options(argc, argv, builtin_help_options,
520 builtin_help_usage, 0);
522 if (show_all) {
523 printf("usage: %s\n\n", git_usage_string);
524 list_commands();
525 return 0;
528 if (!argv[0]) {
529 printf("usage: %s\n\n", git_usage_string);
530 list_common_cmds_help();
531 return 0;
534 alias = alias_lookup(argv[0]);
535 if (alias && !is_git_command(argv[0])) {
536 printf("`git %s' is aliased to `%s'\n", argv[0], alias);
537 return 0;
540 switch (help_format) {
541 case HELP_FORMAT_MAN:
542 show_man_page(argv[0]);
543 break;
544 case HELP_FORMAT_INFO:
545 show_info_page(argv[0]);
546 break;
547 case HELP_FORMAT_WEB:
548 show_html_page(argv[0]);
549 break;
552 return 0;