From f51f18d33557ea9078f7781f4a4235706228f5c9 Mon Sep 17 00:00:00 2001 From: Daniel Eklöf Date: Thu, 12 Nov 2026 11:50:55 +0100 Subject: spawn-terminal: spawn with all the original command line options Before this, spawn-terminal would spawn a new foot or footclient, but *without* any of the command line options the user started the original foot/footclient with. We did have a hack for --config, but all other options were forgotten in the new instance. This patch fixes this. * term_init() stores the raw argc/argv in the terminal struct. * term_spawn_new() calls spawn() with the stored argc/argv. * for single instances, main() calls term_init() with the unparsed argc/argv. * the client/server protocol is extended to always send the raw argc/argv to the server. * the server calls term_init() with the argc/argv from the client. * bonus: on linux, we now use /proc/self/exe to determine which binary to launch. This means if you run e.g. a foot from your build tree, spawn-terminal will exec _that_ foot, not the system foot. A couple of behavioral changes, and/or side-effects, caused by the above (not exhaustive): * footclient -E: spawn-terminal will not use the _exact_ environment from the original footclient, but will simply pass -E to the new footclient. This means the new instance will inherit the *current* environment of the original instance. * foot htop: spawn-instance will _also_ launch a new foot+htop. Before this patch, spawn-instance would launch a new foot+shell. Closes #1622 --- main.c | 25 ++++++++++++++++++++++--- 1 file changed, 22 insertions(+), 3 deletions(-) (limited to 'main.c') diff --git a/main.c b/main.c index 383d57a3..d1f5dbf7 100644 --- a/main.c +++ b/main.c @@ -108,7 +108,20 @@ print_usage(const char *prog_name) puts(options); } -bool +static const char * +get_foot_exe(void) +{ + /* Linux only, fallback to "foot" */ + static char lnk[PATH_MAX]; + ssize_t ret = readlink("/proc/self/exe", lnk, sizeof(lnk) - 1); + if (ret < 0) + return "foot"; + + lnk[ret] = '\0'; + return lnk; +} + +static bool locale_is_utf8(void) { static const char u8[] = u8"ö"; @@ -208,6 +221,12 @@ main(int argc, char *const *argv) * don't pass this on to programs launched by us */ unsetenv("DESKTOP_STARTUP_ID"); + /* Temporary "clean" copy of argv, for term_init() */ + const int foot_argc = argc; + const char *foot_argv[foot_argc]; + for (int i = 0; i < foot_argc; i++) + foot_argv[i] = argv[i]; + const char *const prog_name = argc > 0 ? argv[0] : ""; static const struct option longopts[] = { @@ -649,8 +668,8 @@ main(int argc, char *const *argv) goto out; if (!as_server && (term = term_init( - &conf, fdm, reaper, wayl, "foot", cwd, token, pty_path, - argc, argv, NULL, + &conf, fdm, reaper, wayl, get_foot_exe(), cwd, token, pty_path, + argc, argv, NULL, foot_argc, foot_argv, &term_shutdown_cb, &shutdown_ctx)) == NULL) { goto out; } -- cgit v1.3