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