diff --git a/.clang-format b/.clang-format index f292a0f219069123dea60222d9fc5c4b6e9e7efd..ab33d60ce6277bc1a79d720f2ce8f92401786231 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 19c6e825e6168157f03e41497c31878ade87b26f..58dd6134836f733c5111f67ab06b707225ef03f0 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 20926b1fb18c3baf3fbe026a6b3490f17086275a..2e230424f6d62c7f11661dfd52e602e341ef8420 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