e161f78379b7471dbef548a97a42233cbd03df2c
[freeradius.git] / src / lib / token.c
1 /*
2  * token.c      Read the next token from a string.
3  *              Yes it's pretty primitive but effective.
4  *
5  * Version:     $Id$
6  *
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.
11  *
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.
16  *
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
20  *
21  * Copyright 2000,2006  The FreeRADIUS server project
22  */
23
24 #include <freeradius-devel/ident.h>
25 RCSID("$Id$")
26
27 #include <freeradius-devel/libradius.h>
28 #include <freeradius-devel/token.h>
29
30 #include <ctype.h>
31
32 static const FR_NAME_NUMBER tokens[] = {
33         { "=~", T_OP_REG_EQ,    }, /* order is important! */
34         { "!~", T_OP_REG_NE,    },
35         { "{",  T_LCBRACE,      },
36         { "}",  T_RCBRACE,      },
37         { "(",  T_LBRACE,       },
38         { ")",  T_RBRACE,       },
39         { ",",  T_COMMA,        },
40         { "+=", T_OP_ADD,       },
41         { "-=", T_OP_SUB,       },
42         { ":=", T_OP_SET,       },
43         { "=*", T_OP_CMP_TRUE,  },
44         { "!*", T_OP_CMP_FALSE, },
45         { "==", T_OP_CMP_EQ,    },
46         { "=",  T_OP_EQ,        },
47         { "!=", T_OP_NE,        },
48         { ">=", T_OP_GE,        },
49         { ">",  T_OP_GT,        },
50         { "<=", T_OP_LE,        },
51         { "<",  T_OP_LT,        },
52         { "#",  T_HASH,         },
53         { ";",  T_SEMICOLON,    },
54         { NULL, 0,              },
55 };
56
57 /*
58  *      This works only as long as special tokens
59  *      are max. 2 characters, but it's fast.
60  */
61 #define TOKEN_MATCH(bptr, tptr) \
62         ( (tptr)[0] == (bptr)[0] && \
63          ((tptr)[1] == (bptr)[1] || (tptr)[1] == 0))
64
65 /*
66  *      Read a word from a buffer and advance pointer.
67  *      This function knows about escapes and quotes.
68  *
69  *      At end-of-line, buf[0] is set to '\0'.
70  *      Returns 0 or special token value.
71  */
72 static FR_TOKEN getthing(const char **ptr, char *buf, int buflen, int tok,
73                          const FR_NAME_NUMBER *tokenlist)
74 {
75         char *s;
76         const char *p;
77         int     quote, end = 0;
78         int     escape;
79         unsigned int    x;
80         const FR_NAME_NUMBER*t;
81         FR_TOKEN rcode;
82
83         buf[0] = 0;
84
85         /* Skip whitespace */
86         p = *ptr;
87         while (*p && isspace((int) *p))
88                 p++;
89
90         if (*p == 0) {
91                 *ptr = p;
92                 return T_EOL;
93         }
94
95         /*
96          *      Might be a 1 or 2 character token.
97          */
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))
103                                 p++;
104                         *ptr = p;
105                         return (FR_TOKEN) t->number;
106                 }
107         }
108
109         /* Read word. */
110         quote = 0;
111         if ((*p == '"') ||
112             (*p == '\'') ||
113             (*p == '`')) {
114                 quote = *p;
115                 end = 0;
116                 p++;
117         }
118         s = buf;
119         escape = 0;
120
121         while (*p && buflen-- > 1) {
122                 if (quote && (*p == '\\')) {
123                         p++;
124
125                         switch(*p) {
126                                 case 'r':
127                                         *s++ = '\r';
128                                         break;
129                                 case 'n':
130                                         *s++ = '\n';
131                                         break;
132                                 case 't':
133                                         *s++ = '\t';
134                                         break;
135                                 case '\0':
136                                         *s++ = '\\';
137                                         p--; /* force EOS */
138                                         break;
139                                 default:
140                                         if (*p >= '0' && *p <= '9' &&
141                                             sscanf(p, "%3o", &x) == 1) {
142                                                 *s++ = x;
143                                                 p += 2;
144                                         } else
145                                                 *s++ = *p;
146                                         break;
147                         }
148                         p++;
149                         continue;
150                 }
151                 if (quote && (*p == quote)) {
152                         end = 1;
153                         p++;
154                         break;
155                 }
156                 if (!quote) {
157                         if (isspace((int) *p))
158                                 break;
159                         if (tok) {
160                                 for (t = tokenlist; t->name; t++)
161                                         if (TOKEN_MATCH(p, t->name))
162                                                 break;
163                                 if (t->name != NULL)
164                                         break;
165                         }
166                 }
167                 *s++ = *p++;
168         }
169         *s++ = 0;
170
171         if (quote && !end) {
172                 fr_strerror_printf("Unterminated string");
173                 return T_OP_INVALID;
174         }
175
176         /* Skip whitespace again. */
177         while (*p && isspace((int) *p))
178                 p++;
179         *ptr = p;
180
181         /* we got SOME form of output string, even if it is empty */
182         switch (quote) {
183         default:
184           rcode = T_BARE_WORD;
185           break;
186
187         case '\'':
188           rcode = T_SINGLE_QUOTED_STRING;
189           break;
190
191         case '"':
192           rcode = T_DOUBLE_QUOTED_STRING;
193           break;
194
195         case '`':
196           rcode = T_BACK_QUOTED_STRING;
197           break;
198         }
199
200         return rcode;
201 }
202
203 /*
204  *      Read a "word" - this means we don't honor
205  *      tokens as delimiters.
206  */
207 int getword(const char **ptr, char *buf, int buflen)
208 {
209         return getthing(ptr, buf, buflen, 0, tokens) == T_EOL ? 0 : 1;
210 }
211
212 /*
213  *      Read a bare "word" - this means we don't honor
214  *      tokens as delimiters.
215  */
216 int getbareword(const char **ptr, char *buf, int buflen)
217 {
218         FR_TOKEN token;
219
220         token = getthing(ptr, buf, buflen, 0, NULL);
221         if (token != T_BARE_WORD) {
222                 return 0;
223         }
224
225         return 1;
226 }
227
228 /*
229  *      Read the next word, use tokens as delimiters.
230  */
231 FR_TOKEN gettoken(const char **ptr, char *buf, int buflen)
232 {
233         return getthing(ptr, buf, buflen, 1, tokens);
234 }
235
236 /*
237  *      Expect a string.
238  */
239 FR_TOKEN getstring(const char **ptr, char *buf, int buflen)
240 {
241         const char *p = *ptr;
242
243         while (p && (isspace((int)*p))) p++;
244
245         *ptr = p;
246
247         if ((*p == '"') || (*p == '\'') || (*p == '`')) {
248                 return gettoken(ptr, buf, buflen);
249         }
250
251         return getthing(ptr, buf, buflen, 0, tokens);
252 }
253
254 /*
255  *      Convert a string to an integer
256  */
257 int fr_str2int(const FR_NAME_NUMBER *table, const char *name, int def)
258 {
259         const FR_NAME_NUMBER *this;
260
261         for (this = table; this->name != NULL; this++) {
262                 if (strcasecmp(this->name, name) == 0) {
263                         return this->number;
264                 }
265         }
266
267         return def;
268 }
269
270 /*
271  *      Convert an integer to a string.
272  */
273 const char *fr_int2str(const FR_NAME_NUMBER *table, int number,
274                          const char *def)
275 {
276         const FR_NAME_NUMBER *this;
277
278         for (this = table; this->name != NULL; this++) {
279                 if (this->number == number) {
280                         return this->name;
281                 }
282         }
283
284         return def;
285 }