2 * token.c Read the next token from a string.
3 * Yes it's pretty primitive but effective.
7 * This library is free software; you can redistribute it and/or
8 * modify it under the terms of the GNU Lesser General Public
9 * License as published by the Free Software Foundation; either
10 * version 2.1 of the License, or (at your option) any later version.
12 * This library is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 * Lesser General Public License for more details.
17 * You should have received a copy of the GNU Lesser General Public
18 * License along with this library; if not, write to the Free Software
19 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
21 * Copyright 2000,2006 The FreeRADIUS server project
26 #include <freeradius-devel/libradius.h>
30 const FR_NAME_NUMBER fr_tokens[] = {
31 { "=~", T_OP_REG_EQ, }, /* order is important! */
32 { "!~", T_OP_REG_NE, },
38 { "++", T_OP_INCRM, },
42 { "=*", T_OP_CMP_TRUE, },
43 { "!*", T_OP_CMP_FALSE, },
44 { "==", T_OP_CMP_EQ, },
52 { ";", T_SEMICOLON, },
56 const bool fr_assignment_op[] = {
57 false, /* invalid token */
58 false, /* end of line */
82 false, /* bare word */
89 const bool fr_equality_op[] = {
90 false, /* invalid token */
91 false, /* end of line */
115 false, /* bare word */
116 false, /* "foo" 25 */
122 const bool fr_str_tok[] = {
123 false, /* invalid token */
124 false, /* end of line */
148 true, /* bare word */
156 * This works only as long as special tokens
157 * are max. 2 characters, but it's fast.
159 #define TOKEN_MATCH(bptr, tptr) \
160 ( (tptr)[0] == (bptr)[0] && \
161 ((tptr)[1] == (bptr)[1] || (tptr)[1] == 0))
164 * Read a word from a buffer and advance pointer.
165 * This function knows about escapes and quotes.
167 * At end-of-line, buf[0] is set to '\0'.
168 * Returns 0 or special token value.
170 static FR_TOKEN getthing(char const **ptr, char *buf, int buflen, bool tok,
171 FR_NAME_NUMBER const *tokenlist, bool unescape)
178 FR_NAME_NUMBER const *t;
183 /* Skip whitespace */
185 while (*p && isspace((int) *p))
194 * Might be a 1 or 2 character token.
196 if (tok) for (t = tokenlist; t->name; t++) {
197 if (TOKEN_MATCH(p, t->name)) {
198 strcpy(buf, t->name);
199 p += strlen(t->name);
200 while (isspace((int) *p))
203 return (FR_TOKEN) t->number;
218 while (*p && buflen-- > 1) {
219 if (unescape && quote && (*p == '\\')) {
237 if (*p >= '0' && *p <= '9' &&
238 sscanf(p, "%3o", &x) == 1) {
250 * Deal with quotes and escapes, but don't mash
251 * escaped characters into their non-escaped
254 if (!unescape && quote && (*p == '\\')) {
255 if (!p[1]) continue; /* force end of string */
257 if (p[1] == quote) { /* convert '\'' --> ' */
266 if (quote && (*p == quote)) {
272 if (isspace((int) *p))
275 for (t = tokenlist; t->name; t++)
276 if (TOKEN_MATCH(p, t->name))
281 if (*p == ',') break; /* hack */
288 fr_strerror_printf("Unterminated string");
292 /* Skip whitespace again. */
293 while (*p && isspace((int) *p))
297 /* we got SOME form of output string, even if it is empty */
304 rcode = T_SINGLE_QUOTED_STRING;
308 rcode = T_DOUBLE_QUOTED_STRING;
312 rcode = T_BACK_QUOTED_STRING;
320 * Read a "word" - this means we don't honor
321 * tokens as delimiters.
323 int getword(char const **ptr, char *buf, int buflen, bool unescape)
325 return getthing(ptr, buf, buflen, false, fr_tokens, unescape) == T_EOL ? 0 : 1;
330 * Read the next word, use tokens as delimiters.
332 FR_TOKEN gettoken(char const **ptr, char *buf, int buflen, bool unescape)
334 return getthing(ptr, buf, buflen, true, fr_tokens, unescape);
338 * Expect an operator.
340 FR_TOKEN getop(char const **ptr)
345 rcode = getthing(ptr, op, sizeof(op), true, fr_tokens, false);
346 if (!fr_assignment_op[rcode] && !fr_equality_op[rcode]) {
347 fr_strerror_printf("Expected operator");
356 FR_TOKEN getstring(char const **ptr, char *buf, int buflen, bool unescape)
360 if (!ptr || !*ptr || !buf) return T_INVALID;
364 while (*p && (isspace((int)*p))) p++;
368 if ((*p == '"') || (*p == '\'') || (*p == '`')) {
369 return gettoken(ptr, buf, buflen, unescape);
372 return getthing(ptr, buf, buflen, 0, fr_tokens, unescape);
376 * Convert a string to an integer
378 int fr_str2int(FR_NAME_NUMBER const *table, char const *name, int def)
380 FR_NAME_NUMBER const *this;
386 for (this = table; this->name != NULL; this++) {
387 if (strcasecmp(this->name, name) == 0) {
396 * Convert a string matching part of name to an integer.
398 int fr_substr2int(FR_NAME_NUMBER const *table, char const *name, int def, int len)
400 FR_NAME_NUMBER const *this;
407 for (this = table; this->name != NULL; this++) {
410 tlen = strlen(this->name);
413 * Don't match "request" to user input "req".
415 if ((len > 0) && (len < (int) tlen)) continue;
418 * Match up to the length of the table entry if len is < 0.
420 max = (len < 0) ? tlen : (unsigned)len;
422 if (strncasecmp(this->name, name, max) == 0) {
431 * Convert an integer to a string.
433 char const *fr_int2str(FR_NAME_NUMBER const *table, int number,
436 FR_NAME_NUMBER const *this;
438 for (this = table; this->name != NULL; this++) {
439 if (this->number == number) {
447 char const *fr_token_name(int token)
449 return fr_int2str(fr_tokens, token, "???");