2 * token.c Read the next token from a string.
3 * Yes it's pretty primitive but effective.
5 * Version: @(#)token.c 1.00 19-Jul-1999 miquels@cistron.nl
20 static TOKEN tokens[] = {
39 * This works only as long as special tokens
40 * are max. 2 characters, but it's fast.
42 #define TOKEN_MATCH(bptr, tptr) \
43 ( (tptr)[0] == (bptr)[0] && \
44 ((tptr)[1] == (bptr)[1] || (tptr)[1] == 0))
47 * Read a word from a buffer and advance pointer.
48 * This function knows about escapes and quotes.
50 * At end-of-line, buf[0] is set to '\0'.
51 * Returns 0 or special token value.
53 static int getthing(char **ptr, char *buf, int buflen, int tok)
65 while (*p && isspace(*p))
74 * Might be a 1 or 2 character token.
76 if (tok) for (t = tokens; t->str; t++) {
77 if (TOKEN_MATCH(p, t->str)) {
96 while (*p && buflen-- > 0) {
110 if (*p >= '0' && *p <= '9' &&
111 sscanf(p, "%3o", &x) == 1) {
126 if (quote && *p == '"') {
134 for (t = tokens; t->str; t++)
135 if (TOKEN_MATCH(p, t->str))
145 /* Skip whitespace again. */
146 while (*p && isspace(*p))
150 return (buf[0]) ? 0 : T_EOL;
154 * Read a "word" - this means we don't honor
155 * tokens as delimiters.
157 int getword(char **ptr, char *buf, int buflen)
159 return getthing(ptr, buf, buflen, 0) == T_EOL ? 0 : 1;
163 * Read the next word, use tokens as delimiters.
165 int gettoken(char **ptr, char *buf, int buflen)
167 return getthing(ptr, buf, buflen, 1);