builtin-config.c 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240
  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] [section.name[=value] ...]",
  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 set_config(struct perf_config_set *set, const char *file_name,
  30. const char *var, const char *value)
  31. {
  32. struct perf_config_section *section = NULL;
  33. struct perf_config_item *item = NULL;
  34. const char *first_line = "# this file is auto-generated.";
  35. FILE *fp;
  36. if (set == NULL)
  37. return -1;
  38. fp = fopen(file_name, "w");
  39. if (!fp)
  40. return -1;
  41. perf_config_set__collect(set, file_name, var, value);
  42. fprintf(fp, "%s\n", first_line);
  43. /* overwrite configvariables */
  44. perf_config_items__for_each_entry(&set->sections, section) {
  45. if (!use_system_config && section->from_system_config)
  46. continue;
  47. fprintf(fp, "[%s]\n", section->name);
  48. perf_config_items__for_each_entry(&section->items, item) {
  49. if (!use_system_config && section->from_system_config)
  50. continue;
  51. if (item->value)
  52. fprintf(fp, "\t%s = %s\n",
  53. item->name, item->value);
  54. }
  55. }
  56. fclose(fp);
  57. return 0;
  58. }
  59. static int show_spec_config(struct perf_config_set *set, const char *var)
  60. {
  61. struct perf_config_section *section;
  62. struct perf_config_item *item;
  63. if (set == NULL)
  64. return -1;
  65. perf_config_items__for_each_entry(&set->sections, section) {
  66. if (prefixcmp(var, section->name) != 0)
  67. continue;
  68. perf_config_items__for_each_entry(&section->items, item) {
  69. const char *name = var + strlen(section->name) + 1;
  70. if (strcmp(name, item->name) == 0) {
  71. char *value = item->value;
  72. if (value) {
  73. printf("%s=%s\n", var, value);
  74. return 0;
  75. }
  76. }
  77. }
  78. }
  79. return 0;
  80. }
  81. static int show_config(struct perf_config_set *set)
  82. {
  83. struct perf_config_section *section;
  84. struct perf_config_item *item;
  85. if (set == NULL)
  86. return -1;
  87. perf_config_set__for_each_entry(set, section, item) {
  88. char *value = item->value;
  89. if (value)
  90. printf("%s.%s=%s\n", section->name,
  91. item->name, value);
  92. }
  93. return 0;
  94. }
  95. static int parse_config_arg(char *arg, char **var, char **value)
  96. {
  97. const char *last_dot = strchr(arg, '.');
  98. /*
  99. * Since "var" actually contains the section name and the real
  100. * config variable name separated by a dot, we have to know where the dot is.
  101. */
  102. if (last_dot == NULL || last_dot == arg) {
  103. pr_err("The config variable does not contain a section name: %s\n", arg);
  104. return -1;
  105. }
  106. if (!last_dot[1]) {
  107. pr_err("The config variable does not contain a variable name: %s\n", arg);
  108. return -1;
  109. }
  110. *value = strchr(arg, '=');
  111. if (*value == NULL)
  112. *var = arg;
  113. else if (!strcmp(*value, "=")) {
  114. pr_err("The config variable does not contain a value: %s\n", arg);
  115. return -1;
  116. } else {
  117. *value = *value + 1; /* excluding a first character '=' */
  118. *var = strsep(&arg, "=");
  119. if (*var[0] == '\0') {
  120. pr_err("invalid config variable: %s\n", arg);
  121. return -1;
  122. }
  123. }
  124. return 0;
  125. }
  126. int cmd_config(int argc, const char **argv, const char *prefix __maybe_unused)
  127. {
  128. int i, ret = 0;
  129. struct perf_config_set *set;
  130. char *user_config = mkpath("%s/.perfconfig", getenv("HOME"));
  131. argc = parse_options(argc, argv, config_options, config_usage,
  132. PARSE_OPT_STOP_AT_NON_OPTION);
  133. if (use_system_config && use_user_config) {
  134. pr_err("Error: only one config file at a time\n");
  135. parse_options_usage(config_usage, config_options, "user", 0);
  136. parse_options_usage(NULL, config_options, "system", 0);
  137. return -1;
  138. }
  139. if (use_system_config)
  140. config_exclusive_filename = perf_etc_perfconfig();
  141. else if (use_user_config)
  142. config_exclusive_filename = user_config;
  143. /*
  144. * At only 'config' sub-command, individually use the config set
  145. * because of reinitializing with options config file location.
  146. */
  147. set = perf_config_set__new();
  148. if (!set) {
  149. ret = -1;
  150. goto out_err;
  151. }
  152. switch (actions) {
  153. case ACTION_LIST:
  154. if (argc) {
  155. pr_err("Error: takes no arguments\n");
  156. parse_options_usage(config_usage, config_options, "l", 1);
  157. } else {
  158. ret = show_config(set);
  159. if (ret < 0) {
  160. const char * config_filename = config_exclusive_filename;
  161. if (!config_exclusive_filename)
  162. config_filename = user_config;
  163. pr_err("Nothing configured, "
  164. "please check your %s \n", config_filename);
  165. }
  166. }
  167. break;
  168. default:
  169. if (argc) {
  170. for (i = 0; argv[i]; i++) {
  171. char *var, *value;
  172. char *arg = strdup(argv[i]);
  173. if (!arg) {
  174. pr_err("%s: strdup failed\n", __func__);
  175. ret = -1;
  176. break;
  177. }
  178. if (parse_config_arg(arg, &var, &value) < 0) {
  179. free(arg);
  180. ret = -1;
  181. break;
  182. }
  183. if (value == NULL)
  184. ret = show_spec_config(set, var);
  185. else {
  186. const char *config_filename = config_exclusive_filename;
  187. if (!config_exclusive_filename)
  188. config_filename = user_config;
  189. ret = set_config(set, config_filename, var, value);
  190. }
  191. free(arg);
  192. }
  193. } else
  194. usage_with_options(config_usage, config_options);
  195. }
  196. perf_config_set__delete(set);
  197. out_err:
  198. return ret;
  199. }