builtin-config.c 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248
  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)
  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. argc = parse_options(argc, argv, config_options, config_usage,
  133. PARSE_OPT_STOP_AT_NON_OPTION);
  134. if (use_system_config && use_user_config) {
  135. pr_err("Error: only one config file at a time\n");
  136. parse_options_usage(config_usage, config_options, "user", 0);
  137. parse_options_usage(NULL, config_options, "system", 0);
  138. return -1;
  139. }
  140. if (use_system_config)
  141. config_exclusive_filename = perf_etc_perfconfig();
  142. else if (use_user_config)
  143. config_exclusive_filename = user_config;
  144. if (!config_exclusive_filename)
  145. config_filename = user_config;
  146. else
  147. config_filename = config_exclusive_filename;
  148. /*
  149. * At only 'config' sub-command, individually use the config set
  150. * because of reinitializing with options config file location.
  151. */
  152. set = perf_config_set__new();
  153. if (!set)
  154. goto out_err;
  155. switch (actions) {
  156. case ACTION_LIST:
  157. if (argc) {
  158. pr_err("Error: takes no arguments\n");
  159. parse_options_usage(config_usage, config_options, "l", 1);
  160. } else {
  161. if (show_config(set) < 0) {
  162. pr_err("Nothing configured, "
  163. "please check your %s \n", config_filename);
  164. goto out_err;
  165. }
  166. }
  167. break;
  168. default:
  169. if (!argc) {
  170. usage_with_options(config_usage, config_options);
  171. break;
  172. }
  173. for (i = 0; argv[i]; i++) {
  174. char *var, *value;
  175. char *arg = strdup(argv[i]);
  176. if (!arg) {
  177. pr_err("%s: strdup failed\n", __func__);
  178. goto out_err;
  179. }
  180. if (parse_config_arg(arg, &var, &value) < 0) {
  181. free(arg);
  182. goto out_err;
  183. }
  184. if (value == NULL) {
  185. if (show_spec_config(set, var) < 0) {
  186. pr_err("%s is not configured: %s\n",
  187. var, config_filename);
  188. free(arg);
  189. goto out_err;
  190. }
  191. } else {
  192. if (set_config(set, config_filename, var, value) < 0) {
  193. pr_err("Failed to set '%s=%s' on %s\n",
  194. var, value, config_filename);
  195. free(arg);
  196. goto out_err;
  197. }
  198. }
  199. free(arg);
  200. }
  201. }
  202. ret = 0;
  203. out_err:
  204. perf_config_set__delete(set);
  205. return ret;
  206. }