Merge branch 'master' into next
[paraslash.git] / command.c
index fc5b5ca..afa1e66 100644 (file)
--- a/command.c
+++ b/command.c
@@ -6,11 +6,13 @@
 
 /** \file command.c Client authentication and server commands. */
 
+#include <regex.h>
 #include <signal.h>
 #include <sys/time.h>
 #include <sys/types.h>
 #include <dirent.h>
 #include <openssl/rc4.h>
+#include <osl.h>
 
 #include "para.h"
 #include "error.h"
@@ -41,6 +43,9 @@
 extern int mmd_mutex;
 extern struct misc_meta_data *mmd;
 extern struct sender senders[];
+int send_afs_status(struct rc4_context *rc4c, int parser_friendly);
+
+const char *status_item_list[] = {STATUS_ITEM_ARRAY};
 
 static void dummy(__a_unused int s)
 {
@@ -100,14 +105,15 @@ static char *vss_get_status_flags(unsigned int flags)
        return msg;
 }
 
-static char *get_status(struct misc_meta_data *nmmd)
+static char *get_status(struct misc_meta_data *nmmd, int parser_friendly)
 {
-       char *ret, mtime[30] = "";
+       char mtime[30] = "";
        char *status, *flags; /* vss status info */
        char *ut = uptime_str();
        long offset = (nmmd->offset + 500) / 1000;
        struct timeval current_time;
        struct tm mtime_tm;
+       struct para_buffer b = {.flags = parser_friendly? PBF_SIZE_PREFIX : 0};
 
        /* report real status */
        status = vss_status_tohuman(nmmd->vss_status_flags);
@@ -117,38 +123,22 @@ static char *get_status(struct misc_meta_data *nmmd)
                strftime(mtime, 29, "%b %d %Y", &mtime_tm);
        }
        gettimeofday(&current_time, NULL);
-       ret = make_message(
-               "%s: %zu\n" /* file size */
-               "%s: %s\n" /* mtime */
-               "%s: %s\n" /* status */
-               "%s: %s\n" /* status flags */
-               "%s: %li\n" /* offset */
-               "%s: %s\n" /* afs mode */
-               "%s: %lu.%lu\n" /* stream start */
-               "%s: %lu.%lu\n" /* current server time */
-               "%s", /* afs status info */
-               status_item_list[SI_FILE_SIZE], nmmd->size / 1024,
-               status_item_list[SI_MTIME], mtime,
-               status_item_list[SI_STATUS], status,
-               status_item_list[SI_STATUS_FLAGS], flags,
-
-               status_item_list[SI_OFFSET], offset,
-               status_item_list[SI_AFS_MODE], mmd->afs_mode_string,
-
-               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)current_time.tv_sec,
-                       (long unsigned)current_time.tv_usec,
-
-               nmmd->afd.verbose_ls_output
-
-       );
+       WRITE_STATUS_ITEM(&b, SI_FILE_SIZE, "%zu\n", nmmd->size / 1024);
+       WRITE_STATUS_ITEM(&b, SI_MTIME, "%s\n", mtime);
+       WRITE_STATUS_ITEM(&b, SI_STATUS, "%s\n", status);
+       WRITE_STATUS_ITEM(&b, SI_STATUS_FLAGS, "%s\n", flags);
+       WRITE_STATUS_ITEM(&b, SI_OFFSET, "%li\n", offset);
+       WRITE_STATUS_ITEM(&b, SI_AFS_MODE, "%s\n", mmd->afs_mode_string);
+       WRITE_STATUS_ITEM(&b, SI_STREAM_START, "%lu.%lu\n",
+               (long unsigned)nmmd->stream_start.tv_sec,
+               (long unsigned)nmmd->stream_start.tv_usec);
+       WRITE_STATUS_ITEM(&b, SI_CURRENT_TIME, "%lu.%lu\n",
+               (long unsigned)current_time.tv_sec,
+               (long unsigned)current_time.tv_usec);
        free(flags);
        free(status);
        free(ut);
-       return ret;
+       return b.buf;
 }
 
 static int check_sender_args(int argc, char * const * argv, struct sender_command_data *scd)
@@ -294,27 +284,110 @@ int com_version(struct rc4_context *rc4c, int argc, __a_unused char * const * ar
        );
 }
 
