tools include: Adopt strstarts() from the kernel

Replacing prefixcmp(), same purpose, inverted result, so standardize on
the kernel variant, to reduce silly differences among tools/ and the
kernel sources, making it easier for people to work in both codebases.

And then doing:

	if (strstarts(option, "no-"))

Looks clearer than doing:

	if (!prefixcmp(option, "no-"))

To figure out if option starts witn "no-".

Cc: Adrian Hunter <adrian.hunter@intel.com>
Cc: David Ahern <dsahern@gmail.com>
Cc: Jiri Olsa <jolsa@kernel.org>
Cc: Josh Poimboeuf <jpoimboe@redhat.com>
Cc: Namhyung Kim <namhyung@kernel.org>
Cc: Rusty Russell <rusty@rustcorp.com.au>
Cc: Wang Nan <wangnan0@huawei.com>
Link: http://lkml.kernel.org/n/tip-kaei42gi7lpa8subwtv7eug8@git.kernel.orgSigned-off-by: default avatarArnaldo Carvalho de Melo <acme@redhat.com>
parent 082ab9a1
#ifndef _TOOLS_LINUX_STRING_H_
#define _TOOLS_LINUX_STRING_H_
#include <linux/types.h> /* for size_t */
#include <string.h>
void *memdup(const void *src, size_t len);
......@@ -18,6 +18,14 @@ extern size_t strlcpy(char *dest, const char *src, size_t size);
char *str_error_r(int errnum, char *buf, size_t buflen);
int prefixcmp(const char *str, const char *prefix);
/**
* strstarts - does @str start with @prefix?
* @str: string to examine
* @prefix: prefix to look for.
*/
static inline bool strstarts(const char *str, const char *prefix)
{
return strncmp(str, prefix, strlen(prefix)) == 0;
}
#endif /* _LINUX_STRING_H_ */
......@@ -87,12 +87,3 @@ size_t __weak strlcpy(char *dest, const char *src, size_t size)
}
return ret;
}
int prefixcmp(const char *str, const char *prefix)
{
for (; ; str++, prefix++)
if (!*prefix)
return 0;
else if (*str != *prefix)
return (unsigned char)*prefix - (unsigned char)*str;
}
......@@ -171,7 +171,7 @@ static void list_commands_in_dir(struct cmdnames *cmds,
while ((de = readdir(dir)) != NULL) {
int entlen;
if (prefixcmp(de->d_name, prefix))
if (!strstarts(de->d_name, prefix))
continue;
astrcat(&buf, de->d_name);
......
......@@ -368,7 +368,7 @@ static int parse_long_opt(struct parse_opt_ctx_t *p, const char *arg,
return 0;
}
if (!rest) {
if (!prefixcmp(options->long_name, "no-")) {
if (strstarts(options->long_name, "no-")) {
/*
* The long name itself starts with "no-", so
* accept the option without "no-" so that users
......@@ -381,7 +381,7 @@ static int parse_long_opt(struct parse_opt_ctx_t *p, const char *arg,
goto match;
}
/* Abbreviated case */
if (!prefixcmp(options->long_name + 3, arg)) {
if (strstarts(options->long_name + 3, arg)) {
flags |= OPT_UNSET;
goto is_abbreviated;
}
......@@ -406,7 +406,7 @@ static int parse_long_opt(struct parse_opt_ctx_t *p, const char *arg,
continue;
}
/* negated and abbreviated very much? */
if (!prefixcmp("no-", arg)) {
if (strstarts("no-", arg)) {
flags |= OPT_UNSET;
goto is_abbreviated;
}
......@@ -416,7 +416,7 @@ static int parse_long_opt(struct parse_opt_ctx_t *p, const char *arg,
flags |= OPT_UNSET;
rest = skip_prefix(arg + 3, options->long_name);
/* abbreviated and negated? */
if (!rest && !prefixcmp(options->long_name, arg + 3))
if (!rest && strstarts(options->long_name, arg + 3))
goto is_abbreviated;
if (!rest)
continue;
......@@ -456,7 +456,7 @@ static void check_typos(const char *arg, const struct option *options)
if (strlen(arg) < 3)
return;
if (!prefixcmp(arg, "no-")) {
if (strstarts(arg, "no-")) {
fprintf(stderr, " Error: did you mean `--%s` (with two dashes ?)", arg);
exit(129);
}
......@@ -464,7 +464,7 @@ static void check_typos(const char *arg, const struct option *options)
for (; options->type != OPTION_END; options++) {
if (!options->long_name)
continue;
if (!prefixcmp(options->long_name, arg)) {
if (strstarts(options->long_name, arg)) {
fprintf(stderr, " Error: did you mean `--%s` (with two dashes ?)", arg);
exit(129);
}
......@@ -933,10 +933,10 @@ int parse_options_usage(const char * const *usagestr,
if (opts->long_name == NULL)
continue;
if (!prefixcmp(opts->long_name, optstr))
if (strstarts(opts->long_name, optstr))
print_option_help(opts, 0);
if (!prefixcmp("no-", optstr) &&
!prefixcmp(opts->long_name, optstr + 3))
if (strstarts("no-", optstr) &&
strstarts(opts->long_name, optstr + 3))
print_option_help(opts, 0);
}
......
......@@ -13,6 +13,7 @@
#include "util/util.h"
#include "util/debug.h"
#include "util/config.h"
#include <linux/string.h>
static bool use_system_config, use_user_config;
......@@ -79,7 +80,7 @@ static int show_spec_config(struct perf_config_set *set, const char *var)
return -1;
perf_config_items__for_each_entry(&set->sections, section) {
if (prefixcmp(var, section->name) != 0)
if (!strstarts(var, section->name))
continue;
perf_config_items__for_each_entry(&section->items, item) {
......
......@@ -381,7 +381,7 @@ static int perf_ftrace_config(const char *var, const char *value, void *cb)
{
struct perf_ftrace *ftrace = cb;
if (prefixcmp(var, "ftrace."))
if (!strstarts(var, "ftrace."))
return 0;
if (strcmp(var, "ftrace.tracer"))
......
......@@ -90,7 +90,7 @@ static int check_emacsclient_version(void)
*/
finish_command(&ec_process);
if (prefixcmp(buffer.buf, "emacsclient")) {
if (!strstarts(buffer.buf, "emacsclient")) {
fprintf(stderr, "Failed to parse emacsclient version.\n");
goto out;
}
......@@ -283,7 +283,7 @@ static int perf_help_config(const char *var, const char *value, void *cb)
add_man_viewer(value);
return 0;
}
if (!prefixcmp(var, "man."))
if (!strstarts(var, "man."))
return add_man_viewer_info(var, value);
return 0;
......@@ -313,7 +313,7 @@ static const char *cmd_to_page(const char *perf_cmd)
if (!perf_cmd)
return "perf";
else if (!prefixcmp(perf_cmd, "perf"))
else if (!strstarts(perf_cmd, "perf"))
return perf_cmd;
return asprintf(&s, "perf-%s", perf_cmd) < 0 ? NULL : s;
......
......@@ -89,7 +89,7 @@ struct pager_config {
static int pager_command_config(const char *var, const char *value, void *data)
{
struct pager_config *c = data;
if (!prefixcmp(var, "pager.") && !strcmp(var + 6, c->cmd))
if (strstarts(var, "pager.") && !strcmp(var + 6, c->cmd))
c->val = perf_config_bool(var, value);
return 0;
}
......@@ -108,9 +108,9 @@ static int check_pager_config(const char *cmd)
static int browser_command_config(const char *var, const char *value, void *data)
{
struct pager_config *c = data;
if (!prefixcmp(var, "tui.") && !strcmp(var + 4, c->cmd))
if (strstarts(var, "tui.") && !strcmp(var + 4, c->cmd))
c->val = perf_config_bool(var, value);
if (!prefixcmp(var, "gtk.") && !strcmp(var + 4, c->cmd))
if (strstarts(var, "gtk.") && !strcmp(var + 4, c->cmd))
c->val = perf_config_bool(var, value) ? 2 : 0;
return 0;
}
......@@ -192,7 +192,7 @@ static int handle_options(const char ***argv, int *argc, int *envchanged)
/*
* Check remaining flags.
*/
if (!prefixcmp(cmd, CMD_EXEC_PATH)) {
if (strstarts(cmd, CMD_EXEC_PATH)) {
cmd += strlen(CMD_EXEC_PATH);
if (*cmd == '=')
set_argv_exec_path(cmd + 1);
......@@ -229,7 +229,7 @@ static int handle_options(const char ***argv, int *argc, int *envchanged)
*envchanged = 1;
(*argv)++;
(*argc)--;
} else if (!prefixcmp(cmd, CMD_DEBUGFS_DIR)) {
} else if (strstarts(cmd, CMD_DEBUGFS_DIR)) {
tracing_path_set(cmd + strlen(CMD_DEBUGFS_DIR));
fprintf(stderr, "dir: %s\n", tracing_path);
if (envchanged)
......@@ -470,14 +470,14 @@ int main(int argc, const char **argv)
* So we just directly call the internal command handler, and
* die if that one cannot handle it.
*/
if (!prefixcmp(cmd, "perf-")) {
if (strstarts(cmd, "perf-")) {
cmd += 5;
argv[0] = cmd;
handle_internal_command(argc, argv);
fprintf(stderr, "cannot handle %s internally", cmd);
goto out;
}
if (!prefixcmp(cmd, "trace")) {
if (strstarts(cmd, "trace")) {
#ifdef HAVE_LIBAUDIT_SUPPORT
setup_path();
argv[0] = "trace";
......@@ -495,7 +495,7 @@ int main(int argc, const char **argv)
commit_pager_choice();
if (argc > 0) {
if (!prefixcmp(argv[0], "--"))
if (strstarts(argv[0], "--"))
argv[0] += 2;
} else {
/* The user didn't specify a command; give them help */
......
......@@ -8,6 +8,7 @@
#include <linux/compiler.h>
#include <linux/list.h>
#include <linux/rbtree.h>
#include <linux/string.h>
#include <stdlib.h>
#include <sys/ttydefaults.h>
#include "browser.h"
......@@ -563,7 +564,7 @@ static int ui_browser__color_config(const char *var, const char *value,
int i;
/* same dir for all commands */
if (prefixcmp(var, "colors.") != 0)
if (!strstarts(var, "colors.") != 0)
return 0;
for (i = 0; ui_browser__colorsets[i].name != NULL; ++i) {
......
......@@ -13,6 +13,7 @@
#include <inttypes.h>
#include <pthread.h>
#include <linux/kernel.h>
#include <linux/string.h>
#include <sys/ttydefaults.h>
struct disasm_line_samples {
......@@ -1198,7 +1199,7 @@ static int annotate__config(const char *var, const char *value,
struct annotate_config *cfg;
const char *name;
if (prefixcmp(var, "annotate.") != 0)
if (!strstarts(var, "annotate."))
return 0;
name = var + 9;
......
#include <stdio.h>
#include <linux/string.h>
#include "../../util/util.h"
#include "../../util/hist.h"
......@@ -292,7 +293,7 @@ static size_t callchain__fprintf_graph(FILE *fp, struct rb_root *root,
* displayed twice.
*/
if (!i++ && field_order == NULL &&
sort_order && !prefixcmp(sort_order, "sym"))
sort_order && strstarts(sort_order, "sym"))
continue;
if (!printed) {
......
......@@ -1246,7 +1246,7 @@ int bpf__config_obj(struct bpf_object *obj,
if (!obj || !term || !term->config)
return -EINVAL;
if (!prefixcmp(term->config, "map:")) {
if (strstarts(term->config, "map:")) {
key_scan_pos = sizeof("map:") - 1;
err = bpf__obj_config_map(obj, term, evlist, &key_scan_pos);
goto out;
......
......@@ -304,7 +304,7 @@ int perf_callchain_config(const char *var, const char *value)
{
char *endptr;
if (prefixcmp(var, "call-graph."))
if (!strstarts(var, "call-graph."))
return 0;
var += sizeof("call-graph.") - 1;
......
......@@ -19,6 +19,7 @@
#include <sys/types.h>
#include <sys/stat.h>
#include <unistd.h>
#include <linux/string.h>
#include "sane_ctype.h"
......@@ -433,22 +434,22 @@ static int perf_ui_config(const char *var, const char *value)
int perf_default_config(const char *var, const char *value,
void *dummy __maybe_unused)
{
if (!prefixcmp(var, "core."))
if (strstarts(var, "core."))
return perf_default_core_config(var, value);
if (!prefixcmp(var, "hist."))
if (strstarts(var, "hist."))
return perf_hist_config(var, value);
if (!prefixcmp(var, "ui."))
if (strstarts(var, "ui."))
return perf_ui_config(var, value);
if (!prefixcmp(var, "call-graph."))
if (strstarts(var, "call-graph."))
return perf_callchain_config(var, value);
if (!prefixcmp(var, "llvm."))
if (strstarts(var, "llvm."))
return perf_llvm_config(var, value);
if (!prefixcmp(var, "buildid."))
if (strstarts(var, "buildid."))
return perf_buildid_config(var, value);
/* Add other config variables here. */
......
......@@ -33,7 +33,7 @@ struct llvm_param llvm_param = {
int perf_llvm_config(const char *var, const char *value)
{
if (prefixcmp(var, "llvm."))
if (!strstarts(var, "llvm."))
return 0;
var += sizeof("llvm.") - 1;
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment