Loading...
1// SPDX-License-Identifier: GPL-2.0
2/*
3 * builtin-config.c
4 *
5 * Copyright (C) 2015, Taeung Song <treeze.taeung@gmail.com>
6 *
7 */
8#include "builtin.h"
9
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>
15#include <stdio.h>
16#include <stdlib.h>
17
18static bool use_system_config, use_user_config;
19
20static const char * const config_usage[] = {
21 "perf config [<file-option>] [options] [section.name[=value] ...]",
22 NULL
23};
24
25enum actions {
26 ACTION_LIST = 1
27} actions;
28
29static 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"),
34 OPT_END()
35};
36
37static int set_config(struct perf_config_set *set, const char *file_name)
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
51 fprintf(fp, "%s\n", first_line);
52
53 /* overwrite configvariables */
54 perf_config_items__for_each_entry(&set->sections, section) {
55 if (!use_system_config && section->from_system_config)
56 continue;
57 fprintf(fp, "[%s]\n", section->name);
58
59 perf_config_items__for_each_entry(§ion->items, item) {
60 if (!use_system_config && item->from_system_config)
61 continue;
62 if (item->value)
63 fprintf(fp, "\t%s = %s\n",
64 item->name, item->value);
65 }
66 }
67 fclose(fp);
68
69 return 0;
70}
71
72static int show_spec_config(struct perf_config_set *set, const char *var)
73{
74 struct perf_config_section *section;
75 struct perf_config_item *item;
76
77 if (set == NULL)
78 return -1;
79
80 perf_config_items__for_each_entry(&set->sections, section) {
81 if (!strstarts(var, section->name))
82 continue;
83
84 perf_config_items__for_each_entry(§ion->items, item) {
85 const char *name = var + strlen(section->name) + 1;
86
87 if (strcmp(name, item->name) == 0) {
88 char *value = item->value;
89
90 if (value) {
91 printf("%s=%s\n", var, value);
92 return 0;
93 }
94 }
95
96 }
97 }
98
99 return 0;
100}
101
102static int show_config(struct perf_config_set *set)
103{
104 struct perf_config_section *section;
105 struct perf_config_item *item;
106
107 if (set == NULL)
108 return -1;
109
110 perf_config_set__for_each_entry(set, section, item) {
111 char *value = item->value;
112
113 if (value)
114 printf("%s.%s=%s\n", section->name,
115 item->name, value);
116 }
117
118 return 0;
119}
120
121static int parse_config_arg(char *arg, char **var, char **value)
122{
123 const char *last_dot = strchr(arg, '.');
124
125 /*
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.
128 */
129 if (last_dot == NULL || last_dot == arg) {
130 pr_err("The config variable does not contain a section name: %s\n", arg);
131 return -1;
132 }
133 if (!last_dot[1]) {
134 pr_err("The config variable does not contain a variable name: %s\n", arg);
135 return -1;
136 }
137
138 *value = strchr(arg, '=');
139 if (*value == NULL)
140 *var = arg;
141 else if (!strcmp(*value, "=")) {
142 pr_err("The config variable does not contain a value: %s\n", arg);
143 return -1;
144 } else {
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);
149 return -1;
150 }
151 }
152
153 return 0;
154}
155
156int cmd_config(int argc, const char **argv)
157{
158 int i, ret = -1;
159 struct perf_config_set *set;
160 char *user_config = mkpath("%s/.perfconfig", getenv("HOME"));
161 const char *config_filename;
162 bool changed = false;
163
164 argc = parse_options(argc, argv, config_options, config_usage,
165 PARSE_OPT_STOP_AT_NON_OPTION);
166
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);
171 return -1;
172 }
173
174 if (use_system_config)
175 config_exclusive_filename = perf_etc_perfconfig();
176 else if (use_user_config)
177 config_exclusive_filename = user_config;
178
179 if (!config_exclusive_filename)
180 config_filename = user_config;
181 else
182 config_filename = config_exclusive_filename;
183
184 /*
185 * At only 'config' sub-command, individually use the config set
186 * because of reinitializing with options config file location.
187 */
188 set = perf_config_set__new();
189 if (!set)
190 goto out_err;
191
192 switch (actions) {
193 case ACTION_LIST:
194 if (argc) {
195 pr_err("Error: takes no arguments\n");
196 parse_options_usage(config_usage, config_options, "l", 1);
197 } else {
198do_action_list:
199 if (show_config(set) < 0) {
200 pr_err("Nothing configured, "
201 "please check your %s \n", config_filename);
202 goto out_err;
203 }
204 }
205 break;
206 default:
207 if (!argc)
208 goto do_action_list;
209
210 for (i = 0; argv[i]; i++) {
211 char *var, *value;
212 char *arg = strdup(argv[i]);
213
214 if (!arg) {
215 pr_err("%s: strdup failed\n", __func__);
216 goto out_err;
217 }
218
219 if (parse_config_arg(arg, &var, &value) < 0) {
220 free(arg);
221 goto out_err;
222 }
223
224 if (value == NULL) {
225 if (show_spec_config(set, var) < 0) {
226 pr_err("%s is not configured: %s\n",
227 var, config_filename);
228 free(arg);
229 goto out_err;
230 }
231 } else {
232 if (perf_config_set__collect(set, config_filename,
233 var, value) < 0) {
234 pr_err("Failed to add '%s=%s'\n",
235 var, value);
236 free(arg);
237 goto out_err;
238 }
239 changed = true;
240 }
241 free(arg);
242 }
243
244 if (!changed)
245 break;
246
247 if (set_config(set, config_filename) < 0) {
248 pr_err("Failed to set the configs on %s\n",
249 config_filename);
250 goto out_err;
251 }
252 }
253
254 ret = 0;
255out_err:
256 perf_config_set__delete(set);
257 return ret;
258}
1/*
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"
12#include <subcmd/parse-options.h>
13#include "util/util.h"
14#include "util/debug.h"
15#include "util/config.h"
16
17static bool use_system_config, use_user_config;
18
19static const char * const config_usage[] = {
20 "perf config [<file-option>] [options] [section.name[=value] ...]",
21 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),
31 OPT_BOOLEAN(0, "system", &use_system_config, "use system config file"),
32 OPT_BOOLEAN(0, "user", &use_user_config, "use user config file"),
33 OPT_END()
34};
35
36static 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
51 perf_config_set__collect(set, file_name, var, value);
52 fprintf(fp, "%s\n", first_line);
53
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);
59
60 perf_config_items__for_each_entry(§ion->items, item) {
61 if (!use_system_config && section->from_system_config)
62 continue;
63 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
73static 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(§ion->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
103static int show_config(struct perf_config_set *set)
104{
105 struct perf_config_section *section;
106 struct perf_config_item *item;
107
108 if (set == NULL)
109 return -1;
110
111 perf_config_set__for_each_entry(set, section, item) {
112 char *value = item->value;
113
114 if (value)
115 printf("%s.%s=%s\n", section->name,
116 item->name, value);
117 }
118
119 return 0;
120}
121
122static int parse_config_arg(char *arg, char **var, char **value)
123{
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
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;
151 }
152 }
153
154 return 0;
155}
156
157int cmd_config(int argc, const char **argv, const char *prefix __maybe_unused)
158{
159 int i, ret = 0;
160 struct perf_config_set *set;
161 char *user_config = mkpath("%s/.perfconfig", getenv("HOME"));
162
163 argc = parse_options(argc, argv, config_options, config_usage,
164 PARSE_OPT_STOP_AT_NON_OPTION);
165
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);
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
178 /*
179 * At only 'config' sub-command, individually use the config set
180 * because of reinitializing with options config file location.
181 */
182 set = perf_config_set__new();
183 if (!set) {
184 ret = -1;
185 goto out_err;
186 }
187
188 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 {
194 ret = show_config(set);
195 if (ret < 0) {
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);
201 }
202 }
203 break;
204 default:
205 if (argc) {
206 for (i = 0; argv[i]; i++) {
207 char *var, *value;
208 char *arg = strdup(argv[i]);
209
210 if (!arg) {
211 pr_err("%s: strdup failed\n", __func__);
212 ret = -1;
213 break;
214 }
215
216 if (parse_config_arg(arg, &var, &value) < 0) {
217 free(arg);
218 ret = -1;
219 break;
220 }
221
222 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 }
231 free(arg);
232 }
233 } else
234 usage_with_options(config_usage, config_options);
235 }
236
237 perf_config_set__delete(set);
238out_err:
239 return ret;
240}