From 9acf7ab8acd00089687b6a92906c61ba1c0421fa Mon Sep 17 00:00:00 2001
From: Kubat <mael.martin31@gmail.com>
Date: Wed, 15 Sep 2021 11:31:13 +0200
Subject: [PATCH] FIX: Fix the return code for the command_addid -> returns
 true on success...

---
 .clang-format       |  1 +
 inc/lektor/common.h |  3 +++
 src/base/commands.c | 15 +++++----------
 3 files changed, 9 insertions(+), 10 deletions(-)

diff --git a/.clang-format b/.clang-format
index f292a0f2..ab33d60c 100644
--- a/.clang-format
+++ b/.clang-format
@@ -63,6 +63,7 @@ ForEachMacros:
   - 'FOR_EVER_IF'
   - 'FOR_EVER_UNTIL'
   - 'FOR_EACH_FLAT_LIST_ITEM'
+  - 'FOR_EACH_ARGUMENT'
 
 IncludeCategories:
   - Regex: '.*'
diff --git a/inc/lektor/common.h b/inc/lektor/common.h
index 19c6e825..58dd6134 100644
--- a/inc/lektor/common.h
+++ b/inc/lektor/common.h
@@ -73,6 +73,9 @@ extern EXIT_FUNCTION ___not_implemented(const char *func, char *file, int line);
 /* For each, for flat-list. Need to be null terminated on the normally non-null field. */
 #define FOR_EACH_FLAT_LIST_ITEM(it, nnull_field) for (; (it)->nnull_field != NULL; it++)
 
+/* For each argument in a thing like a `char *args[]` */
+#define FOR_EACH_ARGUMENT(i, args) for (int i = 0; args[i]; ++i)
+
 /* Custom defined assert. */
 extern void (*___lkt_assert)(const char *file, int line, const char *func, const char *msg);
 #ifdef assert
diff --git a/src/base/commands.c b/src/base/commands.c
index 20926b1f..2e230424 100644
--- a/src/base/commands.c
+++ b/src/base/commands.c
@@ -352,9 +352,8 @@ command_add(struct lkt_state *srv, char *args[LKT_MESSAGE_ARGS_MAX], int priorit
 {
     RETURN_UNLESS(args && args[0], "Invalid argument", false);
     struct lkt_uri uri;
-    int ret;
     RETURN_UNLESS(lkt_uri_from(&uri, args[0]), "Failed to parse query", false);
-    ret = database_queue_add_uri(srv->db, &uri, priority);
+    bool ret = database_queue_add_uri(srv->db, &uri, priority);
     lkt_uri_free(&uri);
     if (!ret)
         LOG_ERROR("COMMAND", "Failed to add with priority %d in queue", priority);
@@ -366,16 +365,12 @@ bool
 command_addid(struct lkt_state *srv, char *args[LKT_MESSAGE_ARGS_MAX])
 {
     RETURN_UNLESS(args, "Invalid argument", false);
-    errno = 0;
-    int i, id;
-    for (i = 0; !errno && args[i]; ++i) {
-        id = strtol(args[i], NULL, 0);
-        if (id == 0)
-            break;
-        errno |= !database_queue_add_id(srv->db, id, 1);
+    bool error = false;
+    FOR_EACH_ARGUMENT (i, args) {
+        error |= (!database_queue_add_id(srv->db, strtol(args[i], NULL, 0), 1));
     }
     srv->mpd_idle_events |= MPD_IDLE_PLAYLIST;
-    return !errno;
+    return !error;
 }
 
 inline bool
-- 
GitLab