/*--------------------------------------------------------------------*/ void strbuf_store (strbuf_t *buf, svalue_t *svp) /* Store the string collected in <buf>, which may be the null string "", * into the empty svalue *<svp>, then clear <buf>. */ { svp->type = T_STRING; if (buf->buf && buf->length) { svp->u.str = new_n_mstring(buf->buf, buf->length); } else { svp->u.str = ref_mstring(STR_EMPTY); } /* Empty the string buffer */ if (buf->buf) xfree(buf->buf); buf->buf = NULL; buf->length = 0; buf->alloc_len = 0; } /* strbuf_store() */
/*-------------------------------------------------------------------------*/ string_t * trim_all_spaces (const string_t * txt) /* Trim the input string <txt> by removing all leading and trailing * space, and by folding embedded space runs into just one each. * Return the new string with one ref; the refcount of <txt> is not changed. * * Throw an error when out of memory. */ { char * dest; const char * src; size_t dest_ix, src_ix, srclen; string_t * rc; dest = alloca(mstrsize(txt)); if (dest == NULL) errorf("Stack overflow (%zu bytes)\n", mstrsize(txt)); src = get_txt((string_t *const)txt); srclen = mstrsize(txt); src_ix = 0; dest_ix = 0; /* Blank out trailing spaces */ while (srclen > 0 && src[srclen-1] == ' ') srclen--; /* Skip leading spaces */ while (src_ix < srclen && *src == ' ') src_ix++, src++; /* Copy characters, but fold embedded spaces. */ for ( ; src_ix < srclen; src_ix++, src++, dest_ix++) { dest[dest_ix] = *src; /* If this and the next character is a space, forward * src until the last space in this run. */ if (' ' == *src) { while (src_ix+1 < srclen && ' ' == src[1]) src_ix++, src++; } } memsafe(rc = new_n_mstring(dest, dest_ix), dest_ix, "trimmed result"); return rc; } /* trim_all_spaces() */
svalue_t * f_convert_charset (svalue_t *sp) /* EFUN convert_charset() * * string convert_charset(string str, string from_cs, string to_cs) * * Convert the string <str> from charset <from_cs> to charset <to_cs> * and return the converted string. * * The efun is only available on systems with libiconv. */ { iconv_t context; string_t *from_cs, *to_cs, *in_str, *out_str; #if HAS_ICONV_NONCONST_IN # define ICONV_IN_CAST (char**) #else # define ICONV_IN_CAST #endif const char *pIn; /* Input string pointer */ size_t in_len; /* Input length */ size_t in_left; /* Input length left */ char * out_buf; /* Output buffer */ size_t out_size; /* Size of the output buffer */ size_t out_left; /* Size left in output buffer */ char *pOut; /* Output string pointer */ in_str = sp[-2].u.str; from_cs = sp[-1].u.str; to_cs = sp->u.str; pIn = get_txt(in_str); in_len = mstrsize(in_str); in_left = in_len; /* If the input string is empty, we can return immediately * (and in fact must since the allocator will balk at allocating 0 bytes) */ if (!in_len) { sp -= 2; free_string_svalue(sp); free_string_svalue(sp+1); put_string(sp, sp[2].u.str); return sp; } /* Allocate a temporary output string */ out_size = in_len > 65536 ? (in_len + 33) : (2 * in_len); out_left = out_size; xallocate(out_buf, out_size, "iconv buffer"); pOut = out_buf; /* Open the iconv context */ context = iconv_open(get_txt(to_cs), get_txt(from_cs)); if (context == (iconv_t) -1) { xfree(out_buf); if (errno == EINVAL) errorf("convert_charset(): Conversion '%s' -> '%s' not supported.\n" , get_txt(from_cs), get_txt(to_cs) ); else errorf("convert_charset(): Error %d.\n", errno); /* NOTREACHED */ return sp; } /* Convert the string, reallocating the output buffer where necessary */ while (in_left) { size_t rc; rc = iconv(context, ICONV_IN_CAST &pIn, &in_left, &pOut, &out_left); if (rc == (size_t)-1) { if (errno == E2BIG) { /* Reallocate output buffer */ size_t newsize; char * tmp; newsize = out_size + (in_len > 128 ? in_len : 128); tmp = rexalloc(out_buf, newsize); if (!tmp) { iconv_close(context); xfree(out_buf); outofmem(newsize, "iconv buffer"); /* NOTREACHED */ return sp; } out_buf = tmp; pOut = out_buf + out_size; out_left = newsize - out_size; out_size = newsize; continue; } /* Other error: clean up */ iconv_close(context); xfree(out_buf); if (errno == EILSEQ) { errorf("convert_charset(): Invalid character sequence at " "index %td\n", (ptrdiff_t)(pIn - get_txt(in_str))); /* NOTREACHED */ return sp; } if (errno == EINVAL) { errorf("convert_charset(): Incomplete character sequence at " "index %td\n", (ptrdiff_t)(pIn - get_txt(in_str))); /* NOTREACHED */ return sp; } errorf("convert_charset(): Error %d at index %td\n" , errno, (ptrdiff_t)(pIn - get_txt(in_str)) ); /* NOTREACHED */ return sp; } /* if (rc < 0) */ } /* while (in_left) */ /* While the actual conversion is complete, the output stream may now * be in a non-base state. Add the necessary epilogue to get back * to the base state. */ while(1) { size_t rc; rc = iconv(context, NULL, NULL, &pOut, &out_left); if (rc == (size_t)-1) { if (errno == E2BIG) { /* Reallocate output buffer */ size_t newsize; char * tmp; newsize = out_size + (in_len > 128 ? in_len : 128); tmp = rexalloc(out_buf, newsize); if (!tmp) { iconv_close(context); xfree(out_buf); outofmem(newsize, "iconv buffer"); /* NOTREACHED */ return sp; } out_buf = tmp; pOut = out_buf + out_size; out_left = newsize - out_size; out_size = newsize; continue; } /* Other error: clean up */ iconv_close(context); xfree(out_buf); if (errno == EILSEQ) { errorf("convert_charset(): Invalid character sequence at " "index %td\n", (ptrdiff_t)(pIn - get_txt(in_str))); /* NOTREACHED */ return sp; } if (errno == EINVAL) { errorf("convert_charset(): Incomplete character sequence at " "index %td\n", (ptrdiff_t)(pIn - get_txt(in_str))); /* NOTREACHED */ return sp; } errorf("convert_charset(): Error %d at index %td\n" , errno, (ptrdiff_t)(pIn - get_txt(in_str)) ); /* NOTREACHED */ return sp; } /* if (rc < 0) */ /* At this point, the iconv() succeeded: we're done */ break; } /* while(1) */ iconv_close(context); /* Get the return string and prepare the return arguments */ out_str = new_n_mstring(out_buf, out_size - out_left); xfree(out_buf); if (!out_str) { outofmem(out_size - out_left, "convert_charset() result"); /* NOTREACHED */ return sp; } free_string_svalue(sp--); free_string_svalue(sp--); free_string_svalue(sp); put_string(sp, out_str); return sp; } /* f_convert_charset() */
svalue_t * f_psyc_parse (svalue_t *sp) { char *buffer = NULL; svalue_t *sv; vector_t *v, *list; mapping_t *map; char oper = 0; psycString name = {0,0}, value = {0,0}, elems[MAX_LIST_SIZE], elem; psycParseListState listState; int ret, retl, type = -1, error = 0; size_t size, i; ssize_t n; time_t timmy; if (!psyc_dispatch_callback) psyc_dispatch_callback = new_tabled("psyc_dispatch"); if (!psyc_error_callback) psyc_error_callback = new_tabled("psyc_error"); assert_shadow_sent(current_object); psyc_state_t *state = O_GET_PSYC_STATE(current_object); if (!state) { state = pxalloc(sizeof(psyc_state_t)); if (!state) { errorf("Out of memory for psyc state struct.\n"); return sp; // not reached } O_GET_PSYC_STATE(current_object) = state; memset(state, 0, sizeof(psyc_state_t)); state->parser = pxalloc(sizeof(psycParseState)); if (!state->parser) { errorf("Out of memory for psyc parse state struct.\n"); return sp; // not reached } psyc_initParseState(state->parser); } v = state->packet; if (sp->type == T_POINTER) { errorf("\npsyc_parse got %ld int* bytes... not supported yet\n", VEC_SIZE(sp->u.vec)); return sp; // not reached } else if (sp->type == T_STRING) { #ifdef DEBUG printf("\npsyc_parse got a %ld bytes long string...\n", mstrsize(sp->u.str)); #endif if (state->remaining) { // there are remaining bytes from the previous call to psyc_parse, // copy them together with the newly arrived data buffer = pxalloc(state->remaining_len + mstrsize(sp->u.str)); if (!buffer) { errorf("Out of memory for psyc_parse buffer.\n"); return sp; // not reached } memcpy(buffer, state->remaining, state->remaining_len); memcpy(buffer + state->remaining_len, get_txt(sp->u.str), mstrsize(sp->u.str)); psyc_setParseBuffer2(state->parser, buffer, state->remaining_len + mstrsize(sp->u.str)); pfree(state->remaining); state->remaining = NULL; state->remaining_len = 0; } else { psyc_setParseBuffer2(state->parser, get_txt(sp->u.str), mstrsize(sp->u.str)); } } else { errorf("\npsyc_parse got type %d, not supported\n", sp->type); return sp; // not reached } do { ret = psyc_parse(state->parser, &oper, &name, &value); #ifdef DEBUG printf("#%2d %c%.*s = %.*s\n", ret, oper ? oper : ' ', (int)name.length, name.ptr, (int)value.length, value.ptr); #endif if (!state->packet) { state->packet = allocate_array(4); if (!state->packet) { errorf("Out of memory for psyc_parse array.\n"); return sp; // not reached } v = state->packet; map = allocate_mapping(0, 2); // empty mapping if (!map) { errorf("Out of memory for psyc_parse routing header.\n"); return sp; // not reached } put_mapping(&v->item[PACKET_ROUTING], map); map = allocate_mapping(0, 2); // empty mapping if (!map) { errorf("Out of memory for psyc_parse entity header.\n"); return sp; // not reached } put_mapping(&v->item[PACKET_ENTITY], map); } switch (ret) { case PSYC_PARSE_ENTITY_START: case PSYC_PARSE_BODY_START: // save oper, name & value in state at the start of // incomplete entity or body state->oper = oper; state->name = mstring_alloc_string(name.length); memcpy(get_txt(state->name), name.ptr, name.length); if (!state->name) { errorf("Out of memory for name.\n"); return sp; // not reached } // allocate memory for the total length of the value state->value_len = 0; state->value = mstring_alloc_string(psyc_getParseValueLength(state->parser)); if (!state->value) { errorf("Out of memory for value.\n"); return sp; // not reached } // fall thru case PSYC_PARSE_ENTITY_CONT: case PSYC_PARSE_BODY_CONT: case PSYC_PARSE_ENTITY_END: case PSYC_PARSE_BODY_END: // append value to tmp buffer in state memcpy(get_txt(state->value) + state->value_len, value.ptr, value.length); state->value_len += value.length; } if (ret == PSYC_PARSE_ENTITY_END || ret == PSYC_PARSE_BODY_END) { // incomplete entity or body parsing done, // set oper/name/value to the ones saved in state oper = state->oper; name.ptr = get_txt(state->name); name.length = mstrsize(state->name); value.ptr = get_txt(state->value); value.length = mstrsize(state->value); } switch (ret) { case PSYC_PARSE_ROUTING: sv = pxalloc(sizeof(svalue_t)); // 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[PACKET_ROUTING].u.map, sv); put_number(&sv[1], oper); // 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)); break; case PSYC_PARSE_ENTITY_START: case PSYC_PARSE_ENTITY_CONT: break; case PSYC_PARSE_ENTITY_END: case PSYC_PARSE_ENTITY: sv = pxalloc(sizeof(svalue_t)); if (ret == PSYC_PARSE_ENTITY) put_string(sv, new_n_tabled(name.ptr, name.length)); else // PSYC_PARSE_ENTITY_END put_string(sv, make_tabled(state->name)); sv = get_map_lvalue(v->item[PACKET_ENTITY].u.map, sv); put_number(&sv[1], oper); type = psyc_getVarType(&name); switch (type) { case PSYC_TYPE_DATE: // number + PSYC_EPOCH if (psyc_parseDate(&value, &timmy)) put_number(sv, timmy); else error = PSYC_PARSE_ERROR_DATE; break; case PSYC_TYPE_TIME: // number if (psyc_parseTime(&value, &timmy)) put_number(sv, timmy); else error = PSYC_PARSE_ERROR_TIME; break; case PSYC_TYPE_AMOUNT: // number if (psyc_parseNumber(&value, &n)) put_number(sv, n); else error = PSYC_PARSE_ERROR_AMOUNT; break; case PSYC_TYPE_DEGREE: // first digit if (value.length && value.ptr[0] >= '0' && value.ptr[0] <= '9') put_number(sv, value.ptr[0] - '0'); else error = PSYC_PARSE_ERROR_DEGREE; break; case PSYC_TYPE_FLAG: // 0 or 1 if (value.length && value.ptr[0] >= '0' && value.ptr[0] <= '1') put_number(sv, value.ptr[0] - '0'); else error = PSYC_PARSE_ERROR_FLAG; break; case PSYC_TYPE_LIST: // array size = 0; if (value.length) { psyc_initParseListState(&listState); psyc_setParseListBuffer(&listState, value); elem = (psycString){0, 0}; do { retl = psyc_parseList(&listState, &elem); switch (retl) { case PSYC_PARSE_LIST_END: retl = 0; case PSYC_PARSE_LIST_ELEM: if (size >= MAX_LIST_SIZE) { error = PSYC_PARSE_ERROR_LIST_TOO_LARGE; break; } elems[size++] = elem; break; default: error = PSYC_PARSE_ERROR_LIST; } } while (retl > 0 && !error); } if (error) break; list = allocate_array(size); for (i = 0; i < size; i++) put_string(&list->item[i], new_n_tabled(elems[i].ptr, elems[i].length)); put_array(sv, list); break; default: // string if (ret == PSYC_PARSE_ENTITY) // 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)); else // PSYC_PARSE_ENTITY_END put_string(sv, state->value); } break; case PSYC_PARSE_BODY_START: case PSYC_PARSE_BODY_CONT: break; case PSYC_PARSE_BODY_END: put_string(&v->item[PACKET_METHOD], make_tabled(state->name)); put_string(&v->item[PACKET_BODY], state->value); break; case PSYC_PARSE_BODY: // new_n_tabled gets the shared string for the 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[PACKET_BODY], new_n_mstring(value.ptr, value.length)); break; case PSYC_PARSE_COMPLETE: put_array(inter_sp, v); sapply(psyc_dispatch_callback, current_object, 1); state->packet = NULL; break; case PSYC_PARSE_INSUFFICIENT: // insufficient data, save remaining bytes state->remaining_len = psyc_getParseRemainingLength(state->parser); if (state->remaining_len) { state->remaining = pxalloc(state->remaining_len); memcpy(state->remaining, psyc_getParseRemainingBuffer(state->parser), state->remaining_len); } else state->remaining = NULL; ret = 0; break; default: error = ret; } switch (ret) { case PSYC_PARSE_BODY_END: case PSYC_PARSE_ENTITY_END: // reset tmp buffers in state when incomplete // entity or body parsing is finished state->oper = 0; state->name = NULL; state->value = NULL; } } while (ret && !error); if (buffer) pfree(buffer); free_svalue(sp); put_number(sp, error); return sp; } /* f_psyc_parse */