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