#include "subcmd-config.h"
#include <string.h>
+#include "subcmd-util.h"
#define MAX_ARGS 32
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)
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);
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)
#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)
{
int prefix_len;
DIR *dir = opendir(path);
struct dirent *de;
- struct strbuf buf = STRBUF_INIT;
- int len;
+ char *buf = NULL;
if (!dir)
return;
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;
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;
add_cmdname(cmds, de->d_name + prefix_len, entlen);
}
closedir(dir);
- strbuf_release(&buf);
+ free(buf);
}
void load_command_list(const char *prefix,
#include "util.h"
+#include "subcmd-util.h"
#include "parse-options.h"
#include "cache.h"
#include "header.h"
#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)
{
/* 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);
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);
}
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++);
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);
}
--- /dev/null
+#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 */