]>
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[] = { |
c6fc018a | 20 | "perf config [<file-option>] [options] [section.name[=value] ...]", |
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 | ||
c6fc018a TS |
36 | static int set_config(struct perf_config_set *set, const char *file_name, |
37 | const char *var, const char *value) | |
38 | { | |
39 | struct perf_config_section *section = NULL; | |
40 | struct perf_config_item *item = NULL; | |
41 | const char *first_line = "# this file is auto-generated."; | |
42 | FILE *fp; | |
43 | ||
44 | if (set == NULL) | |
45 | return -1; | |
46 | ||
47 | fp = fopen(file_name, "w"); | |
48 | if (!fp) | |
49 | return -1; | |
50 | ||
08d090cf | 51 | perf_config_set__collect(set, file_name, var, value); |
c6fc018a TS |
52 | fprintf(fp, "%s\n", first_line); |
53 | ||
54 | /* overwrite configvariables */ | |
55 | perf_config_items__for_each_entry(&set->sections, section) { | |
08d090cf TS |
56 | if (!use_system_config && section->from_system_config) |
57 | continue; | |
c6fc018a TS |
58 | fprintf(fp, "[%s]\n", section->name); |
59 | ||
60 | perf_config_items__for_each_entry(§ion->items, item) { | |
08d090cf TS |
61 | if (!use_system_config && section->from_system_config) |
62 | continue; | |
c6fc018a TS |
63 | if (item->value) |
64 | fprintf(fp, "\t%s = %s\n", | |
65 | item->name, item->value); | |
66 | } | |
67 | } | |
68 | fclose(fp); | |
69 | ||
70 | return 0; | |
71 | } | |
72 | ||
90923608 TS |
73 | static int show_spec_config(struct perf_config_set *set, const char *var) |
74 | { | |
75 | struct perf_config_section *section; | |
76 | struct perf_config_item *item; | |
77 | ||
78 | if (set == NULL) | |
79 | return -1; | |
80 | ||
81 | perf_config_items__for_each_entry(&set->sections, section) { | |
82 | if (prefixcmp(var, section->name) != 0) | |
83 | continue; | |
84 | ||
85 | perf_config_items__for_each_entry(§ion->items, item) { | |
86 | const char *name = var + strlen(section->name) + 1; | |
87 | ||
88 | if (strcmp(name, item->name) == 0) { | |
89 | char *value = item->value; | |
90 | ||
91 | if (value) { | |
92 | printf("%s=%s\n", var, value); | |
93 | return 0; | |
94 | } | |
95 | } | |
96 | ||
97 | } | |
98 | } | |
99 | ||
100 | return 0; | |
101 | } | |
102 | ||
860b8d4b | 103 | static int show_config(struct perf_config_set *set) |
30862f2c | 104 | { |
860b8d4b TS |
105 | struct perf_config_section *section; |
106 | struct perf_config_item *item; | |
860b8d4b TS |
107 | |
108 | if (set == NULL) | |
109 | return -1; | |
110 | ||
4a35b349 TS |
111 | perf_config_set__for_each_entry(set, section, item) { |
112 | char *value = item->value; | |
860b8d4b | 113 | |
4a35b349 TS |
114 | if (value) |
115 | printf("%s.%s=%s\n", section->name, | |
116 | item->name, value); | |
860b8d4b | 117 | } |
30862f2c TS |
118 | |
119 | return 0; | |
120 | } | |
121 | ||
c6fc018a | 122 | static int parse_config_arg(char *arg, char **var, char **value) |
36662794 TS |
123 | { |
124 | const char *last_dot = strchr(arg, '.'); | |
125 | ||
126 | /* | |
127 | * Since "var" actually contains the section name and the real | |
128 | * config variable name separated by a dot, we have to know where the dot is. | |
129 | */ | |
130 | if (last_dot == NULL || last_dot == arg) { | |
131 | pr_err("The config variable does not contain a section name: %s\n", arg); | |
132 | return -1; | |
133 | } | |
134 | if (!last_dot[1]) { | |
135 | pr_err("The config variable does not contain a variable name: %s\n", arg); | |
136 | return -1; | |
137 | } | |
138 | ||
c6fc018a TS |
139 | *value = strchr(arg, '='); |
140 | if (*value == NULL) | |
141 | *var = arg; | |
142 | else if (!strcmp(*value, "=")) { | |
143 | pr_err("The config variable does not contain a value: %s\n", arg); | |
144 | return -1; | |
145 | } else { | |
146 | *value = *value + 1; /* excluding a first character '=' */ | |
147 | *var = strsep(&arg, "="); | |
148 | if (*var[0] == '\0') { | |
149 | pr_err("invalid config variable: %s\n", arg); | |
150 | return -1; | |
151 | } | |
152 | } | |
153 | ||
36662794 TS |
154 | return 0; |
155 | } | |
156 | ||
b0ad8ea6 | 157 | int cmd_config(int argc, const char **argv) |
30862f2c | 158 | { |
90923608 | 159 | int i, ret = 0; |
860b8d4b | 160 | struct perf_config_set *set; |
c7ac2417 | 161 | char *user_config = mkpath("%s/.perfconfig", getenv("HOME")); |
4341ec6b | 162 | const char *config_filename; |
30862f2c TS |
163 | |
164 | argc = parse_options(argc, argv, config_options, config_usage, | |
165 | PARSE_OPT_STOP_AT_NON_OPTION); | |
166 | ||
c7ac2417 TS |
167 | if (use_system_config && use_user_config) { |
168 | pr_err("Error: only one config file at a time\n"); | |
169 | parse_options_usage(config_usage, config_options, "user", 0); | |
170 | parse_options_usage(NULL, config_options, "system", 0); | |
171 | return -1; | |
172 | } | |
173 | ||
174 | if (use_system_config) | |
175 | config_exclusive_filename = perf_etc_perfconfig(); | |
176 | else if (use_user_config) | |
177 | config_exclusive_filename = user_config; | |
178 | ||
4341ec6b TS |
179 | if (!config_exclusive_filename) |
180 | config_filename = user_config; | |
181 | else | |
182 | config_filename = config_exclusive_filename; | |
183 | ||
8a0a9c7e TS |
184 | /* |
185 | * At only 'config' sub-command, individually use the config set | |
186 | * because of reinitializing with options config file location. | |
187 | */ | |
860b8d4b TS |
188 | set = perf_config_set__new(); |
189 | if (!set) { | |
190 | ret = -1; | |
191 | goto out_err; | |
192 | } | |
193 | ||
30862f2c TS |
194 | switch (actions) { |
195 | case ACTION_LIST: | |
196 | if (argc) { | |
197 | pr_err("Error: takes no arguments\n"); | |
198 | parse_options_usage(config_usage, config_options, "l", 1); | |
199 | } else { | |
860b8d4b | 200 | ret = show_config(set); |
4341ec6b | 201 | if (ret < 0) |
30862f2c | 202 | pr_err("Nothing configured, " |
c7ac2417 | 203 | "please check your %s \n", config_filename); |
30862f2c TS |
204 | } |
205 | break; | |
206 | default: | |
36662794 TS |
207 | if (argc) { |
208 | for (i = 0; argv[i]; i++) { | |
c6fc018a TS |
209 | char *var, *value; |
210 | char *arg = strdup(argv[i]); | |
36662794 TS |
211 | |
212 | if (!arg) { | |
213 | pr_err("%s: strdup failed\n", __func__); | |
214 | ret = -1; | |
215 | break; | |
216 | } | |
217 | ||
c6fc018a | 218 | if (parse_config_arg(arg, &var, &value) < 0) { |
36662794 TS |
219 | free(arg); |
220 | ret = -1; | |
221 | break; | |
222 | } | |
223 | ||
c6fc018a TS |
224 | if (value == NULL) |
225 | ret = show_spec_config(set, var); | |
4341ec6b | 226 | else |
c6fc018a | 227 | ret = set_config(set, config_filename, var, value); |
36662794 TS |
228 | free(arg); |
229 | } | |
230 | } else | |
90923608 | 231 | usage_with_options(config_usage, config_options); |
30862f2c TS |
232 | } |
233 | ||
860b8d4b TS |
234 | perf_config_set__delete(set); |
235 | out_err: | |
30862f2c TS |
236 | return ret; |
237 | } |