From d61eaf0d10dc8f9dd007c3f3e2c506f228a3fede Mon Sep 17 00:00:00 2001 From: Kubat <mael.martin31@gmail.com> Date: Wed, 15 Apr 2020 19:18:49 +0200 Subject: [PATCH] WIP: Adding lkt commands --- src/main/lkt.c | 109 ++++++++++++++++++++++++++++++++++++++++++++----- 1 file changed, 98 insertions(+), 11 deletions(-) diff --git a/src/main/lkt.c b/src/main/lkt.c index ca1875c9..846e7291 100644 --- a/src/main/lkt.c +++ b/src/main/lkt.c @@ -82,10 +82,7 @@ help(void) " Supported types are: title, [a]ny, source, [auth]or, [lang]uage, type, title\n" "\n" " RANGE:\n" - " A range is specified like in python:\n" - " - BEGIN:END which implies from BEGIN to END included\n" - " - :END which implies from the very begining to END included\n" - " - BEGIN: which implies from BEGIN to the very end included\n" + " A range is specified like BEGIN:END which implies from BEGIN to END included\n" "\n"; write(1, help_str, strlen(help_str)); exit(EXIT_SUCCESS); @@ -254,7 +251,7 @@ lkt_skip_key(char *buffer) /* Functions implementing options. */ noreturn void -clear__(struct lkt_cmd_args *args) +queue_clear__(struct lkt_cmd_args *args) { if (args->argc != 0) fail("Invalid argument, the clear command takes no arguments"); @@ -262,6 +259,15 @@ clear__(struct lkt_cmd_args *args) lkt_send_and_exit(cmd__, sizeof(cmd__)); } +noreturn void +queue_crop__(struct lkt_cmd_args *args) +{ + if (args->argc != 0) + fail("Invalid argument, the crop command takes no arguments"); + static const char cmd__[] = "crop\nclose\n"; + lkt_send_and_exit(cmd__, sizeof(cmd__)); +} + noreturn void next__(struct lkt_cmd_args *args) @@ -412,6 +418,47 @@ error: exit(EXIT_FAILURE); } +noreturn void +queue_pop__(struct lkt_cmd_args *args) +{ + if (args->argc != 0) + fail("Invalid argument, the status command takes no arguments"); + + int ret = EXIT_FAILURE, songid = 0; + char buff[LKT_MESSAGE_MAX]; + FILE *sock = lkt_connect(); + + /* Get lektor's status. */ + + if (write_socket(sock, "status\n", sizeof("status\n"))) + goto 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) + goto error; + + size_t len = strcspn(buff, LKT_KEY_VALUE_SEP); + assign_int("songid", songid) + + /* At this point every key has been parsed. */ + if (! strncmp(buff, "OK", 2)) + break; + else if (! strncmp(buff, "ACK", 3)) + goto error; + } + + //fclose(sock); + if (!songid) + goto error; + write_socket_format(sock, "next\ndeleteid %d\n", songid); + ret = EXIT_SUCCESS; +error: + exit(ret); +} + noreturn void status__(struct lkt_cmd_args *args) { @@ -499,7 +546,7 @@ shuffle__(struct lkt_cmd_args *args) } noreturn void -delete__(struct lkt_cmd_args *args) +queue_delete__(struct lkt_cmd_args *args) { if (args->argc != 1) fail("Invalid argument, need onlt one argument"); @@ -565,7 +612,30 @@ add__(struct lkt_cmd_args *args) } noreturn void -list__(struct lkt_cmd_args *args) +queue_seek__(struct lkt_cmd_args *args) +{ + if (args->argc != 1) + fail("The seek command needs one argument"); + + char *endptr, buf[3]; + long id = strtol(args->argv[0], &endptr, 0); + + if ((errno == ERANGE && (id == LONG_MAX || id == LONG_MIN)) || + (errno != 0 && id == 0) || + (endptr == args->argv[0])) + fail("Invalid argument, not an integer"); + + if (*endptr != '\0') + fail("Invalid argument, must be only one integer"); + + FILE *sock = lkt_connect(); + write_socket_format(sock, "playid %ld\n", id); + read_socket(sock, buf, 2); + exit(!strncmp(buf, "OK", 2)); +} + +noreturn void +queue_pos__(struct lkt_cmd_args *args) { char buff[LKT_MESSAGE_MAX], *endptr; @@ -573,7 +643,7 @@ list__(struct lkt_cmd_args *args) args->argv = LKT_QUEUE_DEFAULT; else if (args->argc > 1) - fail("Invalid argument for the queue command"); + fail("Invalid argument for the pos command"); long continuation = 0, up = 0; @@ -808,6 +878,16 @@ search_queue__(struct lkt_cmd_args *args) /* Parsing stuff. */ +static struct lkt_cmd_opt options_queue[] = { + { .name = "pos", .call = queue_pos__ }, + { .name = "pop", .call = queue_pop__ }, + { .name = "seek", .call = queue_seek__ }, + { .name = "delete", .call = queue_delete__ }, + { .name = "clear", .call = queue_clear__ }, + { .name = "crop", .call = queue_crop__ }, + LKT_OPT_NULL, +}; + static struct lkt_cmd_opt options_plt[] = { { .name = "help", .call = help__ }, { .name = "add", .call = plt_add__ }, @@ -828,6 +908,15 @@ static struct lkt_cmd_opt options_search[] = { LKT_OPT_NULL, }; +noreturn void +queue__(struct lkt_cmd_args *args) +{ + if (args->argc == 0) + fail("Invalid argument, you must specify a sub command for the queue command"); + + lkt_cmd_parse(options_queue, args->argc, args->argv, help); +} + noreturn void search__(struct lkt_cmd_args *args) { @@ -848,13 +937,11 @@ plt__(struct lkt_cmd_args *args) static struct lkt_cmd_opt options_[] = { { .name = "help", .call = help__ }, - { .name = "clear", .call = clear__ }, { .name = "current", .call = current__ }, { .name = "play", .call = play__ }, - { .name = "delete", .call = delete__ }, { .name = "next", .call = next__ }, { .name = "previous", .call = prev__ }, - { .name = "queue", .call = list__ }, + { .name = "queue", .call = queue__ }, { .name = "add", .call = add__ }, { .name = "shuffle", .call = shuffle__ }, { .name = "status", .call = status__ }, -- GitLab