builtin-config.c 5.6 KB

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