ztmux 0.1.0

A Rust port of tmux — the full terminal multiplexer, server and client
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
// Copyright (c) 2009 Nicholas Marriott <nicholas.marriott@gmail.com>
//
// Permission to use, copy, modify, and distribute this software for any
// purpose with or without fee is hereby granted, provided that the above
// copyright notice and this permission notice appear in all copies.
//
// THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
// WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
// MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
// ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
// WHATSOEVER RESULTING FROM LOSS OF MIND, USE, DATA OR PROFITS, WHETHER
// IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING
// OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
use crate::*;
use crate::options_::*;

pub type environ = rb_head<environ_entry>;
RB_GENERATE!(environ, environ_entry, entry, discr_entry, environ_cmp);

/// C `vendor/tmux/environ.c:37`: `static int environ_cmp(struct environ_entry *envent1, struct environ_entry *envent2)`
pub fn environ_cmp(envent1: &environ_entry, envent2: &environ_entry) -> std::cmp::Ordering {
    unsafe {
        i32_to_ordering(libc::strcmp(
            transmute_ptr(envent1.name),
            transmute_ptr(envent2.name),
        ))
    }
}

/// C `vendor/tmux/environ.c:44`: `struct environ *environ_create(void)`
pub fn environ_create() -> NonNull<environ> {
    unsafe {
        let env = xcalloc1::<environ>();
        rb_init(env);
        NonNull::new_unchecked(env)
    }
}

/// C `vendor/tmux/environ.c:56`: `void environ_free(struct environ *env)`
pub unsafe fn environ_free(env: *mut environ) {
    unsafe {
        for envent in rb_foreach(env).map(NonNull::as_ptr) {
            rb_remove(env, envent);
            free_(transmute_ptr((*envent).name));
            free_(transmute_ptr((*envent).value));
            free_(envent);
        }
        free_(env);
    }
}

/// C `vendor/tmux/environ.c:73`: `struct environ_entry *environ_first(struct environ *env)`
pub unsafe fn environ_first(env: *mut environ) -> *mut environ_entry {
    unsafe { rb_min(env) }
}

/// C `vendor/tmux/environ.c:79`: `struct environ_entry *environ_next(struct environ_entry *envent)`
pub unsafe fn environ_next(envent: *mut environ_entry) -> *mut environ_entry {
    unsafe { rb_next(envent) }
}

/// C `vendor/tmux/environ.c:86`: `void environ_copy(struct environ *srcenv, struct environ *dstenv)`
pub unsafe fn environ_copy(srcenv: *mut environ, dstenv: *mut environ) {
    unsafe {
        for envent in rb_foreach(srcenv).map(NonNull::as_ptr) {
            if let Some(value) = (*envent).value {
                environ_set!(
                    dstenv,
                    (*envent).name.unwrap().as_ptr(),
                    (*envent).flags,
                    "{}",
                    _s(value.as_ptr()),
                );
            } else {
                environ_clear(dstenv, transmute_ptr((*envent).name));
            }
        }
    }
}

/// C `vendor/tmux/environ.c:102`: `struct environ_entry *environ_find(struct environ *env, const char *name)`
pub unsafe fn environ_find(env: *mut environ, name: *const u8) -> *mut environ_entry {
    let mut envent: MaybeUninit<environ_entry> = MaybeUninit::uninit();
    let envent = envent.as_mut_ptr();

    unsafe {
        (*envent).name = NonNull::new(name.cast_mut());
        // std::ptr::write(&raw mut (*envent).name, name);
    }

    unsafe { rb_find(env, envent) }
}

macro_rules! environ_set {
   ($env:expr, $name:expr, $flags:expr, $fmt:literal $(, $args:expr)* $(,)?) => {
        crate::environ_::environ_set_($env, $name, $flags, format_args!($fmt $(, $args)*))
    };
}
pub(crate) use environ_set;
pub unsafe fn environ_set_(
    env: *mut environ,
    name: *const u8,
    flags: environ_flags,
    args: std::fmt::Arguments,
) {
    unsafe {
        let mut envent = environ_find(env, name);
        let mut s = args.to_string();
        s.push('\0');
        let s = NonNull::new(s.leak().as_mut_ptr().cast());

        if !envent.is_null() {
            (*envent).flags = flags;
            free_(transmute_ptr((*envent).value));
            (*envent).value = s;
        } else {
            envent = Box::leak(Box::new(environ_entry {
                name : Some(xstrdup(name).cast()),
                value: s,
                flags,
                entry: rb_entry::default(),
            }));
            rb_insert(env, envent);
        }
    }
}

