commit 5cd7ee48896ba1c89a6845187509f9f8437de186
parent 56cc828abf19143afb462eff14df6b6abf4bf9d3
Author: Jacob R. Edwards <jacobouno@protonmail.com>
Date: Tue, 20 Jul 2021 18:09:58 -0700
Limit command.c to commands and their helpers
The aps_read and aps_command functions are now located in aps.c
with minor revisions to account for the commands array now being a
member of the aps struct.
Diffstat:
5 files changed, 102 insertions(+), 86 deletions(-)
diff --git a/aps/aps.c b/aps/aps.c
@@ -28,7 +28,9 @@
#include "arg.h"
#include "command.h"
#include "config.h"
+#include "log.h"
#include "queue.h"
+#include "split.h"
#include "util.h"
struct aps *
@@ -154,3 +156,67 @@ aps_update(struct aps *aps)
return 0;
}
+
+char *
+aps_read(struct aps *aps, int s)
+{
+ char *end;
+ int len;
+ struct buf *buf;
+
+ buf = aps->clients[s].request;
+ if (buf->len == buf->size && bufresize(buf, MAX(buf->size, 128) * 2)) {
+ aps_log(aps, ERROR, strerror(errno), s);
+ return NULL;
+ }
+
+ len = recv(s, buf->data + buf->len, buf->size - buf->len, 0);
+ if (len == -1)
+ return NULL;
+ buf->len += len;
+
+ end = memchr(buf->data + (buf->len - len), '\n', len);
+ if (end == NULL)
+ return NULL; /* not a full command yet */
+ *end = 0;
+
+ return end + 1;
+}
+
+int
+aps_command(struct aps *aps, int s)
+{
+ char *next;
+ char *status;
+ struct buf *buf;
+ char *argv[AP_ARGV_MAX];
+ int i;
+ int len;
+ char *(*com)(struct aps *, int, int, char **);
+
+ if ((next = aps_read(aps, s)) == NULL)
+ return 0;
+
+ buf = aps->clients[s].request;
+
+ len = split(argv, LEN(argv), buf->data, AP_ARG_SEPS);
+ if (len == 0)
+ return respfinish(aps, s, "No command");
+ if (len < 0)
+ return respfinish(aps, s, strerror(E2BIG));
+
+ for (com = NULL, i = 0; com == NULL && i < aps->ncoms; ++i)
+ if (strcmp(*argv, aps->coms[i].name) == 0)
+ com = aps->coms[i].func;
+
+ if (com == NULL)
+ status = "Command not found";
+ else
+ status = com(aps, s, len - 1, argv + 1);
+
+ len = next - (char *)buf->data;
+ memmove(buf->data, buf->data + len, buf->len - len);
+ buf->len -= len;
+
+ return respfinish(aps, s, status);
+}
diff --git a/aps/aps.h b/aps/aps.h
@@ -20,6 +20,8 @@ struct aps {
struct player *player;
struct pollfd pfds[OPEN_MAX];
struct item *queue;
+ struct command *coms;
+ unsigned int ncoms;
};
struct aps *aps_open(char *, char **);
@@ -28,3 +30,5 @@ int aps_drop(struct aps *, int);
int aps_accept(struct aps *);
int aps_handle(struct aps *, int);
int aps_update(struct aps *);
+char *aps_read(struct aps *, int);
+int aps_command(struct aps *, int);
diff --git a/aps/command.c b/aps/command.c
@@ -27,12 +27,9 @@
#include "aps.h"
#include "arg.h"
#include "find.h"
-#include "log.h"
#include "queue.h"
-#include "split.h"
#include "util.h"
-static char *earg = "Invalid number of arguments";
static char *enotfound = "Not found";
static char *eposition = "No position in queue";
@@ -187,32 +184,6 @@ aps_previous(struct aps *aps, int s, int argc, char **argv)
}
char *
-aps_read(struct aps *aps, int s)
-{
- char *end;
- int len;
- struct buf *buf;
-
- buf = aps->clients[s].request;
- if (buf->len == buf->size && bufresize(buf, MAX(buf->size, 128) * 2)) {
- aps_log(aps, ERROR, strerror(errno), s);
- return NULL;
- }
-
- len = recv(s, buf->data + buf->len, buf->size - buf->len, 0);
- if (len == -1)
- return NULL;
- buf->len += len;
-
- end = memchr(buf->data + (buf->len - len), '\n', len);
- if (end == NULL)
- return NULL; /* not a full command yet */
- *end = 0;
-
- return end + 1;
-}
-
-char *
aps_status(struct aps *aps, int s, int argc, char **argv)
{
char *status;
@@ -244,59 +215,3 @@ aps_status(struct aps *aps, int s, int argc, char **argv)
return strerror(errno);
return NULL;
}
-
-int
-aps_command(struct aps *aps, int s)
-{
- char *next;
- char *status;
- struct buf *buf;
- char *argv[AP_ARGV_MAX];
- int i;
- int len;
- char *(*command)(struct aps *, int, int, char **);
-
- struct command {
- char *name;
- char *(*func)(struct aps *, int, int, char **);
- } commands[] = {
- { "add", aps_add },
- { "list", aps_list },
- { "name", aps_name },
- { "next", aps_next },
- { "pause", aps_pause },
- { "play", aps_play },
- { "previous", aps_previous },
- { "remove", aps_remove },
- { "seek", aps_seek },
- { "status", aps_status },
- { "stop", aps_stop },
- { "toggle", aps_toggle }
- };
-
- if ((next = aps_read(aps, s)) == NULL)
- return 0;
-
- buf = aps->clients[s].request;
-
- len = split(argv, LEN(argv), buf->data, AP_ARG_SEPS);
- if (len == 0)
- return respfinish(aps, s, "No command");
- if (len < 0)
- return respfinish(aps, s, earg);
-
- for (command = NULL, i = 0; command == NULL && i < LEN(commands); ++i)
- if (strcmp(*argv, commands[i].name) == 0)
- command = commands[i].func;
-
- if (command == NULL)
- status = "Command not found";
- else
- status = command(aps, s, len - 1, argv + 1);
-
- len = next - (char *)buf->data;
- memmove(buf->data, buf->data + len, buf->len - len);
- buf->len -= len;
-
- return respfinish(aps, s, status);
-}
diff --git a/aps/command.h b/aps/command.h
@@ -1,2 +1,17 @@
+struct command {
+ char *name;
+ char *(*func)(struct aps *, int, int, char **);
+};
+
+char *aps_add(struct aps *, int, int, char **);
+char *aps_remove(struct aps *, int, int, char **);
+char *aps_list(struct aps *, int, int, char **);
+char *aps_seek(struct aps *, int, int, char **);
+char *aps_play(struct aps *, int, int, char **);
+char *aps_pause(struct aps *, int, int, char **);
+char *aps_stop(struct aps *, int, int, char **);
+char *aps_toggle(struct aps *, int, int, char **);
+char *aps_name(struct aps *, int, int, char **);
char *aps_next(struct aps *, int, int, char **);
-int aps_command(struct aps *, int);
+char *aps_previous(struct aps *, int, int, char **);
+char *aps_status(struct aps *, int, int, char **);
diff --git a/aps/main.c b/aps/main.c
@@ -36,6 +36,20 @@
#include "util.h"
static struct aps *aps;
+static struct command commands[] = {
+ { "add", aps_add },
+ { "list", aps_list },
+ { "name", aps_name },
+ { "next", aps_next },
+ { "pause", aps_pause },
+ { "play", aps_play },
+ { "previous", aps_previous },
+ { "remove", aps_remove },
+ { "seek", aps_seek },
+ { "status", aps_status },
+ { "stop", aps_stop },
+ { "toggle", aps_toggle }
+};
void
slog(char *s)
@@ -105,6 +119,8 @@ main(int argc, char *argv[])
aps->player->state = STOPPED;
aps->logmask = INFO | WARN | ERROR | FATAL;
+ aps->coms = commands;
+ aps->ncoms = LEN(commands);
signal(SIGCHLD, sigchld);
signal(SIGINT, sigint);