5093e8b5167b61f19473e7536ab80d530ff36536
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.
24 #include <libubox/utils.h>
27 jp_alloc_op(struct jp_state
*s
, int type
, int num
, char *str
, ...)
31 struct jp_opcode
*newop
, *child
;
33 newop
= calloc_a(sizeof(*newop
),
34 str
? &ptr
: NULL
, str
? strlen(str
) + 1 : 0);
38 fprintf(stderr
, "Out of memory\n");
46 newop
->str
= strcpy(ptr
, str
);
50 while ((child
= va_arg(ap
, void *)) != NULL
)
54 append_op(newop
->down
, child
);
58 newop
->next
= s
->pool
;
65 jp_free(struct jp_state
*s
)
67 struct jp_opcode
*op
, *tmp
;
69 for (op
= s
->pool
; op
;)
83 jp_parse(const char *expr
)
87 const char *ptr
= expr
;
89 int len
= strlen(expr
);
92 s
= calloc(1, sizeof(*s
));
97 pParser
= ParseAlloc(malloc
);
104 s
->off
= (ptr
- expr
);
106 op
= jp_get_token(s
, ptr
, &mlen
);
111 s
->error
= strdup((mlen
== -3) ? "String too long" :
112 (mlen
== -2) ? "Invalid escape sequence" :
113 (mlen
== -1) ? "Unterminated string" :
120 Parse(pParser
, op
->type
, op
, s
);
126 Parse(pParser
, 0, NULL
, s
);
129 ParseFree(pParser
, free
);