/// C `vendor/tmux/environ.c:135`: `void environ_clear(struct environ *env, const char *name)`
pub unsafe fn environ_clear(env: *mut environ, name: *const u8) {
    unsafe {
        let mut envent = environ_find(env, name);
        if !envent.is_null() {
            free_(transmute_ptr((*envent).value));
            (*envent).value = None;
        } else {
            envent = Box::leak(Box::new(environ_entry {
                name : Some(xstrdup(name).cast()),
                value: None,
                flags: environ_flags::empty(),
                entry: rb_entry::default(),
            }));
            rb_insert(env, envent);
        }
    }
}

/// C `vendor/tmux/environ.c:153`: `void environ_put(struct environ *env, const char *var, int flags)`
pub unsafe fn environ_put(env: *mut environ, var: *const u8, flags: environ_flags) {
    unsafe {
        let mut value = libc::strchr(var, b'=' as c_int);
        if value.is_null() {
            return;
        }
        value = value.add(1);

        let name: *mut u8 = xstrdup(var).cast().as_ptr();
        *name.add(libc::strcspn(name, c!("="))) = b'\0';

        environ_set!(env, name, flags, "{}", _s(value));
        free_(name);
    }
}

/// C `vendor/tmux/environ.c:172`: `void environ_unset(struct environ *env, const char *name)`
pub unsafe fn environ_unset(env: *mut environ, name: *const u8) {
    unsafe {
        let envent = environ_find(env, name);
        if envent.is_null() {
            return;
        }
        rb_remove(env, envent);
        free_(transmute_ptr((*envent).name));
        free_(transmute_ptr((*envent).value));
        free_(envent);
    }
}

/// C `vendor/tmux/environ.c:186`: `void environ_update(struct options *oo, struct environ *src, struct environ *dst)`
pub unsafe fn environ_update(oo: *mut options, src: *mut environ, dst: *mut environ) {
    unsafe {
        let mut found;

        let o = options_get(&mut *oo, "update-environment");
        if o.is_null() {
            return;
        }
        let mut a = options_array_first(o);
        while !a.is_null() {
            let ov = options_array_item_value(a);
            found = false;
            for envent in rb_foreach(src).map(NonNull::as_ptr) {
                if libc::fnmatch((*ov).string, transmute_ptr((*envent).name), 0) == 0 {
                    environ_set!(
                        dst,
                        transmute_ptr((*envent).name),
                        environ_flags::empty(),
                        "{}",
                        _s(transmute_ptr((*envent).value)),
                    );
                    found = true;
                }
            }
            if !found {
                environ_clear(dst, (*ov).string);
            }
            a = options_array_next(a);
        }
    }
}

/// C `vendor/tmux/environ.c:216`: `void environ_push(struct environ *env)`
pub unsafe fn environ_push(env: *mut environ) {
    unsafe {
        environ = xcalloc_::<*mut u8>(1).as_ptr();
        for envent in rb_foreach(env).map(NonNull::as_ptr) {
            if (*envent).value.is_some()
                && *(*envent).name.unwrap().as_ptr() != b'\0'
                && !(*envent).flags.intersects(ENVIRON_HIDDEN)
            {
                std::env::set_var(
                    cstr_to_str(transmute_ptr((*envent).name)),
                    cstr_to_str(transmute_ptr((*envent).value)),
                );
            }
        }
    }
}

macro_rules! environ_log {
   ($env:expr, $fmt:literal $(, $args:expr)* $(,)?) => {
        crate::environ_::environ_log_($env, format_args!($fmt $(, $args)*))
    };
}
pub(crate) use environ_log;

