tokenize.c 8.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323
  1. /*
  2. * This file defines the string_tokenize interface
  3. * Time-stamp: "2007-11-12 20:40:36 bkorb"
  4. *
  5. * This file is part of AutoOpts, a companion to AutoGen.
  6. * AutoOpts is free software.
  7. * AutoOpts is copyright (c) 1992-2009 by Bruce Korb - all rights reserved
  8. *
  9. * AutoOpts is available under any one of two licenses. The license
  10. * in use must be one of these two and the choice is under the control
  11. * of the user of the license.
  12. *
  13. * The GNU Lesser General Public License, version 3 or later
  14. * See the files "COPYING.lgplv3" and "COPYING.gplv3"
  15. *
  16. * The Modified Berkeley Software Distribution License
  17. * See the file "COPYING.mbsd"
  18. *
  19. * These files have the following md5sums:
  20. *
  21. * 43b91e8ca915626ed3818ffb1b71248b pkg/libopts/COPYING.gplv3
  22. * 06a1a2e4760c90ea5e1dad8dfaac4d39 pkg/libopts/COPYING.lgplv3
  23. * 66a5cedaf62c4b2637025f049f9b826f pkg/libopts/COPYING.mbsd
  24. */
  25. #include <errno.h>
  26. #include <stdlib.h>
  27. #define cc_t const unsigned char
  28. #define ch_t unsigned char
  29. /* = = = START-STATIC-FORWARD = = = */
  30. /* static forward declarations maintained by mk-fwd */
  31. static void
  32. copy_cooked( ch_t** ppDest, char const ** ppSrc );
  33. static void
  34. copy_raw( ch_t** ppDest, char const ** ppSrc );
  35. /* = = = END-STATIC-FORWARD = = = */
  36. static void
  37. copy_cooked( ch_t** ppDest, char const ** ppSrc )
  38. {
  39. ch_t* pDest = (ch_t*)*ppDest;
  40. const ch_t* pSrc = (const ch_t*)(*ppSrc + 1);
  41. for (;;) {
  42. ch_t ch = *(pSrc++);
  43. switch (ch) {
  44. case NUL: *ppSrc = NULL; return;
  45. case '"': goto done;
  46. case '\\':
  47. pSrc += ao_string_cook_escape_char( (char*)pSrc, (char*)&ch, 0x7F );
  48. if (ch == 0x7F)
  49. break;
  50. /* FALLTHROUGH */
  51. default:
  52. *(pDest++) = ch;
  53. }
  54. }
  55. done:
  56. *ppDest = (ch_t*)pDest; /* next spot for storing character */
  57. *ppSrc = (char const *)pSrc; /* char following closing quote */
  58. }
  59. static void
  60. copy_raw( ch_t** ppDest, char const ** ppSrc )
  61. {
  62. ch_t* pDest = *ppDest;
  63. cc_t* pSrc = (cc_t*) (*ppSrc + 1);
  64. for (;;) {
  65. ch_t ch = *(pSrc++);
  66. switch (ch) {
  67. case NUL: *ppSrc = NULL; return;
  68. case '\'': goto done;
  69. case '\\':
  70. /*
  71. * *Four* escapes are handled: newline removal, escape char
  72. * quoting and apostrophe quoting
  73. */
  74. switch (*pSrc) {
  75. case NUL: *ppSrc = NULL; return;
  76. case '\r':
  77. if (*(++pSrc) == '\n')
  78. ++pSrc;
  79. continue;
  80. case '\n':
  81. ++pSrc;
  82. continue;
  83. case '\'':
  84. ch = '\'';
  85. /* FALLTHROUGH */
  86. case '\\':
  87. ++pSrc;
  88. break;
  89. }
  90. /* FALLTHROUGH */
  91. default:
  92. *(pDest++) = ch;
  93. }
  94. }
  95. done:
  96. *ppDest = pDest; /* next spot for storing character */
  97. *ppSrc = (char const *) pSrc; /* char following closing quote */
  98. }
  99. /*=export_func ao_string_tokenize
  100. *
  101. * what: tokenize an input string
  102. *
  103. * arg: + char const* + string + string to be tokenized +
  104. *
  105. * ret_type: token_list_t*
  106. * ret_desc: pointer to a structure that lists each token
  107. *
  108. * doc:
  109. *
  110. * This function will convert one input string into a list of strings.
  111. * The list of strings is derived by separating the input based on
  112. * white space separation. However, if the input contains either single
  113. * or double quote characters, then the text after that character up to
  114. * a matching quote will become the string in the list.
  115. *
  116. * The returned pointer should be deallocated with @code{free(3C)} when
  117. * are done using the data. The data are placed in a single block of
  118. * allocated memory. Do not deallocate individual token/strings.
  119. *
  120. * The structure pointed to will contain at least these two fields:
  121. * @table @samp
  122. * @item tkn_ct
  123. * The number of tokens found in the input string.
  124. * @item tok_list
  125. * An array of @code{tkn_ct + 1} pointers to substring tokens, with
  126. * the last pointer set to NULL.
  127. * @end table
  128. *
  129. * There are two types of quoted strings: single quoted (@code{'}) and
  130. * double quoted (@code{"}). Singly quoted strings are fairly raw in that
  131. * escape characters (@code{\\}) are simply another character, except when
  132. * preceding the following characters:
  133. * @example
  134. * @code{\\} double backslashes reduce to one
  135. * @code{'} incorporates the single quote into the string
  136. * @code{\n} suppresses both the backslash and newline character
  137. * @end example
  138. *
  139. * Double quote strings are formed according to the rules of string
  140. * constants in ANSI-C programs.
  141. *
  142. * example:
  143. * @example
  144. * #include <stdlib.h>
  145. * int ix;
  146. * token_list_t* ptl = ao_string_tokenize( some_string )
  147. * for (ix = 0; ix < ptl->tkn_ct; ix++)
  148. * do_something_with_tkn( ptl->tkn_list[ix] );
  149. * free( ptl );
  150. * @end example
  151. * Note that everything is freed with the one call to @code{free(3C)}.
  152. *
  153. * err:
  154. * NULL is returned and @code{errno} will be set to indicate the problem:
  155. * @itemize @bullet
  156. * @item
  157. * @code{EINVAL} - There was an unterminated quoted string.
  158. * @item
  159. * @code{ENOENT} - The input string was empty.
  160. * @item
  161. * @code{ENOMEM} - There is not enough memory.
  162. * @end itemize
  163. =*/
  164. token_list_t*
  165. ao_string_tokenize( char const* str )
  166. {
  167. int max_token_ct = 1; /* allow for trailing NUL on string */
  168. token_list_t* res;
  169. if (str == NULL) goto bogus_str;
  170. /*
  171. * Trim leading white space. Use "ENOENT" and a NULL return to indicate
  172. * an empty string was passed.
  173. */
  174. while (IS_WHITESPACE_CHAR(*str)) str++;
  175. if (*str == NUL) {
  176. bogus_str:
  177. errno = ENOENT;
  178. return NULL;
  179. }
  180. /*
  181. * Take an approximate count of tokens. If no quoted strings are used,
  182. * it will be accurate. If quoted strings are used, it will be a little
  183. * high and we'll squander the space for a few extra pointers.
  184. */
  185. {
  186. cc_t* pz = (cc_t*)str;
  187. do {
  188. max_token_ct++;
  189. while (! IS_WHITESPACE_CHAR(*++pz))
  190. if (*pz == NUL) goto found_nul;
  191. while (IS_WHITESPACE_CHAR(*pz)) pz++;
  192. } while (*pz != NUL);
  193. found_nul:
  194. ;
  195. }
  196. res = malloc( sizeof(*res) + strlen(str) + (max_token_ct * sizeof(ch_t*)) );
  197. if (res == NULL) {
  198. errno = ENOMEM;
  199. return res;
  200. }
  201. /*
  202. * Now copy each token into the output buffer.
  203. */
  204. {
  205. ch_t* pzDest = (ch_t*)(res->tkn_list + (max_token_ct + 1));
  206. res->tkn_ct = 0;
  207. do {
  208. res->tkn_list[ res->tkn_ct++ ] = pzDest;
  209. for (;;) {
  210. int ch = (ch_t)*str;
  211. if (IS_WHITESPACE_CHAR(ch)) {
  212. found_white_space:
  213. while (IS_WHITESPACE_CHAR(*++str)) ;
  214. break;
  215. }
  216. switch (ch) {
  217. case '"':
  218. copy_cooked( &pzDest, &str );
  219. if (str == NULL) {
  220. free(res);
  221. errno = EINVAL;
  222. return NULL;
  223. }
  224. if (IS_WHITESPACE_CHAR(*str))
  225. goto found_white_space;
  226. break;
  227. case '\'':
  228. copy_raw( &pzDest, &str );
  229. if (str == NULL) {
  230. free(res);
  231. errno = EINVAL;
  232. return NULL;
  233. }
  234. if (IS_WHITESPACE_CHAR(*str))
  235. goto found_white_space;
  236. break;
  237. case NUL:
  238. goto copy_done;
  239. default:
  240. str++;
  241. *(pzDest++) = ch;
  242. }
  243. } copy_done:;
  244. /*
  245. * NUL terminate the last token and see if we have any more tokens.
  246. */
  247. *(pzDest++) = NUL;
  248. } while (*str != NUL);
  249. res->tkn_list[ res->tkn_ct ] = NULL;
  250. }
  251. return res;
  252. }
  253. #ifdef TEST
  254. #include <stdio.h>
  255. #include <string.h>
  256. int
  257. main( int argc, char** argv )
  258. {
  259. if (argc == 1) {
  260. printf("USAGE: %s arg [ ... ]\n", *argv);
  261. return 1;
  262. }
  263. while (--argc > 0) {
  264. char* arg = *(++argv);
  265. token_list_t* p = ao_string_tokenize( arg );
  266. if (p == NULL) {
  267. printf( "Parsing string ``%s'' failed:\n\terrno %d (%s)\n",
  268. arg, errno, strerror( errno ));
  269. } else {
  270. int ix = 0;
  271. printf( "Parsed string ``%s''\ninto %d tokens:\n", arg, p->tkn_ct );
  272. do {
  273. printf( " %3d: ``%s''\n", ix+1, p->tkn_list[ix] );
  274. } while (++ix < p->tkn_ct);
  275. free(p);
  276. }
  277. }
  278. return 0;
  279. }
  280. #endif
  281. /*
  282. * Local Variables:
  283. * mode: C
  284. * c-file-style: "stroustrup"
  285. * indent-tabs-mode: nil
  286. * End:
  287. * end of autoopts/tokenize.c */