diff --git a/AGENTS.md b/AGENTS.md new file mode 100644 index 0000000..7e44d2a --- /dev/null +++ b/AGENTS.md @@ -0,0 +1,68 @@ +# murx project instructions + +murx is a deliberately small terminal multiplexer: one C99 source file +(`murx.c`), no dependencies, no config file. + +## Build + +```sh +make # cc -O2 -std=c99 -Wall -Wextra -o murx murx.c +make clean +``` + +There is no test suite. Build must stay warning-free except for the known +pre-existing `g_spawn_err` unused-variable warning. Use a throwaway harness +outside the repo for geometry/logic checks; do not leave test files behind. + +## Conventions + +- Keep changes inside `murx.c` where possible; the single-file design is + intentional. +- `README.md` is user-facing documentation. Update it when shortcuts, behavior, + or limitations change. +- Match the existing style: 4-space indentation, C99 declarations, `/* */` + comments. +- Do not remove existing user changes in the working tree; check `git diff` + before editing and keep unrelated edits intact. + +## Layout model (important for navigation/resizing) + +- Windows use a binary `Layout` tree: `L_COLS` (left/right split) or + `L_ROWS` (top/bottom split), with `Layout` x/y/w/h plus a `Pane` leaf. +- `layout_apply()` assigns pane content rectangles. With `--pane-titles`, + each leaf reserves its top row for a title and sets + `pane->y = layout->y + 1`. By default, content fills the leaf. +- Splits reserve one border column/row between children. With titles shown, + horizontal splits use the lower pane's title row as the separator; with titles + hidden, horizontal splits draw a border row like vertical splits. +- Pane movement must be based on rectangle edges, not centres. A pane is only + to the right if `p->x >= a->x + a->w` (and similar for the other directions). + Centre comparison wrongly classifies tall/wide panes that merely extend past + the active pane. + +## Current work (2026-10-30) + +Reworked `select_pane_dir()` in `murx.c` so `Ctrl-A h/j/k/l` and the arrow +keys choose the pane exactly left/down/up/right: + +1. Only panes strictly beyond the active pane's edge in the requested direction + are candidates. +2. Prefer candidates that overlap on the perpendicular axis. +3. Then choose the smallest edge distance in the direction of travel. +4. Break ties by perpendicular gap, then centre alignment. + +Verified with a temporary randomized split-layout simulation: the old +centre-based algorithm chose a pane outside the requested direction in many +cases; the new one never did and always preferred an overlapping candidate when +one existed. `make` succeeds with only the pre-existing warning above. + +### Terminal resize robustness + +The client now reconciles its size with `TIOCGWINSZ` every `SIZE_CHECK_MS` +(250 ms) instead of only reacting to `SIGWINCH`. Some terminals update the pty +size without delivering a signal, or deliver it before the new size is +visible; the periodic check corrects both. Keep the SIGWINCH self-pipe and flag +as the low-latency path. The session size is set by the most recently attached +or resized client, so reattaching from a larger terminal grows the session even +if an older client is still connected; smaller clients show the top-left part. +Use `Ctrl-A *` to list attached clients when diagnosing size reports. diff --git a/README.md b/README.md index 12ee0ec..c81181f 100644 --- a/README.md +++ b/README.md @@ -8,25 +8,22 @@ dependencies. ```sh make ./murx # session "0" -./murx work # separate/attache named session -./murx 1 # separate/attache numbered session +./murx work # separate named session +./murx 1 # separate numbered session ./murx -p x work # use Ctrl-X as this client's prefix ./murx --prefix x work # same as -p x ./murx -s bottom work # show the session status line at the bottom ./murx --status bottom work # same as -s bottom -./murx -d work # --cwd, show the current directory in the status bar -./murx -m work # --murx, show [murx] on the left of the status bar -./murx -c 191a1c work # --color, use a grey status bar background -./murx -H 10000 work # --history, keep up to 10,000 history lines per pane -./murx -C work # --clock, show the status bar clock -./murx -n work # --hostname, show the hostname: prefix in window titles -./murx -t work # --titles, show a title row above each pane -./murx -l # --list-sessions, list running sessions without attaching -./murx -h # --help, usage and shortcuts -./murx -v # --version, print the version and exit -./murx --resize-on-input work # session size follows the last client with input -./murx --adopt # hand the default session to a new server binary -./murx --adopt work # same for a named session +./murx --cwd work # show the current directory in the status bar +./murx --murx work # show [murx] on the left of the status bar +./murx -c 191a1c work # use a grey status bar background +./murx --history 10000 work # keep up to 10,000 history lines per pane +./murx --clock work # show the status bar clock +./murx --hostname work # show the hostname: prefix in window titles +./murx --pane-titles work # show a title row above each pane +./murx -l, --list-sessions # list running sessions without attaching +./murx --help # version, usage and all shortcuts +./murx -v, --version # print the version and exit ``` Detach with `Ctrl-A d`. Run `murx` again to reattach. @@ -74,41 +71,19 @@ and `--clock` to show the clock on the right. Use `--murx` to show `[murx]` on the left when `--cwd` is not enabled; the directory takes precedence if both are supplied. Use `--hostname` to show the `hostname:` prefix in window names and the outer terminal title. These display options are disabled by -default. Use `--titles` to show the title row above each pane. These +default. Use `--pane-titles` to show the title row above each pane. These options set the initial values for a new session. The `Configuration...` menu can change them in a running session (see [Menu](#menu)). To restore the previous default display, use `murx --cwd --clock --hostname session`. -By default, the most recently attached or resized client sets the session size. -Use `--resize-on-input` (or the `Resize to last input` toggle in the -Configuration menu) to make any input from a client — a keystroke, menu -navigation, or a scrollback command — make that client the size source instead. -The menu toggle turns it off again. Both change the running session for all -attached clients; omitting the option preserves its current mode. - -`--adopt` asks a running server to hand its session to a freshly started server -process, so an upgraded binary can take over without killing panes or -disconnecting clients. It accepts a session name, or no argument at all for the -default session: `murx --adopt` and `murx --adopt work` both work. Attached -clients keep their connections and see a short `Session adopted by new murx -server` notification. If no session is running, `--adopt` simply starts one. - -Handoff requires cooperation from the server that is already running. Sessions -started by this version (or a later one) can be adopted after an upgrade. -Against an older server, `--adopt` prints a clear message and attaches normally; -those sessions still need one restart. The handoff carries the listening socket, -pty descriptors, client sockets, window layout, pane screens and scrollback, so -shells keep their process IDs and panes keep their contents. - ## Upgrading from moerx The executable, source and manual are now `murx`, `murx.c` and `murx.1`. Update aliases and scripts to use `murx`. The status label is now disabled by default; use `--murx` to enable it. Sessions use a separate `murx-` socket directory; finish -existing sessions with the old executable before removing it. Sessions started -by a handoff-capable build can be moved forward with `--adopt`; older ones are -not migrated automatically. Replace the optional remote SSH +existing sessions with the old executable before removing it. Existing +sessions are not migrated automatically. Replace the optional remote SSH hooks below and reload the remote shell: custom reports now use `murx-cwd` and `murx-root`. @@ -125,12 +100,10 @@ and `murx-root`. works in programs such as Midnight Commander. Menus and prompts use normal cursor-key mode; detaching restores normal cursor keys in the outer terminal. - Home (Pos1) and End use the pane’s `screen-256color` key sequences. -- Multiple clients can attach to the same session. Normally the most recently - attached or resized client sets the session size. With `--resize-on-input`, - the client that most recently sent any input does instead. A larger client - shows the session centred in its terminal, while a smaller client shows the - top-left part. `Ctrl-A R` forces the session back to the current client's - terminal size. +- Multiple clients can attach to the same session. The most recently attached + or resized client sets the session size; a larger client shows the session + centred in its terminal, while a smaller client shows the top-left part. + `Ctrl-A R` forces the session back to the current client's terminal size. - Panes run `$SHELL` as a login shell with `TERM=screen-256color` and `MURX=`. They inherit the launching process's umask, so builds keep their usual file permissions. After upgrading from a version that forced `077`, @@ -351,8 +324,8 @@ scrollback, forced resize and kill server. `Configuration...` contains: - `Status line...` — position (top/bottom) and background (default, black `000000`, grey `191a1c`). - `Prefix...` — this client's Ctrl-key prefix, `a` through `z`. -- Toggles for the current directory, clock, `[murx]` label, hostname, pane - titles and `Resize to last input`. +- Toggles for the current directory, clock, `[murx]` label, hostname and pane + titles. - `Reset display/status defaults`. Press Enter on a toggle to turn it on or off. Settings changes apply @@ -415,7 +388,7 @@ Hard-coded: A root shell that does not run the hooks cannot report its privilege change. - Pane borders are thin box-drawing lines (`─`, `│`, `┬`, `┼`, ...) in `colour8`, drawn on the terminal's default background. Enable title rows - with `--titles` or the Configuration menu. Each title row shows + with `--pane-titles` or the Configuration menu. Each title row shows `[N: title]`. On the active pane, brackets and numbers use `colour7`, the separator uses `colour11`, and the title uses `colour15`. Inactive panes use `colour8`, `colour3`, and `colour12`. diff --git a/RELEASES.md b/RELEASES.md index f10a44d..7eb06ff 100644 --- a/RELEASES.md +++ b/RELEASES.md @@ -1,20 +1,3 @@ -# murx 1.0.4 released - -**November 24, 2026** - -- Added short options for `--history` (`-H`), `--cwd` (`-d`), - `--clock` (`-C`), `--murx` (`-m`), `--hostname` (`-n`), and - `--titles` (`-t`). -- Renamed `--pane-titles` to `--titles`. -- Use `murx --adopt work` after upgrading to move a running session to the new - server while keeping panes, scrollback, and client connections. The running - server must support handoff. Sessions started by older versions need a - one-time restart. -- Use `--resize-on-input` or the Configuration menu's **Resize to last input** - toggle to make the session size follow the client that last sent input. -- Fixed the build with Arch's compiler flags by reading handoff timestamps - into matching 64-bit types. - # murx 1.0.3 released **November 23, 2026** diff --git a/murx.1 b/murx.1 index bf0ea61..97d6fa2 100644 --- a/murx.1 +++ b/murx.1 @@ -6,14 +6,12 @@ murx \- small persistent terminal multiplexer .RB [ \-p " letter"] .RB [ \-s " top|bottom"] .RB [ \-c " RRGGBB"] -.RB [ \-H " lines"] -.RB [ \-d ] -.RB [ \-C ] -.RB [ \-m ] -.RB [ \-n ] -.RB [ \-t ] -.RB [ \-\-resize\-on\-input ] -.RB [ \-\-adopt ] +.RB [ \-\-history " lines"] +.RB [ \-\-cwd ] +.RB [ \-\-clock ] +.RB [ \-\-murx ] +.RB [ \-\-hostname ] +.RB [ \-\-pane-titles ] .RB [ \-\- ] .RI [ session ] .br @@ -48,7 +46,7 @@ hyphens or underscores. Use before a name beginning with a hyphen. .SH OPTIONS .TP -.BR \-H ", " \-\-history " lines" +.BR \-\-history " lines" Set the maximum number of normal-screen history lines retained per pane in a new session. Accepts 0 to 100,000, with 0 disabling history retention. Reattaching preserves the existing session's limit. @@ -80,55 +78,32 @@ Supply six hexadecimal digits, such as 000000 for black. Omitting this option preserves the session's color when reattaching. New sessions use the terminal's default background unless a color is supplied. .TP -.BR \-d ", " \-\-cwd +.B \-\-cwd Show the current directory at the left of the status line. .TP -.BR \-C ", " \-\-clock +.B \-\-clock Show the status clock. .TP -.BR \-m ", " \-\-murx +.B \-\-murx Show the [murx] label at the left of the status line when .B \-\-cwd is not enabled. The directory takes precedence if both options are supplied. .TP -.BR \-n ", " \-\-hostname +.B \-\-hostname Show the hostname: prefix in window names and the outer terminal title. .TP -.BR \-t ", " \-\-titles +.B \-\-pane-titles Show the title row above each pane. Pane titles are off by default. .TP -.B \-\-resize\-on\-input -Make the session size follow the client that most recently sent input. -Any bytes from a client count, including keystrokes, menu navigation and -scrollback commands. Attaching and resizing are used only until the first -input arrives. This changes the running session for all attached clients. -.TP -.B \-\-adopt -Ask a running server to hand its session to a newly started server process. -The session name is optional, so both -.B murx \-\-adopt -and -.B murx \-\-adopt session -work; without a name the default session 0 is used. Panes, client connections, -window layout, screens and scrollback survive the swap. Attached clients see a -short -.I Session adopted by new murx server -notification. If no session is running, a new one is started normally. -Handoff requires a cooperating server: sessions started by this version or a -later one can be adopted after an upgrade. Against an older server, murx -prints a clear message and attaches normally. -.TP .BR \-h ", " \-\-help -Print the text logo, program version, usage and keyboard shortcuts, then exit. +Print the program version, usage and keyboard shortcuts, then exit. .TP .BR \-v ", " \-\-version Print the program version and exit. .PP Display options set a new session's values; the Configuration menu can change them in a running session. The cwd, clock, [murx] label, hostname -and pane title options are off by default. The default size mode uses -attach/resize recency; omitting \-\-resize\-on\-input preserves the running -session's mode. +and pane title options are off by default. To restore the previous default display, use .BR "murx \-\-cwd \-\-clock \-\-hostname session" . .SH KEY BINDINGS @@ -252,11 +227,10 @@ Use Up/Down or j/k to move and Enter to select. Left/Right or h/l enter and leave submenus; q, Escape, or Ctrl-C closes the menu. Long menus scroll. The Configuration submenu sets the status line position and background (default, black or grey), the client prefix, display toggles for the directory, -clock, [murx] label, hostname and pane titles, a toggle for resizing to the -last client with input, and resets display/status defaults (display toggles, -status position and background). Session settings affect all attached clients; -the prefix is per-client. Changes take effect immediately and are not saved to -disk. +clock, [murx] label and hostname, and resets display/status defaults (display toggles, status position and +background). Session +settings affect all attached clients; the prefix is per-client. Changes take +effect immediately and are not saved to disk. In prompts use Enter to accept, Escape or Ctrl-C to cancel, Backspace or Ctrl-H to delete, and Ctrl-U to clear. Confirm destructive actions with y, Y or Enter; n, N, Escape or Ctrl-C cancels. @@ -304,7 +278,7 @@ The outer terminal title follows [murx] program, or [murx] hostname:program with --hostname. Its [murx] prefix is always present; --murx controls only the status label. Hostnames are shortened at the first dot. -With \-\-titles enabled, each pane has a title row above its content showing +With \-\-pane-titles enabled, each pane has a title row above its content showing [R N: title R], where N is the pane number. Active pane titles use colour7 brackets/numbers, colour11 separator and colour15 title; inactive panes use colour8, colour3 and colour12. Toggle title rows in the Configuration menu. @@ -327,11 +301,9 @@ are passed through as 24-bit SGR when COLORTERM is truecolor or 24bit and TERM does not start with screen, tmux, linux, or dumb; otherwise RGB is mapped to the nearest xterm-256 entry. .SH SESSION SIZE -By default, the most recently attached or resized client determines the session -size, clamped to a minimum of 20 columns by 5 rows and a maximum of 1,000 by -1,000. With the \-\-resize\-on\-input option or the Configuration menu toggle, -the client that most recently sent input determines the size instead; -attaching and resizing are only used before any client has sent input. +The most recently attached or resized client determines the session size, +clamped to a minimum of 20 columns by 5 rows and a maximum of 1,000 by 1,000. +Larger clients center the session; smaller clients show its top-left portion. Ctrl-A R forces the session to the current client's terminal size. Ctrl-A * lists attached clients for size diagnostics. .PP @@ -345,9 +317,7 @@ assume --hostname is enabled. SSH does not expose remote foreground programs or working directories locally. Configure the remote shell to send terminal reports using one of the examples below. Reload the shell configuration or open a new SSH shell afterward. -Sessions started by a handoff-capable build can move to a new binary with -.BR "murx \-\-adopt [" session "]"; -older session servers still need a restart after upgrading. +Restart session servers after upgrading murx to gain newly added features. .PP OSC 0 or OSC 2 titles of the form hostname:program update the SSH window name. Use a hostname containing only letters, digits, dots, underscores or hyphens, diff --git a/murx.c b/murx.c index ff9a12b..bb701db 100644 --- a/murx.c +++ b/murx.c @@ -56,7 +56,6 @@ #include #include #include -#include #include #include #include @@ -88,16 +87,13 @@ #define MURX_NOIPA #endif -#define MURX_VERSION "1.0.4" +#define MURX_VERSION "1.0.3" #define DEFAULT_PREFIX 0x01 /* client -> server */ -enum { C_ATTACH = 1, C_INPUT = 2, C_RESIZE = 3, C_ADOPT = 6 }; +enum { C_ATTACH = 1, C_INPUT = 2, C_RESIZE = 3 }; /* server -> client */ -enum { S_DRAW = 4, S_DETACH = 5, S_HANDOFF = 7 }; - -/* C_ATTACH session-size mode: 0 preserves the running session's setting. */ -enum { SIZE_MODE_KEEP = 0, SIZE_MODE_ON = 1 }; +enum { S_DRAW = 4, S_DETACH = 5 }; typedef struct { uint32_t type; @@ -106,263 +102,16 @@ typedef struct { static int send_msg(int fd, uint32_t type, const void *data, size_t len); -/* xterm-256 palette indices. The terminal defines the ANSI colours. */ -#define C_BLACK 0 -#define C_MAROON 1 -#define C_DGREEN 2 -#define C_OLIVE 3 -#define C_NAVY 4 -#define C_PURPLE 5 -#define C_CYAN 6 -#define C_WHITE 7 -#define C_GREY 8 -#define C_RED 9 -#define C_GREEN 10 -#define C_YELLOW 11 -#define C_BLUE 12 -#define C_MAGENTA 13 -#define C_CYAN2 14 -#define C_BWHITE 15 -#define C_GREY_0 16 -#define C_NAVY_BLUE 17 -#define C_DARK_BLUE 18 -#define C_BLUE_3_19 19 -#define C_BLUE_3_20 20 -#define C_BLUE_1 21 -#define C_DARK_GREEN 22 -#define C_DEEP_SKY_BLUE_4_23 23 -#define C_DEEP_SKY_BLUE_4_24 24 -#define C_DEEP_SKY_BLUE_4_25 25 -#define C_DODGER_BLUE_3 26 -#define C_DODGER_BLUE_2 27 -#define C_GREEN_4 28 -#define C_SPRING_GREEN_4 29 -#define C_TURQUOISE_4 30 -#define C_DEEP_SKY_BLUE_3_31 31 -#define C_DEEP_SKY_BLUE_3_32 32 -#define C_DODGER_BLUE_1 33 -#define C_GREEN_3_34 34 -#define C_SPRING_GREEN_3_35 35 -#define C_DARK_CYAN 36 -#define C_LIGHT_SEA_GREEN 37 -#define C_DEEP_SKY_BLUE_2 38 -#define C_DEEP_SKY_BLUE_1 39 -#define C_GREEN_3_40 40 -#define C_SPRING_GREEN_3_41 41 -#define C_SPRING_GREEN_2_42 42 -#define C_CYAN_3 43 -#define C_DARK_TURQUOISE 44 -#define C_TURQUOISE_2 45 -#define C_GREEN_1 46 -#define C_SPRING_GREEN_2_47 47 -#define C_SPRING_GREEN_1 48 -#define C_MEDIUM_SPRING_GREEN 49 -#define C_CYAN_2 50 -#define C_CYAN_1 51 -#define C_DARK_RED_52 52 -#define C_DEEP_PINK_4_53 53 -#define C_PURPLE_4_54 54 -#define C_PURPLE_4_55 55 -#define C_PURPLE_3 56 -#define C_BLUE_VIOLET 57 -#define C_ORANGE_4_58 58 -#define C_GREY_37 59 -#define C_MEDIUM_PURPLE_4 60 -#define C_SLATE_BLUE_3_61 61 -#define C_SLATE_BLUE_3_62 62 -#define C_ROYAL_BLUE_1 63 -#define C_CHARTREUSE_4 64 -#define C_DARK_SEA_GREEN_4_65 65 -#define C_PALE_TURQUOISE_4 66 -#define C_STEEL_BLUE 67 -#define C_STEEL_BLUE_3 68 -#define C_CORNFLOWER_BLUE 69 -#define C_CHARTREUSE_3_70 70 -#define C_DARK_SEA_GREEN_4_71 71 -#define C_CADET_BLUE_72 72 -#define C_CADET_BLUE_73 73 -#define C_SKY_BLUE_3 74 -#define C_STEEL_BLUE_1_75 75 -#define C_CHARTREUSE_3_76 76 -#define C_PALE_GREEN_3_77 77 -#define C_SEA_GREEN_3 78 -#define C_AQUAMARINE_3 79 -#define C_MEDIUM_TURQUOISE 80 -#define C_STEEL_BLUE_1_81 81 -#define C_CHARTREUSE_2_82 82 -#define C_SEA_GREEN_2 83 -#define C_SEA_GREEN_1_84 84 -#define C_SEA_GREEN_1_85 85 -#define C_AQUAMARINE_1_86 86 -#define C_DARK_SLATE_GRAY_2 87 -#define C_DARK_RED_88 88 -#define C_DEEP_PINK_4_89 89 -#define C_DARK_MAGENTA_90 90 -#define C_DARK_MAGENTA_91 91 -#define C_DARK_VIOLET_92 92 -#define C_PURPLE_93 93 -#define C_ORANGE_4_94 94 -#define C_LIGHT_PINK_4 95 -#define C_PLUM_4 96 -#define C_MEDIUM_PURPLE_3_97 97 -#define C_MEDIUM_PURPLE_3_98 98 -#define C_SLATE_BLUE_1 99 -#define C_YELLOW_4_100 100 -#define C_WHEAT_4 101 -#define C_GREY_53 102 -#define C_LIGHT_SLATE_GREY 103 -#define C_MEDIUM_PURPLE 104 -#define C_LIGHT_SLATE_BLUE 105 -#define C_YELLOW_4_106 106 -#define C_DARK_OLIVE_GREEN_3_107 107 -#define C_DARK_SEA_GREEN 108 -#define C_LIGHT_SKY_BLUE_3_109 109 -#define C_LIGHT_SKY_BLUE_3_110 110 -#define C_SKY_BLUE_2 111 -#define C_CHARTREUSE_2_112 112 -#define C_DARK_OLIVE_GREEN_3_113 113 -#define C_PALE_GREEN_3_114 114 -#define C_DARK_SEA_GREEN_3_115 115 -#define C_DARK_SLATE_GRAY_3 116 -#define C_SKY_BLUE_1 117 -#define C_CHARTREUSE_1 118 -#define C_LIGHT_GREEN_119 119 -#define C_LIGHT_GREEN_120 120 -#define C_PALE_GREEN_1_121 121 -#define C_AQUAMARINE_1_122 122 -#define C_DARK_SLATE_GRAY_1 123 -#define C_RED_3_124 124 -#define C_DEEP_PINK_4_125 125 -#define C_MEDIUM_VIOLET_RED 126 -#define C_MAGENTA_3_127 127 -#define C_DARK_VIOLET_128 128 -#define C_PURPLE_129 129 -#define C_DARK_ORANGE_3_130 130 -#define C_INDIAN_RED_131 131 -#define C_HOT_PINK_3_132 132 -#define C_MEDIUM_ORCHID_3 133 -#define C_MEDIUM_ORCHID 134 -#define C_MEDIUM_PURPLE_2_135 135 -#define C_DARK_GOLDENROD 136 -#define C_LIGHT_SALMON_3_137 137 -#define C_ROSY_BROWN 138 -#define C_GREY_63 139 -#define C_MEDIUM_PURPLE_2_140 140 -#define C_MEDIUM_PURPLE_1 141 -#define C_GOLD_3_142 142 -#define C_DARK_KHAKI 143 -#define C_NAVAJO_WHITE_3 144 -#define C_GREY_69 145 -#define C_LIGHT_STEEL_BLUE_3 146 -#define C_LIGHT_STEEL_BLUE 147 -#define C_YELLOW_3_148 148 -#define C_DARK_OLIVE_GREEN_3_149 149 -#define C_DARK_SEA_GREEN_3_150 150 -#define C_DARK_SEA_GREEN_2_151 151 -#define C_LIGHT_CYAN_3 152 -#define C_LIGHT_SKY_BLUE_1 153 -#define C_GREEN_YELLOW 154 -#define C_DARK_OLIVE_GREEN_2 155 -#define C_PALE_GREEN_1_156 156 -#define C_DARK_SEA_GREEN_2_157 157 -#define C_DARK_SEA_GREEN_1_158 158 -#define C_PALE_TURQUOISE_1 159 -#define C_RED_3_160 160 -#define C_DEEP_PINK_3_161 161 -#define C_DEEP_PINK_3_162 162 -#define C_MAGENTA_3_163 163 -#define C_MAGENTA_3_164 164 -#define C_MAGENTA_2_165 165 -#define C_DARK_ORANGE_3_166 166 -#define C_INDIAN_RED_167 167 -#define C_HOT_PINK_3_168 168 -#define C_HOT_PINK_2 169 -#define C_ORCHID 170 -#define C_MEDIUM_ORCHID_1_171 171 -#define C_ORANGE_3 172 -#define C_LIGHT_SALMON_3_173 173 -#define C_LIGHT_PINK_3 174 -#define C_PINK_3 175 -#define C_PLUM_3 176 -#define C_VIOLET 177 -#define C_GOLD_3_178 178 -#define C_LIGHT_GOLDENROD_3 179 -#define C_TAN 180 -#define C_MISTY_ROSE_3 181 -#define C_THISTLE_3 182 -#define C_PLUM_2 183 -#define C_YELLOW_3_184 184 -#define C_KHAKI_3 185 -#define C_LIGHT_GOLDENROD_2_186 186 -#define C_LIGHT_YELLOW_3 187 -#define C_GREY_84 188 -#define C_LIGHT_STEEL_BLUE_1 189 -#define C_YELLOW_2 190 -#define C_DARK_OLIVE_GREEN_1_191 191 -#define C_DARK_OLIVE_GREEN_1_192 192 -#define C_DARK_SEA_GREEN_1_193 193 -#define C_HONEYDEW_2 194 -#define C_LIGHT_CYAN_1 195 -#define C_RED_1 196 -#define C_DEEP_PINK_2 197 -#define C_DEEP_PINK_1_198 198 -#define C_DEEP_PINK_1_199 199 -#define C_MAGENTA_2_200 200 -#define C_MAGENTA_1 201 -#define C_ORANGE_RED_1 202 -#define C_INDIAN_RED_1_203 203 -#define C_INDIAN_RED_1_204 204 -#define C_HOT_PINK_205 205 -#define C_HOT_PINK_206 206 -#define C_MEDIUM_ORCHID_1_207 207 -#define C_DARK_ORANGE 208 -#define C_SALMON_1 209 -#define C_LIGHT_CORAL 210 -#define C_PALE_VIOLET_RED_1 211 -#define C_ORCHID_2 212 -#define C_ORCHID_1 213 -#define C_ORANGE_1 214 -#define C_SANDY_BROWN 215 -#define C_LIGHT_SALMON_1 216 -#define C_LIGHT_PINK_1 217 -#define C_PINK_1 218 -#define C_PLUM_1 219 -#define C_GOLD_1 220 -#define C_LIGHT_GOLDENROD_2_221 221 -#define C_LIGHT_GOLDENROD_2_222 222 -#define C_NAVAJO_WHITE_1 223 -#define C_MISTY_ROSE_1 224 -#define C_THISTLE_1 225 -#define C_YELLOW_1 226 -#define C_LIGHT_GOLDENROD_1 227 -#define C_KHAKI_1 228 -#define C_WHEAT_1 229 -#define C_CORNSILK_1 230 -#define C_GREY_100 231 -#define C_GREY_3 232 -#define C_GREY_7 233 -#define C_GREY_11 234 -#define C_GREY_15 235 -#define C_GREY_19 236 -#define C_GREY_23 237 -#define C_GREY_27 238 -#define C_GREY_30 239 -#define C_GREY_35 240 -#define C_GREY_39 241 -#define C_GREY_42 242 -#define C_GREY_46 243 -#define C_GREY_50 244 -#define C_GREY_54 245 -#define C_GREY_58 246 -#define C_GREY_62 247 -#define C_GREY_66 248 -#define C_GREY_70 249 -#define C_GREY_74 250 -#define C_GREY_78 251 -#define C_GREY_82 252 -#define C_GREY_85 253 -#define C_GREY_89 254 -#define C_GREY_93 255 +/* colour palette */ +#define C_BLACK 0 +#define C_CYAN 6 +#define C_WHITE 7 +#define C_GREY 8 +#define C_RED 9 +#define C_YELLOW 11 +#define C_BLUE 12 +#define C_CYAN2 14 +#define C_BWHITE 15 /* Pane borders: thin box-drawing lines in a muted grey, drawn over the terminal's own background. */ @@ -400,11 +149,6 @@ static int send_msg(int fd, uint32_t type, const void *data, size_t len); #define SIZE_CHECK_MS 250 #define NAME_CHECK_MS 1000 #define MENU_MAX_DEPTH 8 -#define DEFAULT_HISTORY_LINES 2000 -#define MAX_HISTORY_LINES 100000 -#define MAX_HANDOFF_FDS 256 /* SCM_RIGHTS cap for a session handoff */ -#define MAX_HANDOFF_BYTES (256u * 1024u * 1024u) -#define ADOPT_TIMEOUT_MS 2000 /* ------------------------------------------------------------------ */ /* small helpers */ @@ -530,10 +274,7 @@ static int utf8_decode(const unsigned char *s, size_t n, uint32_t *cp) { static int cp_width(uint32_t cp) { int w; if (cp == 0 || cp == WIDE_CONT) return 1; - if (!unicode_scalar(cp)) return 1; -#if WCHAR_MAX < 0x10FFFF - if (cp > WCHAR_MAX) return 1; -#endif + if (!unicode_scalar(cp) || (uintmax_t)cp > (uintmax_t)WCHAR_MAX) return 1; w = wcwidth((wchar_t)cp); if (w < 0) return 1; return w; @@ -652,6 +393,9 @@ typedef enum { PS_STRING_ESC } ParseState; +#define DEFAULT_HISTORY_LINES 2000 +#define MAX_HISTORY_LINES 100000 + typedef struct Pane Pane; struct Pane { @@ -1521,11 +1265,10 @@ struct Client { int cols, rows; int attached; uint64_t order; /* attach/resize sequence, newest client wins */ - uint64_t input_order; /* last input sequence, used by resize-on-input */ int truecolor; unsigned char in[16384]; size_t inlen; - int64_t escape_since; + long long escape_since; unsigned char net[65536]; size_t netlen; @@ -1556,7 +1299,7 @@ struct Client { int confirm_action; /* 1 kill pane, 2 kill server, 3 kill window */ char msg[256]; - int64_t msg_until; + long long msg_until; char title[160]; @@ -1582,7 +1325,6 @@ struct Session { int w, h; int status_bottom; - int resize_on_input; /* size follows the client that last sent input */ int32_t status_bg; /* 0 uses the terminal default */ DisplayOptions display; int history_limit; @@ -1593,37 +1335,25 @@ struct Session { Client *clients; uint64_t client_order; int running; - int adopted; /* handed off to a new server; do not clean up fds */ int redraw; /* a removed client changed the session size */ long long last_status; long long last_name_check; }; -/* Compact sequence numbers on wrap, preserving both attach/resize and - input ordering across all clients. */ +/* Compact sequence numbers on wrap, preserving the newest-client ordering. */ static uint64_t client_next_order(Session *s) { if (s->client_order == UINT64_MAX) { uint64_t previous = 0, rank = 0; for (;;) { Client *next = NULL; - uint64_t next_value = 0; for (Client *c = s->clients; c; c = c->next) { - uint64_t values[2] = { c->order, c->input_order }; - for (int i = 0; i < 2; i++) { - uint64_t value = values[i]; - if (value > previous && (!next || value < next_value)) { - next = c; - next_value = value; - } - } + if (c->order > previous && (!next || c->order < next->order)) next = c; } if (!next) break; + previous = next->order; if (rank == UINT64_MAX - 1) range_error(); - if (next->order == next_value) next->order = rank + 1; - if (next->input_order == next_value) next->input_order = rank + 1; - previous = next_value; - rank++; + next->order = ++rank; } s->client_order = rank; } @@ -1852,7 +1582,7 @@ static Pane *pane_new(const char *cwd, int w, int h, const char *title, signal(SIGTERM, SIG_DFL); setenv("TERM", "screen-256color", 1); if (session_name && *session_name) setenv("MURX", session_name, 1); - if (cwd && *cwd && chdir(cwd) < 0) perror("murx: chdir"); + if (cwd && *cwd) chdir(cwd); const char *base = strrchr(shell, '/'); base = base ? base + 1 : shell; char arg0[PATH_MAX]; @@ -2620,7 +2350,6 @@ enum { SET_SHOW_MURX, SET_SHOW_HOSTNAME, SET_SHOW_PANE_TITLES, - SET_RESIZE_ON_INPUT, SET_PREFIX }; @@ -2696,7 +2425,6 @@ static const MenuItem menu_config_items[] = { { "Show [murx] label", MI_TOGGLE, SET_SHOW_MURX, 1, 0 }, { "Show hostname", MI_TOGGLE, SET_SHOW_HOSTNAME, 1, 0 }, { "Show pane titles", MI_TOGGLE, SET_SHOW_PANE_TITLES, 1, 0 }, - { "Resize to last input", MI_TOGGLE, SET_RESIZE_ON_INPUT, 1, 0 }, MENU_SEP, { "Reset display/status defaults", MI_ACTION, ACT_RESET_DISPLAY, 0, 0 } }; @@ -2793,7 +2521,6 @@ static int menu_setting_on(Session *s, Client *c, const MenuItem *it) { case SET_SHOW_MURX: return s->display.show_murx == it->arg; case SET_SHOW_HOSTNAME: return s->display.show_hostname == it->arg; case SET_SHOW_PANE_TITLES: return s->display.show_pane_titles == it->arg; - case SET_RESIZE_ON_INPUT: return s->resize_on_input == it->arg; case SET_PREFIX: return c->prefix == it->arg; default: return 0; } @@ -3022,7 +2749,7 @@ static int color256_from_rgb(int r, int g, int b) { int cr = nearest_cube_level(r, &ri); int cg = nearest_cube_level(g, &gi); int cb = nearest_cube_level(b, &bi); - int cube = C_GREY_0 + 36 * ri + 6 * gi + bi; + int cube = 16 + 36 * ri + 6 * gi + bi; long cubed = (long)(r - cr) * (r - cr) + (long)(g - cg) * (g - cg) + (long)(b - cb) * (b - cb); @@ -3036,7 +2763,7 @@ static int color256_from_rgb(int r, int g, int b) { (long)(g - gv) * (g - gv) + (long)(b - gv) * (b - gv); - if (grayd < cubed) return C_GREY_3 + gidx; + if (grayd < cubed) return 232 + gidx; return cube; } @@ -3222,94 +2949,6 @@ static int send_msg(int fd, uint32_t type, const void *data, size_t len) { return 0; } -static int read_exact(int fd, void *buf, size_t len) { - unsigned char *p = buf; - while (len > 0) { - ssize_t n = read(fd, p, len); - if (n > 0) { - p += (size_t)n; - len -= (size_t)n; - } else if (n < 0 && errno == EINTR) { - continue; - } else { - return -1; - } - } - return 0; -} - -/* Send a message header with file descriptors attached. The payload follows - as ordinary stream bytes so callers can pass large handoff blobs. */ -static int send_msg_fds(int fd, uint32_t type, const void *data, size_t len, - const int *fds, size_t nfds) { - MsgHdr h; - if (len > UINT32_MAX || nfds > MAX_HANDOFF_FDS) { errno = EMSGSIZE; return -1; } - h.type = type; - h.len = (uint32_t)len; - - char control[CMSG_SPACE(MAX_HANDOFF_FDS * sizeof(int))]; - memset(control, 0, sizeof control); - struct iovec iov = { &h, sizeof h }; - struct msghdr msg; - memset(&msg, 0, sizeof msg); - msg.msg_iov = &iov; - msg.msg_iovlen = 1; - if (nfds) { - msg.msg_control = control; - msg.msg_controllen = CMSG_SPACE(nfds * sizeof(int)); - struct cmsghdr *cm = CMSG_FIRSTHDR(&msg); - cm->cmsg_level = SOL_SOCKET; - cm->cmsg_type = SCM_RIGHTS; - cm->cmsg_len = CMSG_LEN(nfds * sizeof(int)); - memcpy(CMSG_DATA(cm), fds, nfds * sizeof(int)); - } - ssize_t n; - do { - n = sendmsg(fd, &msg, 0); - } while (n < 0 && errno == EINTR); - if (n < 0) return -1; - if (len && write_all(fd, data, len) < 0) return -1; - return 0; -} - -/* Receive exactly one message header, collecting any attached descriptors. */ -static int recv_msg_header(int fd, MsgHdr *h, int *outfds, int maxfds, int *nfds) { - *nfds = 0; - unsigned char *p = (unsigned char *)h; - size_t off = 0; - int got_fds = 0; - while (off < sizeof *h) { - char control[CMSG_SPACE(MAX_HANDOFF_FDS * sizeof(int))]; - memset(control, 0, sizeof control); - struct iovec iov = { p + off, sizeof *h - off }; - struct msghdr msg; - memset(&msg, 0, sizeof msg); - msg.msg_iov = &iov; - msg.msg_iovlen = 1; - msg.msg_control = control; - msg.msg_controllen = sizeof control; - ssize_t n; - do { - n = recvmsg(fd, &msg, 0); - } while (n < 0 && errno == EINTR); - if (n <= 0) return -1; - off += (size_t)n; - if (!got_fds) { - for (struct cmsghdr *cm = CMSG_FIRSTHDR(&msg); cm; cm = CMSG_NXTHDR(&msg, cm)) { - if (cm->cmsg_level != SOL_SOCKET || cm->cmsg_type != SCM_RIGHTS) continue; - size_t count = (cm->cmsg_len - CMSG_LEN(0)) / sizeof(int); - const int *fdp = (const int *)CMSG_DATA(cm); - for (size_t i = 0; i < count; i++) { - if (*nfds < maxfds) outfds[(*nfds)++] = fdp[i]; - else close(fdp[i]); - } - got_fds = 1; - } - } - } - return 0; -} - static void short_hostname(char *out, size_t n) { if (gethostname(out, n) < 0) snprintf(out, n, "localhost"); out[n - 1] = '\0'; @@ -3327,25 +2966,14 @@ static void invalidate_clients(Session *s) { } static void session_recalc_size(Session *s) { - /* Normally the most recently attached or resized client decides the - session size. This makes a reattach from a larger terminal grow the - session even if an older, smaller client is still connected. Such - clients render only the part of the session that fits (see draw_frame). - - With resize-on-input, the last client to send any input decides instead; - attach/resize order is only the fallback until the first input arrives. */ + /* The most recently attached or resized client decides the session size. + This makes a reattach from a larger terminal grow the session even if an + older, smaller client is still connected. Such clients render only the + part of the session that fits (see draw_frame). */ Client *best = NULL; - if (s->resize_on_input) { - for (Client *c = s->clients; c; c = c->next) { - if (!c->attached || c->dead || c->fd < 0 || !c->input_order) continue; - if (!best || c->input_order > best->input_order) best = c; - } - } - if (!best) { - for (Client *c = s->clients; c; c = c->next) { - if (!c->attached || c->dead || c->fd < 0) continue; - if (!best || c->order > best->order) best = c; - } + for (Client *c = s->clients; c; c = c->next) { + if (!c->attached || c->dead || c->fd < 0) continue; + if (!best || c->order > best->order) best = c; } if (!best) return; int cols = best->cols; @@ -3895,10 +3523,6 @@ static void menu_apply_setting(Client *c, Session *s, const MenuItem *it) { session_layout(s); s->redraw = 1; break; - case SET_RESIZE_ON_INPUT: - s->resize_on_input = value ? 1 : 0; - session_recalc_size(s); - break; case SET_PREFIX: c->prefix = (unsigned char)value; break; default: return; } @@ -4488,584 +4112,7 @@ static Client *client_new(int fd) { return c; } -/* ------------------------------------------------------------------ */ -/* session handoff serialization */ -/* ------------------------------------------------------------------ */ - -typedef struct { - unsigned char *p; - size_t len, cap; -} HBuf; - -typedef struct { - const unsigned char *p; - size_t len, off; -} HRdBuf; - -static void buf_reserve(HBuf *b, size_t need) { - if (need <= b->cap) return; - size_t cap = buffer_capacity(b->cap ? b->cap : 65536, need); - b->p = xrealloc(b->p, cap); - b->cap = cap; -} - -static void buf_put(HBuf *b, const void *data, size_t len) { - buf_reserve(b, size_add(b->len, len)); - memcpy(b->p + b->len, data, len); - b->len += len; -} - -static void buf_i32(HBuf *b, int32_t v) { buf_put(b, &v, sizeof v); } -static void buf_u32(HBuf *b, uint32_t v) { buf_put(b, &v, sizeof v); } -static void buf_u64(HBuf *b, uint64_t v) { buf_put(b, &v, sizeof v); } -static void buf_i64(HBuf *b, int64_t v) { buf_put(b, &v, sizeof v); } - -static void buf_double(HBuf *b, double v) { - uint64_t bits; - memcpy(&bits, &v, sizeof bits); - buf_u64(b, bits); -} - -static void buf_str(HBuf *b, const char *s, size_t max) { - size_t n = strlen(s); - if (n >= max) n = max - 1; - buf_u32(b, (uint32_t)n); - buf_put(b, s, n); -} - -static int rd_take(HRdBuf *r, void *data, size_t len) { - if (len > r->len - r->off) return -1; - memcpy(data, r->p + r->off, len); - r->off += len; - return 0; -} - -static int rd_i32(HRdBuf *r, int32_t *v) { return rd_take(r, v, sizeof *v); } -static int rd_u32(HRdBuf *r, uint32_t *v) { return rd_take(r, v, sizeof *v); } -static int rd_u64(HRdBuf *r, uint64_t *v) { return rd_take(r, v, sizeof *v); } -static int rd_i64(HRdBuf *r, int64_t *v) { return rd_take(r, v, sizeof *v); } - -static int rd_double(HRdBuf *r, double *v) { - uint64_t bits; - if (rd_u64(r, &bits) < 0) return -1; - memcpy(v, &bits, sizeof *v); - return 0; -} - -static int rd_str(HRdBuf *r, char *out, size_t max) { - uint32_t n; - if (rd_u32(r, &n) < 0 || n >= max || rd_take(r, out, n) < 0) return -1; - out[n] = '\0'; - return 0; -} - -static void ser_pen(HBuf *b, const Pen *pen) { - buf_i32(b, pen->fg); - buf_i32(b, pen->bg); - buf_u32(b, pen->attr); -} - -static int rd_pen(HRdBuf *r, Pen *pen) { - uint32_t attr; - if (rd_i32(r, &pen->fg) < 0 || rd_i32(r, &pen->bg) < 0 || - rd_u32(r, &attr) < 0) return -1; - pen->attr = (uint8_t)attr; - return 0; -} - -static void ser_grid(HBuf *b, const Grid *g) { - buf_i32(b, g->w); - buf_i32(b, g->h); - if (g->w > 0 && g->h > 0) - buf_put(b, g->c, size_mul((size_t)g->w, (size_t)g->h) * sizeof(Cell)); -} - -static int rd_grid(HRdBuf *r, Grid *g) { - int32_t w, h; - if (rd_i32(r, &w) < 0 || rd_i32(r, &h) < 0) return -1; - if (w < 1 || h < 1 || w > MAX_DIMENSION || h > MAX_DIMENSION) return -1; - size_t cells = size_mul((size_t)w, (size_t)h); - if (cells > SIZE_MAX / sizeof(Cell) || cells * sizeof(Cell) > r->len - r->off) return -1; - grid_free(g); - grid_init(g, w, h); - memcpy(g->c, r->p + r->off, cells * sizeof(Cell)); - r->off += cells * sizeof(Cell); - return 0; -} - -static void ser_layout(HBuf *b, const Layout *l) { - buf_i32(b, l->type); - buf_double(b, l->ratio); - if (l->type == L_LEAF) { - buf_i32(b, l->pane ? l->pane->id : 0); - } else { - ser_layout(b, l->a); - ser_layout(b, l->b); - } -} - -static Layout *rd_layout(HRdBuf *r, Pane **panes, int npane) { - int32_t type; - Layout *l = xcalloc(1, sizeof *l); - if (rd_i32(r, &type) < 0 || rd_double(r, &l->ratio) < 0) { free(l); return NULL; } - l->type = (int)type; - if (l->type == L_LEAF) { - int32_t id; - if (rd_i32(r, &id) < 0) { free(l); return NULL; } - for (int i = 0; i < npane; i++) { - if (panes[i]->id == id) { l->pane = panes[i]; break; } - } - if (!l->pane) { free(l); return NULL; } - return l; - } - if (l->type != L_COLS && l->type != L_ROWS) { free(l); return NULL; } - l->a = rd_layout(r, panes, npane); - l->b = rd_layout(r, panes, npane); - if (!l->a || !l->b) { - layout_free(l); - return NULL; - } - l->a->parent = l; - l->b->parent = l; - return l; -} - -static Pane *find_pane_id(Pane **panes, int npane, int32_t id) { - for (int i = 0; i < npane; i++) if (panes[i]->id == id) return panes[i]; - return NULL; -} - -static void ser_pane(HBuf *b, const Pane *p, int master_idx, int tty_idx) { - buf_i32(b, p->id); - buf_i32(b, master_idx); - buf_i32(b, tty_idx); - buf_i32(b, p->pid); - buf_i32(b, p->alive); - buf_i32(b, p->x); buf_i32(b, p->y); buf_i32(b, p->w); buf_i32(b, p->h); - ser_grid(b, &p->main_grid); - ser_grid(b, &p->alt_grid); - buf_i32(b, p->on_alt); - buf_i32(b, p->history_start); - buf_i32(b, p->history_count); - for (int i = 0; i < p->history_count; i++) { - int slot = (p->history_start + i) % p->history_limit; - ser_grid(b, &p->history[slot]); - } - buf_i32(b, p->cx); buf_i32(b, p->cy); buf_i32(b, p->wrap_pending); - buf_i32(b, p->scroll_top); buf_i32(b, p->scroll_bot); - buf_i32(b, p->cursor_visible); buf_i32(b, p->app_cursor); - buf_i32(b, p->insert_mode); - ser_pen(b, &p->pen); - buf_i32(b, p->saved_x); buf_i32(b, p->saved_y); - ser_pen(b, &p->saved_pen); - buf_i32(b, p->main_x); buf_i32(b, p->main_y); - ser_pen(b, &p->main_pen); - buf_i32(b, p->pstate); - buf_str(b, p->seq, sizeof p->seq); - buf_i32(b, p->seqlen); - buf_i32(b, p->u8_need); buf_i32(b, p->u8_have); - buf_u32(b, p->u8_cp); - buf_str(b, p->title, sizeof p->title); - buf_i32(b, p->title_pgrp); - buf_str(b, p->remote_title, sizeof p->remote_title); - buf_i32(b, p->remote_title_pgrp); - buf_str(b, p->reported_cwd, sizeof p->reported_cwd); - buf_i32(b, p->reported_cwd_pgrp); - buf_i32(b, p->reported_root); - buf_i32(b, p->reported_root_pgrp); -} - -static int rd_pane(HRdBuf *r, Pane *p, int *fds, int nfds, int history_limit) { - int32_t id, master_idx, tty_idx; - if (rd_i32(r, &id) < 0 || rd_i32(r, &master_idx) < 0 || - rd_i32(r, &tty_idx) < 0) return -1; - if (master_idx < 0 || master_idx >= nfds || tty_idx < 0 || tty_idx >= nfds) return -1; - p->id = id; - p->fd = fds[master_idx]; - p->tty_fd = fds[tty_idx]; - if (rd_i32(r, (int32_t *)&p->pid) < 0 || rd_i32(r, &p->alive) < 0) return -1; - if (rd_i32(r, &p->x) < 0 || rd_i32(r, &p->y) < 0 || - rd_i32(r, &p->w) < 0 || rd_i32(r, &p->h) < 0) return -1; - if (rd_grid(r, &p->main_grid) < 0 || rd_grid(r, &p->alt_grid) < 0) return -1; - int32_t on_alt, count, start; - if (rd_i32(r, &on_alt) < 0 || rd_i32(r, &start) < 0 || - rd_i32(r, &count) < 0) return -1; - p->on_alt = on_alt != 0; - p->history_limit = history_limit; - if (count < 0 || count > history_limit || start < 0 || - (history_limit > 0 && start >= history_limit)) return -1; - p->history_start = start; - p->history_count = count; - if (count) { - p->history = xcalloc((size_t)history_limit, sizeof *p->history); - for (int i = 0; i < count; i++) { - int slot = (start + i) % history_limit; - if (rd_grid(r, &p->history[slot]) < 0) return -1; - } - } - if (rd_i32(r, &p->cx) < 0 || rd_i32(r, &p->cy) < 0 || - rd_i32(r, &p->wrap_pending) < 0) return -1; - if (rd_i32(r, &p->scroll_top) < 0 || rd_i32(r, &p->scroll_bot) < 0 || - rd_i32(r, &p->cursor_visible) < 0 || rd_i32(r, &p->app_cursor) < 0 || - rd_i32(r, &p->insert_mode) < 0) return -1; - if (rd_pen(r, &p->pen) < 0 || rd_i32(r, &p->saved_x) < 0 || - rd_i32(r, &p->saved_y) < 0 || rd_pen(r, &p->saved_pen) < 0 || - rd_i32(r, &p->main_x) < 0 || rd_i32(r, &p->main_y) < 0 || - rd_pen(r, &p->main_pen) < 0) return -1; - int32_t pstate, seqlen, u8_need, u8_have, u8cp; - if (rd_i32(r, &pstate) < 0 || rd_str(r, p->seq, sizeof p->seq) < 0 || - rd_i32(r, &seqlen) < 0) return -1; - p->pstate = (ParseState)pstate; - p->seqlen = seqlen; - if (rd_i32(r, &u8_need) < 0 || rd_i32(r, &u8_have) < 0 || - rd_i32(r, (int32_t *)&u8cp) < 0) return -1; - p->u8_need = u8_need; - p->u8_have = u8_have; - p->u8_cp = (uint32_t)u8cp; - if (rd_str(r, p->title, sizeof p->title) < 0 || - rd_i32(r, (int32_t *)&p->title_pgrp) < 0 || - rd_str(r, p->remote_title, sizeof p->remote_title) < 0 || - rd_i32(r, (int32_t *)&p->remote_title_pgrp) < 0 || - rd_str(r, p->reported_cwd, sizeof p->reported_cwd) < 0 || - rd_i32(r, (int32_t *)&p->reported_cwd_pgrp) < 0 || - rd_i32(r, &p->reported_root) < 0 || - rd_i32(r, (int32_t *)&p->reported_root_pgrp) < 0) return -1; - p->grid = p->on_alt ? &p->alt_grid : &p->main_grid; - return 0; -} - -static void ser_client(HBuf *b, const Client *c, int fd_idx, - Pane **panes, int npane, size_t skip_net) { - buf_i32(b, fd_idx); - buf_i32(b, c->cols); buf_i32(b, c->rows); - buf_i32(b, c->attached); - buf_u64(b, c->order); - buf_u64(b, c->input_order); - buf_i32(b, c->truecolor); - buf_u32(b, (uint32_t)c->inlen); - buf_put(b, c->in, c->inlen); - buf_i64(b, c->escape_since); - /* Preserve partially received protocol messages. The invoking client's - C_ADOPT request is stripped by the caller so it is not replayed in the - new server. */ - size_t netoff = skip_net <= c->netlen ? skip_net : c->netlen; - size_t netlen = c->netlen - netoff; - buf_u32(b, (uint32_t)netlen); - buf_put(b, c->net + netoff, netlen); - buf_i32(b, c->locked); buf_i32(b, c->prefix_pending); - buf_i32(b, c->prefix); - buf_i32(b, c->mode); buf_i32(b, c->prompt_kind); - buf_str(b, c->prompt, sizeof c->prompt); - buf_i32(b, c->prompt_len); buf_i32(b, c->prompt_label_len); - buf_i32(b, c->menu_page); buf_i32(b, c->menu_top); - buf_i32(b, c->menu_sel); buf_i32(b, c->menu_depth); - for (int i = 0; i < MENU_MAX_DEPTH; i++) { - buf_i32(b, c->menu_parent[i]); - buf_i32(b, c->menu_parent_sel[i]); - } - buf_i32(b, c->confirm_action); - buf_str(b, c->msg, sizeof c->msg); - buf_i64(b, c->msg_until); - buf_str(b, c->title, sizeof c->title); - buf_i32(b, c->dead); - - uint32_t nscroll = 0; - for (Scrollback *sb = c->scrollbacks; sb; sb = sb->next) nscroll++; - buf_u32(b, nscroll); - for (Scrollback *sb = c->scrollbacks; sb; sb = sb->next) { - int32_t id = 0; - for (int i = 0; i < npane; i++) { - if (panes[i] == sb->pane) { id = panes[i]->id; break; } - } - buf_i32(b, id); - buf_i32(b, sb->offset); - ser_grid(b, &sb->grid); - } -} - -static int rd_client(HRdBuf *r, Client *c, int *fds, int nfds, - Pane **panes, int npane) { - int32_t fd_idx; - if (rd_i32(r, &fd_idx) < 0) return -1; - if (fd_idx < 0 || fd_idx >= nfds) return -1; - c->fd = fds[fd_idx]; - if (rd_i32(r, &c->cols) < 0 || rd_i32(r, &c->rows) < 0 || - rd_i32(r, &c->attached) < 0) return -1; - if (rd_u64(r, &c->order) < 0 || rd_u64(r, &c->input_order) < 0) return -1; - if (rd_i32(r, &c->truecolor) < 0) return -1; - uint32_t inlen; - if (rd_u32(r, &inlen) < 0 || inlen > sizeof c->in || - rd_take(r, c->in, inlen) < 0) return -1; - c->inlen = inlen; - if (rd_i64(r, &c->escape_since) < 0) return -1; - uint32_t netlen; - if (rd_u32(r, &netlen) < 0 || netlen > sizeof c->net || - rd_take(r, c->net, netlen) < 0) return -1; - c->netlen = netlen; - int32_t prefix; - if (rd_i32(r, &c->locked) < 0 || rd_i32(r, &c->prefix_pending) < 0 || - rd_i32(r, &prefix) < 0) return -1; - c->prefix = (unsigned char)prefix; - if (rd_i32(r, &c->mode) < 0 || rd_i32(r, &c->prompt_kind) < 0 || - rd_str(r, c->prompt, sizeof c->prompt) < 0 || - rd_i32(r, &c->prompt_len) < 0 || - rd_i32(r, &c->prompt_label_len) < 0) return -1; - if (rd_i32(r, &c->menu_page) < 0 || rd_i32(r, &c->menu_top) < 0 || - rd_i32(r, &c->menu_sel) < 0 || rd_i32(r, &c->menu_depth) < 0) return -1; - for (int i = 0; i < MENU_MAX_DEPTH; i++) { - if (rd_i32(r, &c->menu_parent[i]) < 0 || - rd_i32(r, &c->menu_parent_sel[i]) < 0) return -1; - } - if (rd_i32(r, &c->confirm_action) < 0 || - rd_str(r, c->msg, sizeof c->msg) < 0 || - rd_i64(r, &c->msg_until) < 0 || - rd_str(r, c->title, sizeof c->title) < 0) return -1; - int32_t dead; - if (rd_i32(r, &dead) < 0) return -1; - c->dead = dead != 0; - - uint32_t nscroll; - if (rd_u32(r, &nscroll) < 0 || nscroll > 512) return -1; - for (uint32_t i = 0; i < nscroll; i++) { - Scrollback *sb = xcalloc(1, sizeof *sb); - int32_t id, offset; - if (rd_i32(r, &id) < 0 || rd_i32(r, &offset) < 0) { - free(sb); - return -1; - } - sb->pane = find_pane_id(panes, npane, id); - if (!sb->pane || rd_grid(r, &sb->grid) < 0) { - free(sb); - return -1; - } - sb->offset = offset; - sb->next = c->scrollbacks; - c->scrollbacks = sb; - } - return 0; -} - -static int session_serialize(Session *s, HBuf *b, int *fds, int *nfds, - Client *adopt_client, size_t adopt_skip) { - Pane *panes[512]; - int npane = 0; - session_panes(s, panes, 512, &npane); - int nclient = 0; - for (Client *c = s->clients; c; c = c->next) nclient++; - /* listening socket + master/tty per pane + every client socket */ - if (count_add(count_add(1, count_add(npane, npane)), nclient) > MAX_HANDOFF_FDS) - return -1; - - buf_u32(b, 0x4d48524c); /* "MHRL" */ - buf_u32(b, 1); - buf_str(b, s->name, sizeof s->name); - buf_str(b, s->cwd, sizeof s->cwd); - buf_str(b, s->sock_path, sizeof s->sock_path); - buf_i32(b, s->w); buf_i32(b, s->h); - buf_i32(b, s->status_bottom); buf_i32(b, s->resize_on_input); - buf_i32(b, s->status_bg); - buf_i32(b, s->history_limit); - buf_i32(b, s->display.show_cwd); buf_i32(b, s->display.show_clock); - buf_i32(b, s->display.show_murx); buf_i32(b, s->display.show_hostname); - buf_i32(b, s->display.show_pane_titles); - buf_u64(b, s->client_order); - - int fd_i = 0; - fds[fd_i++] = s->listen_fd; - buf_u32(b, (uint32_t)npane); - for (int i = 0; i < npane; i++) { - int master = fd_i++; - int tty = fd_i++; - fds[master] = panes[i]->fd; - fds[tty] = panes[i]->tty_fd; - ser_pane(b, panes[i], master, tty); - } - - int nwin = 0; - for (Window *w = s->windows; w; w = w->next) nwin++; - buf_u32(b, (uint32_t)nwin); - int cur_idx = -1, last_idx = -1, wi = 0; - for (Window *w = s->windows; w; w = w->next, wi++) { - if (w == s->cur) cur_idx = wi; - if (w == s->last) last_idx = wi; - buf_str(b, w->name, sizeof w->name); - buf_i32(b, w->index); - buf_i32(b, w->privileged); - buf_i32(b, w->active ? w->active->id : 0); - ser_layout(b, w->root); - } - buf_i32(b, cur_idx); - buf_i32(b, last_idx); - - buf_u32(b, (uint32_t)nclient); - for (Client *c = s->clients; c; c = c->next) { - fds[fd_i++] = c->fd; - ser_client(b, c, fd_i - 1, panes, npane, - c == adopt_client ? adopt_skip : 0); - } - *nfds = fd_i; - return 0; -} - -static Session *session_deserialize(HRdBuf *r, int *fds, int nfds) { - uint32_t magic, version; - if (rd_u32(r, &magic) < 0 || magic != 0x4d48524c || - rd_u32(r, &version) < 0 || version != 1) return NULL; - Session *s = xcalloc(1, sizeof *s); - if (rd_str(r, s->name, sizeof s->name) < 0 || - rd_str(r, s->cwd, sizeof s->cwd) < 0 || - rd_str(r, s->sock_path, sizeof s->sock_path) < 0) goto fail; - if (rd_i32(r, &s->w) < 0 || rd_i32(r, &s->h) < 0 || - rd_i32(r, &s->status_bottom) < 0 || - rd_i32(r, &s->resize_on_input) < 0 || - rd_i32(r, &s->status_bg) < 0 || - rd_i32(r, &s->history_limit) < 0 || - s->history_limit < 0 || s->history_limit > MAX_HISTORY_LINES) goto fail; - if (rd_i32(r, &s->display.show_cwd) < 0 || - rd_i32(r, &s->display.show_clock) < 0 || - rd_i32(r, &s->display.show_murx) < 0 || - rd_i32(r, &s->display.show_hostname) < 0 || - rd_i32(r, &s->display.show_pane_titles) < 0) goto fail; - if (rd_u64(r, &s->client_order) < 0) goto fail; - - s->listen_fd = fds[0]; - uint32_t npane; - if (rd_u32(r, &npane) < 0 || npane > 512) goto fail; - Pane **panes = xcalloc(npane ? npane : 1, sizeof *panes); - for (uint32_t i = 0; i < npane; i++) { - panes[i] = xcalloc(1, sizeof *panes[i]); - if (rd_pane(r, panes[i], fds, nfds, s->history_limit) < 0) - goto fail_panes; - } - - uint32_t nwin; - if (rd_u32(r, &nwin) < 0 || nwin > 512) goto fail_panes; - for (uint32_t i = 0; i < nwin; i++) { - Window *w = xcalloc(1, sizeof *w); - int32_t active_id; - if (rd_str(r, w->name, sizeof w->name) < 0 || - rd_i32(r, &w->index) < 0 || rd_i32(r, &w->privileged) < 0 || - rd_i32(r, &active_id) < 0) { free(w); goto fail_panes; } - w->root = rd_layout(r, panes, (int)npane); - if (!w->root) { free(w); goto fail_panes; } - w->active = find_pane_id(panes, (int)npane, active_id); - w->next = s->windows; - s->windows = w; - } - /* Rebuild in serialization order. */ - Window **order = xcalloc(nwin ? nwin : 1, sizeof *order); - uint32_t n = 0; - for (Window *w = s->windows; w; w = w->next) order[n++] = w; - s->windows = NULL; - for (uint32_t i = n; i > 0; i--) { - order[i - 1]->next = s->windows; - s->windows = order[i - 1]; - } - free(order); - s->nwin = (int)nwin; - int32_t cur_idx, last_idx; - if (rd_i32(r, &cur_idx) < 0 || rd_i32(r, &last_idx) < 0) goto fail_panes; - for (int i = 0; i < (int)nwin; i++) { - Window *w = s->windows; - for (int k = 0; k < i; k++) w = w->next; - if (i == cur_idx) s->cur = w; - if (i == last_idx) s->last = w; - } - - uint32_t nclient; - if (rd_u32(r, &nclient) < 0 || nclient > MAX_HANDOFF_FDS) goto fail_panes; - Client **clients = xcalloc(nclient ? nclient : 1, sizeof *clients); - for (uint32_t i = 0; i < nclient; i++) { - clients[i] = xcalloc(1, sizeof *clients[i]); - if (rd_client(r, clients[i], fds, nfds, panes, (int)npane) < 0) - goto fail_clients; - } - for (uint32_t i = nclient; i > 0; i--) { - clients[i - 1]->next = s->clients; - s->clients = clients[i - 1]; - } - free(clients); - free(panes); - s->running = 1; - s->redraw = 1; - s->last_status = now_ms(); - s->last_name_check = now_ms(); - session_layout(s); - for (Client *c = s->clients; c; c = c->next) { - client_invalidate(c); - client_message(c, "Session adopted by new murx server"); - } - return s; - -fail_clients: - for (uint32_t i = 0; i < nclient; i++) { - if (!clients[i]) continue; - if (clients[i]->prev_valid) grid_free(&clients[i]->prev); - while (clients[i]->scrollbacks) - scrollback_remove(&clients[i]->scrollbacks); - free(clients[i]); - } - free(clients); -fail_panes: - for (uint32_t i = 0; i < npane; i++) { - if (!panes[i]) continue; - grid_free(&panes[i]->main_grid); - grid_free(&panes[i]->alt_grid); - if (panes[i]->history) { - for (int k = 0; k < panes[i]->history_count; k++) - grid_free(&panes[i]->history[k]); - free(panes[i]->history); - } - free(panes[i]); - } - free(panes); -fail: { - /* Panes are freed separately above; free only the window scaffolding. */ - Window *w = s->windows; - while (w) { - Window *next = w->next; - layout_free(w->root); - free(w); - w = next; - } - free(s); - return NULL; -} -} - -static void server_handoff(Client *c, Session *s, int handoff_fd, - uint32_t adopt_len) { - HBuf b; - memset(&b, 0, sizeof b); - int fds[MAX_HANDOFF_FDS]; - int nfds = 0; - MsgHdr fail = { 0, 0 }; - size_t adopt_skip = size_add(sizeof(MsgHdr), adopt_len); - if (handoff_fd < 0 || - session_serialize(s, &b, fds, &nfds, c, adopt_skip) < 0 || - b.len == 0 || b.len > MAX_HANDOFF_BYTES) { - if (handoff_fd >= 0) (void)write_all(handoff_fd, &fail, sizeof fail); - free(b.p); - return; - } - if (send_msg_fds(handoff_fd, S_HANDOFF, b.p, b.len, fds, (size_t)nfds) < 0) { - (void)write_all(handoff_fd, &fail, sizeof fail); - } else { - /* The new server owns duplicates of every descriptor. Exit without - closing our copies, detaching clients or unlinking the socket. */ - s->adopted = 1; - s->running = 0; - } - free(b.p); -} - -static void handle_message(Client *c, Session *s, uint32_t type, - const unsigned char *data, uint32_t len, - int *fds, int nfds) { - if (type == C_ADOPT) { - server_handoff(c, s, nfds > 0 ? fds[0] : -1, len); - return; - } +static void handle_message(Client *c, Session *s, uint32_t type, const unsigned char *data, uint32_t len) { if (type == C_ATTACH) { if (len >= 8) { uint32_t cols, rows, flags = 0; @@ -5083,10 +4130,6 @@ static void handle_message(Client *c, Session *s, uint32_t type, uint32_t color = 0; if (len >= 24) memcpy(&color, data + 20, 4); if (color && (color >> 24) != 1) { c->dead = 1; return; } - uint32_t size_mode = SIZE_MODE_KEEP; - if (len >= 28) memcpy(&size_mode, data + 24, 4); - if (size_mode > SIZE_MODE_ON) { c->dead = 1; return; } - if (size_mode == SIZE_MODE_ON) s->resize_on_input = 1; if (color) s->status_bg = (int32_t)color; if (status) { s->status_bottom = status == 2; @@ -5122,72 +4165,38 @@ static void handle_message(Client *c, Session *s, uint32_t type, invalidate_clients(s); } } else if (type == C_INPUT) { - /* Track input even while the mode is off so enabling it later (from - the menu or a reattach) already knows the last active client. */ - if (len && c->attached) { - c->input_order = client_next_order(s); - if (s->resize_on_input) session_recalc_size(s); - } client_feed(c, s, data, len); } } static void client_read_socket(Client *c, Session *s) { unsigned char tmp[16384]; - char control[CMSG_SPACE(MAX_HANDOFF_FDS * sizeof(int))]; - memset(control, 0, sizeof control); - struct iovec iov = { tmp, sizeof tmp }; - struct msghdr msg; - memset(&msg, 0, sizeof msg); - msg.msg_iov = &iov; - msg.msg_iovlen = 1; - msg.msg_control = control; - msg.msg_controllen = sizeof control; - - ssize_t n; - do { - n = recvmsg(c->fd, &msg, 0); - } while (n < 0 && errno == EINTR); + ssize_t n = read(c->fd, tmp, sizeof tmp); if (n <= 0) { - if (n < 0 && (errno == EAGAIN || errno == EWOULDBLOCK)) return; + if (n < 0 && (errno == EINTR || errno == EAGAIN)) return; c->dead = 1; return; } - int msgfds[MAX_HANDOFF_FDS]; - int nfds = 0; - for (struct cmsghdr *cm = CMSG_FIRSTHDR(&msg); cm; cm = CMSG_NXTHDR(&msg, cm)) { - if (cm->cmsg_level != SOL_SOCKET || cm->cmsg_type != SCM_RIGHTS) continue; - size_t count = (cm->cmsg_len - CMSG_LEN(0)) / sizeof(int); - const int *fdp = (const int *)CMSG_DATA(cm); - for (size_t i = 0; i < count && nfds < MAX_HANDOFF_FDS; i++) - msgfds[nfds++] = fdp[i]; - } - if ((size_t)n > sizeof c->net - c->netlen) { c->dead = 1; - for (int i = 0; i < nfds; i++) close(msgfds[i]); return; } memcpy(c->net + c->netlen, tmp, (size_t)n); c->netlen += (size_t)n; size_t off = 0; - int first = 1; while (c->netlen - off >= sizeof(MsgHdr)) { MsgHdr h; memcpy(&h, c->net + off, sizeof h); - if (h.len > sizeof c->net - sizeof h) { c->dead = 1; break; } + if (h.len > sizeof c->net - sizeof h) { c->dead = 1; return; } if (h.len > c->netlen - off - sizeof h) break; - handle_message(c, s, h.type, c->net + off + sizeof h, h.len, - first ? msgfds : NULL, first ? nfds : 0); - first = 0; + handle_message(c, s, h.type, c->net + off + sizeof h, h.len); off += sizeof h + h.len; } if (off > 0) { memmove(c->net, c->net + off, c->netlen - off); c->netlen -= off; } - for (int i = 0; i < nfds; i++) close(msgfds[i]); } static void accept_client(Session *s) { @@ -5370,11 +4379,9 @@ static void run_server(Session *s) { } } - if (!s->adopted) { - char msg[128]; - snprintf(msg, sizeof msg, "[murx: session \"%s\" server exited]", s->name); - server_notify_all(s, msg); - } + char msg[128]; + snprintf(msg, sizeof msg, "[murx: session \"%s\" server exited]", s->name); + server_notify_all(s, msg); free(panes); } @@ -5414,8 +4421,7 @@ static void server_fail(int readyfd, const char *stage, int err) { } static void server_start(const char *path, const char *name, int readyfd, - const DisplayOptions *display, int history_limit, - int adopt_fd, int *adopt_fds, int adopt_nfds) { + const DisplayOptions *display, int history_limit) { signal(SIGPIPE, SIG_IGN); signal(SIGHUP, SIG_IGN); signal(SIGCHLD, SIG_DFL); @@ -5426,86 +4432,61 @@ static void server_start(const char *path, const char *name, int readyfd, sigaction(SIGINT, &sa, NULL); sigaction(SIGQUIT, &sa, NULL); - Session *s = NULL; - int lfd = -1; - if (adopt_fd >= 0) { - MsgHdr h; - if (adopt_nfds < 1 || read_exact(adopt_fd, &h, sizeof h) < 0 || - h.type != S_HANDOFF || h.len == 0 || h.len > MAX_HANDOFF_BYTES) { - server_fail(readyfd, "adopt handshake", EPROTO); - _exit(1); - } - unsigned char *payload = xmalloc(h.len ? h.len : 1); - if (read_exact(adopt_fd, payload, h.len) < 0) { - free(payload); - server_fail(readyfd, "adopt handshake", EPROTO); - _exit(1); - } - HRdBuf r = { payload, h.len, 0 }; - s = session_deserialize(&r, adopt_fds, adopt_nfds); - free(payload); - if (!s) { - server_fail(readyfd, "adopt state", EPROTO); - _exit(1); - } - lfd = s->listen_fd; - } else { - lfd = socket(AF_UNIX, SOCK_STREAM, 0); - if (lfd < 0) { - server_fail(readyfd, "socket", errno); - _exit(1); - } - struct sockaddr_un saun; - memset(&saun, 0, sizeof saun); - saun.sun_family = AF_UNIX; - size_t path_len = strlen(path); - if (path_len >= sizeof saun.sun_path) { - close(lfd); - server_fail(readyfd, "socket path", ENAMETOOLONG); - _exit(1); - } - memcpy(saun.sun_path, path, path_len + 1); - unlink(path); - /* Protect the socket without imposing private file permissions on pane - shells and their build output (rsync -a preserves those permissions). */ - mode_t saved_umask = umask(077); - int bound = bind(lfd, (struct sockaddr *)&saun, sizeof saun); - int bind_err = errno; - umask(saved_umask); - if (bound < 0) { - close(lfd); - server_fail(readyfd, "bind", bind_err); - _exit(1); - } - if (listen(lfd, 16) < 0) { - int err = errno; - close(lfd); - server_fail(readyfd, "listen", err); - _exit(1); - } - chmod(path, 0600); - - s = xcalloc(1, sizeof *s); - snprintf(s->name, sizeof s->name, "%s", name); - snprintf(s->sock_path, sizeof s->sock_path, "%s", path); - if (!getcwd(s->cwd, sizeof s->cwd)) snprintf(s->cwd, sizeof s->cwd, "/"); - s->listen_fd = lfd; - s->w = 80; - s->h = 24; - s->running = 1; - s->display = *display; - s->history_limit = history_limit; - s->last_status = now_ms(); - - Window *w = window_new(s, NULL); - if (!w) { - server_fail(readyfd, "cannot create pane (openpty)", errno); - server_cleanup(s); - _exit(1); - } - s->cur = w; - session_layout(s); + int lfd = socket(AF_UNIX, SOCK_STREAM, 0); + if (lfd < 0) { + server_fail(readyfd, "socket", errno); + _exit(1); } + struct sockaddr_un saun; + memset(&saun, 0, sizeof saun); + saun.sun_family = AF_UNIX; + size_t path_len = strlen(path); + if (path_len >= sizeof saun.sun_path) { + close(lfd); + server_fail(readyfd, "socket path", ENAMETOOLONG); + _exit(1); + } + memcpy(saun.sun_path, path, path_len + 1); + unlink(path); + /* Protect the socket without imposing private file permissions on pane + shells and their build output (rsync -a preserves those permissions). */ + mode_t saved_umask = umask(077); + int bound = bind(lfd, (struct sockaddr *)&saun, sizeof saun); + int bind_err = errno; + umask(saved_umask); + if (bound < 0) { + close(lfd); + server_fail(readyfd, "bind", bind_err); + _exit(1); + } + if (listen(lfd, 16) < 0) { + int err = errno; + close(lfd); + server_fail(readyfd, "listen", err); + _exit(1); + } + chmod(path, 0600); + + Session *s = xcalloc(1, sizeof *s); + snprintf(s->name, sizeof s->name, "%s", name); + snprintf(s->sock_path, sizeof s->sock_path, "%s", path); + if (!getcwd(s->cwd, sizeof s->cwd)) snprintf(s->cwd, sizeof s->cwd, "/"); + s->listen_fd = lfd; + s->w = 80; + s->h = 24; + s->running = 1; + s->display = *display; + s->history_limit = history_limit; + s->last_status = now_ms(); + + Window *w = window_new(s, NULL); + if (!w) { + server_fail(readyfd, "cannot create pane (openpty)", errno); + server_cleanup(s); + _exit(1); + } + s->cur = w; + session_layout(s); if (readyfd >= 0) { char ok = '1'; @@ -5514,7 +4495,6 @@ static void server_start(const char *path, const char *name, int readyfd, } run_server(s); - if (s->adopted) _exit(0); server_cleanup(s); free(s); _exit(0); @@ -5657,103 +4637,8 @@ static int ensure_socket_dir(void) { return 0; } -/* Result of a successful C_ADOPT exchange. The descriptors were received via - SCM_RIGHTS and are inherited by the new server across fork(). */ -typedef struct { - int client_fd; /* existing connection, kept for the attach below */ - MsgHdr h; - unsigned char *payload; - int *fds; - int nfds; -} AdoptHandoff; - -static void adopt_handoff_free(AdoptHandoff *a) { - if (!a) return; - for (int i = 0; i < a->nfds; i++) { - if (a->fds[i] >= 0) close(a->fds[i]); - } - free(a->fds); - free(a->payload); - memset(a, 0, sizeof *a); -} - -static void close_fds(int *fds, int nfds) { - for (int i = 0; i < nfds; i++) { - if (fds[i] >= 0) close(fds[i]); - } -} - -/* Ask a cooperating server to hand its session to a new process. An older - server ignores C_ADOPT, so a timeout is a clean "cannot adopt" result. */ -static int request_adoption(int client_fd, AdoptHandoff *a) { - memset(a, 0, sizeof *a); - a->client_fd = client_fd; - - int sp[2]; - if (socketpair(AF_UNIX, SOCK_STREAM, 0, sp) < 0) return -1; - int pass_fd = sp[1]; - if (send_msg_fds(client_fd, C_ADOPT, NULL, 0, &pass_fd, 1) < 0) { - int err = errno; - close(sp[0]); - close(sp[1]); - errno = err; - return -1; - } - close(sp[1]); - - struct pollfd pfd; - pfd.fd = sp[0]; - pfd.events = POLLIN; - pfd.revents = 0; - int pr; - do { - pr = poll(&pfd, 1, ADOPT_TIMEOUT_MS); - } while (pr < 0 && errno == EINTR); - if (pr == 0) { - close(sp[0]); - errno = ETIMEDOUT; - return -1; - } - if (pr < 0) { - int err = errno; - close(sp[0]); - errno = err; - return -1; - } - - int fds[MAX_HANDOFF_FDS]; - int nfds = 0; - MsgHdr h; - if (recv_msg_header(sp[0], &h, fds, MAX_HANDOFF_FDS, &nfds) < 0 || - h.type != S_HANDOFF || h.len == 0 || h.len > MAX_HANDOFF_BYTES || - nfds < 1) { - close_fds(fds, nfds); - close(sp[0]); - errno = EPROTO; - return -1; - } - unsigned char *payload = xmalloc(h.len); - if (read_exact(sp[0], payload, h.len) < 0) { - int err = errno ? errno : EPROTO; - close_fds(fds, nfds); - close(sp[0]); - free(payload); - errno = err; - return -1; - } - close(sp[0]); - - a->h = h; - a->payload = payload; - a->nfds = nfds; - a->fds = xmalloc((size_t)nfds * sizeof *a->fds); - memcpy(a->fds, fds, (size_t)nfds * sizeof *a->fds); - return 0; -} - static int spawn_server(const char *path, const char *name, - const DisplayOptions *display, int history_limit, - const AdoptHandoff *adopt) { + const DisplayOptions *display, int history_limit) { if (ensure_socket_dir() < 0) return -1; char dir[PATH_MAX], lockpath[PATH_MAX + 64]; socket_dir(dir, sizeof dir); @@ -5765,14 +4650,11 @@ static int spawn_server(const char *path, const char *name, return -1; } - int fd = -1; - if (!adopt) { - fd = connect_session(path); - if (fd >= 0) { - flock(lock, LOCK_UN); - close(lock); - return fd; - } + int fd = connect_session(path); + if (fd >= 0) { + flock(lock, LOCK_UN); + close(lock); + return fd; } int p[2]; @@ -5781,30 +4663,17 @@ static int spawn_server(const char *path, const char *name, close(lock); return -1; } - int hp[2] = { -1, -1 }; - if (adopt && pipe(hp) < 0) { - close(p[0]); - close(p[1]); - flock(lock, LOCK_UN); - close(lock); - return -1; - } pid_t pid = fork(); if (pid < 0) { close(p[0]); close(p[1]); - if (adopt) { - close(hp[0]); - close(hp[1]); - } flock(lock, LOCK_UN); close(lock); return -1; } if (pid == 0) { close(p[0]); - if (adopt) close(hp[1]); setsid(); pid_t pid2 = fork(); if (pid2 < 0) _exit(1); @@ -5817,25 +4686,10 @@ static int spawn_server(const char *path, const char *name, if (dn > 2) close(dn); } close(lock); - if (adopt) { - server_start(path, name, p[1], display, history_limit, hp[0], - adopt->fds, adopt->nfds); - } else { - server_start(path, name, p[1], display, history_limit, -1, NULL, 0); - } + server_start(path, name, p[1], display, history_limit); _exit(1); } close(p[1]); - - int handoff_err = 0; - if (adopt) { - close(hp[0]); - if (write_all(hp[1], &adopt->h, sizeof adopt->h) < 0 || - write_all(hp[1], adopt->payload, adopt->h.len) < 0) - handoff_err = errno; - close(hp[1]); - } - char msg[256]; size_t used = 0; ssize_t got; @@ -5856,10 +4710,6 @@ static int spawn_server(const char *path, const char *name, flock(lock, LOCK_UN); close(lock); - if (handoff_err) { - errno = handoff_err; - return -1; - } if (used < 1 || msg[0] != '1') { if (used > 0) { fprintf(stderr, "murx: server failed to start: %s\n", msg); @@ -5869,7 +4719,6 @@ static int spawn_server(const char *path, const char *name, } return -1; } - if (adopt) return adopt->client_fd; for (int i = 0; i < 300; i++) { fd = connect_session(path); if (fd >= 0) return fd; @@ -5943,8 +4792,7 @@ static int terminal_truecolor(void) { return 1; } -static int run_client(int fd, const char *name, unsigned char prefix, int status, - uint32_t color, uint32_t resize_mode) { +static int run_client(int fd, const char *name, unsigned char prefix, int status, uint32_t color) { struct termios saved; if (tty_raw(&saved) < 0) { fprintf(stderr, "murx: not a terminal\n"); @@ -5979,9 +4827,8 @@ static int run_client(int fd, const char *name, unsigned char prefix, int status int cols, rows; tty_size(&cols, &rows); - uint32_t attach[7] = { (uint32_t)cols, (uint32_t)rows, - (uint32_t)terminal_truecolor(), prefix, (uint32_t)status, color, - resize_mode }; + uint32_t attach[6] = { (uint32_t)cols, (uint32_t)rows, + (uint32_t)terminal_truecolor(), prefix, (uint32_t)status, color }; if (send_msg(fd, C_ATTACH, attach, sizeof attach) < 0) { /* errno is the only clue: the server accepted the connection but the attach write failed, usually because it closed the socket while @@ -6115,15 +4962,6 @@ static void help_row(FILE *out, const char *left, const char *right) { } static void print_help(FILE *out) { - fputs(" +-------------------------+\n" - " | murx | |\n" - " +-------------+-----------+\n" - " | >_ | ======== |\n" - " | | ==== |\n" - " | terminal +-----------+\n" - " | multiplexer | :|:||: |\n" - " +-------------+-----------+\n" - "\n", out); fprintf(out, "murx %s\n\n", MURX_VERSION); fputs("usage: murx [OPTIONS] [session]\n" "\n" @@ -6135,7 +4973,6 @@ static void print_help(FILE *out) { help_row(out, "-h, --help", "show this help and exit"); help_row(out, "-v, --version", "print the version and exit"); help_row(out, "-l, --list-sessions", "list running sessions without attaching"); - help_row(out, "--adopt", "move a session to a new server binary"); help_row(out, "-p, --prefix LETTER", "client prefix Ctrl-LETTER (a-z, default: a)"); help_row(out, "-s, --status POSITION", "status line: top or bottom (new sessions: top)"); help_row(out, "-c, --color RRGGBB", "status bar background, six hex digits (e.g. 000000)"); @@ -6143,25 +4980,17 @@ static void print_help(FILE *out) { snprintf(history_help, sizeof history_help, "history lines per pane in new sessions (0-%d, default: %d)", MAX_HISTORY_LINES, DEFAULT_HISTORY_LINES); - help_row(out, "-H, --history LINES", history_help); - help_row(out, "-d, --cwd", "show current directory in status bar"); - help_row(out, "-C, --clock", "show clock in status bar"); - help_row(out, "-m, --murx", "show [murx] in status bar unless --cwd is set"); - help_row(out, "-n, --hostname", "show hostname: prefix in window titles"); - help_row(out, "-t, --titles", "show the title row above each pane (default: off)"); - help_row(out, "--resize-on-input", "session size follows the last client with input"); + help_row(out, "--history LINES", history_help); + help_row(out, "--cwd", "show current directory in status bar"); + help_row(out, "--clock", "show clock in status bar"); + help_row(out, "--murx", "show [murx] in status bar unless --cwd is set"); + help_row(out, "--hostname", "show hostname: prefix in window titles"); + help_row(out, "--pane-titles", "show the title row above each pane (default: off)"); fputs("\n" - "--adopt asks the running server to hand the session to a new server\n" - "process, so an upgraded binary can take over without restarting panes.\n" - "It works with a session name or with the default session, and needs\n" - "cooperation from the running server; older servers are attached to\n" - "unchanged. Clients see a short notification when adoption succeeds.\n" - "\n" "Without -s or -c, an existing session keeps its position and color.\n" - "Without --resize-on-input, a session keeps its current size mode.\n" - "Display options and the size mode can be changed later\n" - "from the Configuration menu.\n" + "Display options set new session values and can be changed later from\n" + "the Configuration menu.\n" "\n" "Shortcuts (press the prefix, release it, then press the key shown;\n" "Ctrl-A is the default prefix):\n" @@ -6217,9 +5046,6 @@ static int parse_prefix(const char *text, unsigned char *prefix) { int main(int argc, char **argv) { setlocale(LC_CTYPE, ""); - /* The adoption handoff writes to a pipe whose reader is a forked server; - a failed start must surface as EPIPE, not kill this process. */ - signal(SIGPIPE, SIG_IGN); int argi = 1; unsigned char prefix = DEFAULT_PREFIX; @@ -6227,9 +5053,7 @@ int main(int argc, char **argv) { uint32_t color = 0; DisplayOptions display = {0}; int history_limit = DEFAULT_HISTORY_LINES; - uint32_t resize_mode = SIZE_MODE_KEEP; int list = 0; - int adopt = 0; while (argi < argc && argv[argi][0] == '-' && argv[argi][1]) { const char *option = argv[argi++]; if (strcmp(option, "--") == 0) break; @@ -6243,7 +5067,7 @@ int main(int argc, char **argv) { } if (strcmp(option, "-l") == 0 || strcmp(option, "--list-sessions") == 0) { list = 1; - } else if (strcmp(option, "-H") == 0 || strcmp(option, "--history") == 0) { + } else if (strcmp(option, "--history") == 0) { const char *value = argi < argc ? argv[argi] : ""; char *end; long lines = strtol(value, &end, 10); @@ -6254,20 +5078,16 @@ int main(int argc, char **argv) { } history_limit = (int)lines; argi++; - } else if (strcmp(option, "--adopt") == 0) { - adopt = 1; - } else if (strcmp(option, "-d") == 0 || strcmp(option, "--cwd") == 0) { + } else if (strcmp(option, "--cwd") == 0) { display.show_cwd = 1; - } else if (strcmp(option, "-C") == 0 || strcmp(option, "--clock") == 0) { + } else if (strcmp(option, "--clock") == 0) { display.show_clock = 1; - } else if (strcmp(option, "-m") == 0 || strcmp(option, "--murx") == 0) { + } else if (strcmp(option, "--murx") == 0) { display.show_murx = 1; - } else if (strcmp(option, "-n") == 0 || strcmp(option, "--hostname") == 0) { + } else if (strcmp(option, "--hostname") == 0) { display.show_hostname = 1; - } else if (strcmp(option, "-t") == 0 || strcmp(option, "--titles") == 0) { + } else if (strcmp(option, "--pane-titles") == 0) { display.show_pane_titles = 1; - } else if (strcmp(option, "--resize-on-input") == 0) { - resize_mode = SIZE_MODE_ON; } else if (strcmp(option, "-p") == 0 || strcmp(option, "--prefix") == 0) { if (argi >= argc || !parse_prefix(argv[argi], &prefix)) { fprintf(stderr, "murx: %s requires a letter from a through z\n", option); @@ -6327,36 +5147,7 @@ int main(int argc, char **argv) { make_socket_path(name, path, sizeof path); int fd = connect_session(path); - AdoptHandoff handoff; - memset(&handoff, 0, sizeof handoff); - if (fd >= 0 && adopt) { - if (request_adoption(fd, &handoff) == 0) { - int adopted_fd = spawn_server(path, name, &display, history_limit, - &handoff); - adopt_handoff_free(&handoff); - if (adopted_fd < 0) { - int err = errno; - close(fd); - if (err) { - fprintf(stderr, "murx: cannot hand off session '%s': %s\n", - name, strerror(err)); - } - return 1; - } - fd = adopted_fd; - } else { - int err = errno ? errno : EPROTO; - if (err == ETIMEDOUT) { - fprintf(stderr, - "murx: session '%s' runs a server without adoption support; " - "attaching normally\n", name); - } else { - fprintf(stderr, "murx: cannot adopt session '%s': %s; " - "attaching normally\n", name, strerror(err)); - } - } - } - if (fd < 0) fd = spawn_server(path, name, &display, history_limit, NULL); + if (fd < 0) fd = spawn_server(path, name, &display, history_limit); if (fd < 0) { if (errno != 0) { fprintf(stderr, "murx: cannot start or attach session '%s': %s\n", @@ -6364,5 +5155,5 @@ int main(int argc, char **argv) { } return 1; } - return run_client(fd, name, prefix, status, color, resize_mode); + return run_client(fd, name, prefix, status, color); } diff --git a/os/alpine/APKBUILD b/os/alpine/APKBUILD index 996287e..9c329a8 100644 --- a/os/alpine/APKBUILD +++ b/os/alpine/APKBUILD @@ -1,6 +1,6 @@ # Local source snapshot; refresh version and checksums with `make pkgsums`. pkgname=murx -pkgver=1.0.4 +pkgver=1.0.3 pkgrel=0 pkgdesc="Small persistent terminal multiplexer" url="https://codeberg.org/armin/murx" @@ -26,10 +26,10 @@ package() { } sha512sums=" -fb4a217160f7816b03adb76272a6c477d106d4b5d91a404f3041b579910c233df9fcb84dbef358219e1b9df783a59729458c9418b0277e257661a9d5a5bb7300 murx.c +68c0e952a57d03e5c2cc6a7451cca1687341c20620634617529b6dc84f0fcdc7cb49cbc662bfad5b1fe3229c082a61480813ed5c01a1994c1b2a8e34bd421e29 murx.c ac75bfe596fae665ba882a10f5d16551bdd02cd601b786364763ac74dbc651c386d3a464eb63e0344354b953611629135190633e3b8ff3a8ef0f0849d60a3f7b Makefile -16a542970423f761175da657bc54d37c089197cc3428397c2de51a312280c751b03af70ce38cf5b3fea9e0b2ed006d5b892e54ddbc625806e05e41a726b18225 murx.1 -1ce08e99e30b85347bf8f228525440887e083f9cc2175f951693fa2dad9357870fa587ffc1ee98a60edad957468f2b8b8af96ac3bf6ddd38df13deceb429f381 MURX-README.md +219082e6277ee8c226cdd61a5b17d4e3c6893adf04b2f59ea84a6b6421dbd5a6d0ff38b93d8dd8ae41ed037e77252058dacadd5ff1f0c36c7c1ff95503e1e7a8 murx.1 +1b98d82dc3c41c376827cfd3883ee8918f24e82eab138fbb0e62bc5c2b0ba9498eb3abe59dc96b3f3d67a20fbdb331ed113d1898297c4f3c904e6341364cfdc9 MURX-README.md 2fe33ba22c5f68e124904be594208f050be20f33d115f9a56f60cac93304102da32e6a7e78154e831d3704da7ca8bfefd7025e7b8901e65babcd83e7342f6382 LICENSE 4d32dba475e5f9818fcd008068b011cbae9fc4b8e29897c933775d6660f44a7992d08a1550320d83233af729a42b241ef1d182bf5983ef2f64deab76cab2260d AUTHORS " diff --git a/os/arch/PKGBUILD b/os/arch/PKGBUILD index f26783b..a036fb1 100644 --- a/os/arch/PKGBUILD +++ b/os/arch/PKGBUILD @@ -1,6 +1,6 @@ # Local source snapshot; refresh version and checksums with `make pkgsums`. pkgname=murx -pkgver=1.0.4 +pkgver=1.0.3 pkgrel=1 pkgdesc='Small persistent terminal multiplexer' arch=('x86_64' 'aarch64') @@ -8,10 +8,10 @@ url='https://codeberg.org/armin/murx' license=('Apache-2.0') depends=('glibc') source=('murx.c' 'Makefile' 'murx.1' 'MURX-README.md' 'LICENSE' 'AUTHORS') -sha256sums=('ab974ac7c5aab39c60d8b1c4e13974bd25d4cee3a228a821dc4f83cafc0d2e0d' +sha256sums=('358d39eed410a9657ef811892288ef407fe8f17709cb1b4b5e0405635594b70d' '78a9c19c212e8038422e1bb4bb96d9b25eb19ad1359489cd32edf9e1a76638b2' - '00496e2c82881dfc10e70d64ebcfde94f25bd3227c1481fa29ce564c81b55c2d' - '615dffa59389202bf7efb1e2a0b672e1e6b1801e1289f4f68728c9dfe98d4ea2' + '953966bfdd0b49cb26c11220e1e2bf6b5862aaa1b2d0bf002cde9d276f0ae96b' + '1a9c479723f74cdc3a2af6cf7a4ad52388681b11a5ba3f4cfb73d28af631e036' '31c5556be4cdd9bf52bb55f18ead0c68a4f0e793c2c2dab0a2a2c6d92407d0d4' 'dbc8a4fc569d4cd7bcd45516ccc98c1396fce0682c262103069c8f5150538cdb') diff --git a/os/debian/changelog b/os/debian/changelog index c7c15ff..4ee4e82 100644 --- a/os/debian/changelog +++ b/os/debian/changelog @@ -1,9 +1,3 @@ -murx (1.0.4-1) unstable; urgency=medium - - * Update to upstream version 1.0.4. - - -- Johannes Findeisen Tue, 24 Nov 2026 10:40:26 +0100 - murx (1.0.3-1) unstable; urgency=medium * Add Debian packaging for murx 1.0.3. diff --git a/os/freebsd/sysutils/murx/Makefile b/os/freebsd/sysutils/murx/Makefile index 01888b1..9c56547 100644 --- a/os/freebsd/sysutils/murx/Makefile +++ b/os/freebsd/sysutils/murx/Makefile @@ -1,5 +1,5 @@ PORTNAME= murx -DISTVERSION= 1.0.4 +DISTVERSION= 1.0.3 CATEGORIES= sysutils MASTER_SITES= https://xw3.org/murx/murx/archive/ diff --git a/os/openbsd/sysutils/murx/Makefile b/os/openbsd/sysutils/murx/Makefile index 3d66363..4d342b6 100644 --- a/os/openbsd/sysutils/murx/Makefile +++ b/os/openbsd/sysutils/murx/Makefile @@ -1,6 +1,6 @@ COMMENT = small persistent terminal multiplexer -DISTNAME = murx-1.0.4 +DISTNAME = murx-1.0.3 CATEGORIES = sysutils HOMEPAGE = https://codeberg.org/armin/murx MAINTAINER = Johannes Findeisen