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_TRUE, },
34 { "!*", T_OP_CMP_FALSE, },
35 { "==", T_OP_CMP_EQ, },
43 { ";", T_SEMICOLON, },
48 * This works only as long as special tokens
49 * are max. 2 characters, but it's fast.
51 #define TOKEN_MATCH(bptr, tptr) \
52 ( (tptr)[0] == (bptr)[0] && \
53 ((tptr)[1] == (bptr)[1] || (tptr)[1] == 0))
56 * Read a word from a buffer and advance pointer.
57 * This function knows about escapes and quotes.
59 * At end-of-line, buf[0] is set to '\0'.
60 * Returns 0 or special token value.
62 static LRAD_TOKEN getthing(char **ptr, char *buf, int buflen, int tok,
63 const TOKEN *tokenlist)
76 while (*p && isspace((int) *p))
85 * Might be a 1 or 2 character token.
87 if (tok) for (t = tokenlist; t->str; t++) {
88 if (TOKEN_MATCH(p, t->str)) {
91 while (isspace((int) *p))
109 while (*p && buflen-- > 0) {
132 if (*p >= '0' && *p <= '9' &&
133 sscanf(p, "%3o", &x) == 1) {
148 if (quote && (*p == quote)) {
153 if (isspace((int) *p))
156 for (t = tokenlist; t->str; t++)
157 if (TOKEN_MATCH(p, t->str))
167 /* Skip whitespace again. */
168 while (*p && isspace((int) *p))
172 /* we got SOME form of output string, even if it is empty */
179 rcode = T_SINGLE_QUOTED_STRING;
183 rcode = T_DOUBLE_QUOTED_STRING;
187 rcode = T_BACK_QUOTED_STRING;
195 * Read a "word" - this means we don't honor
196 * tokens as delimiters.
198 int getword(char **ptr, char *buf, int buflen)
200 return getthing(ptr, buf, buflen, 0, tokens) == T_EOL ? 0 : 1;
204 * Read a bare "word" - this means we don't honor
205 * tokens as delimiters.
207 int getbareword(char **ptr, char *buf, int buflen)
211 token = getthing(ptr, buf, buflen, 0, NULL);
212 if (token != T_BARE_WORD) {
220 * Read the next word, use tokens as delimiters.
222 LRAD_TOKEN gettoken(char **ptr, char *buf, int buflen)
224 return getthing(ptr, buf, buflen, 1, tokens);