Added more 'const'
[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;
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                 p++;
116         }
117         s = buf;
118         escape = 0;
119
120         while (*p && buflen-- > 1) {
121                 if (quote && (*p == '\\')) {
122                         p++;
123
124                         switch(*p) {
125                                 case 'r':
126                                         *s++ = '\r';
127                                         break;
128                                 case 'n':
129                                         *s++ = '\n';
130                                         break;
131                                 case 't':
132                                         *s++ = '\t';
133                                         break;
134                                 case '\0':
135                                         *s++ = '\\';
136                                         p--; /* force EOS */
137                                         break;
138                                 default:
139                                         if (*p >= '0' && *p <= '9' &&
140                                             sscanf(p, "%3o", &x) == 1) {
141                                                 *s++ = x;
142                                                 p += 2;
143                                         } else
144                                                 *s++ = *p;
145                                         break;
146                         }
147                         p++;
148                         continue;
149                 }
150                 if (quote && (*p == quote)) {
151                         p++;
152                         break;
153                 }
154                 if (!quote) {
155                         if (isspace((int) *p))
156                                 break;
157                         if (tok) {
158                                 for (t = tokenlist; t->name; t++)
159                                         if (TOKEN_MATCH(p, t->name))
160                                                 break;
161                                 if (t->name != NULL)
162                                         break;
163                         }
164                 }
165                 *s++ = *p++;
166         }
167         *s++ = 0;
168
169         /* Skip whitespace again. */
170         while (*p && isspace((int) *p))
171                 p++;
172         *ptr = p;
173
174         /* we got SOME form of output string, even if it is empty */
175         switch (quote) {
176         default:
177           rcode = T_BARE_WORD;
178           break;
179
180         case '\'':
181           rcode = T_SINGLE_QUOTED_STRING;
182           break;
183
184         case '"':
185           rcode = T_DOUBLE_QUOTED_STRING;
186           break;
187
188         case '`':
189           rcode = T_BACK_QUOTED_STRING;
190           break;
191         }
192
193         return rcode;
194 }
195
196 /*
197  *      Read a "word" - this means we don't honor
198  *      tokens as delimiters.
199  */
200 int getword(const char **ptr, char *buf, int buflen)
201 {
202         return getthing(ptr, buf, buflen, 0, tokens) == T_EOL ? 0 : 1;
203 }
204
205 /*
206  *      Read a bare "word" - this means we don't honor
207  *      tokens as delimiters.
208  */
209 int getbareword(const char **ptr, char *buf, int buflen)
210 {
211         FR_TOKEN token;
212
213         token = getthing(ptr, buf, buflen, 0, NULL);
214         if (token != T_BARE_WORD) {
215                 return 0;
216         }
217
218         return 1;
219 }
220
221 /*
222  *      Read the next word, use tokens as delimiters.
223  */
224 FR_TOKEN gettoken(const char **ptr, char *buf, int buflen)
225 {
226         return getthing(ptr, buf, buflen, 1, tokens);
227 }
228
229 /*
230  *      Expect a string.
231  */
232 FR_TOKEN getstring(const char **ptr, char *buf, int buflen)
233 {
234         const char *p = *ptr;
235
236         while (p && (isspace((int)*p))) p++;
237
238         *ptr = p;
239
240         if ((*p == '"') || (*p == '\'') || (*p == '`')) {
241                 return gettoken(ptr, buf, buflen);
242         }
243
244         return getthing(ptr, buf, buflen, 0, tokens);
245 }
246
247 /*
248  *      Convert a string to an integer
249  */
250 int fr_str2int(const FR_NAME_NUMBER *table, const char *name, int def)
251 {
252         const FR_NAME_NUMBER *this;
253
254         for (this = table; this->name != NULL; this++) {
255                 if (strcasecmp(this->name, name) == 0) {
256                         return this->number;
257                 }
258         }
259
260         return def;
261 }
262
263 /*
264  *      Convert an integer to a string.
265  */
266 const char *fr_int2str(const FR_NAME_NUMBER *table, int number,
267                          const char *def)
268 {
269         const FR_NAME_NUMBER *this;
270
271         for (this = table; this->name != NULL; this++) {
272                 if (this->number == number) {
273                         return this->name;
274                 }
275         }
276
277         return def;
278 }