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