1 // SPDX-License-Identifier: GPL-2.0
5 * Copyright (C) 2015, Taeung Song <treeze.taeung@gmail.com>
10 #include "util/cache.h"
11 #include <subcmd/parse-options.h>
12 #include "util/debug.h"
13 #include "util/config.h"
14 #include <linux/string.h>
18 static bool use_system_config, use_user_config;
20 static const char * const config_usage[] = {
21 "perf config [<file-option>] [options] [section.name[=value] ...]",
29 static struct option config_options[] = {
30 OPT_SET_UINT('l', "list", &actions,
31 "show current config variables", ACTION_LIST),
32 OPT_BOOLEAN(0, "system", &use_system_config, "use system config file"),
33 OPT_BOOLEAN(0, "user", &use_user_config, "use user config file"),
37 static int set_config(struct perf_config_set *set, const char *file_name)
39 struct perf_config_section *section = NULL;
40 struct perf_config_item *item = NULL;
41 const char *first_line = "# this file is auto-generated.";
47 fp = fopen(file_name, "w");
51 fprintf(fp, "%s\n", first_line);
53 /* overwrite configvariables */
54 perf_config_items__for_each_entry(&set->sections, section) {
55 if (!use_system_config && section->from_system_config)
57 fprintf(fp, "[%s]\n", section->name);
59 perf_config_items__for_each_entry(§ion->items, item) {
60 if (!use_system_config && item->from_system_config)
63 fprintf(fp, "\t%s = %s\n",
64 item->name, item->value);
72 static int show_spec_config(struct perf_config_set *set, const char *var)
74 struct perf_config_section *section;
75 struct perf_config_item *item;
80 perf_config_items__for_each_entry(&set->sections, section) {
81 if (!strstarts(var, section->name))
84 perf_config_items__for_each_entry(§ion->items, item) {
85 const char *name = var + strlen(section->name) + 1;
87 if (strcmp(name, item->name) == 0) {
88 char *value = item->value;
91 printf("%s=%s\n", var, value);
102 static int show_config(struct perf_config_set *set)
104 struct perf_config_section *section;
105 struct perf_config_item *item;
110 perf_config_set__for_each_entry(set, section, item) {
111 char *value = item->value;
114 printf("%s.%s=%s\n", section->name,
121 static int parse_config_arg(char *arg, char **var, char **value)
123 const char *last_dot = strchr(arg, '.');
126 * Since "var" actually contains the section name and the real
127 * config variable name separated by a dot, we have to know where the dot is.
129 if (last_dot == NULL || last_dot == arg) {
130 pr_err("The config variable does not contain a section name: %s\n", arg);
134 pr_err("The config variable does not contain a variable name: %s\n", arg);
138 *value = strchr(arg, '=');
141 else if (!strcmp(*value, "=")) {
142 pr_err("The config variable does not contain a value: %s\n", arg);
145 *value = *value + 1; /* excluding a first character '=' */
146 *var = strsep(&arg, "=");
147 if (*var[0] == '\0') {
148 pr_err("invalid config variable: %s\n", arg);
156 int cmd_config(int argc, const char **argv)
159 struct perf_config_set *set;
160 char *user_config = mkpath("%s/.perfconfig", getenv("HOME"));
161 const char *config_filename;
162 bool changed = false;
164 argc = parse_options(argc, argv, config_options, config_usage,
165 PARSE_OPT_STOP_AT_NON_OPTION);
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);
174 if (use_system_config)
175 config_exclusive_filename = perf_etc_perfconfig();
176 else if (use_user_config)
177 config_exclusive_filename = user_config;
179 if (!config_exclusive_filename)
180 config_filename = user_config;
182 config_filename = config_exclusive_filename;
185 * At only 'config' sub-command, individually use the config set
186 * because of reinitializing with options config file location.
188 set = perf_config_set__new();
195 pr_err("Error: takes no arguments\n");
196 parse_options_usage(config_usage, config_options, "l", 1);
199 if (show_config(set) < 0) {
200 pr_err("Nothing configured, "
201 "please check your %s \n", config_filename);
210 for (i = 0; argv[i]; i++) {
212 char *arg = strdup(argv[i]);
215 pr_err("%s: strdup failed\n", __func__);
219 if (parse_config_arg(arg, &var, &value) < 0) {
225 if (show_spec_config(set, var) < 0) {
226 pr_err("%s is not configured: %s\n",
227 var, config_filename);
232 if (perf_config_set__collect(set, config_filename,
234 pr_err("Failed to add '%s=%s'\n",
247 if (set_config(set, config_filename) < 0) {
248 pr_err("Failed to set the configs on %s\n",
256 perf_config_set__delete(set);