]> git.proxmox.com Git - mirror_ubuntu-artful-kernel.git/blame - tools/perf/util/string.c
perf list: Make vendor event matching case insensitive
[mirror_ubuntu-artful-kernel.git] / tools / perf / util / string.c
CommitLineData
d2fb8b41 1#include "util.h"
b232e073 2#include "linux/string.h"
ea5cc87c 3
d2fb8b41
HM
4#define K 1024LL
5/*
6 * perf_atoll()
7 * Parse (\d+)(b|B|kb|KB|mb|MB|gb|GB|tb|TB) (e.g. "256MB")
8 * and return its numeric value
9 */
10s64 perf_atoll(const char *str)
11{
8ba7f6c2
AV
12 s64 length;
13 char *p;
14 char c;
d2fb8b41
HM
15
16 if (!isdigit(str[0]))
17 goto out_err;
18
8ba7f6c2
AV
19 length = strtoll(str, &p, 10);
20 switch (c = *p++) {
21 case 'b': case 'B':
22 if (*p)
d2fb8b41 23 goto out_err;
8ba7f6c2
AV
24 case '\0':
25 return length;
26 default:
27 goto out_err;
28 /* two-letter suffices */
29 case 'k': case 'K':
30 length <<= 10;
d2fb8b41 31 break;
8ba7f6c2
AV
32 case 'm': case 'M':
33 length <<= 20;
d2fb8b41 34 break;
8ba7f6c2
AV
35 case 'g': case 'G':
36 length <<= 30;
d2fb8b41 37 break;
8ba7f6c2
AV
38 case 't': case 'T':
39 length <<= 40;
d2fb8b41 40 break;
d2fb8b41 41 }
8ba7f6c2
AV
42 /* we want the cases to match */
43 if (islower(c)) {
44 if (strcmp(p, "b") != 0)
45 goto out_err;
46 } else {
47 if (strcmp(p, "B") != 0)
48 goto out_err;
49 }
50 return length;
d2fb8b41
HM
51
52out_err:
8ba7f6c2 53 return -1;
d2fb8b41 54}
e1c01d61
MH
55
56/*
57 * Helper function for splitting a string into an argv-like array.
25985edc 58 * originally copied from lib/argv_split.c
e1c01d61
MH
59 */
60static const char *skip_sep(const char *cp)
61{
62 while (*cp && isspace(*cp))
63 cp++;
64
65 return cp;
66}
67
68static const char *skip_arg(const char *cp)
69{
70 while (*cp && !isspace(*cp))
71 cp++;
72
73 return cp;
74}
75
76static int count_argc(const char *str)
77{
78 int count = 0;
79
80 while (*str) {
81 str = skip_sep(str);
82 if (*str) {
83 count++;
84 str = skip_arg(str);
85 }
86 }
87
88 return count;
89}
90
91/**
92 * argv_free - free an argv
93 * @argv - the argument vector to be freed
94 *
95 * Frees an argv and the strings it points to.
96 */
97void argv_free(char **argv)
98{
99 char **p;
100 for (p = argv; *p; p++)
74cf249d 101 zfree(p);
e1c01d61
MH
102
103 free(argv);
104}
105
106/**
107 * argv_split - split a string at whitespace, returning an argv
108 * @str: the string to be split
109 * @argcp: returned argument count
110 *
111 * Returns an array of pointers to strings which are split out from
112 * @str. This is performed by strictly splitting on white-space; no
113 * quote processing is performed. Multiple whitespace characters are
114 * considered to be a single argument separator. The returned array
115 * is always NULL-terminated. Returns NULL on memory allocation
116 * failure.
117 */
118char **argv_split(const char *str, int *argcp)
119{
120 int argc = count_argc(str);
121 char **argv = zalloc(sizeof(*argv) * (argc+1));
122 char **argvp;
123
124 if (argv == NULL)
125 goto out;
126
127 if (argcp)
128 *argcp = argc;
129
130 argvp = argv;
131
132 while (*str) {
133 str = skip_sep(str);
134
135 if (*str) {
136 const char *p = str;
137 char *t;
138
139 str = skip_arg(str);
140
141 t = strndup(p, str-p);
142 if (t == NULL)
143 goto fail;
144 *argvp++ = t;
145 }
146 }
147 *argvp = NULL;
148
149out:
150 return argv;
151
152fail:
153 argv_free(argv);
154 return NULL;
155}
bbbb521b 156
6964cd2c
MH
157/* Character class matching */
158static bool __match_charclass(const char *pat, char c, const char **npat)
159{
160 bool complement = false, ret = true;
161
162 if (*pat == '!') {
163 complement = true;
164 pat++;
165 }
166 if (*pat++ == c) /* First character is special */
167 goto end;
168
169 while (*pat && *pat != ']') { /* Matching */
170 if (*pat == '-' && *(pat + 1) != ']') { /* Range */
171 if (*(pat - 1) <= c && c <= *(pat + 1))
172 goto end;
173 if (*(pat - 1) > *(pat + 1))
174 goto error;
175 pat += 2;
176 } else if (*pat++ == c)
177 goto end;
178 }
179 if (!*pat)
180 goto error;
181 ret = false;
182
183end:
184 while (*pat && *pat != ']') /* Searching closing */
185 pat++;
186 if (!*pat)
187 goto error;
188 *npat = pat + 1;
189 return complement ? !ret : ret;
190
191error:
192 return false;
193}
194
2a9c8c36 195/* Glob/lazy pattern matching */
38d14f0c
AK
196static bool __match_glob(const char *str, const char *pat, bool ignore_space,
197 bool case_ins)
bbbb521b
MH
198{
199 while (*str && *pat && *pat != '*') {
2a9c8c36
MH
200 if (ignore_space) {
201 /* Ignore spaces for lazy matching */
202 if (isspace(*str)) {
203 str++;
204 continue;
205 }
206 if (isspace(*pat)) {
207 pat++;
208 continue;
209 }
210 }
6964cd2c 211 if (*pat == '?') { /* Matches any single character */
bbbb521b
MH
212 str++;
213 pat++;
6964cd2c
MH
214 continue;
215 } else if (*pat == '[') /* Character classes/Ranges */
216 if (__match_charclass(pat + 1, *str, &pat)) {
217 str++;
218 continue;
219 } else
bbbb521b 220 return false;
6964cd2c
MH
221 else if (*pat == '\\') /* Escaped char match as normal char */
222 pat++;
38d14f0c
AK
223 if (case_ins) {
224 if (tolower(*str) != tolower(*pat))
225 return false;
226 } else if (*str != *pat)
6964cd2c 227 return false;
38d14f0c
AK
228 str++;
229 pat++;
bbbb521b
MH
230 }
231 /* Check wild card */
232 if (*pat == '*') {
233 while (*pat == '*')
234 pat++;
235 if (!*pat) /* Tail wild card matches all */
236 return true;
237 while (*str)
38d14f0c 238 if (__match_glob(str++, pat, ignore_space, case_ins))
bbbb521b
MH
239 return true;
240 }
241 return !*str && !*pat;
242}
243
2a9c8c36
MH
244/**
245 * strglobmatch - glob expression pattern matching
246 * @str: the target string to match
247 * @pat: the pattern string to match
248 *
249 * This returns true if the @str matches @pat. @pat can includes wildcards
250 * ('*','?') and character classes ([CHARS], complementation and ranges are
251 * also supported). Also, this supports escape character ('\') to use special
252 * characters as normal character.
253 *
254 * Note: if @pat syntax is broken, this always returns false.
255 */
256bool strglobmatch(const char *str, const char *pat)
257{
38d14f0c
AK
258 return __match_glob(str, pat, false, false);
259}
260
261bool strglobmatch_nocase(const char *str, const char *pat)
262{
263 return __match_glob(str, pat, false, true);
2a9c8c36
MH
264}
265
266/**
267 * strlazymatch - matching pattern strings lazily with glob pattern
268 * @str: the target string to match
269 * @pat: the pattern string to match
270 *
271 * This is similar to strglobmatch, except this ignores spaces in
272 * the target string.
273 */
274bool strlazymatch(const char *str, const char *pat)
275{
38d14f0c 276 return __match_glob(str, pat, true, false);
2a9c8c36 277}
bad03ae4
MH
278
279/**
280 * strtailcmp - Compare the tail of two strings
281 * @s1: 1st string to be compared
282 * @s2: 2nd string to be compared
283 *
284 * Return 0 if whole of either string is same as another's tail part.
285 */
286int strtailcmp(const char *s1, const char *s2)
287{
288 int i1 = strlen(s1);
289 int i2 = strlen(s2);
290 while (--i1 >= 0 && --i2 >= 0) {
291 if (s1[i1] != s2[i2])
292 return s1[i1] - s2[i2];
293 }
294 return 0;
295}
296
ea36c46b
JO
297/**
298 * strxfrchar - Locate and replace character in @s
299 * @s: The string to be searched/changed.
300 * @from: Source character to be replaced.
301 * @to: Destination character.
302 *
303 * Return pointer to the changed string.
304 */
305char *strxfrchar(char *s, char from, char to)
306{
307 char *p = s;
308
309 while ((p = strchr(p, from)) != NULL)
310 *p++ = to;
311
312 return s;
313}
314
08aa9cce
NK
315/**
316 * ltrim - Removes leading whitespace from @s.
317 * @s: The string to be stripped.
318 *
319 * Return pointer to the first non-whitespace character in @s.
320 */
321char *ltrim(char *s)
322{
323 int len = strlen(s);
324
325 while (len && isspace(*s)) {
326 len--;
327 s++;
328 }
329
330 return s;
331}
332
cb1a28a0
ACM
333/**
334 * rtrim - Removes trailing whitespace from @s.
335 * @s: The string to be stripped.
336 *
337 * Note that the first trailing whitespace is replaced with a %NUL-terminator
338 * in the given string @s. Returns @s.
339 */
340char *rtrim(char *s)
341{
342 size_t size = strlen(s);
343 char *end;
344
345 if (!size)
346 return s;
347
348 end = s + size - 1;
349 while (end >= s && isspace(*end))
350 end--;
351 *(end + 1) = '\0';
352
353 return s;
354}
b232e073 355
93ec4ce7
ACM
356char *asprintf_expr_inout_ints(const char *var, bool in, size_t nints, int *ints)
357{
358 /*
359 * FIXME: replace this with an expression using log10() when we
360 * find a suitable implementation, maybe the one in the dvb drivers...
361 *
362 * "%s == %d || " = log10(MAXINT) * 2 + 8 chars for the operators
363 */
364 size_t size = nints * 28 + 1; /* \0 */
365 size_t i, printed = 0;
366 char *expr = malloc(size);
367
368 if (expr) {
369 const char *or_and = "||", *eq_neq = "==";
370 char *e = expr;
371
372 if (!in) {
373 or_and = "&&";
374 eq_neq = "!=";
375 }
376
377 for (i = 0; i < nints; ++i) {
378 if (printed == size)
379 goto out_err_overflow;
380
381 if (i > 0)
382 printed += snprintf(e + printed, size - printed, " %s ", or_and);
383 printed += scnprintf(e + printed, size - printed,
384 "%s %s %d", var, eq_neq, ints[i]);
385 }
386 }
387
388 return expr;
389
390out_err_overflow:
391 free(expr);
392 return NULL;
393}