2011-04-25 20:59:53 +00:00
|
|
|
/*------------------------------------------------------------------
|
|
|
|
* Glue for libpsyc.
|
|
|
|
*------------------------------------------------------------------
|
|
|
|
* test LPC code:
|
2011-04-26 08:02:17 +00:00
|
|
|
*
|
2011-04-25 20:59:53 +00:00
|
|
|
// doesn't work with (int *) yet
|
2011-05-12 11:54:56 +00:00
|
|
|
string s = ":_context\ttest\n\n:_topic\ttesting\n_notice_test_libpsyc\nJust [_topic] libpsyc.\n|\n";
|
|
|
|
mixed *f = psyc_parse(s);
|
2011-05-11 15:09:22 +00:00
|
|
|
mixed fr = psyc_render(f);
|
|
|
|
mixed r = ({ ([ "_context": "test" ]), ([ "_topic": "testing" ]),
|
|
|
|
"_notice_test_libpsyc", "Just [_topic] libpsyc." });
|
|
|
|
mixed rr = psyc_render(r);
|
|
|
|
|
2011-05-12 11:54:56 +00:00
|
|
|
debug_message(sprintf("libpsyc returned %O,\n%O\nand\n%O\n(%s)\n", f, fr, rr,
|
|
|
|
fr == s && rr == s ? "SUCCESS": "FAIL!"));
|
2011-05-11 15:09:22 +00:00
|
|
|
mixed *p1 = psyc_parse(":_context\ttest\n\n:_topic\ttest");
|
|
|
|
debug_message(sprintf("p1: libpsyc returned %O\n", p1));
|
|
|
|
mixed *p2 = psyc_parse("ing\n_notice_test_libpsyc\nJust [_topic] libpsyc.\n|\n");
|
|
|
|
debug_message(sprintf("p2: libpsyc returned %O\n", p2));
|
2011-05-19 16:50:22 +00:00
|
|
|
|
|
|
|
mixed pr = psyc_render(p2);
|
|
|
|
debug_message(sprintf("libpsyc returned %O (%s)\n", p2,
|
2011-05-12 11:54:56 +00:00
|
|
|
pr == s ? "SUCCESS": "FAIL!"));
|
2011-05-13 21:17:10 +00:00
|
|
|
|
|
|
|
string ls = ":_context\ttest\n\n:_list_foo\t|foo|bar|baz\n:_list_bar\t\n_notice_test_libpsyc\nJust [_topic] libpsyc.\n|\n";
|
|
|
|
string ls2 = ":_context\ttest\n\n:_list_bar\t\n:_list_foo\t|foo|bar|baz\n_notice_test_libpsyc\nJust [_topic] libpsyc.\n|\n";
|
|
|
|
mixed *lf = psyc_parse(ls);
|
|
|
|
mixed *lfr = psyc_render(lf);
|
|
|
|
mixed lr = ({ ([ "_context": "test" ]),
|
|
|
|
([ "_list_foo": ({ "foo", "bar", "baz" }), "_list_bar": ({}) ]),
|
|
|
|
"_notice_test_libpsyc",
|
|
|
|
"Just [_topic] libpsyc." });
|
|
|
|
mixed lrr = psyc_render(lr);
|
|
|
|
|
|
|
|
debug_message(sprintf("libpsyc returned %O,\n%O\nand\n%O\n(%s)\n", lf, lfr, lrr,
|
|
|
|
(lfr == ls || lfr == ls2) && (lrr == ls || lrr == ls2) ? "SUCCESS": "FAIL!"));
|
2011-04-25 20:59:53 +00:00
|
|
|
*/
|
|
|
|
|
|
|
|
#include "array.h"
|
|
|
|
#include "interpret.h"
|
2011-04-26 00:42:01 +00:00
|
|
|
#include "mapping.h"
|
2011-04-25 20:59:53 +00:00
|
|
|
#include "mstrings.h"
|
2011-05-08 19:54:28 +00:00
|
|
|
#include "object.h"
|
2011-04-26 08:02:17 +00:00
|
|
|
#include "pkg-psyc.h"
|
2011-04-25 20:59:53 +00:00
|
|
|
#include "simulate.h"
|
2011-04-26 00:42:01 +00:00
|
|
|
#include "xalloc.h"
|
2011-04-25 20:59:53 +00:00
|
|
|
|
2011-04-26 08:02:17 +00:00
|
|
|
#ifdef USE_PSYC
|
2011-04-25 20:59:53 +00:00
|
|
|
|
2011-05-08 19:28:31 +00:00
|
|
|
# include "pkg-psyc.h"
|
|
|
|
|
2011-04-26 08:02:17 +00:00
|
|
|
# include <stdio.h>
|
|
|
|
# include <unistd.h>
|
|
|
|
# include <fcntl.h>
|
2011-04-25 20:59:53 +00:00
|
|
|
|
2011-04-26 08:02:17 +00:00
|
|
|
# include <psyc.h>
|
2011-05-09 19:33:45 +00:00
|
|
|
# include <psyc/parse.h>
|
2011-04-26 08:02:17 +00:00
|
|
|
# include <psyc/render.h>
|
2011-04-25 20:59:53 +00:00
|
|
|
|
2011-05-12 14:40:45 +00:00
|
|
|
void
|
|
|
|
fill_header_from_mapping (svalue_t *key, svalue_t *val, void *extra) {
|
|
|
|
psyc_modifier_t *m = extra;
|
2011-05-12 11:54:56 +00:00
|
|
|
char *name, *value;
|
2011-05-13 21:17:10 +00:00
|
|
|
size_t namelen, valuelen, i;
|
2011-05-12 11:54:56 +00:00
|
|
|
|
2011-05-13 21:17:10 +00:00
|
|
|
if (key->type != T_STRING)
|
|
|
|
errorf("fill_header_from_mapping: key type %d not supported\n", key->type);
|
2011-05-12 11:54:56 +00:00
|
|
|
|
|
|
|
name = get_txt(key->u.str);
|
|
|
|
namelen = mstrsize(key->u.str);
|
|
|
|
|
2011-05-13 21:17:10 +00:00
|
|
|
if (val->type == T_STRING) {
|
|
|
|
value = get_txt(val->u.str);
|
|
|
|
valuelen = mstrsize(val->u.str);
|
|
|
|
} else if (val->type == T_POINTER) {
|
|
|
|
psycList list;
|
|
|
|
psycString *elems = NULL;
|
|
|
|
|
|
|
|
if (VEC_SIZE(val->u.vec)) {
|
|
|
|
elems = pxalloc(sizeof(psycString) * VEC_SIZE(val->u.vec));
|
|
|
|
if (!elems) errorf("Out of memory in fill_header_from_mapping for elems");
|
|
|
|
|
|
|
|
for (i = 0; i < VEC_SIZE(val->u.vec); i++) {
|
|
|
|
if (val->u.vec->item[i].type == T_STRING)
|
|
|
|
elems[i] = (psycString){mstrsize(val->u.vec->item[i].u.str), get_txt(val->u.vec->item[i].u.str)};
|
|
|
|
else
|
|
|
|
errorf("fill_header_from_mapping: list value type %d not supported\n", key->type);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
list = psyc_newList(elems, VEC_SIZE(val->u.vec), PSYC_LIST_CHECK_LENGTH);
|
|
|
|
valuelen = list.length;
|
|
|
|
value = pxalloc(valuelen);
|
|
|
|
if (!value) errorf("Out of memory in fill_header_from_mapping for list value");
|
|
|
|
|
|
|
|
psyc_renderList(&list, value, valuelen);
|
|
|
|
} else
|
|
|
|
errorf("fill_header_from_mapping: value type %d not supported\n", key->type);
|
2011-05-12 11:54:56 +00:00
|
|
|
|
2011-05-12 14:40:45 +00:00
|
|
|
m->header->modifiers[m->header->lines++] =
|
|
|
|
psyc_newModifier2(m->oper, name, namelen, value, valuelen, m->flag);
|
2011-05-12 11:54:56 +00:00
|
|
|
}
|
|
|
|
|
2011-04-25 20:59:53 +00:00
|
|
|
/*-------------------------------------------------------------------------*/
|
2011-04-26 08:02:17 +00:00
|
|
|
// old: string psyc_render(mapping, mapping, string, int* | string);
|
|
|
|
// new: string psyc_render(mixed*);
|
2011-04-25 20:59:53 +00:00
|
|
|
|
|
|
|
svalue_t *
|
|
|
|
f_psyc_render(svalue_t *sp) {
|
2011-04-26 08:02:17 +00:00
|
|
|
mp_int i;
|
|
|
|
vector_t *v;
|
2011-04-25 20:59:53 +00:00
|
|
|
string_t *out;
|
2011-04-26 22:19:55 +00:00
|
|
|
char *meth, *body;
|
2011-04-27 10:26:37 +00:00
|
|
|
size_t mlen, blen;
|
|
|
|
mapping_t *map;
|
2011-04-26 08:02:17 +00:00
|
|
|
|
2011-05-12 11:54:56 +00:00
|
|
|
psycPacket packet;
|
|
|
|
psycHeader headers[2];
|
|
|
|
|
2011-04-26 08:02:17 +00:00
|
|
|
// unless (sp->type == T_POINTER) return sp;
|
|
|
|
v = sp->u.vec;
|
2011-05-12 11:54:56 +00:00
|
|
|
if ((i = (mp_int)VEC_SIZE(v)) != 1+PACKET_BODY) {
|
2011-04-26 08:02:17 +00:00
|
|
|
errorf("Wrong number of elements (%"PRIdMPINT") in array argument to psyc_render()\n", i);
|
|
|
|
/* NOTREACHED */
|
|
|
|
return sp;
|
|
|
|
}
|
2011-05-12 11:54:56 +00:00
|
|
|
for (i = PACKET_ROUTING; i <= PACKET_ENTITY; i++) {
|
|
|
|
if (v->item[i].type == T_MAPPING) {
|
|
|
|
map = v->item[i].u.map;
|
|
|
|
headers[i].lines = 0;
|
|
|
|
headers[i].modifiers = malloc(sizeof(psycModifier) * MAP_SIZE(map));
|
|
|
|
if (!headers[i].modifiers) errorf("Out of memory in psyc_render for header.\n");
|
|
|
|
|
2011-05-12 14:40:45 +00:00
|
|
|
if (i == PACKET_ROUTING)
|
|
|
|
walk_mapping(map, &fill_header_from_mapping,
|
|
|
|
&(psyc_modifier_t) {&headers[i], C_GLYPH_OPERATOR_SET,
|
|
|
|
PSYC_MODIFIER_ROUTING});
|
2011-05-12 11:54:56 +00:00
|
|
|
else
|
2011-05-12 14:40:45 +00:00
|
|
|
walk_mapping(map, &fill_header_from_mapping,
|
|
|
|
&(psyc_modifier_t) {&headers[i], C_GLYPH_OPERATOR_SET,
|
|
|
|
PSYC_MODIFIER_CHECK_LENGTH});
|
2011-05-12 11:54:56 +00:00
|
|
|
}
|
2011-04-27 10:26:37 +00:00
|
|
|
}
|
|
|
|
|
2011-04-26 22:19:55 +00:00
|
|
|
#if 0
|
2011-05-12 11:54:56 +00:00
|
|
|
if (v->item[PACKET_METHOD].type != T_STRING) {
|
|
|
|
errorf("Wrong type for PACKET_METHOD element in PSYC packet.\n");
|
2011-04-26 08:02:17 +00:00
|
|
|
/* NOTREACHED */
|
|
|
|
return sp;
|
|
|
|
}
|
2011-05-12 11:54:56 +00:00
|
|
|
if (v->item[PACKET_BODY].type != T_STRING) {
|
|
|
|
errorf("Wrong type for PACKET_BODY element in PSYC packet.\n");
|
2011-04-26 08:02:17 +00:00
|
|
|
/* NOTREACHED */
|
|
|
|
return sp;
|
|
|
|
}
|
2011-04-26 22:19:55 +00:00
|
|
|
#else
|
2011-05-12 11:54:56 +00:00
|
|
|
if (v->item[PACKET_METHOD].type == T_STRING) {
|
|
|
|
meth = get_txt(v->item[PACKET_METHOD].u.str);
|
|
|
|
mlen = mstrsize(v->item[PACKET_METHOD].u.str);
|
2011-04-26 22:19:55 +00:00
|
|
|
} else {
|
|
|
|
meth = NULL;
|
|
|
|
mlen = 0;
|
|
|
|
}
|
2011-05-12 11:54:56 +00:00
|
|
|
if (v->item[PACKET_BODY].type == T_STRING) {
|
|
|
|
body = get_txt(v->item[PACKET_BODY].u.str);
|
|
|
|
blen = mstrsize(v->item[PACKET_BODY].u.str);
|
2011-04-26 22:19:55 +00:00
|
|
|
} else {
|
|
|
|
body = NULL;
|
|
|
|
blen = 0;
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
// TODO: handle _lists
|
2011-05-12 11:54:56 +00:00
|
|
|
packet = psyc_newPacket2(headers[PACKET_ROUTING].modifiers, headers[PACKET_ROUTING].lines,
|
|
|
|
headers[PACKET_ENTITY].modifiers, headers[PACKET_ENTITY].lines,
|
2011-05-08 19:28:31 +00:00
|
|
|
meth, mlen, body, blen,
|
|
|
|
PSYC_PACKET_CHECK_LENGTH);
|
2011-04-26 08:02:17 +00:00
|
|
|
|
2011-05-09 19:33:45 +00:00
|
|
|
printf("rendering... packet.length = %ld\n", packet.length);
|
2011-04-26 20:00:45 +00:00
|
|
|
// alloc_mstring creates an *untabled* string suitable for tmp data
|
2011-04-26 08:02:17 +00:00
|
|
|
memsafe(out = alloc_mstring(packet.length), packet.length, "f_psyc_render");
|
|
|
|
psyc_render(&packet, get_txt(out), packet.length);
|
|
|
|
|
|
|
|
free_svalue(sp);
|
2011-04-25 20:59:53 +00:00
|
|
|
put_string(sp, out);
|
2011-04-26 20:00:45 +00:00
|
|
|
// stack should take care of freeing the string after use
|
2011-04-25 20:59:53 +00:00
|
|
|
return sp;
|
|
|
|
|
|
|
|
} /* f_psyc_render */
|
|
|
|
|
|
|
|
/*-------------------------------------------------------------------------*/
|
|
|
|
// mixed psyc_parse(int* | string);
|
|
|
|
|
|
|
|
svalue_t *
|
|
|
|
f_psyc_parse (svalue_t *sp) {
|
2011-05-11 15:09:22 +00:00
|
|
|
char *buffer = NULL;
|
2011-05-13 21:17:10 +00:00
|
|
|
svalue_t *sv;
|
|
|
|
vector_t *v, *list;
|
2011-04-26 00:42:01 +00:00
|
|
|
mapping_t *map;
|
2011-05-08 14:58:35 +00:00
|
|
|
char oper = 0;
|
2011-05-13 21:17:10 +00:00
|
|
|
psycString name = {0,0}, value = {0,0}, elems[MAX_LIST_SIZE], elem;
|
2011-04-26 00:42:01 +00:00
|
|
|
psycParseListState listState;
|
2011-05-13 21:17:10 +00:00
|
|
|
int ret, retl;
|
|
|
|
size_t size, i;
|
2011-04-25 20:59:53 +00:00
|
|
|
|
2011-05-08 20:11:58 +00:00
|
|
|
assert_shadow_sent(current_object);
|
2011-05-09 19:33:45 +00:00
|
|
|
psyc_state_t *state = O_GET_PSYC_STATE(current_object);
|
2011-05-08 14:58:35 +00:00
|
|
|
if (!state) {
|
2011-05-09 19:33:45 +00:00
|
|
|
state = pxalloc(sizeof(psyc_state_t));
|
2011-05-08 14:58:35 +00:00
|
|
|
if (!state) errorf("Out of memory for psyc state struct.\n");
|
|
|
|
O_GET_PSYC_STATE(current_object) = state;
|
2011-05-09 23:02:14 +00:00
|
|
|
memset(state, 0, sizeof(psyc_state_t));
|
|
|
|
|
2011-05-08 14:58:35 +00:00
|
|
|
state->parser = pxalloc(sizeof(psycParseState));
|
|
|
|
if (!state->parser) errorf("Out of memory for psyc parse state struct.\n");
|
|
|
|
psyc_initParseState(state->parser);
|
|
|
|
}
|
|
|
|
|
2011-04-25 20:59:53 +00:00
|
|
|
if (sp->type == T_POINTER) {
|
2011-05-13 21:17:10 +00:00
|
|
|
errorf("\npsyc_parse got %ld int* bytes... not supported yet\n", VEC_SIZE(sp->u.vec));
|
2011-05-11 15:09:22 +00:00
|
|
|
} else if (sp->type == T_STRING) {
|
2011-05-09 19:33:45 +00:00
|
|
|
printf("\npsyc_parse got a %ld bytes long string...\n", mstrsize(sp->u.str));
|
2011-05-11 15:09:22 +00:00
|
|
|
|
|
|
|
if (state->remaining) {
|
|
|
|
// there are remaining bytes from the previous call to psyc_parse,
|
|
|
|
// copy them together with the newly arrived data
|
|
|
|
buffer = pxalloc(state->remaining_len + mstrsize(sp->u.str));
|
|
|
|
if (!buffer) errorf("Out of memory for psyc_parse buffer.\n");
|
|
|
|
memcpy(buffer, state->remaining, state->remaining_len);
|
|
|
|
memcpy(buffer + state->remaining_len, get_txt(sp->u.str), mstrsize(sp->u.str));
|
|
|
|
psyc_setParseBuffer2(state->parser, buffer, state->remaining_len + mstrsize(sp->u.str));
|
|
|
|
pfree(state->remaining);
|
|
|
|
state->remaining = NULL;
|
|
|
|
state->remaining_len = 0;
|
|
|
|
} else {
|
|
|
|
psyc_setParseBuffer2(state->parser, get_txt(sp->u.str), mstrsize(sp->u.str));
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
errorf("\npsyc_parse got type %d, not supported\n", sp->type);
|
2011-04-25 20:59:53 +00:00
|
|
|
}
|
|
|
|
|
2011-05-19 16:50:22 +00:00
|
|
|
free_svalue(sp);
|
|
|
|
|
2011-05-08 14:58:35 +00:00
|
|
|
if (!state->packet) {
|
|
|
|
state->packet = allocate_array(4);
|
|
|
|
if (!state->packet) errorf("Out of memory for psyc_parse array.\n");
|
2011-05-19 16:50:22 +00:00
|
|
|
v = state->packet;
|
|
|
|
|
|
|
|
map = allocate_mapping(0, 1); // empty mapping
|
|
|
|
if (!map) errorf("Out of memory for psyc_parse routing header.\n");
|
|
|
|
put_mapping(&v->item[PACKET_ROUTING], map);
|
|
|
|
map = allocate_mapping(0, 1); // empty mapping
|
|
|
|
if (!map) errorf("Out of memory for psyc_parse entity header.\n");
|
|
|
|
put_mapping(&v->item[PACKET_ENTITY], map);
|
|
|
|
} else
|
|
|
|
v = state->packet;
|
2011-04-26 00:42:01 +00:00
|
|
|
|
2011-04-25 20:59:53 +00:00
|
|
|
do {
|
2011-05-08 19:28:31 +00:00
|
|
|
ret = psyc_parse(state->parser, &oper, &name, &value);
|
2011-05-08 14:58:35 +00:00
|
|
|
|
2011-05-12 11:54:56 +00:00
|
|
|
printf("#%2d %c%.*s = %.*s\n", ret, oper ? oper : ' ', (int)name.length, name.ptr, (int)value.length, value.ptr);
|
2011-05-09 19:33:45 +00:00
|
|
|
|
2011-05-08 14:58:35 +00:00
|
|
|
switch (ret) {
|
|
|
|
case PSYC_PARSE_ENTITY_START: case PSYC_PARSE_BODY_START:
|
2011-05-09 19:33:45 +00:00
|
|
|
// save oper, name & value in state at the start of incomplete entity or body
|
|
|
|
state->oper = oper;
|
2011-05-11 15:09:22 +00:00
|
|
|
state->name = mstring_alloc_string(name.length);
|
|
|
|
memcpy(get_txt(state->name), name.ptr, name.length);
|
2011-05-09 19:33:45 +00:00
|
|
|
if (!state->name)
|
|
|
|
errorf("Out of memory for name.\n");
|
|
|
|
|
|
|
|
// allocate memory for the total length of the value
|
2011-05-11 15:09:22 +00:00
|
|
|
state->value_len = 0;
|
|
|
|
state->value = mstring_alloc_string(psyc_getParseValueLength(state->parser));
|
2011-05-09 19:33:45 +00:00
|
|
|
if (!state->value)
|
|
|
|
errorf("Out of memory for value.\n");
|
|
|
|
|
|
|
|
// fall thru
|
2011-05-08 14:58:35 +00:00
|
|
|
case PSYC_PARSE_ENTITY_CONT: case PSYC_PARSE_BODY_CONT:
|
|
|
|
case PSYC_PARSE_ENTITY_END: case PSYC_PARSE_BODY_END:
|
2011-05-09 19:33:45 +00:00
|
|
|
// append value to tmp buffer in state
|
2011-05-11 15:09:22 +00:00
|
|
|
memcpy(get_txt(state->value) + state->value_len, value.ptr, value.length);
|
2011-05-09 19:33:45 +00:00
|
|
|
state->value_len += value.length;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (ret == PSYC_PARSE_ENTITY_END || ret == PSYC_PARSE_BODY_END) {
|
|
|
|
// incomplete entity or body parsing done, set oper/name/value to the ones saved in state
|
|
|
|
oper = state->oper;
|
2011-05-11 15:09:22 +00:00
|
|
|
name.ptr = get_txt(state->name);
|
|
|
|
name.length = mstrsize(state->name);
|
|
|
|
value.ptr = get_txt(state->value);
|
|
|
|
value.length = mstrsize(state->value);
|
2011-05-08 14:58:35 +00:00
|
|
|
}
|
|
|
|
|
2011-04-25 20:59:53 +00:00
|
|
|
switch (ret) {
|
2011-05-09 19:33:45 +00:00
|
|
|
case PSYC_PARSE_ROUTING:
|
2011-04-26 00:42:01 +00:00
|
|
|
if (oper != ':') {
|
2011-05-09 19:33:45 +00:00
|
|
|
puts("_failure_unsupported_state");
|
|
|
|
continue;
|
2011-04-26 00:42:01 +00:00
|
|
|
}
|
2011-05-08 14:58:35 +00:00
|
|
|
sv = pxalloc(sizeof(svalue_t));
|
2011-04-26 20:00:45 +00:00
|
|
|
// new_n_tabled fetches a reference of a probably existing
|
|
|
|
// shared string
|
|
|
|
put_string(sv, new_n_tabled(name.ptr, name.length));
|
2011-05-12 11:54:56 +00:00
|
|
|
sv = get_map_lvalue(v->item[PACKET_ROUTING].u.map, sv);
|
2011-04-26 20:00:45 +00:00
|
|
|
// strings are capable of containing 0 so we can do this
|
|
|
|
// for binary data too. let's use a tabled string even
|
|
|
|
// for values of routing variables as they repeat a lot
|
|
|
|
put_string(sv, new_n_tabled(value.ptr, value.length));
|
2011-04-26 00:42:01 +00:00
|
|
|
break;
|
2011-05-11 15:09:22 +00:00
|
|
|
|
2011-05-10 12:10:30 +00:00
|
|
|
case PSYC_PARSE_ENTITY_START:
|
|
|
|
case PSYC_PARSE_ENTITY_CONT:
|
|
|
|
break;
|
2011-05-11 15:09:22 +00:00
|
|
|
|
2011-05-09 19:33:45 +00:00
|
|
|
case PSYC_PARSE_ENTITY_END:
|
2011-05-11 15:09:22 +00:00
|
|
|
if (oper != ':') {
|
|
|
|
puts("_failure_unsupported_state");
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
sv = pxalloc(sizeof(svalue_t));
|
|
|
|
put_string(sv, make_tabled(state->name));
|
2011-05-12 11:54:56 +00:00
|
|
|
sv = get_map_lvalue(v->item[PACKET_ENTITY].u.map, sv);
|
2011-05-11 15:09:22 +00:00
|
|
|
put_string(sv, state->value);
|
|
|
|
break;
|
|
|
|
|
2011-05-09 19:33:45 +00:00
|
|
|
case PSYC_PARSE_ENTITY:
|
|
|
|
if (oper != ':') {
|
|
|
|
puts("_failure_unsupported_state");
|
|
|
|
continue;
|
2011-04-26 00:42:01 +00:00
|
|
|
}
|
2011-05-09 19:33:45 +00:00
|
|
|
|
|
|
|
sv = pxalloc(sizeof(svalue_t));
|
|
|
|
put_string(sv, new_n_tabled(name.ptr, name.length));
|
2011-05-12 11:54:56 +00:00
|
|
|
sv = get_map_lvalue(v->item[PACKET_ENTITY].u.map, sv);
|
2011-05-08 14:58:35 +00:00
|
|
|
|
2011-04-26 20:00:45 +00:00
|
|
|
// is it good to put entity variable values into the
|
|
|
|
// shared string table? probably yes.. but it's a guess
|
|
|
|
//t_string(sv, new_n_mstring(value.ptr, value.length));
|
|
|
|
put_string(sv, new_n_tabled(value.ptr, value.length));
|
2011-04-26 00:42:01 +00:00
|
|
|
break;
|
2011-05-11 15:09:22 +00:00
|
|
|
|
2011-05-10 12:10:30 +00:00
|
|
|
case PSYC_PARSE_BODY_START:
|
|
|
|
case PSYC_PARSE_BODY_CONT:
|
|
|
|
break;
|
2011-05-11 15:09:22 +00:00
|
|
|
|
2011-05-09 19:33:45 +00:00
|
|
|
case PSYC_PARSE_BODY_END:
|
2011-05-12 11:54:56 +00:00
|
|
|
put_string(&v->item[PACKET_METHOD], make_tabled(state->name));
|
|
|
|
put_string(&v->item[PACKET_BODY], state->value);
|
2011-05-11 15:09:22 +00:00
|
|
|
break;
|
|
|
|
|
2011-05-09 19:33:45 +00:00
|
|
|
case PSYC_PARSE_BODY:
|
2011-04-26 20:00:45 +00:00
|
|
|
// new_n_tabled gets the shared string for the method
|
2011-05-12 11:54:56 +00:00
|
|
|
put_string(&v->item[PACKET_METHOD],
|
2011-05-09 19:33:45 +00:00
|
|
|
new_n_tabled(name.ptr, name.length));
|
2011-04-26 20:00:45 +00:00
|
|
|
|
|
|
|
// allocate an untabled string for the packet body
|
2011-05-12 11:54:56 +00:00
|
|
|
put_string(&v->item[PACKET_BODY],
|
2011-04-26 20:00:45 +00:00
|
|
|
new_n_mstring(value.ptr, value.length));
|
2011-04-25 20:59:53 +00:00
|
|
|
break;
|
2011-05-11 15:09:22 +00:00
|
|
|
|
2011-05-09 19:33:45 +00:00
|
|
|
case PSYC_PARSE_COMPLETE:
|
2011-05-09 23:02:14 +00:00
|
|
|
state->packet = NULL;
|
2011-05-19 16:50:22 +00:00
|
|
|
put_array(sp, v);
|
2011-04-25 20:59:53 +00:00
|
|
|
ret = 0;
|
|
|
|
break;
|
2011-05-11 15:09:22 +00:00
|
|
|
|
2011-05-09 19:33:45 +00:00
|
|
|
case PSYC_PARSE_INSUFFICIENT:
|
|
|
|
// insufficient data, save remaining bytes
|
|
|
|
state->remaining_len = psyc_getParseRemainingLength(state->parser);
|
|
|
|
if (state->remaining_len) {
|
|
|
|
state->remaining = pxalloc(state->remaining_len);
|
|
|
|
memcpy(state->remaining,
|
|
|
|
psyc_getParseRemainingBuffer(state->parser),
|
|
|
|
state->remaining_len);
|
|
|
|
} else
|
|
|
|
state->remaining = NULL;
|
2011-05-11 15:09:22 +00:00
|
|
|
|
2011-05-19 16:50:22 +00:00
|
|
|
put_number(sp, 0);
|
2011-05-11 15:09:22 +00:00
|
|
|
ret = 0;
|
2011-05-09 19:33:45 +00:00
|
|
|
break;
|
2011-05-11 15:09:22 +00:00
|
|
|
|
2011-05-09 19:33:45 +00:00
|
|
|
default:
|
2011-04-25 20:59:53 +00:00
|
|
|
errorf("Error while parsing PSYC: %i\n", ret);
|
|
|
|
/* NOTREACHED */
|
|
|
|
return sp;
|
|
|
|
}
|
2011-05-09 19:33:45 +00:00
|
|
|
|
2011-05-11 15:09:22 +00:00
|
|
|
switch (ret) {
|
|
|
|
case PSYC_PARSE_BODY_END:
|
|
|
|
case PSYC_PARSE_ENTITY_END:
|
|
|
|
// reset tmp buffers in state when incomplete entity or body parsing is done
|
|
|
|
state->oper = 0;
|
|
|
|
state->name = NULL;
|
|
|
|
state->value = NULL;
|
|
|
|
if (ret == PSYC_PARSE_BODY_END)
|
|
|
|
break;
|
|
|
|
case PSYC_PARSE_ENTITY:
|
|
|
|
if (psyc_isListVar(&name)) {
|
2011-05-13 21:17:10 +00:00
|
|
|
size = 0;
|
|
|
|
if (value.length) {
|
|
|
|
psyc_initParseListState(&listState);
|
|
|
|
psyc_setParseListBuffer(&listState, value);
|
|
|
|
elem = (psycString){0, 0};
|
|
|
|
do {
|
|
|
|
retl = psyc_parseList(&listState, &elem);
|
|
|
|
switch (retl) {
|
|
|
|
case PSYC_PARSE_LIST_END:
|
|
|
|
retl = 0;
|
|
|
|
case PSYC_PARSE_LIST_ELEM:
|
|
|
|
if (size >= MAX_LIST_SIZE) {
|
|
|
|
errorf("Error while parsing PSYC list: max list size reached\n");
|
|
|
|
/* NOTREACHED */
|
|
|
|
return sp;
|
|
|
|
}
|
|
|
|
elems[size++] = elem;
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
errorf("Error while parsing PSYC list: %i\n", retl);
|
|
|
|
/* NOTREACHED */
|
|
|
|
return sp;
|
|
|
|
}
|
|
|
|
} while (retl > 0);
|
2011-05-11 15:09:22 +00:00
|
|
|
}
|
2011-05-13 21:17:10 +00:00
|
|
|
|
|
|
|
list = allocate_array(size);
|
|
|
|
for (i = 0; i < size; i++)
|
|
|
|
put_string(&list->item[i], new_n_tabled(elems[i].ptr, elems[i].length));
|
|
|
|
|
|
|
|
put_array(sv, list);
|
2011-05-11 15:09:22 +00:00
|
|
|
}
|
2011-05-09 19:33:45 +00:00
|
|
|
}
|
2011-04-25 20:59:53 +00:00
|
|
|
} while (ret);
|
|
|
|
|
2011-05-11 15:09:22 +00:00
|
|
|
if (buffer)
|
|
|
|
pfree(buffer);
|
|
|
|
|
2011-04-25 20:59:53 +00:00
|
|
|
return sp;
|
|
|
|
} /* f_psyc_parse */
|
|
|
|
|
|
|
|
#endif /* USE_PSYC */
|