2 * Copyright (C) 2013-2014 Jo-Philipp Wich <jo@mein.io>
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
;)
80 jp_parse(const char *expr
)
84 const char *ptr
= expr
;
86 int len
= strlen(expr
);
89 s
= calloc(1, sizeof(*s
));
94 pParser
= ParseAlloc(malloc
);
101 op
= jp_get_token(s
, ptr
, &mlen
);
105 s
->error_code
= mlen
;
110 Parse(pParser
, op
->type
, op
, s
);
118 Parse(pParser
, 0, NULL
, s
);
121 ParseFree(pParser
, free
);