static int is_printable_list(const ETERM* term) { while (ERL_TYPE(term) == ERL_LIST) { ETERM* head = HEAD(term); if (!ERL_IS_BYTE(head)) { return 0; } if (ERL_INT_VALUE(head) < ' ') { switch (ERL_INT_VALUE(head)) { case '\n': case '\r': case '\t': case '\v': case '\b': case '\f': break; default: return 0; } } term = TAIL(term); } return ERL_IS_EMPTY_LIST(term); }
/* * Return the CONTENT of a VARIABLE with NAME. * If the content is non-nil then bump its * reference counter. */ ETERM *erl_var_content (const ETERM *ep, const char *name) { int i; ETERM *vp; if ((!ep) || (!name)) return NULL; /* ASSERT(ep != NULL); */ switch(ERL_TYPE(ep)) { case ERL_VARIABLE: if (strcmp(ERL_VAR_NAME(ep), name) == 0) { if ((vp = ERL_VAR_VALUE(ep)) != NULL) { ERL_COUNT(vp)++; return vp; } } break; case ERL_LIST: while (ep && (ERL_TYPE(ep) != ERL_EMPTY_LIST)) { if ((vp = erl_var_content(HEAD(ep), name))) return vp; ep = TAIL(ep); } break; case ERL_TUPLE: for (i=0; i < ERL_TUPLE_SIZE(ep); i++) if ((vp = erl_var_content(ERL_TUPLE_ELEMENT(ep, i), name))) { return vp; } break; default: /* variables can't occur in other types */ break; } /* nothing found ! */ return NULL; }
/* * Extract the HEAD of a LIST. Bump the reference * counter on the head object. */ ETERM *erl_hd (const ETERM *ep) { if (!ep) return NULL; /* ASSERT(ep != NULL); */ if (ERL_TYPE(ep) != ERL_LIST) { return (ETERM *) NULL; } ERL_COUNT(ERL_CONS_HEAD(ep))++; return ERL_CONS_HEAD(ep); }
/* * Extract the TAIL of a LIST. Bump the reference * counter on the tail object. */ ETERM *erl_tl (const ETERM *ep) { ETERM *tl; if (!ep) return NULL; /* ASSERT(ep != NULL); */ if (ERL_TYPE(ep) != ERL_LIST) { return (ETERM *) NULL; } tl = TAIL(ep); ERL_COUNT(tl)++; return tl; }
/* * Extract an ELEMENT from a TUPLE. Bump the * reference counter on the extracted object. */ ETERM *erl_element (int ix, const ETERM *ep) { if ((!ep) || (ix < 0)) return NULL; /* * ASSERT(ep != NULL); * ASSERT(ix >= 0); */ if ((ERL_TYPE(ep) == ERL_TUPLE) && (ix <= ERL_TUPLE_SIZE(ep))) { ERL_COUNT(ERL_TUPLE_ELEMENT(ep, ix-1))++; return ERL_TUPLE_ELEMENT(ep, ix-1); } else return NULL; } /* erl_element */
/* * Return the LENGTH of a LIST. * At failure -1 is returned (this include non-proper lists like [a|b]). */ int erl_length(const ETERM *ep) { int n = 0; if (!ep) return -1; /* ASSERT(ep != NULL); */ while (ERL_TYPE(ep) == ERL_LIST) { n++; ep = TAIL(ep); } if (!ERL_IS_EMPTY_LIST(ep)) return -1; return n; }
int erl_setelement (int ix, ETERM *ep, ETERM *vp) { if ((!ep) || (!vp)) return 0; /* ASSERT(ep != NULL); * ASSERT(vp != NULL); */ if ((ERL_TYPE(ep) == ERL_TUPLE) && (ix <= ERL_TUPLE_SIZE(ep))) { erl_free_term(ERL_TUPLE_ELEMENT(ep, ix-1)); ERL_TUPLE_ELEMENT(ep, ix-1) = vp; ERL_COUNT(vp)++; return 1; } erl_err_msg("<ERROR> erl_setelement: Bad type to setelement or out of range \n"); return 0; }
/* * Return the SIZE of a TUPLE or a BINARY. * At failure -1 is returned. */ int erl_size (const ETERM *ep) { if (!ep) return -1; /* ASSERT(ep != NULL); */ switch (ERL_TYPE(ep)) { case ERL_TUPLE: return ERL_TUPLE_SIZE(ep); case ERL_BINARY: return ERL_BIN_SIZE(ep); default: return -1; } }
int erl_print_term(FILE *fp, const ETERM *ep) { int j,i,doquote; int ch_written = 0; /* counter of written chars */ if ((!fp) || (!ep)) return 0; /* ASSERT(ep != NULL); */ j = i = doquote = 0; switch(ERL_TYPE(ep)) { case ERL_ATOM: { char* adata = ERL_ATOM_PTR(ep); /* FIXME: what if some weird locale is in use? */ if (!islower(adata[0])) doquote = 1; for (i = 0; !doquote && i < ERL_ATOM_SIZE(ep); i++) { doquote = !(isalnum(adata[i]) || (adata[i] == '_')); } if (doquote) { putc('\'', fp); ch_written++; } fputs(adata, fp); ch_written += ERL_ATOM_SIZE(ep); if (doquote) { putc('\'', fp); ch_written++; } break; } case ERL_VARIABLE: if (!isupper((int)ERL_VAR_NAME(ep)[0])) { doquote = 1; putc('\'', fp); ch_written++; } fputs(ERL_VAR_NAME(ep), fp); ch_written += ERL_VAR_LEN(ep); if (doquote) { putc('\'', fp); ch_written++; } break; case ERL_PID: ch_written += fprintf(fp, "<%s.%d.%d>", ERL_PID_NODE(ep), ERL_PID_NUMBER(ep), ERL_PID_SERIAL(ep)); break; case ERL_PORT: ch_written += fprintf(fp, "#Port"); break; case ERL_REF: ch_written += fprintf(fp, "#Ref"); break; case ERL_EMPTY_LIST: ch_written += fprintf(fp, "[]"); break; case ERL_LIST: if (is_printable_list(ep)) { ch_written += print_string(fp, ep); } else { putc('[', fp); ch_written++; while (ERL_IS_CONS(ep)) { ch_written += erl_print_term(fp, HEAD(ep)); ep = TAIL(ep); if (ERL_IS_CONS(ep)) { putc(',', fp); ch_written++; } } if (!ERL_IS_EMPTY_LIST(ep)) { putc('|', fp); ch_written++; ch_written += erl_print_term(fp, ep); } putc(']', fp); ch_written++; } break; case ERL_TUPLE: putc('{', fp); ch_written++; for (i=0; i < ERL_TUPLE_SIZE(ep); i++) { ch_written += erl_print_term(fp, ERL_TUPLE_ELEMENT(ep, j++) ); if (i != ERL_TUPLE_SIZE(ep)-1) { putc(',', fp); ch_written++; } } putc('}', fp); ch_written++; break; case ERL_BINARY: { int sz = (ERL_BIN_SIZE(ep) > 20) ? 20 : ERL_BIN_SIZE(ep); unsigned char *ptr = ERL_BIN_PTR(ep); ch_written += fprintf(fp, "#Bin<"); for (i = 0; i < sz; i++) { putc(ptr[i], fp); ch_written++; } if (sz == 20) ch_written += fprintf(fp, "(%d)....>", ERL_BIN_SIZE(ep)-20); else ch_written += fprintf(fp, ">"); break; } case ERL_INTEGER: case ERL_SMALL_BIG: ch_written += fprintf(fp, "%d", ERL_INT_VALUE(ep)); break; case ERL_U_INTEGER: case ERL_U_SMALL_BIG: ch_written += fprintf(fp, "%d", ERL_INT_UVALUE(ep)); break; case ERL_LONGLONG: case ERL_U_LONGLONG: ch_written += fprintf(fp, "%lld", ERL_LL_UVALUE(ep)); break; case ERL_FLOAT: ch_written += fprintf(fp, "%f", ERL_FLOAT_VALUE(ep)); break; case ERL_FUNCTION: ch_written += fprintf(fp, "#Fun<"); ch_written += erl_print_term(fp, ERL_FUN_MODULE(ep)); putc('.', fp); ch_written++; ch_written += erl_print_term(fp, ERL_FUN_INDEX(ep)); putc('.', fp); ch_written++; ch_written += erl_print_term(fp, ERL_FUN_UNIQ(ep)); putc('>', fp); ch_written++; break; default: ch_written = -10000; erl_err_msg("<ERROR> erl_print_term: Bad type of term !"); } return ch_written; }
/* * FIXME: Deep (the whole tree) or shallow (just the top term) copy? * The documentation never says, but the code as written below will * make a deep copy. This should be documented. */ ETERM *erl_copy_term(const ETERM *ep) { int i; ETERM *cp; if (!ep) return NULL; /* ASSERT(ep != NULL); */ cp = erl_alloc_eterm(ERL_TYPE(ep)); ERL_COUNT(cp) = 1; switch(ERL_TYPE(cp)) { case ERL_INTEGER: case ERL_SMALL_BIG: ERL_INT_VALUE(cp) = ERL_INT_VALUE(ep); break; case ERL_U_INTEGER: case ERL_U_SMALL_BIG: ERL_INT_UVALUE(cp) = ERL_INT_UVALUE(ep); break; case ERL_LONGLONG: ERL_LL_VALUE(cp) = ERL_LL_VALUE(ep); break; case ERL_U_LONGLONG: ERL_LL_UVALUE(cp) = ERL_LL_UVALUE(ep); break; case ERL_FLOAT: ERL_FLOAT_VALUE(cp) = ERL_FLOAT_VALUE(ep); break; case ERL_ATOM: if (!erl_atom_copy(&cp->uval.aval.d, &ep->uval.aval.d)) { erl_free_term(cp); erl_errno = ENOMEM; return NULL; } break; case ERL_PID: /* FIXME: First copy the bit pattern, then duplicate the node name and plug in. Somewhat ugly (also done with port and ref below). */ memcpy(&cp->uval.pidval, &ep->uval.pidval, sizeof(Erl_Pid)); erl_atom_copy(&cp->uval.pidval.node, &ep->uval.pidval.node); ERL_COUNT(cp) = 1; break; case ERL_PORT: memcpy(&cp->uval.portval, &ep->uval.portval, sizeof(Erl_Port)); erl_atom_copy(&cp->uval.portval.node, &ep->uval.portval.node); ERL_COUNT(cp) = 1; break; case ERL_REF: memcpy(&cp->uval.refval, &ep->uval.refval, sizeof(Erl_Ref)); erl_atom_copy(&cp->uval.refval.node, &ep->uval.refval.node); ERL_COUNT(cp) = 1; break; case ERL_LIST: HEAD(cp) = erl_copy_term(HEAD(ep)); TAIL(cp) = erl_copy_term(TAIL(ep)); break; case ERL_EMPTY_LIST: break; case ERL_TUPLE: i = ERL_TUPLE_SIZE(cp) = ERL_TUPLE_SIZE(ep); ERL_TUPLE_ELEMS(cp) = (ETERM**) erl_malloc(i * sizeof(ETERM*)); for(i=0; i < ERL_TUPLE_SIZE(ep); i++) ERL_TUPLE_ELEMENT(cp,i) = erl_copy_term(ERL_TUPLE_ELEMENT(ep, i)); break; case ERL_BINARY: ERL_BIN_SIZE(cp) = ERL_BIN_SIZE(ep); ERL_BIN_PTR(cp) = (unsigned char *) erl_malloc(ERL_BIN_SIZE(ep)); memcpy(ERL_BIN_PTR(cp), ERL_BIN_PTR(ep), ERL_BIN_SIZE(ep)); break; case ERL_FUNCTION: i = ERL_CLOSURE_SIZE(cp) = ERL_CLOSURE_SIZE(ep); ERL_FUN_ARITY(cp) = ERL_FUN_ARITY(ep); ERL_FUN_NEW_INDEX(cp) = ERL_FUN_NEW_INDEX(ep); ERL_FUN_INDEX(cp) = erl_copy_term(ERL_FUN_INDEX(ep)); ERL_FUN_UNIQ(cp) = erl_copy_term(ERL_FUN_UNIQ(ep)); ERL_FUN_CREATOR(cp) = erl_copy_term(ERL_FUN_CREATOR(ep)); ERL_FUN_MODULE(cp) = erl_copy_term(ERL_FUN_MODULE(ep)); memcpy(ERL_FUN_MD5(cp), ERL_FUN_MD5(ep), sizeof(ERL_FUN_MD5(ep))); ERL_CLOSURE(cp) = (ETERM**) erl_malloc(i * sizeof(ETERM*)); for(i=0; i < ERL_CLOSURE_SIZE(ep); i++) ERL_CLOSURE_ELEMENT(cp,i) = erl_copy_term(ERL_CLOSURE_ELEMENT(ep, i)); break; default: erl_err_msg("<ERROR> erl_copy_term: wrong type encountered !"); erl_free_term(cp); return (ETERM *) NULL; } return cp; }
int erl_sprint_term(char *buf, const ETERM *ep) { int j,i,doquote; int ch_written = 0; /* counter of written chars */ if ((!buf) || (!ep)) return 0; /* ASSERT(ep != NULL); */ j = i = doquote = 0; switch(ERL_TYPE(ep)) { case ERL_ATOM: /* FIXME: what if some weird locale is in use? */ if (!islower((int)ERL_ATOM_PTR(ep)[0])) doquote = 1; for (i = 0; !doquote && i < ERL_ATOM_SIZE(ep); i++) { doquote = !(isalnum((int)ERL_ATOM_PTR(ep)[i]) || (ERL_ATOM_PTR(ep)[i] == '_')); } if (doquote) { *buf++ = '\''; ch_written++; } { int len = ERL_ATOM_SIZE(ep); strncpy(buf, ERL_ATOM_PTR(ep), len); buf += len; ch_written += len; } if (doquote) { *buf++ = '\''; ch_written++; } break; case ERL_VARIABLE: if (!isupper((int)ERL_VAR_NAME(ep)[0])) { doquote = 1; *buf++ = '\''; ch_written++; } len = ERL_VAR_LEN(ep); strncpy(buf, ERL_VAR_NAME(ep), len); buf += len; ch_written += len; if (doquote) { *buf++ = '\''; ch_written++; } break; case ERL_PID: len = sprintf(buf, "<%s.%d.%d>", ERL_PID_NODE(ep), ERL_PID_NUMBER(ep), ERL_PID_SERIAL(ep)); buf += len; ch_written += len; break; case ERL_PORT: len = sprintf(buf , "#Port"); buf += len; ch_written += len; break; case ERL_REF: len = sprintf(buf , "#Ref"); buf += len; ch_written += len; break; case ERL_EMPTY_LIST: len = sprintf(buf , "[]"); buf += len; ch_written += len; break; case ERL_LIST: if (is_printable_list(ep)) { ch_written += print_string(fp, ep); } else { putc('[', fp); ch_written++; while (ERL_IS_CONS(ep)) { ch_written += erl_sprint_term(fp, HEAD(ep)); ep = TAIL(ep); if (ERL_IS_CONS(ep)) { putc(',', fp); ch_written++; } } if (!ERL_IS_EMPTY_LIST(ep)) { putc('|', fp); ch_written++; ch_written += erl_sprint_term(fp, ep); } putc(']', fp); ch_written++; } break; case ERL_TUPLE: putc('{', fp); ch_written++; for (i=0; i < ERL_TUPLE_SIZE(ep); i++) { ch_written += erl_sprint_term(fp, ERL_TUPLE_ELEMENT(ep, j++) ); if (i != ERL_TUPLE_SIZE(ep)-1) { putc(',', fp); ch_written++; } } putc('}', fp); ch_written++; break; case ERL_BINARY: len = sprintf(buf , "#Bin"); buf += len; ch_written += len; break; case ERL_INTEGER: case ERL_SMALL_BIG: len = sprintf(buf , "%d", ERL_INT_VALUE(ep)); buf += len; ch_written += len; break; case ERL_U_INTEGER: case ERL_U_SMALL_BIG: len = sprintf(buf , "%d", ERL_INT_UVALUE(ep)); buf += len; ch_written += len; break; case ERL_FLOAT: len = sprintf(buf , "%f", ERL_FLOAT_VALUE(ep)); buf += len; ch_written += len; break; case ERL_FUNCTION: len = sprintf(buf , "#Fun<"); buf += len; ch_written += len; ch_written += erl_sprint_term(fp, ERL_FUN_MODULE(ep)); putc('.', fp); ch_written++; ch_written += erl_sprint_term(fp, ERL_FUN_INDEX(ep)); putc('.', fp); ch_written++; ch_written += erl_sprint_term(fp, ERL_FUN_UNIQ(ep)); putc('>', fp); ch_written++; break; default: ch_written = -10000; erl_err_msg("<ERROR> erl_sprint_term: Bad type of term !"); } return ch_written; }
/** ** rockse <path_to_tbl> <path_to_log> **/ int main(int argc, char **argv) { rockse * ptr; ETERM *from_erl; ETERM *cmd; ETERM *key; ETERM *value; char *ret; ETERM *to_send; byte buf[1024]; int length; log = fopen(argv[2], "a+"); erl_init(NULL, 0); fprintf(log, "*** rockse opening table %s\n", argv[1]); ptr = open(argv[1]); while (read_cmd(buf) > 0) { from_erl = erl_decode(buf); fprintf(log, " rockse command type %d\n", ERL_TYPE(from_erl)); if (match_close(from_erl)) { send_bok(); fprintf(log, " rockse command +close+ sent\n"); break; } if (ERL_IS_TUPLE(from_erl) && ERL_TUPLE_SIZE(from_erl) == 3) { /* can only be a {put, key, val} */ cmd = ERL_TUPLE_ELEMENT(from_erl, 0); if (match_put(cmd)) { key = ERL_TUPLE_ELEMENT(from_erl, 1); value = ERL_TUPLE_ELEMENT(from_erl, 2); fprintf(log, " rockse command +put+ %s %s sent\n", erl_iolist_to_string(key), erl_iolist_to_string(value)); put(ptr, erl_iolist_to_string(key), erl_iolist_to_string(value)); send_put_ok(); } erl_free_term(from_erl); erl_free_term(cmd); erl_free_term(key); erl_free_term(value); } if (ERL_IS_TUPLE(from_erl) && ERL_TUPLE_SIZE(from_erl) == 2) { /* can only be a {get, key} or {delete, key} */ cmd = ERL_TUPLE_ELEMENT(from_erl, 0); if (match_get(cmd)) { key = ERL_TUPLE_ELEMENT(from_erl, 1); fprintf(log, " rockse command +get+ %s sent\n", erl_iolist_to_string(key)); ret = get(ptr, erl_iolist_to_string(key)); if (ret == NULL) { send_nok(); } else { length = strlen(ret); fprintf(log, " rockse command +get+ found %s of size %d\n", ret, length); to_send = erl_format("{ok, get, ~s}", ret); send_get_result(to_send); free(ret); } } else if (match_delete(cmd)) { key = ERL_TUPLE_ELEMENT(from_erl, 1); fprintf(log, " rockse command +delete+ %s sent\n", erl_iolist_to_string(key)); delete(ptr, erl_iolist_to_string(key)); send_delete_ok(); } erl_free_term(from_erl); erl_free_term(cmd); erl_free_term(key); } fflush(log); } close_and_destroy(ptr); fprintf(log, "*** rockse closing table %s\n", argv[1]); erl_free_term(from_erl); fclose(log); return 0; }
/* * Dump (print for debugging) a term. Useful if/when things go wrong. */ void dump_term (FILE *fp, ETERM *t) { if (fp == NULL) return; fprintf(fp, "#<%p ", t); if(t != NULL) { fprintf(fp, "count:%d, type:%d", ERL_COUNT(t), ERL_TYPE(t)); switch(ERL_TYPE(t)) { case ERL_UNDEF: fprintf(fp, "==undef"); break; case ERL_INTEGER: fprintf(fp, "==int, val:%d", ERL_INT_VALUE(t)); break; case ERL_U_INTEGER: fprintf(fp, "==uint, val:%u", ERL_INT_UVALUE(t)); break; case ERL_FLOAT: fprintf(fp, "==float, val:%g", ERL_FLOAT_VALUE(t)); break; case ERL_ATOM: fprintf(fp, "==atom, name:%p \"%s\"", ERL_ATOM_PTR(t), ERL_ATOM_PTR(t)); break; case ERL_BINARY: fprintf(fp, "==binary, data:%p,%u", ERL_BIN_PTR(t), ERL_BIN_SIZE(t)); break; case ERL_PID: fprintf(fp, "==pid, node:%p \"%s\"", ERL_PID_NODE(t), ERL_PID_NODE(t)); break; case ERL_PORT: fprintf(fp, "==port, node:%p \"%s\"", ERL_PORT_NODE(t), ERL_PORT_NODE(t)); break; case ERL_REF: fprintf(fp, "==ref, node:%p \"%s\"", ERL_REF_NODE(t), ERL_REF_NODE(t)); break; case ERL_CONS: fprintf(fp, "==cons"); fprintf(fp, ", car:"); dump_term(fp, ERL_CONS_HEAD(t)); fprintf(fp, ", cdr:"); dump_term(fp, ERL_CONS_TAIL(t)); break; case ERL_NIL: fprintf(fp, "==nil"); break; case ERL_TUPLE: fprintf(fp, "==tuple, elems:%p,%u", ERL_TUPLE_ELEMS(t), ERL_TUPLE_SIZE(t)); { size_t i; for(i = 0; i < ERL_TUPLE_SIZE(t); i++) { fprintf(fp, "elem[%u]:", i); dump_term(fp, ERL_TUPLE_ELEMENT(t, i)); } } break; case ERL_VARIABLE: fprintf(fp, "==variable, name:%p \"%s\"", ERL_VAR_NAME(t), ERL_VAR_NAME(t)); fprintf(fp, ", value:"); dump_term(fp, ERL_VAR_VALUE(t)); break; default: break; } } fprintf(fp, ">"); }
PyObject *eterm_to_py(ETERM * obj) { int i; int count; ETERM *obj2; PyObject *eobj = NULL; if (obj == NULL) { Py_INCREF(Py_None); return Py_None; } switch (ERL_TYPE(obj)) { case ERL_CONS: case ERL_NIL: count = erl_length(obj); eobj = PyList_New(0); for (i = 0; i < count; i++) { obj2 = erl_hd(obj); PyList_Append(eobj, eterm_to_py(obj2)); obj = erl_tl(obj); } break; case ERL_TUPLE: eobj = PyTuple_New(erl_size(obj)); for (i = 1; i <= erl_size(obj); i++) { obj2 = erl_element(i, obj); PyTuple_SetItem(eobj, i - 1, eterm_to_py(obj2)); } break; case ERL_ATOM: eobj = PyString_FromStringAndSize(ERL_ATOM_PTR(obj), ERL_ATOM_SIZE(obj)); break; case ERL_INTEGER: eobj = PyInt_FromLong(ERL_INT_VALUE(obj)); break; case ERL_BINARY: fprintf(stderr, "FOUND A BINARY %.*s\n", ERL_BIN_SIZE(obj), ERL_BIN_PTR(obj)); break; case ERL_PID: eobj = PyDict_New(); if (PyDict_SetItemString(eobj, "node", PyString_FromString(ERL_PID_NODE(obj)))) { PyErr_Print(); break; } if (PyDict_SetItemString(eobj, "number", PyInt_FromLong(ERL_PID_NUMBER(obj)))) { PyErr_Print(); break; } if (PyDict_SetItemString(eobj, "serial", PyInt_FromLong(ERL_PID_SERIAL(obj)))) { PyErr_Print(); break; } if (PyDict_SetItemString(eobj, "creation", PyInt_FromLong(ERL_PID_CREATION(obj)))) { PyErr_Print(); break; } default: fprintf(stderr, "UNMANAGED ETERM TYPE: %d\n", ERL_TYPE(obj)); break; } if (eobj == NULL) { Py_INCREF(Py_None); return Py_None; } return eobj; }