From 0c12189d8522820c6b39dc84cd9c042b7caf7024 Mon Sep 17 00:00:00 2001 From: jubalh Date: Tue, 24 Nov 2020 19:19:59 +0100 Subject: [PATCH] Several changes (#18) Show help if no argument is provided In case someone calls `xmppc` assume that he doesn't know how to use it. Which means he might even not be aware of `-h`. So let's print the help in case no argument is provided instead of connecting, then discoverin that no mode is selected and disconnecting again. Fix segfault when not roster subcommand is provided _roster_parse_command() segfaults since it assumes there is an argv[0]. Let's test the number of arguments. Since we are already connected xmpp_connect_client() will wait for a disconnect. So I introduce a teardown function that is called in both the error case and the succesful case. Now _roster_send_query() doesn't disconnect anymore. Set to development by default Only set to release when a new release is tagged. Like this not every developer needs to change the code. And it's git aka development anyways. Don't mix german and english Co-authored-by: Michael Vetter Reviewed-on: https://codeberg.org/Anoxinon_e.V./xmppc/pulls/18 --- configure.ac | 4 ++-- src/main.c | 9 ++++++++- src/mode/roster.c | 21 +++++++++++++++++---- 3 files changed, 27 insertions(+), 7 deletions(-) diff --git a/configure.ac b/configure.ac index 3470cc8..92be79c 100644 --- a/configure.ac +++ b/configure.ac @@ -19,8 +19,8 @@ AS_CASE([$host_os], [cygwin], [PLATFORM="cygwin"], [PLATFORM="nix"]) -#PACKAGE_STATUS="development" -PACKAGE_STATUS="release" +PACKAGE_STATUS="development" +#PACKAGE_STATUS="release" AM_CFLAGS="-Wall -Wno-deprecated-declarations -pedantic -std=c11" AS_IF([test "x$PACKAGE_STATUS" = xdevelopment], diff --git a/src/main.c b/src/main.c index fa2798d..7f1dcd2 100644 --- a/src/main.c +++ b/src/main.c @@ -358,6 +358,13 @@ int main(int argc, char *argv[]) { {"mode", required_argument, 0, 'm'}, {"file", required_argument, 0, 'f'}, {0, 0, 0, 0}}; + + // No arguments provided + if (argc == 1) { + _show_help(); + return EXIT_SUCCESS; + } + while (c > -1) { int option_index = 0; @@ -484,7 +491,7 @@ int main(int argc, char *argv[]) { } if( handler == NULL ) { - logError(&xmppc, "Unbekannter mode\n"); + logError(&xmppc, "Unknown mode\n"); return -1; } diff --git a/src/mode/roster.c b/src/mode/roster.c index 16da191..8a00bcc 100644 --- a/src/mode/roster.c +++ b/src/mode/roster.c @@ -59,11 +59,20 @@ static void _roster_send_query(xmppc_t *xmppc,command_t *command); static int _handle_reply(xmpp_conn_t *const conn, xmpp_stanza_t *const stanza, void *const userdata); +static void _teardown(xmppc_t *xmppc); + void roster_execute_command(xmppc_t *xmppc, int argc, char *argv[]) { command_t *command = malloc(sizeof(command_t)); command->type = UNKOWN; - _roster_parse_command(command, argc, argv); - _roster_send_query(xmppc, command); + + if (argc == 0) { + logError(xmppc, "No subcommand provided\n"); + } else { + _roster_parse_command(command, argc, argv); + _roster_send_query(xmppc, command); + } + + _teardown(xmppc); } static void _roster_parse_command(command_t *command, int argc, char *argv[]) { @@ -115,7 +124,11 @@ int _handle_reply(xmpp_conn_t *const conn, xmpp_stanza_t *const stanza, } } } - xmpp_disconnect(conn); - xmpp_stop(xmpp_conn_get_context(conn)); return 0; } + +static void _teardown(xmppc_t *xmppc) { + xmpp_conn_t *conn = xmppc->conn; + xmpp_disconnect(conn); + xmpp_stop(xmpp_conn_get_context(conn)); +}