blob: f4e3444d90ab4dff70ee182802f4b3c325d802e5 [file] [log] [blame]
Hitoshi Mitaked2fb8b42009-11-15 20:36:53 +09001#include "util.h"
Jiri Olsab232e072012-09-10 18:50:17 +02002#include "linux/string.h"
Arnaldo Carvalho de Meloea5cc872009-06-01 22:31:03 -03003
Hitoshi Mitaked2fb8b42009-11-15 20:36:53 +09004#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{
Al Viro8ba7f6c2014-08-29 12:37:29 -040012 s64 length;
13 char *p;
14 char c;
Hitoshi Mitaked2fb8b42009-11-15 20:36:53 +090015
16 if (!isdigit(str[0]))
17 goto out_err;
18
Al Viro8ba7f6c2014-08-29 12:37:29 -040019 length = strtoll(str, &p, 10);
20 switch (c = *p++) {
21 case 'b': case 'B':
22 if (*p)
Hitoshi Mitaked2fb8b42009-11-15 20:36:53 +090023 goto out_err;
Arnaldo Carvalho de Melo76efd702017-02-08 17:01:46 -030024
25 __fallthrough;
Al Viro8ba7f6c2014-08-29 12:37:29 -040026 case '\0':
27 return length;
Hitoshi Mitaked2fb8b42009-11-15 20:36:53 +090028 default:
Al Viro8ba7f6c2014-08-29 12:37:29 -040029 goto out_err;
30 /* two-letter suffices */
31 case 'k': case 'K':
32 length <<= 10;
Hitoshi Mitaked2fb8b42009-11-15 20:36:53 +090033 break;
Al Viro8ba7f6c2014-08-29 12:37:29 -040034 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;
Hitoshi Mitaked2fb8b42009-11-15 20:36:53 +090043 }
Al Viro8ba7f6c2014-08-29 12:37:29 -040044 /* 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;
Hitoshi Mitaked2fb8b42009-11-15 20:36:53 +090053
54out_err:
Al Viro8ba7f6c2014-08-29 12:37:29 -040055 return -1;
Hitoshi Mitaked2fb8b42009-11-15 20:36:53 +090056}
Masami Hiramatsue1c01d62009-11-30 19:20:05 -050057
58/*
59 * Helper function for splitting a string into an argv-like array.
Lucas De Marchi25985ed2011-03-30 22:57:33 -030060 * originally copied from lib/argv_split.c
Masami Hiramatsue1c01d62009-11-30 19:20:05 -050061 */
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++)
Arnaldo Carvalho de Melo74cf2492013-12-27 16:55:14 -0300103 zfree(p);
Masami Hiramatsue1c01d62009-11-30 19:20:05 -0500104
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}
Masami Hiramatsubbbb5212009-12-15 10:32:10 -0500158
Masami Hiramatsu6964cd22010-01-05 17:47:24 -0500159/* 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
Masami Hiramatsu2a9c8c32010-02-25 08:36:12 -0500197/* Glob/lazy pattern matching */
198static bool __match_glob(const char *str, const char *pat, bool ignore_space)
Masami Hiramatsubbbb5212009-12-15 10:32:10 -0500199{
200 while (*str && *pat && *pat != '*') {
Masami Hiramatsu2a9c8c32010-02-25 08:36:12 -0500201 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 }
Masami Hiramatsu6964cd22010-01-05 17:47:24 -0500212 if (*pat == '?') { /* Matches any single character */
Masami Hiramatsubbbb5212009-12-15 10:32:10 -0500213 str++;
214 pat++;
Masami Hiramatsu6964cd22010-01-05 17:47:24 -0500215 continue;
216 } else if (*pat == '[') /* Character classes/Ranges */
217 if (__match_charclass(pat + 1, *str, &pat)) {
218 str++;
219 continue;
220 } else
Masami Hiramatsubbbb5212009-12-15 10:32:10 -0500221 return false;
Masami Hiramatsu6964cd22010-01-05 17:47:24 -0500222 else if (*pat == '\\') /* Escaped char match as normal char */
223 pat++;
224 if (*str++ != *pat++)
225 return false;
Masami Hiramatsubbbb5212009-12-15 10:32:10 -0500226 }
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)
Masami Hiramatsuea187cfb2010-12-17 22:12:00 +0900234 if (__match_glob(str++, pat, ignore_space))
Masami Hiramatsubbbb5212009-12-15 10:32:10 -0500235 return true;
236 }
237 return !*str && !*pat;
238}
239
Masami Hiramatsu2a9c8c32010-02-25 08:36:12 -0500240/**
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}
Masami Hiramatsubad03ae2011-06-27 16:27:15 +0900269
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
Arnaldo Carvalho de Melocb1a28a2012-06-07 18:23:31 -0300288/**
Jiri Olsaea36c462012-10-27 23:18:31 +0200289 * 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/**
Namhyung Kim08aa9cc2013-01-22 18:09:41 +0900307 * 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/**
Arnaldo Carvalho de Melocb1a28a2012-06-07 18:23:31 -0300325 * 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}
Jiri Olsab232e072012-09-10 18:50:17 +0200346
Arnaldo Carvalho de Melo93ec4ce2015-07-02 17:48:23 -0300347char *asprintf_expr_inout_ints(const char *var, bool in, size_t nints, int *ints)
348{
349 /*
350 * FIXME: replace this with an expression using log10() when we
351 * find a suitable implementation, maybe the one in the dvb drivers...
352 *
353 * "%s == %d || " = log10(MAXINT) * 2 + 8 chars for the operators
354 */
355 size_t size = nints * 28 + 1; /* \0 */
356 size_t i, printed = 0;
357 char *expr = malloc(size);
358
359 if (expr) {
360 const char *or_and = "||", *eq_neq = "==";
361 char *e = expr;
362
363 if (!in) {
364 or_and = "&&";
365 eq_neq = "!=";
366 }
367
368 for (i = 0; i < nints; ++i) {
369 if (printed == size)
370 goto out_err_overflow;
371
372 if (i > 0)
373 printed += snprintf(e + printed, size - printed, " %s ", or_and);
374 printed += scnprintf(e + printed, size - printed,
375 "%s %s %d", var, eq_neq, ints[i]);
376 }
377 }
378
379 return expr;
380
381out_err_overflow:
382 free(expr);
383 return NULL;
384}