diff options
author | Josh Poimboeuf <jpoimboe@redhat.com> | 2015-12-15 10:39:36 -0500 |
---|---|---|
committer | Arnaldo Carvalho de Melo <acme@redhat.com> | 2015-12-16 19:33:13 -0500 |
commit | 901421a5bdf605d24c278825cdd032cd6038bcb8 (patch) | |
tree | e38d011aa6ff9c348a6ea7d3c9e664fef54b237f | |
parent | 096d35585b4fce7d3ee9b8b34314f39f49491ab1 (diff) |
perf tools: Remove subcmd dependencies on strbuf
Introduce and use new astrcat() and astrcatf() functions which replace
the strbuf functionality for subcmd.
For now they duplicate strbuf's die-on-allocation-error policy.
Signed-off-by: Josh Poimboeuf <jpoimboe@redhat.com>
Cc: Jiri Olsa <jolsa@redhat.com>
Cc: Namhyung Kim <namhyung@kernel.org>
Cc: Peter Zijlstra <peterz@infradead.org>
Link: http://lkml.kernel.org/r/957d207e1254406fa11fc2e405e75a7e405aad8f.1450193761.git.jpoimboe@redhat.com
Signed-off-by: Arnaldo Carvalho de Melo <acme@redhat.com>
-rw-r--r-- | tools/perf/util/exec_cmd.c | 27 | ||||
-rw-r--r-- | tools/perf/util/help.c | 14 | ||||
-rw-r--r-- | tools/perf/util/parse-options.c | 42 | ||||
-rw-r--r-- | tools/perf/util/subcmd-util.h | 24 |
4 files changed, 66 insertions, 41 deletions
diff --git a/tools/perf/util/exec_cmd.c b/tools/perf/util/exec_cmd.c index b935e4ce62a2..65d86dcaa984 100644 --- a/tools/perf/util/exec_cmd.c +++ b/tools/perf/util/exec_cmd.c | |||
@@ -4,6 +4,7 @@ | |||
4 | #include "subcmd-config.h" | 4 | #include "subcmd-config.h" |
5 | 5 | ||
6 | #include <string.h> | 6 | #include <string.h> |
7 | #include "subcmd-util.h" | ||
7 | 8 | ||
8 | #define MAX_ARGS 32 | 9 | #define MAX_ARGS 32 |
9 | 10 | ||
@@ -21,14 +22,14 @@ void exec_cmd_init(const char *exec_name, const char *prefix, | |||
21 | 22 | ||
22 | char *system_path(const char *path) | 23 | char *system_path(const char *path) |
23 | { | 24 | { |
24 | struct strbuf d = STRBUF_INIT; | 25 | char *buf = NULL; |
25 | 26 | ||
26 | if (is_absolute_path(path)) | 27 | if (is_absolute_path(path)) |
27 | return strdup(path); | 28 | return strdup(path); |
28 | 29 | ||
29 | strbuf_addf(&d, "%s/%s", subcmd_config.prefix, path); | 30 | astrcatf(&buf, "%s/%s", subcmd_config.prefix, path); |
30 | path = strbuf_detach(&d, NULL); | 31 | |
31 | return (char *)path; | 32 | return buf; |
32 | } | 33 | } |
33 | 34 | ||
34 | const char *perf_extract_argv0_path(const char *argv0) | 35 | const char *perf_extract_argv0_path(const char *argv0) |
@@ -75,22 +76,22 @@ char *perf_exec_path(void) | |||
75 | return system_path(subcmd_config.exec_path); | 76 | return system_path(subcmd_config.exec_path); |
76 | } | 77 | } |
77 | 78 | ||
78 | static void add_path(struct strbuf *out, const char *path) | 79 | static void add_path(char **out, const char *path) |
79 | { | 80 | { |
80 | if (path && *path) { | 81 | if (path && *path) { |
81 | if (is_absolute_path(path)) | 82 | if (is_absolute_path(path)) |
82 | strbuf_addstr(out, path); | 83 | astrcat(out, path); |
83 | else | 84 | else |
84 | strbuf_addstr(out, make_nonrelative_path(path)); | 85 | astrcat(out, make_nonrelative_path(path)); |
85 | 86 | ||
86 | strbuf_addch(out, PATH_SEP); | 87 | astrcat(out, ":"); |
87 | } | 88 | } |
88 | } | 89 | } |
89 | 90 | ||
90 | void setup_path(void) | 91 | void setup_path(void) |
91 | { | 92 | { |
92 | const char *old_path = getenv("PATH"); | 93 | const char *old_path = getenv("PATH"); |
93 | struct strbuf new_path = STRBUF_INIT; | 94 | char *new_path = NULL; |
94 | char *tmp = perf_exec_path(); | 95 | char *tmp = perf_exec_path(); |
95 | 96 | ||
96 | add_path(&new_path, tmp); | 97 | add_path(&new_path, tmp); |
@@ -98,13 +99,13 @@ void setup_path(void) | |||
98 | free(tmp); | 99 | free(tmp); |
99 | 100 | ||
100 | if (old_path) | 101 | if (old_path) |
101 | strbuf_addstr(&new_path, old_path); | 102 | astrcat(&new_path, old_path); |
102 | else | 103 | else |
103 | strbuf_addstr(&new_path, "/usr/local/bin:/usr/bin:/bin"); | 104 | astrcat(&new_path, "/usr/local/bin:/usr/bin:/bin"); |
104 | 105 | ||
105 | setenv("PATH", new_path.buf, 1); | 106 | setenv("PATH", new_path, 1); |
106 | 107 | ||
107 | strbuf_release(&new_path); | 108 | free(new_path); |
108 | } | 109 | } |
109 | 110 | ||
110 | static const char **prepare_perf_cmd(const char **argv) | 111 | static const char **prepare_perf_cmd(const char **argv) |
diff --git a/tools/perf/util/help.c b/tools/perf/util/help.c index 8d74f7d05674..8e5e0ce3870e 100644 --- a/tools/perf/util/help.c +++ b/tools/perf/util/help.c | |||
@@ -2,6 +2,7 @@ | |||
2 | #include "../builtin.h" | 2 | #include "../builtin.h" |
3 | #include "exec_cmd.h" | 3 | #include "exec_cmd.h" |
4 | #include "help.h" | 4 | #include "help.h" |
5 | #include "subcmd-util.h" | ||
5 | 6 | ||
6 | void add_cmdname(struct cmdnames *cmds, const char *name, size_t len) | 7 | void add_cmdname(struct cmdnames *cmds, const char *name, size_t len) |
7 | { | 8 | { |
@@ -119,8 +120,7 @@ static void list_commands_in_dir(struct cmdnames *cmds, | |||
119 | int prefix_len; | 120 | int prefix_len; |
120 | DIR *dir = opendir(path); | 121 | DIR *dir = opendir(path); |
121 | struct dirent *de; | 122 | struct dirent *de; |
122 | struct strbuf buf = STRBUF_INIT; | 123 | char *buf = NULL; |
123 | int len; | ||
124 | 124 | ||
125 | if (!dir) | 125 | if (!dir) |
126 | return; | 126 | return; |
@@ -128,8 +128,7 @@ static void list_commands_in_dir(struct cmdnames *cmds, | |||
128 | prefix = "perf-"; | 128 | prefix = "perf-"; |
129 | prefix_len = strlen(prefix); | 129 | prefix_len = strlen(prefix); |
130 | 130 | ||
131 | strbuf_addf(&buf, "%s/", path); | 131 | astrcatf(&buf, "%s/", path); |
132 | len = buf.len; | ||
133 | 132 | ||
134 | while ((de = readdir(dir)) != NULL) { | 133 | while ((de = readdir(dir)) != NULL) { |
135 | int entlen; | 134 | int entlen; |
@@ -137,9 +136,8 @@ static void list_commands_in_dir(struct cmdnames *cmds, | |||
137 | if (prefixcmp(de->d_name, prefix)) | 136 | if (prefixcmp(de->d_name, prefix)) |
138 | continue; | 137 | continue; |
139 | 138 | ||
140 | strbuf_setlen(&buf, len); | 139 | astrcat(&buf, de->d_name); |
141 | strbuf_addstr(&buf, de->d_name); | 140 | if (!is_executable(buf)) |
142 | if (!is_executable(buf.buf)) | ||
143 | continue; | 141 | continue; |
144 | 142 | ||
145 | entlen = strlen(de->d_name) - prefix_len; | 143 | entlen = strlen(de->d_name) - prefix_len; |
@@ -149,7 +147,7 @@ static void list_commands_in_dir(struct cmdnames *cmds, | |||
149 | add_cmdname(cmds, de->d_name + prefix_len, entlen); | 147 | add_cmdname(cmds, de->d_name + prefix_len, entlen); |
150 | } | 148 | } |
151 | closedir(dir); | 149 | closedir(dir); |
152 | strbuf_release(&buf); | 150 | free(buf); |
153 | } | 151 | } |
154 | 152 | ||
155 | void load_command_list(const char *prefix, | 153 | void load_command_list(const char *prefix, |
diff --git a/tools/perf/util/parse-options.c b/tools/perf/util/parse-options.c index da4ba21cad21..c1da2a53ed4e 100644 --- a/tools/perf/util/parse-options.c +++ b/tools/perf/util/parse-options.c | |||
@@ -1,4 +1,5 @@ | |||
1 | #include "util.h" | 1 | #include "util.h" |
2 | #include "subcmd-util.h" | ||
2 | #include "parse-options.h" | 3 | #include "parse-options.h" |
3 | #include "cache.h" | 4 | #include "cache.h" |
4 | #include "header.h" | 5 | #include "header.h" |
@@ -8,7 +9,7 @@ | |||
8 | #define OPT_SHORT 1 | 9 | #define OPT_SHORT 1 |
9 | #define OPT_UNSET 2 | 10 | #define OPT_UNSET 2 |
10 | 11 | ||
11 | static struct strbuf error_buf = STRBUF_INIT; | 12 | char *error_buf; |
12 | 13 | ||
13 | static int opterror(const struct option *opt, const char *reason, int flags) | 14 | static int opterror(const struct option *opt, const char *reason, int flags) |
14 | { | 15 | { |
@@ -576,19 +577,18 @@ int parse_options_subcommand(int argc, const char **argv, const struct option *o | |||
576 | 577 | ||
577 | /* build usage string if it's not provided */ | 578 | /* build usage string if it's not provided */ |
578 | if (subcommands && !usagestr[0]) { | 579 | if (subcommands && !usagestr[0]) { |
579 | struct strbuf buf = STRBUF_INIT; | 580 | char *buf = NULL; |
581 | |||
582 | astrcatf(&buf, "%s %s [<options>] {", subcmd_config.exec_name, argv[0]); | ||
580 | 583 | ||
581 | strbuf_addf(&buf, "%s %s [<options>] {", | ||
582 | subcmd_config.exec_name, argv[0]); | ||
583 | for (int i = 0; subcommands[i]; i++) { | 584 | for (int i = 0; subcommands[i]; i++) { |
584 | if (i) | 585 | if (i) |
585 | strbuf_addstr(&buf, "|"); | 586 | astrcat(&buf, "|"); |
586 | strbuf_addstr(&buf, subcommands[i]); | 587 | astrcat(&buf, subcommands[i]); |
587 | } | 588 | } |
588 | strbuf_addstr(&buf, "}"); | 589 | astrcat(&buf, "}"); |
589 | 590 | ||
590 | usagestr[0] = strdup(buf.buf); | 591 | usagestr[0] = buf; |
591 | strbuf_release(&buf); | ||
592 | } | 592 | } |
593 | 593 | ||
594 | parse_options_start(&ctx, argc, argv, flags); | 594 | parse_options_start(&ctx, argc, argv, flags); |
@@ -613,13 +613,11 @@ int parse_options_subcommand(int argc, const char **argv, const struct option *o | |||
613 | putchar('\n'); | 613 | putchar('\n'); |
614 | exit(130); | 614 | exit(130); |
615 | default: /* PARSE_OPT_UNKNOWN */ | 615 | default: /* PARSE_OPT_UNKNOWN */ |
616 | if (ctx.argv[0][1] == '-') { | 616 | if (ctx.argv[0][1] == '-') |
617 | strbuf_addf(&error_buf, "unknown option `%s'", | 617 | astrcatf(&error_buf, "unknown option `%s'", |
618 | ctx.argv[0] + 2); | 618 | ctx.argv[0] + 2); |
619 | } else { | 619 | else |
620 | strbuf_addf(&error_buf, "unknown switch `%c'", | 620 | astrcatf(&error_buf, "unknown switch `%c'", *ctx.opt); |
621 | *ctx.opt); | ||
622 | } | ||
623 | usage_with_options(usagestr, options); | 621 | usage_with_options(usagestr, options); |
624 | } | 622 | } |
625 | 623 | ||
@@ -806,9 +804,9 @@ static int usage_with_options_internal(const char * const *usagestr, | |||
806 | 804 | ||
807 | setup_pager(); | 805 | setup_pager(); |
808 | 806 | ||
809 | if (strbuf_avail(&error_buf)) { | 807 | if (error_buf) { |
810 | fprintf(stderr, " Error: %s\n", error_buf.buf); | 808 | fprintf(stderr, " Error: %s\n", error_buf); |
811 | strbuf_release(&error_buf); | 809 | zfree(&error_buf); |
812 | } | 810 | } |
813 | 811 | ||
814 | fprintf(stderr, "\n Usage: %s\n", *usagestr++); | 812 | fprintf(stderr, "\n Usage: %s\n", *usagestr++); |
@@ -852,11 +850,15 @@ void usage_with_options_msg(const char * const *usagestr, | |||
852 | const struct option *opts, const char *fmt, ...) | 850 | const struct option *opts, const char *fmt, ...) |
853 | { | 851 | { |
854 | va_list ap; | 852 | va_list ap; |
853 | char *tmp = error_buf; | ||
855 | 854 | ||
856 | va_start(ap, fmt); | 855 | va_start(ap, fmt); |
857 | strbuf_addv(&error_buf, fmt, ap); | 856 | if (vasprintf(&error_buf, fmt, ap) == -1) |
857 | die("vasprintf failed"); | ||
858 | va_end(ap); | 858 | va_end(ap); |
859 | 859 | ||
860 | free(tmp); | ||
861 | |||
860 | usage_with_options_internal(usagestr, opts, 0, NULL); | 862 | usage_with_options_internal(usagestr, opts, 0, NULL); |
861 | exit(129); | 863 | exit(129); |
862 | } | 864 | } |
diff --git a/tools/perf/util/subcmd-util.h b/tools/perf/util/subcmd-util.h new file mode 100644 index 000000000000..98fb9f9270eb --- /dev/null +++ b/tools/perf/util/subcmd-util.h | |||
@@ -0,0 +1,24 @@ | |||
1 | #ifndef __PERF_SUBCMD_UTIL_H | ||
2 | #define __PERF_SUBCMD_UTIL_H | ||
3 | |||
4 | #include <stdio.h> | ||
5 | |||
6 | #define astrcatf(out, fmt, ...) \ | ||
7 | ({ \ | ||
8 | char *tmp = *(out); \ | ||
9 | if (asprintf((out), "%s" fmt, tmp ?: "", ## __VA_ARGS__) == -1) \ | ||
10 | die("asprintf failed"); \ | ||
11 | free(tmp); \ | ||
12 | }) | ||
13 | |||
14 | static inline void astrcat(char **out, const char *add) | ||
15 | { | ||
16 | char *tmp = *out; | ||
17 | |||
18 | if (asprintf(out, "%s%s", tmp ?: "", add) == -1) | ||
19 | die("asprintf failed"); | ||
20 | |||
21 | free(tmp); | ||
22 | } | ||
23 | |||
24 | #endif /* __PERF_SUBCMD_UTIL_H */ | ||