Beispiel #1
0
void encode_result(ei_x_buff* x, PGresult* res, PGconn* conn)
{
    int row, n_rows, col, n_cols;
    switch (PQresultStatus(res)) {
    case PGRES_TUPLES_OK: 
	n_rows = PQntuples(res); 
	n_cols = PQnfields(res); 
	ei_x_encode_tuple_header(x, 2);
	encode_ok(x);
	ei_x_encode_list_header(x, n_rows+1);
 	ei_x_encode_list_header(x, n_cols);
	for (col = 0; col < n_cols; ++col) {
	    ei_x_encode_string(x, PQfname(res, col));
	}
	ei_x_encode_empty_list(x); 
	for (row = 0; row < n_rows; ++row) {
	    ei_x_encode_list_header(x, n_cols);
	    for (col = 0; col < n_cols; ++col) {
		ei_x_encode_string(x, PQgetvalue(res, row, col));
	    }
	    ei_x_encode_empty_list(x);
	}
	ei_x_encode_empty_list(x); 
	break; 
    case PGRES_COMMAND_OK:
	ei_x_encode_tuple_header(x, 2);
        encode_ok(x);
	ei_x_encode_string(x, PQcmdTuples(res));
        break;
    default:
	encode_error(x, conn);
	break;
    }
}
Beispiel #2
0
void encode_name(const XML_Char *name)
{
  char *name_start;
  char *prefix_start;
  char *buf;
  int name_len, prefix_len, buf_len;

  if ((name_start = strchr(name, '\n'))) {
    if ((prefix_start = strchr(name_start+1, '\n'))) {
      name_len = prefix_start - name_start;
      prefix_len = strlen(prefix_start+1);
      buf_len = prefix_len + name_len;
      buf = driver_alloc(buf_len);
      memcpy(buf, prefix_start+1, prefix_len);
      memcpy(buf+prefix_len, name_start, name_len);
      buf[prefix_len] = ':';
      ei_x_encode_string_len(&event_buf, buf, buf_len);
      driver_free(buf);
    } else {
      ei_x_encode_string(&event_buf, name_start+1);
    };
  } else {
    ei_x_encode_string(&event_buf, name);
  }
}
Beispiel #3
0
void *erlXML_StartElementHandler(expat_data *d,
				 const XML_Char *name,
				 const XML_Char **atts)
{
   int i;

   ei_x_encode_list_header(&event_buf, 1);
   ei_x_encode_tuple_header(&event_buf, 2);
   ei_x_encode_long(&event_buf, XML_START);
   ei_x_encode_tuple_header(&event_buf, 2);
   ei_x_encode_string(&event_buf, name);

   for (i = 0; atts[i]; i += 2) {}

   if (i > 0)
   {
      ei_x_encode_list_header(&event_buf, i/2);

      for (i = 0; atts[i]; i += 2)
      {
	 ei_x_encode_tuple_header(&event_buf, 2);
	 ei_x_encode_string(&event_buf, atts[i]);
	 ei_x_encode_string(&event_buf, atts[i+1]);
      }
   }

   ei_x_encode_empty_list(&event_buf);

   return NULL;
}
Beispiel #4
0
void *erlXML_StartNamespaceDeclHandler(expat_data *d,
				       const XML_Char *prefix,
				       const XML_Char *uri)
{
  int prefix_len;
  char *buf;

  /* From the expat documentation:
     "For a default namespace declaration (xmlns='...'),
     the prefix will be null ...
     ... The URI will be null for the case where
     the default namespace is being unset."

     FIXME: I'm not quite sure what all that means */
  if (uri == NULL)
      return NULL;

  ei_x_encode_list_header(&xmlns_buf, 1);
  ei_x_encode_tuple_header(&xmlns_buf, 2);
  if (prefix) {
    prefix_len = strlen(prefix);
    buf = driver_alloc(7 + prefix_len);
    strcpy(buf, "xmlns:");
    strcpy(buf+6, prefix);
    ei_x_encode_string(&xmlns_buf, buf);
    driver_free(buf);
  } else {
    ei_x_encode_string(&xmlns_buf, "xmlns");
  };
  ei_x_encode_string(&xmlns_buf, uri);

  return NULL;
}
Beispiel #5
0
static int
encode_attr(struct exmpp_xml_ctx *ctx, ei_x_buff *tree,
    const char *attr, int attr_len)
{
	int ret;

	/* Check if the attribute is known and encode it. */
	if (attr_len <= MAXATOMLEN && ctx->names_as_atom) {
		if (is_known_attr(ctx, attr, attr_len))
			if (attr_len == -1)
				ret = ei_x_encode_atom(tree, attr);
			else
				ret = ei_x_encode_atom_len(tree,
				    attr, attr_len);
		else
			if (attr_len == -1)
				ret = ei_x_encode_string(tree, attr);
			else
				ret = ei_x_encode_string_len(tree,
				    attr, attr_len);
	} else {
		if (attr_len == -1)
			ret = ei_x_encode_string(tree, attr);
		else
			ret = ei_x_encode_string_len(tree, attr, attr_len);
	}

	return (ret);
}
Beispiel #6
0
static int
encode_ns(struct exmpp_xml_ctx *ctx, ei_x_buff *tree,
    const char *ns, int ns_len)
{
	int ret;

	if (ns != NULL) {
		/* Check if the namespace is known and encode it. */
		if (ns_len <= MAXATOMLEN && ctx->names_as_atom) {
			if (is_known_ns(ctx, ns, ns_len))
				if (ns_len == -1)
					ret = ei_x_encode_atom(tree, ns);
				else
					ret = ei_x_encode_atom_len(tree,
					    ns, ns_len);
			else
				if (ns_len == -1)
					ret = ei_x_encode_string(tree, ns);
				else
					ret = ei_x_encode_string_len(tree,
					    ns, ns_len);
		} else {
			if (ns_len == -1)
				ret = ei_x_encode_string(tree, ns);
			else
				ret = ei_x_encode_string_len(tree,
				    ns, ns_len);
		}
	} else {
		/* The namespace is "empty". The element may have none. */
		ret = ei_x_encode_atom(tree, "undefined");
	}

	return (ret);
}
Beispiel #7
0
static int
encode_elem(struct exmpp_xml_ctx *ctx, ei_x_buff *tree,
    const char *elem, int elem_len)
{
	int ret;

	/* Check if the element is known and encode it. */
	if (elem_len <= MAXATOMLEN && ctx->names_as_atom) {
		if (is_known_elem(ctx, elem, elem_len))
			if (elem_len == -1)
				ret = ei_x_encode_atom(tree, elem);
			else
				ret = ei_x_encode_atom_len(tree,
				    elem, elem_len);
		else
			if (elem_len == -1)
				ret = ei_x_encode_string(tree, elem);
			else
				ret = ei_x_encode_string_len(tree,
				    elem, elem_len);
	} else {
		if (elem_len == -1)
			ret = ei_x_encode_string(tree, elem);
		else
			ret = ei_x_encode_string_len(tree, elem, elem_len);
	}

	return (ret);
}
Beispiel #8
0
void encode_error(ei_x_buff* x, PGconn* conn)
{
    const char* k_error = "error";
    ei_x_encode_tuple_header(x, 2);
    ei_x_encode_atom(x, k_error);
    ei_x_encode_string(x, PQerrorMessage(conn));
}
Beispiel #9
0
int ei_x_encode_error_tuple_string(ei_x_buff *buff, char *str) {
    EI(ei_x_encode_version(buff));
    EI(ei_x_encode_tuple_header(buff, 2));
    EI(ei_x_encode_atom(buff, "error"));
    EI(ei_x_encode_string(buff, str));
    return 0;
}
Beispiel #10
0
static DWORD WINAPI
#else
static void*
#endif
    einode_thread(void* num)
{
    int n = (int)num;
    ei_cnode ec;
    char myname[100], destname[100];
    int r, fd;

    sprintf(myname, "ei%d", n);
    sprintf(destname, "mth%d", n);
    printf("thread %d (%s %s) connecting\n", n, myname, destname);
    r = ei_connect_init(&ec, myname, cookie, 0);
    fd = ei_connect(&ec, (char*)desthost);
    if (r == 0 && fd >= 0) {
	ei_x_buff x;
	ei_x_new_with_version(&x);
	ei_x_encode_string(&x, myname);
	ei_reg_send(&ec, fd, destname, x.buff, x.index);
	ei_x_free(&x);
	//SleepEx(100);
	shutdown(fd, SD_SEND);
	closesocket(fd);
    } else {
	printf("coudn't connect fd %d r %d\n", fd, r); //	DebugBreak();
    }
    printf("done thread %d\n", n);
    return 0;
}
gboolean GN_tree_view_get_selected(int ARI, ei_x_buff *XBUF, char *B, int *I){
  GtkTreeView *tv;
  GtkTreeSelection *selection;
  GtkTreeModel *model;
  gchar* path;

  if ( ! gn_check_arity(XBUF, 1, ARI) )
    return FALSE;
  if ( ! gn_get_arg_object(XBUF, B, I, GTK_TYPE_TREE_VIEW, (GObject**)&tv) )
    return FALSE;

  model = gtk_tree_view_get_model(tv);
  selection = gtk_tree_view_get_selection(tv);

  GList* list = gtk_tree_selection_get_selected_rows(selection, &model);

  gn_wrap_reply("ok", XBUF);

  while ( list ) {
    path = gtk_tree_path_to_string( (GtkTreePath*) list->data);
    g_assert( ! ei_x_encode_list_header(XBUF, 1) );
    g_assert( ! ei_x_encode_string(XBUF, path) );
    g_free(path);
    list = list->next;
  }
  g_list_free(list);
  g_assert( ! ei_x_encode_empty_list(XBUF));
  return TRUE;
}
Beispiel #12
0
void *erlXML_EndElementHandler(expat_data *d,
			       const XML_Char *name)
{
   ei_x_encode_list_header(&event_buf, 1);
   ei_x_encode_tuple_header(&event_buf, 2);
   ei_x_encode_long(&event_buf, XML_END);
   ei_x_encode_string(&event_buf, name);
   return NULL;
}
static ei_x_buff make_error(const char* text){

	ei_x_buff x;
	ei_x_new_with_version(&x);
	ei_x_encode_tuple_header(&x,2);
	ei_x_encode_atom(&x,"error");
	ei_x_encode_string(&x,text);

	return x;
}
Beispiel #14
0
static int expat_erl_control(ErlDrvData drv_data,
			     unsigned int command,
			     char *buf, int len,
			     char **rbuf, int rlen)
{
   expat_data* d = (expat_data*)drv_data;
   int res, errcode;
   char *errstring;
   ErlDrvBinary *b;
   size_t size;

   switch (command)
   {
      case PARSE_COMMAND:
      case PARSE_FINAL_COMMAND:
	 ei_x_new_with_version(&event_buf);
	 ei_x_new(&xmlns_buf);
	 res = XML_Parse(d->parser, buf, len, command == PARSE_FINAL_COMMAND);

	 if(!res)
	 {
	    errcode = XML_GetErrorCode(d->parser);
	    errstring = (char *)XML_ErrorString(errcode);

	    ei_x_encode_list_header(&event_buf, 1);
	    ei_x_encode_tuple_header(&event_buf, 2);
	    ei_x_encode_long(&event_buf, XML_ERROR);
	    ei_x_encode_tuple_header(&event_buf, 2);
	    ei_x_encode_long(&event_buf, errcode);
	    ei_x_encode_string(&event_buf, errstring);
	 }

	 ei_x_encode_empty_list(&event_buf);

	 size = event_buf.index;

	 b = driver_alloc_binary(size);
	 memcpy(b->orig_bytes, event_buf.buff, size);

	 ei_x_free(&event_buf);
	 ei_x_free(&xmlns_buf);
 
	 *rbuf = (char *)b;
	 return size;
      default:
	 return 0;
   }
}
static ei_x_buff pack_parse_data(parse_result parse_result_){

	ei_x_buff x;
	ei_x_new_with_version(&x);
	ei_x_encode_tuple_header(&x,2);
	//{caly_tekst,[link]} kodujemy caly tekst
	ei_x_encode_string(&x, parse_result_.second.c_str());
	int link_list_length = parse_result_.first.size();
	ei_x_encode_list_header(&x,link_list_length);
	vector<std::string>& links = parse_result_.first;
	for (vector<std::string>::iterator it = links.begin(); it != links.end(); it++){
		ei_x_encode_string_len(&x,(*it).c_str(),(*it).size());
	}
	ei_x_encode_empty_list(&x);

	return x;
}
Beispiel #16
0
 /* 
  * The format letters are:
  *   a  -  An atom
  *   c  -  A character
  *   s  -  A string
  *   i  -  An integer
  *   l  -  A long integer
  *   u  -  An unsigned long integer
  *   f  -  A float 
  *   d  -  A double float 
  *   p  -  An Erlang PID
  */
