builtin-config.c 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110
  1. /*
  2. * builtin-config.c
  3. *
  4. * Copyright (C) 2015, Taeung Song <treeze.taeung@gmail.com>
  5. *
  6. */
  7. #include "builtin.h"
  8. #include "perf.h"
  9. #include "util/cache.h"
  10. #include <subcmd/parse-options.h>
  11. #include "util/util.h"
  12. #include "util/debug.h"
  13. #include "util/config.h"
  14. static bool use_system_config, use_user_config;
  15. static const char * const config_usage[] = {
  16. "perf config [<file-option>] [options]",
  17. NULL
  18. };
  19. enum actions {
  20. ACTION_LIST = 1
  21. } actions;
  22. static struct option config_options[] = {
  23. OPT_SET_UINT('l', "list", &actions,
  24. "show current config variables", ACTION_LIST),
  25. OPT_BOOLEAN(0, "system", &use_system_config, "use system config file"),
  26. OPT_BOOLEAN(0, "user", &use_user_config, "use user config file"),
  27. OPT_END()
  28. };
  29. static int show_config(struct perf_config_set *set)
  30. {
  31. struct perf_config_section *section;
  32. struct perf_config_item *item;
  33. if (set == NULL)
  34. return -1;
  35. perf_config_set__for_each_entry(set, section, item) {
  36. char *value = item->value;
  37. if (value)
  38. printf("%s.%s=%s\n", section->name,
  39. item->name, value);
  40. }
  41. return 0;
  42. }
  43. int cmd_config(int argc, const char **argv, const char *prefix __maybe_unused)
  44. {
  45. int ret = 0;
  46. struct perf_config_set *set;
  47. char *user_config = mkpath("%s/.perfconfig", getenv("HOME"));
  48. argc = parse_options(argc, argv, config_options, config_usage,
  49. PARSE_OPT_STOP_AT_NON_OPTION);
  50. if (use_system_config && use_user_config) {
  51. pr_err("Error: only one config file at a time\n");
  52. parse_options_usage(config_usage, config_options, "user", 0);
  53. parse_options_usage(NULL, config_options, "system", 0);
  54. return -1;
  55. }
  56. if (use_system_config)
  57. config_exclusive_filename = perf_etc_perfconfig();
  58. else if (use_user_config)
  59. config_exclusive_filename = user_config;
  60. /*
  61. * At only 'config' sub-command, individually use the config set
  62. * because of reinitializing with options config file location.
  63. */
  64. set = perf_config_set__new();
  65. if (!set) {
  66. ret = -1;
  67. goto out_err;
  68. }
  69. switch (actions) {
  70. case ACTION_LIST:
  71. if (argc) {
  72. pr_err("Error: takes no arguments\n");
  73. parse_options_usage(config_usage, config_options, "l", 1);
  74. } else {
  75. ret = show_config(set);
  76. if (ret < 0) {
  77. const char * config_filename = config_exclusive_filename;
  78. if (!config_exclusive_filename)
  79. config_filename = user_config;
  80. pr_err("Nothing configured, "
  81. "please check your %s \n", config_filename);
  82. }
  83. }
  84. break;
  85. default:
  86. usage_with_options(config_usage, config_options);
  87. }
  88. perf_config_set__delete(set);
  89. out_err:
  90. return ret;
  91. }