THIS IS A TEST INSTANCE ONLY! REPOSITORIES CAN BE DELETED AT ANY TIME!

Git Source Code Mirror - This is a publish-only repository and all pull requests are ignored. Please follow Documentation/SubmittingPatches procedure for any of your improvements.
git
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

109 lines
2.1KB

  1. #include "cache.h"
  2. #include "alias.h"
  3. #include "config.h"
  4. #include "string-list.h"
  5. struct config_alias_data {
  6. const char *alias;
  7. char *v;
  8. struct string_list *list;
  9. };
  10. static int config_alias_cb(const char *key, const char *value, void *d)
  11. {
  12. struct config_alias_data *data = d;
  13. const char *p;
  14. if (!skip_prefix(key, "alias.", &p))
  15. return 0;
  16. if (data->alias) {
  17. if (!strcasecmp(p, data->alias))
  18. return git_config_string((const char **)&data->v,
  19. key, value);
  20. } else if (data->list) {
  21. string_list_append(data->list, p);
  22. }
  23. return 0;
  24. }
  25. char *alias_lookup(const char *alias)
  26. {
  27. struct config_alias_data data = { alias, NULL };
  28. read_early_config(config_alias_cb, &data);
  29. return data.v;
  30. }
  31. void list_aliases(struct string_list *list)
  32. {
  33. struct config_alias_data data = { NULL, NULL, list };
  34. read_early_config(config_alias_cb, &data);
  35. }
  36. #define SPLIT_CMDLINE_BAD_ENDING 1
  37. #define SPLIT_CMDLINE_UNCLOSED_QUOTE 2
  38. static const char *split_cmdline_errors[] = {
  39. N_("cmdline ends with \\"),
  40. N_("unclosed quote")
  41. };
  42. int split_cmdline(char *cmdline, const char ***argv)
  43. {
  44. int src, dst, count = 0, size = 16;
  45. char quoted = 0;
  46. ALLOC_ARRAY(*argv, size);
  47. /* split alias_string */
  48. (*argv)[count++] = cmdline;
  49. for (src = dst = 0; cmdline[src];) {
  50. char c = cmdline[src];
  51. if (!quoted && isspace(c)) {
  52. cmdline[dst++] = 0;
  53. while (cmdline[++src]
  54. && isspace(cmdline[src]))
  55. ; /* skip */
  56. ALLOC_GROW(*argv, count + 1, size);
  57. (*argv)[count++] = cmdline + dst;
  58. } else if (!quoted && (c == '\'' || c == '"')) {
  59. quoted = c;
  60. src++;
  61. } else if (c == quoted) {
  62. quoted = 0;
  63. src++;
  64. } else {
  65. if (c == '\\' && quoted != '\'') {
  66. src++;
  67. c = cmdline[src];
  68. if (!c) {
  69. FREE_AND_NULL(*argv);
  70. return -SPLIT_CMDLINE_BAD_ENDING;
  71. }
  72. }
  73. cmdline[dst++] = c;
  74. src++;
  75. }
  76. }
  77. cmdline[dst] = 0;
  78. if (quoted) {
  79. FREE_AND_NULL(*argv);
  80. return -SPLIT_CMDLINE_UNCLOSED_QUOTE;
  81. }
  82. ALLOC_GROW(*argv, count + 1, size);
  83. (*argv)[count] = NULL;
  84. return count;
  85. }
  86. const char *split_cmdline_strerror(int split_cmdline_errno)
  87. {
  88. return split_cmdline_errors[-split_cmdline_errno - 1];
  89. }