Hitoshi Mitake | d2fb8b4 | 2009-11-15 20:36:53 +0900 | [diff] [blame] | 1 | #include "util.h" |
Jiri Olsa | b232e07 | 2012-09-10 18:50:17 +0200 | [diff] [blame] | 2 | #include "linux/string.h" |
Arnaldo Carvalho de Melo | ea5cc87 | 2009-06-01 22:31:03 -0300 | [diff] [blame] | 3 | |
Hitoshi Mitake | d2fb8b4 | 2009-11-15 20:36:53 +0900 | [diff] [blame] | 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 | */ |
| 10 | s64 perf_atoll(const char *str) |
| 11 | { |
Al Viro | 8ba7f6c | 2014-08-29 12:37:29 -0400 | [diff] [blame] | 12 | s64 length; |
| 13 | char *p; |
| 14 | char c; |
Hitoshi Mitake | d2fb8b4 | 2009-11-15 20:36:53 +0900 | [diff] [blame] | 15 | |
| 16 | if (!isdigit(str[0])) |
| 17 | goto out_err; |
| 18 | |
Al Viro | 8ba7f6c | 2014-08-29 12:37:29 -0400 | [diff] [blame] | 19 | length = strtoll(str, &p, 10); |
| 20 | switch (c = *p++) { |
| 21 | case 'b': case 'B': |
| 22 | if (*p) |
Hitoshi Mitake | d2fb8b4 | 2009-11-15 20:36:53 +0900 | [diff] [blame] | 23 | goto out_err; |
Arnaldo Carvalho de Melo | 76efd70 | 2017-02-08 17:01:46 -0300 | [diff] [blame] | 24 | |
| 25 | __fallthrough; |
Al Viro | 8ba7f6c | 2014-08-29 12:37:29 -0400 | [diff] [blame] | 26 | case '\0': |
| 27 | return length; |
Hitoshi Mitake | d2fb8b4 | 2009-11-15 20:36:53 +0900 | [diff] [blame] | 28 | default: |
Al Viro | 8ba7f6c | 2014-08-29 12:37:29 -0400 | [diff] [blame] | 29 | goto out_err; |
| 30 | /* two-letter suffices */ |
| 31 | case 'k': case 'K': |
| 32 | length <<= 10; |
Hitoshi Mitake | d2fb8b4 | 2009-11-15 20:36:53 +0900 | [diff] [blame] | 33 | break; |
Al Viro | 8ba7f6c | 2014-08-29 12:37:29 -0400 | [diff] [blame] | 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; |
Hitoshi Mitake | d2fb8b4 | 2009-11-15 20:36:53 +0900 | [diff] [blame] | 43 | } |
Al Viro | 8ba7f6c | 2014-08-29 12:37:29 -0400 | [diff] [blame] | 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; |
Hitoshi Mitake | d2fb8b4 | 2009-11-15 20:36:53 +0900 | [diff] [blame] | 53 | |
| 54 | out_err: |
Al Viro | 8ba7f6c | 2014-08-29 12:37:29 -0400 | [diff] [blame] | 55 | return -1; |
Hitoshi Mitake | d2fb8b4 | 2009-11-15 20:36:53 +0900 | [diff] [blame] | 56 | } |
Masami Hiramatsu | e1c01d6 | 2009-11-30 19:20:05 -0500 | [diff] [blame] | 57 | |
| 58 | /* |
| 59 | * Helper function for splitting a string into an argv-like array. |
Lucas De Marchi | 25985ed | 2011-03-30 22:57:33 -0300 | [diff] [blame] | 60 | * originally copied from lib/argv_split.c |
Masami Hiramatsu | e1c01d6 | 2009-11-30 19:20:05 -0500 | [diff] [blame] | 61 | */ |
| 62 | static const char *skip_sep(const char *cp) |
| 63 | { |
| 64 | while (*cp && isspace(*cp)) |
| 65 | cp++; |
| 66 | |
| 67 | return cp; |
| 68 | } |
| 69 | |
| 70 | static const char *skip_arg(const char *cp) |
| 71 | { |
| 72 | while (*cp && !isspace(*cp)) |
| 73 | cp++; |
| 74 | |
| 75 | return cp; |
| 76 | } |
| 77 | |
| 78 | static 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 | */ |
| 99 | void argv_free(char **argv) |
| 100 | { |
| 101 | char **p; |
| 102 | for (p = argv; *p; p++) |
Arnaldo Carvalho de Melo | 74cf249 | 2013-12-27 16:55:14 -0300 | [diff] [blame] | 103 | zfree(p); |
Masami Hiramatsu | e1c01d6 | 2009-11-30 19:20:05 -0500 | [diff] [blame] | 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 | */ |
| 120 | char **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 | |
| 151 | out: |
| 152 | return argv; |
| 153 | |
| 154 | fail: |
| 155 | argv_free(argv); |
| 156 | return NULL; |
| 157 | } |
Masami Hiramatsu | bbbb521 | 2009-12-15 10:32:10 -0500 | [diff] [blame] | 158 | |
Masami Hiramatsu | 6964cd2 | 2010-01-05 17:47:24 -0500 | [diff] [blame] | 159 | /* Character class matching */ |
| 160 | static 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 | |
| 185 | end: |
| 186 | while (*pat && *pat != ']') /* Searching closing */ |
| 187 | pat++; |
| 188 | if (!*pat) |
| 189 | goto error; |
| 190 | *npat = pat + 1; |
| 191 | return complement ? !ret : ret; |
| 192 | |
| 193 | error: |
| 194 | return false; |
| 195 | } |
| 196 | |
Masami Hiramatsu | 2a9c8c3 | 2010-02-25 08:36:12 -0500 | [diff] [blame] | 197 | /* Glob/lazy pattern matching */ |
| 198 | static bool __match_glob(const char *str, const char *pat, bool ignore_space) |
Masami Hiramatsu | bbbb521 | 2009-12-15 10:32:10 -0500 | [diff] [blame] | 199 | { |
| 200 | while (*str && *pat && *pat != '*') { |
Masami Hiramatsu | 2a9c8c3 | 2010-02-25 08:36:12 -0500 | [diff] [blame] | 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 | } |
Masami Hiramatsu | 6964cd2 | 2010-01-05 17:47:24 -0500 | [diff] [blame] | 212 | if (*pat == '?') { /* Matches any single character */ |
Masami Hiramatsu | bbbb521 | 2009-12-15 10:32:10 -0500 | [diff] [blame] | 213 | str++; |
| 214 | pat++; |
Masami Hiramatsu | 6964cd2 | 2010-01-05 17:47:24 -0500 | [diff] [blame] | 215 | continue; |
| 216 | } else if (*pat == '[') /* Character classes/Ranges */ |
| 217 | if (__match_charclass(pat + 1, *str, &pat)) { |
| 218 | str++; |
| 219 | continue; |
| 220 | } else |
Masami Hiramatsu | bbbb521 | 2009-12-15 10:32:10 -0500 | [diff] [blame] | 221 | return false; |
Masami Hiramatsu | 6964cd2 | 2010-01-05 17:47:24 -0500 | [diff] [blame] | 222 | else if (*pat == '\\') /* Escaped char match as normal char */ |
| 223 | pat++; |
| 224 | if (*str++ != *pat++) |
| 225 | return false; |
Masami Hiramatsu | bbbb521 | 2009-12-15 10:32:10 -0500 | [diff] [blame] | 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) |
Masami Hiramatsu | ea187cfb | 2010-12-17 22:12:00 +0900 | [diff] [blame] | 234 | if (__match_glob(str++, pat, ignore_space)) |
Masami Hiramatsu | bbbb521 | 2009-12-15 10:32:10 -0500 | [diff] [blame] | 235 | return true; |
| 236 | } |
| 237 | return !*str && !*pat; |
| 238 | } |
| 239 | |
Masami Hiramatsu | 2a9c8c3 | 2010-02-25 08:36:12 -0500 | [diff] [blame] | 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 | */ |
| 252 | bool 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 | */ |
| 265 | bool strlazymatch(const char *str, const char *pat) |
| 266 | { |
| 267 | return __match_glob(str, pat, true); |
| 268 | } |
Masami Hiramatsu | bad03ae | 2011-06-27 16:27:15 +0900 | [diff] [blame] | 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 | */ |
| 277 | int 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 Melo | cb1a28a | 2012-06-07 18:23:31 -0300 | [diff] [blame] | 288 | /** |
Jiri Olsa | ea36c46 | 2012-10-27 23:18:31 +0200 | [diff] [blame] | 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 | */ |
| 296 | char *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 Kim | 08aa9cc | 2013-01-22 18:09:41 +0900 | [diff] [blame] | 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 | */ |
| 312 | char *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 Melo | cb1a28a | 2012-06-07 18:23:31 -0300 | [diff] [blame] | 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 | */ |
| 331 | char *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 Olsa | b232e07 | 2012-09-10 18:50:17 +0200 | [diff] [blame] | 346 | |
Arnaldo Carvalho de Melo | 93ec4ce | 2015-07-02 17:48:23 -0300 | [diff] [blame] | 347 | char *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 | |
| 381 | out_err_overflow: |
| 382 | free(expr); |
| 383 | return NULL; |
| 384 | } |