Makefile: customization for supporting HP-UX
[git/jr.git] / help.c
blob1302a61c83c524099e7d39257daa273a09edad4f
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"
11 static const char *help_default_format;
13 static enum help_format {
14 man_format,
15 info_format,
16 web_format,
17 } help_format = man_format;
19 static void parse_help_format(const char *format)
21 if (!format) {
22 help_format = man_format;
23 return;
25 if (!strcmp(format, "man")) {
26 help_format = man_format;
27 return;
29 if (!strcmp(format, "info")) {
30 help_format = info_format;
31 return;
33 if (!strcmp(format, "web") || !strcmp(format, "html")) {
34 help_format = web_format;
35 return;
37 die("unrecognized help format '%s'", format);
40 static int git_help_config(const char *var, const char *value)
42 if (!strcmp(var, "help.format")) {
43 help_default_format = xstrdup(value);
44 return 0;
46 return git_default_config(var, value);
49 /* most GUI terminals set COLUMNS (although some don't export it) */
50 static int term_columns(void)
52 char *col_string = getenv("COLUMNS");
53 int n_cols;
55 if (col_string && (n_cols = atoi(col_string)) > 0)
56 return n_cols;
58 #ifdef TIOCGWINSZ
60 struct winsize ws;
61 if (!ioctl(1, TIOCGWINSZ, &ws)) {
62 if (ws.ws_col)
63 return ws.ws_col;
66 #endif
68 return 80;
71 static inline void mput_char(char c, unsigned int num)
73 while(num--)
74 putchar(c);
77 static struct cmdnames {
78 int alloc;
79 int cnt;
80 struct cmdname {
81 size_t len;
82 char name[1];
83 } **names;
84 } main_cmds, other_cmds;
86 static void add_cmdname(struct cmdnames *cmds, const char *name, int len)
88 struct cmdname *ent = xmalloc(sizeof(*ent) + len);
90 ent->len = len;
91 memcpy(ent->name, name, len);
92 ent->name[len] = 0;
94 ALLOC_GROW(cmds->names, cmds->cnt + 1, cmds->alloc);
95 cmds->names[cmds->cnt++] = ent;
98 static int cmdname_compare(const void *a_, const void *b_)
100 struct cmdname *a = *(struct cmdname **)a_;
101 struct cmdname *b = *(struct cmdname **)b_;
102 return strcmp(a->name, b->name);
105 static void uniq(struct cmdnames *cmds)
107 int i, j;
109 if (!cmds->cnt)
110 return;
112 for (i = j = 1; i < cmds->cnt; i++)
113 if (strcmp(cmds->names[i]->name, cmds->names[i-1]->name))
114 cmds->names[j++] = cmds->names[i];
116 cmds->cnt = j;
119 static void exclude_cmds(struct cmdnames *cmds, struct cmdnames *excludes)
121 int ci, cj, ei;
122 int cmp;
124 ci = cj = ei = 0;
125 while (ci < cmds->cnt && ei < excludes->cnt) {
126 cmp = strcmp(cmds->names[ci]->name, excludes->names[ei]->name);
127 if (cmp < 0)
128 cmds->names[cj++] = cmds->names[ci++];
129 else if (cmp == 0)
130 ci++, ei++;
131 else if (cmp > 0)
132 ei++;
135 while (ci < cmds->cnt)
136 cmds->names[cj++] = cmds->names[ci++];
138 cmds->cnt = cj;
141 static void pretty_print_string_list(struct cmdnames *cmds, int longest)
143 int cols = 1, rows;
144 int space = longest + 1; /* min 1 SP between words */
145 int max_cols = term_columns() - 1; /* don't print *on* the edge */
146 int i, j;
148 if (space < max_cols)
149 cols = max_cols / space;
150 rows = (cmds->cnt + cols - 1) / cols;
152 for (i = 0; i < rows; i++) {
153 printf(" ");
155 for (j = 0; j < cols; j++) {
156 int n = j * rows + i;
157 int size = space;
158 if (n >= cmds->cnt)
159 break;
160 if (j == cols-1 || n + rows >= cmds->cnt)
161 size = 1;
162 printf("%-*s", size, cmds->names[n]->name);
164 putchar('\n');
168 static unsigned int list_commands_in_dir(struct cmdnames *cmds,
169 const char *path)
171 unsigned int longest = 0;
172 const char *prefix = "git-";
173 int prefix_len = strlen(prefix);
174 DIR *dir = opendir(path);
175 struct dirent *de;
177 if (!dir || chdir(path))
178 return 0;
180 while ((de = readdir(dir)) != NULL) {
181 struct stat st;
182 int entlen;
184 if (prefixcmp(de->d_name, prefix))
185 continue;
187 if (stat(de->d_name, &st) || /* stat, not lstat */
188 !S_ISREG(st.st_mode) ||
189 !(st.st_mode & S_IXUSR))
190 continue;
192 entlen = strlen(de->d_name) - prefix_len;
193 if (has_extension(de->d_name, ".exe"))
194 entlen -= 4;
196 if (longest < entlen)
197 longest = entlen;
199 add_cmdname(cmds, de->d_name + prefix_len, entlen);
201 closedir(dir);
203 return longest;
206 static void list_commands(void)
208 unsigned int longest = 0;
209 unsigned int len;
210 const char *env_path = getenv("PATH");
211 char *paths, *path, *colon;
212 const char *exec_path = git_exec_path();
214 if (exec_path)
215 longest = list_commands_in_dir(&main_cmds, exec_path);
217 if (!env_path) {
218 fprintf(stderr, "PATH not set\n");
219 exit(1);
222 path = paths = xstrdup(env_path);
223 while (1) {
224 if ((colon = strchr(path, ':')))
225 *colon = 0;
227 len = list_commands_in_dir(&other_cmds, path);
228 if (len > longest)
229 longest = len;
231 if (!colon)
232 break;
233 path = colon + 1;
235 free(paths);
237 qsort(main_cmds.names, main_cmds.cnt,
238 sizeof(*main_cmds.names), cmdname_compare);
239 uniq(&main_cmds);
241 qsort(other_cmds.names, other_cmds.cnt,
242 sizeof(*other_cmds.names), cmdname_compare);
243 uniq(&other_cmds);
244 exclude_cmds(&other_cmds, &main_cmds);
246 if (main_cmds.cnt) {
247 printf("available git commands in '%s'\n", exec_path);
248 printf("----------------------------");
249 mput_char('-', strlen(exec_path));
250 putchar('\n');
251 pretty_print_string_list(&main_cmds, longest);
252 putchar('\n');
255 if (other_cmds.cnt) {
256 printf("git commands available from elsewhere on your $PATH\n");
257 printf("---------------------------------------------------\n");
258 pretty_print_string_list(&other_cmds, longest);
259 putchar('\n');
263 void list_common_cmds_help(void)
265 int i, longest = 0;
267 for (i = 0; i < ARRAY_SIZE(common_cmds); i++) {
268 if (longest < strlen(common_cmds[i].name))
269 longest = strlen(common_cmds[i].name);
272 puts("The most commonly used git commands are:");
273 for (i = 0; i < ARRAY_SIZE(common_cmds); i++) {
274 printf(" %s ", common_cmds[i].name);
275 mput_char(' ', longest - strlen(common_cmds[i].name));
276 puts(common_cmds[i].help);
280 static const char *cmd_to_page(const char *git_cmd)
282 if (!git_cmd)
283 return "git";
284 else if (!prefixcmp(git_cmd, "git"))
285 return git_cmd;
286 else {
287 int page_len = strlen(git_cmd) + 4;
288 char *p = xmalloc(page_len + 1);
289 strcpy(p, "git-");
290 strcpy(p + 4, git_cmd);
291 p[page_len] = 0;
292 return p;
296 static void setup_man_path(void)
298 struct strbuf new_path;
299 const char *old_path = getenv("MANPATH");
301 strbuf_init(&new_path, 0);
303 /* We should always put ':' after our path. If there is no
304 * old_path, the ':' at the end will let 'man' to try
305 * system-wide paths after ours to find the manual page. If
306 * there is old_path, we need ':' as delimiter. */
307 strbuf_addstr(&new_path, GIT_MAN_PATH);
308 strbuf_addch(&new_path, ':');
309 if (old_path)
310 strbuf_addstr(&new_path, old_path);
312 setenv("MANPATH", new_path.buf, 1);
314 strbuf_release(&new_path);
317 static void show_man_page(const char *git_cmd)
319 const char *page = cmd_to_page(git_cmd);
320 setup_man_path();
321 execlp("man", "man", page, NULL);
324 static void show_info_page(const char *git_cmd)
326 const char *page = cmd_to_page(git_cmd);
327 setenv("INFOPATH", GIT_INFO_PATH, 1);
328 execlp("info", "info", "gitman", page, NULL);
331 static void show_html_page(const char *git_cmd)
333 const char *page = cmd_to_page(git_cmd);
334 execl_git_cmd("help--browse", page, NULL);
337 void help_unknown_cmd(const char *cmd)
339 fprintf(stderr, "git: '%s' is not a git-command. See 'git --help'.\n", cmd);
340 exit(1);
343 int cmd_version(int argc, const char **argv, const char *prefix)
345 printf("git version %s\n", git_version_string);
346 return 0;
349 int cmd_help(int argc, const char **argv, const char *prefix)
351 const char *help_cmd = argv[1];
353 if (argc < 2) {
354 printf("usage: %s\n\n", git_usage_string);
355 list_common_cmds_help();
356 exit(0);
359 if (!strcmp(help_cmd, "--all") || !strcmp(help_cmd, "-a")) {
360 printf("usage: %s\n\n", git_usage_string);
361 list_commands();
364 else if (!strcmp(help_cmd, "--web") || !strcmp(help_cmd, "-w")) {
365 show_html_page(argc > 2 ? argv[2] : NULL);
368 else if (!strcmp(help_cmd, "--info") || !strcmp(help_cmd, "-i")) {
369 show_info_page(argc > 2 ? argv[2] : NULL);
372 else if (!strcmp(help_cmd, "--man") || !strcmp(help_cmd, "-m")) {
373 show_man_page(argc > 2 ? argv[2] : NULL);
376 else {
377 int nongit;
379 setup_git_directory_gently(&nongit);
380 git_config(git_help_config);
381 if (help_default_format)
382 parse_help_format(help_default_format);
384 switch (help_format) {
385 case man_format:
386 show_man_page(help_cmd);
387 break;
388 case info_format:
389 show_info_page(help_cmd);
390 break;
391 case web_format:
392 show_html_page(help_cmd);
393 break;
397 return 0;