Skip to content

Commit

Permalink
get rid of the error() function
Browse files Browse the repository at this point in the history
  • Loading branch information
esmil committed Mar 10, 2009
1 parent 69b1284 commit b1b4968
Show file tree
Hide file tree
Showing 4 changed files with 76 additions and 60 deletions.
2 changes: 0 additions & 2 deletions add.c
Expand Up @@ -21,8 +21,6 @@
#include <lauxlib.h>
#include <dbus/dbus.h>

#include "simpledbus.h"

#define EXPORT
#endif

Expand Down
22 changes: 13 additions & 9 deletions parse.c
Expand Up @@ -22,8 +22,6 @@
#include <lua.h>
#include <expat.h>

#include "simpledbus.h"

#define EXPORT

#endif
Expand Down Expand Up @@ -209,8 +207,11 @@ EXPORT int proxy_parse(lua_State *L)

/* create parser and initialise it */
p = XML_ParserCreate("UTF-8");
if (!p)
return error(L, "Couldn't allocate memory for parser");
if (!p) {
lua_pushnil(L);
lua_pushliteral(L, "Couldn't allocate memory for parser");
return 2;
}

data.L = L;
data.level = 0;
Expand All @@ -233,13 +234,16 @@ EXPORT int proxy_parse(lua_State *L)
lua_getfield(L, 1, "object");

/* now parse the xml document inserting methods as we go */
if (!XML_Parse(p, xml, strlen(xml), 1))
return error(L, "Error parsing introspection data");
/*
if (!XML_Parse(p, xml, strlen(xml), 1)) {
#ifdef DEBUG
fprintf(stderr, "Parse error at line %d:\n%s\n",
(int) XML_GetCurrentLineNumber(p),
(int)XML_GetCurrentLineNumber(p),
XML_ErrorString(XML_GetErrorCode(p)));
*/
#endif
lua_pushnil(L);
lua_pushliteral(L, "Error parsing introspection data");
return 2;
}

/* pop the object name */
lua_pop(L, 1);
Expand Down
105 changes: 63 additions & 42 deletions simpledbus.c
Expand Up @@ -45,18 +45,6 @@ static DBusObjectPathVTable vtable;
static lua_State *mainThread = NULL;
static unsigned int stop;

EXPORT int error(lua_State *L, const char *fmt, ...)
{
va_list ap;

lua_pushnil(L);
va_start(ap, fmt);
lua_pushvfstring(L, fmt, ap);
va_end(ap);

return 2;
}

#ifdef ALLINONE
#include "add.c"
#include "push.c"
Expand Down Expand Up @@ -238,9 +226,11 @@ static void method_return_handler(DBusPendingCall *pending, lua_State *T)
c = lua_touserdata(T, -1);
lua_pop(T, 1);

if (msg == NULL)
nargs = error(T, "Reply null");
else {
if (msg == NULL) {
lua_pushnil(T);
lua_pushliteral(T, "Reply null");
nargs = 2;
} else {
switch (dbus_message_get_type(msg)) {
case DBUS_MESSAGE_TYPE_METHOD_RETURN:
nargs = push_arguments(T, msg);
Expand All @@ -255,7 +245,9 @@ static void method_return_handler(DBusPendingCall *pending, lua_State *T)
break;
default:
dbus_message_unref(msg);
nargs = error(T, "Unknown reply");
lua_pushnil(T);
lua_pushliteral(T, "Uknown reply");
nargs = 2;
}
}

Expand Down Expand Up @@ -325,8 +317,11 @@ static int bus_call_method(lua_State *L)
lua_tostring(L, 3),
interface,
lua_tostring(L, 5));
if (msg == NULL)
return error(L, "Couldn't create message");
if (msg == NULL) {
lua_pushnil(L);
lua_pushliteral(L, "Out of memory");
return 2;
}

/* get the signature and add arguments */
if (lua_isstring(L, 6)) {
Expand All @@ -339,13 +334,19 @@ static int bus_call_method(lua_State *L)
if (mainThread) { /* main loop is running */
DBusPendingCall *pending;

if (!dbus_connection_send_with_reply(c->conn, msg, &pending, -1))
return error(L, "Out of memory");
if (!dbus_connection_send_with_reply(c->conn, msg, &pending, -1)) {
lua_pushnil(L);
lua_pushliteral(L, "Out of memory");
return 2;
}

if (!dbus_pending_call_set_notify(pending,
(DBusPendingCallNotifyFunction)
method_return_handler, L, NULL))
return error(L, "Out of memory");
method_return_handler, L, NULL)) {
lua_pushnil(L);
lua_pushliteral(L, "Out of memory");
return 2;
}

