4 * Copyright (C) 2015, Taeung Song <treeze.taeung@gmail.com>
11 #include "util/cache.h"
12 #include <subcmd/parse-options.h>
13 #include "util/util.h"
14 #include "util/debug.h"
15 #include "util/config.h"
17 static bool use_system_config
, use_user_config
;
19 static const char * const config_usage
[] = {
20 "perf config [<file-option>] [options] [section.name[=value] ...]",
28 static struct option config_options
[] = {
29 OPT_SET_UINT('l', "list", &actions
,
30 "show current config variables", ACTION_LIST
),
31 OPT_BOOLEAN(0, "system", &use_system_config
, "use system config file"),
32 OPT_BOOLEAN(0, "user", &use_user_config
, "use user config file"),
36 static int set_config(struct perf_config_set
*set
, const char *file_name
,
37 const char *var
, const char *value
)
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 perf_config_set__collect(set
, file_name
, var
, value
);
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
)
58 fprintf(fp
, "[%s]\n", section
->name
);
60 perf_config_items__for_each_entry(§ion
->items
, item
) {
61 if (!use_system_config
&& section
->from_system_config
)
64 fprintf(fp
, "\t%s = %s\n",
65 item
->name
, item
->value
);
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
;
81 perf_config_items__for_each_entry(&set
->sections
, section
) {
82 if (prefixcmp(var
, section
->name
) != 0)
85 perf_config_items__for_each_entry(§ion
->items
, item
) {
86 const char *name
= var
+ strlen(section
->name
) + 1;
88 if (strcmp(name
, item
->name
) == 0) {
89 char *value
= item
->value
;
92 printf("%s=%s\n", var
, value
);
103 static int show_config(struct perf_config_set
*set
)
105 struct perf_config_section
*section
;
106 struct perf_config_item
*item
;
111 perf_config_set__for_each_entry(set
, section
, item
) {
112 char *value
= item
->value
;
115 printf("%s.%s=%s\n", section
->name
,
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
);
135 pr_err("The config variable does not contain a variable name: %s\n", arg
);
139 *value
= strchr(arg
, '=');
142 else if (!strcmp(*value
, "=")) {
143 pr_err("The config variable does not contain a value: %s\n", arg
);
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
);
157 int cmd_config(int argc
, const char **argv
, const char *prefix __maybe_unused
)
160 struct perf_config_set
*set
;
161 char *user_config
= mkpath("%s/.perfconfig", getenv("HOME"));
163 argc
= parse_options(argc
, argv
, config_options
, config_usage
,
164 PARSE_OPT_STOP_AT_NON_OPTION
);
166 if (use_system_config
&& use_user_config
) {
167 pr_err("Error: only one config file at a time\n");
168 parse_options_usage(config_usage
, config_options
, "user", 0);
169 parse_options_usage(NULL
, config_options
, "system", 0);
173 if (use_system_config
)
174 config_exclusive_filename
= perf_etc_perfconfig();
175 else if (use_user_config
)
176 config_exclusive_filename
= user_config
;
179 * At only 'config' sub-command, individually use the config set
180 * because of reinitializing with options config file location.
182 set
= perf_config_set__new();
191 pr_err("Error: takes no arguments\n");
192 parse_options_usage(config_usage
, config_options
, "l", 1);
194 ret
= show_config(set
);
196 const char * config_filename
= config_exclusive_filename
;
197 if (!config_exclusive_filename
)
198 config_filename
= user_config
;
199 pr_err("Nothing configured, "
200 "please check your %s \n", config_filename
);
206 for (i
= 0; argv
[i
]; i
++) {
208 char *arg
= strdup(argv
[i
]);
211 pr_err("%s: strdup failed\n", __func__
);
216 if (parse_config_arg(arg
, &var
, &value
) < 0) {
223 ret
= show_spec_config(set
, var
);
225 const char *config_filename
= config_exclusive_filename
;
227 if (!config_exclusive_filename
)
228 config_filename
= user_config
;
229 ret
= set_config(set
, config_filename
, var
, value
);
234 usage_with_options(config_usage
, config_options
);
237 perf_config_set__delete(set
);