commit fd42b7c2802b80594fc1944db191e33bb9a4fdea
parent 9d631531184e3604b269b451ac0f292eace57458
Author: Jacob R. Edwards <jacobouno@protonmail.com>
Date:   Wed, 14 Jul 2021 03:07:16 -0700
Dynamically allocate apcon and aps structures
It's easier for the caller if structure memory is dynamically
allocated.
Rename the apcon source file and functions to be simpler.
Diffstat:
10 files changed, 117 insertions(+), 118 deletions(-)
diff --git a/aps/aps.c b/aps/aps.c
@@ -37,12 +37,12 @@
 #include "config.h"
 #include "util.h"
 
-static struct aps aps;
+static struct aps *aps;
 
 void
 sigclose(int sig)
 {
-	aps.close = 1;
+	aps->close = 1;
 }
 
 void
@@ -53,8 +53,8 @@ sigchld(int sig)
 
 	while ((pid =
 	    waitpid(-1, &status, WCONTINUED | WNOHANG | WUNTRACED)) > 0) {
-		if (pid == aps.player.pid)
-			pupdate(&aps.player, status);
+		if (pid == aps->player.pid)
+			pupdate(&aps->player, status);
 	}
 }
 
@@ -76,13 +76,13 @@ aps_accept(struct aps *aps)
 	int r;
 
 	pfd.events = POLLIN;
-	pfd.fd = aps->con.s;
+	pfd.fd = aps->con->sock;
 
 	r = poll(&pfd, 1, aps->nfds ? 0 : INFTIM);
 	if (r <= 0)
 		return r;
 
