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
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
// 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_::*;
use std::ffi::{CStr, CString};
pub type environ = rb_head<environ_entry>;
RB_GENERATE!(environ, environ_entry, entry, discr_entry, environ_cmp);
impl environ_entry {
/// Borrowed `char *` to the (always present) name.
#[inline]
pub(crate) fn name_ptr(&self) -> *const u8 {
self.name.as_ptr().cast()
}
/// Borrowed `char *` to the value, or NULL for a cleared entry — matches
/// C, where `envent->value` is `NULL` after `environ_clear`.
#[inline]
pub(crate) fn value_ptr(&self) -> *const u8 {
match &self.value {
Some(v) => v.as_ptr().cast(),
None => std::ptr::null(),
}
}
}
/// 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(envent1.name_ptr(), envent2.name_ptr())) }
}
/// 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);
// Reclaim the boxed node; its `CString` fields drop with it.
drop(Box::from_raw(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.as_deref() {
environ_set!(
dstenv,
(*envent).name_ptr(),
(*envent).flags,
"{}",
_s(value.as_ptr().cast::<u8>()),
);
} else {
environ_clear(dstenv, (*envent).name_ptr());
}
}
}
}
/// 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 {
// Search by a borrowed key (no throwaway node): `strcmp(name, node.name)`
// gives the key-vs-node ordering `rb_find_by` walks the tree with.
unsafe { rb_find_by(env, |n: &environ_entry| i32_to_ordering(libc::strcmp(name, n.name_ptr()))) }
}
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 envent = environ_find(env, name);
let value = crate::cstring_truncating(args.to_string());
if !envent.is_null() {
(*envent).flags = flags;
// Assigning the new value drops the old `CString` — no manual free.
(*envent).value = Some(value);
} else {
let envent = Box::into_raw(Box::new(environ_entry {
name: CStr::from_ptr(name.cast()).to_owned(),
value: Some(value),
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 envent = environ_find(env, name);
if !envent.is_null() {
// Dropping the old value (setting None) frees it — no manual free.
(*envent).value = None;
} else {
let envent = Box::into_raw(Box::new(environ_entry {
name: CStr::from_ptr(name.cast()).to_owned(),
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 value = libc::strchr(var, b'=' as c_int);
if value.is_null() {
return;
}
// Name is the prefix before the first '=' (strcspn stops at '=' or
// NUL, so the slice never contains NUL — CString::new cannot fail).
let n = libc::strcspn(var, c!("="));
let name = CString::new(std::slice::from_raw_parts(var, n)).unwrap();
environ_set!(env, name.as_ptr().cast::<u8>(), flags, "{}", _s(value.add(1)));
}
}
/// 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);
// Reclaim the boxed node; its `CString` fields drop with it.
drop(Box::from_raw(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, (*envent).name_ptr(), 0) == 0 {
environ_set!(
dst,
(*envent).name_ptr(),
environ_flags::empty(),
"{}",
_s((*envent).value_ptr()),
);
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_ptr() != b'\0'
&& !(*envent).flags.intersects(ENVIRON_HIDDEN)
{
// Called only in the forked child before exec (job.rs, spawn.rs).
// Use libc setenv (as upstream tmux does) rather than
// std::env::set_var, which takes std's ENV_LOCK - a lock that is
// not reset across fork() and would deadlock/abort the child.
::libc::setenv(
(*envent).name_ptr().cast(),
(*envent).value_ptr().cast(),
1,
);
}
}
}
}
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_ptr() != b'\0' {
log_debug!(
"{}{}={}",
prefix,
_s((*envent).name_ptr()),
_s((*envent).value_ptr())
);
}
}
}
}
/// 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 };
// Advertise the server to panes via $TMUX, pointing at ztmux's own
// socket. The wider ecosystem (powerline, tpm, prompts, zpwr scripts)
// detects a multiplexer by $TMUX being set, so we keep that name and
// never introduce a $ZTMUX variable. ztmux never adopts this socket for
// resolution (see tmux.rs), so it can never end up on a foreign tmux's
// socket even when nested inside real tmux.
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. The owned `CString` name drops with the entry.
unsafe fn make_entry(name: *const u8) -> environ_entry {
environ_entry {
name: unsafe { CStr::from_ptr(name.cast()).to_owned() },
value: None,
flags: environ_flags::empty(),
entry: rb_entry::default(),
}
}
// 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((*e).value_ptr()).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((*e).name_ptr()).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);
// a/b/a2 drop here, freeing their owned names.
}
}
// environ_copy replicates a cleared (value==NULL) src entry as a cleared
// dst entry via environ_clear (environ.c:92-95), not as a value.
#[test]
fn copy_propagates_cleared_entries() {
unsafe {
let src = environ_create().as_ptr();
let dst = environ_create().as_ptr();
environ_set!(src, crate::c!("KEEP"), environ_flags::empty(), "{}", "v");
environ_clear(src, crate::c!("GONE")); // present but value NULL
environ_copy(src, dst);
assert_eq!(value_of(dst, crate::c!("KEEP")).as_deref(), Some("v"));
// GONE exists in dst but with a NULL value.
let e = environ_find(dst, crate::c!("GONE"));
assert!(!e.is_null());
assert!((*e).value.is_none());
environ_free(src);
environ_free(dst);
}
}
// environ_put with "NAME=" sets an empty-string value (environ.c:158-160:
// value is the char after '='), distinct from a cleared entry.
#[test]
fn put_empty_value_is_empty_string_not_null() {
unsafe {
let env = environ_create().as_ptr();
environ_put(env, crate::c!("EMPTY="), environ_flags::empty());
let e = environ_find(env, crate::c!("EMPTY"));
assert!(!e.is_null());
// Value present but empty (Some("")), not cleared (None).
assert_eq!(value_of(env, crate::c!("EMPTY")).as_deref(), Some(""));
environ_free(env);
}
}
// environ_put splits on the FIRST '=' (environ.c:157-159: strcspn to the
// first '='), so "K=a=b" -> name "K", value "a=b".
#[test]
fn put_splits_on_first_equals_only() {
unsafe {
let env = environ_create().as_ptr();
environ_put(env, crate::c!("K=a=b"), environ_flags::empty());
assert_eq!(value_of(env, crate::c!("K")).as_deref(), Some("a=b"));
environ_free(env);
}
}
// environ_clear on an absent name inserts a present entry with a NULL value
// (environ.c:141-146): it shows in iteration but value_of is None.
#[test]
fn clear_absent_inserts_null_entry() {
unsafe {
let env = environ_create().as_ptr();
assert!(environ_find(env, crate::c!("NEW")).is_null());
environ_clear(env, crate::c!("NEW"));
let e = environ_find(env, crate::c!("NEW"));
assert!(!e.is_null());
assert!((*e).value.is_none());
assert_eq!(names_in_order(env), vec!["NEW".to_string()]);
environ_free(env);
}
}
// environ_set records the flags on the entry, and overwriting a name updates
// its flags too (environ.c:113-116: existing branch assigns envent->flags).
#[test]
fn set_records_and_updates_flags() {
unsafe {
let env = environ_create().as_ptr();
environ_set!(env, crate::c!("H"), ENVIRON_HIDDEN, "{}", "1");
let e = environ_find(env, crate::c!("H"));
assert!((*e).flags.intersects(ENVIRON_HIDDEN));
// Overwrite with empty flags clears the flag on the same entry.
environ_set!(env, crate::c!("H"), environ_flags::empty(), "{}", "2");
let e = environ_find(env, crate::c!("H"));
assert!(!(*e).flags.intersects(ENVIRON_HIDDEN));
assert_eq!(value_of(env, crate::c!("H")).as_deref(), Some("2"));
environ_free(env);
}
}
// environ_unset on an absent name returns early with no insertion
// (environ.c:172-176).
#[test]
fn unset_absent_is_noop() {
unsafe {
let env = environ_create().as_ptr();
environ_unset(env, crate::c!("NOPE"));
assert!(names_in_order(env).is_empty());
environ_free(env);
}
}
}