/*------------------------------------------------------------------ * Glue for libpsyc. *------------------------------------------------------------------ * 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(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" #ifdef USE_PSYC # include # include # include # include # include # include /*-------------------------------------------------------------------------*/ // old: string psyc_render(mapping, mapping, string, int* | string); // new: string psyc_render(mixed*); svalue_t * f_psyc_render(svalue_t *sp) { mp_int i; vector_t *v; psycPacket packet; string_t *out; // 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); return sp; } /* f_psyc_render */ /*-------------------------------------------------------------------------*/ // mixed psyc_parse(int* | string); svalue_t * f_psyc_parse (svalue_t *sp) { string_t *str = NULL; vector_t *v; mapping_t *map; svalue_t *sv; mp_int i; psycString name, value, elem; psycParseState state; psycParseListState listState; char oper; int ret; psyc_initParseState(&state); i = 0; if (sp->type == T_POINTER) { printf("\npsyc_parse got %ld int* bytes... not supported yet\n", i); } else if (sp->type == T_STRING) { printf("\npsyc_parse got a %d bytes long string...\n", mstrsize(sp->u.str)); psyc_nextParseBuffer(&state, psyc_newString(get_txt(sp->u.str), mstrsize(sp->u.str))); } 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"); 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[PSYC_ENTITY], map); do { ret = psyc_parse(&state, &oper, &name, &value); switch (ret) { case PSYC_PARSE_ROUTING: if (oper != ':') { puts("_failure_unsupported_state"); continue; } // 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[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: if (oper != ':') { puts("_failure_unsupported_state"); continue; } // 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[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) { write(1, ">>> LIST START\n", 15); psyc_initParseListState(&listState); psyc_nextParseListBuffer(&listState, value); while ((ret = psyc_parseList(&listState, &name, &value, &elem))) { 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; } } } break; case PSYC_PARSE_BODY: if (str) errorf("Got two PSYC methods in the same packet!?\n"); // 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[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[PSYC_BODY], str); 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); put_array(sp, v); return sp; } /* f_psyc_parse */ #endif /* USE_PSYC */