commit 7cb5b22ef4045909674d5c19e79f290553d57a97
parent 261070e48f5f1c022d1fcd7d1c7df3d474231eb8
Author: Jacob R. Edwards <jacobouno@protonmail.com>
Date:   Sat, 19 Dec 2020 23:58:40 -0800
Add command line interface and fix bug
You can now specify initial host, path, and port as command line
arguments.
In connect_to() the error for resolve() was unhelpful.
Diffstat:
| M | main.c |  |  | 46 | +++++++++++++++++++++++++++++++++++----------- | 
1 file changed, 35 insertions(+), 11 deletions(-)
diff --git a/main.c b/main.c
@@ -88,7 +88,7 @@ connect_to(const char *host, const char *proto)
 
 	error = resolve(&ai0, host, proto);
 	if (error) {
-		warnx("Unable to resolve: %s:", gai_strerror(error));
+		warnx("Unable to resolve: '%s' at '%s': %s", host, proto, gai_strerror(error));
 		return -1;
 	}
 
@@ -340,8 +340,8 @@ cgoto(int argc, char **argv, int depth, const char *tmpdir, const char *host, co
 		newpath(npath, argv[0]);
 	else {
 		newpath(npath, argv[1]);
-		nhost = argv[1];
-		if (argc == 2)
+		nhost = argv[0];
+		if (argc == 3)
 			nport = argv[2];
 	}
 
@@ -438,15 +438,39 @@ too_many_args:
 int
 main(int argc, char *argv[])
 {
-	char t[64];
-	strcpy(t, "/tmp/gawk-XXXXXXXXXXX");
-	if (mkdtemp(t) == NULL)
-		err(1, "unable to create temp dir");
+	char *host = "localhost";
+	char *path = "/";
+	char *port = "70";
+	char temp[64];
+	int ch;
+	int status;
+
+	while ((ch = getopt(argc, argv, "d:h:p:")) != -1) {
+		switch (ch) {
+		case 'd':
+			path = optarg;
+			break;
+		case 'h':
+			host = optarg;
+			break;
+		case 'p':
+			port = optarg;
+			break;
+		default:
+			fprintf(stderr, "usage: %s [-d path] [-h host] [-p port]\n",
+			    getprogname());
+			return 1;
+		}
+	}
 
-	gawk(0, t, "localhost", "/","gopher");
+	strcpy(temp, "/tmp/gawk-XXXXXXXXXXX");
+	if (mkdtemp(temp) == NULL)
+		err(1, "mkdtemp '%s'", temp);
 
-	if (rmdir(t) == -1)
-		err(1, "unable to remove temp dir");
+	status = gawk(0, temp, host, path, port);
 
-	return 0;
+	if (rmdir(temp) == -1)
+		err(1, "rmdir '%s'", temp);
+
+	return status;
 }