Linux 5.1.15
[linux/fpc-iii.git] / tools / perf / builtin-config.c
blobd76f831f94c780adc49ce5e750c0b92a26aae73b
1 // SPDX-License-Identifier: GPL-2.0
2 /*
3 * builtin-config.c
5 * Copyright (C) 2015, Taeung Song <treeze.taeung@gmail.com>
7 */
8 #include "builtin.h"
10 #include "perf.h"
12 #include "util/cache.h"
13 #include <subcmd/parse-options.h>
14 #include "util/util.h"
15 #include "util/debug.h"
16 #include "util/config.h"
17 #include <linux/string.h>
19 static bool use_system_config, use_user_config;
21 static const char * const config_usage[] = {
22 "perf config [<file-option>] [options] [section.name[=value] ...]",
23 NULL
26 enum actions {
27 ACTION_LIST = 1
28 } actions;
30 static struct option config_options[] = {
31 OPT_SET_UINT('l', "list", &actions,
32 "show current config variables", ACTION_LIST),
33 OPT_BOOLEAN(0, "system", &use_system_config, "use system config file"),
34 OPT_BOOLEAN(0, "user", &use_user_config, "use user config file"),
35 OPT_END()
38 static int set_config(struct perf_config_set *set, const char *file_name)
40 struct perf_config_section *section = NULL;
41 struct perf_config_item *item = NULL;
42 const char *first_line = "# this file is auto-generated.";
43 FILE *fp;
45 if (set == NULL)
46 return -1;
48 fp = fopen(file_name, "w");
49 if (!fp)
50 return -1;
52 fprintf(fp, "%s\n", first_line);
54 /* overwrite configvariables */
55 perf_config_items__for_each_entry(&set->sections, section) {
56 if (!use_system_config && section->from_system_config)
57 continue;
58 fprintf(fp, "[%s]\n", section->name);
60 perf_config_items__for_each_entry(&section->items, item) {
61 if (!use_system_config && item->from_system_config)
62 continue;
63 if (item->value)
64 fprintf(fp, "\t%s = %s\n",
65 item->name, item->value);
68 fclose(fp);
70 return 0;
73 static int show_spec_config(struct perf_config_set *set, const char *var)
75 struct perf_config_section *section;
76 struct perf_config_item *item;
78 if (set == NULL)
79 return -1;
81 perf_config_items__for_each_entry(&set->sections, section) {
82 if (!strstarts(var, section->name))
83 continue;
85 perf_config_items__for_each_entry(&section->items, item) {
86 const char *name = var + strlen(section->name) + 1;
88 if (strcmp(name, item->name) == 0) {
89 char *value = item->value;
91 if (value) {
92 printf("%s=%s\n", var, value);
93 return 0;
100 return 0;
103 static int show_config(struct perf_config_set *set)
105 struct perf_config_section *section;
106 struct perf_config_item *item;
108 if (set == NULL)
109 return -1;
111 perf_config_set__for_each_entry(set, section, item) {
112 char *value = item->value;
114 if (value)
115 printf("%s.%s=%s\n", section->name,
116 item->name, value);
119 return 0;
122 static int parse_config_arg(char *arg, char **var, char **value)
124 const char *last_dot = strchr(arg, '.');
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.
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;
134 if (!last_dot[1]) {
135 pr_err("The config variable does not contain a variable name: %s\n", arg);
136 return -1;
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;
154 return 0;
157 int cmd_config(int argc, const char **argv)
159 int i, ret = -1;
160 struct perf_config_set *set;
161 char *user_config = mkpath("%s/.perfconfig", getenv("HOME"));
162 const char *config_filename;
163 bool changed = false;
165 argc = parse_options(argc, argv, config_options, config_usage,
166 PARSE_OPT_STOP_AT_NON_OPTION);
168 if (use_system_config && use_user_config) {
169 pr_err("Error: only one config file at a time\n");
170 parse_options_usage(config_usage, config_options, "user", 0);
171 parse_options_usage(NULL, config_options, "system", 0);
172 return -1;
175 if (use_system_config)
176 config_exclusive_filename = perf_etc_perfconfig();
177 else if (use_user_config)
178 config_exclusive_filename = user_config;
180 if (!config_exclusive_filename)
181 config_filename = user_config;
182 else
183 config_filename = config_exclusive_filename;
186 * At only 'config' sub-command, individually use the config set
187 * because of reinitializing with options config file location.
189 set = perf_config_set__new();
190 if (!set)
191 goto out_err;
193 switch (actions) {
194 case ACTION_LIST:
195 if (argc) {
196 pr_err("Error: takes no arguments\n");
197 parse_options_usage(config_usage, config_options, "l", 1);
198 } else {
199 do_action_list:
200 if (show_config(set) < 0) {
201 pr_err("Nothing configured, "
202 "please check your %s \n", config_filename);
203 goto out_err;
206 break;
207 default:
208 if (!argc)
209 goto do_action_list;
211 for (i = 0; argv[i]; i++) {
212 char *var, *value;
213 char *arg = strdup(argv[i]);
215 if (!arg) {
216 pr_err("%s: strdup failed\n", __func__);
217 goto out_err;
220 if (parse_config_arg(arg, &var, &value) < 0) {
221 free(arg);
222 goto out_err;
225 if (value == NULL) {
226 if (show_spec_config(set, var) < 0) {
227 pr_err("%s is not configured: %s\n",
228 var, config_filename);
229 free(arg);
230 goto out_err;
232 } else {
233 if (perf_config_set__collect(set, config_filename,
234 var, value) < 0) {
235 pr_err("Failed to add '%s=%s'\n",
236 var, value);
237 free(arg);
238 goto out_err;
240 changed = true;
242 free(arg);
245 if (!changed)
246 break;
248 if (set_config(set, config_filename) < 0) {
249 pr_err("Failed to set the configs on %s\n",
250 config_filename);
251 goto out_err;
255 ret = 0;
256 out_err:
257 perf_config_set__delete(set);
258 return ret;