Back to home page

OSCL-LXR

 
 

    


0001 // SPDX-License-Identifier: GPL-2.0
0002 /*
0003  * builtin-help.c
0004  *
0005  * Builtin help command
0006  */
0007 #include "util/cache.h"
0008 #include "util/config.h"
0009 #include "util/strbuf.h"
0010 #include "builtin.h"
0011 #include <subcmd/exec-cmd.h>
0012 #include "common-cmds.h"
0013 #include <subcmd/parse-options.h>
0014 #include <subcmd/run-command.h>
0015 #include <subcmd/help.h>
0016 #include "util/debug.h"
0017 #include <linux/kernel.h>
0018 #include <linux/string.h>
0019 #include <linux/zalloc.h>
0020 #include <errno.h>
0021 #include <stdio.h>
0022 #include <stdlib.h>
0023 #include <string.h>
0024 #include <sys/types.h>
0025 #include <sys/stat.h>
0026 #include <unistd.h>
0027 
0028 static struct man_viewer_list {
0029     struct man_viewer_list *next;
0030     char name[0];
0031 } *man_viewer_list;
0032 
0033 static struct man_viewer_info_list {
0034     struct man_viewer_info_list *next;
0035     const char *info;
0036     char name[0];
0037 } *man_viewer_info_list;
0038 
0039 enum help_format {
0040     HELP_FORMAT_NONE,
0041     HELP_FORMAT_MAN,
0042     HELP_FORMAT_INFO,
0043     HELP_FORMAT_WEB,
0044 };
0045 
0046 static enum help_format parse_help_format(const char *format)
0047 {
0048     if (!strcmp(format, "man"))
0049         return HELP_FORMAT_MAN;
0050     if (!strcmp(format, "info"))
0051         return HELP_FORMAT_INFO;
0052     if (!strcmp(format, "web") || !strcmp(format, "html"))
0053         return HELP_FORMAT_WEB;
0054 
0055     pr_err("unrecognized help format '%s'", format);
0056     return HELP_FORMAT_NONE;
0057 }
0058 
0059 static const char *get_man_viewer_info(const char *name)
0060 {
0061     struct man_viewer_info_list *viewer;
0062 
0063     for (viewer = man_viewer_info_list; viewer; viewer = viewer->next) {
0064         if (!strcasecmp(name, viewer->name))
0065             return viewer->info;
0066     }
0067     return NULL;
0068 }
0069 
0070 static int check_emacsclient_version(void)
0071 {
0072     struct strbuf buffer = STRBUF_INIT;
0073     struct child_process ec_process;
0074     const char *argv_ec[] = { "emacsclient", "--version", NULL };
0075     int version;
0076     int ret = -1;
0077 
0078     /* emacsclient prints its version number on stderr */
0079     memset(&ec_process, 0, sizeof(ec_process));
0080     ec_process.argv = argv_ec;
0081     ec_process.err = -1;
0082     ec_process.stdout_to_stderr = 1;
0083     if (start_command(&ec_process)) {
0084         fprintf(stderr, "Failed to start emacsclient.\n");
0085         return -1;
0086     }
0087     if (strbuf_read(&buffer, ec_process.err, 20) < 0) {
0088         fprintf(stderr, "Failed to read emacsclient version\n");
0089         goto out;
0090     }
0091     close(ec_process.err);
0092 
0093     /*
0094      * Don't bother checking return value, because "emacsclient --version"
0095      * seems to always exits with code 1.
0096      */
0097     finish_command(&ec_process);
0098 
0099     if (!strstarts(buffer.buf, "emacsclient")) {
0100         fprintf(stderr, "Failed to parse emacsclient version.\n");
0101         goto out;
0102     }
0103 
0104     version = atoi(buffer.buf + strlen("emacsclient"));
0105 
0106     if (version < 22) {
0107         fprintf(stderr,
0108             "emacsclient version '%d' too old (< 22).\n",
0109             version);
0110     } else
0111         ret = 0;
0112 out:
0113     strbuf_release(&buffer);
0114     return ret;
0115 }
0116 
0117 static void exec_failed(const char *cmd)
0118 {
0119     char sbuf[STRERR_BUFSIZE];
0120     pr_warning("failed to exec '%s': %s", cmd, str_error_r(errno, sbuf, sizeof(sbuf)));
0121 }
0122 
0123 static void exec_woman_emacs(const char *path, const char *page)
0124 {
0125     if (!check_emacsclient_version()) {
0126         /* This works only with emacsclient version >= 22. */
0127         char *man_page;
0128 
0129         if (!path)
0130             path = "emacsclient";
0131         if (asprintf(&man_page, "(woman \"%s\")", page) > 0) {
0132             execlp(path, "emacsclient", "-e", man_page, NULL);
0133             free(man_page);
0134         }
0135         exec_failed(path);
0136     }
0137 }
0138 
0139 static void exec_man_konqueror(const char *path, const char *page)
0140 {
0141     const char *display = getenv("DISPLAY");
0142 
0143     if (display && *display) {
0144         char *man_page;
0145         const char *filename = "kfmclient";
0146 
0147         /* It's simpler to launch konqueror using kfmclient. */
0148         if (path) {
0149             const char *file = strrchr(path, '/');
0150             if (file && !strcmp(file + 1, "konqueror")) {
0151                 char *new = strdup(path);
0152                 char *dest = strrchr(new, '/');
0153 
0154                 /* strlen("konqueror") == strlen("kfmclient") */
0155                 strcpy(dest + 1, "kfmclient");
0156                 path = new;
0157             }
0158             if (file)
0159                 filename = file;
0160         } else
0161             path = "kfmclient";
0162         if (asprintf(&man_page, "man:%s(1)", page) > 0) {
0163             execlp(path, filename, "newTab", man_page, NULL);
0164             free(man_page);
0165         }
0166         exec_failed(path);
0167     }
0168 }
0169 
0170 static void exec_man_man(const char *path, const char *page)
0171 {
0172     if (!path)
0173         path = "man";
0174     execlp(path, "man", page, NULL);
0175     exec_failed(path);
0176 }
0177 
0178 static void exec_man_cmd(const char *cmd, const char *page)
0179 {
0180     char *shell_cmd;
0181 
0182     if (asprintf(&shell_cmd, "%s %s", cmd, page) > 0) {
0183         execl("/bin/sh", "sh", "-c", shell_cmd, NULL);
0184         free(shell_cmd);
0185     }
0186     exec_failed(cmd);
0187 }
0188 
0189 static void add_man_viewer(const char *name)
0190 {
0191     struct man_viewer_list **p = &man_viewer_list;
0192     size_t len = strlen(name);
0193 
0194     while (*p)
0195         p = &((*p)->next);
0196     *p = zalloc(sizeof(**p) + len + 1);
0197     strcpy((*p)->name, name);
0198 }
0199 
0200 static int supported_man_viewer(const char *name, size_t len)
0201 {
0202     return (!strncasecmp("man", name, len) ||
0203         !strncasecmp("woman", name, len) ||
0204         !strncasecmp("konqueror", name, len));
0205 }
0206 
0207 static void do_add_man_viewer_info(const char *name,
0208                    size_t len,
0209                    const char *value)
0210 {
0211     struct man_viewer_info_list *new = zalloc(sizeof(*new) + len + 1);
0212 
0213     strncpy(new->name, name, len);
0214     new->info = strdup(value);
0215     new->next = man_viewer_info_list;
0216     man_viewer_info_list = new;
0217 }
0218 
0219 static void unsupported_man_viewer(const char *name, const char *var)
0220 {
0221     pr_warning("'%s': path for unsupported man viewer.\n"
0222            "Please consider using 'man.<tool>.%s' instead.", name, var);
0223 }
0224 
0225 static int add_man_viewer_path(const char *name,
0226                    size_t len,
0227                    const char *value)
0228 {
0229     if (supported_man_viewer(name, len))
0230         do_add_man_viewer_info(name, len, value);
0231     else
0232         unsupported_man_viewer(name, "cmd");
0233 
0234     return 0;
0235 }
0236 
0237 static int add_man_viewer_cmd(const char *name,
0238                   size_t len,
0239                   const char *value)
0240 {
0241     if (supported_man_viewer(name, len))
0242         unsupported_man_viewer(name, "path");
0243     else
0244         do_add_man_viewer_info(name, len, value);
0245 
0246     return 0;
0247 }
0248 
0249 static int add_man_viewer_info(const char *var, const char *value)
0250 {
0251     const char *name = var + 4;
0252     const char *subkey = strrchr(name, '.');
0253 
0254     if (!subkey) {
0255         pr_err("Config with no key for man viewer: %s", name);
0256         return -1;
0257     }
0258 
0259     if (!strcmp(subkey, ".path")) {
0260         if (!value)
0261             return config_error_nonbool(var);
0262         return add_man_viewer_path(name, subkey - name, value);
0263     }
0264     if (!strcmp(subkey, ".cmd")) {
0265         if (!value)
0266             return config_error_nonbool(var);
0267         return add_man_viewer_cmd(name, subkey - name, value);
0268     }
0269 
0270     pr_warning("'%s': unsupported man viewer sub key.", subkey);
0271     return 0;
0272 }
0273 
0274 static int perf_help_config(const char *var, const char *value, void *cb)
0275 {
0276     enum help_format *help_formatp = cb;
0277 
0278     if (!strcmp(var, "help.format")) {
0279         if (!value)
0280             return config_error_nonbool(var);
0281         *help_formatp = parse_help_format(value);
0282         if (*help_formatp == HELP_FORMAT_NONE)
0283             return -1;
0284         return 0;
0285     }
0286     if (!strcmp(var, "man.viewer")) {
0287         if (!value)
0288             return config_error_nonbool(var);
0289         add_man_viewer(value);
0290         return 0;
0291     }
0292     if (strstarts(var, "man."))
0293         return add_man_viewer_info(var, value);
0294 
0295     return 0;
0296 }
0297 
0298 static struct cmdnames main_cmds, other_cmds;
0299 
0300 void list_common_cmds_help(void)
0301 {
0302     unsigned int i, longest = 0;
0303 
0304     for (i = 0; i < ARRAY_SIZE(common_cmds); i++) {
0305         if (longest < strlen(common_cmds[i].name))
0306             longest = strlen(common_cmds[i].name);
0307     }
0308 
0309     puts(" The most commonly used perf commands are:");
0310     for (i = 0; i < ARRAY_SIZE(common_cmds); i++) {
0311         printf("   %-*s   ", longest, common_cmds[i].name);
0312         puts(common_cmds[i].help);
0313     }
0314 }
0315 
0316 static const char *cmd_to_page(const char *perf_cmd)
0317 {
0318     char *s;
0319 
0320     if (!perf_cmd)
0321         return "perf";
0322     else if (strstarts(perf_cmd, "perf"))
0323         return perf_cmd;
0324 
0325     return asprintf(&s, "perf-%s", perf_cmd) < 0 ? NULL : s;
0326 }
0327 
0328 static void setup_man_path(void)
0329 {
0330     char *new_path;
0331     const char *old_path = getenv("MANPATH");
0332 
0333     /* We should always put ':' after our path. If there is no
0334      * old_path, the ':' at the end will let 'man' to try
0335      * system-wide paths after ours to find the manual page. If
0336      * there is old_path, we need ':' as delimiter. */
0337     if (asprintf(&new_path, "%s:%s", system_path(PERF_MAN_PATH), old_path ?: "") > 0) {
0338         setenv("MANPATH", new_path, 1);
0339         free(new_path);
0340     } else {
0341         pr_err("Unable to setup man path");
0342     }
0343 }
0344 
0345 static void exec_viewer(const char *name, const char *page)
0346 {
0347     const char *info = get_man_viewer_info(name);
0348 
0349     if (!strcasecmp(name, "man"))
0350         exec_man_man(info, page);
0351     else if (!strcasecmp(name, "woman"))
0352         exec_woman_emacs(info, page);
0353     else if (!strcasecmp(name, "konqueror"))
0354         exec_man_konqueror(info, page);
0355     else if (info)
0356         exec_man_cmd(info, page);
0357     else
0358         pr_warning("'%s': unknown man viewer.", name);
0359 }
0360 
0361 static int show_man_page(const char *perf_cmd)
0362 {
0363     struct man_viewer_list *viewer;
0364     const char *page = cmd_to_page(perf_cmd);
0365     const char *fallback = getenv("PERF_MAN_VIEWER");
0366 
0367     setup_man_path();
0368     for (viewer = man_viewer_list; viewer; viewer = viewer->next)
0369         exec_viewer(viewer->name, page); /* will return when unable */
0370 
0371     if (fallback)
0372         exec_viewer(fallback, page);
0373     exec_viewer("man", page);
0374 
0375     pr_err("no man viewer handled the request");
0376     return -1;
0377 }
0378 
0379 static int show_info_page(const char *perf_cmd)
0380 {
0381     const char *page = cmd_to_page(perf_cmd);
0382     setenv("INFOPATH", system_path(PERF_INFO_PATH), 1);
0383     execlp("info", "info", "perfman", page, NULL);
0384     return -1;
0385 }
0386 
0387 static int get_html_page_path(char **page_path, const char *page)
0388 {
0389     struct stat st;
0390     const char *html_path = system_path(PERF_HTML_PATH);
0391 
0392     /* Check that we have a perf documentation directory. */
0393     if (stat(mkpath("%s/perf.html", html_path), &st)
0394         || !S_ISREG(st.st_mode)) {
0395         pr_err("'%s': not a documentation directory.", html_path);
0396         return -1;
0397     }
0398 
0399     return asprintf(page_path, "%s/%s.html", html_path, page);
0400 }
0401 
0402 /*
0403  * If open_html is not defined in a platform-specific way (see for
0404  * example compat/mingw.h), we use the script web--browse to display
0405  * HTML.
0406  */
0407 #ifndef open_html
0408 static void open_html(const char *path)
0409 {
0410     execl_cmd("web--browse", "-c", "help.browser", path, NULL);
0411 }
0412 #endif
0413 
0414 static int show_html_page(const char *perf_cmd)
0415 {
0416     const char *page = cmd_to_page(perf_cmd);
0417     char *page_path; /* it leaks but we exec bellow */
0418 
0419     if (get_html_page_path(&page_path, page) < 0)
0420         return -1;
0421 
0422     open_html(page_path);
0423 
0424     return 0;
0425 }
0426 
0427 int cmd_help(int argc, const char **argv)
0428 {
0429     bool show_all = false;
0430     enum help_format help_format = HELP_FORMAT_MAN;
0431     struct option builtin_help_options[] = {
0432     OPT_BOOLEAN('a', "all", &show_all, "print all available commands"),
0433     OPT_SET_UINT('m', "man", &help_format, "show man page", HELP_FORMAT_MAN),
0434     OPT_SET_UINT('w', "web", &help_format, "show manual in web browser",
0435             HELP_FORMAT_WEB),
0436     OPT_SET_UINT('i', "info", &help_format, "show info page",
0437             HELP_FORMAT_INFO),
0438     OPT_END(),
0439     };
0440     const char * const builtin_help_subcommands[] = {
0441         "buildid-cache", "buildid-list", "diff", "evlist", "help", "list",
0442         "record", "report", "bench", "stat", "timechart", "top", "annotate",
0443         "script", "sched", "kallsyms", "kmem", "lock", "kvm", "test", "inject", "mem", "data",
0444 #ifdef HAVE_LIBELF_SUPPORT
0445         "probe",
0446 #endif
0447 #if defined(HAVE_LIBAUDIT_SUPPORT) || defined(HAVE_SYSCALL_TABLE_SUPPORT)
0448         "trace",
0449 #endif
0450     NULL };
0451     const char *builtin_help_usage[] = {
0452         "perf help [--all] [--man|--web|--info] [command]",
0453         NULL
0454     };
0455     int rc;
0456 
0457     load_command_list("perf-", &main_cmds, &other_cmds);
0458 
0459     rc = perf_config(perf_help_config, &help_format);
0460     if (rc)
0461         return rc;
0462 
0463     argc = parse_options_subcommand(argc, argv, builtin_help_options,
0464             builtin_help_subcommands, builtin_help_usage, 0);
0465 
0466     if (show_all) {
0467         printf("\n Usage: %s\n\n", perf_usage_string);
0468         list_commands("perf commands", &main_cmds, &other_cmds);
0469         printf(" %s\n\n", perf_more_info_string);
0470         return 0;
0471     }
0472 
0473     if (!argv[0]) {
0474         printf("\n usage: %s\n\n", perf_usage_string);
0475         list_common_cmds_help();
0476         printf("\n %s\n\n", perf_more_info_string);
0477         return 0;
0478     }
0479 
0480     switch (help_format) {
0481     case HELP_FORMAT_MAN:
0482         rc = show_man_page(argv[0]);
0483         break;
0484     case HELP_FORMAT_INFO:
0485         rc = show_info_page(argv[0]);
0486         break;
0487     case HELP_FORMAT_WEB:
0488         rc = show_html_page(argv[0]);
0489         break;
0490     case HELP_FORMAT_NONE:
0491         /* fall-through */
0492     default:
0493         rc = -1;
0494         break;
0495     }
0496 
0497     return rc;
0498 }