summaryrefslogtreecommitdiff
path: root/tools/perf/util/parse-sublevel-options.c
blob: a841d17ffd57c3d9f004b3d7b670780ae4f23602 (plain) (blame)
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
#include <stdlib.h>
#include <stdint.h>
#include <string.h>
#include <stdio.h>

#include "util/debug.h"
#include "util/parse-sublevel-options.h"

static int parse_one_sublevel_option(const char *str,
				     struct sublevel_option *opts)
{
	struct sublevel_option *opt = opts;
	char *vstr, *s = strdup(str);
	int v = 1;

	if (!s) {
		pr_err("no memory\n");
		return -1;
	}

	vstr = strchr(s, '=');
	if (vstr)
		*vstr++ = 0;

	while (opt->name) {
		if (!strcmp(s, opt->name))
			break;
		opt++;
	}

	if (!opt->name) {
		pr_err("Unknown option name '%s'\n", s);
		free(s);
		return -1;
	}

	if (vstr)
		v = atoi(vstr);

	*opt->value_ptr = v;
	free(s);
	return 0;
}

/* parse options like --foo a=<n>,b,c... */
int perf_parse_sublevel_options(const char *str, struct sublevel_option *opts)
{
	char *s = strdup(str);
	char *p = NULL;
	int ret;

	if (!s) {
		pr_err("no memory\n");
		return -1;
	}

	p = strtok(s, ",");
	while (p) {
		ret = parse_one_sublevel_option(p, opts);
		if (ret) {
			free(s);
			return ret;
		}

		p = strtok(NULL, ",");
	}

	free(s);
	return 0;
}