pub unsafe fn environ_log_(env: *mut environ, args: std::fmt::Arguments) {
    unsafe {
        let prefix = args.to_string();

        for envent in rb_foreach(env).map(NonNull::as_ptr) {
            if (*envent).value.is_some() && *(*envent).name.unwrap().as_ptr() != b'\0' {
                log_debug!(
                    "{}{}={}",
                    prefix,
                    _s(transmute_ptr((*envent).name)),
                    _s(transmute_ptr((*envent).value))
                );
            }
        }
    }
}

/// C `vendor/tmux/environ.c:253`: `struct environ *environ_for_session(struct session *s, int no_TERM)`
pub unsafe fn environ_for_session(s: *mut session, no_term: c_int) -> *mut environ {
    let env: *mut environ = environ_create().as_ptr();

    unsafe {
        environ_copy(GLOBAL_ENVIRON, env);
        if !s.is_null() {
            environ_copy((*s).environ, env);
        }

        if no_term == 0 {
            let value = options_get_string_(GLOBAL_OPTIONS, "default-terminal");
            environ_set!(env, c!("TERM"), environ_flags::empty(), "{}", _s(value));
            environ_set!(
                env,
                c!("TERM_PROGRAM"),
                environ_flags::empty(),
                "{}",
                "tmux"
            );
            environ_set!(
                env,
                c!("TERM_PROGRAM_VERSION"),
                environ_flags::empty(),
                "{}",
                getversion()
            );
        }

        #[cfg(feature = "systemd")]
        {
            environ_clear(env, c!("LISTEN_PID"));
            environ_clear(env, c!("LISTEN_FDS"));
            environ_clear(env, c!("LISTEN_FDNAMES"));
        }

        let idx = if !s.is_null() { (*s).id as i32 } else { -1 };

        environ_set!(
            env,
            c!("TMUX"),
            environ_flags::empty(),
            "{},{},{}",
            _s(SOCKET_PATH),
            std::process::id(),
            idx,
        );

        env
    }
}

#[cfg(test)]
mod tests {
    use super::*;

    // Build a throwaway entry keyed by `name` (value unset) so environ_cmp can be
    // exercised directly. Free with `free_entry`.
    unsafe fn make_entry(name: *const u8) -> environ_entry {
        environ_entry {
            name: Some(unsafe { xstrdup(name) }),
            value: None,
            flags: environ_flags::empty(),
            entry: rb_entry::default(),
        }
    }

    unsafe fn free_entry(e: &environ_entry) {
        unsafe {
            free_(transmute_ptr(e.name));
            free_(transmute_ptr(e.value));
        }
    }

    // Return the string value of `name` in `env`, or None if the entry is absent
    // or present-but-cleared (value == NULL).
    unsafe fn value_of(env: *mut environ, name: *const u8) -> Option<String> {
        unsafe {
            let e = environ_find(env, name);
            if e.is_null() || (*e).value.is_none() {
                None
            } else {
                Some(cstr_to_str(transmute_ptr((*e).value)).to_string())
            }
        }
    }

    // Collect the names of every entry in tree order (environ_first + environ_next).
    unsafe fn names_in_order(env: *mut environ) -> Vec<String> {
        unsafe {
            let mut out = Vec::new();
            let mut e = environ_first(env);
            while !e.is_null() {
                out.push(cstr_to_str(transmute_ptr((*e).name)).to_string());
                e = environ_next(e);
            }
            out
        }
    }

    #[test]
    fn set_then_find_returns_value() {
        unsafe {
            let env = environ_create().as_ptr();
            assert!(environ_find(env, crate::c!("FOO")).is_null());

            environ_set!(env, crate::c!("FOO"), environ_flags::empty(), "{}", "bar");

            let e = environ_find(env, crate::c!("FOO"));
            assert!(!e.is_null());
            assert_eq!(value_of(env, crate::c!("FOO")).as_deref(), Some("bar"));

            environ_free(env);
        }
    }

    #[test]
    fn set_existing_name_overwrites_value() {
        unsafe {
            let env = environ_create().as_ptr();
            environ_set!(env, crate::c!("FOO"), environ_flags::empty(), "{}", "one");
            environ_set!(env, crate::c!("FOO"), environ_flags::empty(), "{}", "two");

            assert_eq!(value_of(env, crate::c!("FOO")).as_deref(), Some("two"));
            // Overwrite must not create a second entry.
            assert_eq!(names_in_order(env), vec!["FOO".to_string()]);

            environ_free(env);
        }
    }

