From c948e1e6b3c3c82369ba9b703795996f4d0ab9cb Mon Sep 17 00:00:00 2001
From: Kubat <mael.martin31@gmail.com>
Date: Sat, 1 May 2021 23:15:40 +0200
Subject: [PATCH] LKT: Use 'common' macros to simplify the lkt code and make it
 a bit easier to read...

NOTE: Clean this messy code...
---
 src/main/lkt.c | 382 ++++++++++++++++++++++---------------------------
 1 file changed, 175 insertions(+), 207 deletions(-)

diff --git a/src/main/lkt.c b/src/main/lkt.c
index 687f4515..748cbd19 100644
--- a/src/main/lkt.c
+++ b/src/main/lkt.c
@@ -15,26 +15,20 @@
 #include <signal.h>
 
 #define LKT_KEY_VALUE_SEP ": \n\t\0"
-#define fail_if(cond, msg)                                                                                             \
-    {                                                                                                                  \
-        if (cond) {                                                                                                    \
-            LOG_ERROR("LKT", "%s", msg);                                                                               \
-            exit(EXIT_FAILURE);                                                                                        \
-        }                                                                                                              \
-    }
-#define fail(msg)                                                                                                      \
-    {                                                                                                                  \
-        LOG_ERROR("LKT", "%s", msg);                                                                                   \
-        exit(EXIT_FAILURE);                                                                                            \
-    }
 
-#define exit_with_status(sock, buff)                                                                                   \
+#define ___EXIT_WITH_STATUS(sock, buff)                                                                                \
     {                                                                                                                  \
         read_socket(sock, buff, 2);                                                                                    \
-        fail_if(buff[0] != 'O' && buff[1] != 'K', "ACK");                                                              \
+        FAIL_IF(buff[0] != 'O' && buff[1] != 'K', "ACK");                                                              \
         exit(EXIT_SUCCESS);                                                                                            \
     }
 
+#define ___HANDLE_STATUS(buffer, ok)                                                                                   \
+    if (STR_NMATCH(buffer, "OK", 2)) {                                                                                 \
+        ok;                                                                                                            \
+    } else if (STR_NMATCH(buffer, "ACK", 3))                                                                           \
+        exit(EXIT_FAILURE);
+
 /* Type definition. */
 
 typedef struct {
@@ -55,7 +49,7 @@ static const char *LKT_QUEUE_DEFAULT[] = { "10" };
 /* Communication functions and fonction that interact with lektor stuff. */
 
 static inline void
-__concat_strings(char *const res, size_t len, const char **tab, size_t count)
+___concat_strings(char *const res, size_t len, const char **tab, size_t count)
 {
     size_t i;
     for (i = 0; i < count - 1; ++i) {
@@ -76,9 +70,8 @@ lkt_get_query_type(struct cmd_args *args, char *const regex, size_t regex_len)
     /* id, type, lang, category */
     if (args->argc == 1) {
         /* Type ?== id */
-        long id = 0;
         errno   = 0;
-        id      = strtol(args->argv[0], NULL, 10);
+        long id = strtol(args->argv[0], NULL, 10);
         if (id != 0 && errno == 0) {
             safe_snprintf(regex, regex_len, "id://%ld", id);
             return 0;
@@ -113,14 +106,14 @@ lkt_get_query_type(struct cmd_args *args, char *const regex, size_t regex_len)
         /* Type ?== author */
         if (STR_MATCH("author", args->argv[0])) {
             strncat(regex, "author://", regex_len - 1);
-            __concat_strings(regex, regex_len, args->argv + 1, args->argc - 1);
+            ___concat_strings(regex, regex_len, args->argv + 1, args->argc - 1);
             return 0;
         }
 
         /* Type ?== playlist */
         else if (STR_MATCH("plt", args->argv[0]) || STR_MATCH("playlist", args->argv[0])) {
             strncat(regex, "playlist://", regex_len - 1);
-            __concat_strings(regex, regex_len, args->argv + 1, args->argc - 1);
+            ___concat_strings(regex, regex_len, args->argv + 1, args->argc - 1);
             return 0;
         }
 
@@ -149,7 +142,7 @@ lkt_get_query_type(struct cmd_args *args, char *const regex, size_t regex_len)
     /* If 'query' is specified, skip it */
     int index = STR_MATCH("query", args->argv[0]) ? 1 : 0;
     strncat(regex, "query://%", regex_len - 1);
-    __concat_strings(regex, regex_len, args->argv + index, args->argc - index);
+    ___concat_strings(regex, regex_len, args->argv + index, args->argc - index);
     strncat(regex, "%", regex_len - 1);
     return 0;
 }
@@ -163,7 +156,7 @@ read_socket(FILE *sock, char *buff, size_t max_len)
         if (buff[i] == '\n' || len != 1)
             break;
     }
-    fail_if(len == 0, "Connexion error");
+    FAIL_IF(len == 0, "Connexion error");
     return i;
 }
 
@@ -212,9 +205,9 @@ create_socket(const char *host, const char *port)
 
     /* Common part */
     cx = socket(domain, SOCK_STREAM, 0);
-    fail_if(cx <= 0 || connect(cx, ptr_sock, sock_len), "Connect to socket failed");
+    FAIL_IF(cx <= 0 || connect(cx, ptr_sock, sock_len), "Connect to socket failed");
     socket_desc = fdopen(cx, "r+");
-    fail_if(!socket_desc, "Failed to connect to lektord");
+    FAIL_IF(!socket_desc, "Failed to connect to lektord");
     return socket_desc;
 }
 
@@ -235,14 +228,10 @@ write_socket(FILE *sock, const char *format, ...)
     va_list ap;
 
     va_start(ap, format);
-    size                      = vsnprintf(buff, LKT_MESSAGE_MAX - 1, format, ap);
-    buff[LKT_MESSAGE_MAX - 1] = 0;
-
-    if (size < 0)
-        fail("Connexion error");
+    size = safe_vsnprintf(buff, LKT_MESSAGE_MAX - 1, format, ap);
 
-    if (fwrite(buff, sizeof(char), size, sock) < (unsigned int)size)
-        fail("Connexion error");
+    FAIL_IF(size < 0, "Connexion error");
+    FAIL_IF(fwrite(buff, sizeof(char), size, sock) < (unsigned int)size, "Connexion error");
 
     va_end(ap);
 }
