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 | c7ac241 | 2016-02-11 02:51:17 +0900 | [diff] [blame] | 20 | "perf config [<file-option>] [options]", |
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 | 860b8d4 | 2016-04-14 16:53:19 +0900 | [diff] [blame^] | 36 | static int show_config(struct perf_config_set *set) |
Taeung Song | 30862f2 | 2015-11-17 22:53:21 +0900 | [diff] [blame] | 37 | { |
Taeung Song | 860b8d4 | 2016-04-14 16:53:19 +0900 | [diff] [blame^] | 38 | struct perf_config_section *section; |
| 39 | struct perf_config_item *item; |
| 40 | struct list_head *sections; |
| 41 | |
| 42 | if (set == NULL) |
| 43 | return -1; |
| 44 | |
| 45 | sections = &set->sections; |
| 46 | if (list_empty(sections)) |
| 47 | return -1; |
| 48 | |
| 49 | list_for_each_entry(section, sections, node) { |
| 50 | list_for_each_entry(item, §ion->items, node) { |
| 51 | char *value = item->value; |
| 52 | |
| 53 | if (value) |
| 54 | printf("%s.%s=%s\n", section->name, |
| 55 | item->name, value); |
| 56 | } |
| 57 | } |
Taeung Song | 30862f2 | 2015-11-17 22:53:21 +0900 | [diff] [blame] | 58 | |
| 59 | return 0; |
| 60 | } |
| 61 | |
| 62 | int cmd_config(int argc, const char **argv, const char *prefix __maybe_unused) |
| 63 | { |
| 64 | int ret = 0; |
Taeung Song | 860b8d4 | 2016-04-14 16:53:19 +0900 | [diff] [blame^] | 65 | struct perf_config_set *set; |
Taeung Song | c7ac241 | 2016-02-11 02:51:17 +0900 | [diff] [blame] | 66 | char *user_config = mkpath("%s/.perfconfig", getenv("HOME")); |
Taeung Song | 30862f2 | 2015-11-17 22:53:21 +0900 | [diff] [blame] | 67 | |
| 68 | argc = parse_options(argc, argv, config_options, config_usage, |
| 69 | PARSE_OPT_STOP_AT_NON_OPTION); |
| 70 | |
Taeung Song | c7ac241 | 2016-02-11 02:51:17 +0900 | [diff] [blame] | 71 | if (use_system_config && use_user_config) { |
| 72 | pr_err("Error: only one config file at a time\n"); |
| 73 | parse_options_usage(config_usage, config_options, "user", 0); |
| 74 | parse_options_usage(NULL, config_options, "system", 0); |
| 75 | return -1; |
| 76 | } |
| 77 | |
| 78 | if (use_system_config) |
| 79 | config_exclusive_filename = perf_etc_perfconfig(); |
| 80 | else if (use_user_config) |
| 81 | config_exclusive_filename = user_config; |
| 82 | |
Taeung Song | 860b8d4 | 2016-04-14 16:53:19 +0900 | [diff] [blame^] | 83 | set = perf_config_set__new(); |
| 84 | if (!set) { |
| 85 | ret = -1; |
| 86 | goto out_err; |
| 87 | } |
| 88 | |
Taeung Song | 30862f2 | 2015-11-17 22:53:21 +0900 | [diff] [blame] | 89 | switch (actions) { |
| 90 | case ACTION_LIST: |
| 91 | if (argc) { |
| 92 | pr_err("Error: takes no arguments\n"); |
| 93 | parse_options_usage(config_usage, config_options, "l", 1); |
| 94 | } else { |
Taeung Song | 860b8d4 | 2016-04-14 16:53:19 +0900 | [diff] [blame^] | 95 | ret = show_config(set); |
Taeung Song | c7ac241 | 2016-02-11 02:51:17 +0900 | [diff] [blame] | 96 | if (ret < 0) { |
| 97 | const char * config_filename = config_exclusive_filename; |
| 98 | if (!config_exclusive_filename) |
| 99 | config_filename = user_config; |
Taeung Song | 30862f2 | 2015-11-17 22:53:21 +0900 | [diff] [blame] | 100 | pr_err("Nothing configured, " |
Taeung Song | c7ac241 | 2016-02-11 02:51:17 +0900 | [diff] [blame] | 101 | "please check your %s \n", config_filename); |
| 102 | } |
Taeung Song | 30862f2 | 2015-11-17 22:53:21 +0900 | [diff] [blame] | 103 | } |
| 104 | break; |
| 105 | default: |
| 106 | usage_with_options(config_usage, config_options); |
| 107 | } |
| 108 | |
Taeung Song | 860b8d4 | 2016-04-14 16:53:19 +0900 | [diff] [blame^] | 109 | perf_config_set__delete(set); |
| 110 | out_err: |
Taeung Song | 30862f2 | 2015-11-17 22:53:21 +0900 | [diff] [blame] | 111 | return ret; |
| 112 | } |