Merge branch 't/wma_improvements'
[paraslash.git] / command.c
index 9c175f35ec45087325db2ef3dbb3249689de0708..f9bace34146e28ccaf7b0632e0fa34ede51de8aa 100644 (file)
--- a/command.c
+++ b/command.c
@@ -36,6 +36,9 @@
 #include "signal.h"
 #include "version.h"
 
+struct server_command afs_cmds[] = {DEFINE_AFS_CMD_ARRAY};
+struct server_command server_cmds[] = {DEFINE_SERVER_CMD_ARRAY};
+
 /** Commands including options must be shorter than this. */
 #define MAX_COMMAND_LEN 32768
 
@@ -256,6 +259,14 @@ __printf_3_4 int send_sb_va(struct stream_cipher_context *scc, int band,
        return send_sb(scc, msg, ret, band, false);
 }
 
+int send_strerror(struct command_context *cc, int err)
+{
+       return cc->use_sideband?
+               send_sb_va(&cc->scc, SBD_ERROR_LOG, "%s\n", para_strerror(err))
+       :
+               sc_send_va_buffer(&cc->scc, "%s\n", para_strerror(err));
+}
+
 /**
  * Send a sideband packet through a blocking file descriptor.
  *
@@ -306,19 +317,22 @@ fail:
        return ret;
 }
 
-int com_sender(struct command_context *cc)
+static int com_sender(struct command_context *cc)
 {
-       int i, ret;
+       int i, ret = 0;
        char *msg = NULL;
        struct sender_command_data scd;
 
        if (cc->argc < 2) {
                for (i = 0; senders[i].name; i++) {
-                       char *tmp = make_message("%s%s\n",
-                               msg? msg : "", senders[i].name);
+                       char *tmp;
+                       ret = xasprintf(&tmp, "%s%s\n", msg? msg : "",
+                               senders[i].name);
                        free(msg);
                        msg = tmp;
                }
+               if (cc->use_sideband)
+                       return send_sb(&cc->scc, msg, ret, SBD_OUTPUT, false);
                ret = sc_send_buffer(&cc->scc, msg);
                free(msg);
                return ret;
@@ -328,6 +342,9 @@ int com_sender(struct command_context *cc)
                if (scd.sender_num < 0)
                        return ret;
                msg = senders[scd.sender_num].help();
+               if (cc->use_sideband)
+                       return send_sb(&cc->scc, msg, strlen(msg), SBD_OUTPUT,
+                               false);
                ret = sc_send_buffer(&cc->scc, msg);
                free(msg);
                return ret;
@@ -357,11 +374,10 @@ int com_sender(struct command_context *cc)
 }
 
 /* server info */
-int com_si(struct command_context *cc)
+static int com_si(struct command_context *cc)
 {
        int i, ret;
-       char *ut;
-       char *sender_info = NULL;
+       char *msg, *ut, *sender_info = NULL;
 
        if (cc->argc != 1)
                return -E_COMMAND_SYNTAX;
@@ -372,7 +388,7 @@ int com_si(struct command_context *cc)
                free(info);
        }
        ut = get_server_uptime_str(now);
