From fce25c7c4ed4533f70b4506ae07c441071223945 Mon Sep 17 00:00:00 2001
From: Kubat <mael.martin31@gmail.com>
Date: Wed, 15 Apr 2020 17:06:33 +0200
Subject: [PATCH] Second try

---
 src/commands.c       | 18 ++++++++++++------
 src/database/queue.c |  3 +--
 src/main/lkt.c       | 23 ++++++++++++++++++++---
 3 files changed, 33 insertions(+), 11 deletions(-)

diff --git a/src/commands.c b/src/commands.c
index 6c3477e0..03baf866 100644
--- a/src/commands.c
+++ b/src/commands.c
@@ -885,7 +885,7 @@ bool
 command_queue_list(struct lkt_state *srv, size_t c, char *args[LKT_MESSAGE_ARGS_MAX])
 {
     unsigned int count, from, to, tmp_switch;
-    long val, ret;
+    long val;
     char *endptr, *str;
     struct lkt_queue_state state;
     struct lkt_callback callback = {
@@ -963,17 +963,23 @@ is_relative:
 
     if (count < lkt_remaining_msg(srv, c) - 1) {
         lkt_set_continuation(srv, c, 0);
-        return database_queue_list(srv->db, state.current, state.current+ count, &callback);
+        return database_queue_list(srv->db, state.current, state.current + count, &callback);
     } else {
         count = lkt_remaining_msg(srv, c) - 2;  /* One for `continue: i` and one for OK/ACK. */
-        ret = database_queue_list(srv->db, state.current, state.current + count, &callback);
-        return ret;
+        lkt_set_continuation(srv, c, state.current + count);
+        return database_queue_list(srv->db, state.current, state.current + count, &callback);
     }
 
     /* The command is used with a range specifier. */
 is_absolute:
-    ret = database_queue_list(srv->db, from, to, &callback);
-    return ret;
+    if (to - from < lkt_remaining_msg(srv, c) - 1) {
+        lkt_set_continuation(srv, c, 0);
+        return database_queue_list(srv->db, from, to, &callback);
+    } else {
+        count = lkt_remaining_msg(srv, c) - 2 + from;
+        lkt_set_continuation(srv, c, count);
+        return database_queue_list(srv->db, from, count, &callback);
+    }
 }
 
 bool
diff --git a/src/database/queue.c b/src/database/queue.c
index a7301939..2b54a4d4 100644
--- a/src/database/queue.c
+++ b/src/database/queue.c
@@ -79,9 +79,8 @@ database_queue_current_kara(sqlite3 *db, struct kara_metadata *res, int *id)
         res->song_number = sqlite3_column_int(stmt, 6);
 no_metadata:
         /* Most of the time this will be NULL. */
-        if (id && sqlite3_column_type(stmt, 7) != SQLITE_NULL) {
+        if (id && sqlite3_column_type(stmt, 7) != SQLITE_NULL)
             *id = sqlite3_column_int(stmt, 7);
-        }
     } else {
         fprintf(stderr, " ! database_queue_current_kara: failed: %s\n",
                 sqlite3_errmsg(db));
diff --git a/src/main/lkt.c b/src/main/lkt.c
index 5b04b001..834aa483 100644
--- a/src/main/lkt.c
+++ b/src/main/lkt.c
@@ -573,7 +573,7 @@ add__(struct lkt_cmd_args *args)
 noreturn void
 list__(struct lkt_cmd_args *args)
 {
-    char buff[LKT_MESSAGE_MAX];
+    char buff[LKT_MESSAGE_MAX], *endptr;
 
     if (args->argc == 0)
         args->argv = LKT_QUEUE_DEFAULT;
@@ -581,11 +581,28 @@ list__(struct lkt_cmd_args *args)
     else if (args->argc > 1)
         fail("Invalid argument for the queue command");
 
-    int continuation = 0;
+    long continuation = 0, up = 0;
+
+    continuation = strtol(args->argv[0], &endptr, 0);
+    if ((errno == ERANGE && (continuation == LONG_MAX || continuation == LONG_MIN)) || (errno != 0
+            && continuation == 0) || (endptr == args->argv[0]))
+        fail("Invalid argument, not an integer");
+
+    if (*endptr != '\0') {
+        /* A range */
+        if (*(++endptr) == '\0')
+            fail("Invalid argument, a range is two integers");
+        up = atoi(endptr);
+    }
+
+
     FILE *sock = NULL;
 redo:
     sock = lkt_connect();
-    write_socket_format(sock, "%d playlist %s\n", continuation, args->argv[0]);
+    if (up != 0)
+        write_socket_format(sock, "playlist %d:%d\n", continuation, up);
+    else
+        write_socket_format(sock, "playlist %d\n", continuation);
 
     for (;;) {
         memset(buff, 0, LKT_MESSAGE_MAX * sizeof(char));
-- 
GitLab