static int pformat(const char** fmt, union arg** args, ei_x_buff* x)
{
    int res = 0;
    ++(*fmt);	/* skip tilde */
    switch (*(*fmt)++) {
    case 'a': 
	res = ei_x_encode_atom(x, (*args)->s);
	(*args)++;
	break;
    case 'c':
	res = ei_x_encode_char(x, (*args)->c);
	(*args)++;
	break;
    case 's':
	res = ei_x_encode_string(x, (*args)->s);
	(*args)++;
	break;
    case 'i':
	res = ei_x_encode_long(x, (*args)->l);
	(*args)++;
	break;
    case 'l':
	res = ei_x_encode_long(x, (*args)->l);
	(*args)++;
	break;
    case 'u':
	res = ei_x_encode_ulong(x, (*args)->u);
	(*args)++;
	break;
    case 'f':     /* float is expanded to double (C calling conventions) */
    case 'd':
	res = ei_x_encode_double(x, (*args)->d);
	(*args)++;
	break;	
    case 'p':
	res = ei_x_encode_pid(x, (*args)->pid);
	(*args)++;
	break;
    default:
	res = -1;
	break;
    }
    return res;
}
Beispiel #17
0
int
make_declared_ns_in_buf(struct exmpp_xml_ctx *ctx, ei_x_buff *buf,
    const char *ns, int ns_len,
    const char *prefix, int prefix_len)
{

	/* Start declared namespace entry. */
	ei_x_encode_list_header(buf, 1);
	ei_x_encode_tuple_header(buf, 2);

	/* Check if the namespace is known and encode it. */
	encode_ns(ctx, buf, ns, ns_len);

	/* Encode prefix. */
	if (prefix != NULL)
		if (prefix_len == -1)
			ei_x_encode_string(buf, prefix);
		else
			ei_x_encode_string_len(buf, prefix, prefix_len);
	else
		ei_x_encode_atom(buf, "none");

	return (0);
}
Beispiel #18
0
int
make_attribute_legacy(struct exmpp_xml_ctx *ctx,
    const char *attr, int attr_len,
    const char *value, int value_len)
{
	ei_x_buff *tree;

	tree = ctx->current_tree;

	/* Start a simple tuple. */
	ei_x_encode_list_header(tree, 1);
	ei_x_encode_tuple_header(tree, 2);

	/* Check if the attribute is known and encode it. */
	encode_attr(ctx, tree, attr, attr_len);

	/* Encode the value. */
	if (value_len == -1)
		ei_x_encode_string(tree, value);
	else
		ei_x_encode_string_len(tree, value, value_len);

	return (0);
}
Beispiel #19
0
static int
exmpp_xml_control(ErlDrvData drv_data, unsigned int command,
    char *buf, int len, char **rbuf, int rlen)
{
	struct exmpp_xml_data *edd;
	ei_x_buff *to_return;
	ErlDrvBinary *bin;
	int size, ret;

	edd = (struct exmpp_xml_data *)drv_data;
	size = 0;
	bin = NULL;
	to_return = NULL;

	switch (command) {
	/*
	 * Parsing.
	 */

	case COMMAND_PARSE:
	case COMMAND_PARSE_FINAL:
		if (edd->parser == NULL) {
			/* Start a parser. */
			if (create_parser(edd) != 0) {
				to_return = exmpp_new_xbuf();
				if (to_return == NULL)
					return (-1);

				ret = RET_ERROR;
				ei_x_encode_atom(to_return,
				    "parser_setup_failed");

				break;
			}
		}

		/* Control the total size of data to parse. */
		if (!is_data_size_under_limit(&edd->ctx, len)) {
			to_return = exmpp_new_xbuf();
			if (to_return == NULL)
				return (-1);

			ret = RET_ERROR;
			ei_x_encode_atom(to_return, "stanza_too_big");

			break;
		}

		/* Run XML document parsing. */
		ret = XML_Parse(edd->parser, buf, len,
		    command == COMMAND_PARSE_FINAL);

		if (!ret) {
			enum XML_Error errcode;
			const char *errmsg;

			/* An error occured during parsing; most probably,
			 * XML wasn't well-formed. */
			errcode = XML_GetErrorCode(edd->parser);
			errmsg = XML_ErrorString(errcode);

			to_return = exmpp_new_xbuf();
			if (to_return == NULL)
				return (-1);

			ret = RET_ERROR;
			ei_x_encode_tuple_header(to_return, 2);
			ei_x_encode_atom(to_return, "parsing_failed");
			ei_x_encode_tuple_header(to_return, 2);
			ei_x_encode_long(to_return, errcode);
			ei_x_encode_string(to_return, errmsg);

			break;
		}

		/* Return the complete tree(s). */
		ret = RET_OK;
		if (edd->ctx.complete_trees_ready) {
			/* Terminate the complete trees list. */
			ei_x_encode_empty_list(edd->ctx.complete_trees);

			to_return = edd->ctx.complete_trees;
			size = 1 + to_return->index;
			bin = driver_alloc_binary(size);
			if (bin == NULL)
				return (-1);
			bin->orig_bytes[0] = (char)ret;
			memcpy(bin->orig_bytes + 1,
			    to_return->buff, to_return->index);
		} else {
			/* We need more data to produce a tree. */
			to_return = exmpp_new_xbuf();
			if (to_return == NULL)
				return (-1);

			ei_x_encode_atom(to_return,
			    command == COMMAND_PARSE ? "continue" : "done");
		}

		if (command == COMMAND_PARSE) {
			/* Update the size of processed data. */
			add_data_size(&edd->ctx, len);

			/* Reset the complete trees list. */
			reset_complete_trees(&edd->ctx);
		} else {
			/* We're done with the parser. */
			destroy_parser(edd);
		}

		break;

	case COMMAND_RESET_PARSER:
		if (edd->parser != NULL) {
			reset_context(&edd->ctx);
			XML_ParserReset(edd->parser, "UTF-8");
			init_parser(edd);
		}

		ret = RET_OK;

		break;

	/*
	 * Misc.
	 */

	case COMMAND_PORT_REVISION:
		/* Store the revision in the buffer. */
		to_return = exmpp_new_xbuf();
		if (to_return == NULL)
			return (-1);

		ret = RET_OK;
		ei_x_encode_string(to_return, "$Revision$");

		break;

	default:
		/* Other commands are handled in 'exmpp_xml.c' */
		to_return = exmpp_new_xbuf();
		if (to_return == NULL)
			return (-1);

		ret = control(&edd->ctx, command, buf, to_return);
		if (ret < 0)
			return (-1);
	}

	if (bin == NULL) {
		if (to_return != NULL) {
			size = 1 + to_return->index;
			bin = driver_alloc_binary(size);
			if (bin == NULL)
				return (-1);
			bin->orig_bytes[0] = (char)ret;
			if (to_return->index > 0)
				memcpy(bin->orig_bytes + 1,
				    to_return->buff, to_return->index);
			exmpp_free_xbuf(to_return);
		} else {
			/* The command called doesn't return anything. */
			size = 1;
			bin = driver_alloc_binary(size);
			bin->orig_bytes[0] = RET_OK;
		}
	}

	/* Set the returned buffer. */
	*rbuf = (char *)bin;

	/* Return the size of this buffer. */
	return (size);
}
Beispiel #20
0
void string(ei_x_buff *eixb, const char *str) {
  ei_x_encode_string(eixb, str);
}
Beispiel #21
0
int add_event_data(ei_x_buff* sendbuf, int topic, const void* data) {
    switch (topic) {
        case EVENT_COMMAND_FINISHED: {
            ei_x_encode_atom(sendbuf, "event_command_finished");
            struct SCommandFinishedEvent* event_data = (struct SCommandFinishedEvent*)data;
            ei_x_encode_tuple_header(sendbuf, 6);
            {
                if (ei_x_encode_atom(sendbuf, "unitid") < 0) {
                    fprintf(stderr, "cannot encode '%s' as 'atom'\n", "unitid");
                    return -1;
                }
                if (ei_x_encode_long(sendbuf, event_data->unitId) < 0) {
                    fprintf(stderr, "cannot encode '%i' as 'long'\n", event_data->unitId);
                    return -1;
                }
            }
            {
                if (ei_x_encode_atom(sendbuf, "commandid") < 0) {
                    fprintf(stderr, "cannot encode '%s' as 'atom'\n", "commandid");
                    return -1;
                }
                if (ei_x_encode_long(sendbuf, event_data->commandId) < 0) {
                    fprintf(stderr, "cannot encode '%i' as 'long'\n", event_data->commandId);
                    return -1;
                }
            }
            {
                if (ei_x_encode_atom(sendbuf, "commandtopicid") < 0) {
                    fprintf(stderr, "cannot encode '%s' as 'atom'\n", "commandtopicid");
                    return -1;
                }
                if (ei_x_encode_long(sendbuf, event_data->commandTopicId) < 0) {
                    fprintf(stderr, "cannot encode '%i' as 'long'\n", event_data->commandTopicId);
                    return -1;
                }
            }
            break;
        }
        case EVENT_ENEMY_CREATED: {
            ei_x_encode_atom(sendbuf, "event_enemy_created");
            struct SEnemyCreatedEvent* event_data = (struct SEnemyCreatedEvent*)data;
            ei_x_encode_tuple_header(sendbuf, 2);
            {
                if (ei_x_encode_atom(sendbuf, "enemy") < 0) {
                    fprintf(stderr, "cannot encode '%s' as 'atom'\n", "enemy");
                    return -1;
                }
                if (ei_x_encode_long(sendbuf, event_data->enemy) < 0) {
                    fprintf(stderr, "cannot encode '%i' as 'long'\n", event_data->enemy);
                    return -1;
                }
            }
            break;
        }
        case EVENT_ENEMY_DAMAGED: {
            ei_x_encode_atom(sendbuf, "event_enemy_damaged");
            struct SEnemyDamagedEvent* event_data = (struct SEnemyDamagedEvent*)data;
            ei_x_encode_tuple_header(sendbuf, 12);
            {
                if (ei_x_encode_atom(sendbuf, "enemy") < 0) {
                    fprintf(stderr, "cannot encode '%s' as 'atom'\n", "enemy");
                    return -1;
                }
                if (ei_x_encode_long(sendbuf, event_data->enemy) < 0) {
                    fprintf(stderr, "cannot encode '%i' as 'long'\n", event_data->enemy);
                    return -1;
                }
            }
            {
                if (ei_x_encode_atom(sendbuf, "attacker") < 0) {
                    fprintf(stderr, "cannot encode '%s' as 'atom'\n", "attacker");
                    return -1;
                }
                if (ei_x_encode_long(sendbuf, event_data->attacker) < 0) {
                    fprintf(stderr, "cannot encode '%i' as 'long'\n", event_data->attacker);
                    return -1;
                }
            }
            {
                if (ei_x_encode_atom(sendbuf, "damage") < 0) {
                    fprintf(stderr, "cannot encode '%s' as 'atom'\n", "damage");
                    return -1;
                }
                if (ei_x_encode_double(sendbuf, event_data->damage) < 0) {
                    fprintf(stderr, "cannot encode '%f' as 'double'\n", event_data->damage);
                    return -1;
                }
            }
            {
                if (ei_x_encode_atom(sendbuf, "dir_posf3") < 0) {
                    fprintf(stderr, "cannot encode '%s' as 'atom'\n", "dir_posf3");
                    return -1;
                }
                if (ei_x_encode_tuple_header(sendbuf, 3) < 0) {
                    fprintf(stderr, "cannot encode '%i' as 'tuple_header'\n", 3);
                    return -1;
                }
                if (ei_x_encode_double(sendbuf, event_data->dir_posF3[0]) < 0) {
                    fprintf(stderr, "cannot encode '%f' as 'double'\n", event_data->dir_posF3[0]);
                    return -1;
                }
                if (ei_x_encode_double(sendbuf, event_data->dir_posF3[1]) < 0) {
                    fprintf(stderr, "cannot encode '%f' as 'double'\n", event_data->dir_posF3[1]);
                    return -1;
                }
                if (ei_x_encode_double(sendbuf, event_data->dir_posF3[2]) < 0) {
                    fprintf(stderr, "cannot encode '%f' as 'double'\n", event_data->dir_posF3[2]);
                    return -1;
                }
            }
            {
                if (ei_x_encode_atom(sendbuf, "weapondefid") < 0) {
                    fprintf(stderr, "cannot encode '%s' as 'atom'\n", "weapondefid");
                    return -1;
                }
                if (ei_x_encode_long(sendbuf, event_data->weaponDefId) < 0) {
                    fprintf(stderr, "cannot encode '%i' as 'long'\n", event_data->weaponDefId);
                    return -1;
                }
            }
            {
                if (ei_x_encode_atom(sendbuf, "paralyzer") < 0) {
                    fprintf(stderr, "cannot encode '%s' as 'atom'\n", "paralyzer");
                    return -1;
                }
                if (ei_x_encode_atom(sendbuf, event_data->paralyzer ? "true" : "false") < 0) {
                    fprintf(stderr, "cannot encode '%s' as 'atom'\n", event_data->paralyzer ? "true" : "false");
                    return -1;
                }
            }
            break;
        }
        case EVENT_ENEMY_DESTROYED: {
            ei_x_encode_atom(sendbuf, "event_enemy_destroyed");
            struct SEnemyDestroyedEvent* event_data = (struct SEnemyDestroyedEvent*)data;
            ei_x_encode_tuple_header(sendbuf, 4);
            {
                if (ei_x_encode_atom(sendbuf, "enemy") < 0) {
                    fprintf(stderr, "cannot encode '%s' as 'atom'\n", "enemy");
                    return -1;
                }
                if (ei_x_encode_long(sendbuf, event_data->enemy) < 0) {
                    fprintf(stderr, "cannot encode '%i' as 'long'\n", event_data->enemy);
                    return -1;
                }
            }
            {
                if (ei_x_encode_atom(sendbuf, "attacker") < 0) {
                    fprintf(stderr, "cannot encode '%s' as 'atom'\n", "attacker");
                    return -1;
                }
                if (ei_x_encode_long(sendbuf, event_data->attacker) < 0) {
                    fprintf(stderr, "cannot encode '%i' as 'long'\n", event_data->attacker);
                    return -1;
                }
            }
            break;
        }
        case EVENT_ENEMY_ENTER_LOS: {
            ei_x_encode_atom(sendbuf, "event_enemy_enter_los");
            struct SEnemyEnterLOSEvent* event_data = (struct SEnemyEnterLOSEvent*)data;
            ei_x_encode_tuple_header(sendbuf, 2);
            {
                if (ei_x_encode_atom(sendbuf, "enemy") < 0) {
                    fprintf(stderr, "cannot encode '%s' as 'atom'\n", "enemy");
                    return -1;
                }
                if (ei_x_encode_long(sendbuf, event_data->enemy) < 0) {
                    fprintf(stderr, "cannot encode '%i' as 'long'\n", event_data->enemy);
                    return -1;
                }
            }
            break;
        }
        case EVENT_ENEMY_ENTER_RADAR: {
            ei_x_encode_atom(sendbuf, "event_enemy_enter_radar");
            struct SEnemyEnterRadarEvent* event_data = (struct SEnemyEnterRadarEvent*)data;
            ei_x_encode_tuple_header(sendbuf, 2);
            {
                if (ei_x_encode_atom(sendbuf, "enemy") < 0) {
                    fprintf(stderr, "cannot encode '%s' as 'atom'\n", "enemy");
                    return -1;
                }
                if (ei_x_encode_long(sendbuf, event_data->enemy) < 0) {
                    fprintf(stderr, "cannot encode '%i' as 'long'\n", event_data->enemy);
                    return -1;
                }
            }
            break;
        }
        case EVENT_ENEMY_FINISHED: {
            ei_x_encode_atom(sendbuf, "event_enemy_finished");
            struct SEnemyFinishedEvent* event_data = (struct SEnemyFinishedEvent*)data;
            ei_x_encode_tuple_header(sendbuf, 2);
            {
                if (ei_x_encode_atom(sendbuf, "enemy") < 0) {
                    fprintf(stderr, "cannot encode '%s' as 'atom'\n", "enemy");
                    return -1;
                }
                if (ei_x_encode_long(sendbuf, event_data->enemy) < 0) {
                    fprintf(stderr, "cannot encode '%i' as 'long'\n", event_data->enemy);
                    return -1;
                }
            }
            break;
        }
        case EVENT_ENEMY_LEAVE_LOS: {
            ei_x_encode_atom(sendbuf, "event_enemy_leave_los");
            struct SEnemyLeaveLOSEvent* event_data = (struct SEnemyLeaveLOSEvent*)data;
            ei_x_encode_tuple_header(sendbuf, 2);
            {
                if (ei_x_encode_atom(sendbuf, "enemy") < 0) {
                    fprintf(stderr, "cannot encode '%s' as 'atom'\n", "enemy");
                    return -1;
                }
                if (ei_x_encode_long(sendbuf, event_data->enemy) < 0) {
                    fprintf(stderr, "cannot encode '%i' as 'long'\n", event_data->enemy);
                    return -1;
                }
            }
            break;
        }
        case EVENT_ENEMY_LEAVE_RADAR: {
            ei_x_encode_atom(sendbuf, "event_enemy_leave_radar");
            struct SEnemyLeaveRadarEvent* event_data = (struct SEnemyLeaveRadarEvent*)data;
            ei_x_encode_tuple_header(sendbuf, 2);
            {
                if (ei_x_encode_atom(sendbuf, "enemy") < 0) {
                    fprintf(stderr, "cannot encode '%s' as 'atom'\n", "enemy");
                    return -1;
                }
                if (ei_x_encode_long(sendbuf, event_data->enemy) < 0) {
                    fprintf(stderr, "cannot encode '%i' as 'long'\n", event_data->enemy);
                    return -1;
                }
            }
            break;
        }
        case EVENT_LOAD: {
            ei_x_encode_atom(sendbuf, "event_load");
            struct SLoadEvent* event_data = (struct SLoadEvent*)data;
            ei_x_encode_tuple_header(sendbuf, 2);
            {
                if (ei_x_encode_atom(sendbuf, "file") < 0) {
                    fprintf(stderr, "cannot encode '%s' as 'atom'\n", "file");
                    return -1;
                }
                if (ei_x_encode_string(sendbuf, event_data->file) < 0) {
                    fprintf(stderr, "cannot encode '%s' as 'string'\n", event_data->file);
                    return -1;
                }
            }
            break;
        }
        case EVENT_LUA_MESSAGE: {
            ei_x_encode_atom(sendbuf, "event_lua_message");
            struct SLuaMessageEvent* event_data = (struct SLuaMessageEvent*)data;
            ei_x_encode_tuple_header(sendbuf, 2);
            {
                if (ei_x_encode_atom(sendbuf, "indata") < 0) {
                    fprintf(stderr, "cannot encode '%s' as 'atom'\n", "indata");
                    return -1;
                }
                if (ei_x_encode_string(sendbuf, event_data->inData) < 0) {
                    fprintf(stderr, "cannot encode '%s' as 'string'\n", event_data->inData);
                    return -1;
                }
            }
            break;
        }
        case EVENT_MESSAGE: {
            ei_x_encode_atom(sendbuf, "event_message");
            struct SMessageEvent* event_data = (struct SMessageEvent*)data;
            ei_x_encode_tuple_header(sendbuf, 4);
            {
                if (ei_x_encode_atom(sendbuf, "player") < 0) {
                    fprintf(stderr, "cannot encode '%s' as 'atom'\n", "player");
                    return -1;
                }
                if (ei_x_encode_long(sendbuf, event_data->player) < 0) {
                    fprintf(stderr, "cannot encode '%i' as 'long'\n", event_data->player);
                    return -1;
                }
            }
            {
                if (ei_x_encode_atom(sendbuf, "message") < 0) {
                    fprintf(stderr, "cannot encode '%s' as 'atom'\n", "message");
                    return -1;
                }
                if (ei_x_encode_string(sendbuf, event_data->message) < 0) {
                    fprintf(stderr, "cannot encode '%s' as 'string'\n", event_data->message);
                    return -1;
                }
            }
            break;
        }
        case EVENT_RELEASE: {
            ei_x_encode_atom(sendbuf, "event_release");
            struct SReleaseEvent* event_data = (struct SReleaseEvent*)data;
            ei_x_encode_tuple_header(sendbuf, 2);
            {
                if (ei_x_encode_atom(sendbuf, "reason") < 0) {
                    fprintf(stderr, "cannot encode '%s' as 'atom'\n", "reason");
                    return -1;
                }
                if (ei_x_encode_long(sendbuf, event_data->reason) < 0) {
                    fprintf(stderr, "cannot encode '%i' as 'long'\n", event_data->reason);
                    return -1;
                }
            }
            break;
        }
        case EVENT_SAVE: {
            ei_x_encode_atom(sendbuf, "event_save");
            struct SSaveEvent* event_data = (struct SSaveEvent*)data;
            ei_x_encode_tuple_header(sendbuf, 2);
            {
                if (ei_x_encode_atom(sendbuf, "file") < 0) {
                    fprintf(stderr, "cannot encode '%s' as 'atom'\n", "file");
                    return -1;
                }
                if (ei_x_encode_string(sendbuf, event_data->file) < 0) {
                    fprintf(stderr, "cannot encode '%s' as 'string'\n", event_data->file);
                    return -1;
                }
            }
            break;
        }
        case EVENT_SEISMIC_PING: {
            ei_x_encode_atom(sendbuf, "event_seismic_ping");
            struct SSeismicPingEvent* event_data = (struct SSeismicPingEvent*)data;
            ei_x_encode_tuple_header(sendbuf, 4);
            {
                if (ei_x_encode_atom(sendbuf, "pos_posf3") < 0) {
                    fprintf(stderr, "cannot encode '%s' as 'atom'\n", "pos_posf3");
                    return -1;
                }
                if (ei_x_encode_tuple_header(sendbuf, 3) < 0) {
                    fprintf(stderr, "cannot encode '%i' as 'tuple_header'\n", 3);
                    return -1;
                }
                if (ei_x_encode_double(sendbuf, event_data->pos_posF3[0]) < 0) {
                    fprintf(stderr, "cannot encode '%f' as 'double'\n", event_data->pos_posF3[0]);
                    return -1;
                }
                if (ei_x_encode_double(sendbuf, event_data->pos_posF3[1]) < 0) {
                    fprintf(stderr, "cannot encode '%f' as 'double'\n", event_data->pos_posF3[1]);
                    return -1;
                }
                if (ei_x_encode_double(sendbuf, event_data->pos_posF3[2]) < 0) {
                    fprintf(stderr, "cannot encode '%f' as 'double'\n", event_data->pos_posF3[2]);
                    return -1;
                }
            }
            {
                if (ei_x_encode_atom(sendbuf, "strength") < 0) {
                    fprintf(stderr, "cannot encode '%s' as 'atom'\n", "strength");
                    return -1;
                }
                if (ei_x_encode_double(sendbuf, event_data->strength) < 0) {
                    fprintf(stderr, "cannot encode '%f' as 'double'\n", event_data->strength);
                    return -1;
                }
            }
            break;
        }
        case EVENT_UNIT_CAPTURED: {
            ei_x_encode_atom(sendbuf, "event_unit_captured");
            struct SUnitCapturedEvent* event_data = (struct SUnitCapturedEvent*)data;
            ei_x_encode_tuple_header(sendbuf, 6);
            {
                if (ei_x_encode_atom(sendbuf, "unitid") < 0) {
                    fprintf(stderr, "cannot encode '%s' as 'atom'\n", "unitid");
                    return -1;
                }
                if (ei_x_encode_long(sendbuf, event_data->unitId) < 0) {
                    fprintf(stderr, "cannot encode '%i' as 'long'\n", event_data->unitId);
                    return -1;
                }
            }
            {
                if (ei_x_encode_atom(sendbuf, "oldteamid") < 0) {
                    fprintf(stderr, "cannot encode '%s' as 'atom'\n", "oldteamid");
                    return -1;
                }
                if (ei_x_encode_long(sendbuf, event_data->oldTeamId) < 0) {
                    fprintf(stderr, "cannot encode '%i' as 'long'\n", event_data->oldTeamId);
                    return -1;
                }
            }
            {
                if (ei_x_encode_atom(sendbuf, "newteamid") < 0) {
                    fprintf(stderr, "cannot encode '%s' as 'atom'\n", "newteamid");
                    return -1;
                }
                if (ei_x_encode_long(sendbuf, event_data->newTeamId) < 0) {
                    fprintf(stderr, "cannot encode '%i' as 'long'\n", event_data->newTeamId);
                    return -1;
                }
            }
            break;
        }
        case EVENT_UNIT_CREATED: {
            ei_x_encode_atom(sendbuf, "event_unit_created");
            struct SUnitCreatedEvent* event_data = (struct SUnitCreatedEvent*)data;
            ei_x_encode_tuple_header(sendbuf, 4);
            {
                if (ei_x_encode_atom(sendbuf, "unit") < 0) {
                    fprintf(stderr, "cannot encode '%s' as 'atom'\n", "unit");
                    return -1;
                }
                if (ei_x_encode_long(sendbuf, event_data->unit) < 0) {
                    fprintf(stderr, "cannot encode '%i' as 'long'\n", event_data->unit);
                    return -1;
                }
            }
            {
                if (ei_x_encode_atom(sendbuf, "builder") < 0) {
                    fprintf(stderr, "cannot encode '%s' as 'atom'\n", "builder");
                    return -1;
                }
                if (ei_x_encode_long(sendbuf, event_data->builder) < 0) {
                    fprintf(stderr, "cannot encode '%i' as 'long'\n", event_data->builder);
                    return -1;
                }
            }
            break;
        }
        case EVENT_UNIT_DAMAGED: {
            ei_x_encode_atom(sendbuf, "event_unit_damaged");
            struct SUnitDamagedEvent* event_data = (struct SUnitDamagedEvent*)data;
            ei_x_encode_tuple_header(sendbuf, 12);
            {
                if (ei_x_encode_atom(sendbuf, "unit") < 0) {
                    fprintf(stderr, "cannot encode '%s' as 'atom'\n", "unit");
                    return -1;
                }
                if (ei_x_encode_long(sendbuf, event_data->unit) < 0) {
                    fprintf(stderr, "cannot encode '%i' as 'long'\n", event_data->unit);
                    return -1;
                }
            }
            {
                if (ei_x_encode_atom(sendbuf, "attacker") < 0) {
                    fprintf(stderr, "cannot encode '%s' as 'atom'\n", "attacker");
                    return -1;
                }
                if (ei_x_encode_long(sendbuf, event_data->attacker) < 0) {
                    fprintf(stderr, "cannot encode '%i' as 'long'\n", event_data->attacker);
                    return -1;
                }
            }
            {
                if (ei_x_encode_atom(sendbuf, "damage") < 0) {
                    fprintf(stderr, "cannot encode '%s' as 'atom'\n", "damage");
                    return -1;
                }
                if (ei_x_encode_double(sendbuf, event_data->damage) < 0) {
                    fprintf(stderr, "cannot encode '%f' as 'double'\n", event_data->damage);
                    return -1;
                }
            }
            {
                if (ei_x_encode_atom(sendbuf, "dir_posf3") < 0) {
                    fprintf(stderr, "cannot encode '%s' as 'atom'\n", "dir_posf3");
                    return -1;
                }
                if (ei_x_encode_tuple_header(sendbuf, 3) < 0) {
                    fprintf(stderr, "cannot encode '%i' as 'tuple_header'\n", 3);
                    return -1;
                }
                if (ei_x_encode_double(sendbuf, event_data->dir_posF3[0]) < 0) {
                    fprintf(stderr, "cannot encode '%f' as 'double'\n", event_data->dir_posF3[0]);
                    return -1;
                }
                if (ei_x_encode_double(sendbuf, event_data->dir_posF3[1]) < 0) {
                    fprintf(stderr, "cannot encode '%f' as 'double'\n", event_data->dir_posF3[1]);
                    return -1;
                }
                if (ei_x_encode_double(sendbuf, event_data->dir_posF3[2]) < 0) {
                    fprintf(stderr, "cannot encode '%f' as 'double'\n", event_data->dir_posF3[2]);
                    return -1;
                }
            }
            {
                if (ei_x_encode_atom(sendbuf, "weapondefid") < 0) {
                    fprintf(stderr, "cannot encode '%s' as 'atom'\n", "weapondefid");
                    return -1;
                }
                if (ei_x_encode_long(sendbuf, event_data->weaponDefId) < 0) {
                    fprintf(stderr, "cannot encode '%i' as 'long'\n", event_data->weaponDefId);
                    return -1;
                }
            }
            {
                if (ei_x_encode_atom(sendbuf, "paralyzer") < 0) {
                    fprintf(stderr, "cannot encode '%s' as 'atom'\n", "paralyzer");
                    return -1;
                }
                if (ei_x_encode_atom(sendbuf, event_data->paralyzer ? "true" : "false") < 0) {
                    fprintf(stderr, "cannot encode '%s' as 'atom'\n", event_data->paralyzer ? "true" : "false");
                    return -1;
                }
            }
            break;
        }
        case EVENT_UNIT_DESTROYED: {
            ei_x_encode_atom(sendbuf, "event_unit_destroyed");
            struct SUnitDestroyedEvent* event_data = (struct SUnitDestroyedEvent*)data;
            ei_x_encode_tuple_header(sendbuf, 4);
            {
                if (ei_x_encode_atom(sendbuf, "unit") < 0) {
                    fprintf(stderr, "cannot encode '%s' as 'atom'\n", "unit");
                    return -1;
                }
                if (ei_x_encode_long(sendbuf, event_data->unit) < 0) {
                    fprintf(stderr, "cannot encode '%i' as 'long'\n", event_data->unit);
                    return -1;
                }
            }
            {
                if (ei_x_encode_atom(sendbuf, "attacker") < 0) {
                    fprintf(stderr, "cannot encode '%s' as 'atom'\n", "attacker");
                    return -1;
                }
                if (ei_x_encode_long(sendbuf, event_data->attacker) < 0) {
                    fprintf(stderr, "cannot encode '%i' as 'long'\n", event_data->attacker);
                    return -1;
                }
            }
            break;
        }
        case EVENT_UNIT_FINISHED: {
            ei_x_encode_atom(sendbuf, "event_unit_finished");
            struct SUnitFinishedEvent* event_data = (struct SUnitFinishedEvent*)data;
            ei_x_encode_tuple_header(sendbuf, 2);
            {
                if (ei_x_encode_atom(sendbuf, "unit") < 0) {
                    fprintf(stderr, "cannot encode '%s' as 'atom'\n", "unit");
                    return -1;
                }
                if (ei_x_encode_long(sendbuf, event_data->unit) < 0) {
                    fprintf(stderr, "cannot encode '%i' as 'long'\n", event_data->unit);
                    return -1;
                }
            }
            break;
        }
        case EVENT_UNIT_GIVEN: {
            ei_x_encode_atom(sendbuf, "event_unit_given");
            struct SUnitGivenEvent* event_data = (struct SUnitGivenEvent*)data;
            ei_x_encode_tuple_header(sendbuf, 6);
            {
                if (ei_x_encode_atom(sendbuf, "unitid") < 0) {
                    fprintf(stderr, "cannot encode '%s' as 'atom'\n", "unitid");
                    return -1;
                }
                if (ei_x_encode_long(sendbuf, event_data->unitId) < 0) {
                    fprintf(stderr, "cannot encode '%i' as 'long'\n", event_data->unitId);
                    return -1;
                }
            }
            {
                if (ei_x_encode_atom(sendbuf, "oldteamid") < 0) {
                    fprintf(stderr, "cannot encode '%s' as 'atom'\n", "oldteamid");
                    return -1;
                }
                if (ei_x_encode_long(sendbuf, event_data->oldTeamId) < 0) {
                    fprintf(stderr, "cannot encode '%i' as 'long'\n", event_data->oldTeamId);
                    return -1;
                }
            }
            {
                if (ei_x_encode_atom(sendbuf, "newteamid") < 0) {
                    fprintf(stderr, "cannot encode '%s' as 'atom'\n", "newteamid");
                    return -1;
                }
                if (ei_x_encode_long(sendbuf, event_data->newTeamId) < 0) {
                    fprintf(stderr, "cannot encode '%i' as 'long'\n", event_data->newTeamId);
                    return -1;
                }
            }
            break;
        }
        case EVENT_UNIT_IDLE: {
            ei_x_encode_atom(sendbuf, "event_unit_idle");
            struct SUnitIdleEvent* event_data = (struct SUnitIdleEvent*)data;
            ei_x_encode_tuple_header(sendbuf, 2);
            {
                if (ei_x_encode_atom(sendbuf, "unit") < 0) {
                    fprintf(stderr, "cannot encode '%s' as 'atom'\n", "unit");
                    return -1;
                }
                if (ei_x_encode_long(sendbuf, event_data->unit) < 0) {
                    fprintf(stderr, "cannot encode '%i' as 'long'\n", event_data->unit);
                    return -1;
                }
            }
            break;
        }
        case EVENT_UNIT_MOVE_FAILED: {
            ei_x_encode_atom(sendbuf, "event_unit_move_failed");
            struct SUnitMoveFailedEvent* event_data = (struct SUnitMoveFailedEvent*)data;
            ei_x_encode_tuple_header(sendbuf, 2);
            {
                if (ei_x_encode_atom(sendbuf, "unit") < 0) {
                    fprintf(stderr, "cannot encode '%s' as 'atom'\n", "unit");
                    return -1;
                }
                if (ei_x_encode_long(sendbuf, event_data->unit) < 0) {
                    fprintf(stderr, "cannot encode '%i' as 'long'\n", event_data->unit);
                    return -1;
                }
            }
            break;
        }
        case EVENT_UPDATE: {
            ei_x_encode_atom(sendbuf, "event_update");
            struct SUpdateEvent* event_data = (struct SUpdateEvent*)data;
            ei_x_encode_tuple_header(sendbuf, 2);
            {
                if (ei_x_encode_atom(sendbuf, "frame") < 0) {
                    fprintf(stderr, "cannot encode '%s' as 'atom'\n", "frame");
                    return -1;
                }
                if (ei_x_encode_long(sendbuf, event_data->frame) < 0) {
                    fprintf(stderr, "cannot encode '%i' as 'long'\n", event_data->frame);
                    return -1;
                }
            }
            break;
        }
        case EVENT_WEAPON_FIRED: {
            ei_x_encode_atom(sendbuf, "event_weapon_fired");
            struct SWeaponFiredEvent* event_data = (struct SWeaponFiredEvent*)data;
            ei_x_encode_tuple_header(sendbuf, 4);
            {
                if (ei_x_encode_atom(sendbuf, "unitid") < 0) {
                    fprintf(stderr, "cannot encode '%s' as 'atom'\n", "unitid");
                    return -1;
                }
                if (ei_x_encode_long(sendbuf, event_data->unitId) < 0) {
                    fprintf(stderr, "cannot encode '%i' as 'long'\n", event_data->unitId);
                    return -1;
                }
            }
            {
                if (ei_x_encode_atom(sendbuf, "weapondefid") < 0) {
                    fprintf(stderr, "cannot encode '%s' as 'atom'\n", "weapondefid");
                    return -1;
                }
                if (ei_x_encode_long(sendbuf, event_data->weaponDefId) < 0) {
                    fprintf(stderr, "cannot encode '%i' as 'long'\n", event_data->weaponDefId);
                    return -1;
                }
            }
            break;
        }
        default: {
            ei_x_encode_long(sendbuf, topic);
            ei_x_encode_binary(sendbuf, data, sizeof(data));
        }
    }
    return 0;
}
Beispiel #22
0
Datei: writer.c Projekt: ion1/joo
int
joo_write (int      fd,
           JooTerm *terms)
{
  int ret = 0;

  ei_x_buff buf;

  if (joo_term_count_children (terms) < 0)
    return -1;

  if (ei_x_new_with_version (&buf)) {
    errno = ENOMEM;
    return -1;
  }

  for (JooTerm *term = terms; term->type != JOO_END; term++) {
    if (term->push) {
      errno = EINVAL;
      return -1;
    }

    switch (term->type) {
      case JOO_ATOM:
        if (ei_x_encode_atom (&buf, term->value.text)) {
          errno = ENOMEM;
          return -1;
        }
        break;

      case JOO_STRING:
        if (ei_x_encode_string (&buf, term->value.text)) {
          errno = ENOMEM;
          return -1;
        }
        break;

      case JOO_BINARY:
        if (ei_x_encode_binary (&buf, term->value.binary.data,
                                term->value.binary.size)) {
          errno = ENOMEM;
          return -1;
        }
        break;

      case JOO_TUPLE_START:
        if (ei_x_encode_tuple_header (&buf, term->num_children)) {
          errno = ENOMEM;
          return -1;
        }
        break;

      case JOO_TUPLE_END:
        break;

      default:
        abort ();
    }
  }

  if (write_ei_x_buff (fd, &buf) < 0) {
    errno = EIO;
    ret = -1;
  }

  ei_x_free (&buf);

  return ret;
}
Beispiel #23
0
int main(void)
#endif
{
  ErlConnect conp;
  Erl_IpAddr thisipaddr = (Erl_IpAddr)0;
  FILE *fp = (FILE *)0;
  char* charp = "foo";
  double *doublep = NULL;
  double doublex = 0.0;
  ei_cnode xec;
  ei_reg *ei_regp = NULL;
  ei_term eterm;
  ei_x_buff eix;
  erlang_big *bigp = NULL;
  erlang_fun efun;
  erlang_msg *msgp = NULL;
  erlang_msg emsg;
  erlang_pid *pidp = NULL;
  erlang_pid epid;
  erlang_port eport;
  erlang_ref eref;
  erlang_trace etrace;
  int *intp = NULL;
  int intx = 0;
  long *longp = NULL;
  long longx = 0;
  short creation = 0;
  struct ei_reg_stat *ei_reg_statp = NULL;
  struct ei_reg_tabstat *ei_reg_tabstatp = NULL;
  struct hostent *hostp = NULL;
  unsigned char * ucharp = (unsigned char *)"foo";
  unsigned long *ulongp = NULL;
  unsigned long ulongx = 0;
  void *voidp = NULL;
#ifndef VXWORKS
  EI_LONGLONG *longlongp = (EI_LONGLONG*)NULL;
  EI_LONGLONG longlongx = 0;
  EI_ULONGLONG *ulonglongp = (EI_ULONGLONG*)NULL;
  EI_ULONGLONG ulonglongx = 0;
#endif
  enum erlang_char_encoding enc;

  intx = erl_errno;

  ei_connect_init(&xec, charp, charp, creation);
  ei_connect_xinit (&xec, charp, charp, charp, thisipaddr, charp, creation);

  ei_connect(&xec, charp);
  ei_xconnect (&xec, thisipaddr, charp);

  ei_receive(intx, ucharp, intx);
  ei_receive_msg(intx, &emsg, &eix);
  ei_xreceive_msg(intx, &emsg, &eix);

  ei_send(intx, &epid, charp, intx);
  ei_reg_send(&xec, intx, charp, charp, intx);

  ei_rpc(&xec, intx, charp, charp, charp, intx, &eix);
  ei_rpc_to(&xec, intx, charp, charp, charp, intx);
  ei_rpc_from(&xec, intx, intx, &emsg, &eix);

  ei_publish(&xec, intx);
  ei_accept(&xec, intx, &conp);
  ei_unpublish(&xec);

  ei_thisnodename(&xec);
  ei_thishostname(&xec);
  ei_thisalivename(&xec);

  ei_self(&xec);

  ei_gethostbyname(charp);
  ei_gethostbyaddr(charp, intx, intx);
  ei_gethostbyname_r(charp, hostp, charp, intx, intp);
  ei_gethostbyaddr_r(charp, intx, intx, hostp, charp, intx, intp);

  ei_encode_version(charp, intp);
  ei_x_encode_version(&eix);
  ei_encode_long(charp, intp, longx);
  ei_x_encode_long(&eix, longx);
  ei_encode_ulong(charp, intp, ulongx);
  ei_x_encode_ulong(&eix, ulongx);
  ei_encode_double(charp, intp, doublex);
  ei_x_encode_double(&eix, doublex);
  ei_encode_boolean(charp, intp, intx);
  ei_x_encode_boolean(&eix, intx);
  ei_encode_char(charp, intp, 'a');
  ei_x_encode_char(&eix, 'a');
  ei_encode_string(charp, intp, charp);
  ei_encode_string_len(charp, intp, charp, intx);
  ei_x_encode_string(&eix, charp);
  ei_x_encode_string_len(&eix, charp, intx);
  ei_encode_atom(charp, intp, charp);
  ei_encode_atom_as(charp, intp, charp, ERLANG_LATIN1, ERLANG_UTF8);
  ei_encode_atom_len(charp, intp, charp, intx);
  ei_encode_atom_len_as(charp, intp, charp, intx, ERLANG_ASCII, ERLANG_LATIN1);
  ei_x_encode_atom(&eix, charp);
  ei_x_encode_atom_as(&eix, charp, ERLANG_LATIN1, ERLANG_UTF8);
  ei_x_encode_atom_len(&eix, charp, intx);
  ei_x_encode_atom_len_as(&eix, charp, intx, ERLANG_LATIN1, ERLANG_UTF8);
  ei_encode_binary(charp, intp, (void *)0, longx);
  ei_x_encode_binary(&eix, (void*)0, intx);
  ei_encode_pid(charp, intp, &epid);
  ei_x_encode_pid(&eix, &epid);
  ei_encode_fun(charp, intp, &efun);
  ei_x_encode_fun(&eix, &efun);
  ei_encode_port(charp, intp, &eport);
  ei_x_encode_port(&eix, &eport);
  ei_encode_ref(charp, intp, &eref);
  ei_x_encode_ref(&eix, &eref);
  ei_encode_trace(charp, intp, &etrace);
  ei_x_encode_trace(&eix, &etrace);
  ei_encode_tuple_header(charp, intp, intx);
  ei_x_encode_tuple_header(&eix, longx);
  ei_encode_list_header(charp, intp, intx);
  ei_x_encode_list_header(&eix, longx);
/* #define ei_encode_empty_list(buf,i) ei_encode_list_header(buf,i,0) */
  ei_x_encode_empty_list(&eix);

  ei_get_type(charp, intp, intp, intp);
  ei_get_type_internal(charp, intp, intp, intp);

  ei_decode_version(charp, intp, intp);
  ei_decode_long(charp, intp, longp);
  ei_decode_ulong(charp, intp, ulongp);
  ei_decode_double(charp, intp, doublep);
  ei_decode_boolean(charp, intp, intp);
  ei_decode_char(charp, intp, charp);
  ei_decode_string(charp, intp, charp);
  ei_decode_atom(charp, intp, charp);
  ei_decode_atom_as(charp, intp, charp, MAXATOMLEN_UTF8, ERLANG_WHATEVER, &enc, &enc);
  ei_decode_binary(charp, intp, (void *)0, longp);
  ei_decode_fun(charp, intp, &efun);
  free_fun(&efun);
  ei_decode_pid(charp, intp, &epid);
  ei_decode_port(charp, intp, &eport);
  ei_decode_ref(charp, intp, &eref);
  ei_decode_trace(charp, intp, &etrace);
  ei_decode_tuple_header(charp, intp, intp);
  ei_decode_list_header(charp, intp, intp);

  ei_decode_ei_term(charp, intp, &eterm);

  ei_print_term(fp, charp, intp);
  ei_s_print_term(&charp, charp, intp);

  ei_x_format(&eix, charp);
  ei_x_format_wo_ver(&eix, charp);

  ei_x_new(&eix);
  ei_x_new_with_version(&eix);
  ei_x_free(&eix);
  ei_x_append(&eix, &eix);
  ei_x_append_buf(&eix, charp, intx);
  ei_skip_term(charp, intp);

  ei_reg_open(intx);
  ei_reg_resize(ei_regp, intx);
  ei_reg_close(ei_regp);

  ei_reg_setival(ei_regp, charp, longx);
  ei_reg_setfval(ei_regp, charp, doublex);
  ei_reg_setsval(ei_regp, charp, charp);
  ei_reg_setpval(ei_regp, charp, voidp, intx);

  ei_reg_setval(ei_regp, charp, intx);

  ei_reg_getival(ei_regp, charp);
  ei_reg_getfval(ei_regp, charp);
  ei_reg_getsval(ei_regp, charp);
  ei_reg_getpval(ei_regp, charp, intp);

  ei_reg_getval(ei_regp, charp, intx);

  ei_reg_markdirty(ei_regp, charp);

  ei_reg_delete(ei_regp, charp);

  ei_reg_stat(ei_regp, charp, ei_reg_statp);

  ei_reg_tabstat(ei_regp, ei_reg_tabstatp);

  ei_reg_dump(intx, ei_regp, charp, intx);
  ei_reg_restore(intx, ei_regp, charp);
  ei_reg_purge(ei_regp);

#if defined(HAVE_GMP_H) && defined(HAVE_LIBGMP)
  {
      mpz_t obj;
      ei_decode_bignum(charp, intp, obj);
      ei_encode_bignum(charp, intp, obj);
      ei_x_encode_bignum(&eix, obj);
  }
#endif /* HAVE_GMP_H && HAVE_LIBGMP */

#ifndef VXWORKS

  ei_decode_longlong(charp, intp, longlongp);
  ei_decode_ulonglong(charp, intp, ulonglongp);
  ei_encode_longlong(charp, intp, longlongx);
  ei_encode_ulonglong(charp, intp, ulonglongx);
  ei_x_encode_longlong(&eix, longlongx);
  ei_x_encode_ulonglong(&eix, ulonglongx);

#endif

#ifdef USE_EI_UNDOCUMENTED

  ei_decode_intlist(charp, intp, longp, intp);

  ei_receive_encoded(intx, &charp, intp, msgp, intp);
  ei_send_encoded(intx, pidp, charp, intx);
  ei_send_reg_encoded(intx, pidp, charp, charp, intx);

  ei_decode_big(charp, intp, bigp);
  ei_big_comp(bigp, bigp);
  ei_big_to_double(bigp, doublep);
  ei_small_to_big(intx, bigp);
  ei_alloc_big(intx);
  ei_free_big(bigp);

#endif /* USE_EI_UNDOCUMENTED */

  return
      BUFSIZ +
      EAGAIN +
      EHOSTUNREACH +
      EIO +
      EI_BIN +
      EI_DELET +
      EI_DIRTY +
      EI_FLT +
      EI_FORCE +
      EI_INT +
      EI_NOPURGE +
      EI_STR +
      EMSGSIZE +
      ENOMEM +
      ERL_ERROR +
      ERL_EXIT +
      ERL_LINK +
      ERL_MSG +
      ERL_NO_TIMEOUT +
      ERL_REG_SEND +
      ERL_SEND +
      ERL_TICK +
      ERL_TIMEOUT +
      ERL_UNLINK +
      ETIMEDOUT +
      MAXATOMLEN;
}
Beispiel #24
0
static int accept_sec_context(char *buf, int index, ei_x_buff *presult)
{
    ei_x_buff result = *presult;

    /*
      {accept_sec_context, {Idx, In}} ->
      {ok, {Idx, Name, CCName, Out}} |
      {needsmore, {Idx, Out}}
    */

    int arity;
    gss_buffer_desc in;
    gss_buffer_desc out;
    gss_buffer_desc name;
    int res;
    char *ccname = NULL;
    long idx;
    OM_uint32 min_stat;

    memset(&in, 0, sizeof(in));
    memset(&out, 0, sizeof(out));
    memset(&name, 0, sizeof(name));

    EI(ei_decode_tuple_header(buf, &index, &arity));
    
    EI(arity != 2);

    EI(ei_decode_long(buf, &index, &idx));

    EI(decode_gssapi_binary(buf, &index, &in));

    if (idx < 0) {
	idx = session_find_free();
	if (idx < 0) ENCODE_ERROR("no_mem");
	g_sessions[idx] = GSS_C_NO_CONTEXT;
    } else {
	if (idx < 0 || idx >= MAX_SESSIONS || !g_sessions[idx])
	    ENCODE_ERROR("bad_instance");
    }

    res = accept_user(&g_sessions[idx], &in, &out, &name, &ccname);

    if (!GSS_ERROR(res)) {
	if (res & GSS_S_CONTINUE_NEEDED) {
	    EI(ei_x_encode_atom(&result, "needsmore") ||
	       ei_x_encode_tuple_header(&result, 2) ||
	       ei_x_encode_long(&result, idx) ||
	       ei_x_encode_binary(&result, out.value, out.length)
		);
	} else {
	    const char *ret_ccname = ccname;
	    if (!ret_ccname)
		ret_ccname = "";

	    EI(ei_x_encode_atom(&result, "ok") ||
	       ei_x_encode_tuple_header(&result, 4) ||
	       ei_x_encode_long(&result, idx) ||
	       ei_x_encode_string_len(&result, name.value, name.length) ||
	       ei_x_encode_string(&result, ret_ccname) ||
	       ei_x_encode_binary(&result, out.value, out.length)
		);

	}
    } else {
	EI(ei_x_encode_atom(&result, "error") || ei_x_encode_atom(&result, "unauthorized"));
    }

error:
    if (ccname)
	free(ccname);

    if (in.value)
	gss_release_buffer(&min_stat, &in);

    if (out.value)
	gss_release_buffer(&min_stat, &out);

    if (name.value)
	gss_release_buffer(&min_stat, &name);

    *presult = result;
    return 0;
}
int srdb1_encode_kv(int tupsize,const db_key_t* _k, const db_op_t* _op, const db_val_t* _v,
				const int _n, ei_x_buff *argbuf) {
	int i;
	struct tm* tt;
	time_t t_t;

	if(_k) {
	    ei_x_encode_list_header(argbuf, _n);
	    for(i = 0; i < _n; i++) {
		db_val_t *vv;
		ei_x_encode_tuple_header(argbuf, tupsize);
		ei_x_encode_atom_len(argbuf,_k[i]->s,_k[i]->len);
		if(tupsize == 3 ) {
		    if (_op) {
			ei_x_encode_atom(argbuf,_op[i]);
		    } else {
			ei_x_encode_atom(argbuf,"=");
		    }
		}
		vv=&(_v[i]);
		if (VAL_NULL(vv)) {
		    ei_x_encode_atom(argbuf,"undefined");
		} else {
		    switch(VAL_TYPE(vv)) {
			case DB1_INT:
			    ei_x_encode_ulong(argbuf, VAL_INT(vv));
			    break;
			case DB1_BIGINT:
			    ei_x_encode_longlong(argbuf, VAL_BIGINT(vv));
			    break;
			case DB1_DOUBLE:
			    ei_x_encode_double(argbuf, VAL_DOUBLE(vv));
			    break;
			case DB1_STRING:
			    ei_x_encode_string(argbuf,VAL_STRING(vv));
			    break;
			case DB1_STR:
			    ei_x_encode_string_len(argbuf,VAL_STR(vv).s,VAL_STR(vv).len);
			    break;
			case DB1_DATETIME:
			    t_t=VAL_TIME(vv);
			    tt= localtime(&t_t);
			    ei_x_encode_tuple_header(argbuf, 2);
			    ei_x_encode_tuple_header(argbuf, 3);
			    ei_x_encode_long(argbuf, tt->tm_year + 1900);
			    ei_x_encode_long(argbuf, tt->tm_mon +1);
			    ei_x_encode_long(argbuf, tt->tm_mday);
			    ei_x_encode_tuple_header(argbuf, 3);
			    ei_x_encode_long(argbuf, tt->tm_hour);
			    ei_x_encode_long(argbuf, tt->tm_min);
			    ei_x_encode_long(argbuf, tt->tm_sec);
			    break;
			case DB1_BLOB:
			    ei_x_encode_binary(argbuf,VAL_BLOB(vv).s,VAL_BLOB(vv).len);
			    break;
			case DB1_BITMAP:
			    ei_x_encode_ulong(argbuf,VAL_BITMAP(vv));
			    break;
		    }
		}
	    }
	    ei_x_encode_empty_list(argbuf);
	} else {
	    ei_x_encode_list_header(argbuf, 0);
	}
	return 0;
}