blob: e4207a23b52c0924e1d5c119f888467f7e9f1150 [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 Songc7ac2412016-02-11 02:51:17 +090020 "perf config [<file-option>] [options]",
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 Song860b8d42016-04-14 16:53:19 +090036static int show_config(struct perf_config_set *set)
Taeung Song30862f22015-11-17 22:53:21 +090037{
Taeung Song860b8d42016-04-14 16:53:19 +090038 struct perf_config_section *section;
39 struct perf_config_item *item;
Taeung Song860b8d42016-04-14 16:53:19 +090040
41 if (set == NULL)
42 return -1;
43
Taeung Song4a35b342016-06-23 23:14:32 +090044 perf_config_set__for_each_entry(set, section, item) {
45 char *value = item->value;
Taeung Song860b8d42016-04-14 16:53:19 +090046
Taeung Song4a35b342016-06-23 23:14:32 +090047 if (value)
48 printf("%s.%s=%s\n", section->name,
49 item->name, value);
Taeung Song860b8d42016-04-14 16:53:19 +090050 }
Taeung Song30862f22015-11-17 22:53:21 +090051
52 return 0;
53}
54
55int cmd_config(int argc, const char **argv, const char *prefix __maybe_unused)
56{
57 int ret = 0;
Taeung Song860b8d42016-04-14 16:53:19 +090058 struct perf_config_set *set;
Taeung Songc7ac2412016-02-11 02:51:17 +090059 char *user_config = mkpath("%s/.perfconfig", getenv("HOME"));
Taeung Song30862f22015-11-17 22:53:21 +090060
61 argc = parse_options(argc, argv, config_options, config_usage,
62 PARSE_OPT_STOP_AT_NON_OPTION);
63
Taeung Songc7ac2412016-02-11 02:51:17 +090064 if (use_system_config && use_user_config) {
65 pr_err("Error: only one config file at a time\n");
66 parse_options_usage(config_usage, config_options, "user", 0);
67 parse_options_usage(NULL, config_options, "system", 0);
68 return -1;
69 }
70
71 if (use_system_config)
72 config_exclusive_filename = perf_etc_perfconfig();
73 else if (use_user_config)
74 config_exclusive_filename = user_config;
75
Taeung Song8a0a9c72016-06-23 23:14:31 +090076 /*
77 * At only 'config' sub-command, individually use the config set
78 * because of reinitializing with options config file location.
79 */
Taeung Song860b8d42016-04-14 16:53:19 +090080 set = perf_config_set__new();
81 if (!set) {
82 ret = -1;
83 goto out_err;
84 }
85
Taeung Song30862f22015-11-17 22:53:21 +090086 switch (actions) {
87 case ACTION_LIST:
88 if (argc) {
89 pr_err("Error: takes no arguments\n");
90 parse_options_usage(config_usage, config_options, "l", 1);
91 } else {
Taeung Song860b8d42016-04-14 16:53:19 +090092 ret = show_config(set);
Taeung Songc7ac2412016-02-11 02:51:17 +090093 if (ret < 0) {
94 const char * config_filename = config_exclusive_filename;
95 if (!config_exclusive_filename)
96 config_filename = user_config;
Taeung Song30862f22015-11-17 22:53:21 +090097 pr_err("Nothing configured, "
Taeung Songc7ac2412016-02-11 02:51:17 +090098 "please check your %s \n", config_filename);
99 }
Taeung Song30862f22015-11-17 22:53:21 +0900100 }
101 break;
102 default:
103 usage_with_options(config_usage, config_options);
104 }
105
Taeung Song860b8d42016-04-14 16:53:19 +0900106 perf_config_set__delete(set);
107out_err:
Taeung Song30862f22015-11-17 22:53:21 +0900108 return ret;
109}