2 * token.c Read the next token from a string.
3 * Yes it's pretty primitive but effective.
15 static const char rcsid[] = "$Id$";
22 static TOKEN tokens[] = {
23 { "=~", T_OP_REG_EQ, }, /* order is important! */
24 { "!~", T_OP_REG_NE, },
43 * This works only as long as special tokens
44 * are max. 2 characters, but it's fast.
46 #define TOKEN_MATCH(bptr, tptr) \
47 ( (tptr)[0] == (bptr)[0] && \
48 ((tptr)[1] == (bptr)[1] || (tptr)[1] == 0))
51 * Read a word from a buffer and advance pointer.
52 * This function knows about escapes and quotes.
54 * At end-of-line, buf[0] is set to '\0'.
55 * Returns 0 or special token value.
57 static int getthing(char **ptr, char *buf, int buflen, int tok)
69 while (*p && isspace(*p))
78 * Might be a 1 or 2 character token.
80 if (tok) for (t = tokens; t->str; t++) {
81 if (TOKEN_MATCH(p, t->str)) {
100 while (*p && buflen-- > 0) {
114 if (*p >= '0' && *p <= '9' &&
115 sscanf(p, "%3o", &x) == 1) {
130 if (quote && *p == '"') {
138 for (t = tokens; t->str; t++)
139 if (TOKEN_MATCH(p, t->str))
149 /* Skip whitespace again. */
150 while (*p && isspace(*p))
154 return (buf[0]) ? 0 : T_EOL;
158 * Read a "word" - this means we don't honor
159 * tokens as delimiters.
161 int getword(char **ptr, char *buf, int buflen)
163 return getthing(ptr, buf, buflen, 0) == T_EOL ? 0 : 1;
167 * Read the next word, use tokens as delimiters.
169 int gettoken(char **ptr, char *buf, int buflen)
171 return getthing(ptr, buf, buflen, 1);