argv.c 4.1 KB

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