    #[test]
    fn put_parses_name_equals_value() {
        unsafe {
            let env = environ_create().as_ptr();
            environ_put(env, crate::c!("PATH=/usr/bin"), environ_flags::empty());

            assert_eq!(value_of(env, crate::c!("PATH")).as_deref(), Some("/usr/bin"));

            // A string with no '=' is ignored entirely.
            environ_put(env, crate::c!("NOEQUALS"), environ_flags::empty());
            assert!(environ_find(env, crate::c!("NOEQUALS")).is_null());

            environ_free(env);
        }
    }

    #[test]
    fn next_iterates_in_sorted_order() {
        unsafe {
            let env = environ_create().as_ptr();
            // Insert out of order; the tree is keyed by environ_cmp (strcmp).
            environ_set!(env, crate::c!("CHARLIE"), environ_flags::empty(), "{}", "3");
            environ_set!(env, crate::c!("ALPHA"), environ_flags::empty(), "{}", "1");
            environ_set!(env, crate::c!("BRAVO"), environ_flags::empty(), "{}", "2");

            assert_eq!(
                names_in_order(env),
                vec!["ALPHA".to_string(), "BRAVO".to_string(), "CHARLIE".to_string()]
            );

            environ_free(env);
        }
    }

    #[test]
    fn clear_keeps_entry_but_nulls_value() {
        unsafe {
            let env = environ_create().as_ptr();
            environ_set!(env, crate::c!("FOO"), environ_flags::empty(), "{}", "bar");

            environ_clear(env, crate::c!("FOO"));

            // environ_clear keeps the entry present but with a NULL value.
            let e = environ_find(env, crate::c!("FOO"));
            assert!(!e.is_null());
            assert!((*e).value.is_none());

            environ_free(env);
        }
    }

    #[test]
    fn unset_removes_entry() {
        unsafe {
            let env = environ_create().as_ptr();
            environ_set!(env, crate::c!("FOO"), environ_flags::empty(), "{}", "bar");
            assert!(!environ_find(env, crate::c!("FOO")).is_null());

            environ_unset(env, crate::c!("FOO"));

            assert!(environ_find(env, crate::c!("FOO")).is_null());
            assert!(names_in_order(env).is_empty());

            environ_free(env);
        }
    }

    #[test]
    fn copy_duplicates_entries() {
        unsafe {
            let src = environ_create().as_ptr();
            let dst = environ_create().as_ptr();
            environ_set!(src, crate::c!("FOO"), environ_flags::empty(), "{}", "bar");
            environ_set!(src, crate::c!("BAZ"), environ_flags::empty(), "{}", "qux");

            environ_copy(src, dst);

            assert_eq!(value_of(dst, crate::c!("FOO")).as_deref(), Some("bar"));
            assert_eq!(value_of(dst, crate::c!("BAZ")).as_deref(), Some("qux"));

            // The copies are independent allocations: mutating dst leaves src intact.
            environ_set!(dst, crate::c!("FOO"), environ_flags::empty(), "{}", "changed");
            assert_eq!(value_of(src, crate::c!("FOO")).as_deref(), Some("bar"));
            assert_eq!(value_of(dst, crate::c!("FOO")).as_deref(), Some("changed"));

            environ_free(src);
            environ_free(dst);
        }
    }

    #[test]
    fn cmp_orders_by_name() {
        unsafe {
            let a = make_entry(crate::c!("AAA"));
            let b = make_entry(crate::c!("BBB"));
            let a2 = make_entry(crate::c!("AAA"));

            assert_eq!(environ_cmp(&a, &b), std::cmp::Ordering::Less);
            assert_eq!(environ_cmp(&b, &a), std::cmp::Ordering::Greater);
            assert_eq!(environ_cmp(&a, &a2), std::cmp::Ordering::Equal);

            free_entry(&a);
            free_entry(&b);
            free_entry(&a2);
        }
    }
}