builtin-config.c 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112
  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. struct list_head *sections;
  34. if (set == NULL)
  35. return -1;
  36. sections = &set->sections;
  37. if (list_empty(sections))
  38. return -1;
  39. list_for_each_entry(section, sections, node) {
  40. list_for_each_entry(item, &section->items, node) {
  41. char *value = item->value;
  42. if (value)
  43. printf("%s.%s=%s\n", section->name,
  44. item->name, value);
  45. }
  46. }
  47. return 0;
  48. }
  49. int cmd_config(int argc, const char **argv, const char *prefix __maybe_unused)
  50. {
  51. int ret = 0;
  52. struct perf_config_set *set;
  53. char *user_config = mkpath("%s/.perfconfig", getenv("HOME"));
  54. argc = parse_options(argc, argv, config_options, config_usage,
  55. PARSE_OPT_STOP_AT_NON_OPTION);
  56. if (use_system_config && use_user_config) {
  57. pr_err("Error: only one config file at a time\n");
  58. parse_options_usage(config_usage, config_options, "user", 0);
  59. parse_options_usage(NULL, config_options, "system", 0);
  60. return -1;
  61. }
  62. if (use_system_config)
  63. config_exclusive_filename = perf_etc_perfconfig();
  64. else if (use_user_config)
  65. config_exclusive_filename = user_config;
  66. set = perf_config_set__new();
  67. if (!set) {
  68. ret = -1;
  69. goto out_err;
  70. }
  71. switch (actions) {
  72. case ACTION_LIST:
  73. if (argc) {
  74. pr_err("Error: takes no arguments\n");
  75. parse_options_usage(config_usage, config_options, "l", 1);
  76. } else {
  77. ret = show_config(set);
  78. if (ret < 0) {
  79. const char * config_filename = config_exclusive_filename;
  80. if (!config_exclusive_filename)
  81. config_filename = user_config;
  82. pr_err("Nothing configured, "
  83. "please check your %s \n", config_filename);
  84. }
  85. }
  86. break;
  87. default:
  88. usage_with_options(config_usage, config_options);
  89. }
  90. perf_config_set__delete(set);
  91. out_err:
  92. return ret;
  93. }