commit 565d745a5f4d4d00a0fa3813b5b49c2a2797036e
parent 3b7ac14952496a2007ca69aaa992f1d78f47bb9c
Author: Jacob R. Edwards <jacobouno@protonmail.com>
Date: Sat, 19 Jun 2021 16:01:55 -0700
Modify queue finding functions used by the seek command
- Add carot prefix to start from queue head
- Parse prefixes in find rather than both findnum and findpat
- Replace the clunky aps_playnext function
Diffstat:
5 files changed, 67 insertions(+), 62 deletions(-)
diff --git a/aps.c b/aps.c
@@ -142,7 +142,7 @@ run(struct aps *aps)
{
while (!aps->close) {
if (!aps->player.state && aps->queue.pos)
- aps_playnext(aps);
+ aps_next(aps, -1, 0, NULL);
if ((aps_accept(aps) || aps_update(aps)) && errno != EINTR)
return 1;
}
@@ -170,6 +170,8 @@ aps_close(struct aps *aps)
{
int i;
+ for (i = 0; i < LEN(aps->next); ++i)
+ free(aps->next[i]);
for (i = 0; i < LEN(aps->pfds); ++i)
if (aps->pfds[i].fd != -1)
aps_drop(aps, i);
@@ -195,10 +197,12 @@ main(int argc, char *argv[])
if (!player[i][0])
player[i] = aps.player.path;
+ if (aps_next(&aps, -1, LEN(next), next))
+ die("aps_next");
+
aps.player.argv = player + 1;
aps.player.prog = player[0];
aps.player.state = STOPPED;
- strcpy(aps.next, "+1");
if (aps_open(&aps))
die("open connection");
diff --git a/aps.h b/aps.h
@@ -1,5 +1,6 @@
struct aps {
- char next[PATH_MAX];
+ char *next[AP_ARGV_MAX];
+ int nextlen;
int close;
int nfds;
int playing;
diff --git a/command.c b/command.c
@@ -146,60 +146,63 @@ aps_list(struct aps *aps, int s, int argc, char **argv)
}
struct item *
-findnum(struct aps *aps, char *num, char **err)
+findnum(struct item *(*incr)(struct item *), struct item *item, char *num,
+ char **err)
{
int n;
- struct item *(*incr)(struct item *);
- struct item *item;
- n = strtonum(num, INT_MIN, INT_MAX, err);
+ n = strtonum(num, 0, INT_MAX, err);
if (*err)
return NULL;
-
- incr = item_next;
- if (strchr("+-", *num) == NULL) {
- item = aps->queue.head;
- } else {
- if (*num == '-') {
- incr = item_prev;
- n = -n;
- }
- item = aps->queue.pos;
- }
-
*err = enotfound;
return item_seek(incr, item, n);
}
struct item *
-findpat(struct aps *aps, char *pat, char **err)
+findpat(struct item *(*incr)(struct item *), struct item *item,
+ char **patterns, int len, char **err)
{
+ *err = enotfound;
+ return nextmatch(incr, item, patterns, len, 0);
+}
+
+struct item *
+find(struct aps *aps, int argc, char **argv, char **err)
+{
+ char *e;
struct item *(*incr)(struct item *);
struct item *item;
+ ++argv;
+ --argc;
+ if (argc < 1) {
+earg:
+ *err = earg;
+ return NULL;
+ }
+
incr = item_next;
item = aps->queue.pos;
- if (strchr("+-^", *pat)) {
- if (*pat == '-')
+ if (argc > 1 && strchr("+-^", **argv) && !argv[0][1]) {
+ switch (**argv) {
+ case '-':
incr = item_prev;
- if (*pat == '^')
+ break;
+ case '^':
item = aps->queue.head;
- else if (item)
- item = incr(item);
- ++pat;
+ break;
+ }
+ ++argv;
+ --argc;
}
- *err = enotfound;
- return nextmatch(incr, item, &pat, 1, 0);
-}
-
-struct item *
-find(struct aps *aps, char *exp, char **err)
-{
- if (isdigit(exp[0]) || (strchr("+-", exp[0]) && isdigit(exp[1])))
- return findnum(aps, exp, err);
- else
- return findpat(aps, exp, err);
+ if (!isdigit(**argv)) {
+ return findpat(incr, incr(item), argv, argc, err);
+ } else {
+ if (argc != 1)
+ goto earg;
+ return findnum(incr, item, *argv, err);
+ }
}
char *
@@ -208,10 +211,7 @@ aps_seek(struct aps *aps, int s, int argc, char **argv)
char *err;
struct item *item;
- if (argc != 2)
- return earg;
-
- item = find(aps, argv[1], &err);
+ item = find(aps, argc, argv, &err);
if (item == NULL)
return err;
aps->queue.pos = item;
@@ -265,10 +265,10 @@ aps_name(struct aps *aps, int s, int argc, char **argv)
struct item *item;
char *err;
- if (argc > 2)
- return strerror(E2BIG);
-
- item = find(aps, (argc <= 1 ? "+0" : argv[1]), &err);
+ if (argc < 2)
+ item = aps->queue.pos;
+ else
+ item = find(aps, argc, argv, &err);
if (item == NULL)
return err;
@@ -278,27 +278,23 @@ aps_name(struct aps *aps, int s, int argc, char **argv)
}
char *
-aps_playnext(struct aps *aps)
-{
- char *argv[3] = {
- __func__, aps->next, NULL
- };
-
- pstop(&aps->player);
- return aps_play(aps, -1, 2, argv);
-}
-
-char *
aps_next(struct aps *aps, int s, int argc, char **argv)
{
- if (argc > 2)
- return strerror(E2BIG);
+ int i;
+
if (argc > 1) {
- if (strlcpy(aps->next, argv[1], sizeof(aps->next)) >= sizeof(aps->next))
- return strerror(ENAMETOOLONG);
+ for (i = 0; i < argc; ++i) {
+ free(aps->next[i]);
+ aps->next[i] = strdup(argv[i]);
+ if (aps->next[i] == NULL)
+ return strerror(errno);
+ }
+ aps->nextlen = i;
return NULL;
}
- return aps_playnext(aps);
+
+ pstop(&aps->player);
+ return aps_play(aps, -1, aps->nextlen, aps->next);
}
int
diff --git a/command.h b/command.h
@@ -1,2 +1,2 @@
+char *aps_next(struct aps *, int, int, char **);
int aps_command(struct aps *, int);
-char *aps_playnext(struct aps *);
diff --git a/config.def.h b/config.def.h
@@ -4,4 +4,8 @@ static char *player[] = {
"ffplay", "apsplayer", "-loglevel", "error", "-nodisp", "-autoexit", "", NULL
};
+static char *next[] = {
+ "config.h", "+", "1"
+};
+
static int timeout = 250;