From a770a42eacc36df162dc2373890feddb9f2d735d Mon Sep 17 00:00:00 2001 From: Gabor Adam Toth Date: Thu, 12 May 2011 13:54:56 +0200 Subject: [PATCH] pkg-psyc: modifier rendering, fix for conflicting PSYC_ defines --- src/pkg-psyc.c | 108 +++++++++++++++++++++++++++++++++---------------- src/pkg-psyc.h | 8 ++-- src/version.sh | 2 +- 3 files changed, 79 insertions(+), 39 deletions(-) diff --git a/src/pkg-psyc.c b/src/pkg-psyc.c index 7731426..e86496f 100644 --- a/src/pkg-psyc.c +++ b/src/pkg-psyc.c @@ -4,14 +4,15 @@ * test LPC code: * // doesn't work with (int *) yet - mixed *f = psyc_parse(":_context\ttest\n\n:_topic\ttesting\n_notice_test_libpsyc\nJust [_topic] libpsyc.\n|\n"); + string s = ":_context\ttest\n\n:_topic\ttesting\n_notice_test_libpsyc\nJust [_topic] libpsyc.\n|\n"; + mixed *f = psyc_parse(s); mixed fr = psyc_render(f); mixed r = ({ ([ "_context": "test" ]), ([ "_topic": "testing" ]), "_notice_test_libpsyc", "Just [_topic] libpsyc." }); mixed rr = psyc_render(r); - debug_message(sprintf("libpsyc returned %O, %O and %O (%s)\n", f, fr, rr, - fr == rr ? "success": "FAIL!")); + debug_message(sprintf("libpsyc returned %O,\n%O\nand\n%O\n(%s)\n", f, fr, rr, + fr == s && rr == s ? "SUCCESS": "FAIL!")); 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"); @@ -20,7 +21,7 @@ (p1[2]||"") + p2[2], (p1[3]||"") + p2[3] }); mixed pr = psyc_render(p); debug_message(sprintf("libpsyc returned %O (%s)\n", p, - pr == rr ? "success": "FAIL!")); + pr == s ? "SUCCESS": "FAIL!")); */ #include "array.h" @@ -44,6 +45,34 @@ # include # include +static inline void +fill_header_from_mapping (svalue_t *key, svalue_t *val, psycHeader *header, psycModifierFlag flag) { + char *name, *value; + size_t namelen, valuelen; + + if (key->type != T_STRING || val->type != T_STRING) + errorf("fill_header_from_mapping: only strings are supported for routing & entity header names & values\n"); + + name = get_txt(key->u.str); + namelen = mstrsize(key->u.str); + + value = get_txt(val->u.str); + valuelen = mstrsize(val->u.str); + + header->modifiers[header->lines++] = psyc_newModifier2(C_GLYPH_OPERATOR_SET, + name, namelen, value, valuelen, flag); +} + +void +fill_routing_header_from_mapping (svalue_t *key, svalue_t *val, void *header) { + fill_header_from_mapping(key, val, header, PSYC_MODIFIER_ROUTING); +} + +void +fill_entity_header_from_mapping (svalue_t *key, svalue_t *val, void *header) { + fill_header_from_mapping(key, val, header, PSYC_MODIFIER_CHECK_LENGTH); +} + /*-------------------------------------------------------------------------*/ // old: string psyc_render(mapping, mapping, string, int* | string); // new: string psyc_render(mixed*); @@ -52,55 +81,66 @@ svalue_t * f_psyc_render(svalue_t *sp) { mp_int i; vector_t *v; - psycPacket packet; string_t *out; char *meth, *body; size_t mlen, blen; mapping_t *map; + psycPacket packet; + psycHeader headers[2]; + // unless (sp->type == T_POINTER) return sp; v = sp->u.vec; - if ((i = (mp_int)VEC_SIZE(v)) != 1+PSYC_BODY) { + if ((i = (mp_int)VEC_SIZE(v)) != 1+PACKET_BODY) { errorf("Wrong number of elements (%"PRIdMPINT") in array argument to psyc_render()\n", i); /* NOTREACHED */ return sp; } - if (v->item[PSYC_ROUTING].type == T_MAPPING) { - map = v->item[PSYC_ROUTING].u.map; - } else { - map = NULL; + 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"); + + //if (i == PACKET_ROUTING) + if (i == 0) + walk_mapping(map, &fill_routing_header_from_mapping, &headers[i]); + else + walk_mapping(map, &fill_entity_header_from_mapping, &headers[i]); + } } #if 0 - if (v->item[PSYC_METHOD].type != T_STRING) { - errorf("Wrong type for PSYC_METHOD element in PSYC packet.\n"); + if (v->item[PACKET_METHOD].type != T_STRING) { + errorf("Wrong type for PACKET_METHOD element in PSYC packet.\n"); /* NOTREACHED */ return sp; } - if (v->item[PSYC_BODY].type != T_STRING) { - errorf("Wrong type for PSYC_BODY element in PSYC packet.\n"); + if (v->item[PACKET_BODY].type != T_STRING) { + errorf("Wrong type for PACKET_BODY element in PSYC packet.\n"); /* NOTREACHED */ return sp; } #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); + 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); } 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); + 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); } else { body = NULL; blen = 0; } #endif // TODO: handle _lists - // FIXME: handle mappings - packet = psyc_newPacket2(NULL, 0, NULL, 0, + packet = psyc_newPacket2(headers[PACKET_ROUTING].modifiers, headers[PACKET_ROUTING].lines, + headers[PACKET_ENTITY].modifiers, headers[PACKET_ENTITY].lines, meth, mlen, body, blen, PSYC_PACKET_CHECK_LENGTH); @@ -174,18 +214,18 @@ f_psyc_parse (svalue_t *sp) { } v = state->packet; - map = allocate_mapping( 0, 1 ); // empty mapping + map = allocate_mapping(0, 1); // empty mapping if (!map) errorf("Out of memory for psyc_parse routing header.\n"); - put_mapping(&v->item[PSYC_ROUTING], map); - map = allocate_mapping( 0, 1 ); // empty mapping + 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[PSYC_ENTITY], map); + put_mapping(&v->item[PACKET_ENTITY], map); do { svalue_t *sv; ret = psyc_parse(state->parser, &oper, &name, &value); - printf("#%2d %c%.*s = %.*s\n", ret, oper ? oper : ' ', name.length, name.ptr, value.length, value.ptr); + printf("#%2d %c%.*s = %.*s\n", ret, oper ? oper : ' ', (int)name.length, name.ptr, (int)value.length, value.ptr); switch (ret) { case PSYC_PARSE_ENTITY_START: case PSYC_PARSE_BODY_START: @@ -229,7 +269,7 @@ f_psyc_parse (svalue_t *sp) { // new_n_tabled fetches a reference of a probably existing // shared string put_string(sv, new_n_tabled(name.ptr, name.length)); - sv = get_map_lvalue(v->item[PSYC_ROUTING].u.map, sv); + sv = get_map_lvalue(v->item[PACKET_ROUTING].u.map, sv); // 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 @@ -248,7 +288,7 @@ f_psyc_parse (svalue_t *sp) { sv = pxalloc(sizeof(svalue_t)); put_string(sv, make_tabled(state->name)); - sv = get_map_lvalue(v->item[PSYC_ENTITY].u.map, sv); + sv = get_map_lvalue(v->item[PACKET_ENTITY].u.map, sv); put_string(sv, state->value); break; @@ -260,7 +300,7 @@ f_psyc_parse (svalue_t *sp) { sv = pxalloc(sizeof(svalue_t)); put_string(sv, new_n_tabled(name.ptr, name.length)); - sv = get_map_lvalue(v->item[PSYC_ENTITY].u.map, sv); + sv = get_map_lvalue(v->item[PACKET_ENTITY].u.map, sv); // is it good to put entity variable values into the // shared string table? probably yes.. but it's a guess @@ -273,18 +313,18 @@ f_psyc_parse (svalue_t *sp) { break; case PSYC_PARSE_BODY_END: - put_string(&v->item[PSYC_METHOD], make_tabled(state->name)); - put_string(&v->item[PSYC_BODY], state->value); + put_string(&v->item[PACKET_METHOD], make_tabled(state->name)); + put_string(&v->item[PACKET_BODY], state->value); break; case PSYC_PARSE_BODY: if (str) errorf("Got two PSYC methods in the same packet!?\n"); // new_n_tabled gets the shared string for the method - put_string(&v->item[PSYC_METHOD], + put_string(&v->item[PACKET_METHOD], new_n_tabled(name.ptr, name.length)); // allocate an untabled string for the packet body - put_string(&v->item[PSYC_BODY], + put_string(&v->item[PACKET_BODY], new_n_mstring(value.ptr, value.length)); break; diff --git a/src/pkg-psyc.h b/src/pkg-psyc.h index b3d65fb..2a7aabf 100644 --- a/src/pkg-psyc.h +++ b/src/pkg-psyc.h @@ -11,10 +11,10 @@ # include "array.h" # include "xalloc.h" -# define PSYC_ROUTING 0 -# define PSYC_ENTITY 1 -# define PSYC_METHOD 2 -# define PSYC_BODY 3 +# define PACKET_ROUTING 0 +# define PACKET_ENTITY 1 +# define PACKET_METHOD 2 +# define PACKET_BODY 3 typedef struct psyc_state_s { psycParseState *parser; diff --git a/src/version.sh b/src/version.sh index 92f29c0..cdb9873 100644 --- a/src/version.sh +++ b/src/version.sh @@ -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="Wed May 11 16:44:23 CEST 2011" +version_stamp="Thu May 12 13:50:02 CEST 2011" # Okay, LDMUD is using 3.x.x so to avoid conflicts let's just use 4.x.x version_major=4