1
0
Fork 0
mirror of git://git.psyc.eu/libpsyc synced 2024-08-15 03:19:02 +00:00
libpsyc/pike/psyc.cmod

488 lines
15 KiB
Text
Raw Normal View History

2011-06-12 10:52:27 +00:00
#include "global.h"
#include "interpret.h"
#include "stralloc.h"
#include "mapping.h"
2011-06-15 19:36:43 +00:00
#include "array.h"
2011-06-12 10:52:27 +00:00
#include "svalue.h"
#include "operators.h"
#include "array.h"
#include "builtin_functions.h"
#include "module.h"
#include <stdlib.h>
#ifndef DEFAULT_CMOD_STORAGE
#define DEFAULT_CMOD_STORAGE
#endif
// libpsyc dependencies
2011-06-15 19:36:43 +00:00
#include <psyc.h>
#include <psyc/parse.h>
#include <psyc/render.h>
#include <psyc/text.h>
2011-06-12 10:52:27 +00:00
#define MODULE_NAME "PSYC"
#define MODULE_MAJOR 0
2011-06-15 19:36:43 +00:00
#define MODULE_MINOR 2
2011-06-12 10:52:27 +00:00
#define MODULE_PATCH 0
/*! @module PSYC
*!
*! Implements PSYC parsing and rendering based on libpsyc
*!
*/
2011-06-15 19:36:43 +00:00
// psyctext helper
2011-10-31 19:26:47 +00:00
PsycTextValueRC lookup_value_mapping(const char *name, size_t len, PsycString *value, void *extra)
2011-06-15 19:36:43 +00:00
{
2011-06-16 06:18:27 +00:00
//printf("lookup_value_mapping called for %.*s\n", (int) len, name);
2011-06-15 19:36:43 +00:00
struct pike_string *key = make_shared_binary_string(name, len);
struct mapping *m = (struct mapping *) extra;
struct svalue *s = low_mapping_string_lookup(m, key);
free_string(key);
if (s == NULL) {
return PSYC_TEXT_VALUE_NOT_FOUND;
}
switch(s->type) {
case PIKE_T_STRING:
//printf("lookup returned %.*s\n", (int) s->u.string->len, STR0(s->u.string));
2011-11-01 11:06:58 +00:00
value->data = (char *) STR0(s->u.string);
2011-06-15 19:36:43 +00:00
value->length = s->u.string->len;
break;
default:
printf("lookup did return !string\n");
2011-06-16 06:18:27 +00:00
// FIXME: we need the automagic value conversion
2011-11-01 11:06:58 +00:00
value->data = "";
2011-06-15 19:36:43 +00:00
value->length = 0;
}
return PSYC_TEXT_VALUE_FOUND;
}
/*! @decl string psyc_text(string template, mapping vars)
*! @param template
*! template to be filled
*! @param vars
*! mapping(string:string) with substituions
*! @returns
*! rendered string
*/
PIKEFUN string psyc_text(string template, mapping vars) {
2011-10-31 19:26:47 +00:00
PsycTextState state;
PsycTextRC ret;
2011-06-15 19:36:43 +00:00
size_t len = 0;
// FIXME:
char buffer[512];
2011-10-31 19:04:16 +00:00
psyc_text_state_init(&state, (char *) STR0(template), template->len, buffer, 512);
2011-06-15 19:36:43 +00:00
do
{
ret = psyc_text(&state, lookup_value_mapping, vars);
2011-10-31 19:04:16 +00:00
len += psyc_text_bytes_written(&state);
2011-06-15 19:36:43 +00:00
switch (ret) {
case PSYC_TEXT_INCOMPLETE: // need to realloc buffer
2011-10-31 19:04:16 +00:00
//psyc_text_buffer_set(&state, buffer + len, BUFSIZE - len);
2011-06-15 19:36:43 +00:00
break;
case PSYC_TEXT_COMPLETE: // we're done
RETURN make_shared_binary_string(buffer, len);
case PSYC_TEXT_NO_SUBST: // no substituions, return original string
RETURN template;
}
} while (ret == PSYC_TEXT_INCOMPLETE);
}
2011-07-17 08:48:01 +00:00
/*! @decl string is_routingvar(string name)
*! @param name
*! name of the variable
*! @returns
*! 1 if it is a routing variable
*! 0 otherwise
*/
PIKEFUN int is_routingvar(string name) {
2011-10-31 19:04:16 +00:00
RETURN psyc_var_is_routing((char *) STR0(name), name->len);
2011-07-17 08:48:01 +00:00
}
2011-06-12 10:52:27 +00:00
/*! @decl string render(mapping rvars, mapping evars, string method, string|void body)
*! @param rvars
2011-06-15 19:36:43 +00:00
*! routing vars (mapping string:string)
2011-06-12 10:52:27 +00:00
*! @param evars
2011-06-15 19:36:43 +00:00
*! entity vars (mapping string:mixed
2011-06-12 10:52:27 +00:00
*! @param method
2011-06-15 19:36:43 +00:00
*! method name
2011-06-12 10:52:27 +00:00
*! @param data
2011-06-15 19:36:43 +00:00
*! body
2011-06-12 10:52:27 +00:00
*! @returns
*! serialized packet as a string
*/
PIKEFUN string render(mapping rvars, mapping evars, string method, string|void body) {
2011-10-31 19:26:47 +00:00
PsycPacket packet;
PsycHeader rheaders, eheaders;
2011-06-12 10:52:27 +00:00
struct keypair *k; // for mappings
INT32 e;
char oper = C_GLYPH_OPERATOR_SET;
// fill headers
rheaders.lines = 0;
2011-10-31 19:26:47 +00:00
rheaders.modifiers = malloc(sizeof(PsycModifier) * rvars->data->size);
2011-06-12 10:52:27 +00:00
NEW_MAPPING_LOOP(rvars->data) {
if (k->ind.type == PIKE_T_STRING) {
switch(k->val.type) {
case PIKE_T_STRING:
2011-10-31 19:04:16 +00:00
rheaders.modifiers[rheaders.lines++] = psyc_modifier_new(oper,
2011-06-12 10:52:27 +00:00
(char *)STR0(k->ind.u.string),
k->ind.u.string->len,
(char *)STR0(k->val.u.string),
2011-10-31 19:04:16 +00:00
k->val.u.string->len,
2011-06-12 10:52:27 +00:00
PSYC_MODIFIER_ROUTING);
break;
default:
2011-06-15 19:36:43 +00:00
Pike_error("psyc render: unsupported non-string value in rvars\n");
2011-06-12 10:52:27 +00:00
break;
}
} else {
Pike_error("psyc render: unsupported non-string key in rvars\n");
}
}
eheaders.lines = 0;
2011-10-31 19:26:47 +00:00
eheaders.modifiers = malloc(sizeof(PsycModifier) * evars->data->size);
2011-06-12 10:52:27 +00:00
NEW_MAPPING_LOOP(evars->data) {
if (k->ind.type == PIKE_T_STRING) {
char *key;
size_t keylen;
char *val = NULL;
size_t vallen = 0;
2011-06-15 19:36:43 +00:00
struct pike_string *s;
2011-06-12 10:52:27 +00:00
key = (char *) STR0(k->ind.u.string);
keylen = k->ind.u.string->len;
switch(k->val.type) {
case PIKE_T_INT:
2011-06-15 19:36:43 +00:00
do {
struct string_builder b;
init_string_builder(&b, 0);
string_builder_append_integer(&b, k->val.u.integer,
10, APPEND_SIGNED, 0, 0);
s = finish_string_builder(&b);
val = (char *) STR0(s);
vallen = s->len;
free_string(s);
} while (0);
2011-06-12 10:52:27 +00:00
break;
case PIKE_T_STRING:
2011-06-15 19:36:43 +00:00
val = (char *) STR0(k->val.u.string);
vallen = k->val.u.string->len;
2011-06-12 10:52:27 +00:00
break;
2011-06-15 19:36:43 +00:00
/*
2011-06-12 10:52:27 +00:00
case PIKE_T_FLOAT:
printf("float value %f\n", k->val.u.float_number);
break;
*/
2011-06-15 19:36:43 +00:00
case PIKE_T_ARRAY:
do {
2011-10-31 19:26:47 +00:00
PsycString *elems = xcalloc(k->val.u.array->size, sizeof(PsycString));
PsycList list;
2011-06-15 19:36:43 +00:00
// FIXME: check for out of memory
for(e = 0; e < k->val.u.array->size; e++) {
struct svalue item = k->val.u.array->item[e];
switch(item.type) {
case PIKE_T_STRING:
2011-10-31 19:26:47 +00:00
elems[e] = (PsycString) { item.u.string->len, (char *) STR0(item.u.string) };
2011-06-15 19:36:43 +00:00
break;
default:
// FIXME: xfree(elems) ?
Pike_error("psyc_render: unsupported data type in list\n");
}
}
2011-10-31 19:04:16 +00:00
list = psyc_list_new(elems, k->val.u.array->size, PSYC_LIST_CHECK_LENGTH);
2011-06-15 19:36:43 +00:00
struct pike_string *listbuf = begin_shared_string(list.length);
2011-10-31 19:04:16 +00:00
psyc_render_list(&list, (char *) STR0(listbuf), listbuf->len);
2011-06-15 19:36:43 +00:00
end_shared_string(listbuf);
val = (char *) STR0(listbuf);
vallen = listbuf->len;
xfree(elems);
} while (0);
2011-06-12 10:52:27 +00:00
break;
default:
2011-06-15 19:36:43 +00:00
Pike_error("psyc_render: unsupported value in evars\n");
2011-06-12 10:52:27 +00:00
break;
}
2011-10-31 19:04:16 +00:00
eheaders.modifiers[eheaders.lines++] = psyc_modifier_new(oper,
2011-06-12 10:52:27 +00:00
key, keylen,
val, vallen,
PSYC_MODIFIER_CHECK_LENGTH);
} else {
Pike_error("psyc render: unsupported non-string key in evars\n");
}
}
if (body != NULL) {
2011-10-31 19:04:16 +00:00
packet = psyc_packet_new(rheaders.modifiers,
2011-06-12 10:52:27 +00:00
rheaders.lines,
eheaders.modifiers,
eheaders.lines,
2011-10-31 19:04:16 +00:00
(const char *)STR0(method), method->len,
2011-06-12 10:52:27 +00:00
(const char *)STR0(body), body->len,
PSYC_PACKET_CHECK_LENGTH);
} else { // body arg was not given
2011-10-31 19:04:16 +00:00
packet = psyc_packet_new(rheaders.modifiers,
2011-06-12 10:52:27 +00:00
rheaders.lines,
eheaders.modifiers,
eheaders.lines,
2011-10-31 19:04:16 +00:00
(const char *)STR0(method), method->len,
2011-06-12 10:52:27 +00:00
NULL, 0,
PSYC_PACKET_CHECK_LENGTH);
}
2011-06-15 19:36:43 +00:00
struct pike_string *s = begin_shared_string(packet.length);
psyc_render(&packet, (char *) STR0(s), packet.length);
2011-06-12 10:52:27 +00:00
// pop_n_elems(args);
2011-06-15 19:36:43 +00:00
RETURN end_shared_string(s);
2011-06-12 10:52:27 +00:00
}
PIKECLASS Parser {
2011-10-31 19:26:47 +00:00
CVAR PsycParseState parser;
2011-06-15 19:36:43 +00:00
CVAR struct pike_string *buffer;
CVAR int handle_packet;
CVAR int handle_error;
// packet state
CVAR struct mapping *rvars;
CVAR struct mapping *evars;
CVAR struct pike_string *method;
CVAR struct pike_string *body;
// for incomplete length prefixed entity var data / body data
CVAR struct string_builder incomplete;
2011-06-12 10:52:27 +00:00
INIT {
2011-10-31 19:04:16 +00:00
psyc_parse_state_init(&THIS->parser, PSYC_PARSE_ALL);
2011-06-15 19:36:43 +00:00
THIS->buffer = NULL;
THIS->handle_packet = find_identifier("handle_packet", Pike_fp->current_object->prog);
THIS->handle_error = find_identifier("handle_error", Pike_fp->current_object->prog);
THIS->rvars = allocate_mapping(0);
THIS->evars = allocate_mapping(0);
THIS->method = NULL;
THIS->body = NULL;
//THIS->body_buffer = NULL;
2011-06-12 10:52:27 +00:00
}
EXIT {
2011-06-15 19:36:43 +00:00
if (THIS->buffer != NULL) {
free_string(THIS->buffer);
}
if (THIS->rvars != NULL) {
do_free_mapping(THIS->rvars);
}
// FIXME: free packet state
2011-06-12 10:52:27 +00:00
}
PIKEFUN void feed(string data) {
char oper;
2011-10-31 19:26:47 +00:00
PsycString name, value;
2011-06-12 10:52:27 +00:00
int ret;
2011-06-15 19:36:43 +00:00
int err;
2011-06-12 10:52:27 +00:00
2011-06-15 19:36:43 +00:00
if (THIS->buffer != NULL) {
/* we have remaining buffer from previous input */
//printf("%d bytes remaining from previous read\n", THIS->buffer->len);
struct pike_string *tmp;
tmp = add_shared_strings(THIS->buffer, data);
free_string(THIS->buffer);
data = tmp;
THIS->buffer = NULL;
}
2011-10-31 19:04:16 +00:00
psyc_parse_buffer_set(&THIS->parser,
2011-06-12 10:52:27 +00:00
(char *) STR0(data), data->len);
2011-06-15 19:36:43 +00:00
do {
2011-06-12 10:52:27 +00:00
ret = psyc_parse(&THIS->parser, &oper, &name, &value);
2011-06-15 19:36:43 +00:00
switch(ret) {
case PSYC_PARSE_ROUTING:
2011-11-01 11:06:58 +00:00
// printf("R %.*s -> %.*s\n", (int)name.length, name.data, (int)value.length, value.data);
2011-06-15 19:36:43 +00:00
mapping_string_insert_string(THIS->rvars,
2011-11-01 11:06:58 +00:00
make_shared_binary_string(name.data, name.length),
make_shared_binary_string(value.data, value.length));
2011-06-12 10:52:27 +00:00
break;
2011-06-15 19:36:43 +00:00
case PSYC_PARSE_ENTITY_START: // entity var with length
2011-10-31 19:04:16 +00:00
init_string_builder_alloc(&THIS->incomplete, psyc_parse_value_length(&THIS->parser), 0);
2011-06-15 19:36:43 +00:00
// fall thru
case PSYC_PARSE_ENTITY_CONT:
2011-11-01 11:06:58 +00:00
string_builder_append(&THIS->incomplete, MKPCHARP(value.data, 0), value.length);
2011-06-15 19:36:43 +00:00
break;
case PSYC_PARSE_ENTITY_END:
2011-11-01 11:06:58 +00:00
string_builder_append(&THIS->incomplete, MKPCHARP(value.data, 0), value.length);
2011-06-15 19:36:43 +00:00
do {
struct pike_string *tmp = finish_string_builder(&THIS->incomplete);
value.length = tmp->len;
2011-11-01 11:06:58 +00:00
value.data = (char *) STR0(tmp);
2011-06-15 19:36:43 +00:00
// FIXME: not sure if this is really safe
free_string(tmp);
} while (0);
// fall thru
case PSYC_PARSE_ENTITY:
2011-11-01 11:06:58 +00:00
//printf("E %.*s -> %.*s\n", (int)name.length, name.data, (int)value.length, value.data);
2011-06-15 19:36:43 +00:00
do {
err = 0;
2011-10-31 19:04:16 +00:00
int type = psyc_var_type(PSYC_S2ARG(&name));
2011-06-15 19:36:43 +00:00
struct svalue sv;
time_t timmy;
switch(type) {
case PSYC_TYPE_DATE:
2011-10-31 19:04:16 +00:00
if (psyc_parse_date(&value, &timmy)) {
2011-06-15 19:36:43 +00:00
sv.type = PIKE_T_INT; sv.u.integer = timmy;
mapping_string_insert(THIS->evars,
2011-11-01 11:06:58 +00:00
make_shared_binary_string(name.data, name.length),
2011-06-15 19:36:43 +00:00
&sv);
} else {
err = 1;
}
break;
case PSYC_TYPE_TIME:
2011-10-31 19:04:16 +00:00
if (psyc_parse_time(&value, &timmy)) {
2011-06-15 19:36:43 +00:00
sv.type = PIKE_T_INT; sv.u.integer = timmy;
mapping_string_insert(THIS->evars,
2011-11-01 11:06:58 +00:00
make_shared_binary_string(name.data, name.length),
2011-06-15 19:36:43 +00:00
&sv);
} else {
err = 2;
}
break;
case PSYC_TYPE_AMOUNT:
break;
case PSYC_TYPE_DEGREE:
2011-11-01 11:06:58 +00:00
if (value.length && value.data[0] >= '0' && value.data[0] <= '9') {
sv.type = PIKE_T_FLOAT; sv.u.float_number = (float) (value.data[0] - '0') / 10.0;
2011-06-15 19:36:43 +00:00
mapping_string_insert(THIS->evars,
2011-11-01 11:06:58 +00:00
make_shared_binary_string(name.data, name.length),
2011-06-15 19:36:43 +00:00
&sv);
} else {
err = 3;
}
break;
case PSYC_TYPE_FLAG:
2011-11-01 11:06:58 +00:00
if (value.length && value.data[0] >= '0' && value.data[0] <= '1') {
sv.type = PIKE_T_INT; sv.u.integer = value.data[0] - '0';
2011-06-15 19:36:43 +00:00
mapping_string_insert(THIS->evars,
2011-11-01 11:06:58 +00:00
make_shared_binary_string(name.data, name.length),
2011-06-15 19:36:43 +00:00
&sv);
} else {
err = 4;
}
break;
case PSYC_TYPE_LIST:
do {
struct array *elems = low_allocate_array(0, 32);
if (value.length > 0) {
int retl;
int count = 0;
2011-10-31 19:26:47 +00:00
PsycParseListState listState;
PsycString elem = (PsycString) {0, 0};
2011-06-15 19:36:43 +00:00
2011-10-31 19:04:16 +00:00
psyc_parse_list_state_init(&listState);
psyc_parse_list_buffer_set(&listState, PSYC_S2ARG(value));
2011-06-15 19:36:43 +00:00
do {
2011-10-31 19:04:16 +00:00
retl = psyc_parse_list(&listState, &elem);
2011-06-15 19:36:43 +00:00
switch(retl) {
case PSYC_PARSE_LIST_END: // last element
retl = 0;
case PSYC_PARSE_LIST_ELEM:
2011-11-01 11:06:58 +00:00
sv.type = PIKE_T_STRING; sv.u.string = make_shared_binary_string(elem.data, elem.length);
2011-06-15 19:36:43 +00:00
elems = array_insert(elems, &sv, count++);
break;
default:
err = 5;
break;
}
} while (retl > 0 && !err);
}
if (!err) {
sv.type = PIKE_T_ARRAY;
sv.u.array = elems;
mapping_string_insert(THIS->evars,
2011-11-01 11:06:58 +00:00
make_shared_binary_string(name.data, name.length),
2011-06-15 19:36:43 +00:00
&sv);
}
free_array(elems);
} while (0);
break;
default: // string
mapping_string_insert_string(THIS->evars,
2011-11-01 11:06:58 +00:00
make_shared_binary_string(name.data, name.length),
make_shared_binary_string(value.data, value.length));
2011-06-15 19:36:43 +00:00
}
} while (0);
if (err) { // there was an error while
// FIXME
return;
}
break;
case PSYC_PARSE_BODY_START: // if length was given this is used for body
2011-10-31 19:04:16 +00:00
init_string_builder_alloc(&THIS->incomplete, psyc_parse_value_length(&THIS->parser), 0);
2011-06-15 19:36:43 +00:00
case PSYC_PARSE_BODY_CONT:
2011-11-01 11:06:58 +00:00
string_builder_append(&THIS->incomplete, MKPCHARP(value.data, 0), value.length);
2011-06-15 19:36:43 +00:00
break;
case PSYC_PARSE_BODY_END:
2011-11-01 11:06:58 +00:00
string_builder_append(&THIS->incomplete, MKPCHARP(value.data, 0), value.length);
2011-06-15 19:36:43 +00:00
do {
struct pike_string *tmp = finish_string_builder(&THIS->incomplete);
value.length = tmp->len;
2011-11-01 11:06:58 +00:00
value.data = (char *) STR0(tmp);
2011-06-15 19:36:43 +00:00
// FIXME: not sure if this is really safe
free_string(tmp);
} while (0);
// fall thru
case PSYC_PARSE_BODY:
2011-11-01 11:06:58 +00:00
THIS->method = make_shared_binary_string(name.data, name.length);
THIS->body = make_shared_binary_string(value.data, value.length);
2011-06-15 19:36:43 +00:00
break;
case PSYC_PARSE_COMPLETE: // apply the callback
push_mapping(THIS->rvars);
push_mapping(THIS->evars);
if (THIS->method == NULL) {
apply_low(Pike_fp->current_object, THIS->handle_packet, 2);
} else if (THIS->body == NULL) {
push_string(THIS->method);
apply_low(Pike_fp->current_object, THIS->handle_packet, 3);
} else {
push_string(THIS->method);
push_string(THIS->body);
apply_low(Pike_fp->current_object, THIS->handle_packet, 4);
}
// reset packet state
THIS->rvars = allocate_mapping(0);
THIS->evars = allocate_mapping(0);
THIS->method = NULL;
THIS->body = NULL;
break;
case PSYC_PARSE_INSUFFICIENT: // not enough data
2011-10-31 19:04:16 +00:00
if (psyc_parse_remaining_buffer(&THIS->parser) > 0) {
THIS->buffer = make_shared_binary_string(psyc_parse_remaining_buffer(&THIS->parser),
psyc_parse_remaining_length(&THIS->parser));
2011-06-15 19:36:43 +00:00
}
return;
default: // fatal error
push_int(ret);
apply_low(Pike_fp->current_object, THIS->handle_error, 1);
// FIXME: free stuff? or do we kill the socket and parser anyway
return;
2011-06-12 10:52:27 +00:00
}
2011-06-15 19:36:43 +00:00
} while (1);
2011-06-12 10:52:27 +00:00
}
}
INIT {
}
EXTRA {
add_integer_constant("__version_major", MODULE_MAJOR, 0);
add_integer_constant("__version_minor", MODULE_MAJOR, 0);
add_integer_constant("__version_patch", MODULE_MAJOR, 0);
}