-	while ((s = accept4(aps->con.s, NULL, NULL, 0)) >= 0) {
+	while ((s = accept4(aps->con->sock, NULL, NULL, 0)) >= 0) {
 		++aps->nfds;
 		aps->pfds[s].fd = s;
 		if ((aps->clients[s].request = bufnew()) == NULL ||
@@ -142,20 +142,28 @@ run(struct aps *aps)
 	return 0;
 }
 
-int
-aps_open(struct aps *aps)
+struct aps *
+aps_open(char *path)
 {
+	struct aps *aps;
 	int i;
 
-	if (apcon_open(&aps->con, NULL, bind, SOCK_NONBLOCK) ||
-	    listen(aps->con.s, 10))
-		return 1;
+	aps = calloc(1, sizeof(*aps));
+	if (aps == NULL)
+		return NULL;
+
+	aps->con = apopen(path, bind, SOCK_NONBLOCK);
+	if (aps->con == NULL || listen(aps->con->sock, 10)) {
+		free(aps);
+		return NULL;
+	}
 
 	for (i = 0; i < LEN(aps->pfds); ++i) {
 		aps->pfds[i].fd = -1;
 		aps->pfds[i].events = POLLIN | POLLOUT;
 	}
-	return 0;
+
+	return aps;
 }
 
 void
@@ -168,7 +176,7 @@ aps_close(struct aps *aps)
 	for (i = 0; i < LEN(aps->pfds); ++i)
 		if (aps->pfds[i].fd != -1)
 			aps_drop(aps, i);
-	apcon_close(&aps->con);
+	apclose(aps->con);
 	pstop(&aps->player);
 }
 
@@ -186,25 +194,27 @@ main(int argc, char *argv[])
 	signal(SIGINT, sigclose);
 	signal(SIGTERM, sigclose);
 
+	aps = aps_open(NULL);
+	if (aps == NULL)
+		die("unable to open connection");
+
 	for (i = 1; player[i]; ++i)
 		if (!player[i][0])
-			player[i] = aps.player.path;
+			player[i] = aps->player.path;
 
-	if (aps_next(&aps, -1, LEN(next), next))
+	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;
+	aps->player.argv = player + 1;
+	aps->player.prog = player[0];
+	aps->player.state = STOPPED;
 
-	if (aps_open(&aps))
-		die("open connection");
-	if (run(&aps)) {
+	if (run(aps)) {
 		perror("error");
-		aps_close(&aps);
+		aps_close(aps);
 		return 1;
 	}
 
-	aps_close(&aps);
+	aps_close(aps);
 	return 0;
 }
diff --git a/aps/aps.h b/aps/aps.h
@@ -13,7 +13,7 @@ struct aps {
 	int close;
 	int nfds;
 	int playing;
-	struct apcon con;
+	struct apcon *con;
 	struct client {
 		struct buf *request;
 		struct response *resp;
diff --git a/include/ap/ap.h b/include/ap/ap.h
@@ -2,7 +2,6 @@
 #define AP_ARGV_MAX 128
 #define AP_ARG_SEPS "\t"
 
-#include "apcon.h"
-#include "apfile.h"
 #include "client.h"
+#include "con.h"
 #include "sock.h"
diff --git a/include/ap/apcon.h b/include/ap/apcon.h
@@ -1,8 +0,0 @@
-struct apcon {
-	char *path;
-	int (*init)(int, const struct sockaddr *, socklen_t);
-	int s;
-};
-
-int	apcon_open(struct apcon *, char *, int (*)(int, const struct sockaddr *, socklen_t), int);
-void	apcon_close(struct apcon *);
diff --git a/include/ap/apfile.h b/include/ap/apfile.h
@@ -1 +0,0 @@
-char	*apfile(char *);
diff --git a/include/ap/con.h b/include/ap/con.h
@@ -0,0 +1,9 @@
+struct apcon {
+	char *path;
+	int (*init)(int, const struct sockaddr *, socklen_t);
+	int sock;
+};
+
+char	*apfile(char *);
+struct apcon	*apopen(char *, int (*)(int, const struct sockaddr *, socklen_t), int);
+void	apclose(struct apcon *);
diff --git a/lib/ap/apcon.c b/lib/ap/apcon.c
@@ -1,51 +0,0 @@
-/* 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 <sys/socket.h>
-
-#include <stdlib.h>
-#include <unistd.h>
-
-#include <ap.h>
-
-int
-apcon_open(struct apcon *apc, char *path,
-    int (*init)(int, const struct sockaddr *, socklen_t), int flags)
-{
-	apc->path = apfile(path);
-	if (apc->path == NULL)
-		return 1;
-
-	apc->s = sopen(apc->path, init, SOCK_STREAM | flags);
-	if (apc->s == -1) {
-		free(apc->path);
-		return 1;
-	}
-
-	apc->init = init;
-	return 0;
-}
-
-void
-apcon_close(struct apcon *apc)
-{
-	sclose(apc->s);
-	if (apc->init == bind)
-		unlink(apc->path);
-	free(apc->path);
-}
diff --git a/lib/ap/apfile.c b/lib/ap/apfile.c
@@ -1,31 +0,0 @@
-/* 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>
-
-char *
-apfile(char *path)
-{
-        if (path == NULL) {
-                path = getenv("apsock");
-                if (path == NULL)
-                        path = "/tmp/aps";
-        }
-        return strdup(path);
-}
diff --git a/lib/ap/con.c b/lib/ap/con.c
@@ -0,0 +1,72 @@
+/* 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 <sys/socket.h>
+
+#include <stdlib.h>
+#include <string.h>
+#include <unistd.h>
+
+#include <ap.h>
+
+char *
+apfile(char *path)
+{
+        if (path == NULL) {
+                path = getenv("apsock");
+                if (path == NULL)
+                        path = "/tmp/aps";
+        }
+        return strdup(path);
+}
+
+struct apcon *
+apopen(char *path, int (*init)(int, const struct sockaddr *, socklen_t), int flags)
+{
+	struct apcon *apc;
+
+	apc = malloc(sizeof(*apc));
+	if (apc == NULL)
+		return NULL;
+
+	apc->path = apfile(path);
+	if (apc->path == NULL) {
+		free(apc);
+		return NULL;
+	}
+
+	apc->sock = sopen(apc->path, init, SOCK_STREAM | flags);
+	if (apc->sock == -1) {
+		free(apc->path);
+		free(apc);
+		return NULL;
+	}
+
+	apc->init = init;
+	return apc;
+}
+
+void
+apclose(struct apcon *apc)
+{
+	sclose(apc->sock);
+	if (apc->init == bind)
+		unlink(apc->path);
+	free(apc->path);
+	free(apc);
+}
diff --git a/lib/ap/mkfile b/lib/ap/mkfile
@@ -1,7 +1,7 @@
 # Copyright 2021 Jacob R. Edwards
 
 name = libap.a
-src = apcon.c apfile.c client.c sock.c
+src = client.c con.c sock.c
 obj = ${src:%.c=%.o}
 hdir = ../../include/ap
 mk = ../../mk