aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorDaniel Eklöf <daniel@ekloef.se>2021-02-14 10:17:08 +0000
committerDaniel Eklöf <daniel@ekloef.se>2021-02-14 10:17:08 +0000
commit689a4cc741f2dbb2dbefc16ab817438c953b568c (patch)
tree8368a8c7485136e3b5e17f654b2603b7c25cc470
parentMerge branch 'url-hint-letters' (diff)
parentclient: -D,--working-directory: check path exists and is a directory (diff)
Merge branch 'working-dir-command-line-option'
Closes #347
-rw-r--r--CHANGELOG.md2
-rw-r--r--client.c28
-rw-r--r--completions/fish/foot.fish1
-rw-r--r--completions/fish/footclient.fish1
-rw-r--r--completions/zsh/_foot1
-rw-r--r--completions/zsh/_footclient1
-rw-r--r--doc/foot.1.scd6
-rw-r--r--doc/footclient.1.scd4
-rw-r--r--main.c32
-rw-r--r--meson.build2
-rw-r--r--server.c1
-rw-r--r--user-notification.c38
-rw-r--r--user-notification.h7
13 files changed, 108 insertions, 16 deletions
diff --git a/CHANGELOG.md b/CHANGELOG.md
index f0c18059..ba20e9e5 100644
--- a/CHANGELOG.md
+++ b/CHANGELOG.md
@@ -43,6 +43,8 @@
(https://codeberg.org/dnkl/foot/issues/14).
* `-d,--log-level={info|warning|error}` to both `foot` and
`footclient` (https://codeberg.org/dnkl/foot/issues/337).
+* `-D,--working-directory=DIR` to both `foot` and `footclient`
+ (https://codeberg.org/dnkl/foot/issues/347)
### Changed
diff --git a/client.c b/client.c
index 73b243f7..c012555c 100644
--- a/client.c
+++ b/client.c
@@ -10,6 +10,7 @@
#include <sys/socket.h>
#include <sys/un.h>
+#include <sys/stat.h>
#define LOG_MODULE "foot-client"
#define LOG_ENABLE_DBG 0
@@ -54,6 +55,7 @@ print_usage(const char *prog_name)
" -m,--maximized start in maximized mode\n"
" -F,--fullscreen start in fullscreen mode\n"
" -L,--login-shell start shell as a login shell\n"
+ " -D,--working-directory=DIR directory to start in (CWD)\n"
" -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"
" -d,---log-level={info|warning|error} log level (info)\n"
@@ -77,6 +79,7 @@ main(int argc, char *const *argv)
{"maximized", no_argument, NULL, 'm'},
{"fullscreen", no_argument, NULL, 'F'},
{"login-shell", no_argument, NULL, 'L'},
+ {"working-directory", required_argument, NULL, 'D'},
{"server-socket", required_argument, NULL, 's'},
{"hold", no_argument, NULL, 'H'},
{"log-level", required_argument, NULL, 'd'},
@@ -89,6 +92,7 @@ main(int argc, char *const *argv)
const char *term = "";
const char *title = "";
const char *app_id = "";
+ 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;
@@ -101,7 +105,7 @@ main(int argc, char *const *argv)
bool hold = false;
while (true) {
- int c = getopt_long(argc, argv, "+t:T:a:w:W:mFLs:Hd:l::vh", longopts, NULL);
+ int c = getopt_long(argc, argv, "+t:T:a:w:W:mFLD:s:Hd:l::vh", longopts, NULL);
if (c == -1)
break;
@@ -122,6 +126,16 @@ main(int argc, char *const *argv)
login_shell = true;
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 EXIT_FAILURE;
+ }
+ custom_cwd = optarg;
+ break;
+ }
+
case 'w':
if (sscanf(optarg, "%ux%u", &width, &height) != 2 || width == 0 || height == 0) {
fprintf(stderr, "error: invalid window-size-pixels: %s\n", optarg);
@@ -202,7 +216,7 @@ 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;
+ char *_cwd = NULL;
struct client_argv *cargv = NULL;
int fd = socket(AF_UNIX, SOCK_STREAM, 0);
@@ -247,17 +261,19 @@ main(int argc, char *const *argv)
}
}
- {
+ const char *cwd = custom_cwd;
+ if (cwd == NULL) {
errno = 0;
size_t buf_len = 1024;
do {
- cwd = xrealloc(cwd, buf_len);
- if (getcwd(cwd, buf_len) == NULL && errno != ERANGE) {
+ _cwd = xrealloc(_cwd, buf_len);
+ if (getcwd(_cwd, buf_len) == NULL && errno != ERANGE) {
LOG_ERRNO("failed to get current working directory");
goto err;
}
buf_len *= 2;
} while (errno == ERANGE);
+ cwd = _cwd;
}
/* String lengths, including NULL terminator */
@@ -356,7 +372,7 @@ main(int argc, char *const *argv)
err:
free(cargv);
- free(cwd);
+ free(_cwd);
if (fd != -1)
close(fd);
log_deinit();
diff --git a/completions/fish/foot.fish b/completions/fish/foot.fish
index de31c547..58ae8f11 100644
--- a/completions/fish/foot.fish
+++ b/completions/fish/foot.fish
@@ -8,6 +8,7 @@ complete -c foot -x -s a -l app-id
complete -c foot -s m -l maximized -d "start in maximized mode"
complete -c foot -s F -l fullscreen -d "start in fullscreen mode"
complete -c foot -s L -l login-shell -d "start shell as a login shell"
+complete -c foot -F -s D -l working-directory -d "initial working directory for the client application (CWD)"
complete -c foot -x -s w -l window-size-pixels -d "window WIDTHxHEIGHT, in pixels (700x500)"
complete -c foot -x -s W -l window-size-chars -d "window WIDTHxHEIGHT, in characters (not set)"
complete -c foot -F -s s -l server -d "run as server; open terminals by running footclient"
diff --git a/completions/fish/footclient.fish b/completions/fish/footclient.fish
index 880aaed7..09dc7305 100644
--- a/completions/fish/footclient.fish
+++ b/completions/fish/footclient.fish
@@ -5,6 +5,7 @@ complete -c footclient -x -s a -l app-id
complete -c footclient -s m -l maximized -d "start in maximized mode"
complete -c footclient -s F -l fullscreen -d "start in fullscreen mode"
complete -c footclient -s L -l login-shell -d "start shell as a login shell"
+complete -c footclient -F -s D -l working-directory -d "initial working directory for the client application (CWD)"
complete -c footclient -x -s w -l window-size-pixels -d "window WIDTHxHEIGHT, in pixels (700x500)"
complete -c footclient -x -s W -l window-size-chars -d "window WIDTHxHEIGHT, in characters (not set)"
complete -c footclient -F -s s -l server-socket -d "override the default path to the foot server socket ($XDG_RUNTIME_DIR/foot-$WAYLAND_DISPLAY.sock)"
diff --git a/completions/zsh/_foot b/completions/zsh/_foot
index b706ea2d..d87ad919 100644
--- a/completions/zsh/_foot
+++ b/completions/zsh/_foot
@@ -11,6 +11,7 @@ _arguments \
'(-m --maximized)'{-m,--maximized}'[start in maximized mode]' \
'(-F --fullscreen)'{-F,--fullscreen}'[start in fullscreen mode]' \
'(-L --login-shell)'{-L,--login-shell}'[start shell as a login shell]' \
+ '(-D --working-directory)'{-D,--working-directory}'[initial working directory for the client application (CWD)]:working_directory:_files' \
'(-w --window-size-pixels)'{-w,--window-size-pixels}'[window WIDTHxHEIGHT, in pixels (700x500)]:size_pixels:()' \
'(-W --window-size-chars)'{-W,--window-size-chars}'[window WIDTHxHEIGHT, in characters (not set)]:size_chars:()' \
'(-s --server)'{-s,--server}'[run as server; open terminals by running footclient]:server:_files' \
diff --git a/completions/zsh/_footclient b/completions/zsh/_footclient
index df9b7f25..f8537748 100644
--- a/completions/zsh/_footclient
+++ b/completions/zsh/_footclient
@@ -8,6 +8,7 @@ _arguments \
'(-m --maximized)'{-m,--maximized}'[start in maximized mode]' \
'(-F --fullscreen)'{-F,--fullscreen}'[start in fullscreen mode]' \
'(-L --login-shell)'{-L,--login-shell}'[start shell as a login shell]' \
+ '(-D --working-directory)'{-D,--working-directory}'[initial working directory for the client application (CWD)]:working_directory:_files' \
'(-w --window-size-pixels)'{-w,--window-size-pixels}'[window WIDTHxHEIGHT, in pixels (700x500)]:size_pixels:()' \
'(-W --window-size-chars)'{-W,--window-size-chars}'[window WIDTHxHEIGHT, in characters (not set)]:size_chars:()' \
'(-s --server-socket)'{-s,--server-socket}'[override the default path to the foot server socket ($XDG_RUNTIME_DIR/foot-$WAYLAND_DISPLAY.sock)]:server:_files' \
diff --git a/doc/foot.1.scd b/doc/foot.1.scd
index 2893842c..9b5079b8 100644
--- a/doc/foot.1.scd
+++ b/doc/foot.1.scd
@@ -69,9 +69,13 @@ the foot command line
Start in fullscreen mode. If both *--maximized* and *--fullscreen*
are specified, the _last_ one takes precedence.
-*--login-shell*
+*-L*,*--login-shell*
Start a login shell, by prepending a '-' to argv[0].
+*-D*,*--working-directory*=_DIR_
+ Initial working directory for the client application. Default:
+ _CWD of foot_.
+
*-s*,*--server*[=_PATH_]
Run as a server. In this mode, a single foot instance hosts
multiple terminals (windows). Use *footclient*(1) to launch new
diff --git a/doc/footclient.1.scd b/doc/footclient.1.scd
index 374a9a3a..107e6eaa 100644
--- a/doc/footclient.1.scd
+++ b/doc/footclient.1.scd
@@ -49,6 +49,10 @@ terminal has terminated).
*-L*,*--login-shell*
Start a login shell, by prepending a '-' to argv[0].
+*-D*,*--working-directory*=_DIR_
+ Initial working directory for the client application. Default:
+ _CWD of footclient_.
+
*-s*,*--server-socket*=_PATH_
Connect to _PATH_ instead of
*$XDG\_RUNTIME\_DIR/foot-$WAYLAND\_DISPLAY.sock*.
diff --git a/main.c b/main.c
index b0ae8650..87f7a058 100644
--- a/main.c
+++ b/main.c
@@ -67,6 +67,7 @@ print_usage(const char *prog_name)
" -m,--maximized start in maximized mode\n"
" -F,--fullscreen start in fullscreen mode\n"
" -L,--login-shell start shell as a login shell\n"
+ " -D,--working-directory=DIR directory to start in (CWD)\n"
" -w,--window-size-pixels=WIDTHxHEIGHT initial width and height, in pixels\n"
" -W,--window-size-chars=WIDTHxHEIGHT initial width and height, in characters\n"
" -s,--server[=PATH] run as a server (use 'footclient' to start terminals).\n"
@@ -163,6 +164,7 @@ main(int argc, char *const *argv)
{"title", required_argument, NULL, 'T'},
{"app-id", required_argument, NULL, 'a'},
{"login-shell", no_argument, NULL, 'L'},
+ {"working-directory", required_argument, NULL, 'D'},
{"font", required_argument, NULL, 'f'},
{"window-size-pixels", required_argument, NULL, 'w'},
{"window-size-chars", required_argument, NULL, 'W'},
@@ -185,6 +187,7 @@ main(int argc, char *const *argv)
const char *conf_term = NULL;
const char *conf_title = NULL;
const char *conf_app_id = NULL;
+ const char *custom_cwd = NULL;
bool login_shell = false;
tll(char *) conf_fonts = tll_init();
enum conf_size_type conf_size_type = CONF_SIZE_PX;
@@ -204,7 +207,7 @@ main(int argc, char *const *argv)
user_notifications_t user_notifications = tll_init();
while (true) {
- int c = getopt_long(argc, argv, "+c:Ct:T:a:Lf:w:W:s::HmFPp:d:l::Svh", longopts, NULL);
+ int c = getopt_long(argc, argv, "+c:Ct:T:a:LD:f:w:W:s::HmFPp:d:l::Svh", longopts, NULL);
if (c == -1)
break;
@@ -233,6 +236,16 @@ main(int argc, char *const *argv)
conf_app_id = optarg;
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 EXIT_FAILURE;
+ }
+ custom_cwd = optarg;
+ break;
+ }
+
case 'f':
tll_free_and_free(conf_fonts, free);
for (char *font = strtok(optarg, ","); font != NULL; font = strtok(NULL, ",")) {
@@ -443,18 +456,21 @@ main(int argc, char *const *argv)
struct server *server = NULL;
struct shutdown_context shutdown_ctx = {.term = &term, .exit_code = EXIT_FAILURE};
- char *cwd = NULL;
- {
+ const char *cwd = custom_cwd;
+ char *_cwd = NULL;
+
+ if (cwd == NULL) {
errno = 0;
size_t buf_len = 1024;
do {
- cwd = xrealloc(cwd, buf_len);
- if (getcwd(cwd, buf_len) == NULL && errno != ERANGE) {
+ _cwd = xrealloc(_cwd, buf_len);
+ if (getcwd(_cwd, buf_len) == NULL && errno != ERANGE) {
LOG_ERRNO("failed to get current working directory");
goto out;
}
buf_len *= 2;
} while (errno == ERANGE);
+ cwd = _cwd;
}
shm_set_max_pool_size(conf.tweak.max_shm_pool_size);
@@ -476,8 +492,8 @@ main(int argc, char *const *argv)
&term_shutdown_cb, &shutdown_ctx)) == NULL) {
goto out;
}
- free(cwd);
- cwd = NULL;
+ free(_cwd);
+ _cwd = NULL;
if (as_server && (server = server_init(&conf, fdm, reaper, wayl)) == NULL)
goto out;
@@ -510,7 +526,7 @@ main(int argc, char *const *argv)
ret = aborted || tll_length(wayl->terms) == 0 ? EXIT_SUCCESS : EXIT_FAILURE;
out:
- free(cwd);
+ free(_cwd);
server_destroy(server);
term_destroy(term);
diff --git a/meson.build b/meson.build
index 1465fa3e..f17ab7ef 100644
--- a/meson.build
+++ b/meson.build
@@ -181,7 +181,7 @@ executable(
'spawn.c', 'spawn.h',
'tokenize.c', 'tokenize.h',
'url-mode.c', 'url-mode.h',
- 'user-notification.h',
+ 'user-notification.c', 'user-notification.h',
'wayland.c', 'wayland.h',
wl_proto_src + wl_proto_headers, version,
dependencies: [math, threads, libepoll, pixman, wayland_client, wayland_cursor, xkb, fontconfig,
diff --git a/server.c b/server.c
index 7a4101ea..6fbfaee6 100644
--- a/server.c
+++ b/server.c
@@ -275,6 +275,7 @@ fdm_client(struct fdm *fdm, int fd, int events, void *data)
if (client->term == NULL) {
LOG_ERR("failed to instantiate new terminal");
+ client_send_exit_code(client, -1);
goto shutdown;
}
diff --git a/user-notification.c b/user-notification.c
new file mode 100644
index 00000000..497aa08c
--- /dev/null
+++ b/user-notification.c
@@ -0,0 +1,38 @@
+#include "user-notification.h"
+#include <stdio.h>
+#include <stdarg.h>
+
+static bool
+user_notification_add_va(user_notifications_t *notifications,
+ enum user_notification_kind kind, const char *fmt,
+ va_list ap)
+{
+ va_list ap2;
+ va_copy(ap2, ap);
+ int cnt = vsnprintf(NULL, 0, fmt, ap2);
+ va_end(ap2);
+
+ if (cnt < 0)
+ return false;
+
+ char *text = malloc(cnt + 1);
+ vsnprintf(text, cnt + 1, fmt, ap);
+
+ struct user_notification not = {
+ .kind = kind,
+ .text = text,
+ };
+ tll_push_back(*notifications, not);
+ return true;
+}
+
+bool
+user_notification_add(user_notifications_t *notifications,
+ enum user_notification_kind kind, const char *fmt, ...)
+{
+ va_list ap;
+ va_start(ap, fmt);
+ bool ret = user_notification_add_va(notifications, kind, fmt, ap);
+ va_end(ap);
+ return ret;
+}
diff --git a/user-notification.h b/user-notification.h
index f184871e..507c1791 100644
--- a/user-notification.h
+++ b/user-notification.h
@@ -1,7 +1,10 @@
#pragma once
+#include <stdbool.h>
#include <tllist.h>
+#include "macros.h"
+
enum user_notification_kind {
USER_NOTIFICATION_DEPRECATED,
USER_NOTIFICATION_WARNING,
@@ -22,3 +25,7 @@ user_notifications_free(user_notifications_t *notifications)
free(it->item.text);
tll_free(*notifications);
}
+
+bool user_notification_add(user_notifications_t *notifications,
+ enum user_notification_kind kind,
+ const char *fmt, ...) PRINTF(3);