/* yield the connection */
lua_settop(L, 1);
Expand All @@ -366,8 +367,11 @@ static int bus_call_method(lua_State *L)
dbus_error_free(&err);
return 2;
}
if (ret == NULL)
return error(L, "Reply null");
if (ret == NULL) {
lua_pushnil(L);
lua_pushliteral(L, "Reply null");
return 2;
}

switch (dbus_message_get_type(ret)) {
case DBUS_MESSAGE_TYPE_METHOD_RETURN:
Expand All @@ -384,7 +388,9 @@ static int bus_call_method(lua_State *L)

dbus_message_unref(ret);

return error(L, "Unknown reply");
lua_pushnil(L);
lua_pushliteral(L, "Unknown reply");
return 2;
}

/* this magic string representation of an incoming
Expand Down Expand Up @@ -800,12 +806,18 @@ static int simpledbus_mainloop(lua_State *L)

/* make sure the stack can hold all the "fenv"'s of
* the connections (and a bit more) */
if (!lua_checkstack(L, n+3))
return error(L, "Out of memory");
if (!lua_checkstack(L, n+3)) {
lua_pushnil(L);
lua_pushliteral(L, "Out of memory");
return 2;
}

c = malloc(n * sizeof(LCon *));
if (c == NULL)
return error(L, "Out of memory");
if (c == NULL) {
lua_pushnil(L);
lua_pushliteral(L, "Out of memory");
return 2;
}

for (i = 0; i < n; i++) {
c[i] = bus_check(L, i+1);
Expand All @@ -816,7 +828,9 @@ static int simpledbus_mainloop(lua_State *L)
fds = make_poll_struct(n, c, &nfds);
if (fds == NULL) {
free(c);
return error(L, "Out of memory");
lua_pushnil(L);
lua_pushliteral(L, "Out of memory");
return 2;
}

stop = 0;
Expand All @@ -834,7 +848,9 @@ static int simpledbus_mainloop(lua_State *L)
if (fds == NULL) {
free(c);
mainThread = NULL;
return error(L, "Out of memory");
lua_pushnil(L);
lua_pushliteral(L, "Out of memory");
return 2;
}
}
#ifdef DEBUG
Expand All @@ -844,15 +860,10 @@ static int simpledbus_mainloop(lua_State *L)
free(c);
free(fds);
mainThread = NULL;
#if 1
lua_pushnil(L);
lua_pushfstring(L, "Error polling DBus: %s",
strerror(errno));
return 2;
#else
return error(L, "Error polling DBus: %s",
strerror(errno));
#endif
}
#ifdef DEBUG
printf(")"); fflush(stdout);
Expand Down Expand Up @@ -891,8 +902,11 @@ static int new_connection(lua_State *L, DBusConnection *conn)
return 2;
}

if (conn == NULL)
return error(L, "Couldn't create connection");
if (conn == NULL) {
lua_pushnil(L);
lua_pushliteral(L, "Couldn't create connection");
return 2;
}

lua_pushlightuserdata(L, conn);
lua_rawget(L, lua_upvalueindex(2)); /* connection table */
Expand All @@ -904,8 +918,11 @@ static int new_connection(lua_State *L, DBusConnection *conn)

/* create new userdata for the bus */
c = lua_newuserdata(L, sizeof(LCon));
if (c == NULL)
return error(L, "Out of memory");
if (c == NULL) {
lua_pushnil(L);
lua_pushliteral(L, "Out of memory");
return 2;
}
c->conn = conn;
c->nactive = 0;
c->active = NULL;
Expand All @@ -917,15 +934,19 @@ static int new_connection(lua_State *L, DBusConnection *conn)
(DBusWatchToggledFunction)toggle_watch_cb,
c, NULL)) {
dbus_connection_unref(conn);
return error(L, "Error setting watch functions");
lua_pushnil(L);
lua_pushliteral(L, "Error setting watch functions");
return 2;
}

/* set the signal handler */
if (!dbus_connection_add_filter(conn,
(DBusHandleMessageFunction)signal_handler,
c, NULL)) {
dbus_connection_unref(conn);
return error(L, "Error adding filter");
lua_pushnil(L);
lua_pushliteral(L, "Error adding filter");
return 2;
}

/* set the metatable */
Expand Down
7 changes: 0 additions & 7 deletions simpledbus.h

This file was deleted.

0 comments on commit b1b4968

Please sign in to comment.