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-04-26 08:02:17 +00:00
|
|
|
mixed* x = psyc_parse(":_context\ttest\n\n:_topic\ttesting\n_notice_test_libpsyc\nJust [_topic] libpsyc.\n|\n");
|
|
|
|
mixed y = psyc_render(x);
|
|
|
|
mixed z = ({ ([ "_context": "test" ]), ([ "_topic": "testing" ]), "_notice_test_libpsyc", "Just [_topic] libpsyc." });
|
|
|
|
z = psyc_render(z);
|
|
|
|
// psyc_render currently having some memory corruption problems
|
|
|
|
debug_message(sprintf("libpsyc returned %O, %O and %O (%s)\n", x, y, z,
|
|
|
|
y == z? "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-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-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>
|
|
|
|
# include <psyc/parser.h>
|
|
|
|
# include <psyc/render.h>
|
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;
|
|
|
|
psycPacket packet;
|
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
|
|
|
|
|
|
|
// unless (sp->type == T_POINTER) return sp;
|
|
|
|
v = sp->u.vec;
|
2011-04-26 22:19:55 +00:00
|
|
|
if ((i = (mp_int)VEC_SIZE(v)) != 1+PSYC_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-04-27 10:26:37 +00:00
|
|
|
if (v->item[PSYC_ROUTING].type == T_MAPPING) {
|
|
|
|
map = v->item[PSYC_ROUTING].u.map;
|
|
|
|
} else {
|
|
|
|
map = NULL;
|
|
|
|
}
|
|
|
|
|
2011-04-26 22:19:55 +00:00
|
|
|
#if 0
|
2011-04-26 20:00:45 +00:00
|
|
|
if (v->item[PSYC_METHOD].type != T_STRING) {
|
|
|
|
errorf("Wrong type for PSYC_METHOD element in PSYC packet.\n");
|
2011-04-26 08:02:17 +00:00
|
|
|
/* NOTREACHED */
|
|
|
|
return sp;
|
|
|
|
}
|
2011-04-26 20:00:45 +00:00
|
|
|
if (v->item[PSYC_BODY].type != T_STRING) {
|
|
|
|
errorf("Wrong type for PSYC_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
|
|
|
|
if (v->item[PSYC_METHOD].type == T_STRING) {
|
|
|
|
meth = get_txt(v->item[PSYC_METHOD].u.str);
|
|
|
|
mlen = mstrsize(v->item[PSYC_METHOD].u.str);
|
|
|
|
} else {
|
|
|
|
meth = NULL;
|
|
|
|
mlen = 0;
|
|
|
|
}
|
|
|
|
if (v->item[PSYC_BODY].type == T_STRING) {
|
|
|
|
body = get_txt(v->item[PSYC_BODY].u.str);
|
|
|
|
blen = mstrsize(v->item[PSYC_BODY].u.str);
|
|
|
|
} else {
|
|
|
|
body = NULL;
|
|
|
|
blen = 0;
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
// TODO: handle _lists
|
2011-04-26 08:02:17 +00:00
|
|
|
// FIXME: handle mappings
|
|
|
|
packet = psyc_newPacket2(NULL, 0, NULL, 0,
|
2011-04-26 22:19:55 +00:00
|
|
|
meth, mlen, body, blen,
|
2011-04-26 08:02:17 +00:00
|
|
|
PSYC_PACKET_CHECK_LENGTH);
|
|
|
|
|
|
|
|
printf("rendering... packet.length = %d\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-04-26 00:42:01 +00:00
|
|
|
string_t *str = NULL;
|
|
|
|
vector_t *v;
|
|
|
|
mapping_t *map;
|
|
|
|
svalue_t *sv;
|
2011-04-25 20:59:53 +00:00
|
|
|
mp_int i;
|
2011-04-26 00:42:01 +00:00
|
|
|
psycString name, value, elem;
|
|
|
|
psycParseState state;
|
|
|
|
psycParseListState listState;
|
2011-04-25 20:59:53 +00:00
|
|
|
char oper;
|
|
|
|
int ret;
|
|
|
|
|
2011-04-26 00:42:01 +00:00
|
|
|
psyc_initParseState(&state);
|
2011-04-25 20:59:53 +00:00
|
|
|
i = 0;
|
|
|
|
if (sp->type == T_POINTER) {
|
2011-04-26 00:42:01 +00:00
|
|
|
printf("\npsyc_parse got %ld int* bytes... not supported yet\n", i);
|
2011-04-25 20:59:53 +00:00
|
|
|
}
|
|
|
|
else if (sp->type == T_STRING) {
|
2011-04-26 00:42:01 +00:00
|
|
|
printf("\npsyc_parse got a %d bytes long string...\n", mstrsize(sp->u.str));
|
|
|
|
psyc_nextParseBuffer(&state, psyc_newString(get_txt(sp->u.str),
|
2011-04-25 20:59:53 +00:00
|
|
|
mstrsize(sp->u.str)));
|
|
|
|
}
|
|
|
|
|
2011-04-26 00:42:01 +00:00
|
|
|
v = allocate_array(4);
|
|
|
|
if (!v) errorf("Out of memory for psyc_parse array.\n");
|
|
|
|
map = allocate_mapping( 0, 1 ); // empty mapping
|
|
|
|
if (!map) errorf("Out of memory for psyc_parse routing header.\n");
|
2011-04-26 08:02:17 +00:00
|
|
|
put_mapping(&v->item[PSYC_ROUTING], map);
|
2011-04-26 00:42:01 +00:00
|
|
|
map = allocate_mapping( 0, 1 ); // empty mapping
|
|
|
|
if (!map) errorf("Out of memory for psyc_parse entity header.\n");
|
2011-04-26 08:02:17 +00:00
|
|
|
put_mapping(&v->item[PSYC_ENTITY], map);
|
2011-04-26 00:42:01 +00:00
|
|
|
|
2011-04-25 20:59:53 +00:00
|
|
|
do {
|
2011-04-26 00:42:01 +00:00
|
|
|
ret = psyc_parse(&state, &oper, &name, &value);
|
2011-04-25 20:59:53 +00:00
|
|
|
switch (ret) {
|
|
|
|
case PSYC_PARSE_ROUTING:
|
2011-04-26 00:42:01 +00:00
|
|
|
if (oper != ':') {
|
|
|
|
puts("_failure_unsupported_state");
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
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-04-26 08:02:17 +00:00
|
|
|
sv = get_map_lvalue(v->item[PSYC_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-04-25 20:59:53 +00:00
|
|
|
case PSYC_PARSE_ENTITY:
|
2011-04-26 00:42:01 +00:00
|
|
|
if (oper != ':') {
|
|
|
|
puts("_failure_unsupported_state");
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
sv = pxalloc( sizeof( svalue_t ) );
|
2011-04-26 20:00:45 +00:00
|
|
|
put_string(sv, new_n_tabled(name.ptr, name.length));
|
2011-04-26 08:02:17 +00:00
|
|
|
sv = get_map_lvalue(v->item[PSYC_ENTITY].u.map, sv);
|
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
|
|
|
|
2011-04-26 20:00:45 +00:00
|
|
|
// list parsing not supported yet.. TODO
|
2011-04-25 20:59:53 +00:00
|
|
|
if (memcmp(name.ptr, "_list", 5) == 0)
|
|
|
|
{
|
|
|
|
write(1, ">>> LIST START\n", 15);
|
2011-04-26 00:42:01 +00:00
|
|
|
psyc_initParseListState(&listState);
|
|
|
|
psyc_nextParseListBuffer(&listState, value);
|
|
|
|
while ((ret = psyc_parseList(&listState, &name, &value, &elem)))
|
2011-04-25 20:59:53 +00:00
|
|
|
{
|
|
|
|
switch (ret)
|
|
|
|
{
|
|
|
|
case PSYC_PARSE_LIST_END:
|
|
|
|
case PSYC_PARSE_LIST_ELEM:
|
|
|
|
write(1, "|", 1);
|
|
|
|
write(1, elem.ptr, elem.length);
|
|
|
|
write(1, "\n", 1);
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
errorf("Error while parsing PSYC list: %i\n", ret);
|
|
|
|
/* NOTREACHED */
|
|
|
|
return sp;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (ret == PSYC_PARSE_LIST_END)
|
|
|
|
{
|
|
|
|
write(1, ">>> LIST END\n", 13);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2011-04-26 00:42:01 +00:00
|
|
|
break;
|
|
|
|
case PSYC_PARSE_BODY:
|
|
|
|
if (str) errorf("Got two PSYC methods in the same packet!?\n");
|
2011-04-26 20:00:45 +00:00
|
|
|
// new_n_tabled gets the shared string for the method
|
|
|
|
put_string(&v->item[PSYC_METHOD],
|
|
|
|
new_n_tabled(name.ptr, name.length));
|
|
|
|
|
|
|
|
// allocate an untabled string for the packet body
|
|
|
|
put_string(&v->item[PSYC_BODY],
|
|
|
|
new_n_mstring(value.ptr, value.length));
|
2011-04-26 00:42:01 +00:00
|
|
|
|
2011-04-25 20:59:53 +00:00
|
|
|
break;
|
|
|
|
case PSYC_PARSE_COMPLETE:
|
|
|
|
ret = 0;
|
|
|
|
break;
|
|
|
|
case PSYC_PARSE_INSUFFICIENT:
|
|
|
|
errorf("Insufficient PSYC data.\n");
|
|
|
|
/* NOTREACHED */
|
|
|
|
return sp;
|
|
|
|
default:
|
|
|
|
errorf("Error while parsing PSYC: %i\n", ret);
|
|
|
|
/* NOTREACHED */
|
|
|
|
return sp;
|
|
|
|
}
|
|
|
|
} while (ret);
|
|
|
|
|
|
|
|
free_svalue(sp);
|
2011-04-26 00:42:01 +00:00
|
|
|
put_array(sp, v);
|
2011-04-25 20:59:53 +00:00
|
|
|
return sp;
|
|
|
|
|
|
|
|
} /* f_psyc_parse */
|
|
|
|
|
|
|
|
#endif /* USE_PSYC */
|