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