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