check_wav: return error on premature end of file.
[paraslash.git] / command.c
index b07020a..531c776 100644 (file)
--- a/command.c
+++ b/command.c
@@ -18,7 +18,6 @@
 
 /** \file command.c does client authentication and executes server commands */
 
-#include <malloc.h> /* mallinfo */
 #include <sys/time.h> /* gettimeofday */
 #include "crypt.h"
 #include "server.cmdline.h"
@@ -44,8 +43,7 @@ static unsigned char rc4_buf[2 * RC4_KEY_LEN];
 extern const char *status_item_list[NUM_STAT_ITEMS];
 extern struct misc_meta_data *mmd;
 extern struct gengetopt_args_info conf;
-extern struct audio_file_selector dblist[];
-extern struct audio_format afl[];
+extern struct audio_file_selector selectors[];
 extern struct sender senders[];
 extern char *user_list;
 struct sockaddr_in *in_addr;
@@ -309,7 +307,7 @@ static struct server_command cmd_struct[] = {
 }
 };
 
-static void dummy(__unused int s)
+static void dummy(__a_unused int s)
 {}
 
 static void mmd_dup(struct misc_meta_data *new_mmd)
@@ -426,17 +424,20 @@ static char *get_status(struct misc_meta_data *nmmd)
                status_item_list[SI_STATUS_BAR], bar ? bar : "(none)",
                status_item_list[SI_STATUS], status,
                status_item_list[SI_STATUS_FLAGS], flags,
-               status_item_list[SI_SELECTOR], dblist[nmmd->dbt_num].name,
+               status_item_list[SI_SELECTOR], selectors[nmmd->selector_num].name,
 
                status_item_list[SI_OFFSET], offset,
                status_item_list[SI_FORMAT], audio_format_name(nmmd->audio_format),
-               nmmd->dbinfo,
+               nmmd->selector_info,
                nmmd->audio_file_info,
 
                status_item_list[SI_UPTIME], ut,
-               status_item_list[SI_STREAM_START], nmmd->stream_start.tv_sec,
-                       nmmd->stream_start.tv_usec,
-               status_item_list[SI_CURRENT_TIME], now.tv_sec, now.tv_usec
+               status_item_list[SI_STREAM_START],
+                       (long unsigned)nmmd->stream_start.tv_sec,
+                       (long unsigned)nmmd->stream_start.tv_usec,
+               status_item_list[SI_CURRENT_TIME],
+                       (long unsigned)now.tv_sec,
+                       (long unsigned)now.tv_usec
 
        );
        free(bar);
