builtin / help.con commit Merge branch 'sg/daemon-test-signal-fix' (97b6d63)
   1/*
   2 * Builtin help command
   3 */
   4#include "cache.h"
   5#include "config.h"
   6#include "builtin.h"
   7#include "exec-cmd.h"
   8#include "parse-options.h"
   9#include "run-command.h"
  10#include "column.h"
  11#include "help.h"
  12#include "alias.h"
  13
  14#ifndef DEFAULT_HELP_FORMAT
  15#define DEFAULT_HELP_FORMAT "man"
  16#endif
  17
  18static struct man_viewer_list {
  19        struct man_viewer_list *next;
  20        char name[FLEX_ARRAY];
  21} *man_viewer_list;
  22
  23static struct man_viewer_info_list {
  24        struct man_viewer_info_list *next;
  25        const char *info;
  26        char name[FLEX_ARRAY];
  27} *man_viewer_info_list;
  28
  29enum help_format {
  30        HELP_FORMAT_NONE,
  31        HELP_FORMAT_MAN,
  32        HELP_FORMAT_INFO,
  33        HELP_FORMAT_WEB
  34};
  35
  36static const char *html_path;
  37
  38static int show_all = 0;
  39static int show_guides = 0;
  40static int show_config;
  41static int verbose = 1;
  42static unsigned int colopts;
  43static enum help_format help_format = HELP_FORMAT_NONE;
  44static int exclude_guides;
  45static struct option builtin_help_options[] = {
  46        OPT_BOOL('a', "all", &show_all, N_("print all available commands")),
  47        OPT_HIDDEN_BOOL(0, "exclude-guides", &exclude_guides, N_("exclude guides")),
  48        OPT_BOOL('g', "guides", &show_guides, N_("print list of useful guides")),
  49        OPT_BOOL('c', "config", &show_config, N_("print all configuration variable names")),
  50        OPT_SET_INT_F(0, "config-for-completion", &show_config, "", 2, PARSE_OPT_HIDDEN),
  51        OPT_SET_INT('m', "man", &help_format, N_("show man page"), HELP_FORMAT_MAN),
  52        OPT_SET_INT('w', "web", &help_format, N_("show manual in web browser"),
  53                        HELP_FORMAT_WEB),
  54        OPT_SET_INT('i', "info", &help_format, N_("show info page"),
  55                        HELP_FORMAT_INFO),
  56        OPT__VERBOSE(&verbose, N_("print command description")),
  57        OPT_END(),
  58};
  59
  60static const char * const builtin_help_usage[] = {
  61        N_("git help [--all] [--guides] [--man | --web | --info] [<command>]"),
  62        NULL
  63};
  64
  65static enum help_format parse_help_format(const char *format)
  66{
  67        if (!strcmp(format, "man"))
  68                return HELP_FORMAT_MAN;
  69        if (!strcmp(format, "info"))
  70                return HELP_FORMAT_INFO;
  71        if (!strcmp(format, "web") || !strcmp(format, "html"))
  72                return HELP_FORMAT_WEB;
  73        die(_("unrecognized help format '%s'"), format);
  74}
  75
  76static const char *get_man_viewer_info(const char *name)
  77{
  78        struct man_viewer_info_list *viewer;
  79
  80        for (viewer = man_viewer_info_list; viewer; viewer = viewer->next)
  81        {
  82                if (!strcasecmp(name, viewer->name))
  83                        return viewer->info;
  84        }
  85        return NULL;
  86}
  87
  88static int check_emacsclient_version(void)
  89{
  90        struct strbuf buffer = STRBUF_INIT;
  91        struct child_process ec_process = CHILD_PROCESS_INIT;
  92        const char *argv_ec[] = { "emacsclient", "--version", NULL };
  93        int version;
  94
  95        /* emacsclient prints its version number on stderr */
  96        ec_process.argv = argv_ec;
  97        ec_process.err = -1;
  98        ec_process.stdout_to_stderr = 1;
  99        if (start_command(&ec_process))
 100                return error(_("Failed to start emacsclient."));
 101
 102        strbuf_read(&buffer, ec_process.err, 20);
 103        close(ec_process.err);
 104
 105        /*
 106         * Don't bother checking return value, because "emacsclient --version"
 107         * seems to always exits with code 1.
 108         */
 109        finish_command(&ec_process);
 110
 111        if (!starts_with(buffer.buf, "emacsclient")) {
 112                strbuf_release(&buffer);
 113                return error(_("Failed to parse emacsclient version."));
 114        }
 115
 116        strbuf_remove(&buffer, 0, strlen("emacsclient"));
 117        version = atoi(buffer.buf);
 118
 119        if (version < 22) {
 120                strbuf_release(&buffer);
 121                return error(_("emacsclient version '%d' too old (< 22)."),
 122                        version);
 123        }
 124
 125        strbuf_release(&buffer);
 126        return 0;
 127}
 128
 129static void exec_woman_emacs(const char *path, const char *page)
 130{
 131        if (!check_emacsclient_version()) {
 132                /* This works only with emacsclient version >= 22. */
 133                struct strbuf man_page = STRBUF_INIT;
 134
 135                if (!path)
 136                        path = "emacsclient";
 137                strbuf_addf(&man_page, "(woman \"%s\")", page);
 138                execlp(path, "emacsclient", "-e", man_page.buf, (char *)NULL);
 139                warning_errno(_("failed to exec '%s'"), path);
 140                strbuf_release(&man_page);
 141        }
 142}
 143
 144static void exec_man_konqueror(const char *path, const char *page)
 145{
 146        const char *display = getenv("DISPLAY");
 147        if (display && *display) {
 148                struct strbuf man_page = STRBUF_INIT;
 149                const char *filename = "kfmclient";
 150
 151                /* It's simpler to launch konqueror using kfmclient. */
 152                if (path) {
 153                        size_t len;
 154                        if (strip_suffix(path, "/konqueror", &len))
 155                                path = xstrfmt("%.*s/kfmclient", (int)len, path);
 156                        filename = basename((char *)path);
 157                } else
 158                        path = "kfmclient";
 159                strbuf_addf(&man_page, "man:%s(1)", page);
 160                execlp(path, filename, "newTab", man_page.buf, (char *)NULL);
 161                warning_errno(_("failed to exec '%s'"), path);
 162                strbuf_release(&man_page);
 163        }
 164}
 165
 166static void exec_man_man(const char *path, const char *page)
 167{
 168        if (!path)
 169                path = "man";
 170        execlp(path, "man", page, (char *)NULL);
 171        warning_errno(_("failed to exec '%s'"), path);
 172}
 173
 174static void exec_man_cmd(const char *cmd, const char *page)
 175{
 176        struct strbuf shell_cmd = STRBUF_INIT;
 177        strbuf_addf(&shell_cmd, "%s %s", cmd, page);
 178        execl(SHELL_PATH, SHELL_PATH, "-c", shell_cmd.buf, (char *)NULL);
 179        warning(_("failed to exec '%s'"), cmd);
 180        strbuf_release(&shell_cmd);
 181}
 182
 183static void add_man_viewer(const char *name)
 184{
 185        struct man_viewer_list **p = &man_viewer_list;
 186
 187        while (*p)
 188                p = &((*p)->next);
 189        FLEX_ALLOC_STR(*p, name, name);
 190}
 191
 192static int supported_man_viewer(const char *name, size_t len)
 193{
 194        return (!strncasecmp("man", name, len) ||
 195                !strncasecmp("woman", name, len) ||
 196                !strncasecmp("konqueror", name, len));
 197}
 198
 199static void do_add_man_viewer_info(const char *name,
 200                                   size_t len,
 201                                   const char *value)
 202{
 203        struct man_viewer_info_list *new_man_viewer;
 204        FLEX_ALLOC_MEM(new_man_viewer, name, name, len);
 205        new_man_viewer->info = xstrdup(value);
 206        new_man_viewer->next = man_viewer_info_list;
 207        man_viewer_info_list = new_man_viewer;
 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, *subkey;
 241        int namelen;
 242
 243        if (parse_config_key(var, "man", &name, &namelen, &subkey) < 0 || !name)
 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, namelen, value);
 250        }
 251        if (!strcmp(subkey, "cmd")) {
 252                if (!value)
 253                        return config_error_nonbool(var);
 254                return add_man_viewer_cmd(name, namelen, value);
 255        }
 256
 257        return 0;
 258}
 259
 260static int git_help_config(const char *var, const char *value, void *cb)
 261{
 262        if (starts_with(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 (starts_with(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
 290static int is_git_command(const char *s)
 291{
 292        if (is_builtin(s))
 293                return 1;
 294
 295        load_command_list("git-", &main_cmds, &other_cmds);
 296        return is_in_cmdlist(&main_cmds, s) ||
 297                is_in_cmdlist(&other_cmds, s);
 298}
 299
 300static const char *cmd_to_page(const char *git_cmd)
 301{
 302        if (!git_cmd)
 303                return "git";
 304        else if (starts_with(git_cmd, "git"))
 305                return git_cmd;
 306        else if (is_git_command(git_cmd))
 307                return xstrfmt("git-%s", git_cmd);
 308        else
 309                return xstrfmt("git%s", git_cmd);
 310}
 311
 312static void setup_man_path(void)
 313{
 314        struct strbuf new_path = STRBUF_INIT;
 315        const char *old_path = getenv("MANPATH");
 316        char *git_man_path = system_path(GIT_MAN_PATH);
 317
 318        /* We should always put ':' after our path. If there is no
 319         * old_path, the ':' at the end will let 'man' to try
 320         * system-wide paths after ours to find the manual page. If
 321         * there is old_path, we need ':' as delimiter. */
 322        strbuf_addstr(&new_path, git_man_path);
 323        strbuf_addch(&new_path, ':');
 324        if (old_path)
 325                strbuf_addstr(&new_path, old_path);
 326
 327        free(git_man_path);
 328        setenv("MANPATH", new_path.buf, 1);
 329
 330        strbuf_release(&new_path);
 331}
 332
 333static void exec_viewer(const char *name, const char *page)
 334{
 335        const char *info = get_man_viewer_info(name);
 336
 337        if (!strcasecmp(name, "man"))
 338                exec_man_man(info, page);
 339        else if (!strcasecmp(name, "woman"))
 340                exec_woman_emacs(info, page);
 341        else if (!strcasecmp(name, "konqueror"))
 342                exec_man_konqueror(info, page);
 343        else if (info)
 344                exec_man_cmd(info, page);
 345        else
 346                warning(_("'%s': unknown man viewer."), name);
 347}
 348
 349static void show_man_page(const char *git_cmd)
 350{
 351        struct man_viewer_list *viewer;
 352        const char *page = cmd_to_page(git_cmd);
 353        const char *fallback = getenv("GIT_MAN_VIEWER");
 354
 355        setup_man_path();
 356        for (viewer = man_viewer_list; viewer; viewer = viewer->next)
 357        {
 358                exec_viewer(viewer->name, page); /* will return when unable */
 359        }
 360        if (fallback)
 361                exec_viewer(fallback, page);
 362        exec_viewer("man", page);
 363        die(_("no man viewer handled the request"));
 364}
 365
 366static void show_info_page(const char *git_cmd)
 367{
 368        const char *page = cmd_to_page(git_cmd);
 369        setenv("INFOPATH", system_path(GIT_INFO_PATH), 1);
 370        execlp("info", "info", "gitman", page, (char *)NULL);
 371        die(_("no info viewer handled the request"));
 372}
 373
 374static void get_html_page_path(struct strbuf *page_path, const char *page)
 375{
 376        struct stat st;
 377        char *to_free = NULL;
 378
 379        if (!html_path)
 380                html_path = to_free = system_path(GIT_HTML_PATH);
 381
 382        /* Check that we have a git documentation directory. */
 383        if (!strstr(html_path, "://")) {
 384                if (stat(mkpath("%s/git.html", html_path), &st)
 385                    || !S_ISREG(st.st_mode))
 386                        die("'%s': not a documentation directory.", html_path);
 387        }
 388
 389        strbuf_init(page_path, 0);
 390        strbuf_addf(page_path, "%s/%s.html", html_path, page);
 391        free(to_free);
 392}
 393
 394static void open_html(const char *path)
 395{
 396        execl_git_cmd("web--browse", "-c", "help.browser", path, (char *)NULL);
 397}
 398
 399static void show_html_page(const char *git_cmd)
 400{
 401        const char *page = cmd_to_page(git_cmd);
 402        struct strbuf page_path; /* it leaks but we exec bellow */
 403
 404        get_html_page_path(&page_path, page);
 405
 406        open_html(page_path.buf);
 407}
 408
 409static const char *check_git_cmd(const char* cmd)
 410{
 411        char *alias;
 412
 413        if (is_git_command(cmd))
 414                return cmd;
 415
 416        alias = alias_lookup(cmd);
 417        if (alias) {
 418                const char **argv;
 419                int count;
 420
 421                /*
 422                 * handle_builtin() in git.c rewrites "git cmd --help"
 423                 * to "git help --exclude-guides cmd", so we can use
 424                 * exclude_guides to distinguish "git cmd --help" from
 425                 * "git help cmd". In the latter case, or if cmd is an
 426                 * alias for a shell command, just print the alias
 427                 * definition.
 428                 */
 429                if (!exclude_guides || alias[0] == '!') {
 430                        printf_ln(_("'%s' is aliased to '%s'"), cmd, alias);
 431                        free(alias);
 432                        exit(0);
 433                }
 434                /*
 435                 * Otherwise, we pretend that the command was "git
 436                 * word0 --help". We use split_cmdline() to get the
 437                 * first word of the alias, to ensure that we use the
 438                 * same rules as when the alias is actually
 439                 * used. split_cmdline() modifies alias in-place.
 440                 */
 441                fprintf_ln(stderr, _("'%s' is aliased to '%s'"), cmd, alias);
 442                count = split_cmdline(alias, &argv);
 443                if (count < 0)
 444                        die(_("bad alias.%s string: %s"), cmd,
 445                            split_cmdline_strerror(count));
 446                free(argv);
 447                UNLEAK(alias);
 448                return alias;
 449        }
 450
 451        if (exclude_guides)
 452                return help_unknown_cmd(cmd);
 453
 454        return cmd;
 455}
 456
 457int cmd_help(int argc, const char **argv, const char *prefix)
 458{
 459        int nongit;
 460        enum help_format parsed_help_format;
 461
 462        argc = parse_options(argc, argv, prefix, builtin_help_options,
 463                        builtin_help_usage, 0);
 464        parsed_help_format = help_format;
 465
 466        if (show_all) {
 467                git_config(git_help_config, NULL);
 468                if (verbose) {
 469                        setup_pager();
 470                        list_all_cmds_help();
 471                        return 0;
 472                }
 473                printf(_("usage: %s%s"), _(git_usage_string), "\n\n");
 474                load_command_list("git-", &main_cmds, &other_cmds);
 475                list_commands(colopts, &main_cmds, &other_cmds);
 476        }
 477
 478        if (show_config) {
 479                int for_human = show_config == 1;
 480
 481                if (!for_human) {
 482                        list_config_help(for_human);
 483                        return 0;
 484                }
 485                setup_pager();
 486                list_config_help(for_human);
 487                printf("\n%s\n", _("'git help config' for more information"));
 488                return 0;
 489        }
 490
 491        if (show_guides)
 492                list_common_guides_help();
 493
 494        if (show_all || show_guides) {
 495                printf("%s\n", _(git_more_info_string));
 496                /*
 497                * We're done. Ignore any remaining args
 498                */
 499                return 0;
 500        }
 501
 502        if (!argv[0]) {
 503                printf(_("usage: %s%s"), _(git_usage_string), "\n\n");
 504                list_common_cmds_help();
 505                printf("\n%s\n", _(git_more_info_string));
 506                return 0;
 507        }
 508
 509        setup_git_directory_gently(&nongit);
 510        git_config(git_help_config, NULL);
 511
 512        if (parsed_help_format != HELP_FORMAT_NONE)
 513                help_format = parsed_help_format;
 514        if (help_format == HELP_FORMAT_NONE)
 515                help_format = parse_help_format(DEFAULT_HELP_FORMAT);
 516
 517        argv[0] = check_git_cmd(argv[0]);
 518
 519        switch (help_format) {
 520        case HELP_FORMAT_NONE:
 521        case HELP_FORMAT_MAN:
 522                show_man_page(argv[0]);
 523                break;
 524        case HELP_FORMAT_INFO:
 525                show_info_page(argv[0]);
 526                break;
 527        case HELP_FORMAT_WEB:
 528                show_html_page(argv[0]);
 529                break;
 530        }
 531
 532        return 0;
 533}