static int term_sequence_test(IC_Env *env) { ETERM* et_array[4] = { erl_format("[{apa, 1, 23}, \"string\", {1.23, 45}]"), erl_format("[{banan, 1, 23}, \"string\", {1.23, 45}]"), erl_format("[{apelsin, 1, 23}, \"string\", {1.23, 45}]"), erl_format("[{mango, 1, 23}, \"string\", {1.23, 45}]")}; m_etseq etsi = {4, 4, et_array}, *etso, *etsr; fprintf(stdout, "\n======== m_i_term_sequence test ======\n\n"); etsr = m_i_term_sequence_test(NULL, &etsi, &etso, env); CHECK_EXCEPTION(env); RETURN_IF_OK(cmp_etseq(&etsi, etso) && cmp_etseq(&etsi, etsr)); if (!cmp_etseq(&etsi, etso)) { fprintf(stdout, " out parameter error, sent:\n"); print_etseq(&etsi); fprintf(stdout, "got:\n"); print_etseq(etso); } if (!cmp_etseq(&etsi, etsr)) { fprintf(stdout, " result error, sent:\n"); print_etseq(&etsi); fprintf(stdout, "got:\n"); print_etseq(etsr); } free_etseq_buf(&etsi); free_etseq_buf(etso); free_etseq_buf(etsr); CORBA_free(etso); CORBA_free(etsr); return -1; }
ETERM * new_image_blank(ETERM *arg, int c_node) { int stride, cbufsize, status, key_length; ETERM *width, *height; cairo_context *ctx = NULL; width = erl_element(1, arg); height = erl_element(2, arg); stride = ERL_INT_VALUE(width) * 4; cbufsize = ERL_INT_VALUE(height) * stride; ctx = malloc(sizeof(cairo_context)); if (ctx) { ctx->cbuf = (byte *)malloc(cbufsize); if (ctx->cbuf) { memset(ctx->cbuf, 0, cbufsize); ctx->sf = cairo_image_surface_create_for_data(ctx->cbuf, CAIRO_FORMAT_ARGB32, ERL_INT_VALUE(width), ERL_INT_VALUE(height), stride); ctx->cr = cairo_create(ctx->sf); return erl_format("{c_node, ~i, {ok, ~i}}", c_node, ctx); } else { free(ctx); return erl_format("{c_node, ~i, {error, '~s'}}", c_node, ERR_CONTEXT); } } else { return erl_format("{c_node, ~i, {error, '~s'}}", c_node, ERR_CONTEXT); } erl_free_term(width); erl_free_term(height); }
void handle_last(ETERM *msg) { ETERM *ecols, *erows, *resp; if (results == NULL) { resp = erl_format("{error, result_set_does_not_exist}"); write_msg(resp); erl_free_term(resp); return; } mysql_stmt_data_seek(sth, numrows - 1); resultoffset = numrows; ecols = make_cols(); erows = make_rows(1); resp = erl_format("{selected, ~w, ~w}", ecols, erows); erl_free_term(erows); erl_free_term(ecols); write_msg(resp); erl_free_term(resp); }
void handle_prev(ETERM *msg) { ETERM *ecols, *erows, *resp; if (results == NULL) { resp = erl_format("{error, result_set_does_not_exist}"); write_msg(resp); erl_free_term(resp); return; } ecols = make_cols(); if (resultoffset <= 1) { resp = erl_format("{selected, ~w, []}", ecols); } else { resultoffset--; mysql_stmt_data_seek(sth, resultoffset - 1); erows = make_rows(1); resp = erl_format("{selected, ~w, ~w}", ecols, erows); erl_free_term(erows); } erl_free_term(ecols); write_msg(resp); erl_free_term(resp); }
ETERM * rel_curve_to(ETERM* arg, int c_node) { ETERM *c1x, *c1y, *c2x, *c2y, *x, *y; cairo_context *ctx = get_cairo_context(arg); if (ctx) { c1x = erl_element(2, arg); c1y = erl_element(3, arg); c2x = erl_element(4, arg); c2y = erl_element(5, arg); x = erl_element(6, arg); y = erl_element(7, arg); cairo_rel_curve_to(ctx->cr, val(c1x), val(c1y), val(c2x), val(c2y), val(x), val(y)); erl_free_term(c1x); erl_free_term(c1y); erl_free_term(c2x); erl_free_term(c2y); erl_free_term(x); erl_free_term(y); return erl_format("{c_node, ~i, ok}", c_node); } else { return erl_format("{c_node, ~i, {error, '~s'}}", c_node, ERR_CONTEXT); } }
static int hello_getattr(const char *path, struct stat *stbuf) { ETERM * response = erl_rpc(FERL_DATA->erlang_fd,"gen_server","call",erl_format("[nefs,{get_attr,~s}]",path)); ETERM * pattern = erl_format("{directory,Mode,Nlink}"); ETERM * pattern2 = erl_format("{file,Mode,Nlink,Size}"); int res = 0; memset(stbuf, 0, sizeof(struct stat)); if(erl_match(pattern, response)) { //directory ETERM * Mode = erl_var_content(pattern, "Mode"); ETERM * Nlink = erl_var_content(pattern, "Nlink"); if(ERL_IS_INTEGER(Nlink) && ERL_IS_INTEGER(Mode)){ stbuf->st_mode = S_IFDIR | ERL_INT_VALUE(Mode); //permissions stbuf->st_nlink = ERL_INT_VALUE(Nlink); // directories have the number of files in them }else{ res = -ENOENT; } }else if(erl_match(pattern2, response)){ //file ETERM * Mode = erl_var_content(pattern2, "Mode"); ETERM * Nlink = erl_var_content(pattern2, "Nlink"); ETERM * Size = erl_var_content(pattern2, "Size"); if(ERL_IS_INTEGER(Nlink) && ERL_IS_INTEGER(Mode) && ERL_IS_INTEGER(Size)){ stbuf->st_mode = S_IFREG | ERL_INT_VALUE(Mode); //permissions stbuf->st_nlink = ERL_INT_VALUE(Nlink); //files only have 1 stbuf->st_size = ERL_INT_VALUE(Size); // length of the file }else{ res = -ENOENT; } }else{ res = -ENOENT; } return res; }
static int hello_readdir(const char *path, void *buf, fuse_fill_dir_t filler, off_t offset, struct fuse_file_info *fi) { int res = 0; ETERM * response = erl_rpc(FERL_DATA->erlang_fd,"gen_server","call",erl_format("[nefs,{read_dir,~s}]",path)); ETERM * pattern = erl_format("{ok,Listing}"); if(erl_match(pattern, response)) { //directory ETERM * Tail = erl_var_content(pattern, "Listing"); if(ERL_IS_LIST(Tail)){ while(!ERL_IS_EMPTY_LIST(Tail)){ ETERM * elem = ERL_CONS_HEAD(Tail); // add in the links to the previous folders filler(buf, ".", NULL, 0); filler(buf, "..", NULL, 0); // add in the folders contents if(ERL_IS_LIST(elem)){ filler(buf,(char *)ERL_BIN_PTR(elem),NULL,0); }else{ res = -ENOENT; break; } Tail = ERL_CONS_TAIL(Tail); } }else{ res = -ENOENT; } }else{ res = -ENOENT; } return res; }
ETERM * stroke_preserve(ETERM* arg, int c_node) { cairo_context *ctx = get_cairo_context(arg); if (ctx) { cairo_stroke_preserve(ctx->cr); return erl_format("{c_node, ~i, ok}", c_node); } else { return erl_format("{c_node, ~i, {error, '~s'}}", c_node, ERR_CONTEXT); } }
ETERM * close_image(ETERM* arg, int c_node) { cairo_context *ctx = get_cairo_context(arg); if (ctx) { cairo_destroy(ctx->cr); cairo_surface_destroy(ctx->sf); free(ctx->cbuf); return erl_format("{c_node, ~i, ok}", c_node); } else { return erl_format("{c_node, ~i, {error, '~s'}}", c_node, ERR_CONTEXT); } }
ETERM * set_line_width(ETERM* arg, int c_node) { ETERM *width; cairo_context *ctx = get_cairo_context(arg); if (ctx) { width = erl_element(2, arg); cairo_set_line_width(ctx->cr, ERL_INT_VALUE(width)); erl_free_term(width); return erl_format("{c_node, ~i, ok}", c_node); } else { return erl_format("{c_node, ~i, {error, '~s'}}", c_node, ERR_CONTEXT); } }
ETERM * show_text(ETERM* arg, int c_node) { ETERM *text; cairo_context *ctx = get_cairo_context(arg); if (ctx) { text = erl_element(2, arg); cairo_show_text(ctx->cr, (char *)ERL_ATOM_PTR(text)); erl_free_term(text); return erl_format("{c_node, ~i, ok}", c_node); } else { return erl_format("{c_node, ~i, {error, '~s'}}", c_node, ERR_CONTEXT); } }
ETERM * set_font_size(ETERM* arg, int c_node) { ETERM *size; cairo_context *ctx = get_cairo_context(arg); if (ctx) { size = erl_element(2, arg); cairo_set_font_size(ctx->cr, val(size)); erl_free_term(size); return erl_format("{c_node, ~i, ok}", c_node); } else { return erl_format("{c_node, ~i, {error, '~s'}}", c_node, ERR_CONTEXT); } }
ETERM * rotate(ETERM* arg, int c_node) { ETERM *angle; cairo_context *ctx = get_cairo_context(arg); if (ctx) { angle = erl_element(2, arg); cairo_rotate(ctx->cr, val(angle)); erl_free_term(angle); return erl_format("{c_node, ~i, ok}", c_node); } else { return erl_format("{c_node, ~i, {error, '~s'}}", c_node, ERR_CONTEXT); } }
ETERM * set_operator(ETERM* arg, int c_node) { ETERM *operator; cairo_context *ctx = get_cairo_context(arg); if (ctx) { operator = erl_element(2, arg); cairo_set_operator(ctx->cr, ERL_INT_VALUE(operator)); erl_free_term(operator); return erl_format("{c_node, ~i, ok}", c_node); } else { return erl_format("{c_node, ~i, {error, '~s'}}", c_node, ERR_CONTEXT); } }
ETERM * write_to_png(ETERM* arg, int c_node) { int status; ETERM *file; cairo_context *ctx = get_cairo_context(arg); if (ctx) { file = erl_element(2, arg); status = cairo_surface_write_to_png(ctx->sf, (char *)ERL_ATOM_PTR(file)); erl_free_term(file); return erl_format("{c_node, ~i, ok}", c_node); } else { return erl_format("{c_node, ~i, {error, '~s'}}", c_node, ERR_CONTEXT); } }
ETERM * text_extents(ETERM* arg, int c_node) { ETERM *text; cairo_text_extents_t extents; cairo_context *ctx = get_cairo_context(arg); if (ctx) { text = erl_element(2, arg); cairo_text_extents (ctx->cr, (char *)ERL_ATOM_PTR(text), &extents); erl_free_term(text); return erl_format("{c_node, ~i, {~f, ~f}}", c_node, extents.width, extents.height); } else { return erl_format("{c_node, ~i, {error, '~s'}}", c_node, ERR_CONTEXT); } }
ETERM * rel_line_to(ETERM* arg, int c_node) { ETERM *x, *y; cairo_context *ctx = get_cairo_context(arg); if (ctx) { x = erl_element(2, arg); y = erl_element(3, arg); cairo_rel_line_to(ctx->cr, val(x), val(y)); erl_free_term(x); erl_free_term(y); return erl_format("{c_node, ~i, ok}", c_node); } else { return erl_format("{c_node, ~i, {error, '~s'}}", c_node, ERR_CONTEXT); } }
ETERM * translate(ETERM* arg, int c_node) { ETERM *tx, *ty; cairo_context *ctx = get_cairo_context(arg); if (ctx) { tx = erl_element(2, arg); ty = erl_element(3, arg); cairo_translate(ctx->cr, val(tx), val(ty)); erl_free_term(tx); erl_free_term(ty); return erl_format("{c_node, ~i, ok}", c_node); } else { return erl_format("{c_node, ~i, {error, '~s'}}", c_node, ERR_CONTEXT); } }
ETERM * scale(ETERM* arg, int c_node) { ETERM *sx, *sy; cairo_context *ctx = get_cairo_context(arg); if (ctx) { sx = erl_element(2, arg); sy = erl_element(3, arg); cairo_scale(ctx->cr, val(sx), val(sy)); erl_free_term(sx); erl_free_term(sy); return erl_format("{c_node, ~i, ok}", c_node); } else { return erl_format("{c_node, ~i, {error, '~s'}}", c_node, ERR_CONTEXT); } }
static int process_hashpw(ETERM *pid, ETERM *data) { int retval = 0; ETERM *pattern, *pwd, *slt; char *password, *salt; char *ret = NULL; pattern = erl_format("{Pass, Salt}"); if (erl_match(pattern, data)) { pwd = erl_var_content(pattern, "Pass"); password = erl_iolist_to_string(pwd); slt = erl_var_content(pattern, "Salt"); salt = erl_iolist_to_string(slt); if (NULL == (ret = bcrypt(password, salt)) || 0 == strcmp(ret, ":")) { retval = process_reply(pid, CMD_HASHPW, "Invalid salt"); } else { retval = process_reply(pid, CMD_HASHPW, ret); } erl_free_term(pwd); erl_free_term(slt); erl_free(password); erl_free(salt); }; erl_free_term(pattern); return retval; }
static int typedef_test(IC_Env *env) { m_banan mbi, mbo; /* erlang_port */ m_apa mai; /* ETERM* */ m_apa mao = NULL; long tl; strcpy(mbi.node,"node"); mbi.id = 15; mbi.creation = 1; fprintf(stdout, "\n======== m_i_typedef test ======\n\n"); mai = erl_format("[{hej, 1, 23}, \"string\", {1.23, 45}]"); tl = m_i_typedef_test(NULL, mai, &mbi, &mao, &mbo, env); CHECK_EXCEPTION(env); RETURN_IF_OK(erl_match(mai, mao) && cmp_port(&mbi, &mbo) && tl == 4711); if (!erl_match(mai, mao)) { fprintf(stdout, " out parameter error (term), sent:\n"); print_term(mai); fprintf(stdout, "got:\n"); print_term(mao); } if (!cmp_port(&mbi, &mbo)) { fprintf(stdout, " out parameter error (port), sent:\n"); print_port(&mbi); fprintf(stdout, "got:\n"); print_port(&mbo); } if (tl != 4711) { fprintf(stdout, " result error, sent: 4711, got %ld\n", tl); } erl_free_term(mai); erl_free_term(mao); return -1; }
static int term_test(IC_Env *env) { ETERM *ti, *to, *tr; ti = erl_format("[{hej, 1, 23}, \"string\", {1.23, 45}]"); fprintf(stdout, "\n======== m_i_term test ======\n\n"); tr = m_i_term_test(NULL, ti, &to, env); CHECK_EXCEPTION(env); RETURN_IF_OK(erl_match(ti, to) && erl_match(ti, tr)); if (!erl_match(ti, to)) { fprintf(stdout, " out parameter error, sent:\n"); print_term(ti); fprintf(stdout, "got:\n"); print_term(to); } if (!erl_match(ti, tr)) { fprintf(stdout, " result error, sent:\n"); print_term(ti); fprintf(stdout, "got:\n"); print_term(tr); } erl_free_term(ti); erl_free_term(to); erl_free_term(tr); return -1; }
erlang_pid *erl_whereis(int fd, const char *name) { ETERM *reply; ETERM *n; /* FIXME problem for threaded ? */ static erlang_pid pid; n = erl_format("[~a]",name); reply = erl_rpc(fd,"erlang","whereis",n); erl_free_term(n); if (reply && (ERL_IS_PID(reply))) { char *node; node = ERL_PID_NODE(reply); strcpy(pid.node,node); pid.num = ERL_PID_NUMBER(reply); pid.serial = ERL_PID_SERIAL(reply); pid.creation = ERL_PID_CREATION(reply); erl_free_term(reply); return &pid; } if (reply) erl_free_term(reply); return NULL; }
void vaskerl_loop (int fd) { unsigned char buf[MAX_BUF]; while (1) { ErlMessage emsg; int got = erl_receive_msg(fd, buf, MAX_BUF, &emsg); if (got == ERL_TICK) continue; else if (got == ERL_ERROR) { fprintf(stderr, "erl_error\n"); break; } if (emsg.type == ERL_REG_SEND) { printf("Got something...\n"); ETERM *resp = erl_format("{cnode, \"hello\"}"); erl_send(fd, emsg.from, resp); erl_free_term(resp); erl_free_term(emsg.from); erl_free_term(emsg.msg); } } }
static int hello_read(const char *path, char *buf, size_t size, off_t offset, struct fuse_file_info *fi) { int ret_bytes = 0; ETERM * response = erl_rpc(FERL_DATA->erlang_fd,"file","pread",erl_format("[~s,{bof,~i},~i]",offset,size)); ETERM * pattern = erl_format("{ok,Data}"); if(erl_match(pattern, response)) { ETERM * Data = erl_var_content(pattern, "Data"); if(ERL_IS_BINARY(Data)){ ret_bytes = ERL_BIN_SIZE(Data); char * data = ERL_BIN_PTR(Data); memcpy(buf,data,size); //copy the data over into the buffer } } return ret_bytes; }
static int term_struct_test(IC_Env *env) { m_et eti = { erl_format("[{hej, 1, 23}, \"string\", {1.23, 45}]"), 121212 }; m_et eto, etr; fprintf(stdout, "\n======== m_i_term_struct test ======\n\n"); etr = m_i_term_struct_test(NULL, &eti, &eto, env); CHECK_EXCEPTION(env); RETURN_IF_OK(cmp_et(&eti, &eto) && cmp_et(&eti, &etr)); if (!cmp_et(&eti, &eto)) { fprintf(stdout, " out parameter error, sent:\n"); print_et(&eti); fprintf(stdout, "got:\n"); print_et(&eto); } if (!cmp_et(&eti, &etr)) { fprintf(stdout, " result error, sent:\n"); print_et(&eti); fprintf(stdout, "got:\n"); print_et(&etr); } free_et(&eti); free_et(&eto); free_et(&etr); return -1; }
static int process_encode_salt(ETERM *pid, ETERM *data) { int retval = 0; ETERM *pattern, *cslt, *lr; byte *csalt = NULL; long log_rounds = -1; int csaltlen = -1; char ret[64]; pattern = erl_format("{Csalt, LogRounds}"); if (erl_match(pattern, data)) { cslt = erl_var_content(pattern, "Csalt"); csaltlen = ERL_BIN_SIZE(cslt); csalt = ERL_BIN_PTR(cslt); lr = erl_var_content(pattern, "LogRounds"); log_rounds = ERL_INT_UVALUE(lr); if (16 != csaltlen) { retval = process_reply(pid, CMD_SALT, "Invalid salt length"); } else if (log_rounds < 4 || log_rounds > 31) { retval = process_reply(pid, CMD_SALT, "Invalid number of rounds"); } else { encode_salt(ret, (u_int8_t*)csalt, csaltlen, log_rounds); retval = process_reply(pid, CMD_SALT, ret); } erl_free_term(cslt); erl_free_term(lr); }; erl_free_term(pattern); return retval; }
ETERM * set_source_surface(ETERM* arg, int c_node) { ETERM *surface, *x, *y; cairo_context *ctx = get_cairo_context(arg); if (ctx) { surface = erl_element(2, arg); x = erl_element(3, arg); y = erl_element(4, arg); cairo_set_source_surface(ctx->cr, ptr(surface), val(x), val(y)); erl_free_term(surface); erl_free_term(x); erl_free_term(y); return erl_format("{c_node, ~i, ok}", c_node); } else { return erl_format("{c_node, ~i, {error, '~s'}}", c_node, ERR_CONTEXT); } }
static int process_command(unsigned char *buf) { int retval = 0; ETERM *pattern, *tuple, *cmd, *port, *data; pattern = erl_format("{Cmd, Port, Data}"); tuple = erl_decode(buf); if (erl_match(pattern, tuple)) { cmd = erl_var_content(pattern, "Cmd"); port = erl_var_content(pattern, "Port"); data = erl_var_content(pattern, "Data"); switch (ERL_INT_VALUE(cmd)) { case CMD_SALT: retval = process_encode_salt(port, data); break; case CMD_HASHPW: retval = process_hashpw(port, data); break; }; erl_free_term(cmd); erl_free_term(port); erl_free_term(data); } erl_free_term(pattern); erl_free_term(tuple); return retval; }
ETERM * make_row() { ETERM **rowtup, *rc; unsigned int i; rowtup = (ETERM **)safe_malloc(numfields * sizeof(ETERM *)); for (i = 0; i < numfields; i++) { if (*r_bind[i].is_null) rowtup[i] = erl_mk_atom("null"); else rowtup[i] = erl_mk_estring(r_bind[i].buffer, *r_bind[i].length); } rc = erl_mk_tuple(rowtup, numfields); if (rc == NULL) { ETERM *resp; resp = erl_format("{error, {erl_mk_tuple, ~i}}", numfields); write_msg(resp); erl_free_term(resp); exit(3); } for (i = 0; i < numfields; i++) erl_free_term(rowtup[i]); free(rowtup); return rc; }