2 * Copyright (C) 2013-2014 Jo-Philipp Wich <jow@openwrt.org>
4 * Permission to use, copy, modify, and/or distribute this software for any
5 * purpose with or without fee is hereby granted, provided that the above
6 * copyright notice and this permission notice appear in all copies.
8 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
9 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
10 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
11 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
12 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
13 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
14 * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
31 int (*parse
)(const char *buf
, struct jp_opcode
*op
);
38 (((x) >= 'a') ? (10 + (x) - 'a') : \
39 (((x) >= 'A') ? (10 + (x) - 'A') : dec(x)))
42 * Stores the given codepoint as a utf8 multibyte sequence into the given
43 * output buffer and substracts the required amount of bytes from the given
46 * Returns false if the multibyte sequence would not fit into the buffer,
51 utf8enc(char **out
, int *rem
, int code
)
53 if (code
> 0 && code
<= 0x7F)
58 *(*out
++) = code
; (*rem
)--;
61 else if (code
> 0 && code
<= 0x7FF)
66 *(*out
)++ = ((code
>> 6) & 0x1F) | 0xC0; (*rem
)--;
67 *(*out
)++ = ( code
& 0x3F) | 0x80; (*rem
)--;
70 else if (code
> 0 && code
<= 0xFFFF)
75 *(*out
)++ = ((code
>> 12) & 0x0F) | 0xE0; (*rem
)--;
76 *(*out
)++ = ((code
>> 6) & 0x3F) | 0x80; (*rem
)--;
77 *(*out
)++ = ( code
& 0x3F) | 0x80; (*rem
)--;
80 else if (code
> 0 && code
<= 0x10FFFF)
85 *(*out
)++ = ((code
>> 18) & 0x07) | 0xF0; (*rem
)--;
86 *(*out
)++ = ((code
>> 12) & 0x3F) | 0x80; (*rem
)--;
87 *(*out
)++ = ((code
>> 6) & 0x3F) | 0x80; (*rem
)--;
88 *(*out
)++ = ( code
& 0x3F) | 0x80; (*rem
)--;
97 * Parses a string literal from the given buffer.
99 * Returns a negative value on error, otherwise the amount of consumed
100 * characters from the given buffer.
103 * -1 Unterminated string
104 * -2 Invalid escape sequence
105 * -3 String literal too long
109 parse_string(const char *buf
, struct jp_opcode
*op
)
112 char str
[128] = { 0 };
114 const char *in
= buf
;
116 int rem
= sizeof(str
) - 1;
121 /* continuation of escape sequence */
127 if (isxdigit(in
[1]) && isxdigit(in
[2]) &&
128 isxdigit(in
[3]) && isxdigit(in
[4]))
130 if (!utf8enc(&out
, &rem
,
131 hex(in
[1]) * 16 * 16 * 16 +
132 hex(in
[2]) * 16 * 16 +
146 else if (in
[0] == 'x')
148 if (isxdigit(in
[1]) && isxdigit(in
[2]))
150 if (!utf8enc(&out
, &rem
, hex(in
[1]) * 16 + hex(in
[2])))
161 /* \377, \77 or \7 */
162 else if (in
[0] >= '0' && in
[0] <= '7')
165 if (in
[1] >= '0' && in
[1] <= '7' &&
166 in
[2] >= '0' && in
[2] <= '7')
168 code
= dec(in
[0]) * 8 * 8 +
175 if (!utf8enc(&out
, &rem
, code
))
182 else if (in
[1] >= '0' && in
[1] <= '7')
184 if (!utf8enc(&out
, &rem
, dec(in
[0]) * 8 + dec(in
[1])))
193 if (!utf8enc(&out
, &rem
, dec(in
[0])))
200 /* single character escape */
208 case 'a': *out
= '\a'; break;
209 case 'b': *out
= '\b'; break;
210 case 'e': *out
= '\e'; break;
211 case 'f': *out
= '\f'; break;
212 case 'n': *out
= '\n'; break;
213 case 'r': *out
= '\r'; break;
214 case 't': *out
= '\t'; break;
215 case 'v': *out
= '\v'; break;
216 default: *out
= *in
; break;
226 /* begin of escape sequence */
227 else if (*in
== '\\')
233 /* terminating quote */
236 op
->str
= strdup(str
);
237 return (in
- buf
) + 2;
255 * Parses a label from the given buffer.
257 * Returns a negative value on error, otherwise the amount of consumed
258 * characters from the given buffer.
265 parse_label(const char *buf
, struct jp_opcode
*op
)
267 char str
[128] = { 0 };
269 const char *in
= buf
;
270 int rem
= sizeof(str
) - 1;
272 while (*in
== '_' || isalnum(*in
))
280 if (!strcmp(str
, "true") || !strcmp(str
, "false"))
282 op
->num
= (str
[0] == 't');
287 op
->str
= strdup(str
);
295 * Parses a number literal from the given buffer.
297 * Returns a negative value on error, otherwise the amount of consumed
298 * characters from the given buffer.
301 * -2 Invalid number character
305 parse_number(const char *buf
, struct jp_opcode
*op
)
308 int n
= strtol(buf
, &e
, 10);
318 static const struct token tokens
[] = {
328 { T_BROPEN
, "[", 1 },
329 { T_BRCLOSE
, "]", 1 },
331 { T_PCLOSE
, ")", 1 },
339 { T_WILDCARD
, "*", 1 },
340 { T_STRING
, "'", 1, parse_string
},
341 { T_STRING
, "\"", 1, parse_string
},
342 { T_LABEL
, "_", 1, parse_label
},
343 { T_LABEL
, "az", 0, parse_label
},
344 { T_LABEL
, "AZ", 0, parse_label
},
345 { T_NUMBER
, "-", 1, parse_number
},
346 { T_NUMBER
, "09", 0, parse_number
},
349 const char *tokennames
[23] = {
365 [T_WILDCARD
] = "'*'",
369 [T_NUMBER
] = "Number",
370 [T_STRING
] = "String",
377 match_token(const char *ptr
, struct jp_opcode
*op
)
380 const struct token
*tok
;
382 for (i
= 0, tok
= &tokens
[0];
383 i
< sizeof(tokens
) / sizeof(tokens
[0]);
384 i
++, tok
= &tokens
[i
])
386 if ((tok
->plen
> 0 && !strncmp(ptr
, tok
->pat
, tok
->plen
)) ||
387 (tok
->plen
== 0 && *ptr
>= tok
->pat
[0] && *ptr
<= tok
->pat
[1]))
389 op
->type
= tok
->type
;
392 return tok
->parse(ptr
, op
);
402 jp_get_token(struct jp_state
*s
, const char *input
, int *mlen
)
404 struct jp_opcode op
= { 0 };
406 *mlen
= match_token(input
, &op
);
408 if (*mlen
< 0 || op
.type
== 0)
411 return jp_alloc_op(s
, op
.type
, op
.num
, op
.str
, NULL
);