@@ -453,22 +454,21 @@ static int check_sender_args(int argc, char **argv, struct sender_command_data *
        const char *subcmds[] = {"add", "delete", "allow", "deny", "on", "off", NULL};
 
        scd->sender_num = -1;
-       if (argc < 0)
+       if (argc < 2)
                return -E_COMMAND_SYNTAX;
        for (i = 0; senders[i].name; i++)
-               if (!strcmp(senders[i].name, argv[0]))
+               if (!strcmp(senders[i].name, argv[1]))
                        break;
-//     PARA_DEBUG_LOG("%d:%s\n", argc, argv[0]);
+       PARA_DEBUG_LOG("%d:%s\n", argc, argv[1]);
        if (!senders[i].name)
                return -E_COMMAND_SYNTAX;
        scd->sender_num = i;
        for (i = 0; subcmds[i]; i++)
-               if (!strcmp(subcmds[i], argv[1]))
+               if (!strcmp(subcmds[i], argv[2]))
                        break;
        if (!subcmds[i])
                return -E_COMMAND_SYNTAX;
        scd->cmd_num = i;
-//     scd->self = *in_addr;
        mmd_lock();
        if (!senders[scd->sender_num].client_cmds[scd->cmd_num]) {
                mmd_unlock();
@@ -478,31 +478,31 @@ static int check_sender_args(int argc, char **argv, struct sender_command_data *
        switch (scd->cmd_num) {
        case SENDER_ON:
        case SENDER_OFF:
-               if (argc != 1)
+               if (argc != 3)
                        return -E_COMMAND_SYNTAX;
                break;
        case SENDER_DENY:
        case SENDER_ALLOW:
-               if (argc != 2 && argc != 3)
+               if (argc != 4 && argc != 5)
                        return -E_COMMAND_SYNTAX;
-               if (!inet_aton(argv[2], &scd->addr))
+               if (!inet_aton(argv[3], &scd->addr))
                        return -E_COMMAND_SYNTAX;
                scd->netmask = 32;
-               if (argc == 3) {
-                       scd->netmask = atoi(argv[3]);
+               if (argc == 5) {
+                       scd->netmask = atoi(argv[4]);
                        if (scd->netmask < 0 || scd->netmask > 32)
                                return -E_COMMAND_SYNTAX;
                }
                break;
        case SENDER_ADD:
        case SENDER_DELETE:
-               if (argc != 2 && argc != 3)
+               if (argc != 4 && argc != 5)
                        return -E_COMMAND_SYNTAX;
-               if (!inet_aton(argv[2], &scd->addr))
+               if (!inet_aton(argv[3], &scd->addr))
                        return -E_COMMAND_SYNTAX;
                scd->port = -1;
-               if (argc == 3) {
-                       scd->port = atoi(argv[3]);
+               if (argc == 5) {
+                       scd->port = atoi(argv[4]);
                        if (scd->port < 0 || scd->port > 65535)
                                return -E_COMMAND_SYNTAX;
                }
@@ -518,7 +518,7 @@ static int com_sender(int fd, int argc, char **argv)
        int i, ret;
        struct sender_command_data scd;
 
-       if (!argc) {
+       if (argc < 2) {
                char *msg = NULL;
                for (i = 0; senders[i].name; i++) {
                        char *tmp = make_message("%s%s\n",
@@ -530,7 +530,7 @@ static int com_sender(int fd, int argc, char **argv)
                free(msg);
                return ret;
        }
-       ret = check_sender_args(argc - 1, argv + 1, &scd);
+       ret = check_sender_args(argc, argv, &scd);
        if (ret < 0) {
                char *msg;
                if (scd.sender_num < 0)
@@ -555,19 +555,18 @@ static int com_sender(int fd, int argc, char **argv)
 }
 
 /* server info */
-static int com_si(int fd, int argc, __unused char **argv)
+static int com_si(int fd, int argc, __a_unused char **argv)
 {
        int i, ret;
        char *ut;
-       char *selectors = NULL, *sender_info = NULL, *sender_list = NULL;
-       struct mallinfo mi = mallinfo();
+       char *selector_string = NULL, *sender_info = NULL, *sender_list = NULL;
 
-       if (argc)
+       if (argc != 1)
                return -E_COMMAND_SYNTAX;
        mmd_lock();
-       for (i = 0; dblist[i].name; i++) {
-               selectors = para_strcat(selectors, dblist[i].name);
-               selectors = para_strcat(selectors, " ");
+       for (i = 0; selectors[i].name; i++) {
+               selector_string = para_strcat(selector_string, selectors[i].name);
+               selector_string = para_strcat(selector_string, " ");
        }
        for (i = 0; senders[i].name; i++) {
                char *info = senders[i].info();
@@ -579,37 +578,35 @@ static int com_si(int fd, int argc, __unused char **argv)
        ut = uptime_str();
        ret = send_va_buffer(fd, "up: %s\nplayed: %u\n"
                "pid: %d\n"
-               "mallinfo: %d\n"
                "connections (active/accepted/total): %u/%u/%u\n"
                "current loglevel: %i\n"
-               "supported database tools: %s\n"
+               "supported audio file selectors: %s\n"
                "supported audio formats: %s\n"
                "supported senders: %s\n"
                "%s",
                ut, mmd->num_played,
                getppid(),
-               mi.arena / 1024,
                mmd->active_connections,
                mmd->num_commands,
                mmd->num_connects,
                conf.loglevel_arg,
-               selectors,
-               SUPPORTED_AUDIO_FORMATS,
+               selector_string,
+               supported_audio_formats(),
                sender_list,
                sender_info
        );
        mmd_unlock();
        free(ut);
-       free(selectors);
+       free(selector_string);
        free(sender_list);
        free(sender_info);
        return ret;
 }
 
 /* version */
-static int com_version(int socket_fd, int argc, __unused char **argv)
+static int com_version(int socket_fd, int argc, __a_unused char **argv)
 {
-       if (argc)
+       if (argc != 1)
                return -E_COMMAND_SYNTAX;
        return send_buffer(socket_fd, "para_server-" VERSION ", \"" CODENAME "\"\n"
                        COPYRIGHT "\n"
@@ -624,7 +621,7 @@ static int com_sc(int socket_fd, int argc, char **argv)
        char *name = NULL;
        int ret, old = 0, count = -1; /* print af change forever */
 
-       if (argc)
+       if (argc > 1)
                count = atoi(argv[1]);
 repeat:
        mmd_lock();
@@ -639,7 +636,7 @@ repeat:
                name = NULL;
                if (ret < 0)
                        return ret;
-               if (argc && !--count)
+               if (argc > 1 && !--count)
                        return 1;
        }
        usleep(500000);
@@ -654,7 +651,7 @@ static int com_sb(int socket_fd, int argc, char **argv)
                                 * times. Negative value means: print
                                 * forever
                                 */
-       if (argc)
+       if (argc > 1)
                nr = atoi(argv[1]);
        while (nr) {
                mmd_lock();
@@ -676,7 +673,6 @@ static int com_sb(int socket_fd, int argc, char **argv)
 /* stat */
 static int com_stat(int socket_fd, int argc, char **argv)
 {
-//     char *old_stat = NULL, *old_dbinfo = NULL;
        int ret, num = 0;/* status will be printed that many
                          * times. num <= 0 means: print forever
                          */
@@ -685,7 +681,7 @@ static int com_stat(int socket_fd, int argc, char **argv)
 
        signal(SIGUSR1, dummy);
 
-       if (argc)
+       if (argc > 1)
                num = atoi(argv[1]);
        for (;;) {
 
@@ -721,7 +717,7 @@ static int send_description(int fd, struct server_command *cmd, const char *hand
        return 1;
 }
 
-/* always returns string that must be freed by the caller in handeler */
+/* always returns string that must be freed by the caller in handler */
 static struct server_command *get_cmd_ptr(char *name, char **handler)
 {
        struct server_command *cmd = cmd_struct;
@@ -735,8 +731,9 @@ static struct server_command *get_cmd_ptr(char *name, char **handler)
        /* not found, look for commands supported by the current selector */
        mmd_lock();
        if (handler)
-               *handler = make_message("the %s database tool", dblist[mmd->dbt_num].name);
-       cmd = dblist[mmd->dbt_num].cmd_list;
+               *handler = make_message("the %s selector",
+                       selectors[mmd->selector_num].name);
+       cmd = selectors[mmd->selector_num].cmd_list;
        mmd_unlock();
        for (; cmd->name; cmd++)
                if (!strcmp(cmd->name, name))
@@ -751,13 +748,13 @@ static int com_help(int fd, int argc, char **argv)
        char *perms, *handler;
        int ret;
 
-       if (!argc) {
+       if (argc < 2) {
                /* no argument given, print list of commands */
                if ((ret = send_description(fd, cmd_struct, "server", 0)) < 0)
                        return ret;
                mmd_lock();
-               handler = para_strdup(dblist[mmd->dbt_num].name);
-               cmd = dblist[mmd->dbt_num].cmd_list;
+               handler = para_strdup(selectors[mmd->selector_num].name);
+               cmd = selectors[mmd->selector_num].cmd_list;
                mmd_unlock();
                ret = send_description(fd, cmd, handler, 0);
                free(handler);
@@ -792,26 +789,26 @@ static int com_help(int fd, int argc, char **argv)
 }
 
 /* hup */
-static int com_hup(__unused int socket_fd, int argc, __unused char **argv)
+static int com_hup(__a_unused int socket_fd, int argc, __a_unused char **argv)
 {
-       if (argc)
+       if (argc != 1)
                return -E_COMMAND_SYNTAX;
        kill(getppid(), SIGHUP);
        return 1;
 }
 
 /* term */
-static int com_term(__unused int socket_fd, int argc, __unused char **argv)
+static int com_term(__a_unused int socket_fd, int argc, __a_unused char **argv)
 {
-       if (argc)
+       if (argc != 1)
                return -E_COMMAND_SYNTAX;
        kill(getppid(), SIGTERM);
        return 1;
 }
 
-static int com_play(__unused int socket_fd, int argc, __unused char **argv)
+static int com_play(__a_unused int socket_fd, int argc, __a_unused char **argv)
 {
-       if (argc)
+       if (argc != 1)
                return -E_COMMAND_SYNTAX;
        mmd_lock();
        mmd->new_afs_status_flags |= AFS_PLAYING;
@@ -822,9 +819,9 @@ static int com_play(__unused int socket_fd, int argc, __unused char **argv)
 }
 
 /* stop */
-static int com_stop(__unused int socket_fd, int argc, __unused char **argv)
+static int com_stop(__a_unused int socket_fd, int argc, __a_unused char **argv)
 {
-       if (argc)
+       if (argc != 1)
                return -E_COMMAND_SYNTAX;
        mmd_lock();
        mmd->new_afs_status_flags &= ~AFS_PLAYING;
@@ -835,9 +832,9 @@ static int com_stop(__unused int socket_fd, int argc, __unused char **argv)
 }
 
 /* pause */
-static int com_pause(__unused int socket_fd, int argc, __unused char **argv)
+static int com_pause(__a_unused int socket_fd, int argc, __a_unused char **argv)
 {
-       if (argc)
+       if (argc != 1)
                return -E_COMMAND_SYNTAX;
        mmd_lock();
        if (!afs_paused())
@@ -852,42 +849,43 @@ static int com_chs(int fd, int argc, char **argv)
 {
        int i, ret;
 
-       if (!argc) {
+       if (argc == 1) {
                char *selector;
                mmd_lock();
-               selector = para_strdup(dblist[mmd->dbt_num].name);
+               selector = para_strdup(selectors[mmd->selector_num].name);
                mmd_unlock();
                ret = send_va_buffer(fd, "%s\n", selector);
                free(selector);
                return ret;
        }
-       for (i = 0; dblist[i].name; i++) {
-               if (strcmp(dblist[i].name, argv[1]))
+       for (i = 0; selectors[i].name; i++) {
+               if (strcmp(selectors[i].name, argv[1]))
                        continue;
                mmd_lock();
-               mmd->dbt_change = i;
+               mmd->selector_change = i;
                mmd->events++;
                mmd_unlock();
                return 1;
        }
-       return -E_BAD_DBTOOL;
+       return -E_BAD_SELECTOR;
 }
 
 /* next */
-static int com_next(__unused int socket_fd, int argc, __unused char **argv)
+static int com_next(__a_unused int socket_fd, int argc, __a_unused char **argv)
 {
-       if (argc)
+       if (argc != 1)
                return -E_COMMAND_SYNTAX;
        mmd_lock();
+       mmd->events++;
        mmd->new_afs_status_flags |= AFS_NEXT;
        mmd_unlock();
        return 1;
 }
 
 /* nomore */
-static int com_nomore(__unused int socket_fd, int argc, __unused char **argv)
+static int com_nomore(__a_unused int socket_fd, int argc, __a_unused char **argv)
 {
-       if (argc)
+       if (argc != 1)
                return -E_COMMAND_SYNTAX;
        mmd_lock();
        if (afs_playing() || afs_paused())
@@ -897,14 +895,14 @@ static int com_nomore(__unused int socket_fd, int argc, __unused char **argv)
 }
 
 /* ff */
-static int com_ff(__unused int socket_fd, int argc, char **argv)
+static int com_ff(__a_unused int socket_fd, int argc, char **argv)
 {
        long promille;
        int ret, backwards = 0;
        unsigned i;
        char c;
 
-       if (!argc)
+       if (argc != 2)
                return -E_COMMAND_SYNTAX;
        if (!(ret = sscanf(argv[1], "%u%c", &i, &c)))
                return -E_COMMAND_SYNTAX;
@@ -936,12 +934,12 @@ out:
 }
 
 /* jmp */
-static int com_jmp(__unused int socket_fd, int argc, char **argv)
+static int com_jmp(__a_unused int socket_fd, int argc, char **argv)
 {
        long unsigned int i;
        int ret;
 
-       if (!argc)
+       if (argc != 2)
                return -E_COMMAND_SYNTAX;
        if (sscanf(argv[1], "%lu", &i) <= 0)
                return -E_COMMAND_SYNTAX;
@@ -1176,7 +1174,7 @@ int handle_connect(int fd, struct sockaddr_in *addr)
        if (use_rc4) {
                crypt_function_recv = rc4_recv;
                crypt_function_send = rc4_send;
-               PARA_INFO_LOG("%s", "rc4 encrytion activated\n");
+               PARA_INFO_LOG("%s", "rc4 encryption activated\n");
        }
        /* read command */
        while ((numbytes = recv_buffer(fd, buf, sizeof(buf))) > 0) {
@@ -1203,8 +1201,7 @@ int handle_connect(int fd, struct sockaddr_in *addr)
                goto err_out;
        /* valid command and sufficient perms */
        alarm(0);
-       argc = split_args(command, &argv, '\n');
-       argv[0] = cmd->name;
+       argc = split_args(command, &argv, "\n");
        mmd_lock();
        mmd->num_commands++;
        mmd_unlock();