commit 590dbdbedcdaa4e58dda724be5b20050de6fb43d
parent 072e9297193249dc48a6fd668f4c6e5546761c66
Author: Jacob R. Edwards <n/a>
Date: Sun, 20 Feb 2022 14:12:49 -0800
Improve the remove command
While it's not possible for it to be simple without changing the
queue system this version is much more straight-forward.
After thinking through the logic thoroughly and testing extensively,
I believe this version is bug-free. (There was an issue that caused
it to crash, which was fixed in either this commit or the last.)
Diffstat:
3 files changed, 28 insertions(+), 23 deletions(-)
diff --git a/aps/command.c b/aps/command.c
@@ -50,31 +50,28 @@ com_add(struct aps *aps, int fd, int argc, char **argv)
char *
com_remove(struct aps *aps, int fd, int argc, char **argv)
{
- struct item *item, *matched;
-
- if ((item = aps->queue) == NULL)
- return NULL;
+ struct item *item;
if (!argc) {
- queue_remove(aps, item);
+ queue_remove(aps, aps->queue);
return NULL;
}
- /*
- * A messy solution, the whole item system should be
- * reworked to make it easy.
- */
- matched = NULL;
- do {
+ item = aps->queue;
+ while (item) {
if (match(item->path, *argv)) {
- if (matched)
- queue_remove(aps, matched);
- matched = item;
- }
- item = item->next;
- } while (item != aps->queue);
- if (matched)
- queue_remove(aps, matched);
+ if (item == aps->queue) {
+ item = queue_remove(aps, item);
+ } else {
+ item = queue_remove(aps, item);
+ if (item == aps->queue)
+ item = NULL;
+ }
+ } else if (item->next != aps->queue)
+ item = item->next;
+ else
+ item = NULL;
+ }
return NULL;
}
diff --git a/aps/queue.c b/aps/queue.c
@@ -61,15 +61,23 @@ queue_add(struct aps *aps, char *s)
return 0;
}
-void
+struct item *
queue_remove(struct aps *aps, struct item *item)
{
+ struct item *next;
+
if (item == NULL)
- return;
+ return NULL;
aps_logfmt(aps, QUEUE, 0, "remove '%s'", item->path);
+
+ next = item->next;
+ if (next == item)
+ next = NULL;
if (aps->queue == item)
- queue_set(aps, item->next == item ? NULL : item->next);
+ queue_set(aps, next);
+
item_unlink(item);
item_free(item);
+ return next;
}
diff --git a/aps/queue.h b/aps/queue.h
@@ -1,3 +1,3 @@
int queue_set(struct aps *, struct item *);
int queue_add(struct aps *, char *);
-void queue_remove(struct aps *, struct item *);
+struct item *queue_remove(struct aps *, struct item *);