Git.pm: Implement options for the command interface
[git/builtin-gsoc.git] / builtin-help.c
blob7470faa56692e17608d202000b78a91565360b72
1 /*
2 * builtin-help.c
4 * Builtin help-related commands (help, usage, version)
5 */
6 #include <sys/ioctl.h>
7 #include "cache.h"
8 #include "builtin.h"
9 #include "exec_cmd.h"
10 #include "common-cmds.h"
12 static const char git_usage[] =
13 "Usage: git [--version] [--exec-path[=GIT_EXEC_PATH]] [--help] COMMAND [ ARGS ]";
15 /* most gui terms set COLUMNS (although some don't export it) */
16 static int term_columns(void)
18 char *col_string = getenv("COLUMNS");
19 int n_cols = 0;
21 if (col_string && (n_cols = atoi(col_string)) > 0)
22 return n_cols;
24 #ifdef TIOCGWINSZ
26 struct winsize ws;
27 if (!ioctl(1, TIOCGWINSZ, &ws)) {
28 if (ws.ws_col)
29 return ws.ws_col;
32 #endif
34 return 80;
37 static void oom(void)
39 fprintf(stderr, "git: out of memory\n");
40 exit(1);
43 static inline void mput_char(char c, unsigned int num)
45 while(num--)
46 putchar(c);
49 static struct cmdname {
50 size_t len;
51 char name[1];
52 } **cmdname;
53 static int cmdname_alloc, cmdname_cnt;
55 static void add_cmdname(const char *name, int len)
57 struct cmdname *ent;
58 if (cmdname_alloc <= cmdname_cnt) {
59 cmdname_alloc = cmdname_alloc + 200;
60 cmdname = realloc(cmdname, cmdname_alloc * sizeof(*cmdname));
61 if (!cmdname)
62 oom();
64 ent = malloc(sizeof(*ent) + len);
65 if (!ent)
66 oom();
67 ent->len = len;
68 memcpy(ent->name, name, len);
69 ent->name[len] = 0;
70 cmdname[cmdname_cnt++] = ent;
73 static int cmdname_compare(const void *a_, const void *b_)
75 struct cmdname *a = *(struct cmdname **)a_;
76 struct cmdname *b = *(struct cmdname **)b_;
77 return strcmp(a->name, b->name);
80 static void pretty_print_string_list(struct cmdname **cmdname, int longest)
82 int cols = 1, rows;
83 int space = longest + 1; /* min 1 SP between words */
84 int max_cols = term_columns() - 1; /* don't print *on* the edge */
85 int i, j;
87 if (space < max_cols)
88 cols = max_cols / space;
89 rows = (cmdname_cnt + cols - 1) / cols;
91 qsort(cmdname, cmdname_cnt, sizeof(*cmdname), cmdname_compare);
93 for (i = 0; i < rows; i++) {
94 printf(" ");
96 for (j = 0; j < cols; j++) {
97 int n = j * rows + i;
98 int size = space;
99 if (n >= cmdname_cnt)
100 break;
101 if (j == cols-1 || n + rows >= cmdname_cnt)
102 size = 1;
103 printf("%-*s", size, cmdname[n]->name);
105 putchar('\n');
109 static void list_commands(const char *exec_path, const char *pattern)
111 unsigned int longest = 0;
112 char path[PATH_MAX];
113 int dirlen;
114 DIR *dir = opendir(exec_path);
115 struct dirent *de;
117 if (!dir) {
118 fprintf(stderr, "git: '%s': %s\n", exec_path, strerror(errno));
119 exit(1);
122 dirlen = strlen(exec_path);
123 if (PATH_MAX - 20 < dirlen) {
124 fprintf(stderr, "git: insanely long exec-path '%s'\n",
125 exec_path);
126 exit(1);
129 memcpy(path, exec_path, dirlen);
130 path[dirlen++] = '/';
132 while ((de = readdir(dir)) != NULL) {
133 struct stat st;
134 int entlen;
136 if (strncmp(de->d_name, "git-", 4))
137 continue;
138 strcpy(path+dirlen, de->d_name);
139 if (stat(path, &st) || /* stat, not lstat */
140 !S_ISREG(st.st_mode) ||
141 !(st.st_mode & S_IXUSR))
142 continue;
144 entlen = strlen(de->d_name);
145 if (4 < entlen && !strcmp(de->d_name + entlen - 4, ".exe"))
146 entlen -= 4;
148 if (longest < entlen)
149 longest = entlen;
151 add_cmdname(de->d_name + 4, entlen-4);
153 closedir(dir);
155 printf("git commands available in '%s'\n", exec_path);
156 printf("----------------------------");
157 mput_char('-', strlen(exec_path));
158 putchar('\n');
159 pretty_print_string_list(cmdname, longest - 4);
160 putchar('\n');
163 static void list_common_cmds_help(void)
165 int i, longest = 0;
167 for (i = 0; i < ARRAY_SIZE(common_cmds); i++) {
168 if (longest < strlen(common_cmds[i].name))
169 longest = strlen(common_cmds[i].name);
172 puts("The most commonly used git commands are:");
173 for (i = 0; i < ARRAY_SIZE(common_cmds); i++) {
174 printf(" %s", common_cmds[i].name);
175 mput_char(' ', longest - strlen(common_cmds[i].name) + 4);
176 puts(common_cmds[i].help);
178 puts("(use 'git help -a' to get a list of all installed git commands)");
181 void cmd_usage(int show_all, const char *exec_path, const char *fmt, ...)
183 if (fmt) {
184 va_list ap;
186 va_start(ap, fmt);
187 printf("git: ");
188 vprintf(fmt, ap);
189 va_end(ap);
190 putchar('\n');
192 else
193 puts(git_usage);
195 if (exec_path) {
196 putchar('\n');
197 if (show_all)
198 list_commands(exec_path, "git-*");
199 else
200 list_common_cmds_help();
203 exit(1);
206 static void show_man_page(const char *git_cmd)
208 const char *page;
210 if (!strncmp(git_cmd, "git", 3))
211 page = git_cmd;
212 else {
213 int page_len = strlen(git_cmd) + 4;
214 char *p = malloc(page_len + 1);
215 strcpy(p, "git-");
216 strcpy(p + 4, git_cmd);
217 p[page_len] = 0;
218 page = p;
221 execlp("man", "man", page, NULL);
224 int cmd_version(int argc, const char **argv, char **envp)
226 printf("git version %s\n", git_version_string);
227 return 0;
230 int cmd_help(int argc, const char **argv, char **envp)
232 const char *help_cmd = argv[1];
233 if (!help_cmd)
234 cmd_usage(0, git_exec_path(), NULL);
235 else if (!strcmp(help_cmd, "--all") || !strcmp(help_cmd, "-a"))
236 cmd_usage(1, git_exec_path(), NULL);
237 else
238 show_man_page(help_cmd);
239 return 0;