builtin-help.con commit test-genrandom: ensure stdout is set to _O_BINARY on Windows (a16753d)
   1/*
   2 * builtin-help.c
   3 *
   4 * Builtin help command
   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"
  12#include "help.h"
  13
  14static struct man_viewer_list {
  15        struct man_viewer_list *next;
  16        char name[FLEX_ARRAY];
  17} *man_viewer_list;
  18
  19static struct man_viewer_info_list {
  20        struct man_viewer_info_list *next;
  21        const char *info;
  22        char name[FLEX_ARRAY];
  23} *man_viewer_info_list;
  24
  25enum help_format {
  26        HELP_FORMAT_MAN,
  27        HELP_FORMAT_INFO,
  28        HELP_FORMAT_WEB,
  29};
  30
  31static int show_all = 0;
  32static enum help_format help_format = HELP_FORMAT_MAN;
  33static struct option builtin_help_options[] = {
  34        OPT_BOOLEAN('a', "all", &show_all, "print all available commands"),
  35        OPT_SET_INT('m', "man", &help_format, "show man page", HELP_FORMAT_MAN),
  36        OPT_SET_INT('w', "web", &help_format, "show manual in web browser",
  37                        HELP_FORMAT_WEB),
  38        OPT_SET_INT('i', "info", &help_format, "show info page",
  39                        HELP_FORMAT_INFO),
  40        OPT_END(),
  41};
  42
  43static const char * const builtin_help_usage[] = {
  44        "git help [--all] [--man|--web|--info] [command]",
  45        NULL
  46};
  47
  48static enum help_format parse_help_format(const char *format)
  49{
  50        if (!strcmp(format, "man"))
  51                return HELP_FORMAT_MAN;
  52        if (!strcmp(format, "info"))
  53                return HELP_FORMAT_INFO;
  54        if (!strcmp(format, "web") || !strcmp(format, "html"))
  55                return HELP_FORMAT_WEB;
  56        die("unrecognized help format '%s'", format);
  57}
  58
  59static const char *get_man_viewer_info(const char *name)
  60{
  61        struct man_viewer_info_list *viewer;
  62
  63        for (viewer = man_viewer_info_list; viewer; viewer = viewer->next)
  64        {
  65                if (!strcasecmp(name, viewer->name))
  66                        return viewer->info;
  67        }
  68        return NULL;
  69}
  70
  71static int check_emacsclient_version(void)
  72{
  73        struct strbuf buffer = STRBUF_INIT;
  74        struct child_process ec_process;
  75        const char *argv_ec[] = { "emacsclient", "--version", NULL };
  76        int version;
  77
  78        /* emacsclient prints its version number on stderr */
  79        memset(&ec_process, 0, sizeof(ec_process));
  80        ec_process.argv = argv_ec;
  81        ec_process.err = -1;
  82        ec_process.stdout_to_stderr = 1;
  83        if (start_command(&ec_process))
  84                return error("Failed to start emacsclient.");
  85
  86        strbuf_read(&buffer, ec_process.err, 20);
  87        close(ec_process.err);
  88
  89        /*
  90         * Don't bother checking return value, because "emacsclient --version"
  91         * seems to always exits with code 1.
  92         */
  93        finish_command(&ec_process);
  94
  95        if (prefixcmp(buffer.buf, "emacsclient")) {
  96                strbuf_release(&buffer);
  97                return error("Failed to parse emacsclient version.");
  98        }
  99
 100        strbuf_remove(&buffer, 0, strlen("emacsclient"));
 101        version = atoi(buffer.buf);
 102
 103        if (version < 22) {
 104                strbuf_release(&buffer);
 105                return error("emacsclient version '%d' too old (< 22).",
 106                        version);
 107        }
 108
 109        strbuf_release(&buffer);
 110        return 0;
 111}
 112
 113static void exec_woman_emacs(const char *path, const char *page)
 114{
 115        if (!check_emacsclient_version()) {
 116                /* This works only with emacsclient version >= 22. */
 117                struct strbuf man_page = STRBUF_INIT;
 118
 119                if (!path)
 120                        path = "emacsclient";
 121                strbuf_addf(&man_page, "(woman \"%s\")", page);
 122                execlp(path, "emacsclient", "-e", man_page.buf, NULL);
 123                warning("failed to exec '%s': %s", path, strerror(errno));
 124        }
 125}
 126
 127static void exec_man_konqueror(const char *path, const char *page)
 128{
 129        const char *display = getenv("DISPLAY");
 130        if (display && *display) {
 131                struct strbuf man_page = STRBUF_INIT;
 132                const char *filename = "kfmclient";
 133
 134                /* It's simpler to launch konqueror using kfmclient. */
 135                if (path) {
 136                        const char *file = strrchr(path, '/');
 137                        if (file && !strcmp(file + 1, "konqueror")) {
 138                                char *new = xstrdup(path);
 139                                char *dest = strrchr(new, '/');
 140
 141                                /* strlen("konqueror") == strlen("kfmclient") */
 142                                strcpy(dest + 1, "kfmclient");
 143                                path = new;
 144                        }
 145                        if (file)
 146                                filename = file;
 147                } else
 148                        path = "kfmclient";
 149                strbuf_addf(&man_page, "man:%s(1)", page);
 150                execlp(path, filename, "newTab", man_page.buf, NULL);
 151                warning("failed to exec '%s': %s", path, strerror(errno));
 152        }
 153}
 154
 155static void exec_man_man(const char *path, const char *page)
 156{
 157        if (!path)
 158                path = "man";
 159        execlp(path, "man", page, NULL);
 160        warning("failed to exec '%s': %s", path, strerror(errno));
 161}
 162
 163static void exec_man_cmd(const char *cmd, const char *page)
 164{
 165        struct strbuf shell_cmd = STRBUF_INIT;
 166        strbuf_addf(&shell_cmd, "%s %s", cmd, page);
 167        execl("/bin/sh", "sh", "-c", shell_cmd.buf, NULL);
 168        warning("failed to exec '%s': %s", cmd, strerror(errno));
 169}
 170
 171static void add_man_viewer(const char *name)
 172{
 173        struct man_viewer_list **p = &man_viewer_list;
 174        size_t len = strlen(name);
 175
 176        while (*p)
 177                p = &((*p)->next);
 178        *p = xcalloc(1, (sizeof(**p) + len + 1));
 179        strncpy((*p)->name, name, len);
 180}
 181
 182static int supported_man_viewer(const char *name, size_t len)
 183{
 184        return (!strncasecmp("man", name, len) ||
 185                !strncasecmp("woman", name, len) ||
 186                !strncasecmp("konqueror", name, len));
 187}
 188
 189static void do_add_man_viewer_info(const char *name,
 190                                   size_t len,
 191                                   const char *value)
 192{
 193        struct man_viewer_info_list *new = xcalloc(1, sizeof(*new) + len + 1);
 194
 195        strncpy(new->name, name, len);
 196        new->info = xstrdup(value);
 197        new->next = man_viewer_info_list;
 198        man_viewer_info_list = new;
 199}
 200
 201static int add_man_viewer_path(const char *name,
 202                               size_t len,
 203                               const char *value)
 204{
 205        if (supported_man_viewer(name, len))
 206                do_add_man_viewer_info(name, len, value);
 207        else
 208                warning("'%s': path for unsupported man viewer.\n"
 209                        "Please consider using 'man.<tool>.cmd' instead.",
 210                        name);
 211
 212        return 0;
 213}
 214
 215static int add_man_viewer_cmd(const char *name,
 216                              size_t len,
 217                              const char *value)
 218{
 219        if (supported_man_viewer(name, len))
 220                warning("'%s': cmd for supported man viewer.\n"
 221                        "Please consider using 'man.<tool>.path' instead.",
 222                        name);
 223        else
 224                do_add_man_viewer_info(name, len, value);
 225
 226        return 0;
 227}
 228
 229static int add_man_viewer_info(const char *var, const char *value)
 230{
 231        const char *name = var + 4;
 232        const char *subkey = strrchr(name, '.');
 233
 234        if (!subkey)
 235                return 0;
 236
 237        if (!strcmp(subkey, ".path")) {
 238                if (!value)
 239                        return config_error_nonbool(var);
 240                return add_man_viewer_path(name, subkey - name, value);
 241        }
 242        if (!strcmp(subkey, ".cmd")) {
 243                if (!value)
 244                        return config_error_nonbool(var);
 245                return add_man_viewer_cmd(name, subkey - name, value);
 246        }
 247
 248        return 0;
 249}
 250
 251static int git_help_config(const char *var, const char *value, void *cb)
 252{
 253        if (!strcmp(var, "help.format")) {
 254                if (!value)
 255                        return config_error_nonbool(var);
 256                help_format = parse_help_format(value);
 257                return 0;
 258        }
 259        if (!strcmp(var, "man.viewer")) {
 260                if (!value)
 261                        return config_error_nonbool(var);
 262                add_man_viewer(value);
 263                return 0;
 264        }
 265        if (!prefixcmp(var, "man."))
 266                return add_man_viewer_info(var, value);
 267
 268        return git_default_config(var, value, cb);
 269}
 270
 271static struct cmdnames main_cmds, other_cmds;
 272
 273void list_common_cmds_help(void)
 274{
 275        int i, longest = 0;
 276
 277        for (i = 0; i < ARRAY_SIZE(common_cmds); i++) {
 278                if (longest < strlen(common_cmds[i].name))
 279                        longest = strlen(common_cmds[i].name);
 280        }
 281
 282        puts("The most commonly used git commands are:");
 283        for (i = 0; i < ARRAY_SIZE(common_cmds); i++) {
 284                printf("   %s   ", common_cmds[i].name);
 285                mput_char(' ', longest - strlen(common_cmds[i].name));
 286                puts(common_cmds[i].help);
 287        }
 288}
 289
 290static int is_git_command(const char *s)
 291{
 292        return is_in_cmdlist(&main_cmds, s) ||
 293                is_in_cmdlist(&other_cmds, s);
 294}
 295
 296static const char *prepend(const char *prefix, const char *cmd)
 297{
 298        size_t pre_len = strlen(prefix);
 299        size_t cmd_len = strlen(cmd);
 300        char *p = xmalloc(pre_len + cmd_len + 1);
 301        memcpy(p, prefix, pre_len);
 302        strcpy(p + pre_len, cmd);
 303        return p;
 304}
 305
 306static const char *cmd_to_page(const char *git_cmd)
 307{
 308        if (!git_cmd)
 309                return "git";
 310        else if (!prefixcmp(git_cmd, "git"))
 311                return git_cmd;
 312        else if (is_git_command(git_cmd))
 313                return prepend("git-", git_cmd);
 314        else
 315                return prepend("git", git_cmd);
 316}
 317
 318static void setup_man_path(void)
 319{
 320        struct strbuf new_path = STRBUF_INIT;
 321        const char *old_path = getenv("MANPATH");
 322
 323        /* We should always put ':' after our path. If there is no
 324         * old_path, the ':' at the end will let 'man' to try
 325         * system-wide paths after ours to find the manual page. If
 326         * there is old_path, we need ':' as delimiter. */
 327        strbuf_addstr(&new_path, system_path(GIT_MAN_PATH));
 328        strbuf_addch(&new_path, ':');
 329        if (old_path)
 330                strbuf_addstr(&new_path, old_path);
 331
 332        setenv("MANPATH", new_path.buf, 1);
 333
 334        strbuf_release(&new_path);
 335}
 336
 337static void exec_viewer(const char *name, const char *page)
 338{
 339        const char *info = get_man_viewer_info(name);
 340
 341        if (!strcasecmp(name, "man"))
 342                exec_man_man(info, page);
 343        else if (!strcasecmp(name, "woman"))
 344                exec_woman_emacs(info, page);
 345        else if (!strcasecmp(name, "konqueror"))
 346                exec_man_konqueror(info, page);
 347        else if (info)
 348                exec_man_cmd(info, page);
 349        else
 350                warning("'%s': unknown man viewer.", name);
 351}
 352
 353static void show_man_page(const char *git_cmd)
 354{
 355        struct man_viewer_list *viewer;
 356        const char *page = cmd_to_page(git_cmd);
 357        const char *fallback = getenv("GIT_MAN_VIEWER");
 358
 359        setup_man_path();
 360        for (viewer = man_viewer_list; viewer; viewer = viewer->next)
 361        {
 362                exec_viewer(viewer->name, page); /* will return when unable */
 363        }
 364        if (fallback)
 365                exec_viewer(fallback, page);
 366        exec_viewer("man", page);
 367        die("no man viewer handled the request");
 368}
 369
 370static void show_info_page(const char *git_cmd)
 371{
 372        const char *page = cmd_to_page(git_cmd);
 373        setenv("INFOPATH", system_path(GIT_INFO_PATH), 1);
 374        execlp("info", "info", "gitman", page, NULL);
 375}
 376
 377static void get_html_page_path(struct strbuf *page_path, const char *page)
 378{
 379        struct stat st;
 380        const char *html_path = system_path(GIT_HTML_PATH);
 381
 382        /* Check that we have a git documentation directory. */
 383        if (stat(mkpath("%s/git.html", html_path), &st)
 384            || !S_ISREG(st.st_mode))
 385                die("'%s': not a documentation directory.", html_path);
 386
 387        strbuf_init(page_path, 0);
 388        strbuf_addf(page_path, "%s/%s.html", html_path, page);
 389}
 390
 391/*
 392 * If open_html is not defined in a platform-specific way (see for
 393 * example compat/mingw.h), we use the script web--browse to display
 394 * HTML.
 395 */
 396#ifndef open_html
 397static void open_html(const char *path)
 398{
 399        execl_git_cmd("web--browse", "-c", "help.browser", path, NULL);
 400}
 401#endif
 402
 403static void show_html_page(const char *git_cmd)
 404{
 405        const char *page = cmd_to_page(git_cmd);
 406        struct strbuf page_path; /* it leaks but we exec bellow */
 407
 408        get_html_page_path(&page_path, page);
 409
 410        open_html(page_path.buf);
 411}
 412
 413int cmd_help(int argc, const char **argv, const char *prefix)
 414{
 415        int nongit;
 416        const char *alias;
 417        load_command_list("git-", &main_cmds, &other_cmds);
 418
 419        setup_git_directory_gently(&nongit);
 420        git_config(git_help_config, NULL);
 421
 422        argc = parse_options(argc, argv, prefix, builtin_help_options,
 423                        builtin_help_usage, 0);
 424
 425        if (show_all) {
 426                printf("usage: %s\n\n", git_usage_string);
 427                list_commands("git commands", &main_cmds, &other_cmds);
 428                printf("%s\n", git_more_info_string);
 429                return 0;
 430        }
 431
 432        if (!argv[0]) {
 433                printf("usage: %s\n\n", git_usage_string);
 434                list_common_cmds_help();
 435                printf("\n%s\n", git_more_info_string);
 436                return 0;
 437        }
 438
 439        alias = alias_lookup(argv[0]);
 440        if (alias && !is_git_command(argv[0])) {
 441                printf("`git %s' is aliased to `%s'\n", argv[0], alias);
 442                return 0;
 443        }
 444
 445        switch (help_format) {
 446        case HELP_FORMAT_MAN:
 447                show_man_page(argv[0]);
 448                break;
 449        case HELP_FORMAT_INFO:
 450                show_info_page(argv[0]);
 451                break;
 452        case HELP_FORMAT_WEB:
 453                show_html_page(argv[0]);
 454                break;
 455        }
 456
 457        return 0;
 458}