diff --git a/src/commands.c b/src/commands.c index 6c3477e0932e5e5d489c329f875838db1a8106a1..03baf866f4a48fac22a5241007d86fcf7f08d79c 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 a73019391310d3edee36c335d77d9482e74c6e80..2b54a4d4e238300db2422b1b5fd42de49b87a11e 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 5b04b0011cd5f0d35a5aa4e6a25e6dc54e843a65..834aa48311818e785cfbc4133b37aed220fef297 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));