aboutsummaryrefslogtreecommitdiffstats
path: root/include
diff options
context:
space:
mode:
authorPeter Geissler <peter@haxx.in>2026-08-28 00:11:24 +0200
committerPeter Geissler <peter@haxx.in>2026-08-28 00:23:19 +0200
commitd1b5a64c3855b07074b3c958739df4e16f6084a5 (patch)
tree10579e84bd972661738f93fdd8c221674f09b857 /include
parentmacos zip: carry the same paperwork as the linux tarballs (diff)
downloadslosh-d1b5a64c3855b07074b3c958739df4e16f6084a5.tar.gz
slosh-d1b5a64c3855b07074b3c958739df4e16f6084a5.tar.xz
slosh-d1b5a64c3855b07074b3c958739df4e16f6084a5.zip
windows: a native port, on ConPTY and AF_UNIX
slosh builds and runs on Windows 10/11, x86-64 and ARM64, as one exe with no runtime dependency beyond the OS: panes, splits, tabs, detachable sessions, the config file and its live reload, the control socket, the mouse. The port is a compatibility layer rather than a fork. compat/win holds stub <poll.h>, <sys/socket.h>, <termios.h> and friends that redirect to include/compat_win.h, which is force-included ahead of every translation unit -- so the existing sources keep their POSIX includes and their POSIX call sites, and the platform differences are answered in one place. Two decisions carry most of it. Everything the event loop waits on is a socket. On POSIX a pty master, an inotify descriptor and a unix socket are all file descriptors, so one poll() covers them; Windows has no such union, and WSAPoll waits on SOCKETs and nothing else. Rather than rewrite server.c's loop around WaitForMultipleObjects -- which caps at 64 objects and cannot express POLLOUT -- each non-socket source gets a pump thread that copies it into one end of a loopback socketpair. The loop is then the POSIX one unchanged, and only what sits behind the descriptor differs. Socket descriptors are tagged rather than guessed: a wrapped SOCKET comes back as SL_SOCK_BASE + slot, so read, write, close and poll can tell a socket from a CRT descriptor with certainty instead of inferring it from the numeric range. That is why socket() and accept() are shimmed too -- an untagged descriptor escaping into the loop is the one failure mode this scheme has, and it is worth closing completely. Three things bit hard enough to be worth naming. A pseudoconsole does not stop the child inheriting your stdio. CreateProcess copies the parent's standard handles into the child when STARTF_USESTDHANDLES is absent, and those win. With slosh's own stdout redirected -- a pipe under ssh, a file under --script, a log under the daemon -- the shell wrote its banner into slosh's output instead of into the pane, and the pane had a correct title (ConPTY emits that itself) over an empty body. The three handles are cleared for the duration of the call, leaving the child nothing to inherit. A ConPTY never reports EOF. The output pipe belongs to the pseudoconsole, not to the child, so it stays open after the child exits and pane_pump()'s read() -- the entire mechanism by which a pane learns its program is gone -- never returns 0. A pane whose shell had been told `exit` sat there looking alive forever. So the exit is waited for on the process handle, and ClosePseudoConsole is what then produces the EOF. That third thread is also why the per-pty state is reference counted: a pump blocked on a dead pipe must not be waited for, or pty_close would hang the session on the one program that misbehaves, so whoever finishes last frees. cmd.exe does not use CommandLineToArgvW's quoting. A literal quote is \" to an ordinary program and a backslash-then-end-of-quoted-section to cmd, so building `cmd /c notepad "C:\...\config.kdl"` the first way handed the editor \"C:\...\config.kdl\", backslashes and all, and notepad answered "Not a valid file name" about a path that existed. cmd /c now gets the rest of the line verbatim -- the caller composed a complete command with its own quoting, exactly as it does for sh -c. Paths are mixed, and only some programs mind. slosh joins with '/' while the environment supplies '\', so the config landed at C:\Users\you/.config/slosh/config.kdl. The CRT opens that without complaint, which is why --check reported it fine, but a GUI file dialog rejects it outright. Anything handed to another program now goes through sl_path_native() first; the internal representation is left alone. The rest is small. Panes run %ComSpec%, sessions live in %LOCALAPPDATA%\slosh rather than $XDG_RUNTIME_DIR because a session socket must not follow a roaming profile to another host, there is no SIGWINCH so a resize is noticed by comparing the console size on a bounded poll, no fork so the daemon is a fresh detached process that reconstructs its arguments on the command line, and no process group on a pty so the foreground program is the youngest descendant of the pane's shell. C-a e prefers whichever console editor is actually installed -- notepad is a GUI program, so a pane running it draws nothing and looks broken -- and says so when it has to fall back. make -f Makefile.windows ARCH=x86_64 (or aarch64) cross-compiles from a mac or Linux, and builds natively on the box with zig on PATH.
Diffstat (limited to 'include')
-rw-r--r--include/compat_win.h349
-rw-r--r--include/slosh.h14
2 files changed, 363 insertions, 0 deletions
diff --git a/include/compat_win.h b/include/compat_win.h
new file mode 100644
index 0000000..b52f512
--- /dev/null
+++ b/include/compat_win.h
@@ -0,0 +1,349 @@
+/* Windows compatibility shim for slosh.
+ *
+ * The port's one idea: *everything the event loop waits on is a socket*.
+ *
+ * On POSIX a pty master, an inotify fd and a unix socket are all fds, so one
+ * poll() waits on all of them. Windows has no such union -- a ConPTY hands
+ * back pipe HANDLEs, a directory watch is a completion callback, and WSAPoll
+ * waits on SOCKETs and nothing else. Rather than rewrite server.c's loop
+ * around WaitForMultipleObjects (which caps at 64 and cannot express POLLOUT),
+ * each non-socket source gets a pump thread that copies it into one end of a
+ * loopback socketpair. The loop then polls sockets exclusively and its
+ * structure is unchanged from the POSIX build.
+ *
+ * Socket "fds" are tagged integers (SL_SOCK_BASE + slot) rather than raw
+ * SOCKET values, so read/write/close can tell a socket from a CRT fd with
+ * certainty instead of guessing from the numeric range.
+ *
+ * System headers are included *before* the POSIX names are macro-defined, so
+ * that e.g. unistd.h's own `read` declaration is seen as itself and not
+ * rewritten into ours. */
+#ifndef SLOSH_COMPAT_WIN_H
+#define SLOSH_COMPAT_WIN_H
+
+#ifdef _WIN32
+
+#ifndef WIN32_LEAN_AND_MEAN
+#define WIN32_LEAN_AND_MEAN
+#endif
+#ifndef _WIN32_WINNT
+#define _WIN32_WINNT 0x0A00 /* Windows 10+: AF_UNIX, ConPTY, WSAPoll */
+#endif
+
+/* winsock2 before windows.h, or windows.h drags in the winsock 1.1 headers
+ * and every socket symbol collides. */
+#include <winsock2.h>
+#include <ws2tcpip.h>
+#include <afunix.h>
+#include <windows.h>
+
+/* Everything that declares a name we are about to macro-define is pulled in
+ * here first, so those declarations are read as themselves rather than being
+ * rewritten (this header is force-included ahead of the source's own
+ * includes, so there is no second chance). */
+#include <direct.h>
+#include <dirent.h>
+#include <errno.h>
+#include <fcntl.h>
+#include <io.h>
+#include <limits.h>
+#include <process.h>
+#include <unistd.h>
+#include <stdarg.h>
+#include <stdbool.h>
+#include <stdint.h>
+#include <stdio.h>
+#include <stdlib.h>
+#include <string.h>
+#include <sys/stat.h>
+#include <sys/types.h>
+#include <time.h>
+
+/* windows.h is generous with short macro names, and several are words slosh
+ * uses as enum constants. Undefining them here (rather than renaming the enums)
+ * keeps the shim's cost inside the shim. RegisterHotKey's MOD_* are the ones
+ * that actually collide; the rest are common enough to be worth pre-empting. */
+#undef MOD_SHIFT
+#undef MOD_CONTROL
+#undef MOD_ALT
+#undef MOD_WIN
+#undef DELETE
+#undef ERROR
+#undef OPTIONAL
+#undef IN
+#undef OUT
+#undef near
+#undef far
+#undef small
+
+#ifdef __cplusplus
+extern "C" {
+#endif
+
+/* ---- small gaps in the C library ---------------------------------------- */
+
+#ifndef PATH_MAX
+#define PATH_MAX 4096
+#endif
+
+#ifndef S_ISDIR
+#define S_ISDIR(m) (((m) & _S_IFMT) == _S_IFDIR)
+#endif
+#ifndef S_ISREG
+#define S_ISREG(m) (((m) & _S_IFMT) == _S_IFREG)
+#endif
+#ifndef S_ISLNK
+#define S_ISLNK(m) (0) /* no symlinks worth chasing here */
+#endif
+
+typedef unsigned long nfds_t;
+
+char *sl_strndup(const char *s, size_t n);
+char *sl_realpath(const char *path, char *resolved);
+long sl_readlink(const char *path, char *buf, size_t cap);
+int sl_setenv(const char *k, const char *v, int overwrite);
+int sl_unsetenv(const char *k);
+int sl_nanosleep_ms(long ms);
+
+/* ---- fcntl / open flags -------------------------------------------------- */
+
+#ifndef O_NONBLOCK
+#define O_NONBLOCK 0x40000000
+#endif
+#ifndef O_NOCTTY
+#define O_NOCTTY 0
+#endif
+#ifndef O_CLOEXEC
+#define O_CLOEXEC 0
+#endif
+#ifndef O_EVTONLY
+#define O_EVTONLY 0
+#endif
+#ifndef F_GETFL
+#define F_GETFL 3
+#define F_SETFL 4
+#define F_GETFD 1
+#define F_SETFD 2
+#define FD_CLOEXEC 1
+#endif
+int sl_fcntl(int fd, int cmd, ...);
+
+/* ---- signals we only ever name ------------------------------------------- */
+
+#ifndef SIGHUP
+#define SIGHUP 1
+#endif
+#ifndef SIGKILL
+#define SIGKILL 9
+#endif
+#ifndef SIGPIPE
+#define SIGPIPE 13
+#endif
+#ifndef SIGWINCH
+#define SIGWINCH 28
+#endif
+#ifndef SIGCHLD
+#define SIGCHLD 17
+#endif
+#ifndef SIG_SETMASK
+#define SIG_SETMASK 2
+#endif
+
+#ifndef WNOHANG
+#define WNOHANG 1
+#endif
+#define WIFEXITED(s) (((s) & 0x7f) == 0)
+#define WEXITSTATUS(s) (((s) >> 8) & 0xff)
+#define WIFSIGNALED(s) (((s) & 0x7f) != 0 && ((s) & 0x7f) != 0x7f)
+#define WTERMSIG(s) ((s) & 0x7f)
+
+int sl_kill(pid_t pid, int sig);
+pid_t sl_waitpid(pid_t pid, int *status, int options);
+
+/* ---- terminal size ------------------------------------------------------- */
+
+struct winsize {
+ unsigned short ws_row, ws_col, ws_xpixel, ws_ypixel;
+};
+#ifndef TIOCGWINSZ
+#define TIOCGWINSZ 0x5413
+#define TIOCSWINSZ 0x5414
+#define TIOCSCTTY 0x540E
+#endif
+int sl_ioctl(int fd, unsigned long req, ...);
+
+/* ---- the console, in place of termios ------------------------------------ */
+
+/* Raw mode is ENABLE_VIRTUAL_TERMINAL_INPUT on the input handle plus
+ * ENABLE_VIRTUAL_TERMINAL_PROCESSING on the output, which is how a modern
+ * conhost speaks the same escape sequences the POSIX client already writes. */
+bool sl_console_raw(void);
+void sl_console_restore(void);
+/* stdin as a pollable socket: a thread reads the console and forwards. */
+int sl_console_stdin_socket(void);
+/* Signals the console delivers as events rather than as signals. */
+int sl_console_resized(void); /* 1 once per observed size change */
+
+/* ---- sockets ------------------------------------------------------------- */
+
+/* Tagged so read/write/close can dispatch without guessing. */
+#define SL_SOCK_BASE 0x40000000
+
+int sl_sock_wrap(SOCKET s);
+SOCKET sl_sock_get(int fd);
+static inline int sl_is_sock(int fd) { return fd >= SL_SOCK_BASE; }
+
+/* The BSD socket calls, tagged. Without these, socket()/accept() would hand
+ * back a raw SOCKET that read/write/poll would mistake for a CRT fd -- which
+ * is exactly the kind of ambiguity the tagging exists to remove. */
+int sl_socket(int domain, int type, int proto);
+int sl_bind(int fd, const struct sockaddr *a, int len);
+int sl_listen(int fd, int backlog);
+int sl_accept(int fd, struct sockaddr *a, int *len);
+int sl_connect(int fd, const struct sockaddr *a, int len);
+long sl_send(int fd, const void *b, size_t n, int flags);
+long sl_recv(int fd, void *b, size_t n, int flags);
+/* An AF_UNIX socket file is a reparse point, so removing a stale one is
+ * DeleteFile rather than anything the CRT offers. */
+int sl_unlink(const char *path);
+
+int sl_socketpair(int fds[2]);
+/* Half-tagged pair, for the pump threads: `inner` stays a raw SOCKET owned by
+ * a pump, `outer_fd` is the tagged fd the event loop polls. */
+int sl_socketpair_pump(SOCKET *inner, int *outer_fd);
+long sl_read(int fd, void *buf, size_t n);
+long sl_write(int fd, const void *buf, size_t n);
+int sl_close(int fd);
+
+struct sl_pollfd {
+ int fd;
+ short events;
+ short revents;
+};
+int sl_poll(struct sl_pollfd *fds, nfds_t n, int timeout_ms);
+
+/* ---- dynamic loading ----------------------------------------------------- */
+
+#ifndef RTLD_NOW
+#define RTLD_NOW 0
+#define RTLD_LOCAL 0
+#define RTLD_LAZY 0
+#define RTLD_GLOBAL 0
+#endif
+void *sl_dlopen(const char *path, int flags);
+void *sl_dlsym(void *lib, const char *sym);
+int sl_dlclose(void *lib);
+const char *sl_dlerror(void);
+
+/* ---- glob ---------------------------------------------------------------- */
+
+typedef struct {
+ size_t gl_pathc;
+ char **gl_pathv;
+ size_t gl_offs;
+} glob_t;
+#ifndef GLOB_NOSORT
+#define GLOB_NOSORT 0
+#define GLOB_MARK 0
+#define GLOB_NOMATCH 3
+#define GLOB_TILDE 0
+#endif
+int sl_glob(const char *pat, int flags, int (*errfn)(const char *, int),
+ glob_t *g);
+void sl_globfree(glob_t *g);
+
+/* ---- directory watching (in place of inotify / kqueue) ------------------- */
+
+/* One watcher: a socket you can poll, fed by a ReadDirectoryChangesW thread.
+ * The payload is deliberately not inotify's, because server.c's Windows branch
+ * reads it as "something under a watched directory changed" and re-stats the
+ * files it cares about -- the same conclusion its kqueue branch reaches. */
+int sl_watch_init(void); /* -> pollable fd, or -1 */
+int sl_watch_add(int fd, const char *dir);
+void sl_watch_close(int fd);
+int sl_watch_drain(int fd); /* consume readiness; 1 if anything changed */
+
+/* ---- process helpers ----------------------------------------------------- */
+
+/* argv of a live process, for the pane title. */
+bool sl_proc_cmdline(pid_t pid, char *out, size_t cap);
+/* A pty has no foreground process group here, so "what is this pane running"
+ * is answered with the most recently started descendant of the pane's shell --
+ * which is the process tcgetpgrp() would have named on POSIX. */
+pid_t sl_proc_foreground(pid_t root);
+/* the running executable's own path, in place of /proc/self/exe. */
+bool sl_self_exe(char *out, size_t cap);
+/* spawn ourselves detached, in place of fork()+setsid() for the daemon. */
+int sl_spawn_detached(const char *const argv[], const char *logpath);
+/* `which`: is this program on PATH? Used to pick an editor that will actually
+ * draw inside a pane. */
+bool sl_which(const char *exe);
+
+/* A path as a Windows *program* expects to see it: absolute, with backslashes.
+ *
+ * slosh builds paths with '/' throughout, because that is what it splits and
+ * compares on, and mixing the two is unavoidable the moment a '/' literal is
+ * joined to an environment variable that came back with '\\' -- $HOME is
+ * `C:\Users\you`, so the config lands at `C:\Users\you/.config/slosh/...`.
+ * The CRT accepts that happily. GUI file dialogs do not: notepad answers a
+ * mixed-separator path with "Not a valid file name". Anything handed to
+ * another program goes through here first. */
+const char *sl_path_native(const char *in, char *out, size_t cap);
+
+#ifdef __cplusplus
+}
+#endif
+
+/* ---- the POSIX names, defined last --------------------------------------- */
+
+#define strndup sl_strndup
+#define realpath sl_realpath
+#define readlink sl_readlink
+#define setenv sl_setenv
+#define unsetenv sl_unsetenv
+
+#define mkdir(p, m) _mkdir(p)
+#define fcntl sl_fcntl
+#define ioctl sl_ioctl
+#define kill sl_kill
+#define waitpid sl_waitpid
+
+#define read sl_read
+#define write sl_write
+#define close sl_close
+#define poll sl_poll
+#define pollfd sl_pollfd
+#define socketpair(d, t, p, sv) sl_socketpair(sv)
+#define unlink sl_unlink
+/* Permissions on the session socket are the directory's job here: LOCALAPPDATA
+ * is already per-user, and Windows has no mode bits to set on it. */
+#define chmod(p, m) (0)
+
+/* The shim's own translation units call the real socket API, so they are
+ * compiled with SL_COMPAT_IMPL and see through these. */
+#ifndef SL_COMPAT_IMPL
+#define socket sl_socket
+#define bind sl_bind
+#define listen sl_listen
+#define accept sl_accept
+#define connect sl_connect
+#define send sl_send
+#define recv sl_recv
+#endif
+
+#define dlopen sl_dlopen
+#define dlsym sl_dlsym
+#define dlclose sl_dlclose
+#define dlerror sl_dlerror
+
+#define glob sl_glob
+#define globfree sl_globfree
+
+/* ssize_t is what the callers assign our long returns to. */
+#ifndef _SSIZE_T_DEFINED
+#define _SSIZE_T_DEFINED
+typedef long long ssize_t;
+#endif
+
+#endif /* _WIN32 */
+#endif /* SLOSH_COMPAT_WIN_H */
diff --git a/include/slosh.h b/include/slosh.h
index c0dbeba..3e293b3 100644
--- a/include/slosh.h
+++ b/include/slosh.h
@@ -123,6 +123,20 @@ const char *path_relative(const char *path, const char *base);
/* mkdir -p, for the directory a config or a session socket wants to live in. */
bool path_mkdirs(const char *dir);
+/* ---- the shell a pane gets when nothing else is said -------------------- */
+
+/* One spelling of "run this command line through a shell", because three
+ * places need it and a Windows box has neither /bin/sh nor -c. SHELL is not
+ * set on Windows either, so ComSpec is what stands in for it (see
+ * default_argv). */
+#ifdef _WIN32
+#define SLOSH_SHELL_DEFAULT "cmd.exe"
+#define SLOSH_SHELL_CFLAG "/c"
+#else
+#define SLOSH_SHELL_DEFAULT "/bin/sh"
+#define SLOSH_SHELL_CFLAG "-c"
+#endif
+
/* ---- pty --------------------------------------------------------------- */
typedef struct {