+#define EMPTY_STATUS_ITEMS \
+       ITEM(PATH) \
+       ITEM(DIRECTORY) \
+       ITEM(BASENAME) \
+       ITEM(SCORE) \
+       ITEM(ATTRIBUTES_BITMAP) \
+       ITEM(ATTRIBUTES_TXT) \
+       ITEM(HASH) \
+       ITEM(IMAGE_ID) \
+       ITEM(IMAGE_NAME) \
+       ITEM(LYRICS_ID) \
+       ITEM(LYRICS_NAME) \
+       ITEM(BITRATE) \
+       ITEM(FORMAT) \
+       ITEM(FREQUENCY) \
+       ITEM(CHANNELS) \
+       ITEM(DURATION) \
+       ITEM(SECONDS_TOTAL) \
+       ITEM(NUM_PLAYED) \
+       ITEM(LAST_PLAYED) \
+       ITEM(TECHINFO) \
+       ITEM(ARTIST) \
+       ITEM(TITLE) \
+       ITEM(YEAR) \
+       ITEM(ALBUM) \
+       ITEM(COMMENT) \
+       ITEM(AMPLIFICATION)
+
+/**
+ * Write a list of audio-file related status items with empty values.
+ *
+ * This is used by vss when currently no audio file is open.
+ */
+static char *empty_status_items(int parser_friendly)
+{
+       if (parser_friendly)
+               return make_message(
+                       #define ITEM(x) "0004 %02x:\n"
+                       EMPTY_STATUS_ITEMS
+                       #undef ITEM
+                       #define ITEM(x) , SI_ ## x
+                       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
+       );
+}
+#undef EMPTY_STATUS_ITEMS
+
 /* stat */
 int com_stat(struct rc4_context *rc4c, int argc, char * const * argv)
 {
-       int ret, num = 0;/* status will be printed that many
-                         * times. num <= 0 means: print forever
-                         */
+       int i, ret;
        struct misc_meta_data tmp, *nmmd = &tmp;
        char *s;
+       int32_t num = 0;
+       int parser_friendly = 0;
 
        para_sigaction(SIGUSR1, dummy);
 
-       if (argc > 1)
-               num = atoi(argv[1]);
+       for (i = 1; i < argc; i++) {
+               const char *arg = argv[i];
+               if (arg[0] != '-')
+                       break;
+               if (!strcmp(arg, "--")) {
+                       i++;
+                       break;
+               }
+               if (!strncmp(arg, "-n=", 3)) {
+                       ret = para_atoi32(arg + 3, &num);
+                       if (ret < 0)
+                               return ret;
+                       continue;
+               }
+               if (!strcmp(arg, "-p")) {
+                       parser_friendly = 1;
+                       continue;
+               }
+               return -E_COMMAND_SYNTAX;
+       }
+       if (i != argc)
+               return -E_COMMAND_SYNTAX;
        for (;;) {
-
                mmd_dup(nmmd);
-               s = get_status(nmmd);
+               s = get_status(nmmd, parser_friendly);
                ret = rc4_send_buffer(rc4c, s);
                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 = rc4_send_buffer(rc4c, esi);
+                       if (ret < 0)
+                               goto out;
+               } else
+                       send_afs_status(rc4c, parser_friendly);
                ret = 1;
                if (num > 0 && !--num)
                        goto out;
@@ -753,10 +826,14 @@ __noreturn void handle_connect(int fd, const char *peername)
        if (ret < 0)
                goto err_out;
        /* valid command and sufficient perms */
-       argc = split_args(command, &argv, "\n");
+       ret = create_argv(command, "\n", &argv);
+       if (ret < 0)
+               goto err_out;
+       argc = ret;
        PARA_NOTICE_LOG("calling com_%s() for %s@%s\n", cmd->name, u->name,
                        peername);
        ret = cmd->handler(&rc4c, argc, argv);
+       free_argv(argv);
        mutex_lock(mmd_mutex);
        mmd->num_commands++;
        mutex_unlock(mmd_mutex);
@@ -768,7 +845,6 @@ net_err:
        PARA_NOTICE_LOG("%s\n", para_strerror(-ret));
 out:
        free(command);
-       free(argv);
        mutex_lock(mmd_mutex);
        if (cmd && (cmd->perms & AFS_WRITE) && ret >= 0)
                mmd->events++;