From: tip-bot for Josh Poimboeuf <tipbot@zytor.com>
To: linux-tip-commits@vger.kernel.org
Cc: jpoimboe@redhat.com, linux-kernel@vger.kernel.org, hpa@zytor.com,
acme@redhat.com, peterz@infradead.org, mingo@kernel.org,
namhyung@kernel.org, tglx@linutronix.de, jolsa@redhat.com
Subject: [tip:perf/core] perf tools: Remove subcmd dependencies on strbuf
Date: Fri, 18 Dec 2015 00:53:25 -0800 [thread overview]
Message-ID: <tip-901421a5bdf605d24c278825cdd032cd6038bcb8@git.kernel.org> (raw)
In-Reply-To: <957d207e1254406fa11fc2e405e75a7e405aad8f.1450193761.git.jpoimboe@redhat.com>
Commit-ID: 901421a5bdf605d24c278825cdd032cd6038bcb8
Gitweb: http://git.kernel.org/tip/901421a5bdf605d24c278825cdd032cd6038bcb8
Author: Josh Poimboeuf <jpoimboe@redhat.com>
AuthorDate: Tue, 15 Dec 2015 09:39:36 -0600
Committer: Arnaldo Carvalho de Melo <acme@redhat.com>
CommitDate: Wed, 16 Dec 2015 21:33:13 -0300
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>
---
tools/perf/util/exec_cmd.c | 27 +++++++++++++-------------
tools/perf/util/help.c | 14 ++++++--------
tools/perf/util/parse-options.c | 42 +++++++++++++++++++++--------------------
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 b935e4c..65d86dc 100644
--- a/tools/perf/util/exec_cmd.c
+++ b/tools/perf/util/exec_cmd.c
@@ -4,6 +4,7 @@
#include "subcmd-config.h"
#include <string.h>
+#include "subcmd-util.h"
#define MAX_ARGS 32
@@ -21,14 +22,14 @@ void exec_cmd_init(const char *exec_name, const char *prefix,
char *system_path(const char *path)
{
- struct strbuf d = STRBUF_INIT;
+ char *buf = NULL;
if (is_absolute_path(path))
return strdup(path);
- strbuf_addf(&d, "%s/%s", subcmd_config.prefix, path);
- path = strbuf_detach(&d, NULL);
- return (char *)path;
+ astrcatf(&buf, "%s/%s", subcmd_config.prefix, path);
+
+ return buf;
}
const char *perf_extract_argv0_path(const char *argv0)
@@ -75,22 +76,22 @@ char *perf_exec_path(void)
return system_path(subcmd_config.exec_path);
}
-static void add_path(struct strbuf *out, const char *path)
+static void add_path(char **out, const char *path)
{
if (path && *path) {
if (is_absolute_path(path))
- strbuf_addstr(out, path);
+ astrcat(out, path);
else
- strbuf_addstr(out, make_nonrelative_path(path));
+ astrcat(out, make_nonrelative_path(path));
- strbuf_addch(out, PATH_SEP);
+ astrcat(out, ":");
}
}
void setup_path(void)
{
const char *old_path = getenv("PATH");
- struct strbuf new_path = STRBUF_INIT;
+ char *new_path = NULL;
char *tmp = perf_exec_path();
add_path(&new_path, tmp);
@@ -98,13 +99,13 @@ void setup_path(void)
free(tmp);
if (old_path)
- strbuf_addstr(&new_path, old_path);
+ astrcat(&new_path, old_path);
else
- strbuf_addstr(&new_path, "/usr/local/bin:/usr/bin:/bin");
+ astrcat(&new_path, "/usr/local/bin:/usr/bin:/bin");
- setenv("PATH", new_path.buf, 1);
+ setenv("PATH", new_path, 1);
- strbuf_release(&new_path);
+ free(new_path);
}
static const char **prepare_perf_cmd(const char **argv)
diff --git a/tools/perf/util/help.c b/tools/perf/util/help.c
index 8d74f7d..8e5e0ce 100644
--- a/tools/perf/util/help.c
+++ b/tools/perf/util/help.c
@@ -2,6 +2,7 @@
#include "../builtin.h"
#include "exec_cmd.h"
#include "help.h"
+#include "subcmd-util.h"
void add_cmdname(struct cmdnames *cmds, const char *name, size_t len)
{
@@ -119,8 +120,7 @@ static void list_commands_in_dir(struct cmdnames *cmds,
int prefix_len;
DIR *dir = opendir(path);
struct dirent *de;
- struct strbuf buf = STRBUF_INIT;
- int len;
+ char *buf = NULL;
if (!dir)
return;
@@ -128,8 +128,7 @@ static void list_commands_in_dir(struct cmdnames *cmds,
prefix = "perf-";
prefix_len = strlen(prefix);
- strbuf_addf(&buf, "%s/", path);
- len = buf.len;
+ astrcatf(&buf, "%s/", path);
while ((de = readdir(dir)) != NULL) {
int entlen;
@@ -137,9 +136,8 @@ static void list_commands_in_dir(struct cmdnames *cmds,
if (prefixcmp(de->d_name, prefix))
continue;
- strbuf_setlen(&buf, len);
- strbuf_addstr(&buf, de->d_name);
- if (!is_executable(buf.buf))
+ astrcat(&buf, de->d_name);
+ if (!is_executable(buf))
continue;
entlen = strlen(de->d_name) - prefix_len;
@@ -149,7 +147,7 @@ static void list_commands_in_dir(struct cmdnames *cmds,
add_cmdname(cmds, de->d_name + prefix_len, entlen);
}
closedir(dir);
- strbuf_release(&buf);
+ free(buf);
}
void load_command_list(const char *prefix,
diff --git a/tools/perf/util/parse-options.c b/tools/perf/util/parse-options.c
index da4ba21..c1da2a5 100644
--- a/tools/perf/util/parse-options.c
+++ b/tools/perf/util/parse-options.c
@@ -1,4 +1,5 @@
#include "util.h"
+#include "subcmd-util.h"
#include "parse-options.h"
#include "cache.h"
#include "header.h"
@@ -8,7 +9,7 @@
#define OPT_SHORT 1
#define OPT_UNSET 2
-static struct strbuf error_buf = STRBUF_INIT;
+char *error_buf;
static int opterror(const struct option *opt, const char *reason, int flags)
{
@@ -576,19 +577,18 @@ int parse_options_subcommand(int argc, const char **argv, const struct option *o
/* build usage string if it's not provided */
if (subcommands && !usagestr[0]) {
- struct strbuf buf = STRBUF_INIT;
+ char *buf = NULL;
+
+ astrcatf(&buf, "%s %s [<options>] {", subcmd_config.exec_name, argv[0]);
- strbuf_addf(&buf, "%s %s [<options>] {",
- subcmd_config.exec_name, argv[0]);
for (int i = 0; subcommands[i]; i++) {
if (i)
- strbuf_addstr(&buf, "|");
- strbuf_addstr(&buf, subcommands[i]);
+ astrcat(&buf, "|");
+ astrcat(&buf, subcommands[i]);
}
- strbuf_addstr(&buf, "}");
+ astrcat(&buf, "}");
- usagestr[0] = strdup(buf.buf);
- strbuf_release(&buf);
+ usagestr[0] = buf;
}
parse_options_start(&ctx, argc, argv, flags);
@@ -613,13 +613,11 @@ int parse_options_subcommand(int argc, const char **argv, const struct option *o
putchar('\n');
exit(130);
default: /* PARSE_OPT_UNKNOWN */
- if (ctx.argv[0][1] == '-') {
- strbuf_addf(&error_buf, "unknown option `%s'",
- ctx.argv[0] + 2);
- } else {
- strbuf_addf(&error_buf, "unknown switch `%c'",
- *ctx.opt);
- }
+ if (ctx.argv[0][1] == '-')
+ astrcatf(&error_buf, "unknown option `%s'",
+ ctx.argv[0] + 2);
+ else
+ astrcatf(&error_buf, "unknown switch `%c'", *ctx.opt);
usage_with_options(usagestr, options);
}
@@ -806,9 +804,9 @@ static int usage_with_options_internal(const char * const *usagestr,
setup_pager();
- if (strbuf_avail(&error_buf)) {
- fprintf(stderr, " Error: %s\n", error_buf.buf);
- strbuf_release(&error_buf);
+ if (error_buf) {
+ fprintf(stderr, " Error: %s\n", error_buf);
+ zfree(&error_buf);
}
fprintf(stderr, "\n Usage: %s\n", *usagestr++);
@@ -852,11 +850,15 @@ void usage_with_options_msg(const char * const *usagestr,
const struct option *opts, const char *fmt, ...)
{
va_list ap;
+ char *tmp = error_buf;
va_start(ap, fmt);
- strbuf_addv(&error_buf, fmt, ap);
+ if (vasprintf(&error_buf, fmt, ap) == -1)
+ die("vasprintf failed");
va_end(ap);
+ free(tmp);
+
usage_with_options_internal(usagestr, opts, 0, NULL);
exit(129);
}
diff --git a/tools/perf/util/subcmd-util.h b/tools/perf/util/subcmd-util.h
new file mode 100644
index 0000000..98fb9f9
--- /dev/null
+++ b/tools/perf/util/subcmd-util.h
@@ -0,0 +1,24 @@
+#ifndef __PERF_SUBCMD_UTIL_H
+#define __PERF_SUBCMD_UTIL_H
+
+#include <stdio.h>
+
+#define astrcatf(out, fmt, ...) \
+({ \
+ char *tmp = *(out); \
+ if (asprintf((out), "%s" fmt, tmp ?: "", ## __VA_ARGS__) == -1) \
+ die("asprintf failed"); \
+ free(tmp); \
+})
+
+static inline void astrcat(char **out, const char *add)
+{
+ char *tmp = *out;
+
+ if (asprintf(out, "%s%s", tmp ?: "", add) == -1)
+ die("asprintf failed");
+
+ free(tmp);
+}
+
+#endif /* __PERF_SUBCMD_UTIL_H */
next prev parent reply other threads:[~2015-12-18 8:54 UTC|newest]
Thread overview: 23+ messages / expand[flat|nested] mbox.gz Atom feed top
2015-12-15 15:39 [PATCH v4 0/9] perf tools: Move perf subcommand framework to a library Josh Poimboeuf
2015-12-15 15:39 ` [PATCH v4 1/9] tools build: Fix feature Makefile issues with 'O=' Josh Poimboeuf
2015-12-18 8:51 ` [tip:perf/core] tools build: Fix feature Makefile issues with 'O= ' tip-bot for Josh Poimboeuf
2015-12-15 15:39 ` [PATCH v4 2/9] perf tools: Move strlcpy() from perf to tools/lib/string.c Josh Poimboeuf
2015-12-18 8:52 ` [tip:perf/core] perf tools: Move strlcpy() from perf to tools/lib /string.c tip-bot for Josh Poimboeuf
2015-12-15 15:39 ` [PATCH v4 3/9] perf: Document the fact that parse_options*() may exit Josh Poimboeuf
2015-12-18 8:52 ` [tip:perf/core] perf tools: Document the fact that parse_options* () " tip-bot for Josh Poimboeuf
2015-12-15 15:39 ` [PATCH v4 4/9] perf: Provide subcmd configuration at runtime Josh Poimboeuf
2015-12-18 8:53 ` [tip:perf/core] perf tools: " tip-bot for Josh Poimboeuf
2015-12-15 15:39 ` [PATCH v4 5/9] perf: Remove subcmd dependencies on strbuf Josh Poimboeuf
2015-12-18 8:53 ` tip-bot for Josh Poimboeuf [this message]
2015-12-15 15:39 ` [PATCH v4 6/9] perf: Remove 'perf' from subcmd function and variable names Josh Poimboeuf
2015-12-18 8:53 ` [tip:perf/core] perf tools: " tip-bot for Josh Poimboeuf
2015-12-15 15:39 ` [PATCH v4 7/9] perf: Finalize subcmd independence Josh Poimboeuf
2015-12-17 0:57 ` Arnaldo Carvalho de Melo
2015-12-17 1:09 ` Josh Poimboeuf
2015-12-17 1:27 ` Arnaldo Carvalho de Melo
2015-12-17 4:12 ` Josh Poimboeuf
2015-12-18 8:54 ` [tip:perf/core] perf tools: " tip-bot for Josh Poimboeuf
2015-12-15 15:39 ` [PATCH v4 8/9] perf subcmd: Create subcmd library Josh Poimboeuf
2015-12-18 8:54 ` [tip:perf/core] " tip-bot for Josh Poimboeuf
2015-12-15 15:39 ` [PATCH v4 9/9] tools subcmd: Rename subcmd header include guards Josh Poimboeuf
2015-12-18 8:54 ` [tip:perf/core] " tip-bot for Josh Poimboeuf
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=tip-901421a5bdf605d24c278825cdd032cd6038bcb8@git.kernel.org \
--to=tipbot@zytor.com \
--cc=acme@redhat.com \
--cc=hpa@zytor.com \
--cc=jolsa@redhat.com \
--cc=jpoimboe@redhat.com \
--cc=linux-kernel@vger.kernel.org \
--cc=linux-tip-commits@vger.kernel.org \
--cc=mingo@kernel.org \
--cc=namhyung@kernel.org \
--cc=peterz@infradead.org \
--cc=tglx@linutronix.de \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.