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
24 #include <freeradius-devel/ident.h>
31 #include <freeradius-devel/token.h>
33 static const LRAD_NAME_NUMBER tokens[] = {
34 { "=~", T_OP_REG_EQ, }, /* order is important! */
35 { "!~", T_OP_REG_NE, },
44 { "=*", T_OP_CMP_TRUE, },
45 { "!*", T_OP_CMP_FALSE, },
46 { "==", T_OP_CMP_EQ, },
54 { ";", T_SEMICOLON, },
59 * This works only as long as special tokens
60 * are max. 2 characters, but it's fast.
62 #define TOKEN_MATCH(bptr, tptr) \
63 ( (tptr)[0] == (bptr)[0] && \
64 ((tptr)[1] == (bptr)[1] || (tptr)[1] == 0))
67 * Read a word from a buffer and advance pointer.
68 * This function knows about escapes and quotes.
70 * At end-of-line, buf[0] is set to '\0'.
71 * Returns 0 or special token value.
73 static LRAD_TOKEN getthing(char **ptr, char *buf, int buflen, int tok,
74 const LRAD_NAME_NUMBER *tokenlist)
80 const LRAD_NAME_NUMBER*t;
87 while (*p && isspace((int) *p))
96 * Might be a 1 or 2 character token.
98 if (tok) for (t = tokenlist; t->name; t++) {
99 if (TOKEN_MATCH(p, t->name)) {
100 strcpy(buf, t->name);
101 p += strlen(t->name);
102 while (isspace((int) *p))
105 return (LRAD_TOKEN) t->number;
120 while (*p && buflen-- > 1) {
143 if (*p >= '0' && *p <= '9' &&
144 sscanf(p, "%3o", &x) == 1) {
159 if (quote && (*p == quote)) {
164 if (isspace((int) *p))
167 for (t = tokenlist; t->name; t++)
168 if (TOKEN_MATCH(p, t->name))
178 /* Skip whitespace again. */
179 while (*p && isspace((int) *p))
183 /* we got SOME form of output string, even if it is empty */
190 rcode = T_SINGLE_QUOTED_STRING;
194 rcode = T_DOUBLE_QUOTED_STRING;
198 rcode = T_BACK_QUOTED_STRING;
206 * Read a "word" - this means we don't honor
207 * tokens as delimiters.
209 int getword(char **ptr, char *buf, int buflen)
211 return getthing(ptr, buf, buflen, 0, tokens) == T_EOL ? 0 : 1;
215 * Read a bare "word" - this means we don't honor
216 * tokens as delimiters.
218 int getbareword(char **ptr, char *buf, int buflen)
222 token = getthing(ptr, buf, buflen, 0, NULL);
223 if (token != T_BARE_WORD) {
231 * Read the next word, use tokens as delimiters.
233 LRAD_TOKEN gettoken(char **ptr, char *buf, int buflen)
235 return getthing(ptr, buf, buflen, 1, tokens);
239 * Convert a string to an integer
241 int lrad_str2int(const LRAD_NAME_NUMBER *table, const char *name, int def)
243 const LRAD_NAME_NUMBER *this;
245 for (this = table; this->name != NULL; this++) {
246 if (strcasecmp(this->name, name) == 0) {
255 * Convert an integer to a string.
257 const char *lrad_int2str(const LRAD_NAME_NUMBER *table, int number,
260 const LRAD_NAME_NUMBER *this;
262 for (this = table; this->name != NULL; this++) {
263 if (this->number == number) {