commit 54266ea5cdefc0a6f43401991033d6de035599ac
parent bca75728dbc017877e7a9312398d449f42fb4c66
Author: Jacob R. Edwards <jacobouno@protonmail.com>
Date: Sat, 19 Dec 2020 12:07:29 -0800
Cleanup 'g' command and gawkat()
Diffstat:
M | main.c | | | 58 | +++++++++++++++++++++++++++++++--------------------------- |
1 file changed, 31 insertions(+), 27 deletions(-)
diff --git a/main.c b/main.c
@@ -170,6 +170,7 @@ getgph(int sock, const char *path)
ssize_t bytes;
char buf[BUFSIZE];
+ /* NOTE: SIGPIPE is recieved */
if (send(sock, path, strlen(path), 0) == -1) {
warn("unable to send request");
return 1;
@@ -179,6 +180,7 @@ getgph(int sock, const char *path)
if (fwrite(buf, 1, bytes, stdout) != bytes)
return 1;
}
+
return 0;
}
@@ -194,30 +196,38 @@ gawkat(int sock, const char *path)
return 1;
}
+ argv[0] = format_prog;
+ argv[1] = NULL;
+
switch (fork()) {
case -1:
warn("unable to fork");
return 1;
case 0:
- if (dup2(fds[1], STDIN_FILENO) == -1 || close(fds[0]))
+ if (dup2(fds[1], STDIN_FILENO) == -1) {
warn("unable to setup pipe");
- argv[0] = format_prog;
- argv[1] = NULL;
+ _exit(1);
+ }
execvp(*argv, argv);
warn("execvp %s", *argv);
return 1;
default:
- if (dup2(fds[0], STDOUT_FILENO) == -1 || close(fds[1]))
+ if (dup2(fds[0], STDOUT_FILENO) == -1) {
warn("unable to setup pipe");
+ status = 1;
+ break;
+ }
+ if ((status = getgph(sock, path)) == 0) {
+ wait(&status);
+ if (status)
+ warnx("child exited %d", status);
+ }
}
- if ((status = getgph(sock, path)) == 0) {
- wait(&status);
- if (status)
- warnx("child exited %d", status);
- }
- return status;
+ if (close(fds[0]) || close(fds[1]))
+ warn("unable to close pipe");
+ return status;
}
/* returns `<0` on error, if positive `n - 1` is
@@ -226,7 +236,6 @@ int
gawk(int sock, int depth, const char *host, const char *path, const char *port)
{
int done;
- int child;
char cbuf[PATH_MAX + HOST_NAME_MAX + PROTO_MAX];
char *cmd;
int closeit;
@@ -248,7 +257,6 @@ gawk(int sock, int depth, const char *host, const char *path, const char *port)
snprintf(prompt, sizeof(prompt), "[%s] %s ", host, path);
- child = -1;
done = 0;
while (!done && (cmd = input(cbuf, sizeof(cbuf), prompt, stdin))) {
/* `strcpy(cmd, "p")` works I suppose, but I like
@@ -262,7 +270,9 @@ gawk(int sock, int depth, const char *host, const char *path, const char *port)
break;
case 'p': /* print */
gawk_at:
- gawkat(sock, path);
+ puts("gawking");
+ /* NOTE: SIGPIPE on second call.
+ * gawkat(sock, path); */
break;
case 'b': /* back */
if (*cmd == '\0')
@@ -278,28 +288,22 @@ gawk_at:
case 'g': /* goto or gawk */
if (*cmd == '\0') {
warnx("'g': No arguments.");
- break;;
+ break;
}
-
gawkparse(cmd, nhost, npath, nport);
- if (strcmp(host, nhost) != 0 && strcmp(port, nport) != 0) {
- done = gawk(child, depth, nhost, npath, nport);
- } else {
- child = connect_to(nhost, nport);
- if (child == -1) {
- warn("unable to connect '%s' '%s'", nhost, nport);
- break;
- }
- done = gawk(child, depth, nhost, npath, nport);
- if (close(child) == -1)
- warn("unable to close '%s' '%s'", nhost, npath);
- }
+ if (strcmp(host, nhost) != 0 || strcmp(port, nport) != 0)
+ done = gawk(-1, depth, nhost, npath, nport);
+ else
+ done = gawk(sock, depth, nhost, npath, nport);
break;
default:
warnx("invalid command '%c'", *--cmd);
}
}
+ if (closeit)
+ if (close(sock) == -1)
+ warn("unable to disconnect");
if (ferror(stdin)) {
warn("'stdin'");
return -1;