Taeung Song | 30862f2 | 2015-11-17 22:53:21 +0900 | [diff] [blame] | 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" |
Josh Poimboeuf | 4b6ab94 | 2015-12-15 09:39:39 -0600 | [diff] [blame] | 12 | #include <subcmd/parse-options.h> |
Taeung Song | 30862f2 | 2015-11-17 22:53:21 +0900 | [diff] [blame] | 13 | #include "util/util.h" |
| 14 | #include "util/debug.h" |
Taeung Song | 860b8d4 | 2016-04-14 16:53:19 +0900 | [diff] [blame] | 15 | #include "util/config.h" |
Taeung Song | 30862f2 | 2015-11-17 22:53:21 +0900 | [diff] [blame] | 16 | |
Taeung Song | c7ac241 | 2016-02-11 02:51:17 +0900 | [diff] [blame] | 17 | static bool use_system_config, use_user_config; |
| 18 | |
Taeung Song | 30862f2 | 2015-11-17 22:53:21 +0900 | [diff] [blame] | 19 | static const char * const config_usage[] = { |
Taeung Song | 9092360 | 2016-11-04 15:44:17 +0900 | [diff] [blame] | 20 | "perf config [<file-option>] [options] [section.name ...]", |
Taeung Song | 30862f2 | 2015-11-17 22:53:21 +0900 | [diff] [blame] | 21 | NULL |
| 22 | }; |
| 23 | |
| 24 | enum actions { |
| 25 | ACTION_LIST = 1 |
| 26 | } actions; |
| 27 | |
| 28 | static struct option config_options[] = { |
| 29 | OPT_SET_UINT('l', "list", &actions, |
| 30 | "show current config variables", ACTION_LIST), |
Taeung Song | c7ac241 | 2016-02-11 02:51:17 +0900 | [diff] [blame] | 31 | OPT_BOOLEAN(0, "system", &use_system_config, "use system config file"), |
| 32 | OPT_BOOLEAN(0, "user", &use_user_config, "use user config file"), |
Taeung Song | 30862f2 | 2015-11-17 22:53:21 +0900 | [diff] [blame] | 33 | OPT_END() |
| 34 | }; |
| 35 | |
Taeung Song | 9092360 | 2016-11-04 15:44:17 +0900 | [diff] [blame] | 36 | static int show_spec_config(struct perf_config_set *set, const char *var) |
| 37 | { |
| 38 | struct perf_config_section *section; |
| 39 | struct perf_config_item *item; |
| 40 | |
| 41 | if (set == NULL) |
| 42 | return -1; |
| 43 | |
| 44 | perf_config_items__for_each_entry(&set->sections, section) { |
| 45 | if (prefixcmp(var, section->name) != 0) |
| 46 | continue; |
| 47 | |
| 48 | perf_config_items__for_each_entry(§ion->items, item) { |
| 49 | const char *name = var + strlen(section->name) + 1; |
| 50 | |
| 51 | if (strcmp(name, item->name) == 0) { |
| 52 | char *value = item->value; |
| 53 | |
| 54 | if (value) { |
| 55 | printf("%s=%s\n", var, value); |
| 56 | return 0; |
| 57 | } |
| 58 | } |
| 59 | |
| 60 | } |
| 61 | } |
| 62 | |
| 63 | return 0; |
| 64 | } |
| 65 | |
Taeung Song | 860b8d4 | 2016-04-14 16:53:19 +0900 | [diff] [blame] | 66 | static int show_config(struct perf_config_set *set) |
Taeung Song | 30862f2 | 2015-11-17 22:53:21 +0900 | [diff] [blame] | 67 | { |
Taeung Song | 860b8d4 | 2016-04-14 16:53:19 +0900 | [diff] [blame] | 68 | struct perf_config_section *section; |
| 69 | struct perf_config_item *item; |
Taeung Song | 860b8d4 | 2016-04-14 16:53:19 +0900 | [diff] [blame] | 70 | |
| 71 | if (set == NULL) |
| 72 | return -1; |
| 73 | |
Taeung Song | 4a35b34 | 2016-06-23 23:14:32 +0900 | [diff] [blame] | 74 | perf_config_set__for_each_entry(set, section, item) { |
| 75 | char *value = item->value; |
Taeung Song | 860b8d4 | 2016-04-14 16:53:19 +0900 | [diff] [blame] | 76 | |
Taeung Song | 4a35b34 | 2016-06-23 23:14:32 +0900 | [diff] [blame] | 77 | if (value) |
| 78 | printf("%s.%s=%s\n", section->name, |
| 79 | item->name, value); |
Taeung Song | 860b8d4 | 2016-04-14 16:53:19 +0900 | [diff] [blame] | 80 | } |
Taeung Song | 30862f2 | 2015-11-17 22:53:21 +0900 | [diff] [blame] | 81 | |
| 82 | return 0; |
| 83 | } |
| 84 | |
Taeung Song | 3666279 | 2016-11-04 15:44:19 +0900 | [diff] [blame^] | 85 | static int parse_config_arg(char *arg, char **var) |
| 86 | { |
| 87 | const char *last_dot = strchr(arg, '.'); |
| 88 | |
| 89 | /* |
| 90 | * Since "var" actually contains the section name and the real |
| 91 | * config variable name separated by a dot, we have to know where the dot is. |
| 92 | */ |
| 93 | if (last_dot == NULL || last_dot == arg) { |
| 94 | pr_err("The config variable does not contain a section name: %s\n", arg); |
| 95 | return -1; |
| 96 | } |
| 97 | if (!last_dot[1]) { |
| 98 | pr_err("The config variable does not contain a variable name: %s\n", arg); |
| 99 | return -1; |
| 100 | } |
| 101 | |
| 102 | *var = arg; |
| 103 | return 0; |
| 104 | } |
| 105 | |
Taeung Song | 30862f2 | 2015-11-17 22:53:21 +0900 | [diff] [blame] | 106 | int cmd_config(int argc, const char **argv, const char *prefix __maybe_unused) |
| 107 | { |
Taeung Song | 9092360 | 2016-11-04 15:44:17 +0900 | [diff] [blame] | 108 | int i, ret = 0; |
Taeung Song | 860b8d4 | 2016-04-14 16:53:19 +0900 | [diff] [blame] | 109 | struct perf_config_set *set; |
Taeung Song | c7ac241 | 2016-02-11 02:51:17 +0900 | [diff] [blame] | 110 | char *user_config = mkpath("%s/.perfconfig", getenv("HOME")); |
Taeung Song | 30862f2 | 2015-11-17 22:53:21 +0900 | [diff] [blame] | 111 | |
| 112 | argc = parse_options(argc, argv, config_options, config_usage, |
| 113 | PARSE_OPT_STOP_AT_NON_OPTION); |
| 114 | |
Taeung Song | c7ac241 | 2016-02-11 02:51:17 +0900 | [diff] [blame] | 115 | if (use_system_config && use_user_config) { |
| 116 | pr_err("Error: only one config file at a time\n"); |
| 117 | parse_options_usage(config_usage, config_options, "user", 0); |
| 118 | parse_options_usage(NULL, config_options, "system", 0); |
| 119 | return -1; |
| 120 | } |
| 121 | |
| 122 | if (use_system_config) |
| 123 | config_exclusive_filename = perf_etc_perfconfig(); |
| 124 | else if (use_user_config) |
| 125 | config_exclusive_filename = user_config; |
| 126 | |
Taeung Song | 8a0a9c7 | 2016-06-23 23:14:31 +0900 | [diff] [blame] | 127 | /* |
| 128 | * At only 'config' sub-command, individually use the config set |
| 129 | * because of reinitializing with options config file location. |
| 130 | */ |
Taeung Song | 860b8d4 | 2016-04-14 16:53:19 +0900 | [diff] [blame] | 131 | set = perf_config_set__new(); |
| 132 | if (!set) { |
| 133 | ret = -1; |
| 134 | goto out_err; |
| 135 | } |
| 136 | |
Taeung Song | 30862f2 | 2015-11-17 22:53:21 +0900 | [diff] [blame] | 137 | switch (actions) { |
| 138 | case ACTION_LIST: |
| 139 | if (argc) { |
| 140 | pr_err("Error: takes no arguments\n"); |
| 141 | parse_options_usage(config_usage, config_options, "l", 1); |
| 142 | } else { |
Taeung Song | 860b8d4 | 2016-04-14 16:53:19 +0900 | [diff] [blame] | 143 | ret = show_config(set); |
Taeung Song | c7ac241 | 2016-02-11 02:51:17 +0900 | [diff] [blame] | 144 | if (ret < 0) { |
| 145 | const char * config_filename = config_exclusive_filename; |
| 146 | if (!config_exclusive_filename) |
| 147 | config_filename = user_config; |
Taeung Song | 30862f2 | 2015-11-17 22:53:21 +0900 | [diff] [blame] | 148 | pr_err("Nothing configured, " |
Taeung Song | c7ac241 | 2016-02-11 02:51:17 +0900 | [diff] [blame] | 149 | "please check your %s \n", config_filename); |
| 150 | } |
Taeung Song | 30862f2 | 2015-11-17 22:53:21 +0900 | [diff] [blame] | 151 | } |
| 152 | break; |
| 153 | default: |
Taeung Song | 3666279 | 2016-11-04 15:44:19 +0900 | [diff] [blame^] | 154 | if (argc) { |
| 155 | for (i = 0; argv[i]; i++) { |
| 156 | char *var, *arg = strdup(argv[i]); |
| 157 | |
| 158 | if (!arg) { |
| 159 | pr_err("%s: strdup failed\n", __func__); |
| 160 | ret = -1; |
| 161 | break; |
| 162 | } |
| 163 | |
| 164 | if (parse_config_arg(arg, &var) < 0) { |
| 165 | free(arg); |
| 166 | ret = -1; |
| 167 | break; |
| 168 | } |
| 169 | |
| 170 | ret = show_spec_config(set, var); |
| 171 | free(arg); |
| 172 | } |
| 173 | } else |
Taeung Song | 9092360 | 2016-11-04 15:44:17 +0900 | [diff] [blame] | 174 | usage_with_options(config_usage, config_options); |
Taeung Song | 30862f2 | 2015-11-17 22:53:21 +0900 | [diff] [blame] | 175 | } |
| 176 | |
Taeung Song | 860b8d4 | 2016-04-14 16:53:19 +0900 | [diff] [blame] | 177 | perf_config_set__delete(set); |
| 178 | out_err: |
Taeung Song | 30862f2 | 2015-11-17 22:53:21 +0900 | [diff] [blame] | 179 | return ret; |
| 180 | } |