1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
|
// SPDX-License-Identifier: GPL-2.0
#include "string2.h"
#include <linux/kernel.h>
#include <linux/string.h>
#include <stdlib.h>
#include <linux/ctype.h>
const char *graph_dotted_line =
"---------------------------------------------------------------------"
"---------------------------------------------------------------------"
"---------------------------------------------------------------------";
const char *dots =
"....................................................................."
"....................................................................."
".....................................................................";
/*
* perf_atoll()
* Parse (\d+)(b|B|kb|KB|mb|MB|gb|GB|tb|TB) (e.g. "256MB")
* and return its numeric value
*/
s64 perf_atoll(const char *str)
{
s64 length;
char *p;
char c;
if (!isdigit(str[0]))
goto out_err;
length = strtoll(str, &p, 10);
switch (c = *p++) {
case 'b': case 'B':
if (*p)
goto out_err;
fallthrough;
case '\0':
return length;
default:
goto out_err;
/* two-letter suffices */
case 'k': case 'K':
length <<= 10;
break;
case 'm': case 'M':
length <<= 20;
break;
case 'g': case 'G':
length <<= 30;
break;
case 't': case 'T':
length <<= 40;
break;
}
/* we want the cases to match */
if (islower(c)) {
if (strcmp(p, "b") != 0)
goto out_err;
} else {
if (strcmp(p, "B") != 0)
goto out_err;
}
return length;
out_err:
return -1;
}
/* Character class matching */
static bool __match_charclass(const char *pat, char c, const char **npat)
{
bool complement = false, ret = true;
if (*pat == '!') {
complement = true;
pat++;
}
if (*pat++ == c) /* First character is special */
goto end;
while (*pat && *pat != ']') { /* Matching */
if (*pat == '-' && *(pat + 1) != ']') { /* Range */
if (*(pat - 1) <= c && c <= *(pat + 1))
goto end;
if (*(pat - 1) > *(pat + 1))
goto error;
pat += 2;
} else if (*pat++ == c)
goto end;
}
if (!*pat)
goto error;
ret = false;
end:
while (*pat && *pat != ']') /* Searching closing */
pat++;
if (!*pat)
goto error;
*npat = pat + 1;
return complement ? !ret : ret;
error:
return false;
}
/* Glob/lazy pattern matching */
static bool __match_glob(const char *str, const char *pat, bool ignore_space,
bool case_ins)
{
while (*str && *pat && *pat != '*') {
if (ignore_space) {
/* Ignore spaces for lazy matching */
if (isspace(*str)) {
str++;
continue;
}
if (isspace(*pat)) {
pat++;
continue;
}
}
if (*pat == '?') { /* Matches any single character */
str++;
pat++;
continue;
} else if (*pat == '[') /* Character classes/Ranges */
if (__match_charclass(pat + 1, *str, &pat)) {
str++;
continue;
} else
return false;
else if (*pat == '\\') /* Escaped char match as normal char */
pat++;
if (case_ins) {
if (tolower(*str) != tolower(*pat))
return false;
} else if (*str != *pat)
return false;
str++;
pat++;
}
/* Check wild card */
if (*pat == '*') {
while (*pat == '*')
pat++;
if (!*pat) /* Tail wild card matches all */
return true;
while (*str)
if (__match_glob(str++, pat, ignore_space, case_ins))
return true;
}
return !*str && !*pat;
}
/**
* strglobmatch - glob expression pattern matching
* @str: the target string to match
* @pat: the pattern string to match
*
* This returns true if the @str matches @pat. @pat can includes wildcards
* ('*','?') and character classes ([CHARS], complementation and ranges are
* also supported). Also, this supports escape character ('\') to use special
* characters as normal character.
*
* Note: if @pat syntax is broken, this always returns false.
*/
bool strglobmatch(const char *str, const char *pat)
{
return __match_glob(str, pat, false, false);
}
bool strglobmatch_nocase(const char *str, const char *pat)
{
return __match_glob(str, pat, false, true);
}
/**
* strlazymatch - matching pattern strings lazily with glob pattern
* @str: the target string to match
* @pat: the pattern string to match
*
* This is similar to strglobmatch, except this ignores spaces in
* the target string.
*/
bool strlazymatch(const char *str, const char *pat)
{
return __match_glob(str, pat, true, false);
}
/**
* strtailcmp - Compare the tail of two strings
* @s1: 1st string to be compared
* @s2: 2nd string to be compared
*
* Return 0 if whole of either string is same as another's tail part.
*/
int strtailcmp(const char *s1, const char *s2)
{
int i1 = strlen(s1);
int i2 = strlen(s2);
while (--i1 >= 0 && --i2 >= 0) {
if (s1[i1] != s2[i2])
return s1[i1] - s2[i2];
}
return 0;
}
char *asprintf_expr_inout_ints(const char *var, bool in, size_t nints, int *ints)
{
/*
* FIXME: replace this with an expression using log10() when we
* find a suitable implementation, maybe the one in the dvb drivers...
*
* "%s == %d || " = log10(MAXINT) * 2 + 8 chars for the operators
*/
size_t size = nints * 28 + 1; /* \0 */
size_t i, printed = 0;
char *expr = malloc(size);
if (expr) {
const char *or_and = "||", *eq_neq = "==";
char *e = expr;
if (!in) {
or_and = "&&";
eq_neq = "!=";
}
for (i = 0; i < nints; ++i) {
if (printed == size)
goto out_err_overflow;
if (i > 0)
printed += scnprintf(e + printed, size - printed, " %s ", or_and);
printed += scnprintf(e + printed, size - printed,
"%s %s %d", var, eq_neq, ints[i]);
}
}
return expr;
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 strpbrk_esc(), but not break if it is quoted with single/double quotes */
char *strpbrk_esq(char *str, const char *stopset)
{
char *_stopset = NULL;
char *ptr;
const char *squote = "'";
const char *dquote = "\"";
if (asprintf(&_stopset, "%s%c%c", stopset, *squote, *dquote) < 0)
return NULL;
do {
ptr = strpbrk_esc(str, _stopset);
if (!ptr)
break;
if (*ptr == *squote)
ptr = strpbrk_esc(ptr + 1, squote);
else if (*ptr == *dquote)
ptr = strpbrk_esc(ptr + 1, dquote);
else
break;
str = ptr + 1;
} while (ptr);
free(_stopset);
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;
}
/* Remove backslash right before quote and return next quote address. */
static char *remove_consumed_esc(char *str, int len, int quote)
{
char *ptr = str, *end = str + len;
while (*ptr != quote && ptr < end) {
if (*ptr == '\\' && *(ptr + 1) == quote) {
memmove(ptr, ptr + 1, end - (ptr + 1));
/* now *ptr is `quote`. */
end--;
}
ptr++;
}
return *ptr == quote ? ptr : NULL;
}
/*
* Like strdup_esc, but keep quoted string as it is (and single backslash
* before quote is removed). If there is no closed quote, return NULL.
*/
char *strdup_esq(const char *str)
{
char *d, *ret;
/* If there is no quote, return normal strdup_esc() */
d = strpbrk_esc((char *)str, "\"'");
if (!d)
return strdup_esc(str);
ret = strdup(str);
if (!ret)
return NULL;
d = ret;
do {
d = strpbrk(d, "\\\"\'");
if (!d)
break;
if (*d == '"' || *d == '\'') {
/* This is non-escaped quote */
int quote = *d;
int len = strlen(d + 1) + 1;
/*
* Remove the start quote and remove consumed escape (backslash
* before quote) and remove the end quote. If there is no end
* quote, it is the input error.
*/
memmove(d, d + 1, len);
d = remove_consumed_esc(d, len, quote);
if (!d)
goto error;
memmove(d, d + 1, strlen(d + 1) + 1);
}
if (*d == '\\') {
memmove(d, d + 1, strlen(d + 1) + 1);
if (*d == '\\') {
/* double backslash -- keep the second one. */
d++;
}
}
} while (*d != '\0');
return ret;
error:
free(ret);
return NULL;
}
unsigned int hex(char c)
{
if (c >= '0' && c <= '9')
return c - '0';
if (c >= 'a' && c <= 'f')
return c - 'a' + 10;
return c - 'A' + 10;
}
/*
* Replace all occurrences of character 'needle' in string 'haystack' with
* string 'replace'
*
* The new string could be longer so a new string is returned which must be
* freed.
*/
char *strreplace_chars(char needle, const char *haystack, const char *replace)
{
int replace_len = strlen(replace);
char *new_s, *to;
const char *loc = strchr(haystack, needle);
const char *from = haystack;
int num = 0;
/* Count occurrences */
while (loc) {
loc = strchr(loc + 1, needle);
num++;
}
/* Allocate enough space for replacements and reset first location */
new_s = malloc(strlen(haystack) + (num * (replace_len - 1) + 1));
if (!new_s)
return NULL;
loc = strchr(haystack, needle);
to = new_s;
while (loc) {
/* Copy original string up to found char and update positions */
memcpy(to, from, 1 + loc - from);
to += loc - from;
from = loc + 1;
/* Copy replacement string and update positions */
memcpy(to, replace, replace_len);
to += replace_len;
/* needle next occurrence or end of string */
loc = strchr(from, needle);
}
/* Copy any remaining chars + null */
strcpy(to, from);
return new_s;
}
|