From 6637c8aeda76a46a953267b082aada7557eab49c Mon Sep 17 00:00:00 2001 From: Daniel Eklöf Date: Fri, 1 Nov 2019 20:39:34 +0100 Subject: client: a standalone binary that connects to a foot --server --- client.c | 130 +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 130 insertions(+) create mode 100644 client.c (limited to 'client.c') diff --git a/client.c b/client.c new file mode 100644 index 00000000..bcd3fa6b --- /dev/null +++ b/client.c @@ -0,0 +1,130 @@ +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include + +#include +#include + +#define LOG_MODULE "foot-client" +#include "log.h" +#include "version.h" + +static volatile sig_atomic_t aborted = 0; + +static void +sig_handler(int signo) +{ + aborted = 1; +} + +static void +print_usage(const char *prog_name) +{ + printf("Usage: %s [OPTIONS]...\n", prog_name); + printf("\n"); + printf("Options:\n"); + printf(" -v,--version show the version number and quit\n"); +} + +int +main(int argc, char *const *argv) +{ + int ret = EXIT_FAILURE; + + const char *const prog_name = argv[0]; + + while (true) { + int c = getopt_long(argc, argv, ":hv", NULL, NULL); + if (c == -1) + break; + + switch (c) { + case 'v': + printf("footclient version %s\n", FOOT_VERSION); + return EXIT_SUCCESS; + + case 'h': + print_usage(prog_name); + return EXIT_SUCCESS; + + case ':': + fprintf(stderr, "error: -%c: missing required argument\n", optopt); + return EXIT_FAILURE; + + case '?': + fprintf(stderr, "error: -%c: invalid option\n", optopt); + return EXIT_FAILURE; + } + } + + argc -= optind; + argv += optind; + + int fd = socket(AF_UNIX, SOCK_STREAM, 0); + if (fd == -1) { + LOG_ERRNO("failed to create socket"); + goto err; + } + + bool connected = false; + struct sockaddr_un addr = {.sun_family = AF_UNIX}; + + const char *xdg_runtime = getenv("XDG_RUNTIME_DIR"); + if (xdg_runtime != NULL) { + snprintf(addr.sun_path, sizeof(addr.sun_path), "%s/foot.sock", xdg_runtime); + + if (connect(fd, (const struct sockaddr *)&addr, sizeof(addr)) == 0) + connected = true; + } + + if (!connected) { + strncpy(addr.sun_path, "/tmp/foot.sock", sizeof(addr.sun_path) - 1); + if (connect(fd, (const struct sockaddr *)&addr, sizeof(addr)) < 0) { + LOG_ERRNO("failed to connect (is 'foot --server' running?)"); + goto err; + } + } + + if (send(fd, &argc, sizeof(argc), 0) != sizeof(argc)) { + LOG_ERRNO("failed to send argc/argv to server"); + goto err; + } + + for (int i = 0; i < argc; i++) { + uint16_t len = strlen(argv[i]); + + if (send(fd, &len, sizeof(len), 0) != sizeof(len) || + send(fd, argv[i], len, 0) != sizeof(len)) + { + LOG_ERRNO("failed to send argc/argv to server"); + goto err; + } + } + + if (sigaction(SIGINT, &(struct sigaction){.sa_handler = &sig_handler}, NULL) < 0) { + LOG_ERRNO("failed to register signal handlers"); + goto err; + } + + int exit_code; + ssize_t rcvd = recv(fd, &exit_code, sizeof(exit_code), 0); + + if (rcvd == -1 && errno == EINTR) + assert(aborted); + else if (rcvd != sizeof(exit_code)) + LOG_ERRNO("failed to read server response"); + else + ret = exit_code; + +err: + if (fd != -1) + close(fd); + return ret; +} -- cgit v1.3 From 77ea7fc61c307c81698904c03796c1920e7a0856 Mon Sep 17 00:00:00 2001 From: Daniel Eklöf Date: Fri, 1 Nov 2019 20:50:00 +0100 Subject: client: add long options for --verbose and --help --- client.c | 8 +++++++- 1 file changed, 7 insertions(+), 1 deletion(-) (limited to 'client.c') diff --git a/client.c b/client.c index bcd3fa6b..25dcbd27 100644 --- a/client.c +++ b/client.c @@ -40,8 +40,14 @@ main(int argc, char *const *argv) const char *const prog_name = argv[0]; + static const struct option longopts[] = { + {"version", no_argument, 0, 'v'}, + {"help", no_argument, 0, 'h'}, + {NULL, no_argument, 0, 0}, + }; + while (true) { - int c = getopt_long(argc, argv, ":hv", NULL, NULL); + int c = getopt_long(argc, argv, ":hv", longopts, NULL); if (c == -1) break; -- cgit v1.3 From 438d6eaff0b4280492c8a0a161a33daec5de3707 Mon Sep 17 00:00:00 2001 From: Daniel Eklöf Date: Fri, 1 Nov 2019 21:10:47 +0100 Subject: client/server: add -t,--term to footclient --- client.c | 20 ++++++++++++++++++-- completions/zsh/_footclient | 7 +++++++ server.c | 16 +++++++++++++++- 3 files changed, 40 insertions(+), 3 deletions(-) (limited to 'client.c') diff --git a/client.c b/client.c index 25dcbd27..bac72838 100644 --- a/client.c +++ b/client.c @@ -30,7 +30,8 @@ print_usage(const char *prog_name) printf("Usage: %s [OPTIONS]...\n", prog_name); printf("\n"); printf("Options:\n"); - printf(" -v,--version show the version number and quit\n"); + printf(" -t,--term=TERM value to set the environment variable TERM to (foot)\n" + " -v,--version show the version number and quit\n"); } int @@ -41,17 +42,24 @@ main(int argc, char *const *argv) const char *const prog_name = argv[0]; static const struct option longopts[] = { + {"term", required_argument, 0, 't'}, {"version", no_argument, 0, 'v'}, {"help", no_argument, 0, 'h'}, {NULL, no_argument, 0, 0}, }; + const char *term = ""; + while (true) { - int c = getopt_long(argc, argv, ":hv", longopts, NULL); + int c = getopt_long(argc, argv, ":t:hv", longopts, NULL); if (c == -1) break; switch (c) { + case 't': + term = optarg; + break; + case 'v': printf("footclient version %s\n", FOOT_VERSION); return EXIT_SUCCESS; @@ -98,6 +106,14 @@ main(int argc, char *const *argv) } } + uint16_t term_len = strlen(term); + if (send(fd, &term_len, sizeof(term_len), 0) != sizeof(term_len) || + send(fd, term, term_len, 0) != term_len) + { + LOG_ERRNO("failed to send TERM to server"); + goto err; + } + if (send(fd, &argc, sizeof(argc), 0) != sizeof(argc)) { LOG_ERRNO("failed to send argc/argv to server"); goto err; diff --git a/completions/zsh/_footclient b/completions/zsh/_footclient index 6ab98fd0..47a050cb 100644 --- a/completions/zsh/_footclient +++ b/completions/zsh/_footclient @@ -2,5 +2,12 @@ _arguments \ -s \ + '(-t,--term)'{-t,--term}'[value to set the environment variable TERM to (foot)]:term:->terms' \ '(-v --version)'{-v,--version}'[show the version number and quit]' \ '(-h --help)'{-h,--help}'[show help message and quit]' + +case ${state} in + terms) + _values 'terminal definitions' $(find /usr/share/terminfo -type f -printf "%f\n") + ;; +esac diff --git a/server.c b/server.c index f48ed262..6307b1bf 100644 --- a/server.c +++ b/server.c @@ -94,12 +94,22 @@ fdm_client(struct fdm *fdm, int fd, int events, void *data) { struct client *client = data; struct server *server = client->server; + char *term_env = NULL; if (events & EPOLLHUP) goto shutdown; assert(events & EPOLLIN); + uint16_t term_env_len; + if (recv(fd, &term_env_len, sizeof(term_env_len), 0) != sizeof(term_env_len)) + goto shutdown; + + term_env = malloc(term_env_len + 1); + term_env[term_env_len] = '\0'; + if (recv(fd, term_env, term_env_len, 0) != term_env_len) + goto shutdown; + if (recv(fd, &client->argc, sizeof(client->argc), 0) != sizeof(client->argc)) goto shutdown; @@ -117,7 +127,8 @@ fdm_client(struct fdm *fdm, int fd, int events, void *data) assert(client->term == NULL); client->term = term_init( - server->conf, server->fdm, server->wayl, server->conf->term, + server->conf, server->fdm, server->wayl, + term_env_len > 0 ? term_env : server->conf->term, client->argc, client->argv, &term_shutdown_handler, client); if (client->term == NULL) { @@ -125,11 +136,14 @@ fdm_client(struct fdm *fdm, int fd, int events, void *data) goto shutdown; } + free(term_env); return true; shutdown: LOG_DBG("client FD=%d: disconnected", client->fd); + free(term_env); + fdm_del(fdm, fd); client->fd = -1; -- cgit v1.3 From 2544e93c0e47f52362f6ce6945d7bf17d0b37e24 Mon Sep 17 00:00:00 2001 From: Daniel Eklöf Date: Fri, 1 Nov 2019 21:13:37 +0100 Subject: client: register signal handler for SIGTERM too --- client.c | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) (limited to 'client.c') diff --git a/client.c b/client.c index bac72838..6b36a4bb 100644 --- a/client.c +++ b/client.c @@ -130,7 +130,8 @@ main(int argc, char *const *argv) } } - if (sigaction(SIGINT, &(struct sigaction){.sa_handler = &sig_handler}, NULL) < 0) { + const struct sigaction sa = {.sa_handler = &sig_handler}; + if (sigaction(SIGINT, &sa, NULL) < 0 || sigaction(SIGTERM, &sa, NULL) < 0) { LOG_ERRNO("failed to register signal handlers"); goto err; } -- cgit v1.3