diff --git a/src/main/lkt.c b/src/main/lkt.c index 1d1adf83400c1ae201ccb011b33b80ac07438c33..99a92990030656a1fa572719556cb6e099294faf 100644 --- a/src/main/lkt.c +++ b/src/main/lkt.c @@ -642,55 +642,35 @@ queue_seek__(struct lkt_cmd_args *args) } noreturn void -queue_list__(struct lkt_cmd_args *args) +queue_pos__(struct lkt_cmd_args *args) { char buff[LKT_MESSAGE_MAX], *endptr; - FILE *sock = NULL; - long continuation = 0, up = 0, song_index = 1; - /* Arguments stuff. */ + if (args->argc != 1) + fail("Invalid argument for the pos command"); - if (args->argc == 0) - args->argv = LKT_QUEUE_DEFAULT; - else if (args->argc != 1) - fail("Invalid argument"); + 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') - fail("Invalid argument"); - - /* Get the current pos to get limits for the playlist command. */ - sock = lkt_connect(); - if (write_socket(sock, "status\n", sizeof("status\n"))) - fail("Communication error"); - -#define assign_int(str, var) if (! strncmp(buff, str, len)) { var = (atoi(lkt_skip_key(buff))); continue; } - - for (;;) { - memset(buff, 0, LKT_MESSAGE_MAX * sizeof(char)); - if (read_socket(sock, buff, LKT_MESSAGE_MAX - 1) <= 0) - fail("Connextion error"); - - size_t len = strcspn(buff, LKT_KEY_VALUE_SEP); - assign_int("song", song_index); - - /* At this point every key has been parsed. */ - if (! strncmp(buff, "OK", 2)) - break; - else if (! strncmp(buff, "ACK", 3)) - exit(EXIT_FAILURE); + if (*endptr != '\0') { + /* A range */ + if (*(++endptr) == '\0') + fail("Invalid argument, a range is two integers"); + up = atoi(endptr); } - /* Get the content of the queue. */ - song_index = MAX(song_index, 1); - fclose(sock); + + FILE *sock = NULL; redo: sock = lkt_connect(); - write_socket_format(sock, "playlist %d:%d\n", continuation, up); + 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)); @@ -714,55 +694,59 @@ redo: } noreturn void -queue_pos__(struct lkt_cmd_args *args) +queue_list__(struct lkt_cmd_args *args) { char buff[LKT_MESSAGE_MAX], *endptr; + FILE *sock = NULL; + long continuation = 0, song_index = 1; - if (args->argc != 1) - fail("Invalid argument for the pos command"); + /* Arguments stuff. */ - long continuation = 0, up = 0; + if (args->argc == 0) + args->argv = LKT_QUEUE_DEFAULT; + else if (args->argc != 1) + fail("Invalid argument"); 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); - } + if (*endptr != '\0') + fail("Invalid argument"); + /* Get the current pos to get limits for the playlist command. */ - FILE *sock = NULL; -redo: sock = lkt_connect(); - if (up != 0) - write_socket_format(sock, "playlist %d:%d\n", continuation, up); - else - write_socket_format(sock, "playlist %d\n", continuation); + if (write_socket(sock, "status\n", sizeof("status\n"))) + fail("Communication error"); + +#define assign_int(str, var) if (! strncmp(buff, str, len)) { var = (atoi(lkt_skip_key(buff))); continue; } for (;;) { memset(buff, 0, LKT_MESSAGE_MAX * sizeof(char)); - assert(read_socket(sock, buff, LKT_MESSAGE_MAX - 1) > 0); + if (read_socket(sock, buff, LKT_MESSAGE_MAX - 1) <= 0) + fail("Connextion error"); - if (! strncmp(buff, "continue:", strlen("continue:"))) { - continuation = atoi(lkt_skip_key(buff)); - if (continuation > 0) { - fclose(sock); - goto redo; - } - } + size_t len = strcspn(buff, LKT_KEY_VALUE_SEP); + assign_int("song", song_index); + /* At this point every key has been parsed. */ if (! strncmp(buff, "OK", 2)) - exit(EXIT_SUCCESS); + break; else if (! strncmp(buff, "ACK", 3)) exit(EXIT_FAILURE); - - fprintf(stdout, "%s", buff); } + fclose(sock); + + /* Get the content of the queue. */ + + song_index = MAX(song_index + 1, 1); + snprintf(buff, LKT_MESSAGE_MAX - 1, "%ld:%ld", song_index, song_index + continuation - 1); + buff[LKT_MESSAGE_MAX - 1] = '\0'; + args->argc = 1; + args->argv[0] = buff; + queue_pos__(args); } /* Functions implementing options, but for for playlists. */