Merge branch 'working-dir-command-line-option'

Closes #347
This commit is contained in:
Daniel Eklöf 2021-02-14 11:17:08 +01:00
commit 689a4cc741
No known key found for this signature in database
GPG key ID: 5BBD4992C116573F
13 changed files with 108 additions and 16 deletions

View file

@ -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

View file

@ -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();

View file

@ -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"

View file

@ -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)"

View file

@ -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' \

View file

@ -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' \

View file

@ -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

View file

@ -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*.

32
main.c
View file

@ -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);

View file

@ -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,

View file

@ -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;
}

38
user-notification.c Normal file
View file

@ -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;
}

View file

@ -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);