argv_parse.c 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172
  1. /*
  2. * argv_parse.c --- utility function for parsing a string into a
  3. * argc, argv array.
  4. *
  5. * This file defines a function argv_parse() which parsing a
  6. * passed-in string, handling double quotes and backslashes, and
  7. * creates an allocated argv vector which can be freed using the
  8. * argv_free() function.
  9. *
  10. * See argv_parse.h for the formal definition of the functions.
  11. *
  12. * Copyright 1999 by Theodore Ts'o.
  13. *
  14. * Permission to use, copy, modify, and distribute this software for
  15. * any purpose with or without fee is hereby granted, provided that
  16. * the above copyright notice and this permission notice appear in all
  17. * copies. THE SOFTWARE IS PROVIDED "AS IS" AND THEODORE TS'O (THE
  18. * AUTHOR) DISCLAIMS ALL WARRANTIES WITH REGARD TO THIS SOFTWARE,
  19. * INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS.
  20. * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY SPECIAL, DIRECT,
  21. * INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER
  22. * RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION
  23. * OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR
  24. * IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. (Isn't
  25. * it sick that the U.S. culture of lawsuit-happy lawyers requires
  26. * this kind of disclaimer?)
  27. *
  28. * Version 1.1, modified 2/27/1999
  29. */
  30. #include <stdlib.h>
  31. #include <ctype.h>
  32. #include <string.h>
  33. #include "argv_parse.h"
  34. #define STATE_WHITESPACE 1
  35. #define STATE_TOKEN 2
  36. #define STATE_QUOTED 3
  37. /*
  38. * Returns 0 on success, -1 on failure.
  39. */
  40. int argv_parse(const char *in_buf, int *ret_argc, char ***ret_argv)
  41. {
  42. int argc = 0, max_argc = 0;
  43. char **argv, **new_argv, *buf, ch;
  44. const char *cp = NULL;
  45. char *outcp = NULL;
  46. int state = STATE_WHITESPACE;
  47. buf = malloc(strlen(in_buf)+1);
  48. if (!buf)
  49. return -1;
  50. argv = NULL;
  51. outcp = buf;
  52. for (cp = in_buf; (ch = *cp); cp++) {
  53. if (state == STATE_WHITESPACE) {
  54. if (isspace((int) ch))
  55. continue;
  56. /* Not whitespace, so start a new token */
  57. state = STATE_TOKEN;
  58. if (argc >= max_argc) {
  59. max_argc += 3;
  60. new_argv = realloc(argv,
  61. (max_argc+1)*sizeof(char *));
  62. if (!new_argv) {
  63. if (argv) free(argv);
  64. free(buf);
  65. return -1;
  66. }
  67. argv = new_argv;
  68. }
  69. argv[argc++] = outcp;
  70. }
  71. if (state == STATE_QUOTED) {
  72. if (ch == '"')
  73. state = STATE_TOKEN;
  74. else
  75. *outcp++ = ch;
  76. continue;
  77. }
  78. /* Must be processing characters in a word */
  79. if (isspace((int) ch)) {
  80. /*
  81. * Terminate the current word and start
  82. * looking for the beginning of the next word.
  83. */
  84. *outcp++ = 0;
  85. state = STATE_WHITESPACE;
  86. continue;
  87. }
  88. if (ch == '"') {
  89. state = STATE_QUOTED;
  90. continue;
  91. }
  92. if (ch == '\\') {
  93. ch = *++cp;
  94. switch (ch) {
  95. case '\0':
  96. ch = '\\'; cp--; break;
  97. case 'n':
  98. ch = '\n'; break;
  99. case 't':
  100. ch = '\t'; break;
  101. case 'b':
  102. ch = '\b'; break;
  103. }
  104. }
  105. *outcp++ = ch;
  106. }
  107. if (state != STATE_WHITESPACE)
  108. *outcp++ = '\0';
  109. if (ret_argv) {
  110. if (argv == NULL) {
  111. free(buf);
  112. if ((argv=malloc(sizeof(char *))) == NULL)
  113. return -1;
  114. }
  115. argv[argc] = NULL;
  116. *ret_argv = argv;
  117. } else {
  118. free(buf);
  119. free(argv);
  120. }
  121. if (ret_argc)
  122. *ret_argc = argc;
  123. return 0;
  124. }
  125. void argv_free(char **argv)
  126. {
  127. if (argv) {
  128. if (*argv)
  129. free(*argv);
  130. free(argv);
  131. }
  132. }
  133. #ifdef DEBUG_ARGV_PARSE
  134. /*
  135. * For debugging
  136. */
  137. #include <stdio.h>
  138. int main(int argc, char **argv)
  139. {
  140. int ac, ret;
  141. char **av, **cpp;
  142. char buf[256];
  143. while (!feof(stdin)) {
  144. if (fgets(buf, sizeof(buf), stdin) == NULL)
  145. break;
  146. ret = argv_parse(buf, &ac, &av);
  147. if (ret != 0) {
  148. printf("Argv_parse returned %d!\n", ret);
  149. continue;
  150. }
  151. printf("Argv_parse returned %d arguments...\n", ac);
  152. for (cpp = av; *cpp; cpp++) {
  153. if (cpp != av)
  154. printf(", ");
  155. printf("'%s'", *cpp);
  156. }
  157. printf("\n");
  158. argv_free(av);
  159. }
  160. exit(0);
  161. }
  162. #endif