Skip to content
Extraits de code Groupes Projets
Vérifiée Valider 4dff7913 rédigé par Kubat's avatar Kubat
Parcourir les fichiers

Correct the lkt queue command

parent 767024dc
Aucune branche associée trouvée
Aucune étiquette associée trouvée
1 requête de fusion!58lkt update
...@@ -642,55 +642,35 @@ queue_seek__(struct lkt_cmd_args *args) ...@@ -642,55 +642,35 @@ queue_seek__(struct lkt_cmd_args *args)
} }
noreturn void noreturn void
queue_list__(struct lkt_cmd_args *args) queue_pos__(struct lkt_cmd_args *args)
{ {
char buff[LKT_MESSAGE_MAX], *endptr; 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) long continuation = 0, up = 0;
args->argv = LKT_QUEUE_DEFAULT;
else if (args->argc != 1)
fail("Invalid argument");
continuation = strtol(args->argv[0], &endptr, 0); continuation = strtol(args->argv[0], &endptr, 0);
if ((errno == ERANGE && (continuation == LONG_MAX || continuation == LONG_MIN)) || (errno != 0 if ((errno == ERANGE && (continuation == LONG_MAX || continuation == LONG_MIN)) || (errno != 0
&& continuation == 0) || (endptr == args->argv[0])) && continuation == 0) || (endptr == args->argv[0]))
fail("Invalid argument, not an integer"); fail("Invalid argument, not an integer");
if (*endptr != '\0') if (*endptr != '\0') {
fail("Invalid argument"); /* A range */
if (*(++endptr) == '\0')
/* Get the current pos to get limits for the playlist command. */ fail("Invalid argument, a range is two integers");
sock = lkt_connect(); up = atoi(endptr);
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);
} }
/* Get the content of the queue. */
song_index = MAX(song_index, 1); FILE *sock = NULL;
fclose(sock);
redo: redo:
sock = lkt_connect(); sock = lkt_connect();
if (up != 0)
write_socket_format(sock, "playlist %d:%d\n", continuation, up); write_socket_format(sock, "playlist %d:%d\n", continuation, up);
else
write_socket_format(sock, "playlist %d\n", continuation);
for (;;) { for (;;) {
memset(buff, 0, LKT_MESSAGE_MAX * sizeof(char)); memset(buff, 0, LKT_MESSAGE_MAX * sizeof(char));
...@@ -714,55 +694,59 @@ redo: ...@@ -714,55 +694,59 @@ redo:
} }
noreturn void noreturn void
queue_pos__(struct lkt_cmd_args *args) queue_list__(struct lkt_cmd_args *args)
{ {
char buff[LKT_MESSAGE_MAX], *endptr; char buff[LKT_MESSAGE_MAX], *endptr;
FILE *sock = NULL;
long continuation = 0, song_index = 1;
if (args->argc != 1) /* Arguments stuff. */
fail("Invalid argument for the pos command");
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); continuation = strtol(args->argv[0], &endptr, 0);
if ((errno == ERANGE && (continuation == LONG_MAX || continuation == LONG_MIN)) || (errno != 0 if ((errno == ERANGE && (continuation == LONG_MAX || continuation == LONG_MIN)) || (errno != 0
&& continuation == 0) || (endptr == args->argv[0])) && continuation == 0) || (endptr == args->argv[0]))
fail("Invalid argument, not an integer"); fail("Invalid argument, not an integer");
if (*endptr != '\0') { if (*endptr != '\0')
/* A range */ fail("Invalid argument");
if (*(++endptr) == '\0')
fail("Invalid argument, a range is two integers");
up = atoi(endptr);
}
/* Get the current pos to get limits for the playlist command. */
FILE *sock = NULL;
redo:
sock = lkt_connect(); sock = lkt_connect();
if (up != 0) if (write_socket(sock, "status\n", sizeof("status\n")))
write_socket_format(sock, "playlist %d:%d\n", continuation, up); fail("Communication error");
else
write_socket_format(sock, "playlist %d\n", continuation); #define assign_int(str, var) if (! strncmp(buff, str, len)) { var = (atoi(lkt_skip_key(buff))); continue; }
for (;;) { for (;;) {
memset(buff, 0, LKT_MESSAGE_MAX * sizeof(char)); 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:"))) { size_t len = strcspn(buff, LKT_KEY_VALUE_SEP);
continuation = atoi(lkt_skip_key(buff)); assign_int("song", song_index);
if (continuation > 0) {
fclose(sock);
goto redo;
}
}
/* At this point every key has been parsed. */
if (! strncmp(buff, "OK", 2)) if (! strncmp(buff, "OK", 2))
exit(EXIT_SUCCESS); break;
else if (! strncmp(buff, "ACK", 3)) else if (! strncmp(buff, "ACK", 3))
exit(EXIT_FAILURE); 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. */ /* Functions implementing options, but for for playlists. */
......
0% Chargement en cours ou .
You are about to add 0 people to the discussion. Proceed with caution.
Veuillez vous inscrire ou vous pour commenter