Merge tag 'block-5.9-2020-08-14' of git://git.kernel.dk/linux-block
[linux/fpc-iii.git] / tools / perf / util / parse-sublevel-options.c
bloba841d17ffd57c3d9f004b3d7b670780ae4f23602
1 #include <stdlib.h>
2 #include <stdint.h>
3 #include <string.h>
4 #include <stdio.h>
6 #include "util/debug.h"
7 #include "util/parse-sublevel-options.h"
9 static int parse_one_sublevel_option(const char *str,
10 struct sublevel_option *opts)
12 struct sublevel_option *opt = opts;
13 char *vstr, *s = strdup(str);
14 int v = 1;
16 if (!s) {
17 pr_err("no memory\n");
18 return -1;
21 vstr = strchr(s, '=');
22 if (vstr)
23 *vstr++ = 0;
25 while (opt->name) {
26 if (!strcmp(s, opt->name))
27 break;
28 opt++;
31 if (!opt->name) {
32 pr_err("Unknown option name '%s'\n", s);
33 free(s);
34 return -1;
37 if (vstr)
38 v = atoi(vstr);
40 *opt->value_ptr = v;
41 free(s);
42 return 0;
45 /* parse options like --foo a=<n>,b,c... */
46 int perf_parse_sublevel_options(const char *str, struct sublevel_option *opts)
48 char *s = strdup(str);
49 char *p = NULL;
50 int ret;
52 if (!s) {
53 pr_err("no memory\n");
54 return -1;
57 p = strtok(s, ",");
58 while (p) {
59 ret = parse_one_sublevel_option(p, opts);
60 if (ret) {
61 free(s);
62 return ret;
65 p = strtok(NULL, ",");
68 free(s);
69 return 0;