diff --git a/APKBUILD b/APKBUILD index 8c1921f..37585a7 100644 --- a/APKBUILD +++ b/APKBUILD @@ -25,9 +25,9 @@ package() { } sha512sums=" -f3490711f2fdebe4c9200654b63a90b69ffd4dad0e9cf2fd5040a4cd57371ebb3c012a855aff9d8d2aa0a2c500baf2143b581a980177685f863ca3e4cc763851 moerx.c +efc748dd85e0fda80baaf3b047f8b5a8d57c6231973540be2da9383a8aad8ae4c08a9a1665e219bcd4b4e045d2a88d271d2cde934b1023cf165096c188ea8da4 moerx.c e6336e712a9a6071b16cbde83cd6f591c480da68cf5185f0db27f321b26adb075844a958e792da419dcb82a930de6cff28bc64a98e9b0facd9350e382aa95ce1 Makefile -56e641534bc68b48a70d7be8337df8a191c5a2a1bafee12dc6d9ac401d107cb6b8cf8138bcc6d4f74d373689e1bb8f2ac298a87043a95f0383d6163dfc363116 moerx.1 -63f842b7ee65c7bfd3feceeb27a0199f44f3e741a399ee6f59e5c78cfdde951cce88b6f368abd451d4c2efd76b6cda97be15450743031d590fc22020268fd6af README.md +06aa39542f0842cfddf7c14dda7fcff739c58a6c21ee899be77f9c18fcd9575fc6061d6846318153a2703747cbce814a9f87e63b6e905f529a42af91ddb98470 moerx.1 +feaee93a326f0b62b1b655ef8b776c470a49feaaab41d6814ca9dd1d6f3416b70f65deda08e9362dc0e28f347123fa84a84e2f2c727566fb702003fed2caa68c README.md 2fe33ba22c5f68e124904be594208f050be20f33d115f9a56f60cac93304102da32e6a7e78154e831d3704da7ca8bfefd7025e7b8901e65babcd83e7342f6382 LICENSE " diff --git a/PKGBUILD b/PKGBUILD index efc0168..d1f7fa4 100644 --- a/PKGBUILD +++ b/PKGBUILD @@ -9,10 +9,10 @@ license=('Apache-2.0') depends=('glibc') source=('moerx.c' 'Makefile' 'moerx.1' 'README.md' 'LICENSE') sha256sums=( - 'abc3273b9f9b5b2f29aa72ac73ff925631457786e1200ae1b7c3f9319e1833d0' + '4418d829418d51903609b03b334cdd5e4b8e2b667d180980890ee3efae1c9f12' 'ad08bd0acd671f1f1716250528c2d4e5acc78a83bf55fcebdf41fa101fbb71d5' - '56ef9511287758cd32e3c5cf777d7b9d99c470e1cf6570f1a94dcfd2f649b3e6' - '4a4d3158fb5288b93bcfd0ceec76b6b4d83fdb683e2a2a05afa63a0804ba9311' + '882d5876e386ea0539c49f1f45759f454460d36c2ede43f5ec7c0401509fc5f0' + '000a031b54dff60d34c621c0318138b01524ba63b7d166778ccb00c2652e8d1d' '31c5556be4cdd9bf52bb55f18ead0c68a4f0e793c2c2dab0a2a2c6d92407d0d4' ) diff --git a/README.md b/README.md index 34d65ff..b3a993d 100644 --- a/README.md +++ b/README.md @@ -5,21 +5,29 @@ dependencies. ```sh make -B CC=clang -./moerx # session "0" -./moerx work # separate named session -./moerx -p x work # use Ctrl-X as this client's prefix -./moerx --prefix x work # same as -p x -./moerx -s bottom work # show the session status line at the bottom -./moerx --status bottom work # same as -s bottom -./moerx --no-cwd work # show [moerx] instead of the directory -./moerx --no-cwd --no-moerx work # do not show [moerx] instead of the directory -./moerx --no-clock work # hide the status bar clock -./moerx --no-hostname work # hide the hostname: prefix in window titles -./moerx --help # all shortcuts +./moerx # session "0" +./moerx work # separate named session +./moerx -p x work # use Ctrl-X as this client's prefix +./moerx --prefix x work # same as -p x +./moerx -s bottom work # show the session status line at the bottom +./moerx --status bottom work # same as -s bottom +./moerx --no-cwd work # show [moerx] instead of the directory +./moerx --no-cwd --no-moerx work # do not show [moerx] instead of the directory +./moerx --no-clock work # hide the status bar clock +./moerx --no-hostname work # hide the hostname: prefix in window titles +./moerx -l, --list-sessions # list running sessions without attaching +./moerx --help # all shortcuts ``` Detach with `Ctrl-A d`. Run `moerx` again to reattach. +Use `moerx -l` or `moerx --list-sessions` to list running sessions, sorted by +name, one per line. Listing works without a terminal and does not create or +attach to a session. It checks the current user's runtime socket directory +and `/tmp` fallback, skips stale sockets, and prints nothing when no sessions +are running. Do not pass a session name with this option. Use `moerx name` +to attach to a listed session. + The [moerx(1) manual](moerx.1) covers all options, shortcuts, session behavior, and remote shell setup. Read it locally with `man ./moerx.1`. Run `make install` to install both the program and manual, then use `man moerx`. diff --git a/moerx.1 b/moerx.1 index 37d6b3a..207bf2a 100644 --- a/moerx.1 +++ b/moerx.1 @@ -14,6 +14,9 @@ moerx \- small persistent terminal multiplexer .br .B moerx .BR \-h " | " \-\-help +.br +.B moerx +.BR \-l " | " \-\-list\-sessions .SH DESCRIPTION .B moerx runs shells and terminal programs in windows and split panes. @@ -34,6 +37,13 @@ hyphens or underscores. Use before a name beginning with a hyphen. .SH OPTIONS .TP +.BR \-l ", " \-\-list\-sessions +List running sessions for the current user, sorted by name, one per line. +Checks the runtime socket directory and /tmp fallback, skips stale sockets +and suppresses duplicate names. No terminal is required and no session is +created or attached. Prints nothing if no sessions are running. +Do not supply a session name with this option. +.TP .BR \-p " letter, " \-\-prefix " letter" Choose this client's control prefix. Letters a through z are accepted, case-insensitively; a means Ctrl-A and x means Ctrl-X. @@ -407,10 +417,12 @@ or use the confirmed kill-pane, kill-window or kill-server bindings. .SH EXIT STATUS .TP .B 0 -Normal client exit, including detaching, or successful help output. +Normal client exit, including detaching, successful help output or session +listing (including an empty list). .TP .B 1 -Unable to start or attach to a session, or client terminal setup failed. +Unable to start or attach to a session, client terminal setup failed, or +a session directory could not be read while listing. .TP .B 2 Invalid command-line options or session name. diff --git a/moerx.c b/moerx.c index 92d3255..ea3b687 100644 --- a/moerx.c +++ b/moerx.c @@ -40,6 +40,7 @@ #include #include +#include #include #include #include @@ -3657,6 +3658,81 @@ static int connect_session(const char *path) { return fd; } +static int session_name_compare(const void *a, const void *b) { + return strcmp((const char *)a, (const char *)b); +} + +static int list_sessions(void) { + char dirs[2][PATH_MAX]; + socket_dir(dirs[0], sizeof dirs[0]); + snprintf(dirs[1], sizeof dirs[1], "/tmp/moerx-%u", (unsigned)getuid()); + char (*names)[49] = NULL; + size_t count = 0; + int result = 0; + for (int i = 0; i < 2; i++) { + if (i == 1 && strcmp(dirs[0], dirs[1]) == 0) continue; + DIR *dir = opendir(dirs[i]); + if (!dir) { + if (errno != ENOENT) { + fprintf(stderr, "moerx: cannot list %s: %s\n", dirs[i], strerror(errno)); + result = 1; + } + continue; + } + struct dirent *entry; + for (;;) { + errno = 0; + entry = readdir(dir); + if (!entry) { + if (errno) { + fprintf(stderr, "moerx: cannot read %s: %s\n", dirs[i], strerror(errno)); + result = 1; + } + break; + } + size_t len = strlen(entry->d_name); + if (len <= 5 || len > 53 || strcmp(entry->d_name + len - 5, ".sock") != 0) + continue; + char name[49]; + memcpy(name, entry->d_name, len - 5); + name[len - 5] = '\0'; + if (!valid_session_name(name)) continue; + struct sockaddr_un sa; + memset(&sa, 0, sizeof sa); + sa.sun_family = AF_UNIX; + size_t dirlen = strlen(dirs[i]); + if (dirlen + len + 2 > sizeof sa.sun_path) continue; + memcpy(sa.sun_path, dirs[i], dirlen); + sa.sun_path[dirlen] = '/'; + memcpy(sa.sun_path + dirlen + 1, entry->d_name, len + 1); + struct stat st; + if (lstat(sa.sun_path, &st) < 0 || !S_ISSOCK(st.st_mode) || st.st_uid != getuid()) + continue; + int fd = socket(AF_UNIX, SOCK_STREAM, 0); + if (fd < 0) continue; + int flags = fcntl(fd, F_GETFL, 0); + if (flags < 0 || fcntl(fd, F_SETFL, flags | O_NONBLOCK) < 0) { + close(fd); + continue; + } + int connected = connect(fd, (struct sockaddr *)&sa, sizeof sa); + int live = connected == 0 || errno == EINPROGRESS || errno == EAGAIN; + close(fd); + if (!live) continue; + names = xrealloc(names, (count + 1) * sizeof *names); + memcpy(names[count++], name, len - 4); + } + closedir(dir); + } + if (count > 1) qsort(names, count, sizeof *names, session_name_compare); + for (size_t i = 0; i < count; i++) { + if (!i || strcmp(names[i], names[i - 1]) != 0) puts(names[i]); + } + free(names); + if (ferror(stdout)) result = 1; + return result; +} + static int ensure_socket_dir(void) { char dir[PATH_MAX]; socket_dir(dir, sizeof dir); @@ -3976,11 +4052,14 @@ static int run_client(int fd, const char *name, unsigned char prefix, int status static void print_help(FILE *out) { fprintf(out, "usage: moerx [-p letter] [-s top|bottom] [--no-cwd] [--no-clock] [--no-moerx] [--no-hostname] [session]\n" + " moerx -l | --list-sessions\n" " moerx -h | --help\n" "\n" "Attach to a running session, or start it if it is not running.\n" "The session name defaults to \"0\".\n" "\n" + "Use -l or --list-sessions to list running sessions without attaching.\n" + "\n" "Use -s top|bottom (or --status top|bottom) to position the status line.\n" "New sessions default to top; an explicit choice updates all attached clients.\n" "Without -s, keep the session position.\n" @@ -4066,6 +4145,7 @@ int main(int argc, char **argv) { int show_clock = 1; int show_moerx = 1; int show_hostname = 1; + int list = 0; while (argi < argc && argv[argi][0] == '-' && argv[argi][1]) { const char *option = argv[argi++]; if (strcmp(option, "--") == 0) break; @@ -4073,7 +4153,9 @@ int main(int argc, char **argv) { print_help(stdout); return 0; } - if (strcmp(option, "--no-cwd") == 0) { + if (strcmp(option, "-l") == 0 || strcmp(option, "--list-sessions") == 0) { + list = 1; + } else if (strcmp(option, "--no-cwd") == 0) { show_cwd = 0; } else if (strcmp(option, "--no-clock") == 0) { show_clock = 0; @@ -4101,6 +4183,13 @@ int main(int argc, char **argv) { } } + if (list) { + if (argi != argc) { + fprintf(stderr, "moerx: --list-sessions does not take a session name\n"); + return 2; + } + return list_sessions(); + } if (argc - argi > 1) { fprintf(stderr, "moerx: too many arguments\n"); fprintf(stderr, "Try 'moerx --help' for more information.\n");