builtin-config.c 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261
  1. // SPDX-License-Identifier: GPL-2.0
  2. /*
  3. * builtin-config.c
  4. *
  5. * Copyright (C) 2015, Taeung Song <treeze.taeung@gmail.com>
  6. *
  7. */
  8. #include "builtin.h"
  9. #include "perf.h"
  10. #include "util/cache.h"
  11. #include <subcmd/parse-options.h>
  12. #include "util/util.h"
  13. #include "util/debug.h"
  14. #include "util/config.h"
  15. #include <linux/string.h>
  16. static bool use_system_config, use_user_config;
  17. static const char * const config_usage[] = {
  18. "perf config [<file-option>] [options] [section.name[=value] ...]",
  19. NULL
  20. };
  21. enum actions {
  22. ACTION_LIST = 1
  23. } actions;
  24. static struct option config_options[] = {
  25. OPT_SET_UINT('l', "list", &actions,
  26. "show current config variables", ACTION_LIST),
  27. OPT_BOOLEAN(0, "system", &use_system_config, "use system config file"),
  28. OPT_BOOLEAN(0, "user", &use_user_config, "use user config file"),
  29. OPT_END()
  30. };
  31. static int set_config(struct perf_config_set *set, const char *file_name)
  32. {
  33. struct perf_config_section *section = NULL;
  34. struct perf_config_item *item = NULL;
  35. const char *first_line = "# this file is auto-generated.";
  36. FILE *fp;
  37. if (set == NULL)
  38. return -1;
  39. fp = fopen(file_name, "w");
  40. if (!fp)
  41. return -1;
  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 && item->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 (!strstarts(var, section->name))
  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)
  127. {
  128. int i, ret = -1;
  129. struct perf_config_set *set;
  130. char *user_config = mkpath("%s/.perfconfig", getenv("HOME"));
  131. const char *config_filename;
  132. bool changed = false;
  133. argc = parse_options(argc, argv, config_options, config_usage,
  134. PARSE_OPT_STOP_AT_NON_OPTION);
  135. if (use_system_config && use_user_config) {
  136. pr_err("Error: only one config file at a time\n");
  137. parse_options_usage(config_usage, config_options, "user", 0);
  138. parse_options_usage(NULL, config_options, "system", 0);
  139. return -1;
  140. }
  141. if (use_system_config)
  142. config_exclusive_filename = perf_etc_perfconfig();
  143. else if (use_user_config)
  144. config_exclusive_filename = user_config;
  145. if (!config_exclusive_filename)
  146. config_filename = user_config;
  147. else
  148. config_filename = config_exclusive_filename;
  149. /*
  150. * At only 'config' sub-command, individually use the config set
  151. * because of reinitializing with options config file location.
  152. */
  153. set = perf_config_set__new();
  154. if (!set)
  155. goto out_err;
  156. switch (actions) {
  157. case ACTION_LIST:
  158. if (argc) {
  159. pr_err("Error: takes no arguments\n");
  160. parse_options_usage(config_usage, config_options, "l", 1);
  161. } else {
  162. if (show_config(set) < 0) {
  163. pr_err("Nothing configured, "
  164. "please check your %s \n", config_filename);
  165. goto out_err;
  166. }
  167. }
  168. break;
  169. default:
  170. if (!argc) {
  171. usage_with_options(config_usage, config_options);
  172. break;
  173. }
  174. for (i = 0; argv[i]; i++) {
  175. char *var, *value;
  176. char *arg = strdup(argv[i]);
  177. if (!arg) {
  178. pr_err("%s: strdup failed\n", __func__);
  179. goto out_err;
  180. }
  181. if (parse_config_arg(arg, &var, &value) < 0) {
  182. free(arg);
  183. goto out_err;
  184. }
  185. if (value == NULL) {
  186. if (show_spec_config(set, var) < 0) {
  187. pr_err("%s is not configured: %s\n",
  188. var, config_filename);
  189. free(arg);
  190. goto out_err;
  191. }
  192. } else {
  193. if (perf_config_set__collect(set, config_filename,
  194. var, value) < 0) {
  195. pr_err("Failed to add '%s=%s'\n",
  196. var, value);
  197. free(arg);
  198. goto out_err;
  199. }
  200. changed = true;
  201. }
  202. free(arg);
  203. }
  204. if (!changed)
  205. break;
  206. if (set_config(set, config_filename) < 0) {
  207. pr_err("Failed to set the configs on %s\n",
  208. config_filename);
  209. goto out_err;
  210. }
  211. }
  212. ret = 0;
  213. out_err:
  214. perf_config_set__delete(set);
  215. return ret;
  216. }