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 const TOKEN tokens[] = {
23 { "=~", T_OP_REG_EQ, }, /* order is important! */
24 { "!~", T_OP_REG_NE, },
33 { "==", T_OP_CMP_EQ, },
45 * This works only as long as special tokens
46 * are max. 2 characters, but it's fast.
48 #define TOKEN_MATCH(bptr, tptr) \
49 ( (tptr)[0] == (bptr)[0] && \
50 ((tptr)[1] == (bptr)[1] || (tptr)[1] == 0))
53 * Read a word from a buffer and advance pointer.
54 * This function knows about escapes and quotes.
56 * At end-of-line, buf[0] is set to '\0'.
57 * Returns 0 or special token value.
59 static int getthing(char **ptr, char *buf, int buflen, int tok)
71 while (*p && isspace(*p))
80 * Might be a 1 or 2 character token.
82 if (tok) for (t = tokens; t->str; t++) {
83 if (TOKEN_MATCH(p, t->str)) {
102 while (*p && buflen-- > 0) {
119 if (*p >= '0' && *p <= '9' &&
120 sscanf(p, "%3o", &x) == 1) {
135 if (quote && *p == '"') {
143 for (t = tokens; t->str; t++)
144 if (TOKEN_MATCH(p, t->str))
154 /* Skip whitespace again. */
155 while (*p && isspace(*p))
159 /* we got SOME form out output string, even if it is empty */
164 * Read a "word" - this means we don't honor
165 * tokens as delimiters.
167 int getword(char **ptr, char *buf, int buflen)
169 return getthing(ptr, buf, buflen, 0) == T_EOL ? 0 : 1;
173 * Read the next word, use tokens as delimiters.
175 int gettoken(char **ptr, char *buf, int buflen)
177 return getthing(ptr, buf, buflen, 1);