1
0

argv.c 4.2 KB

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