-       ret = sc_send_va_buffer(&cc->scc, "version: " GIT_VERSION "\n"
+       ret = xasprintf(&msg, "version: " GIT_VERSION "\n"
                "up: %s\nplayed: %u\n"
                "server_pid: %d\n"
                "afs_pid: %d\n"
@@ -393,18 +409,27 @@ int com_si(struct command_context *cc)
        mutex_unlock(mmd_mutex);
        free(ut);
        free(sender_info);
+       if (cc->use_sideband)
+               return send_sb(&cc->scc, msg, ret, SBD_OUTPUT, false);
+       ret = sc_send_bin_buffer(&cc->scc, msg, ret);
+       free(msg);
        return ret;
 }
 
 /* version */
-int com_version(struct command_context *cc)
+static int com_version(struct command_context *cc)
 {
+       char *msg;
+       size_t len;
+
        if (cc->argc != 1)
                return -E_COMMAND_SYNTAX;
-       return sc_send_buffer(&cc->scc, VERSION_TEXT("server")
-               "built: " BUILD_DATE "\n"
-               UNAME_RS ", " CC_VERSION "\n"
-       );
+       msg = VERSION_TEXT("server") "built: " BUILD_DATE "\n" UNAME_RS
+               ", " CC_VERSION "\n";
+       len = strlen(msg);
+       if (cc->use_sideband)
+               return send_sb(&cc->scc, msg, len, SBD_OUTPUT, true);
+       return sc_send_bin_buffer(&cc->scc, msg, len);
 }
 
 #define EMPTY_STATUS_ITEMS \
@@ -440,10 +465,16 @@ int com_version(struct command_context *cc)
  *
  * This is used by vss when currently no audio file is open.
  */
-static char *empty_status_items(int parser_friendly)
+static unsigned empty_status_items(int parser_friendly, char **result)
 {
+       static char *esi;
+       static unsigned len;
+
+       if (esi)
+               goto out;
+
        if (parser_friendly)
-               return make_message(
+               len = xasprintf(&esi,
                        #define ITEM(x) "0004 %02x:\n"
                        EMPTY_STATUS_ITEMS
                        #undef ITEM
@@ -451,23 +482,27 @@ static char *empty_status_items(int parser_friendly)
                        EMPTY_STATUS_ITEMS
                        #undef ITEM
                );
-       return make_message(
-               #define ITEM(x) "%s:\n"
-               EMPTY_STATUS_ITEMS
-               #undef ITEM
-               #define ITEM(x) ,status_item_list[SI_ ## x]
-               EMPTY_STATUS_ITEMS
-               #undef ITEM
-       );
+       else
+               len = xasprintf(&esi,
+                       #define ITEM(x) "%s:\n"
+                       EMPTY_STATUS_ITEMS
+                       #undef ITEM
+                       #define ITEM(x) ,status_item_list[SI_ ## x]
+                       EMPTY_STATUS_ITEMS
+                       #undef ITEM
+               );
+out:
+       *result = esi;
+       return len;
 }
 #undef EMPTY_STATUS_ITEMS
 
 /* stat */
-int com_stat(struct command_context *cc)
+static int com_stat(struct command_context *cc)
 {
        int i, ret;
        struct misc_meta_data tmp, *nmmd = &tmp;
-       char *s;
+       char *s, *esi = NULL;
        int32_t num = 0;
        int parser_friendly = 0;
 
@@ -498,15 +533,21 @@ int com_stat(struct command_context *cc)
        for (;;) {
                mmd_dup(nmmd);
                ret = get_status(nmmd, parser_friendly, &s);
-               ret = sc_send_bin_buffer(&cc->scc, s, ret);
-               free(s);
+               if (cc->use_sideband)
+                       ret = send_sb(&cc->scc, s, ret, SBD_OUTPUT, false);
+               else {
+                       ret = sc_send_bin_buffer(&cc->scc, s, ret);
+                       free(s);
+               }
                if (ret < 0)
                        goto out;
                if (nmmd->vss_status_flags & VSS_NEXT) {
-                       static char *esi;
-                       if (!esi)
-                               esi = empty_status_items(parser_friendly);
-                       ret = sc_send_buffer(&cc->scc, esi);
+                       ret = empty_status_items(parser_friendly, &esi);
+                       if (cc->use_sideband)
+                               ret = send_sb(&cc->scc, esi, ret, SBD_OUTPUT,
+                                       true);
+                       else
+                               ret = sc_send_bin_buffer(&cc->scc, esi, ret);
                        if (ret < 0)
                                goto out;
                } else
@@ -515,29 +556,34 @@ int com_stat(struct command_context *cc)
                if (num > 0 && !--num)
                        goto out;
                sleep(50);
+               ret = -E_SERVER_CRASH;
                if (getppid() == 1)
-                       return -E_SERVER_CRASH;
+                       goto out;
        }
 out:
+       free(esi);
        return ret;
 }
 
-static int send_list_of_commands(struct stream_cipher_context *scc, struct server_command *cmd,
+static int send_list_of_commands(struct command_context *cc, struct server_command *cmd,
                const char *handler)
 {
-       int ret, i;
+       int ret;
+       char *msg = NULL;
 
-       for (i = 1; cmd->name; cmd++, i++) {
-               char *perms = cmd_perms_itohuman(cmd->perms);
-               ret = sc_send_va_buffer(scc, "%s\t%s\t%s\t%s\n", cmd->name,
-                       handler,
-                       perms,
-                       cmd->description);
+       for (; cmd->name; cmd++) {
+               char *tmp, *perms = cmd_perms_itohuman(cmd->perms);
+               tmp = make_message("%s\t%s\t%s\t%s\n", cmd->name, handler,
+                       perms, cmd->description);
                free(perms);
-               if (ret < 0)
-                       return ret;
+               msg = para_strcat(msg, tmp);
+               free(tmp);
        }
-       return 1;
+       if (cc->use_sideband)
+               return send_sb(&cc->scc, msg, strlen(msg), SBD_OUTPUT, false);
+       ret = sc_send_buffer(&cc->scc, msg);
+       free(msg);
+       return ret;
 }
 
 /* returns string that must be freed by the caller */
@@ -562,25 +608,24 @@ static struct server_command *get_cmd_ptr(const char *name, char **handler)
 }
 
 /* help */
-int com_help(struct command_context *cc)
+static int com_help(struct command_context *cc)
 {
        struct server_command *cmd;
-       char *perms, *handler;
+       char *perms, *handler, *buf;
        int ret;
 
        if (cc->argc < 2) {
                /* no argument given, print list of commands */
-               if ((ret = send_list_of_commands(&cc->scc, server_cmds, "server")) < 0)
+               if ((ret = send_list_of_commands(cc, server_cmds, "server")) < 0)
                        return ret;
-               return send_list_of_commands(&cc->scc, afs_cmds, "afs");
+               return send_list_of_commands(cc, afs_cmds, "afs");
        }
        /* argument given for help */
        cmd = get_cmd_ptr(cc->argv[1], &handler);
        if (!cmd)
                return -E_BAD_CMD;
        perms = cmd_perms_itohuman(cmd->perms);
-       ret = sc_send_va_buffer(&cc->scc,
-               "%s - %s\n\n"
+       ret = xasprintf(&buf, "%s - %s\n\n"
                "handler: %s\n"
                "permissions: %s\n"
                "usage: %s\n\n"
@@ -594,11 +639,15 @@ int com_help(struct command_context *cc)
        );
        free(perms);
        free(handler);
+       if (cc->use_sideband)
+               return send_sb(&cc->scc, buf, ret, SBD_OUTPUT, false);
+       ret = sc_send_buffer(&cc->scc, buf);
+       free(buf);
        return ret;
 }
 
 /* hup */
-int com_hup(struct command_context *cc)
+static int com_hup(struct command_context *cc)
 {
        if (cc->argc != 1)
                return -E_COMMAND_SYNTAX;
@@ -607,7 +656,7 @@ int com_hup(struct command_context *cc)
 }
 
 /* term */
-int com_term(struct command_context *cc)
+static int com_term(struct command_context *cc)
 {
        if (cc->argc != 1)
                return -E_COMMAND_SYNTAX;
@@ -615,7 +664,7 @@ int com_term(struct command_context *cc)
        return 1;
 }
 
-int com_play(struct command_context *cc)
+static int com_play(struct command_context *cc)
 {
        if (cc->argc != 1)
                return -E_COMMAND_SYNTAX;
@@ -627,7 +676,7 @@ int com_play(struct command_context *cc)
 }
 
 /* stop */
-int com_stop(struct command_context *cc)
+static int com_stop(struct command_context *cc)
 {
        if (cc->argc != 1)
                return -E_COMMAND_SYNTAX;
@@ -640,7 +689,7 @@ int com_stop(struct command_context *cc)
 }
 
 /* pause */
-int com_pause(struct command_context *cc)
+static int com_pause(struct command_context *cc)
 {
        if (cc->argc != 1)
                return -E_COMMAND_SYNTAX;
@@ -655,7 +704,7 @@ int com_pause(struct command_context *cc)
 }
 
 /* next */
-int com_next(struct command_context *cc)
+static int com_next(struct command_context *cc)
 {
        if (cc->argc != 1)
                return -E_COMMAND_SYNTAX;
@@ -667,7 +716,7 @@ int com_next(struct command_context *cc)
 }
 
 /* nomore */
-int com_nomore(struct command_context *cc)
+static int com_nomore(struct command_context *cc)
 {
        if (cc->argc != 1)
                return -E_COMMAND_SYNTAX;
@@ -679,7 +728,7 @@ int com_nomore(struct command_context *cc)
 }
 
 /* ff */
-int com_ff(struct command_context *cc)
+static int com_ff(struct command_context *cc)
 {
        long promille;
        int ret, backwards = 0;
@@ -718,7 +767,7 @@ out:
 }
 
 /* jmp */
-int com_jmp(struct command_context *cc)
+static int com_jmp(struct command_context *cc)
 {
        long unsigned int i;
        int ret;
@@ -1054,18 +1103,24 @@ __noreturn void handle_connect(int fd, const char *peername)
        if (ret >= 0)
                goto out;
 err_out:
-       sc_send_va_buffer(&cc->scc, "%s\n", para_strerror(-ret));
+       if (send_strerror(cc, -ret) >= 0 && cc->use_sideband)
+               send_sb(&cc->scc, NULL, 0, SBD_EXIT__FAILURE, true);
 net_err:
        PARA_NOTICE_LOG("%s\n", para_strerror(-ret));
 out:
        free(buf);
        free(command);
-       sc_free(cc->scc.recv);
-       sc_free(cc->scc.send);
        mutex_lock(mmd_mutex);
        if (cc->cmd && (cc->cmd->perms & AFS_WRITE) && ret >= 0)
                mmd->events++;
        mmd->active_connections--;
        mutex_unlock(mmd_mutex);
+       if (ret >= 0 && cc->use_sideband) {
+               ret = send_sb(&cc->scc, NULL, 0, SBD_EXIT__SUCCESS, true);
+               if (ret < 0)
+                       PARA_NOTICE_LOG("%s\n", para_strerror(-ret));
+       }
+       sc_free(cc->scc.recv);
+       sc_free(cc->scc.send);
        exit(ret < 0? EXIT_FAILURE : EXIT_SUCCESS);
 }