help.con commit list_commands(): simplify code by using chdir() (0966003)
   1/*
   2 * builtin-help.c
   3 *
   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 <sys/ioctl.h>
  11
  12/* most GUI terminals set COLUMNS (although some don't export it) */
  13static int term_columns(void)
  14{
  15        char *col_string = getenv("COLUMNS");
  16        int n_cols;
  17
  18        if (col_string && (n_cols = atoi(col_string)) > 0)
  19                return n_cols;
  20
  21#ifdef TIOCGWINSZ
  22        {
  23                struct winsize ws;
  24                if (!ioctl(1, TIOCGWINSZ, &ws)) {
  25                        if (ws.ws_col)
  26                                return ws.ws_col;
  27                }
  28        }
  29#endif
  30
  31        return 80;
  32}
  33
  34static inline void mput_char(char c, unsigned int num)
  35{
  36        while(num--)
  37                putchar(c);
  38}
  39
  40static struct cmdname {
  41        size_t len;
  42        char name[1];
  43} **cmdname;
  44static int cmdname_alloc, cmdname_cnt;
  45
  46static void add_cmdname(const char *name, int len)
  47{
  48        struct cmdname *ent;
  49        if (cmdname_alloc <= cmdname_cnt) {
  50                cmdname_alloc = cmdname_alloc + 200;
  51                cmdname = xrealloc(cmdname, cmdname_alloc * sizeof(*cmdname));
  52        }
  53        ent = xmalloc(sizeof(*ent) + len);
  54        ent->len = len;
  55        memcpy(ent->name, name, len);
  56        ent->name[len] = 0;
  57        cmdname[cmdname_cnt++] = ent;
  58}
  59
  60static int cmdname_compare(const void *a_, const void *b_)
  61{
  62        struct cmdname *a = *(struct cmdname **)a_;
  63        struct cmdname *b = *(struct cmdname **)b_;
  64        return strcmp(a->name, b->name);
  65}
  66
  67static void pretty_print_string_list(struct cmdname **cmdname, int longest)
  68{
  69        int cols = 1, rows;
  70        int space = longest + 1; /* min 1 SP between words */
  71        int max_cols = term_columns() - 1; /* don't print *on* the edge */
  72        int i, j;
  73
  74        if (space < max_cols)
  75                cols = max_cols / space;
  76        rows = (cmdname_cnt + cols - 1) / cols;
  77
  78        qsort(cmdname, cmdname_cnt, sizeof(*cmdname), cmdname_compare);
  79
  80        for (i = 0; i < rows; i++) {
  81                printf("  ");
  82
  83                for (j = 0; j < cols; j++) {
  84                        int n = j * rows + i;
  85                        int size = space;
  86                        if (n >= cmdname_cnt)
  87                                break;
  88                        if (j == cols-1 || n + rows >= cmdname_cnt)
  89                                size = 1;
  90                        printf("%-*s", size, cmdname[n]->name);
  91                }
  92                putchar('\n');
  93        }
  94}
  95
  96static void list_commands(const char *exec_path)
  97{
  98        unsigned int longest = 0;
  99        const char *prefix = "git-";
 100        int prefix_len = strlen(prefix);
 101        DIR *dir = opendir(exec_path);
 102        struct dirent *de;
 103
 104        if (!dir || chdir(exec_path)) {
 105                fprintf(stderr, "git: '%s': %s\n", exec_path, strerror(errno));
 106                exit(1);
 107        }
 108
 109        while ((de = readdir(dir)) != NULL) {
 110                struct stat st;
 111                int entlen;
 112
 113                if (prefixcmp(de->d_name, prefix))
 114                        continue;
 115
 116                if (stat(de->d_name, &st) || /* stat, not lstat */
 117                    !S_ISREG(st.st_mode) ||
 118                    !(st.st_mode & S_IXUSR))
 119                        continue;
 120
 121                entlen = strlen(de->d_name) - prefix_len;
 122                if (has_extension(de->d_name, ".exe"))
 123                        entlen -= 4;
 124
 125                if (longest < entlen)
 126                        longest = entlen;
 127
 128                add_cmdname(de->d_name + prefix_len, entlen);
 129        }
 130        closedir(dir);
 131
 132        printf("git commands available in '%s'\n", exec_path);
 133        printf("----------------------------");
 134        mput_char('-', strlen(exec_path));
 135        putchar('\n');
 136        pretty_print_string_list(cmdname, longest);
 137        putchar('\n');
 138}
 139
 140void list_common_cmds_help(void)
 141{
 142        int i, longest = 0;
 143
 144        for (i = 0; i < ARRAY_SIZE(common_cmds); i++) {
 145                if (longest < strlen(common_cmds[i].name))
 146                        longest = strlen(common_cmds[i].name);
 147        }
 148
 149        puts("The most commonly used git commands are:");
 150        for (i = 0; i < ARRAY_SIZE(common_cmds); i++) {
 151                printf("   %s   ", common_cmds[i].name);
 152                mput_char(' ', longest - strlen(common_cmds[i].name));
 153                puts(common_cmds[i].help);
 154        }
 155        puts("(use 'git help -a' to get a list of all installed git commands)");
 156}
 157
 158static void show_man_page(const char *git_cmd)
 159{
 160        const char *page;
 161
 162        if (!prefixcmp(git_cmd, "git"))
 163                page = git_cmd;
 164        else {
 165                int page_len = strlen(git_cmd) + 4;
 166                char *p = xmalloc(page_len + 1);
 167                strcpy(p, "git-");
 168                strcpy(p + 4, git_cmd);
 169                p[page_len] = 0;
 170                page = p;
 171        }
 172
 173        execlp("man", "man", page, NULL);
 174}
 175
 176void help_unknown_cmd(const char *cmd)
 177{
 178        fprintf(stderr, "git: '%s' is not a git-command. See 'git --help'.\n", cmd);
 179        exit(1);
 180}
 181
 182int cmd_version(int argc, const char **argv, const char *prefix)
 183{
 184        printf("git version %s\n", git_version_string);
 185        return 0;
 186}
 187
 188int cmd_help(int argc, const char **argv, const char *prefix)
 189{
 190        const char *help_cmd = argc > 1 ? argv[1] : NULL;
 191        const char *exec_path = git_exec_path();
 192
 193        if (!help_cmd) {
 194                printf("usage: %s\n\n", git_usage_string);
 195                list_common_cmds_help();
 196                exit(0);
 197        }
 198
 199        else if (!strcmp(help_cmd, "--all") || !strcmp(help_cmd, "-a")) {
 200                printf("usage: %s\n\n", git_usage_string);
 201                if(exec_path)
 202                        list_commands(exec_path);
 203                exit(0);
 204        }
 205
 206        else
 207                show_man_page(help_cmd);
 208
 209        return 0;
 210}