2 * This file defines the string_tokenize interface
3 * Time-stamp: "2012-03-04 13:23:50 bkorb"
5 * This file is part of AutoOpts, a companion to AutoGen.
6 * AutoOpts is free software.
7 * AutoOpts is Copyright (c) 1992-2012 by Bruce Korb - all rights reserved
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.
13 * The GNU Lesser General Public License, version 3 or later
14 * See the files "COPYING.lgplv3" and "COPYING.gplv3"
16 * The Modified Berkeley Software Distribution License
17 * See the file "COPYING.mbsd"
19 * These files have the following md5sums:
21 * 43b91e8ca915626ed3818ffb1b71248b pkg/libopts/COPYING.gplv3
22 * 06a1a2e4760c90ea5e1dad8dfaac4d39 pkg/libopts/COPYING.lgplv3
23 * 66a5cedaf62c4b2637025f049f9b826f pkg/libopts/COPYING.mbsd
29 #define cc_t const unsigned char
30 #define ch_t unsigned char
32 /* = = = START-STATIC-FORWARD = = = */
34 copy_cooked(ch_t
** ppDest
, char const ** ppSrc
);
37 copy_raw(ch_t
** ppDest
, char const ** ppSrc
);
40 alloc_token_list(char const * str
);
41 /* = = = END-STATIC-FORWARD = = = */
44 copy_cooked(ch_t
** ppDest
, char const ** ppSrc
)
46 ch_t
* pDest
= (ch_t
*)*ppDest
;
47 const ch_t
* pSrc
= (const ch_t
*)(*ppSrc
+ 1);
52 case NUL
: *ppSrc
= NULL
; return;
55 pSrc
+= ao_string_cook_escape_char((char*)pSrc
, (char*)&ch
, 0x7F);
66 *ppDest
= (ch_t
*)pDest
; /* next spot for storing character */
67 *ppSrc
= (char const *)pSrc
; /* char following closing quote */
72 copy_raw(ch_t
** ppDest
, char const ** ppSrc
)
74 ch_t
* pDest
= *ppDest
;
75 cc_t
* pSrc
= (cc_t
*) (*ppSrc
+ 1);
80 case NUL
: *ppSrc
= NULL
; return;
84 * *Four* escapes are handled: newline removal, escape char
85 * quoting and apostrophe quoting
88 case NUL
: *ppSrc
= NULL
; return;
114 *ppDest
= pDest
; /* next spot for storing character */
115 *ppSrc
= (char const *) pSrc
; /* char following closing quote */
118 static token_list_t
*
119 alloc_token_list(char const * str
)
123 int max_token_ct
= 2; /* allow for trailing NULL pointer & NUL on string */
125 if (str
== NULL
) goto enoent_res
;
128 * Trim leading white space. Use "ENOENT" and a NULL return to indicate
129 * an empty string was passed.
131 str
= SPN_WHITESPACE_CHARS(str
);
132 if (*str
== NUL
) goto enoent_res
;
135 * Take an approximate count of tokens. If no quoted strings are used,
136 * it will be accurate. If quoted strings are used, it will be a little
137 * high and we'll squander the space for a few extra pointers.
140 char const * pz
= str
;
144 pz
= BRK_WHITESPACE_CHARS(pz
+1);
145 pz
= SPN_WHITESPACE_CHARS(pz
);
146 } while (*pz
!= NUL
);
148 res
= malloc(sizeof(*res
) + (pz
- str
)
149 + (max_token_ct
* sizeof(ch_t
*)));
154 else res
->tkn_list
[0] = (ch_t
*)(res
->tkn_list
+ (max_token_ct
- 1));
164 /*=export_func ao_string_tokenize
166 * what: tokenize an input string
168 * arg: + char const* + string + string to be tokenized +
170 * ret_type: token_list_t*
171 * ret_desc: pointer to a structure that lists each token
175 * This function will convert one input string into a list of strings.
176 * The list of strings is derived by separating the input based on
177 * white space separation. However, if the input contains either single
178 * or double quote characters, then the text after that character up to
179 * a matching quote will become the string in the list.
181 * The returned pointer should be deallocated with @code{free(3C)} when
182 * are done using the data. The data are placed in a single block of
183 * allocated memory. Do not deallocate individual token/strings.
185 * The structure pointed to will contain at least these two fields:
188 * The number of tokens found in the input string.
190 * An array of @code{tkn_ct + 1} pointers to substring tokens, with
191 * the last pointer set to NULL.
194 * There are two types of quoted strings: single quoted (@code{'}) and
195 * double quoted (@code{"}). Singly quoted strings are fairly raw in that
196 * escape characters (@code{\\}) are simply another character, except when
197 * preceding the following characters:
199 * @code{\\} double backslashes reduce to one
200 * @code{'} incorporates the single quote into the string
201 * @code{\n} suppresses both the backslash and newline character
204 * Double quote strings are formed according to the rules of string
205 * constants in ANSI-C programs.
209 * #include <stdlib.h>
211 * token_list_t* ptl = ao_string_tokenize(some_string)
212 * for (ix = 0; ix < ptl->tkn_ct; ix++)
213 * do_something_with_tkn(ptl->tkn_list[ix]);
216 * Note that everything is freed with the one call to @code{free(3C)}.
219 * NULL is returned and @code{errno} will be set to indicate the problem:
222 * @code{EINVAL} - There was an unterminated quoted string.
224 * @code{ENOENT} - The input string was empty.
226 * @code{ENOMEM} - There is not enough memory.
230 ao_string_tokenize(char const* str
)
232 token_list_t
* res
= alloc_token_list(str
);
236 * Now copy each token into the output buffer.
241 pzDest
= (ch_t
*)(res
->tkn_list
[0]);
245 res
->tkn_list
[ res
->tkn_ct
++ ] = pzDest
;
248 if (IS_WHITESPACE_CHAR(ch
)) {
250 str
= SPN_WHITESPACE_CHARS(str
+1);
256 copy_cooked(&pzDest
, &str
);
262 if (IS_WHITESPACE_CHAR(*str
))
263 goto found_white_space
;
267 copy_raw(&pzDest
, &str
);
273 if (IS_WHITESPACE_CHAR(*str
))
274 goto found_white_space
;
282 *(pzDest
++) = (unsigned char)ch
;
287 * NUL terminate the last token and see if we have any more tokens.
290 } while (*str
!= NUL
);
292 res
->tkn_list
[ res
->tkn_ct
] = NULL
;
302 main(int argc
, char** argv
)
305 printf("USAGE: %s arg [ ... ]\n", *argv
);
309 char* arg
= *(++argv
);
310 token_list_t
* p
= ao_string_tokenize(arg
);
312 printf("Parsing string ``%s'' failed:\n\terrno %d (%s)\n",
313 arg
, errno
, strerror(errno
));
316 printf("Parsed string ``%s''\ninto %d tokens:\n", arg
, p
->tkn_ct
);
318 printf(" %3d: ``%s''\n", ix
+1, p
->tkn_list
[ix
]);
319 } while (++ix
< p
->tkn_ct
);
330 * c-file-style: "stroustrup"
331 * indent-tabs-mode: nil
333 * end of autoopts/tokenize.c */