1
0
Fork 0
mirror of git://git.psyced.org/git/psyclpc synced 2024-08-15 03:20:16 +00:00

almost functional psyc_render()

This commit is contained in:
psyc://psyced.org/~lynX 2011-04-26 10:02:17 +02:00
parent 811e40d3d0
commit 6342e69416
4 changed files with 75 additions and 49 deletions

View file

@ -606,8 +606,8 @@ int send_erq(int, int*|string, null|closure default: F_CONST0);
#endif
#ifdef USE_PSYC
mixed psyc_parse(int* | string);
string psyc_render(mapping, mapping, string, int* | string);
mixed* psyc_parse(int* | string);
string psyc_render(mixed*);
#endif /* USE_PSYC */

View file

@ -1,57 +1,78 @@
/*------------------------------------------------------------------
* Glue for libpsyc.
*------------------------------------------------------------------
* func_spec for this:
*
mixed psyc_parse(int* | string);
string psyc_render(mapping, mapping, string, int* | string);
*
*------------------------------------------------------------------
* test LPC code:
*
// doesn't work with (int *) yet
mixed x = psyc_parse(":_context\ttest\n\n:_topic\ttesting\n_notice_test_libpsyc\nJust [_topic] libpsyc.\n|\n");
mixed y = psyc_render(([ "_context": "test" ]), ([ "_topic": "testing" ]), "_notice_test_libpsyc", "Just [_topic] libpsyc.");
debug_message(sprintf("libpsyc returned %O and %O\n", x, y));
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!"));
*
*/
#include "array.h"
#include "interpret.h"
#include "mapping.h"
#include "mstrings.h"
#include "pkg-psyc.h"
#include "simulate.h"
#include "xalloc.h"
#include <psyc.h>
#include <psyc/parser.h>
#include <stdio.h>
#include <unistd.h>
#include <fcntl.h>
#ifdef USE_PSYC
# include <stdio.h>
# include <unistd.h>
# include <fcntl.h>
# include <psyc.h>
# include <psyc/parser.h>
# include <psyc/render.h>
/*-------------------------------------------------------------------------*/
// string psyc_render(mapping, mapping, string, int* | string);
// old: string psyc_render(mapping, mapping, string, int* | string);
// new: string psyc_render(mixed*);
svalue_t *
f_psyc_render(svalue_t *sp) {
free_svalue(sp--);
free_svalue(sp--);
free_svalue(sp--);
free_svalue(sp--);
/*
printf("rendering... not yet\n");
mp_int i;
vector_t *v;
psycPacket packet;
string_t *out;
memsafe(
// out = alloc_mstring(5), 5,
out = new_mstring("PSYC"), 5,
"f_psyc_render test");
// unless (sp->type == T_POINTER) return sp;
v = sp->u.vec;
if ((i = (mp_int)VEC_SIZE(v)) != 4) {
errorf("Wrong number of elements (%"PRIdMPINT") in array argument to psyc_render()\n", i);
/* NOTREACHED */
return sp;
}
if (v->item[PSYC_METHOD]->type != T_STRING) {
errorf("Wrong type for PSYC_METHOD element in PSYC packet.\n", i);
/* NOTREACHED */
return sp;
}
if (v->item[PSYC_BODY]->type != T_STRING) {
errorf("Wrong type for PSYC_BODY element in PSYC packet.\n", i);
/* NOTREACHED */
return sp;
}
// FIXME: handle mappings
packet = psyc_newPacket2(NULL, 0, NULL, 0,
get_txt(v->item[PSYC_METHOD].u.str), mstrsize(v->item[PSYC_METHOD].u.str),
// this should be an int* when "binary".. TODO
get_txt(v->item[PSYC_BODY].u.str), mstrsize(v->item[PSYC_BODY].u.str),
PSYC_PACKET_CHECK_LENGTH);
printf("rendering... packet.length = %d\n", packet.length);
memsafe(out = alloc_mstring(packet.length), packet.length, "f_psyc_render");
psyc_render(&packet, get_txt(out), packet.length);
free_svalue(sp);
put_string(sp, out);
*/
put_c_string(++sp, "PSYC");
return sp;
} /* f_psyc_render */
@ -75,12 +96,6 @@ f_psyc_parse (svalue_t *sp) {
psyc_initParseState(&state);
i = 0;
if (sp->type == T_POINTER) {
if ((i = (mp_int)VEC_SIZE(sp->u.vec)) > 32) {
errorf("Bad arg 1 to psyc_parse(): int[] too long (%"PRIdMPINT")\n"
, i);
/* NOTREACHED */
return sp;
}
printf("\npsyc_parse got %ld int* bytes... not supported yet\n", i);
}
else if (sp->type == T_STRING) {
@ -93,10 +108,10 @@ f_psyc_parse (svalue_t *sp) {
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");
put_mapping(&v->item[0], map);
put_mapping(&v->item[PSYC_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[1], map);
put_mapping(&v->item[PSYC_ENTITY], map);
do {
ret = psyc_parse(&state, &oper, &name, &value);
@ -109,7 +124,8 @@ f_psyc_parse (svalue_t *sp) {
// not sure if the following code is good.. TODO
sv = pxalloc( sizeof( svalue_t ) );
put_string(sv, new_n_mstring(name.ptr, name.length));
sv = get_map_lvalue(v->item[0].u.map, sv);
sv = get_map_lvalue(v->item[PSYC_ROUTING].u.map, sv);
// should support int* when necessary.. TODO
put_string(sv, new_n_mstring(value.ptr, value.length));
break;
case PSYC_PARSE_ENTITY:
@ -120,7 +136,8 @@ f_psyc_parse (svalue_t *sp) {
// not sure if the following code is good.. TODO
sv = pxalloc( sizeof( svalue_t ) );
put_string(sv, new_n_mstring(name.ptr, name.length));
sv = get_map_lvalue(v->item[1].u.map, sv);
sv = get_map_lvalue(v->item[PSYC_ENTITY].u.map, sv);
// should support int* when necessary.. TODO
put_string(sv, new_n_mstring(value.ptr, value.length));
if (memcmp(name.ptr, "_list", 5) == 0)
@ -157,11 +174,12 @@ f_psyc_parse (svalue_t *sp) {
// not sure if new_n_mstring is the right thing here.. TODO
str = new_n_mstring(name.ptr, name.length);
// make_tabled gets the shared string for the method
put_string(&v->item[2], make_tabled(str));
put_string(&v->item[PSYC_METHOD], make_tabled(str));
// not sure if new_n_mstring is the right thing here.. TODO
// should support int* when necessary.. TODO
str = new_n_mstring(value.ptr, value.length);
put_string(&v->item[3], str);
put_string(&v->item[PSYC_BODY], str);
break;
case PSYC_PARSE_COMPLETE:
@ -179,8 +197,6 @@ f_psyc_parse (svalue_t *sp) {
} while (ret);
free_svalue(sp);
// push_string(sp, str);
// put_number(sp, 4404); // push a test rc
put_array(sp, v);
return sp;

10
src/pkg-psyc.h Normal file
View file

@ -0,0 +1,10 @@
/* pkg-psyc takes and produces PSYC packets in form
* of an array of mapping, mapping, string and string
* or int* where necessary.
*/
#define PSYC_ROUTING 0
#define PSYC_ENTITY 1
#define PSYC_METHOD 2
#define PSYC_BODY 3

View file

@ -17,7 +17,7 @@ version_longtype="stable"
# A timestamp, to be used by bumpversion and other scripts.
# It can be used, for example, to 'touch' this file on every build, thus
# forcing revision control systems to add it on every checkin automatically.
version_stamp="Tue Apr 26 02:50:13 CEST 2011"
version_stamp="Tue Apr 26 09:46:14 CEST 2011"
# Okay, LDMUD is using 3.x.x so to avoid conflicts let's just use 4.x.x
version_major=4