Remove redundant file from freeradius-abfab list.
[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 RCSID("$Id$")
25
26 #include <freeradius-devel/libradius.h>
27
28 #include <ctype.h>
29
30 const FR_NAME_NUMBER fr_tokens[] = {
31         { "=~", T_OP_REG_EQ,    }, /* order is important! */
32         { "!~", T_OP_REG_NE,    },
33         { "{",  T_LCBRACE,      },
34         { "}",  T_RCBRACE,      },
35         { "(",  T_LBRACE,       },
36         { ")",  T_RBRACE,       },
37         { ",",  T_COMMA,        },
38         { "++", T_OP_INCRM,     },
39         { "+=", T_OP_ADD,       },
40         { "-=", T_OP_SUB,       },
41         { ":=", T_OP_SET,       },
42         { "=*", T_OP_CMP_TRUE,  },
43         { "!*", T_OP_CMP_FALSE, },
44         { "==", T_OP_CMP_EQ,    },
45         { "=",  T_OP_EQ,        },
46         { "!=", T_OP_NE,        },
47         { ">=", T_OP_GE,        },
48         { ">",  T_OP_GT,        },
49         { "<=", T_OP_LE,        },
50         { "<",  T_OP_LT,        },
51         { "#",  T_HASH,         },
52         { ";",  T_SEMICOLON,    },
53         { NULL, 0,              },
54 };
55
56 const bool fr_assignment_op[] = {
57         false,          /* invalid token */
58         false,          /* end of line */
59         false,          /* { */
60         false,          /* } */
61         false,          /* ( */
62         false,          /* )             5 */
63         false,          /* , */
64         false,          /* ; */
65
66         true,           /* ++ */
67         true,           /* += */
68         true,           /* -=           10 */
69         true,           /* := */
70         true,           /* = */
71         false,          /* != */
72         false,          /* >= */
73         false,          /* >            15 */
74         false,          /* <= */
75         false,          /* < */
76         false,          /* =~ */
77         false,          /* !~ */
78         false,          /* =*           20 */
79         false,          /* !* */
80         false,          /* == */
81         false,                          /* # */
82         false,          /* bare word */
83         false,          /* "foo"        25 */
84         false,          /* 'foo' */
85         false,          /* `foo` */
86         false
87 };
88
89 const bool fr_equality_op[] = {
90         false,          /* invalid token */
91         false,          /* end of line */
92         false,          /* { */
93         false,          /* } */
94         false,          /* ( */
95         false,          /* )             5 */
96         false,          /* , */
97         false,          /* ; */
98
99         false,          /* ++ */
100         false,          /* += */
101         false,          /* -=           10 */
102         false,          /* := */
103         false,          /* = */
104         true,           /* != */
105         true,           /* >= */
106         true,           /* >            15 */
107         true,           /* <= */
108         true,           /* < */
109         true,           /* =~ */
110         true,           /* !~ */
111         true,           /* =*           20 */
112         true,           /* !* */
113         true,           /* == */
114         false,                          /* # */
115         false,          /* bare word */
116         false,          /* "foo"        25 */
117         false,          /* 'foo' */
118         false,          /* `foo` */
119         false
120 };
121
122 const bool fr_str_tok[] = {
123         false,          /* invalid token */
124         false,          /* end of line */
125         false,          /* { */
126         false,          /* } */
127         false,          /* ( */
128         false,          /* )             5 */
129         false,          /* , */
130         false,          /* ; */
131
132         false,          /* ++ */
133         false,          /* += */
134         false,          /* -=           10 */
135         false,          /* := */
136         false,          /* = */
137         false,          /* != */
138         false,          /* >= */
139         false,          /* >            15 */
140         false,          /* <= */
141         false,          /* < */
142         false,          /* =~ */
143         false,          /* !~ */
144         false,          /* =*           20 */
145         false,          /* !* */
146         false,          /* == */
147         false,                          /* # */
148         true,           /* bare word */
149         true,           /* "foo"        25 */
150         true,           /* 'foo' */
151         true,           /* `foo` */
152         false
153 };
154
155 /*
156  *      This works only as long as special tokens
157  *      are max. 2 characters, but it's fast.
158  */
159 #define TOKEN_MATCH(bptr, tptr) \
160         ( (tptr)[0] == (bptr)[0] && \
161          ((tptr)[1] == (bptr)[1] || (tptr)[1] == 0))
162
163 /*
164  *      Read a word from a buffer and advance pointer.
165  *      This function knows about escapes and quotes.
166  *
167  *      At end-of-line, buf[0] is set to '\0'.
168  *      Returns 0 or special token value.
169  */
170 static FR_TOKEN getthing(char const **ptr, char *buf, int buflen, bool tok,
171                          FR_NAME_NUMBER const *tokenlist, bool unescape)
172 {
173         char                    *s;
174         char const              *p;
175         char                    quote;
176         bool                    end = false;
177         unsigned int            x;
178         FR_NAME_NUMBER const    *t;
179         FR_TOKEN rcode;
180
181         buf[0] = '\0';
182
183         /* Skip whitespace */
184         p = *ptr;
185         while (*p && isspace((int) *p))
186                 p++;
187
188         if (*p == 0) {
189                 *ptr = p;
190                 return T_EOL;
191         }
192
193         /*
194          *      Might be a 1 or 2 character token.
195          */
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))
201                                 p++;
202                         *ptr = p;
203                         return (FR_TOKEN) t->number;
204                 }
205         }
206
207         /* Read word. */
208         quote = '\0';
209         if ((*p == '"') ||
210             (*p == '\'') ||
211             (*p == '`')) {
212                 quote = *p;
213                 end = false;
214                 p++;
215         }
216         s = buf;
217
218         while (*p && buflen-- > 1) {
219                 if (unescape && quote && (*p == '\\')) {
220                         p++;
221
222                         switch (*p) {
223                                 case 'r':
224                                         *s++ = '\r';
225                                         break;
226                                 case 'n':
227                                         *s++ = '\n';
228                                         break;
229                                 case 't':
230                                         *s++ = '\t';
231                                         break;
232                                 case '\0':
233                                         *s++ = '\\';
234                                         p--; /* force EOS */
235                                         break;
236                                 default:
237                                         if (*p >= '0' && *p <= '9' &&
238                                             sscanf(p, "%3o", &x) == 1) {
239                                                 *s++ = x;
240                                                 p += 2;
241                                         } else
242                                                 *s++ = *p;
243                                         break;
244                         }
245                         p++;
246                         continue;
247                 }
248
249                 /*
250                  *      Deal with quotes and escapes, but don't mash
251                  *      escaped characters into their non-escaped
252                  *      equivalent.
253                  */
254                 if (!unescape && quote && (*p == '\\')) {
255                         if (!p[1]) continue; /* force end of string */
256
257                         if (p[1] == quote) { /* convert '\'' --> ' */
258                                 p++;
259                         } else {
260                                 *(s++) = *(p++);
261                         }
262                         *(s++) = *(p++);
263                         continue;
264                 }
265
266                 if (quote && (*p == quote)) {
267                         end = true;
268                         p++;
269                         break;
270                 }
271                 if (!quote) {
272                         if (isspace((int) *p))
273                                 break;
274                         if (tok) {
275                                 for (t = tokenlist; t->name; t++)
276                                         if (TOKEN_MATCH(p, t->name))
277                                                 break;
278                                 if (t->name != NULL)
279                                         break;
280                         }
281                         if (*p == ',') break; /* hack */
282                 }
283                 *s++ = *p++;
284         }
285         *s++ = 0;
286
287         if (quote && !end) {
288                 fr_strerror_printf("Unterminated string");
289                 return T_INVALID;
290         }
291
292         /* Skip whitespace again. */
293         while (*p && isspace((int) *p))
294                 p++;
295         *ptr = p;
296
297         /* we got SOME form of output string, even if it is empty */
298         switch (quote) {
299         default:
300                 rcode = T_BARE_WORD;
301                 break;
302
303         case '\'':
304                 rcode = T_SINGLE_QUOTED_STRING;
305                 break;
306
307         case '"':
308                 rcode = T_DOUBLE_QUOTED_STRING;
309                 break;
310
311         case '`':
312                 rcode = T_BACK_QUOTED_STRING;
313                 break;
314         }
315
316         return rcode;
317 }
318
319 /*
320  *      Read a "word" - this means we don't honor
321  *      tokens as delimiters.
322  */
323 int getword(char const **ptr, char *buf, int buflen, bool unescape)
324 {
325         return getthing(ptr, buf, buflen, false, fr_tokens, unescape) == T_EOL ? 0 : 1;
326 }
327
328
329 /*
330  *      Read the next word, use tokens as delimiters.
331  */
332 FR_TOKEN gettoken(char const **ptr, char *buf, int buflen, bool unescape)
333 {
334         return getthing(ptr, buf, buflen, true, fr_tokens, unescape);
335 }
336
337 /*
338  *      Expect an operator.
339  */
340 FR_TOKEN getop(char const **ptr)
341 {
342         char op[3];
343         FR_TOKEN rcode;
344
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");
348                 return T_INVALID;
349         }
350         return rcode;
351 }
352
353 /*
354  *      Expect a string.
355  */
356 FR_TOKEN getstring(char const **ptr, char *buf, int buflen, bool unescape)
357 {
358         char const *p;
359
360         if (!ptr || !*ptr || !buf) return T_INVALID;
361
362         p = *ptr;
363
364         while (*p && (isspace((int)*p))) p++;
365
366         *ptr = p;
367
368         if ((*p == '"') || (*p == '\'') || (*p == '`')) {
369                 return gettoken(ptr, buf, buflen, unescape);
370         }
371
372         return getthing(ptr, buf, buflen, 0, fr_tokens, unescape);
373 }
374
375 /*
376  *      Convert a string to an integer
377  */
378 int fr_str2int(FR_NAME_NUMBER const *table, char const *name, int def)
379 {
380         FR_NAME_NUMBER const *this;
381
382         if (!name) {
383                 return def;
384         }
385
386         for (this = table; this->name != NULL; this++) {
387                 if (strcasecmp(this->name, name) == 0) {
388                         return this->number;
389                 }
390         }
391
392         return def;
393 }
394
395 /*
396  *      Convert a string matching part of name to an integer.
397  */
398 int fr_substr2int(FR_NAME_NUMBER const *table, char const *name, int def, int len)
399 {
400         FR_NAME_NUMBER const *this;
401         size_t max;
402
403         if (!name) {
404                 return def;
405         }
406
407         for (this = table; this->name != NULL; this++) {
408                 size_t tlen;
409
410                 tlen = strlen(this->name);
411
412                 /*
413                  *      Don't match "request" to user input "req".
414                  */
415                 if ((len > 0) && (len < (int) tlen)) continue;
416
417                 /*
418                  *      Match up to the length of the table entry if len is < 0.
419                  */
420                 max = (len < 0) ? tlen : (unsigned)len;
421
422                 if (strncasecmp(this->name, name, max) == 0) {
423                         return this->number;
424                 }
425         }
426
427         return def;
428 }
429
430 /*
431  *      Convert an integer to a string.
432  */
433 char const *fr_int2str(FR_NAME_NUMBER const *table, int number,
434                          char const *def)
435 {
436         FR_NAME_NUMBER const *this;
437
438         for (this = table; this->name != NULL; this++) {
439                 if (this->number == number) {
440                         return this->name;
441                 }
442         }
443
444         return def;
445 }
446
447 char const *fr_token_name(int token)
448 {
449         return fr_int2str(fr_tokens, token, "???");
450 }