From ab7b5a635cd5d7545831a1341278a9fec8499c30 Mon Sep 17 00:00:00 2001 From: airkoala Date: Wed, 17 Jun 2026 14:40:24 +1000 Subject: Add support for file:// URLs for initial working directory This will allow for foot to be registered as a MIME type handler for directories, allowing a "open with foot" action via xdg-open. Some applications pass a simple directory path (eg. qbittorrent) but others pass it as a "file://" URL (lutris), which is also sometimes percent-encoded (calibre) but sometimes not. This patch should be able to handle all 3 of these variants. --- CHANGELOG.md | 1 + client.c | 30 +++++++++++++++++++++++++++--- main.c | 30 +++++++++++++++++++++++++++--- meson.build | 2 +- 4 files changed, 56 insertions(+), 7 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 0ab6db99..e3650d56 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -99,6 +99,7 @@ ([#2417][2417]). * Foot now sets window urgency when a notification is activated, and foot hasn't received an XDG activation token. +* `-D,--working-directory` now supports `file://` URIs. [2383]: https://codeberg.org/dnkl/foot/issues/2383 [2371]: https://codeberg.org/dnkl/foot/issues/2371 diff --git a/client.c b/client.c index f35453b9..ab56d720 100644 --- a/client.c +++ b/client.c @@ -24,6 +24,7 @@ #include "macros.h" #include "util.h" #include "xmalloc.h" +#include "uri.h" extern char **environ; @@ -177,7 +178,7 @@ main(int argc, char *const *argv) {NULL, no_argument, NULL, 0}, }; - const char *custom_cwd = NULL; + char *custom_cwd = NULL; const char *server_socket_path = NULL; enum log_class log_level = LOG_CLASS_WARNING; enum log_colorize log_colorize = LOG_COLORIZE_AUTO; @@ -240,12 +241,34 @@ main(int argc, char *const *argv) break; case 'D': { + // try to parse optarg as a local file URL. if it's not a + // URL, use optarg directly + char *scheme, *host, *path; + if (uri_parse(optarg, strlen(optarg), &scheme, NULL, NULL, + &host, NULL, &path, NULL, NULL)) { + if (!(streq(scheme, "file") && hostname_is_localhost(host))) { + fprintf(stderr, "error: %s: not a local path\n", optarg); + free(scheme); + free(host); + free(path); + goto err; + } + + // ownership moved to custom_cwd, freed at err: + custom_cwd = path; + path = NULL; + } else + custom_cwd = xstrdup(optarg); + + free(scheme); + free(host); + free(path); + struct stat st; - if (stat(optarg, &st) < 0 || !(st.st_mode & S_IFDIR)) { + if (stat(custom_cwd, &st) < 0 || !(st.st_mode & S_IFDIR)) { fprintf(stderr, "error: %s: not a directory\n", optarg); goto err; } - custom_cwd = optarg; break; } @@ -598,6 +621,7 @@ err: free_string_list(&overrides); free(cargv); free(_cwd); + free(custom_cwd); if (fd != -1) close(fd); log_deinit(); diff --git a/main.c b/main.c index d9c5dc3c..383d57a3 100644 --- a/main.c +++ b/main.c @@ -33,6 +33,7 @@ #include "util.h" #include "xmalloc.h" #include "xsnprintf.h" +#include "uri.h" #if !defined(__STDC_UTF_32__) || !__STDC_UTF_32__ #error "char32_t does not use UTF-32" @@ -240,7 +241,7 @@ main(int argc, char *const *argv) bool check_config = false; const char *conf_path = NULL; - const char *custom_cwd = NULL; + char *custom_cwd = NULL; const char *pty_path = NULL; bool as_server = false; const char *conf_server_socket_path = NULL; @@ -294,12 +295,34 @@ main(int argc, char *const *argv) break; case 'D': { + /* try to parse optarg as a local file URL. if it's not a + URL, use optarg directly */ + char *scheme, *host, *path; + if (uri_parse(optarg, strlen(optarg), &scheme, NULL, NULL, + &host, NULL, &path, NULL, NULL)) { + if (!(streq(scheme, "file") && hostname_is_localhost(host))) { + fprintf(stderr, "error: %s: not a local path\n", optarg); + free(scheme); + free(host); + free(path); + return ret; + } + + // ownership moved to custom_cwd, freed at out: + custom_cwd = path; + path = NULL; + } else + custom_cwd = xstrdup(optarg); + + free(scheme); + free(host); + free(path); + struct stat st; - if (stat(optarg, &st) < 0 || !(st.st_mode & S_IFDIR)) { + if (stat(custom_cwd, &st) < 0 || !(st.st_mode & S_IFDIR)) { fprintf(stderr, "error: %s: not a directory\n", optarg); return ret; } - custom_cwd = optarg; break; } @@ -682,6 +705,7 @@ main(int argc, char *const *argv) out: free(_cwd); + free(custom_cwd); server_destroy(server); term_destroy(term); diff --git a/meson.build b/meson.build index 12f2a514..c530d844 100644 --- a/meson.build +++ b/meson.build @@ -357,7 +357,7 @@ executable( 'util.h', version, dependencies: [tllist, utf8proc], - link_with: common, + link_with: [common, misc], install: true) install_data( -- cgit v1.3