commit 1b152fdd9ee833285c15e3936c968b38c5448302
parent 7fba51670da03030dac6ae4c83671cb4adeb7cc6
Author: Jacob R. Edwards <jacobouno@protonmail.com>
Date:   Tue, 20 Jul 2021 19:31:12 -0700
Add dynamic argument array length
A new argument splitting function asplit does all the work using
split, only minor changes to aps_command were needed.
Diffstat:
5 files changed, 54 insertions(+), 10 deletions(-)
diff --git a/aps/aps.c b/aps/aps.c
@@ -26,11 +26,11 @@
 
 #include "aps.h"
 #include "arg.h"
+#include "asplit.h"
 #include "command.h"
 #include "config.h"
 #include "log.h"
 #include "queue.h"
-#include "split.h"
 #include "util.h"
 
 struct aps *
@@ -187,7 +187,7 @@ int
 aps_command(struct aps *aps, int s)
 {
 	char *(*com)(struct aps *, int, int, char **);
-	char *argv[AP_ARGV_MAX];
+	char **argv;
 	char *next;
 	char *status;
 	struct buf *buf;
@@ -198,11 +198,13 @@ aps_command(struct aps *aps, int s)
 		return 0;
 
 	buf = aps->clients[s].request;
-	len = split(argv, LEN(argv), buf->data, AP_ARG_SEPS);
-	if (len == 0)
+	argv = asplit(buf->data, AP_ARG_SEPS);
+	if (argv == NULL)
+		return 1;
+	if (*argv == NULL) {
+		free(argv);
 		return respfinish(aps, s, "No command");
-	if (len >= LEN(argv))
-		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)
@@ -211,11 +213,12 @@ aps_command(struct aps *aps, int s)
 	if (com == NULL)
 		status = "Command not found";
 	else
-		status = com(aps, s, len - 1, argv + 1);
+		status = com(aps, s, arglen(argv + 1), argv + 1);
 		
 	len = next - (char *)buf->data;
 	memmove(buf->data, buf->data + len, buf->len - len);
 	buf->len -= len;
 
+	free(argv);
 	return respfinish(aps, s, status);
 }
diff --git a/aps/asplit.c b/aps/asplit.c
@@ -0,0 +1,41 @@
+/* Copyright 2021 Jacob R. Edwards
+ *
+ * ap -- audio player
+ *
+ * This program is free software: you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License as published by
+ * the Free Software Foundation, either version 3 of the License, or
+ * (at your option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with this program.  If not, see <https://www.gnu.org/licenses/>.
+ */
+
+#include <stdlib.h>
+#include <string.h>
+
+#include "split.h"
+
+char **
+asplit(char *s, char *sep)
+{
+        char **args, *copy;
+        unsigned int len;
+
+        copy = strdup(s);
+        if (copy == NULL)
+                return NULL;
+        len = split(NULL, 0, copy, sep);
+        free(copy);
+
+        args = calloc(++len, sizeof(*args));
+        if (args == NULL)
+                return NULL;
+        split(args, len, s, sep);
+        return args;
+}
diff --git a/aps/asplit.h b/aps/asplit.h
@@ -0,0 +1 @@
+char	**asplit(char *, char *);
diff --git a/aps/mkfile b/aps/mkfile
@@ -1,9 +1,9 @@
 # Copyright 2021 Jacob R. Edwards
 
 name = aps
-src = aps.c arg.c buf.c bug.c command.c find.c item.c log.c main.c match.c player.c queue.c response.c split.c
+src = aps.c arg.c asplit.c buf.c bug.c command.c find.c item.c log.c main.c match.c player.c queue.c response.c split.c
 obj = ${src:%.c=%.o}
-hdr = aps.h arg.h buf.h bug.h command.h config.h find.h item.h log.h match.h player.h queue.h response.h split.h util.h
+hdr = aps.h arg.h asplit.h buf.h bug.h command.h config.h find.h item.h log.h match.h player.h queue.h response.h split.h util.h
 lib = ap
 mk = ../mk
 
diff --git a/include/ap/ap.h b/include/ap/ap.h
@@ -1,4 +1,3 @@
-#define AP_ARGV_MAX 128
 #define AP_ARG_SEPS "\t"
 
 #include "client.h"