[tip:perf/core] perf string: Add {strdup,strpbrk}_esc()

From: tip-bot for Masami Hiramatsu
Date: Thu Dec 28 2017 - 10:34:46 EST


Commit-ID: 1e9f9e8af0de80e8f6a47d991df66090934be0c6
Gitweb: https://git.kernel.org/tip/1e9f9e8af0de80e8f6a47d991df66090934be0c6
Author: Masami Hiramatsu <mhiramat@xxxxxxxxxx>
AuthorDate: Sat, 9 Dec 2017 01:28:41 +0900
Committer: Arnaldo Carvalho de Melo <acme@xxxxxxxxxx>
CommitDate: Wed, 27 Dec 2017 12:15:55 -0300

perf string: Add {strdup,strpbrk}_esc()

To support the special characters escaped by '\' in 'perf probe' event parser.

Signed-off-by: Masami Hiramatsu <mhiramat@xxxxxxxxxx>
Reviewed-by: Thomas Richter <tmricht@xxxxxxxxxxxxxxxxxx>
Acked-by: Ravi Bangoria <ravi.bangoria@xxxxxxxxxxxxxxxxxx>
Cc: Paul Clarke <pc@xxxxxxxxxx>
Cc: bhargavb <bhargavaramudu@xxxxxxxxx>
Cc: linux-rt-users@xxxxxxxxxxxxxxx
Link: http://lkml.kernel.org/r/151275052163.24652.18205979384585484358.stgit@devbox
[ Split from a larger patch ]
Signed-off-by: Arnaldo Carvalho de Melo <acme@xxxxxxxxxx>
---
tools/perf/util/string.c | 46 ++++++++++++++++++++++++++++++++++++++++++++++
tools/perf/util/string2.h | 2 ++
2 files changed, 48 insertions(+)

diff --git a/tools/perf/util/string.c b/tools/perf/util/string.c
index aaa08ee..d8bfd0c 100644
--- a/tools/perf/util/string.c
+++ b/tools/perf/util/string.c
@@ -396,3 +396,49 @@ out_err_overflow:
free(expr);
return NULL;
}
+
+/* Like strpbrk(), but not break if it is right after a backslash (escaped) */
+char *strpbrk_esc(char *str, const char *stopset)
+{
+ char *ptr;
+
+ do {
+ ptr = strpbrk(str, stopset);
+ if (ptr == str ||
+ (ptr == str + 1 && *(ptr - 1) != '\\'))
+ break;
+ str = ptr + 1;
+ } while (ptr && *(ptr - 1) == '\\' && *(ptr - 2) != '\\');
+
+ return ptr;
+}
+
+/* Like strdup, but do not copy a single backslash */
+char *strdup_esc(const char *str)
+{
+ char *s, *d, *p, *ret = strdup(str);
+
+ if (!ret)
+ return NULL;
+
+ d = strchr(ret, '\\');
+ if (!d)
+ return ret;
+
+ s = d + 1;
+ do {
+ if (*s == '\0') {
+ *d = '\0';
+ break;
+ }
+ p = strchr(s + 1, '\\');
+ if (p) {
+ memmove(d, s, p - s);
+ d += p - s;
+ s = p + 1;
+ } else
+ memmove(d, s, strlen(s) + 1);
+ } while (p);
+
+ return ret;
+}
diff --git a/tools/perf/util/string2.h b/tools/perf/util/string2.h
index ee14ca5..4c68a09 100644
--- a/tools/perf/util/string2.h
+++ b/tools/perf/util/string2.h
@@ -39,5 +39,7 @@ static inline char *asprintf_expr_not_in_ints(const char *var, size_t nints, int
return asprintf_expr_inout_ints(var, false, nints, ints);
}

+char *strpbrk_esc(char *str, const char *stopset);
+char *strdup_esc(const char *str);

#endif /* PERF_STRING_H */