@@ -271,8 +260,9 @@ send_cmd_with_uri(FILE *sock, char *cmd, int argc, const char **argv)
         .argc = argc,
         .argv = argv,
     };
+
     memset(buf, 0, LKT_MESSAGE_MAX * sizeof(char));
-    fail_if(lkt_get_query_type(&args, buf, LKT_MESSAGE_MAX), "Query is invalid");
+    FAIL_IF(lkt_get_query_type(&args, buf, LKT_MESSAGE_MAX), "Query is invalid");
     write_socket(sock, "%s %s\n", cmd, buf);
 }
 
@@ -281,46 +271,59 @@ send_cmd_with_uri(FILE *sock, char *cmd, int argc, const char **argv)
 #define just_send_two_args(func, cmd)                                                                                  \
     EXIT_FUNCTION func(struct cmd_args *args)                                                                          \
     {                                                                                                                  \
-        fail_if(args->argc != 2, "Need two arguments");                                                                \
+        FAIL_IF(args->argc != 2, "Need two arguments");                                                                \
         FILE *sock = lkt_connect();                                                                                    \
         char buff[2];                                                                                                  \
         write_socket(sock, cmd " %s %s\n", args->argv[0], args->argv[1]);                                              \
-        exit_with_status(sock, buff);                                                                                  \
+        ___EXIT_WITH_STATUS(sock, buff);                                                                               \
     }
-just_send_two_args(queue_swap__, "swap") just_send_two_args(queue_swapid__, "swapid");
+// clang-format off
+just_send_two_args(queue_swap__,   "swap");
+just_send_two_args(queue_swapid__, "swapid");
+// clang-format on
 #undef just_send_two_args
 
 #define just_send_one_arg(func, cmd)                                                                                   \
     EXIT_FUNCTION func(struct cmd_args *args)                                                                          \
     {                                                                                                                  \
-        fail_if(args->argc != 1, "Invalid argument");                                                                  \
+        FAIL_IF(args->argc != 1, "Invalid argument");                                                                  \
         FILE *sock = lkt_connect();                                                                                    \
         char buff[2];                                                                                                  \
         write_socket(sock, cmd " %s\n", args->argv[0]);                                                                \
-        exit_with_status(sock, buff);                                                                                  \
+        ___EXIT_WITH_STATUS(sock, buff);                                                                               \
     }
-just_send_one_arg(stickers_create__, "sticker __create") just_send_one_arg(stickers_destroy__, "sticker __destroy");
-just_send_one_arg(plt_destroy__, "rm") just_send_one_arg(plt_create__, "playlistadd");
-just_send_one_arg(queue_dump__, "__dump");
+// clang-format off
+just_send_one_arg(stickers_create__,  "sticker __create");
+just_send_one_arg(stickers_destroy__, "sticker __destroy");
+just_send_one_arg(plt_destroy__,      "rm");
+just_send_one_arg(plt_create__,       "playlistadd");
+just_send_one_arg(queue_dump__,       "__dump");
+// clang-format on
 #undef just_send_one_arg
 
 #define just_send(func, msg) /* Just send a simple string functions */                                                 \
     EXIT_FUNCTION func(struct cmd_args *args)                                                                          \
     {                                                                                                                  \
-        fail_if(args->argc, "This command takes no arguments");                                                        \
+        FAIL_IF(args->argc, "This command takes no arguments");                                                        \
         lkt_send_and_exit(msg, sizeof(msg));                                                                           \
     }
-just_send(queue_clear__, "clear\n") just_send(queue_crop__, "crop\n") just_send(next__, "next\n");
-just_send(prev__, "previous\n") just_send(stop__, "stop\n") just_send(pause__, "pause 1\n");
-just_send(unpause__, "pause 0\n") just_send(shuffle__, "shuffle\npause 0\n");
+// clang-format off
+just_send(queue_clear__,   "clear\n");
+just_send(queue_crop__,    "crop\n");
+just_send(next__,          "next\n");
+just_send(prev__,          "previous\n");
+just_send(stop__,          "stop\n");
+just_send(pause__,         "pause 1\n");
+just_send(unpause__,       "pause 0\n");
+just_send(shuffle__,       "shuffle\npause 0\n");
 just_send(queue_flatten__, "__flat\n");
