argv.c 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200
  1. /*
  2. * argv.c
  3. */
  4. #include "private.h"
  5. #include "lub/string.h"
  6. #include <stdlib.h>
  7. #include <string.h>
  8. #include <assert.h>
  9. #include <ctype.h>
  10. #include <stdio.h>
  11. /*--------------------------------------------------------- */
  12. static void lub_argv_init(lub_argv_t * this, const char *line, size_t off)
  13. {
  14. size_t len = 0;
  15. const char *word = NULL;
  16. lub_arg_t *arg = NULL;
  17. bool_t quoted = BOOL_FALSE;
  18. bool_t alt_quoted = BOOL_FALSE;
  19. const char *str = line + off; // Start on specified offset
  20. const char *offset = NULL;
  21. this->argv = NULL;
  22. this->argc = 0;
  23. if (!line)
  24. return;
  25. /* first of all count the words in the line */
  26. this->argc = lub_string_wordcount(line);
  27. if (0 == this->argc)
  28. return;
  29. /* allocate space to hold the vector */
  30. arg = this->argv = malloc(sizeof(lub_arg_t) * this->argc);
  31. assert(arg);
  32. /* then fill out the array with the words */
  33. for (word = lub_string_nextword(str, &len, &offset, &quoted, NULL, &alt_quoted);
  34. word && (*word != '\0');
  35. word = lub_string_nextword(str, &len, &offset, &quoted, NULL, &alt_quoted)) {
  36. if (alt_quoted)
  37. (*arg).arg = lub_string_dupn(word, len);
  38. else
  39. (*arg).arg = lub_string_ndecode(word, len);
  40. (*arg).offset = offset - line;
  41. (*arg).quoted = quoted;
  42. str = offset;
  43. arg++;
  44. }
  45. }
  46. /*--------------------------------------------------------- */
  47. lub_argv_t *lub_argv_new(const char *line, size_t offset)
  48. {
  49. lub_argv_t *this;
  50. this = malloc(sizeof(lub_argv_t));
  51. if (this)
  52. lub_argv_init(this, line, offset);
  53. return this;
  54. }
  55. /*--------------------------------------------------------- */
  56. void lub_argv_add(lub_argv_t * this, const char *text)
  57. {
  58. lub_arg_t * arg;
  59. if (!text)
  60. return;
  61. /* allocate space to hold the vector */
  62. arg = realloc(this->argv, sizeof(lub_arg_t) * (this->argc + 1));
  63. assert(arg);
  64. this->argv = arg;
  65. (this->argv[this->argc++]).arg = strdup(text);
  66. }
  67. /*--------------------------------------------------------- */
  68. static void lub_argv_fini(lub_argv_t * this)
  69. {
  70. unsigned i;
  71. for (i = 0; i < this->argc; i++)
  72. free(this->argv[i].arg);
  73. free(this->argv);
  74. this->argv = NULL;
  75. }
  76. /*--------------------------------------------------------- */
  77. void lub_argv_delete(lub_argv_t * this)
  78. {
  79. lub_argv_fini(this);
  80. free(this);
  81. }
  82. /*--------------------------------------------------------- */
  83. char *lub_argv__get_line(const lub_argv_t * this)
  84. {
  85. int space = 0;
  86. const char *p;
  87. unsigned i;
  88. char *line = NULL;
  89. for (i = 0; i < this->argc; i++) {
  90. if (i != 0)
  91. lub_string_cat(&line, " ");
  92. space = 0;
  93. /* Search for spaces */
  94. for (p = this->argv[i].arg; *p; p++) {
  95. if (isspace(*p)) {
  96. space = 1;
  97. break;
  98. }
  99. }
  100. if (space)
  101. lub_string_cat(&line, "\"");
  102. lub_string_cat(&line, this->argv[i].arg);
  103. if (space)
  104. lub_string_cat(&line, "\"");
  105. }
  106. return line;
  107. }
  108. /*--------------------------------------------------------- */
  109. char **lub_argv__get_argv(const lub_argv_t * this, const char *argv0)
  110. {
  111. char **result = NULL;
  112. unsigned i;
  113. unsigned a = 0;
  114. if (argv0)
  115. a = 1;
  116. result = malloc(sizeof(char *) * (this->argc + 1 + a));
  117. if (argv0)
  118. result[0] = strdup(argv0);
  119. for (i = 0; i < this->argc; i++)
  120. result[i + a] = strdup(this->argv[i].arg);
  121. result[i + a] = NULL;
  122. return result;
  123. }
  124. /*--------------------------------------------------------- */
  125. void lub_argv__free_argv(char **argv)
  126. {
  127. unsigned i;
  128. if (!argv)
  129. return;
  130. for (i = 0; argv[i]; i++)
  131. free(argv[i]);
  132. free(argv);
  133. }
  134. /*--------------------------------------------------------- */
  135. const char *lub_argv__get_arg(const lub_argv_t *this, unsigned int index)
  136. {
  137. const char *result = NULL;
  138. if (!this)
  139. return NULL;
  140. if (this->argc > index)
  141. result = this->argv[index].arg;
  142. return result;
  143. }
  144. /*--------------------------------------------------------- */
  145. unsigned lub_argv__get_count(const lub_argv_t * this)
  146. {
  147. return this->argc;
  148. }
  149. /*--------------------------------------------------------- */
  150. size_t lub_argv__get_offset(const lub_argv_t * this, unsigned index)
  151. {
  152. size_t result = 0;
  153. if (this->argc > index)
  154. result = this->argv[index].offset;
  155. return result;
  156. }
  157. /*--------------------------------------------------------- */
  158. bool_t lub_argv__get_quoted(const lub_argv_t * this, unsigned index)
  159. {
  160. bool_t result = BOOL_FALSE;
  161. if (this->argc > index)
  162. result = this->argv[index].quoted;
  163. return result;
  164. }
  165. /*--------------------------------------------------------- */