From 136d60606a1e01eb68ed984de63b7a55f8966075 Mon Sep 17 00:00:00 2001 From: Daniel Eklöf Date: Wed, 23 Jun 2021 14:22:18 +0200 Subject: client: send overrides for everything that is publicly visible in the conf MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Send a generic “overrides” list to the server, containing options in text, on the format “section.key=value”. This reduces the size of the base client/server protocol packet, as well as opens up for a generic -o,--override command line option (not yet implemented). --- client.c | 140 ++++++++++++++++++++++++++++++++++++--------------------------- 1 file changed, 81 insertions(+), 59 deletions(-) (limited to 'client.c') diff --git a/client.c b/client.c index 1445146c..9dfeda41 100644 --- a/client.c +++ b/client.c @@ -12,6 +12,8 @@ #include #include +#include + #define LOG_MODULE "foot-client" #define LOG_ENABLE_DBG 0 #include "log.h" @@ -96,22 +98,22 @@ main(int argc, char *const *argv) {NULL, no_argument, NULL, 0}, }; - const char *term = ""; - const char *title = ""; - const char *app_id = ""; + tll(char *) overrides = tll_init(); + const char *custom_cwd = NULL; - unsigned size_type = 0; // enum conf_size_type (without pulling in tllist/fcft via config.h) - unsigned width = 0; - unsigned height = 0; const char *server_socket_path = NULL; enum log_class log_level = LOG_CLASS_INFO; enum log_colorize log_colorize = LOG_COLORIZE_AUTO; - bool login_shell = false; - bool maximized = false; - bool fullscreen = false; bool hold = false; bool no_wait = false; + char buf[1024]; + + /* malloc:ed and needs to be in scope of all goto's */ + char *_cwd = NULL; + struct client_string *coverrides = NULL; + struct client_string *cargv = NULL; + while (true) { int c = getopt_long(argc, argv, "+t:T:a:w:W:mFLD:s:HNd:l::vh", longopts, NULL); if (c == -1) @@ -119,55 +121,62 @@ main(int argc, char *const *argv) switch (c) { case 't': - term = optarg; + snprintf(buf, sizeof(buf), "term=%s", optarg); + tll_push_back(overrides, xstrdup(buf)); break; case 'T': - title = optarg; + snprintf(buf, sizeof(buf), "title=%s", optarg); + tll_push_back(overrides, xstrdup(buf)); break; case 'a': - app_id = optarg; + snprintf(buf, sizeof(buf), "app-id=%s", optarg); + tll_push_back(overrides, xstrdup(buf)); break; case 'L': - login_shell = true; + tll_push_back(overrides, xstrdup("login-shell=yes")); break; case 'D': { struct stat st; if (stat(optarg, &st) < 0 || !(st.st_mode & S_IFDIR)) { fprintf(stderr, "error: %s: not a directory\n", optarg); - return ret; + goto err; } custom_cwd = optarg; break; } - case 'w': + case 'w': { + unsigned width, height; if (sscanf(optarg, "%ux%u", &width, &height) != 2 || width == 0 || height == 0) { fprintf(stderr, "error: invalid window-size-pixels: %s\n", optarg); return ret; } - size_type = 0; // CONF_SIZE_PX + snprintf(buf, sizeof(buf), "initial-window-size-pixels=%ux%u", width, height); + tll_push_back(overrides, xstrdup(buf)); break; + } - case 'W': + case 'W': { + unsigned width, height; if (sscanf(optarg, "%ux%u", &width, &height) != 2 || width == 0 || height == 0) { fprintf(stderr, "error: invalid window-size-chars: %s\n", optarg); - return ret; + goto err; } - size_type = 1; // CONF_SIZE_CELLS + snprintf(buf, sizeof(buf), "initial-window-size-chars=%ux%u", width, height); + tll_push_back(overrides, xstrdup(buf)); break; + } case 'm': - maximized = true; - fullscreen = false; + tll_push_back(overrides, xstrdup("initial-window-mode=maximized")); break; case 'F': - fullscreen = true; - maximized = false; + tll_push_back(overrides, xstrdup("initial-window-mode=fullscreen")); break; case 's': @@ -190,7 +199,7 @@ main(int argc, char *const *argv) "-d,--log-level: %s: argument must be one of %s\n", optarg, log_level_string_hint()); - return ret; + goto err; } log_level = lvl; break; @@ -205,20 +214,22 @@ main(int argc, char *const *argv) log_colorize = LOG_COLORIZE_ALWAYS; else { fprintf(stderr, "%s: argument must be one of 'never', 'always' or 'auto'\n", optarg); - return ret; + goto err; } break; case 'v': printf("footclient %s\n", version_and_features()); - return EXIT_SUCCESS; + ret = EXIT_SUCCESS; + goto err; case 'h': print_usage(prog_name); - return EXIT_SUCCESS; + ret = EXIT_SUCCESS; + goto err; case '?': - return ret; + goto err; } } @@ -227,10 +238,6 @@ main(int argc, char *const *argv) log_init(log_colorize, false, LOG_FACILITY_USER, log_level); - /* malloc:ed and needs to be in scope of all goto's */ - char *_cwd = NULL; - struct client_argv *cargv = NULL; - int fd = socket(AF_UNIX, SOCK_STREAM, 0); if (fd == -1) { LOG_ERRNO("failed to create socket"); @@ -290,33 +297,36 @@ main(int argc, char *const *argv) /* String lengths, including NULL terminator */ const size_t cwd_len = strlen(cwd) + 1; - const size_t term_len = strlen(term) + 1; - const size_t title_len = strlen(title) + 1; - const size_t app_id_len = strlen(app_id) + 1; + const size_t override_count = tll_length(overrides); const struct client_data data = { - .width = width, - .height = height, - .size_type = size_type, - .maximized = maximized, - .fullscreen = fullscreen, .hold = hold, - .login_shell = login_shell, .no_wait = no_wait, .cwd_len = cwd_len, - .term_len = term_len, - .title_len = title_len, - .app_id_len = app_id_len, + .override_count = override_count, .argc = argc, }; - /* Total packet length, not (yet) including argv[] */ - uint64_t total_len = ( - sizeof(data) + - cwd_len + - term_len + - title_len + - app_id_len); + /* Total packet length, not (yet) including overrides or argv[] */ + uint64_t total_len = sizeof(data) + cwd_len; + + /* Add overrides to total packet length */ + coverrides = xmalloc(override_count * sizeof(coverrides[0])); + { + size_t i = 0; + tll_foreach(overrides, it) { + const size_t len = strlen(it->item) + 1; + + if (len >= 1 << (8 * sizeof(coverrides[i].len))) { + LOG_ERR("override length overflow"); + goto err; + } + + coverrides[i].len = len; + total_len += sizeof(coverrides[i]) + coverrides[i].len; + i++; + } + } cargv = xmalloc(argc * sizeof(cargv[0])); @@ -336,9 +346,7 @@ main(int argc, char *const *argv) /* Check for size overflows */ if (total_len >= 1llu << (8 * sizeof(uint32_t)) || cwd_len >= 1 << (8 * sizeof(data.cwd_len)) || - term_len >= 1 << (8 * sizeof(data.term_len)) || - title_len >= 1 << (8 * sizeof(data.title_len)) || - app_id_len >= 1 << (8 * sizeof(data.app_id_len)) || + override_count > (size_t)(unsigned int)data.override_count || argc > (int)(unsigned int)data.argc) { LOG_ERR("size overflow"); @@ -348,21 +356,33 @@ main(int argc, char *const *argv) /* Send everything except argv[] */ if (send(fd, &(uint32_t){total_len}, sizeof(uint32_t), 0) != sizeof(uint32_t) || send(fd, &data, sizeof(data), 0) != sizeof(data) || - send(fd, cwd, cwd_len, 0) != cwd_len || - send(fd, term, term_len, 0) != term_len || - send(fd, title, title_len, 0) != title_len || - send(fd, app_id, app_id_len, 0) != app_id_len) + send(fd, cwd, cwd_len, 0) != cwd_len) { LOG_ERRNO("failed to send setup packet to server"); goto err; } + /* Send overrides */ + { + size_t i = 0; + tll_foreach(overrides, it) { + if (send(fd, &coverrides[i], sizeof(coverrides[i]), 0) != sizeof(coverrides[i]) || + send(fd, it->item, coverrides[i].len, 0) != coverrides[i].len) + { + LOG_ERRNO("failed to send setup packet (overrides) to server"); + goto err; + } + + i++; + } + } + /* Send argv[] */ for (size_t i = 0; i < argc; i++) { if (send(fd, &cargv[i], sizeof(cargv[i]), 0) != sizeof(cargv[i]) || send(fd, argv[i], cargv[i].len, 0) != cargv[i].len) { - LOG_ERRNO("failed to send setup packet to server"); + LOG_ERRNO("failed to send setup packet (argv) to server"); goto err; } } @@ -386,7 +406,9 @@ main(int argc, char *const *argv) ret = exit_code; err: + tll_free_and_free(overrides, free); free(cargv); + free(coverrides); free(_cwd); if (fd != -1) close(fd); -- cgit v1.3 From 63a64bdca1978814ade32949e4cbf0d9758e6cbe Mon Sep 17 00:00:00 2001 From: Daniel Eklöf Date: Wed, 23 Jun 2021 14:34:36 +0200 Subject: client: implement -o,--override --- client.c | 8 +++++++- 1 file changed, 7 insertions(+), 1 deletion(-) (limited to 'client.c') diff --git a/client.c b/client.c index 9dfeda41..9c08a037 100644 --- a/client.c +++ b/client.c @@ -63,6 +63,7 @@ print_usage(const char *prog_name) " -s,--server-socket=PATH path to the server UNIX domain socket (default=$XDG_RUNTIME_DIR/foot-$WAYLAND_DISPLAY.sock)\n" " -H,--hold remain open after child process exits\n" " -N,--no-wait detach the client process from the running terminal, exiting immediately\n" + " -o,--override=[section.]key=value override configuration option\n" " -d,--log-level={info|warning|error} log level (info)\n" " -l,--log-colorize=[{never|always|auto}] enable/disable colorization of log output on stderr\n" " -v,--version show the version number and quit\n"); @@ -91,6 +92,7 @@ main(int argc, char *const *argv) {"server-socket", required_argument, NULL, 's'}, {"hold", no_argument, NULL, 'H'}, {"no-wait", no_argument, NULL, 'N'}, + {"override", required_argument, NULL, 'o'}, {"log-level", required_argument, NULL, 'd'}, {"log-colorize", optional_argument, NULL, 'l'}, {"version", no_argument, NULL, 'v'}, @@ -115,7 +117,7 @@ main(int argc, char *const *argv) struct client_string *cargv = NULL; while (true) { - int c = getopt_long(argc, argv, "+t:T:a:w:W:mFLD:s:HNd:l::vh", longopts, NULL); + int c = getopt_long(argc, argv, "+t:T:a:w:W:mFLD:s:HNo:d:l::vh", longopts, NULL); if (c == -1) break; @@ -191,6 +193,10 @@ main(int argc, char *const *argv) no_wait = true; break; + case 'o': + tll_push_back(overrides, xstrdup(optarg)); + break; + case 'd': { int lvl = log_level_from_string(optarg); if (unlikely(lvl < 0)) { -- cgit v1.3 From 5fc8275eb2385933a1f068a4476ddfc28afce584 Mon Sep 17 00:00:00 2001 From: Daniel Eklöf Date: Wed, 23 Jun 2021 15:04:09 +0200 Subject: client: add and use function push_override() MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit We now track override data (length + malloc:ed string) in a struct, and push this struct to our overrides linked list. There’s a new function, push_override() that takes a string, calculates its length, strdup() it and pushes it to the linked list. This function also length-checks the string, to ensure we don’t overflow. This way, we don’t have to loop the overrides list twice; once when calculating the total length of all overrides, and once when sending the overrides to the server. Now, we can update the total length as we add overrides (i.e while parsing the command line, instead of afterwards). This means we only have to loop the list once, and that’s when sending it. This also means there’s no longer any need to malloc an array holding the lengths of each override. --- client.c | 111 ++++++++++++++++++++++++++++++++++++--------------------------- 1 file changed, 63 insertions(+), 48 deletions(-) (limited to 'client.c') diff --git a/client.c b/client.c index 9c08a037..7249c588 100644 --- a/client.c +++ b/client.c @@ -25,6 +25,12 @@ #include "version.h" #include "xmalloc.h" +struct override { + size_t len; + char *str; +}; +typedef tll(struct override) override_list_t; + static volatile sig_atomic_t aborted = 0; static void @@ -69,6 +75,21 @@ print_usage(const char *prog_name) " -v,--version show the version number and quit\n"); } +static bool NOINLINE +push_override(override_list_t *overrides, const char *s, uint64_t *total_len) +{ + size_t len = strlen(s) + 1; + if (len >= 1 << (8 * sizeof(struct client_string))) { + LOG_ERR("override length overflow"); + return false; + } + + struct override o = {len, xstrdup(s)}; + tll_push_back(*overrides, o); + *total_len += sizeof(struct client_string) + o.len; + return true; +} + int main(int argc, char *const *argv) { @@ -100,20 +121,23 @@ main(int argc, char *const *argv) {NULL, no_argument, NULL, 0}, }; - tll(char *) overrides = tll_init(); - const char *custom_cwd = NULL; const char *server_socket_path = NULL; enum log_class log_level = LOG_CLASS_INFO; enum log_colorize log_colorize = LOG_COLORIZE_AUTO; bool hold = false; + + /* Used to format overrides */ bool no_wait = false; char buf[1024]; + /* Total packet length, not (yet) including overrides or argv[] */ + uint64_t total_len = 0; + /* malloc:ed and needs to be in scope of all goto's */ char *_cwd = NULL; - struct client_string *coverrides = NULL; + override_list_t overrides = tll_init(); struct client_string *cargv = NULL; while (true) { @@ -124,21 +148,25 @@ main(int argc, char *const *argv) switch (c) { case 't': snprintf(buf, sizeof(buf), "term=%s", optarg); - tll_push_back(overrides, xstrdup(buf)); + if (!push_override(&overrides, buf, &total_len)) + goto err; break; case 'T': snprintf(buf, sizeof(buf), "title=%s", optarg); - tll_push_back(overrides, xstrdup(buf)); + if (!push_override(&overrides, buf, &total_len)) + goto err; break; case 'a': snprintf(buf, sizeof(buf), "app-id=%s", optarg); - tll_push_back(overrides, xstrdup(buf)); + if (!push_override(&overrides, buf, &total_len)) + goto err; break; case 'L': - tll_push_back(overrides, xstrdup("login-shell=yes")); + if (!push_override(&overrides, "login-shell=yes", &total_len)) + goto err; break; case 'D': { @@ -155,10 +183,12 @@ main(int argc, char *const *argv) unsigned width, height; if (sscanf(optarg, "%ux%u", &width, &height) != 2 || width == 0 || height == 0) { fprintf(stderr, "error: invalid window-size-pixels: %s\n", optarg); - return ret; + goto err; } + snprintf(buf, sizeof(buf), "initial-window-size-pixels=%ux%u", width, height); - tll_push_back(overrides, xstrdup(buf)); + if (!push_override(&overrides, buf, &total_len)) + goto err; break; } @@ -168,17 +198,21 @@ main(int argc, char *const *argv) fprintf(stderr, "error: invalid window-size-chars: %s\n", optarg); goto err; } + snprintf(buf, sizeof(buf), "initial-window-size-chars=%ux%u", width, height); - tll_push_back(overrides, xstrdup(buf)); + if (!push_override(&overrides, buf, &total_len)) + goto err; break; } case 'm': - tll_push_back(overrides, xstrdup("initial-window-mode=maximized")); + if (!push_override(&overrides, "initial-window-mode=maximized", &total_len)) + goto err; break; case 'F': - tll_push_back(overrides, xstrdup("initial-window-mode=fullscreen")); + if (!push_override(&overrides, "initial-window-mode=fullscreen", &total_len)) + goto err; break; case 's': @@ -194,7 +228,8 @@ main(int argc, char *const *argv) break; case 'o': - tll_push_back(overrides, xstrdup(optarg)); + if (!push_override(&overrides, optarg, &total_len)) + goto err; break; case 'd': { @@ -313,30 +348,11 @@ main(int argc, char *const *argv) .argc = argc, }; - /* Total packet length, not (yet) including overrides or argv[] */ - uint64_t total_len = sizeof(data) + cwd_len; - - /* Add overrides to total packet length */ - coverrides = xmalloc(override_count * sizeof(coverrides[0])); - { - size_t i = 0; - tll_foreach(overrides, it) { - const size_t len = strlen(it->item) + 1; - - if (len >= 1 << (8 * sizeof(coverrides[i].len))) { - LOG_ERR("override length overflow"); - goto err; - } - - coverrides[i].len = len; - total_len += sizeof(coverrides[i]) + coverrides[i].len; - i++; - } - } - - cargv = xmalloc(argc * sizeof(cargv[0])); + /* Total packet length, not (yet) including argv[] */ + total_len += sizeof(data) + cwd_len; /* Add argv[] size to total packet length */ + cargv = xmalloc(argc * sizeof(cargv[0])); for (size_t i = 0; i < argc; i++) { const size_t arg_len = strlen(argv[i]) + 1; @@ -369,17 +385,14 @@ main(int argc, char *const *argv) } /* Send overrides */ - { - size_t i = 0; - tll_foreach(overrides, it) { - if (send(fd, &coverrides[i], sizeof(coverrides[i]), 0) != sizeof(coverrides[i]) || - send(fd, it->item, coverrides[i].len, 0) != coverrides[i].len) - { - LOG_ERRNO("failed to send setup packet (overrides) to server"); - goto err; - } - - i++; + tll_foreach(overrides, it) { + const struct override *o = &it->item; + struct client_string s = {o->len}; + if (send(fd, &s, sizeof(s), 0) != sizeof(s) || + send(fd, o->str, o->len, 0) != o->len) + { + LOG_ERRNO("failed to send setup packet (overrides) to server"); + goto err; } } @@ -412,9 +425,11 @@ main(int argc, char *const *argv) ret = exit_code; err: - tll_free_and_free(overrides, free); + tll_foreach(overrides, it) { + free(it->item.str); + tll_remove(overrides, it); + } free(cargv); - free(coverrides); free(_cwd); if (fd != -1) close(fd); -- cgit v1.3