+// clang-format on
 #undef just_send
 
 EXIT_FUNCTION
 simple_send_with_password__(const char *cmd)
 {
-    if (!password)
-        fail("Password not provided");
+    FAIL_UNLESS(password, "Password not provided");
     FILE *sock = lkt_connect();
     write_socket(sock, "command_list_begin\n");
     write_socket(sock, "password %s\n", password);
@@ -332,24 +335,21 @@ simple_send_with_password__(const char *cmd)
 EXIT_FUNCTION
 restart__(struct cmd_args *args)
 {
-    if (args->argc != 0)
-        fail("Invalid argument, the previous command takes no arguments");
+    FAIL_IF(args->argc != 0, "Invalid argument, the previous command takes no arguments");
     simple_send_with_password__("__restart");
 }
 
 EXIT_FUNCTION
 kill__(struct cmd_args *args)
 {
-    if (args->argc != 0)
-        fail("Invalid argument, the previous command takes no arguments");
+    FAIL_IF(args->argc != 0, "Invalid argument, the previous command takes no arguments");
     simple_send_with_password__("kill");
 }
 
 EXIT_FUNCTION
 rescan_or_update__(struct cmd_args *args, const char *cmd)
 {
-    if (!password)
-        fail("Password not provided");
+    FAIL_UNLESS(password, "Password not provided");
     FILE *sock = lkt_connect();
 
     /* All the db */
@@ -364,7 +364,7 @@ rescan_or_update__(struct cmd_args *args, const char *cmd)
     else {
         char buff[LKT_MESSAGE_MAX];
         memset(buff, 0, LKT_MESSAGE_MAX * sizeof(char));
-        fail_if(lkt_get_query_type(args, buff, LKT_MESSAGE_MAX), "Query is not valid");
+        FAIL_IF(lkt_get_query_type(args, buff, LKT_MESSAGE_MAX), "Query is not valid");
         write_socket(sock, "command_list_begin\n");
         write_socket(sock, "password %s\n", password);
         write_socket(sock, "%s %s\n", cmd, buff);
@@ -401,13 +401,12 @@ populate__(struct cmd_args *args)
 EXIT_FUNCTION
 queue_replace__(struct cmd_args *args)
 {
-    if (args->argc != 1)
-        fail("Invalid argument");
-    bool play = false;
+    FAIL_IF(args->argc != 1, "Invalid argument");
     char buff[LKT_MESSAGE_MAX];
+    bool play  = false;
     FILE *sock = lkt_connect();
 
-    for (;;) {
+    FOR_EVER {
         memset(buff, 0, LKT_MESSAGE_MAX * sizeof(char));
         read_socket(sock, buff, LKT_MESSAGE_MAX - 1);
         size_t len = strcspn(buff, LKT_KEY_VALUE_SEP);
@@ -421,7 +420,7 @@ queue_replace__(struct cmd_args *args)
         if (STR_NMATCH(buff, "OK", 2))
             break;
         else if (STR_NMATCH(buff, "ACK", 3))
-            fail("ACK");
+            LOG_FATAL("ACK");
     }
 
     fclose(sock);
@@ -432,7 +431,7 @@ queue_replace__(struct cmd_args *args)
     if (play)
         write_socket(sock, "play\n");
     write_socket(sock, "command_list_end\n");
-    exit_with_status(sock, buff);
+    ___EXIT_WITH_STATUS(sock, buff);
 }
 
 EXIT_FUNCTION
@@ -447,12 +446,9 @@ config__(struct cmd_args *args)
 EXIT_FUNCTION
 play__(struct cmd_args *args)
 {
-    if (args->argc > 1)
-        fail("Invalid argument");
+    FAIL_IF(args->argc > 1, "Invalid argument");
 
-    int pos = 0;
-    if (args->argc != 0)
-        pos = atoi(args->argv[0]);
+    int pos = (args->argc != 0) ? atoi(args->argv[0]) : 0;
 
     static const char status__[]        = "status\n";
     static const char cmd_play__[]      = "play\n";
@@ -463,7 +459,7 @@ play__(struct cmd_args *args)
     FILE *sock = lkt_connect();
     write_socket(sock, status__);
 
-    for (;;) {
+    FOR_EVER {
         memset(buff, 0, LKT_MESSAGE_MAX * sizeof(char));
         read_socket(sock, buff, LKT_MESSAGE_MAX - 1);
         size_t len = strcspn(buff, LKT_KEY_VALUE_SEP);
@@ -491,19 +487,18 @@ play__(struct cmd_args *args)
 EXIT_FUNCTION
 ping__(struct cmd_args *args)
 {
-    if (args->argc != 0)
-        fail("Invalid argument, the ping command takes no arguments");
-    char buff[6] = { 0 };
-    FILE *sock   = lkt_connect();
+    FAIL_IF(args->argc != 0, "Invalid argument, the ping command takes no arguments");
+    char buff[6];
+    FILE *sock = lkt_connect();
+    memset(buff, 0, sizeof(buff));
     write_socket(sock, "ping\n");
     read_socket(sock, buff, 6 * sizeof(char));
-    if (!STR_NMATCH(buff, "OK", 2))
-        fail("ACK");
+    FAIL_UNLESS(STR_NMATCH(buff, "OK", 2), "ACK");
     exit(write(1, "OK\n", sizeof("OK\n")) == 0);
 }
 
-static EXIT_FUNCTION
-__read_kara_and_exit(FILE *sock)
+PRIVATE_FUNCTION EXIT_FUNCTION
+___read_kara_and_exit(FILE *sock)
 {
     char *mem = NULL;
     char buff[LKT_MESSAGE_MAX];
@@ -517,7 +512,11 @@ __read_kara_and_exit(FILE *sock)
     char *const category = &mem[4 * LKT_MESSAGE_MAX];
     char *const language = &mem[5 * LKT_MESSAGE_MAX];
 
-    for (;;) {
+#define ___assign_mdt(what)                                                                                            \
+    if (STR_NMATCH(#what, buff, len))                                                                                  \
+        assert(memcpy(what, value, value_size));
+
+    FOR_EVER {
         memset(buff, 0, LKT_MESSAGE_MAX * sizeof(char));
         read_socket(sock, buff, LKT_MESSAGE_MAX - 1);
 
@@ -525,31 +524,19 @@ __read_kara_and_exit(FILE *sock)
         char *value             = lkt_skip_key(buff);
         const size_t value_size = strcspn(value, "\n\0") * sizeof(char);
 
-        if (STR_NMATCH("title", buff, len))
-            assert(memcpy(title, value, value_size));
-
-        if (STR_NMATCH("author", buff, len))
-            assert(memcpy(author, value, value_size));
-
-        if (STR_NMATCH("source", buff, len))
-            assert(memcpy(source, value, value_size));
-
-        if (STR_NMATCH("type", buff, len))
-            assert(memcpy(type, value, value_size));
-
-        if (STR_NMATCH("language", buff, len))
-            assert(memcpy(language, value, value_size));
-
-        if (STR_NMATCH("category", buff, len))
-            assert(memcpy(category, value, value_size));
+        ___assign_mdt(title);
+        ___assign_mdt(author);
+        ___assign_mdt(source);
+        ___assign_mdt(type);
+        ___assign_mdt(language);
+        ___assign_mdt(category);
 
         /* At this point every key has been parsed. */
-        if (STR_NMATCH(buff, "OK", 2))
-            goto ok;
-        else if (STR_NMATCH(buff, "ACK", 3))
-            exit(EXIT_FAILURE);
+        ___HANDLE_STATUS(buff, { goto ok; });
     }
 
+#undef ___assign_mdt
+
 ok:
     if (language[0])
         printf("%s - %s / %s - %s - %s [%s]\n", category, language, source, type, title, author);
@@ -563,30 +550,28 @@ ok:
 EXIT_FUNCTION
 queue_id__(struct cmd_args *args)
 {
-    if (args->argc != 1)
-        fail("Invalid argument, the current command takes one argument");
+    FAIL_IF(args->argc != 1, "Invalid argument, the current command takes one argument");
     char probe_id__[LKT_LINE_MAX];
     safe_snprintf(probe_id__, LKT_LINE_MAX, "playlistid %ld\n", strtol(args->argv[0], NULL, 0));
     FILE *sock = lkt_connect();
     write_socket(sock, probe_id__);
-    __read_kara_and_exit(sock);
+    ___read_kara_and_exit(sock);
 }
 
 EXIT_FUNCTION
 current__(struct cmd_args *args)
 {
-    if (args->argc != 0)
-        fail("Invalid argument, the current command takes no arguments");
+    FAIL_IF(args->argc != 0, "Invalid argument, the current command takes no arguments");
     static const char current_song__[] = "currentsong\n";
     FILE *sock                         = lkt_connect();
     write_socket(sock, current_song__);
-    __read_kara_and_exit(sock);
+    ___read_kara_and_exit(sock);
 }
 
 EXIT_FUNCTION
 queue_pop__(struct cmd_args *args)
 {
-    fail_if(args->argc, "Invalid argument");
+    FAIL_IF(args->argc, "Invalid argument");
     int song = 0;
     char buff[LKT_MESSAGE_MAX];
     FILE *sock = lkt_connect();
@@ -599,17 +584,14 @@ queue_pop__(struct cmd_args *args)
         var = (atoi(lkt_skip_key(buff)));                                                                              \
         continue;                                                                                                      \
     }
-    for (;;) {
+    FOR_EVER {
         memset(buff, 0, LKT_MESSAGE_MAX * sizeof(char));
         read_socket(sock, buff, LKT_MESSAGE_MAX - 1);
         size_t len = strcspn(buff, LKT_KEY_VALUE_SEP);
         assign_int("song", song);
 
         /* At this point every key has been parsed. */
-        if (STR_NMATCH(buff, "OK", 2))
-            break;
-        else if (STR_NMATCH(buff, "ACK", 3))
-            exit(EXIT_FAILURE);
+        ___HANDLE_STATUS(buff, { break; });
     }
 #undef assign_int
     song += 1; /* Needs the +1, see status command */
@@ -619,13 +601,13 @@ queue_pop__(struct cmd_args *args)
     if (!song)
         exit(EXIT_FAILURE);
     write_socket(sock, "next\ndelete %d\n", song);
-    exit_with_status(sock, buff);
+    ___EXIT_WITH_STATUS(sock, buff);
 }
 
 EXIT_FUNCTION
 status__(struct cmd_args *args)
 {
-    fail_if(args->argc != 0, "Invalid argument, the status command takes no arguments");
+    FAIL_IF(args->argc != 0, "Invalid argument, the status command takes no arguments");
 
     static const char *const status_str__ = "status\n";
     static const char *const stats_str__  = "stats\n";
@@ -634,7 +616,7 @@ status__(struct cmd_args *args)
     char flags[24];
 
     bool play = false, stopped = true, is_updating = false;
-    int time_pos = 0, time_duration = 0, song_index = 0, plt_len = 0, ret = EXIT_FAILURE, it = 0;
+    int time_pos = 0, time_duration = 0, song_index = 0, plt_len = 0, it = 0;
     int update_count = 0, update_tick = 0;
     size_t len;
     time_t update_ts = 0;
@@ -643,42 +625,28 @@ status__(struct cmd_args *args)
     FILE *sock = lkt_connect();
 
 #define assign_flag(str, f)                                                                                            \
-    {                                                                                                                  \
-        if (STR_NMATCH(buff, str, len) && (atoi(lkt_skip_key(buff)) == 1)) {                                           \
-            flags[it++] = f;                                                                                           \
-            continue;                                                                                                  \
-        }                                                                                                              \
+    if (STR_NMATCH(buff, str, len) && (atoi(lkt_skip_key(buff)) == 1)) {                                               \
+        flags[it++] = f;                                                                                               \
+        continue;                                                                                                      \
     }
 #define assign_two_int(str, var1, var2)                                                                                \
-    {                                                                                                                  \
-        if (STR_NMATCH(buff, str, len)) {                                                                              \
-            char *endptr = NULL;                                                                                       \
-            var1         = (strtoll(lkt_skip_key(buff), &endptr, 0));                                                  \
-            endptr += strspn(endptr, "-/:");                                                                           \
-            if (endptr)                                                                                                \
-                var2 = (strtoll(endptr, NULL, 0));                                                                     \
-            continue;                                                                                                  \
-        }                                                                                                              \
+    if (STR_NMATCH(buff, str, len)) {                                                                                  \
+        char *endptr = NULL;                                                                                           \
+        var1         = (strtoll(lkt_skip_key(buff), &endptr, 0));                                                      \
+        endptr += strspn(endptr, "-/:");                                                                               \
+        if (endptr)                                                                                                    \
+            var2 = (strtoll(endptr, NULL, 0));                                                                         \
+        continue;                                                                                                      \
     }
 #define assign_int(str, var)                                                                                           \
-    {                                                                                                                  \
-        if (STR_NMATCH(buff, str, len)) {                                                                              \
-            var = (strtoll(lkt_skip_key(buff), NULL, 0));                                                              \
-            continue;                                                                                                  \
-        }                                                                                                              \
-    }
-#define check_end()                                                                                                    \
-    {                                                                                                                  \
-        if (STR_NMATCH(buff, "OK", 2)) {                                                                               \
-            break;                                                                                                     \
-        } else if (STR_NMATCH(buff, "ACK", 3)) {                                                                       \
-            goto error;                                                                                                \
-        }                                                                                                              \
+    if (STR_NMATCH(buff, str, len)) {                                                                                  \
+        var = (strtoll(lkt_skip_key(buff), NULL, 0));                                                                  \
+        continue;                                                                                                      \
     }
 
     /* Get lektor's status */
     write_socket(sock, status_str__);
-    for (;;) {
+    FOR_EVER {
         len       = read_socket(sock, buff, LKT_MESSAGE_MAX - 1);
         buff[len] = '\0';
         len       = strcspn(buff, LKT_KEY_VALUE_SEP);
@@ -694,19 +662,19 @@ status__(struct cmd_args *args)
         assign_int("elapsed", time_pos) assign_int("duration", time_duration) assign_int("song", song_index);
         assign_int("playlistlength", plt_len);
 
-        check_end();
+        ___HANDLE_STATUS(buff, { break; });
     }
 
     /* Get lektor's stats */
     write_socket(sock, stats_str__);
-    for (;;) {
+    FOR_EVER {
         len       = read_socket(sock, buff, LKT_MESSAGE_MAX - 1);
         buff[len] = '\0';
         len       = strcspn(buff, LKT_KEY_VALUE_SEP);
 
         assign_int("__is_updating", is_updating) assign_int("db_update", update_ts);
         assign_two_int("__update_progress", update_tick, update_count);
-        check_end();
+        ___HANDLE_STATUS(buff, { break; });
     }
 
     /* End of communication */
@@ -731,9 +699,10 @@ status__(struct cmd_args *args)
            "Playback: %d:%02d/%d:%02d\n"
            "Flags: %s\n",
            play ? "play" : "stopped", song_index + 1, plt_len, pla_m, pla_s, dur_m, dur_s, flags[0] ? flags : "(none)");
-    if (is_updating) {
+
+    if (is_updating)
         printf("Updating: %d/%d%s\n", update_tick, update_count, update_count >= update_tick ? "" : " (invalid)");
-    } else
+    else
         printf("Last update: %s\n", buff);
 
     /* If there is a kara loaded in mpv, get this kara. */
@@ -742,16 +711,13 @@ status__(struct cmd_args *args)
         current__(args);
     }
 
-    ret = EXIT_SUCCESS;
-error:
-    exit(ret);
+    exit(EXIT_SUCCESS);
 }
 
 EXIT_FUNCTION
 queue_remove__(struct cmd_args *args)
 {
-    if (args->argc != 1)
-        fail("Invalid argument, need onlt one argument: queue id <id>");
+    FAIL_IF(args->argc != 1, "Invalid argument, need onlt one argument: queue id <id>");
 
     static const char *cmd__ = "deleteid %d\n";
     int dumy                 = 0;
@@ -761,17 +727,16 @@ queue_remove__(struct cmd_args *args)
     sscanf(args->argv[0], "%d", &dumy);
     if (dumy != 0) {
         write_socket(sock, cmd__, dumy);
-        exit_with_status(sock, buff);
+        ___EXIT_WITH_STATUS(sock, buff);
     }
 
-    fail("Invalid argument");
+    LOG_FATAL("Invalid argument");
 }
 
 EXIT_FUNCTION
 queue_delete__(struct cmd_args *args)
 {
-    if (args->argc != 1)
-        fail("Invalid argument, need onlt one argument: queue delete <pos>");
+    FAIL_IF(args->argc != 1, "Invalid argument, need onlt one argument: queue delete <pos>");
 
     static const char *cmd_id__ = "delete %d\n";
     int dumy                    = 0;
@@ -781,49 +746,49 @@ queue_delete__(struct cmd_args *args)
     sscanf(args->argv[0], "%d", &dumy);
     if (dumy != 0) {
         write_socket(sock, cmd_id__, dumy);
-        exit_with_status(sock, buff);
+        ___EXIT_WITH_STATUS(sock, buff);
     }
 
-    fail("Invalid argument");
+    LOG_FATAL("Invalid argument");
 }
 
 EXIT_FUNCTION
 queue_add__(struct cmd_args *args)
 {
     char buff[3];
-    fail_if(args->argc < 1, "Invalid arguments");
+    FAIL_IF(args->argc < 1, "Invalid arguments");
     FILE *sock = lkt_connect();
     send_cmd_with_uri(sock, "add", args->argc, args->argv);
-    exit_with_status(sock, buff);
+    ___EXIT_WITH_STATUS(sock, buff);
 }
 
 EXIT_FUNCTION
 queue_insert__(struct cmd_args *args)
 {
     char buff[3];
-    fail_if(args->argc < 1, "Invalid arguments");
+    FAIL_IF(args->argc < 1, "Invalid arguments");
     FILE *sock = lkt_connect();
     send_cmd_with_uri(sock, "__insert", args->argc, args->argv);
-    exit_with_status(sock, buff);
+    ___EXIT_WITH_STATUS(sock, buff);
 }
 
 EXIT_FUNCTION
 queue_seek__(struct cmd_args *args)
 {
-    fail_if(args->argc != 1, "The seek command needs one argument: queue seek <id>");
+    FAIL_IF(args->argc != 1, "The seek command needs one argument: queue seek <id>");
 
     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");
+    errno = 0;
+    FAIL_IF(errno == ERANGE || (errno != 0 && id == 0) || (endptr == args->argv[0]),
+            "Invalid argument, not an integer");
 
-    if (*endptr != '\0')
-        fail("Invalid argument, must be only one integer");
+    FAIL_IF(*endptr != '\0', "Invalid argument, must be only one integer");
 
     FILE *sock = lkt_connect();
     write_socket(sock, "playid %ld\n", id);
-    exit_with_status(sock, buf);
+    ___EXIT_WITH_STATUS(sock, buf);
 }
 
 EXIT_FUNCTION
@@ -831,20 +796,19 @@ queue_pos__(struct cmd_args *args)
 {
     char buff[LKT_MESSAGE_MAX], *endptr;
 
-    if (args->argc != 1)
-        fail("Invalid argument for the pos command: queue pos <arg> where arg is a position or a range");
+    FAIL_IF(args->argc != 1,
+            "Invalid argument for the pos command: queue pos <arg> where arg is a position or a range");
 
     long continuation = 0, up = 0;
 
+    errno        = 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: queue pos <arg> where arg is a position or a range");
+    FAIL_IF(errno == ERANGE || (errno != 0 && continuation == 0) || (endptr == args->argv[0]),
+            "Invalid argument, not an integer: queue pos <arg> where arg is a position or a range");
 
     if (*endptr != '\0') {
         /* A range */
-        if (*(++endptr) == '\0')
-            fail("Invalid argument, a range is two integers");
+        FAIL_IF(*(++endptr) == '\0', "Invalid argument, a range is two integers");
         up = atoi(endptr);
     }
 
@@ -856,7 +820,7 @@ redo:
     else
         write_socket(sock, "playlist %d\n", continuation);
 
-    for (;;) {
+    FOR_EVER {
         memset(buff, 0, LKT_MESSAGE_MAX * sizeof(char));
         read_socket(sock, buff, LKT_MESSAGE_MAX - 1);
 
@@ -881,27 +845,26 @@ EXIT_FUNCTION
 queue_list__(struct cmd_args *args)
 {
     char buff[LKT_MESSAGE_MAX], *endptr;
-    FILE *sock        = NULL;
-    long continuation = 0, song_index = 1;
+    long continuation = 0;
+    long song_index   = 1;
+    errno             = 0;
 
     /* Arguments stuff. */
 
     if (args->argc == 0)
         args->argv = LKT_QUEUE_DEFAULT;
-    else if (args->argc != 1)
-        fail("Invalid argument: queue <count?>");
+    else
+        FAIL_IF(args->argc != 1, "Invalid argument: queue <count?>");
 
     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");
+    FAIL_IF(errno == ERANGE || (errno != 0 && continuation == 0) || (endptr == args->argv[0]),
+            "Invalid argument, not an integer");
 
-    if (*endptr != '\0')
-        fail("Invalid argument");
+    FAIL_IF(*endptr != '\0', "Invalid argument");
 
     /* Get the current pos to get limits for the playlist command. */
 
-    sock = lkt_connect();
+    FILE *sock = lkt_connect();
     write_socket(sock, "status\n");
 
 #define assign_int(str, var)                                                                                           \
@@ -909,7 +872,7 @@ queue_list__(struct cmd_args *args)
         var = (atoi(lkt_skip_key(buff)));                                                                              \
         continue;                                                                                                      \
     }
-    for (;;) {
+    FOR_EVER {
         memset(buff, 0, LKT_MESSAGE_MAX * sizeof(char));
         read_socket(sock, buff, LKT_MESSAGE_MAX - 1);
         size_t len = strcspn(buff, LKT_KEY_VALUE_SEP);
@@ -940,7 +903,7 @@ EXIT_FUNCTION
 plt_add__(struct cmd_args *args)
 {
     char buff[2];
-    fail_if(args->argc < 2, "Invalid argument, need at least three arguments: plt add <plt> <query>");
+    FAIL_IF(args->argc < 2, "Invalid argument, need at least three arguments: plt add <plt> <query>");
 
     char regex[LKT_MESSAGE_MAX];
     struct cmd_args args_regex = {
@@ -948,10 +911,10 @@ plt_add__(struct cmd_args *args)
         .argv = args->argv + 1,
     };
 
-    fail_if(lkt_get_query_type(&args_regex, regex, LKT_MESSAGE_MAX), "Query is invalid");
+    FAIL_IF(lkt_get_query_type(&args_regex, regex, LKT_MESSAGE_MAX), "Query is invalid");
     FILE *sock = lkt_connect();
     write_socket(sock, "playlistadd %s %s\n", args->argv[0], regex);
-    exit_with_status(sock, buff);
+    ___EXIT_WITH_STATUS(sock, buff);
 }
 
 EXIT_FUNCTION
@@ -960,11 +923,11 @@ plt_delete__(struct cmd_args *args)
     FILE *sock = lkt_connect();
     char buff[2];
     char cmd[128];
-    fail_if(args->argc < 3, "Invalid argument");
+    FAIL_IF(args->argc < 3, "Invalid argument");
     snprintf(cmd, 128 - 1, "playlistdelete %s", args->argv[0]);
     cmd[127] = '\0';
     send_cmd_with_uri(sock, cmd, args->argc - 1, &args->argv[1]);
-    exit_with_status(sock, buff);
+    ___EXIT_WITH_STATUS(sock, buff);
 }
 
 EXIT_FUNCTION
@@ -972,20 +935,25 @@ stickers_get__(struct cmd_args *args)
 {
     char buff[LKT_MESSAGE_MAX];
     FILE *sock;
+
     if (args->argc == 2)
         write_socket(sock = lkt_connect(), "sticker get %s %s\n", args->argv[0], args->argv[1]);
+
     else if (args->argc == 3)
         write_socket(sock = lkt_connect(), "sticker get %s %s %s\n", args->argv[0], args->argv[1], args->argv[2]);
+
     else if (args->argc == 5) {
         const char *op = args->argv[3];
         op             = op[0] == 'e' ? "=" : op[0] == 'l' ? "<" : op[0] == 'g' ? ">" : NULL;
-        fail_if(!op, "Invalid argument");
+        FAIL_IF(!op, "Invalid argument");
         write_socket(sock = lkt_connect(), "sticker get %s %s %s %s %s\n", args->argv[0], args->argv[1], args->argv[2],
                      op, args->argv[4]);
-    } else
-        fail("Invalid argument");
+    }
+
+    else
+        LOG_FATAL("Invalid argument");
 
-    for (;;) {
+    FOR_EVER {
         memset(buff, 0, LKT_MESSAGE_MAX * sizeof(char));
         read_socket(sock, buff, LKT_MESSAGE_MAX - 1);
         if (STR_NMATCH(buff, "OK", 2))
@@ -999,11 +967,11 @@ stickers_get__(struct cmd_args *args)
 EXIT_FUNCTION
 stickers_set__(struct cmd_args *args)
 {
-    fail_if(args->argc != 4, "Invalid argument");
+    FAIL_IF(args->argc != 4, "Invalid argument");
     FILE *sock = lkt_connect();
     char buff[2];
     write_socket(sock, "sticker set %s %s %s %s\n", args->argv[0], args->argv[1], args->argv[2], args->argv[3]);
-    exit_with_status(sock, buff);
+    ___EXIT_WITH_STATUS(sock, buff);
 }
 
 EXIT_FUNCTION
@@ -1016,8 +984,8 @@ stickers_delete__(struct cmd_args *args)
     else if (args->argc == 3)
         write_socket(sock = lkt_connect(), "sticker delete %s %s %s", args->argv[0], args->argv[1], args->argv[2]);
     else
-        fail("Invalid argument");
-    exit_with_status(sock, buff);
+        LOG_FATAL("Invalid argument");
+    ___EXIT_WITH_STATUS(sock, buff);
 }
 
 /* Search functions. */
@@ -1032,7 +1000,7 @@ redo:
     sock = lkt_connect();
 
     write_socket(sock, "%d %s\n", continuation, cmd);
-    for (;;) {
+    FOR_EVER {
         memset(buff, 0, LKT_MESSAGE_MAX * sizeof(char));
         read_socket(sock, buff, LKT_MESSAGE_MAX - 1);
 
@@ -1058,14 +1026,14 @@ redo:
 EXIT_FUNCTION
 list_plt__(struct cmd_args *args)
 {
-    fail_if(args->argc != 0, "Invalid argument number");
+    FAIL_IF(args->argc != 0, "Invalid argument number");
     __continuation_calls("listplaylists");
 }
 
 EXIT_FUNCTION
 list_plt_content__(struct cmd_args *args)
 {
-    fail_if(args->argc != 1, "Invalid argument number");
+    FAIL_IF(args->argc != 1, "Invalid argument number");
     char buff[LKT_MESSAGE_MAX];
     safe_snprintf(buff, LKT_MESSAGE_MAX, "listplaylist %s", args->argv[0]);
     __continuation_calls(buff);
@@ -1080,13 +1048,13 @@ search_with_cmd__(struct cmd_args *args, const char *cmd)
     int continuation = 0;
     FILE *sock       = NULL;
 
-    fail_if(lkt_get_query_type(args, regex, LKT_MESSAGE_MAX), "Query is invalid");
+    FAIL_IF(lkt_get_query_type(args, regex, LKT_MESSAGE_MAX), "Query is invalid");
 
 redo:
     sock = lkt_connect();
     write_socket(sock, "%d %s %s\n", continuation, cmd, regex);
 
-    for (;;) {
+    FOR_EVER {
         memset(buff, 0, LKT_MESSAGE_MAX * sizeof(char));
         read_socket(sock, buff, LKT_MESSAGE_MAX - 1);
 
@@ -1108,14 +1076,14 @@ redo:
 EXIT_FUNCTION
 search_get__(struct cmd_args *args)
 {
-    fail_if(args->argc != 1, "Invalid number of arguments");
-    fail_if(!strtol(args->argv[0], NULL, 0), "Invalid id");
+    FAIL_IF(args->argc != 1, "Invalid number of arguments");
+    FAIL_IF(!strtol(args->argv[0], NULL, 0), "Invalid id");
     char buff[LKT_MESSAGE_MAX];
     char UNUSED sink;
     FILE *sock = lkt_connect();
 
     write_socket(sock, "find %s\n", args->argv[0]);
-    for (;;) {
+    FOR_EVER {
         memset(buff, 0, LKT_MESSAGE_MAX * sizeof(char));
         read_socket(sock, buff, LKT_MESSAGE_MAX - 1);
         if (STR_NMATCH(buff, "OK", 2))
@@ -1143,13 +1111,13 @@ search_plt__(struct cmd_args *args)
         .argv = args->argv + 1,
     };
 
-    fail_if(args->argc < 2, "Invalid number of arguments");
-    fail_if(lkt_get_query_type(&args_regex, regex, LKT_MESSAGE_MAX), "Query is invalid");
+    FAIL_IF(args->argc < 2, "Invalid number of arguments");
+    FAIL_IF(lkt_get_query_type(&args_regex, regex, LKT_MESSAGE_MAX), "Query is invalid");
 redo:
     sock = lkt_connect();
     write_socket(sock, "%d listplaylistinfo %s %s\n", continuation, args->argv[0], regex);
 
-    for (;;) {
+    FOR_EVER {
         memset(buff, 0, LKT_MESSAGE_MAX * sizeof(char));
         read_socket(sock, buff, LKT_MESSAGE_MAX - 1);
 
@@ -1242,7 +1210,7 @@ static struct cmd_opt options_stickers[] = {
 #define sub_command(name) /* Create sub-commands here */                                                               \
     EXIT_FUNCTION name##__(struct cmd_args *args)                                                                      \
     {                                                                                                                  \
-        fail_if(!args->argc, "Invalid command, specify a sub command for " #name);                                     \
+        FAIL_IF(!args->argc, "Invalid command, specify a sub command for " #name);                                     \
         cmd_parse(options_##name, args->argc, args->argv);                                                             \
     }
 sub_command(stickers);
-- 
GitLab