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.

110 lines
2.2KB

  1. #include "cache.h"
  2. #include "argv-array.h"
  3. #include "strbuf.h"
  4. const char *empty_argv[] = { NULL };
  5. void argv_array_init(struct argv_array *array)
  6. {
  7. array->argv = empty_argv;
  8. array->argc = 0;
  9. array->alloc = 0;
  10. }
  11. static void argv_array_push_nodup(struct argv_array *array, const char *value)
  12. {
  13. if (array->argv == empty_argv)
  14. array->argv = NULL;
  15. ALLOC_GROW(array->argv, array->argc + 2, array->alloc);
  16. array->argv[array->argc++] = value;
  17. array->argv[array->argc] = NULL;
  18. }
  19. const char *argv_array_push(struct argv_array *array, const char *value)
  20. {
  21. argv_array_push_nodup(array, xstrdup(value));
  22. return array->argv[array->argc - 1];
  23. }
  24. const char *argv_array_pushf(struct argv_array *array, const char *fmt, ...)
  25. {
  26. va_list ap;
  27. struct strbuf v = STRBUF_INIT;
  28. va_start(ap, fmt);
  29. strbuf_vaddf(&v, fmt, ap);
  30. va_end(ap);
  31. argv_array_push_nodup(array, strbuf_detach(&v, NULL));
  32. return array->argv[array->argc - 1];
  33. }
  34. void argv_array_pushl(struct argv_array *array, ...)
  35. {
  36. va_list ap;
  37. const char *arg;
  38. va_start(ap, array);
  39. while((arg = va_arg(ap, const char *)))
  40. argv_array_push(array, arg);
  41. va_end(ap);
  42. }
  43. void argv_array_pushv(struct argv_array *array, const char **argv)
  44. {
  45. for (; *argv; argv++)
  46. argv_array_push(array, *argv);
  47. }
  48. void argv_array_pop(struct argv_array *array)
  49. {
  50. if (!array->argc)
  51. return;
  52. free((char *)array->argv[array->argc - 1]);
  53. array->argv[array->argc - 1] = NULL;
  54. array->argc--;
  55. }
  56. void argv_array_split(struct argv_array *array, const char *to_split)
  57. {
  58. while (isspace(*to_split))
  59. to_split++;
  60. for (;;) {
  61. const char *p = to_split;
  62. if (!*p)
  63. break;
  64. while (*p && !isspace(*p))
  65. p++;
  66. argv_array_push_nodup(array, xstrndup(to_split, p - to_split));
  67. while (isspace(*p))
  68. p++;
  69. to_split = p;
  70. }
  71. }
  72. void argv_array_clear(struct argv_array *array)
  73. {
  74. if (array->argv != empty_argv) {
  75. int i;
  76. for (i = 0; i < array->argc; i++)
  77. free((char *)array->argv[i]);
  78. free(array->argv);
  79. }
  80. argv_array_init(array);
  81. }
  82. const char **argv_array_detach(struct argv_array *array)
  83. {
  84. if (array->argv == empty_argv)
  85. return xcalloc(1, sizeof(const char *));
  86. else {
  87. const char **ret = array->argv;
  88. argv_array_init(array);
  89. return ret;
  90. }
  91. }