1 // SPDX-License-Identifier: GPL-2.0
5 * Copyright (C) 2015, Taeung Song <treeze.taeung@gmail.com>
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] ...]",
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"),
38 static int set_config(struct perf_config_set *set, const char *file_name,
39 const char *var, const char *value)
41 struct perf_config_section *section = NULL;
42 struct perf_config_item *item = NULL;
43 const char *first_line = "# this file is auto-generated.";
49 fp = fopen(file_name, "w");
53 perf_config_set__collect(set, file_name, var, value);
54 fprintf(fp, "%s\n", first_line);
56 /* overwrite configvariables */
57 perf_config_items__for_each_entry(&set->sections, section) {
58 if (!use_system_config && section->from_system_config)
60 fprintf(fp, "[%s]\n", section->name);
62 perf_config_items__for_each_entry(§ion->items, item) {
63 if (!use_system_config && item->from_system_config)
66 fprintf(fp, "\t%s = %s\n",
67 item->name, item->value);
75 static int show_spec_config(struct perf_config_set *set, const char *var)
77 struct perf_config_section *section;
78 struct perf_config_item *item;
83 perf_config_items__for_each_entry(&set->sections, section) {
84 if (!strstarts(var, section->name))
87 perf_config_items__for_each_entry(§ion->items, item) {
88 const char *name = var + strlen(section->name) + 1;
90 if (strcmp(name, item->name) == 0) {
91 char *value = item->value;
94 printf("%s=%s\n", var, value);
105 static int show_config(struct perf_config_set *set)
107 struct perf_config_section *section;
108 struct perf_config_item *item;
113 perf_config_set__for_each_entry(set, section, item) {
114 char *value = item->value;
117 printf("%s.%s=%s\n", section->name,
124 static int parse_config_arg(char *arg, char **var, char **value)
126 const char *last_dot = strchr(arg, '.');
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.
132 if (last_dot == NULL || last_dot == arg) {
133 pr_err("The config variable does not contain a section name: %s\n", arg);
137 pr_err("The config variable does not contain a variable name: %s\n", arg);
141 *value = strchr(arg, '=');
144 else if (!strcmp(*value, "=")) {
145 pr_err("The config variable does not contain a value: %s\n", arg);
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);
159 int cmd_config(int argc, const char **argv)
162 struct perf_config_set *set;
163 char *user_config = mkpath("%s/.perfconfig", getenv("HOME"));
164 const char *config_filename;
166 argc = parse_options(argc, argv, config_options, config_usage,
167 PARSE_OPT_STOP_AT_NON_OPTION);
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);
176 if (use_system_config)
177 config_exclusive_filename = perf_etc_perfconfig();
178 else if (use_user_config)
179 config_exclusive_filename = user_config;
181 if (!config_exclusive_filename)
182 config_filename = user_config;
184 config_filename = config_exclusive_filename;
187 * At only 'config' sub-command, individually use the config set
188 * because of reinitializing with options config file location.
190 set = perf_config_set__new();
197 pr_err("Error: takes no arguments\n");
198 parse_options_usage(config_usage, config_options, "l", 1);
200 if (show_config(set) < 0) {
201 pr_err("Nothing configured, "
202 "please check your %s \n", config_filename);
209 usage_with_options(config_usage, config_options);
213 for (i = 0; argv[i]; i++) {
215 char *arg = strdup(argv[i]);
218 pr_err("%s: strdup failed\n", __func__);
222 if (parse_config_arg(arg, &var, &value) < 0) {
228 if (show_spec_config(set, var) < 0) {
229 pr_err("%s is not configured: %s\n",
230 var, config_filename);
235 if (set_config(set, config_filename, var, value) < 0) {
236 pr_err("Failed to set '%s=%s' on %s\n",
237 var, value, config_filename);
248 perf_config_set__delete(set);