Commit | Line | Data |
---|---|---|
b2441318 | 1 | // SPDX-License-Identifier: GPL-2.0 |
30862f2c TS |
2 | /* |
3 | * builtin-config.c | |
4 | * | |
5 | * Copyright (C) 2015, Taeung Song <treeze.taeung@gmail.com> | |
6 | * | |
7 | */ | |
8 | #include "builtin.h" | |
9 | ||
10 | #include "perf.h" | |
11 | ||
12 | #include "util/cache.h" | |
4b6ab94e | 13 | #include <subcmd/parse-options.h> |
30862f2c TS |
14 | #include "util/util.h" |
15 | #include "util/debug.h" | |
860b8d4b | 16 | #include "util/config.h" |
8e99b6d4 | 17 | #include <linux/string.h> |
30862f2c | 18 | |
c7ac2417 TS |
19 | static bool use_system_config, use_user_config; |
20 | ||
30862f2c | 21 | static const char * const config_usage[] = { |
c6fc018a | 22 | "perf config [<file-option>] [options] [section.name[=value] ...]", |
30862f2c TS |
23 | NULL |
24 | }; | |
25 | ||
26 | enum actions { | |
27 | ACTION_LIST = 1 | |
28 | } actions; | |
29 | ||
30 | static struct option config_options[] = { | |
31 | OPT_SET_UINT('l', "list", &actions, | |
32 | "show current config variables", ACTION_LIST), | |
c7ac2417 TS |
33 | OPT_BOOLEAN(0, "system", &use_system_config, "use system config file"), |
34 | OPT_BOOLEAN(0, "user", &use_user_config, "use user config file"), | |
30862f2c TS |
35 | OPT_END() |
36 | }; | |
37 | ||
c6fc018a TS |
38 | static int set_config(struct perf_config_set *set, const char *file_name, |
39 | const char *var, const char *value) | |
40 | { | |
41 | struct perf_config_section *section = NULL; | |
42 | struct perf_config_item *item = NULL; | |
43 | const char *first_line = "# this file is auto-generated."; | |
44 | FILE *fp; | |
45 | ||
46 | if (set == NULL) | |
47 | return -1; | |
48 | ||
49 | fp = fopen(file_name, "w"); | |
50 | if (!fp) | |
51 | return -1; | |
52 | ||
08d090cf | 53 | perf_config_set__collect(set, file_name, var, value); |
c6fc018a TS |
54 | fprintf(fp, "%s\n", first_line); |
55 | ||
56 | /* overwrite configvariables */ | |
57 | perf_config_items__for_each_entry(&set->sections, section) { | |
08d090cf TS |
58 | if (!use_system_config && section->from_system_config) |
59 | continue; | |
c6fc018a TS |
60 | fprintf(fp, "[%s]\n", section->name); |
61 | ||
62 | perf_config_items__for_each_entry(§ion->items, item) { | |
cba225d6 | 63 | if (!use_system_config && item->from_system_config) |
08d090cf | 64 | continue; |
c6fc018a TS |
65 | if (item->value) |
66 | fprintf(fp, "\t%s = %s\n", | |
67 | item->name, item->value); | |
68 | } | |
69 | } | |
70 | fclose(fp); | |
71 | ||
72 | return 0; | |
73 | } | |
74 | ||
90923608 TS |
75 | static int show_spec_config(struct perf_config_set *set, const char *var) |
76 | { | |
77 | struct perf_config_section *section; | |
78 | struct perf_config_item *item; | |
79 | ||
80 | if (set == NULL) | |
81 | return -1; | |
82 | ||
83 | perf_config_items__for_each_entry(&set->sections, section) { | |
8e99b6d4 | 84 | if (!strstarts(var, section->name)) |
90923608 TS |
85 | continue; |
86 | ||
87 | perf_config_items__for_each_entry(§ion->items, item) { | |
88 | const char *name = var + strlen(section->name) + 1; | |
89 | ||
90 | if (strcmp(name, item->name) == 0) { | |
91 | char *value = item->value; | |
92 | ||
93 | if (value) { | |
94 | printf("%s=%s\n", var, value); | |
95 | return 0; | |
96 | } | |
97 | } | |
98 | ||
99 | } | |
100 | } | |
101 | ||
102 | return 0; | |
103 | } | |
104 | ||
860b8d4b | 105 | static int show_config(struct perf_config_set *set) |
30862f2c | 106 | { |
860b8d4b TS |
107 | struct perf_config_section *section; |
108 | struct perf_config_item *item; | |
860b8d4b TS |
109 | |
110 | if (set == NULL) | |
111 | return -1; | |
112 | ||
4a35b349 TS |
113 | perf_config_set__for_each_entry(set, section, item) { |
114 | char *value = item->value; | |
860b8d4b | 115 | |
4a35b349 TS |
116 | if (value) |
117 | printf("%s.%s=%s\n", section->name, | |
118 | item->name, value); | |
860b8d4b | 119 | } |
30862f2c TS |
120 | |
121 | return 0; | |
122 | } | |
123 | ||
c6fc018a | 124 | static int parse_config_arg(char *arg, char **var, char **value) |
36662794 TS |
125 | { |
126 | const char *last_dot = strchr(arg, '.'); | |
127 | ||
128 | /* | |
129 | * Since "var" actually contains the section name and the real | |
130 | * config variable name separated by a dot, we have to know where the dot is. | |
131 | */ | |
132 | if (last_dot == NULL || last_dot == arg) { | |
133 | pr_err("The config variable does not contain a section name: %s\n", arg); | |
134 | return -1; | |
135 | } | |
136 | if (!last_dot[1]) { | |
137 | pr_err("The config variable does not contain a variable name: %s\n", arg); | |
138 | return -1; | |
139 | } | |
140 | ||
c6fc018a TS |
141 | *value = strchr(arg, '='); |
142 | if (*value == NULL) | |
143 | *var = arg; | |
144 | else if (!strcmp(*value, "=")) { | |
145 | pr_err("The config variable does not contain a value: %s\n", arg); | |
146 | return -1; | |
147 | } else { | |
148 | *value = *value + 1; /* excluding a first character '=' */ | |
149 | *var = strsep(&arg, "="); | |
150 | if (*var[0] == '\0') { | |
151 | pr_err("invalid config variable: %s\n", arg); | |
152 | return -1; | |
153 | } | |
154 | } | |
155 | ||
36662794 TS |
156 | return 0; |
157 | } | |
158 | ||
b0ad8ea6 | 159 | int cmd_config(int argc, const char **argv) |
30862f2c | 160 | { |
dfe1c6d7 | 161 | int i, ret = -1; |
860b8d4b | 162 | struct perf_config_set *set; |
c7ac2417 | 163 | char *user_config = mkpath("%s/.perfconfig", getenv("HOME")); |
4341ec6b | 164 | const char *config_filename; |
30862f2c TS |
165 | |
166 | argc = parse_options(argc, argv, config_options, config_usage, | |
167 | PARSE_OPT_STOP_AT_NON_OPTION); | |
168 | ||
c7ac2417 TS |
169 | if (use_system_config && use_user_config) { |
170 | pr_err("Error: only one config file at a time\n"); | |
171 | parse_options_usage(config_usage, config_options, "user", 0); | |
172 | parse_options_usage(NULL, config_options, "system", 0); | |
173 | return -1; | |
174 | } | |
175 | ||
176 | if (use_system_config) | |
177 | config_exclusive_filename = perf_etc_perfconfig(); | |
178 | else if (use_user_config) | |
179 | config_exclusive_filename = user_config; | |
180 | ||
4341ec6b TS |
181 | if (!config_exclusive_filename) |
182 | config_filename = user_config; | |
183 | else | |
184 | config_filename = config_exclusive_filename; | |
185 | ||
8a0a9c7e TS |
186 | /* |
187 | * At only 'config' sub-command, individually use the config set | |
188 | * because of reinitializing with options config file location. | |
189 | */ | |
860b8d4b | 190 | set = perf_config_set__new(); |
dfe1c6d7 | 191 | if (!set) |
860b8d4b | 192 | goto out_err; |
860b8d4b | 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 { | |
dfe1c6d7 | 200 | if (show_config(set) < 0) { |
30862f2c | 201 | pr_err("Nothing configured, " |
c7ac2417 | 202 | "please check your %s \n", config_filename); |
dfe1c6d7 TS |
203 | goto out_err; |
204 | } | |
30862f2c TS |
205 | } |
206 | break; | |
207 | default: | |
8c1cedb4 TS |
208 | if (!argc) { |
209 | usage_with_options(config_usage, config_options); | |
210 | break; | |
211 | } | |
36662794 | 212 | |
8c1cedb4 TS |
213 | for (i = 0; argv[i]; i++) { |
214 | char *var, *value; | |
215 | char *arg = strdup(argv[i]); | |
36662794 | 216 | |
8c1cedb4 TS |
217 | if (!arg) { |
218 | pr_err("%s: strdup failed\n", __func__); | |
dfe1c6d7 | 219 | goto out_err; |
8c1cedb4 TS |
220 | } |
221 | ||
222 | if (parse_config_arg(arg, &var, &value) < 0) { | |
36662794 | 223 | free(arg); |
dfe1c6d7 | 224 | goto out_err; |
36662794 | 225 | } |
8c1cedb4 | 226 | |
4f1fd742 | 227 | if (value == NULL) { |
dfe1c6d7 | 228 | if (show_spec_config(set, var) < 0) { |
4f1fd742 TS |
229 | pr_err("%s is not configured: %s\n", |
230 | var, config_filename); | |
231 | free(arg); | |
dfe1c6d7 | 232 | goto out_err; |
4f1fd742 TS |
233 | } |
234 | } else { | |
dfe1c6d7 | 235 | if (set_config(set, config_filename, var, value) < 0) { |
4f1fd742 TS |
236 | pr_err("Failed to set '%s=%s' on %s\n", |
237 | var, value, config_filename); | |
238 | free(arg); | |
dfe1c6d7 | 239 | goto out_err; |
4f1fd742 TS |
240 | } |
241 | } | |
8c1cedb4 TS |
242 | free(arg); |
243 | } | |
30862f2c TS |
244 | } |
245 | ||
dfe1c6d7 | 246 | ret = 0; |
860b8d4b | 247 | out_err: |
dfe1c6d7 | 248 | perf_config_set__delete(set); |
30862f2c TS |
249 | return ret; |
250 | } |