]>
Commit | Line | Data |
---|---|---|
30862f2c TS |
1 | /* |
2 | * builtin-config.c | |
3 | * | |
4 | * Copyright (C) 2015, Taeung Song <treeze.taeung@gmail.com> | |
5 | * | |
6 | */ | |
7 | #include "builtin.h" | |
8 | ||
9 | #include "perf.h" | |
10 | ||
11 | #include "util/cache.h" | |
4b6ab94e | 12 | #include <subcmd/parse-options.h> |
30862f2c TS |
13 | #include "util/util.h" |
14 | #include "util/debug.h" | |
860b8d4b | 15 | #include "util/config.h" |
30862f2c | 16 | |
c7ac2417 TS |
17 | static bool use_system_config, use_user_config; |
18 | ||
30862f2c | 19 | static const char * const config_usage[] = { |
90923608 | 20 | "perf config [<file-option>] [options] [section.name ...]", |
30862f2c TS |
21 | NULL |
22 | }; | |
23 | ||
24 | enum actions { | |
25 | ACTION_LIST = 1 | |
26 | } actions; | |
27 | ||
28 | static struct option config_options[] = { | |
29 | OPT_SET_UINT('l', "list", &actions, | |
30 | "show current config variables", ACTION_LIST), | |
c7ac2417 TS |
31 | OPT_BOOLEAN(0, "system", &use_system_config, "use system config file"), |
32 | OPT_BOOLEAN(0, "user", &use_user_config, "use user config file"), | |
30862f2c TS |
33 | OPT_END() |
34 | }; | |
35 | ||
90923608 TS |
36 | static int show_spec_config(struct perf_config_set *set, const char *var) |
37 | { | |
38 | struct perf_config_section *section; | |
39 | struct perf_config_item *item; | |
40 | ||
41 | if (set == NULL) | |
42 | return -1; | |
43 | ||
44 | perf_config_items__for_each_entry(&set->sections, section) { | |
45 | if (prefixcmp(var, section->name) != 0) | |
46 | continue; | |
47 | ||
48 | perf_config_items__for_each_entry(§ion->items, item) { | |
49 | const char *name = var + strlen(section->name) + 1; | |
50 | ||
51 | if (strcmp(name, item->name) == 0) { | |
52 | char *value = item->value; | |
53 | ||
54 | if (value) { | |
55 | printf("%s=%s\n", var, value); | |
56 | return 0; | |
57 | } | |
58 | } | |
59 | ||
60 | } | |
61 | } | |
62 | ||
63 | return 0; | |
64 | } | |
65 | ||
860b8d4b | 66 | static int show_config(struct perf_config_set *set) |
30862f2c | 67 | { |
860b8d4b TS |
68 | struct perf_config_section *section; |
69 | struct perf_config_item *item; | |
860b8d4b TS |
70 | |
71 | if (set == NULL) | |
72 | return -1; | |
73 | ||
4a35b349 TS |
74 | perf_config_set__for_each_entry(set, section, item) { |
75 | char *value = item->value; | |
860b8d4b | 76 | |
4a35b349 TS |
77 | if (value) |
78 | printf("%s.%s=%s\n", section->name, | |
79 | item->name, value); | |
860b8d4b | 80 | } |
30862f2c TS |
81 | |
82 | return 0; | |
83 | } | |
84 | ||
85 | int cmd_config(int argc, const char **argv, const char *prefix __maybe_unused) | |
86 | { | |
90923608 | 87 | int i, ret = 0; |
860b8d4b | 88 | struct perf_config_set *set; |
c7ac2417 | 89 | char *user_config = mkpath("%s/.perfconfig", getenv("HOME")); |
30862f2c TS |
90 | |
91 | argc = parse_options(argc, argv, config_options, config_usage, | |
92 | PARSE_OPT_STOP_AT_NON_OPTION); | |
93 | ||
c7ac2417 TS |
94 | if (use_system_config && use_user_config) { |
95 | pr_err("Error: only one config file at a time\n"); | |
96 | parse_options_usage(config_usage, config_options, "user", 0); | |
97 | parse_options_usage(NULL, config_options, "system", 0); | |
98 | return -1; | |
99 | } | |
100 | ||
101 | if (use_system_config) | |
102 | config_exclusive_filename = perf_etc_perfconfig(); | |
103 | else if (use_user_config) | |
104 | config_exclusive_filename = user_config; | |
105 | ||
8a0a9c7e TS |
106 | /* |
107 | * At only 'config' sub-command, individually use the config set | |
108 | * because of reinitializing with options config file location. | |
109 | */ | |
860b8d4b TS |
110 | set = perf_config_set__new(); |
111 | if (!set) { | |
112 | ret = -1; | |
113 | goto out_err; | |
114 | } | |
115 | ||
30862f2c TS |
116 | switch (actions) { |
117 | case ACTION_LIST: | |
118 | if (argc) { | |
119 | pr_err("Error: takes no arguments\n"); | |
120 | parse_options_usage(config_usage, config_options, "l", 1); | |
121 | } else { | |
860b8d4b | 122 | ret = show_config(set); |
c7ac2417 TS |
123 | if (ret < 0) { |
124 | const char * config_filename = config_exclusive_filename; | |
125 | if (!config_exclusive_filename) | |
126 | config_filename = user_config; | |
30862f2c | 127 | pr_err("Nothing configured, " |
c7ac2417 TS |
128 | "please check your %s \n", config_filename); |
129 | } | |
30862f2c TS |
130 | } |
131 | break; | |
132 | default: | |
90923608 TS |
133 | if (argc) |
134 | for (i = 0; argv[i]; i++) | |
135 | ret = show_spec_config(set, argv[i]); | |
136 | else | |
137 | usage_with_options(config_usage, config_options); | |
30862f2c TS |
138 | } |
139 | ||
860b8d4b TS |
140 | perf_config_set__delete(set); |
141 | out_err: | |
30862f2c TS |
142 | return ret; |
143 | } |