summaryrefslogtreecommitdiff
path: root/commands.c
diff options
context:
space:
mode:
Diffstat (limited to 'commands.c')
-rw-r--r--commands.c41
1 files changed, 15 insertions, 26 deletions
diff --git a/commands.c b/commands.c
index 3ff250d..db7e133 100644
--- a/commands.c
+++ b/commands.c
@@ -3,8 +3,7 @@
#include <string.h>
-static void commands_list(GSocketConnection *connection, const gchar *cmd) {
- GError *error = NULL;
+static void commands_list(GSocketConnection *connection, const gchar *cmd, GError **error) {
gchar **data = g_strsplit(cmd, " ", 2);
for(gint i = 0; data[i]; i++) {
g_debug("\tdata[%d] = %s", i, data[i]);
@@ -46,16 +45,14 @@ static void commands_list(GSocketConnection *connection, const gchar *cmd) {
GOutputStream *os = g_io_stream_get_output_stream((GIOStream*)connection);
if(g_output_stream_write_all(os, string->str, string->len, NULL, NULL,
- &error) == FALSE) {
- g_warning(error->message);
- g_error_free(error);
+ error) == FALSE) {
+ return;
}
g_string_free(string, TRUE);
}
-static void commands_find(GSocketConnection *connection, const gchar *cmd) {
- GError *error = NULL;
+static void commands_find(GSocketConnection *connection, const gchar *cmd, GError **error) {
gchar **data = g_strsplit(cmd, " ", 3);
if(g_strv_length(data) != 3) {
@@ -88,42 +85,34 @@ static void commands_find(GSocketConnection *connection, const gchar *cmd) {
GOutputStream *os = g_io_stream_get_output_stream((GIOStream*)connection);
if(g_output_stream_write_all(os, string->str, string->len, NULL, NULL,
- &error) == FALSE) {
- g_warning(error->message);
- g_error_free(error);
+ error) == FALSE) {
+ return;
}
g_string_free(string, TRUE);
}
-static void commands_ping(GSocketConnection *connection, const gchar *cmd) {
- GError *error = NULL;
+static void commands_ping(GSocketConnection *connection, const gchar *cmd, GError **error) {
GSocket *socket = g_socket_connection_get_socket(connection);
- if(g_socket_send(socket, "pong\n", 5, NULL, NULL) == -1) {
- g_warning(error->message);
- g_error_free(error);
- }
+ g_socket_send(socket, "pong\n", 5, NULL, error);
}
-static void commands_exit(GSocketConnection *connection, const gchar *cmd) {
+static void commands_exit(GSocketConnection *connection, const gchar *cmd, GError **error) {
GSocket *socket = g_socket_connection_get_socket(connection);
g_socket_close(socket, NULL);
}
-void commands_handle(GSocketConnection *connection, const gchar *cmd) {
+void commands_handle(GSocketConnection *connection, const gchar *cmd, GError **error) {
g_debug(cmd);
if(g_strncasecmp(cmd, "ping", 4) == 0) {
- commands_ping(connection, cmd);
+ commands_ping(connection, cmd, error);
} else if(g_strncasecmp(cmd, "list", 4) == 0) {
- commands_list(connection, cmd);
+ commands_list(connection, cmd, error);
} else if(g_strncasecmp(cmd, "find", 4) == 0) {
- commands_find(connection, cmd);
+ commands_find(connection, cmd, error);
} else if(g_strncasecmp(cmd, "exit", 4) == 0) {
- commands_exit(connection, cmd);
+ commands_exit(connection, cmd, error);
} else {
g_debug("unknown command");
- gchar *buf = g_strdup_printf("error: unknown command %s\n", cmd);
- GSocket *socket = g_socket_connection_get_socket(connection);
- g_socket_send(socket, buf, strlen(buf), NULL, NULL);
- g_free(buf);
+ *error = g_error_new(0, 0, "unknown command %s", cmd);
}
}