builtin-config.c 2.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687
  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. static bool use_system_config, use_user_config;
  14. static const char * const config_usage[] = {
  15. "perf config [<file-option>] [options]",
  16. NULL
  17. };
  18. enum actions {
  19. ACTION_LIST = 1
  20. } actions;
  21. static struct option config_options[] = {
  22. OPT_SET_UINT('l', "list", &actions,
  23. "show current config variables", ACTION_LIST),
  24. OPT_BOOLEAN(0, "system", &use_system_config, "use system config file"),
  25. OPT_BOOLEAN(0, "user", &use_user_config, "use user config file"),
  26. OPT_END()
  27. };
  28. static int show_config(const char *key, const char *value,
  29. void *cb __maybe_unused)
  30. {
  31. if (value)
  32. printf("%s=%s\n", key, value);
  33. else
  34. printf("%s\n", key);
  35. return 0;
  36. }
  37. int cmd_config(int argc, const char **argv, const char *prefix __maybe_unused)
  38. {
  39. int ret = 0;
  40. char *user_config = mkpath("%s/.perfconfig", getenv("HOME"));
  41. argc = parse_options(argc, argv, config_options, config_usage,
  42. PARSE_OPT_STOP_AT_NON_OPTION);
  43. if (use_system_config && use_user_config) {
  44. pr_err("Error: only one config file at a time\n");
  45. parse_options_usage(config_usage, config_options, "user", 0);
  46. parse_options_usage(NULL, config_options, "system", 0);
  47. return -1;
  48. }
  49. if (use_system_config)
  50. config_exclusive_filename = perf_etc_perfconfig();
  51. else if (use_user_config)
  52. config_exclusive_filename = user_config;
  53. switch (actions) {
  54. case ACTION_LIST:
  55. if (argc) {
  56. pr_err("Error: takes no arguments\n");
  57. parse_options_usage(config_usage, config_options, "l", 1);
  58. } else {
  59. ret = perf_config(show_config, NULL);
  60. if (ret < 0) {
  61. const char * config_filename = config_exclusive_filename;
  62. if (!config_exclusive_filename)
  63. config_filename = user_config;
  64. pr_err("Nothing configured, "
  65. "please check your %s \n", config_filename);
  66. }
  67. }
  68. break;
  69. default:
  70. usage_with_options(config_usage, config_options);
  71. }
  72. return ret;
  73. }