blob: 8c0d93b7c2f0316dc95d18d1b36bbb38b3fac191 [file] [log] [blame]
Taeung Song30862f22015-11-17 22:53:21 +09001/*
2 * builtin-config.c
3 *
4 * Copyright (C) 2015, Taeung Song <treeze.taeung@gmail.com>
5 *
6 */
7#include "builtin.h"
8
9#include "perf.h"
10
11#include "util/cache.h"
Josh Poimboeuf4b6ab942015-12-15 09:39:39 -060012#include <subcmd/parse-options.h>
Taeung Song30862f22015-11-17 22:53:21 +090013#include "util/util.h"
14#include "util/debug.h"
Taeung Song860b8d42016-04-14 16:53:19 +090015#include "util/config.h"
Taeung Song30862f22015-11-17 22:53:21 +090016
Taeung Songc7ac2412016-02-11 02:51:17 +090017static bool use_system_config, use_user_config;
18
Taeung Song30862f22015-11-17 22:53:21 +090019static const char * const config_usage[] = {
Taeung Songc6fc0182016-11-04 15:44:20 +090020 "perf config [<file-option>] [options] [section.name[=value] ...]",
Taeung Song30862f22015-11-17 22:53:21 +090021 NULL
22};
23
24enum actions {
25 ACTION_LIST = 1
26} actions;
27
28static struct option config_options[] = {
29 OPT_SET_UINT('l', "list", &actions,
30 "show current config variables", ACTION_LIST),
Taeung Songc7ac2412016-02-11 02:51:17 +090031 OPT_BOOLEAN(0, "system", &use_system_config, "use system config file"),
32 OPT_BOOLEAN(0, "user", &use_user_config, "use user config file"),
Taeung Song30862f22015-11-17 22:53:21 +090033 OPT_END()
34};
35
Taeung Songc6fc0182016-11-04 15:44:20 +090036static int set_config(struct perf_config_set *set, const char *file_name,
37 const char *var, const char *value)
38{
39 struct perf_config_section *section = NULL;
40 struct perf_config_item *item = NULL;
41 const char *first_line = "# this file is auto-generated.";
42 FILE *fp;
43
44 if (set == NULL)
45 return -1;
46
47 fp = fopen(file_name, "w");
48 if (!fp)
49 return -1;
50
Taeung Song08d090c2016-11-04 15:44:22 +090051 perf_config_set__collect(set, file_name, var, value);
Taeung Songc6fc0182016-11-04 15:44:20 +090052 fprintf(fp, "%s\n", first_line);
53
54 /* overwrite configvariables */
55 perf_config_items__for_each_entry(&set->sections, section) {
Taeung Song08d090c2016-11-04 15:44:22 +090056 if (!use_system_config && section->from_system_config)
57 continue;
Taeung Songc6fc0182016-11-04 15:44:20 +090058 fprintf(fp, "[%s]\n", section->name);
59
60 perf_config_items__for_each_entry(&section->items, item) {
Taeung Song08d090c2016-11-04 15:44:22 +090061 if (!use_system_config && section->from_system_config)
62 continue;
Taeung Songc6fc0182016-11-04 15:44:20 +090063 if (item->value)
64 fprintf(fp, "\t%s = %s\n",
65 item->name, item->value);
66 }
67 }
68 fclose(fp);
69
70 return 0;
71}
72
Taeung Song90923602016-11-04 15:44:17 +090073static int show_spec_config(struct perf_config_set *set, const char *var)
74{
75 struct perf_config_section *section;
76 struct perf_config_item *item;
77
78 if (set == NULL)
79 return -1;
80
81 perf_config_items__for_each_entry(&set->sections, section) {
82 if (prefixcmp(var, section->name) != 0)
83 continue;
84
85 perf_config_items__for_each_entry(&section->items, item) {
86 const char *name = var + strlen(section->name) + 1;
87
88 if (strcmp(name, item->name) == 0) {
89 char *value = item->value;
90
91 if (value) {
92 printf("%s=%s\n", var, value);
93 return 0;
94 }
95 }
96
97 }
98 }
99
100 return 0;
101}
102
Taeung Song860b8d42016-04-14 16:53:19 +0900103static int show_config(struct perf_config_set *set)
Taeung Song30862f22015-11-17 22:53:21 +0900104{
Taeung Song860b8d42016-04-14 16:53:19 +0900105 struct perf_config_section *section;
106 struct perf_config_item *item;
Taeung Song860b8d42016-04-14 16:53:19 +0900107
108 if (set == NULL)
109 return -1;
110
Taeung Song4a35b342016-06-23 23:14:32 +0900111 perf_config_set__for_each_entry(set, section, item) {
112 char *value = item->value;
Taeung Song860b8d42016-04-14 16:53:19 +0900113
Taeung Song4a35b342016-06-23 23:14:32 +0900114 if (value)
115 printf("%s.%s=%s\n", section->name,
116 item->name, value);
Taeung Song860b8d42016-04-14 16:53:19 +0900117 }
Taeung Song30862f22015-11-17 22:53:21 +0900118
119 return 0;
120}
121
Taeung Songc6fc0182016-11-04 15:44:20 +0900122static int parse_config_arg(char *arg, char **var, char **value)
Taeung Song36662792016-11-04 15:44:19 +0900123{
124 const char *last_dot = strchr(arg, '.');
125
126 /*
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.
129 */
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;
133 }
134 if (!last_dot[1]) {
135 pr_err("The config variable does not contain a variable name: %s\n", arg);
136 return -1;
137 }
138
Taeung Songc6fc0182016-11-04 15:44:20 +0900139 *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;
151 }
152 }
153
Taeung Song36662792016-11-04 15:44:19 +0900154 return 0;
155}
156
Taeung Song30862f22015-11-17 22:53:21 +0900157int cmd_config(int argc, const char **argv, const char *prefix __maybe_unused)
158{
Taeung Song90923602016-11-04 15:44:17 +0900159 int i, ret = 0;
Taeung Song860b8d42016-04-14 16:53:19 +0900160 struct perf_config_set *set;
Taeung Songc7ac2412016-02-11 02:51:17 +0900161 char *user_config = mkpath("%s/.perfconfig", getenv("HOME"));
Taeung Song30862f22015-11-17 22:53:21 +0900162
163 argc = parse_options(argc, argv, config_options, config_usage,
164 PARSE_OPT_STOP_AT_NON_OPTION);
165
Taeung Songc7ac2412016-02-11 02:51:17 +0900166 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);
170 return -1;
171 }
172
173 if (use_system_config)
174 config_exclusive_filename = perf_etc_perfconfig();
175 else if (use_user_config)
176 config_exclusive_filename = user_config;
177
Taeung Song8a0a9c72016-06-23 23:14:31 +0900178 /*
179 * At only 'config' sub-command, individually use the config set
180 * because of reinitializing with options config file location.
181 */
Taeung Song860b8d42016-04-14 16:53:19 +0900182 set = perf_config_set__new();
183 if (!set) {
184 ret = -1;
185 goto out_err;
186 }
187
Taeung Song30862f22015-11-17 22:53:21 +0900188 switch (actions) {
189 case ACTION_LIST:
190 if (argc) {
191 pr_err("Error: takes no arguments\n");
192 parse_options_usage(config_usage, config_options, "l", 1);
193 } else {
Taeung Song860b8d42016-04-14 16:53:19 +0900194 ret = show_config(set);
Taeung Songc7ac2412016-02-11 02:51:17 +0900195 if (ret < 0) {
196 const char * config_filename = config_exclusive_filename;
197 if (!config_exclusive_filename)
198 config_filename = user_config;
Taeung Song30862f22015-11-17 22:53:21 +0900199 pr_err("Nothing configured, "
Taeung Songc7ac2412016-02-11 02:51:17 +0900200 "please check your %s \n", config_filename);
201 }
Taeung Song30862f22015-11-17 22:53:21 +0900202 }
203 break;
204 default:
Taeung Song36662792016-11-04 15:44:19 +0900205 if (argc) {
206 for (i = 0; argv[i]; i++) {
Taeung Songc6fc0182016-11-04 15:44:20 +0900207 char *var, *value;
208 char *arg = strdup(argv[i]);
Taeung Song36662792016-11-04 15:44:19 +0900209
210 if (!arg) {
211 pr_err("%s: strdup failed\n", __func__);
212 ret = -1;
213 break;
214 }
215
Taeung Songc6fc0182016-11-04 15:44:20 +0900216 if (parse_config_arg(arg, &var, &value) < 0) {
Taeung Song36662792016-11-04 15:44:19 +0900217 free(arg);
218 ret = -1;
219 break;
220 }
221
Taeung Songc6fc0182016-11-04 15:44:20 +0900222 if (value == NULL)
223 ret = show_spec_config(set, var);
224 else {
225 const char *config_filename = config_exclusive_filename;
226
227 if (!config_exclusive_filename)
228 config_filename = user_config;
229 ret = set_config(set, config_filename, var, value);
230 }
Taeung Song36662792016-11-04 15:44:19 +0900231 free(arg);
232 }
233 } else
Taeung Song90923602016-11-04 15:44:17 +0900234 usage_with_options(config_usage, config_options);
Taeung Song30862f22015-11-17 22:53:21 +0900235 }
236
Taeung Song860b8d42016-04-14 16:53:19 +0900237 perf_config_set__delete(set);
238out_err:
Taeung Song30862f22015-11-17 22:53:21 +0900239 return ret;
240}