Repository navigation
Expand file tree
/
Copy pathsymbols.rs
More file actions
865 lines (752 loc) · 29.5 KB
/
Copy pathsymbols.rs
File metadata and controls
865 lines (752 loc) · 29.5 KB
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
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
use libc::{c_char, c_int, c_uint};
use std::fmt::Debug;
pub const EOF: c_int = -1;
pub const BUILTIN_ENABLED: c_int = 0x01;
pub const SEVAL_NOHIST: c_int = 0x004;
pub const SEVAL_NOOPTIMIZE: c_int = 0x400;
pub const SEVAL_NOTIFY: c_int = 0x800;
/* A structure which represents a word. */
// typedef struct word_desc {
// char *word; /* Zero terminated string. */
// int flags; /* Flags associated with this word. */
// } WORD_DESC;
#[repr(C)]
#[allow(dead_code)]
pub struct WordDesc {
pub word: *mut c_char, // Zero terminated string.
pub flags: c_int, // Flags associated with this word.
}
/* A linked list of words. */
// typedef struct word_list {
// struct word_list *next;
// WORD_DESC *word;
// } WORD_LIST;
#[repr(C)]
#[allow(dead_code)]
pub struct WordList {
pub next: *mut WordList,
pub word: *mut WordDesc,
}
pub type BashBuiltinCallFunc = extern "C" fn(*const WordList) -> c_int;
/* The thing that we build the array of builtins out of. */
// struct builtin {
// char *name; /* The name that the user types. */
// sh_builtin_func_t *function; /* The address of the invoked function. */
// int flags; /* One of the #defines above. */
// char * const *long_doc; /* NULL terminated array of strings. */
// const char *short_doc; /* Short version of documentation. */
// char *handle; /* for future use */
// };
#[repr(C)]
#[allow(dead_code)]
pub struct BashBuiltin {
pub name: *const c_char, // The name that the user types.
pub function: Option<BashBuiltinCallFunc>, // The address of the invoked function.
pub flags: c_int, // One of the #defines above.
pub long_doc: *const *const c_char, // NULL terminated array of strings.
pub short_doc: *const c_char, // Short version of documentation.
pub handle: *const c_char, // for future use
}
// shell.h
#[allow(unused_imports)]
pub use crate::shell::BuiltinExitCode;
// Bash input stream types from bash's input.h
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
#[repr(C)]
#[allow(dead_code)]
pub enum StreamType {
None = 0,
Stdin = 1,
Stream = 2,
String = 3,
BStream = 4,
}
// INPUT_STREAM union from bash
#[repr(C)]
pub union InputStreamLocation {
pub string: *mut c_char,
_file: *mut libc::c_void, // FILE* - we don't use this
_buffered_fd: c_int, // for bstream - we don't use this
}
// BUFFERED_STREAM from bash's input.h (opaque pointer for our purposes)
#[repr(C)]
#[allow(dead_code)]
pub struct BufferedStream {
_private: [u8; 0],
}
// sh_cget_func_t and sh_cunget_func_t are function pointer types
#[allow(dead_code)]
pub type ShCGetFunc = unsafe extern "C" fn() -> c_int;
#[allow(dead_code)]
pub type ShCUngetFunc = unsafe extern "C" fn(c_int) -> c_int;
// BASH_INPUT structure from bash's input.h
#[repr(C)]
#[allow(dead_code)]
pub struct BashInput {
pub stream_type: StreamType,
pub name: *mut c_char,
pub location: InputStreamLocation,
pub getter: Option<ShCGetFunc>,
pub ungetter: Option<ShCUngetFunc>,
}
// STREAM_SAVER structure from bash's y.tab.c
#[repr(C)]
#[allow(dead_code)]
pub struct StreamSaver {
pub next: *mut StreamSaver,
pub bash_input: BashInput,
pub line: c_int,
pub bstream: *mut BufferedStream,
}
// hashlib.h BUCKET_CONTENTS
#[repr(C)]
#[allow(dead_code)]
pub struct BucketContents {
pub next: *mut BucketContents,
pub key: *const c_char,
pub data: *mut libc::c_void,
pub khash: c_uint,
pub times_found: c_int,
}
// hashlib.h HASH_TABLE
#[repr(C)]
#[allow(dead_code)]
pub struct HashTable {
pub bucket_array: *mut *mut BucketContents,
pub nbuckets: c_int,
pub nentries: c_int,
}
// builtins/common.h
// /* Flags for describe_command, shared between type.def and command.def */
#[allow(dead_code)]
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum CDescFlag {
All = 0x001, // CDESC_ALL - type -a
ShortDesc = 0x002, // CDESC_SHORTDESC - command -V
Reusable = 0x004, // CDESC_REUSABLE - command -v
Type = 0x008, // CDESC_TYPE - type -t
PathOnly = 0x010, // CDESC_PATH_ONLY - type -p
ForcePath = 0x020, // CDESC_FORCE_PATH - type -ap or type -P
NoFuncs = 0x040, // CDESC_NOFUNCS - type -f
AbsPath = 0x080, // CDESC_ABSPATH - convert to absolute path, no ./
StdPath = 0x100, // CDESC_STDPATH - command -p
}
#[cfg(not(test))]
#[allow(dead_code)]
unsafe extern "C" {
// stream_list global from y.tab.c
#[link_name = "stream_list"]
pub static mut stream_list: *mut StreamSaver;
// input.h
// extern BASH_INPUT bash_input;
#[link_name = "bash_input"]
pub static mut bash_input: BashInput;
// input.h
// void push_stream (int reset_lineno)
pub fn push_stream(reset_lineno: c_int);
// input.h
// void pop_stream (void)
pub fn pop_stream();
// from shell.h
pub static interactive: c_int;
pub static interactive_shell: c_int;
pub static autocd: c_int;
pub fn save_parser_state(ps: *mut libc::c_void) -> *mut libc::c_void;
pub fn restore_parser_state(ps: *mut libc::c_void);
// from shell.h
pub static no_line_editing: c_int;
// y.tab.c
// void with_input_from_stdin (void)
pub fn with_input_from_stdin();
// alias.h
/* Return the value of the alias for NAME, or NULL if there is none. */
// extern char *get_alias_value (const char *);
pub fn get_alias_value(name: *const c_char) -> *mut c_char;
// variables.h
// extern FUNCTION_DEF *find_function_def (const char *);
pub fn find_function_def(name: *const c_char) -> *mut FunctionDef;
// from type.def
// int describe_command (char *command, int dflags)
pub fn describe_command(command: *const c_char, dflags: c_int) -> c_int;
// from pcomplete.c
/* The driver function for the programmable completion code. Returns a list
of matches for WORD, which is an argument to command CMD. START and END
bound the command currently being completed in pcomp_line (usually
rl_line_buffer). */
// char ** programmable_completions (const char *cmd, const char *word, int start, int end, int *foundp)
pub fn programmable_completions(
cmd: *const c_char,
word: *const c_char,
start: c_int,
end: c_int,
foundp: *mut c_int,
) -> *mut *mut c_char;
// from pcomplete.c
// COMPSPEC *progcomp_search (const char *)
pub fn progcomp_search(cmd: *const c_char) -> *mut CompSpec;
// from bashline.c
// char ** bash_default_completion (const char *text, int start, int end, int qc, int compflags)
pub fn bash_default_completion(
text: *const c_char,
start: c_int,
end: c_int,
qc: c_int,
compflags: c_int,
) -> *mut *mut c_char;
// from readline/readline.h
// Line buffer and maintenance
// char *rl_line_buffer
#[link_name = "rl_line_buffer"]
pub static mut rl_line_buffer: *mut c_char;
/* The location of point, and end. */
// extern int rl_point;
#[link_name = "rl_point"]
pub static mut rl_point: c_int;
// extern int rl_end;
#[link_name = "rl_end"]
pub static mut rl_end: c_int;
/* Set to a non-zero value if readline found quoting anywhere in the word to
be completed; set before any application completion function is called. */
// extern int rl_completion_found_quote;
#[link_name = "rl_completion_found_quote"]
pub static mut rl_completion_found_quote: c_int;
/* Set to any quote character readline thinks it finds before any application
completion function is called. */
// extern int rl_completion_quote_character;
#[link_name = "rl_completion_quote_character"]
pub static mut rl_completion_quote_character: c_int;
/* Non-zero means that the results of the matches are to be quoted using
double quotes (or an application-specific quoting mechanism) if the
filename contains any characters in rl_filename_quote_chars. This is
ALWAYS non-zero on entry, and can only be changed within a completion
entry finder function. */
// complete.c
#[link_name = "rl_filename_quoting_desired"]
pub static mut rl_filename_quoting_desired: c_int;
// Only in more recent versions of readline / bash so not using it.
/* Non-zero means we should apply filename-type quoting to all completions
even if we are not otherwise treating the matches as filenames. This is
ALWAYS zero on entry, and can only be changed within a completion entry
finder function. */
// int rl_full_quoting_desired = 0;
// #[link_name = "rl_full_quoting_desired"]
// pub static mut rl_full_quoting_desired: c_int;
/* Non-zero means that the results of the matches are to be treated
as filenames. This is ALWAYS zero on entry, and can only be changed
within a completion entry finder function. */
// int rl_filename_completion_desired = 0;
#[link_name = "rl_filename_completion_desired"]
pub static mut rl_filename_completion_desired: c_int;
// This isn't set by any completion script.
// Bash sets it in command_subst_completion_function but I can't figure out how to trigger that.
/* If non-zero, the completion functions don't append any closing quote.
This is set to 0 by rl_complete_internal and may be changed by an
application-specific completion function. */
// complete.c
// int rl_completion_suppress_quote = 0;
// #[link_name = "rl_completion_suppress_quote"]
// pub static mut rl_completion_suppress_quote: c_int;
/* If non-zero, the completion functions don't append anything except a
possible closing quote. This is set to 0 by rl_complete_internal and
may be changed by an application-specific completion function. */
// int rl_completion_suppress_append = 0;
#[link_name = "rl_completion_suppress_append"]
pub static mut rl_completion_suppress_append: c_int;
/* Character appended to completed words when at the end of the line. The
default is a space. */
// int rl_completion_append_character = ' ';
#[link_name = "rl_completion_append_character"]
pub static mut rl_completion_append_character: c_int;
/* If non-zero, sort the completion matches. On by default. */
// int rl_sort_completion_matches = 1;
#[link_name = "rl_sort_completion_matches"]
pub static mut rl_sort_completion_matches: c_int;
// typedef char *rl_dequote_func_t (char *, int);
// rl_dequote_func_t *rl_filename_dequoting_function
pub static mut rl_filename_dequoting_function:
Option<extern "C" fn(*const c_char, c_int) -> *mut c_char>;
// typedef char *rl_quote_func_t (char *, int, char *);
// rl_quote_func_t *rl_filename_quoting_function
pub static mut rl_filename_quoting_function:
Option<extern "C" fn(*const c_char, c_int, *const c_char) -> *mut c_char>;
// void pcomp_set_readline_variables (int flags, int nval)
pub fn pcomp_set_readline_variables(flags: c_int, nval: c_int);
// alias.h
// alias_t **all_aliases (void);
pub fn all_aliases() -> *mut *mut Alias;
// char **all_variables_matching_prefix (const char *prefix)
pub fn all_variables_matching_prefix(prefix: *const c_char) -> *mut *mut c_char;
// extern SHELL_VAR **all_shell_functions (void);
pub fn all_shell_functions() -> *mut *mut ShellVar;
#[link_name = "shell_functions"]
pub static mut shell_functions: *mut HashTable;
// extern struct builtin *shell_builtins;
#[link_name = "shell_builtins"]
pub static mut shell_builtins: *mut BashBuiltinType;
// num_shell_builtins
#[link_name = "num_shell_builtins"]
pub static mut num_shell_builtins: c_int;
//extern unsigned long rl_readline_state;
#[link_name = "rl_readline_state"]
pub static mut rl_readline_state: libc::c_ulong;
// int current_command_line_count;
#[link_name = "current_command_line_count"]
pub static mut current_command_line_count: c_int;
// extern HIST_ENTRY **history_list (void);
pub fn history_list() -> *mut *mut HistoryEntry;
// y.tab.c
// char *current_readline_prompt
#[link_name = "current_readline_prompt"]
pub static mut current_readline_prompt: *mut c_char;
// getenv.c
// char* getenv(const char* name);
pub fn getenv(name: *const c_char) -> *mut c_char;
// variables.h
// SHELL_VAR * find_variable (const char *name)
pub fn find_variable(name: *const c_char) -> *mut ShellVar;
// array.h / subst.c
// WORD_LIST * array_to_word_list (ARRAY *a)
pub fn array_to_word_list(array: *mut libc::c_void) -> *mut WordList;
// dispose_words (WORD_LIST *list)
pub fn dispose_words(list: *mut WordList);
/* Bind a variable NAME to VALUE. This conses up the name
and value strings. If we have a temporary environment, we bind there
first, then we bind into shell_variables. */
// SHELL_VAR * bind_variable ( const char *name, char *value, int flags;
pub fn bind_variable(name: *const c_char, value: *const c_char, flags: c_int) -> *mut ShellVar;
// int unbind_variable (name) const char *name;
pub fn unbind_variable(name: *const c_char) -> c_int;
// common.h
// int evalstring (char *string, const char *from_file, int flags)
pub fn evalstring(string: *mut c_char, from_file: *const c_char, flags: c_int) -> c_int;
// y.tab.c
// char * decode_prompt_string (char *string, int is_prompt)
pub fn decode_prompt_string(string: *const c_char, is_prompt: c_int) -> *mut c_char;
// char *expand_string_to_string (string, quoted)
pub fn expand_string_to_string(string: *const c_char, quoted: c_int) -> *mut c_char;
// xmalloc.h
fn xmalloc(size: libc::size_t) -> *mut libc::c_void;
fn xrealloc(ptr: *mut libc::c_void, size: libc::size_t) -> *mut libc::c_void;
fn xfree(ptr: *mut libc::c_void);
// Signal handling
/* Set to the value of any terminating signal received. */
// volatile sig_atomic_t terminating_signal = 0;
#[link_name = "terminating_signal"]
pub static mut terminating_signal: c_int;
// void termsig_handler (int sig)
pub fn termsig_handler(sig: c_int);
// int first_pending_trap (void)
pub fn first_pending_trap() -> c_int;
// void run_pending_traps (void)
pub fn run_pending_traps();
// rl_hook_func_t *rl_signal_event_hook = (rl_hook_func_t *)NULL;
pub static mut rl_signal_event_hook: Option<extern "C" fn()>;
/* If this is non-zero, do job control. */
// int job_control = 1;
#[link_name = "job_control"]
pub static mut job_control: c_int;
/* Give the terminal to PGRP. */
// int give_terminal_to (pid_t pgrp, int force)
pub fn give_terminal_to(pgrp: libc::pid_t, force: c_int) -> c_int;
/* The shell's process group. */
// pid_t shell_pgrp = NO_PID;
#[link_name = "shell_pgrp"]
pub static mut shell_pgrp: libc::pid_t;
/* The value returned by the last synchronous command. */
#[link_name = "last_command_exit_value"]
pub static mut last_command_exit_value: c_int;
// char * get_working_directory (const char *for_whom)
pub fn get_working_directory(for_whom: *const c_char) -> *mut c_char;
// char *current_host_name = (char *)NULL;
#[link_name = "current_host_name"]
pub static mut current_host_name: *mut c_char;
// int array_needs_making = 1;
#[link_name = "array_needs_making"]
pub static mut array_needs_making: c_int;
// extern int show_var_attributes (SHELL_VAR *, int, int);
pub fn show_var_attributes(var: *mut ShellVar, flags: c_int, output_fd: c_int) -> c_int;
// bashhist.h
// int check_add_history (char *line, int force)
pub fn check_add_history(line: *const c_char, force: c_int) -> c_int;
}
pub(crate) static BASH_LOCK: parking_lot::ReentrantMutex<()> = parking_lot::ReentrantMutex::new(());
/// Reset `BASH_LOCK` in a forked child so it is not left held.
///
/// # Safety
///
/// Call only in the child, immediately after `fork`, before any other code in
/// this process observes `BASH_LOCK`. The parent may have held the lock at
/// fork time; this force-unlocks it so the child does not deadlock.
pub unsafe fn reset_bash_lock_after_fork() {
while BASH_LOCK.is_locked() {
unsafe {
BASH_LOCK.force_unlock();
}
}
}
/// Allocate via Bash `xmalloc` while holding `BASH_LOCK`.
///
/// # Safety
///
/// `size` must be a valid size for Bash `xmalloc`. The returned pointer is
/// owned by the Bash allocator and must be freed with [`locked_xfree`].
#[cfg(not(test))]
pub unsafe fn locked_xmalloc(size: libc::size_t) -> *mut libc::c_void {
let _guard = BASH_LOCK.lock();
unsafe { xmalloc(size) }
}
/// Free via Bash `xfree` while holding `BASH_LOCK`.
///
/// # Safety
///
/// `ptr` must be null or a pointer previously obtained from Bash `xmalloc` /
/// [`locked_xmalloc`] that has not already been freed.
#[cfg(not(test))]
pub unsafe fn locked_xfree(ptr: *mut libc::c_void) {
if !ptr.is_null() {
let _guard = BASH_LOCK.lock();
unsafe { xfree(ptr) };
}
}
/// Allocate a Bash-owned copy of `s` via `xmalloc`.
///
/// # Safety
///
/// The returned pointer is owned by the Bash allocator and must be freed with
/// [`locked_xfree`]. Bash `xmalloc` must be available in this process.
#[cfg(not(test))]
pub unsafe fn locked_xmalloc_cstr(s: &std::ffi::CStr) -> *mut c_char {
let _guard = BASH_LOCK.lock();
let bytes = s.to_bytes_with_nul();
unsafe {
let ptr = xmalloc(bytes.len()) as *mut c_char;
std::ptr::copy_nonoverlapping(bytes.as_ptr() as *const c_char, ptr, bytes.len());
ptr
}
}
// history.h
pub type HistdataT = *mut libc::c_void;
// history.h
#[repr(C)]
#[allow(dead_code)]
pub struct HistoryEntry {
pub line: *mut c_char,
pub timestamp: *mut c_char,
pub data: HistdataT,
}
// DO NOT USE: BashArray and BashArrayElement definitions vary across different Bash builds/versions.
// Use array_to_word_list / dispose_words via ShellVar::get_array_elements() instead.
//
// pub type ArrayindT = libc::intmax_t;
// #[repr(C)]
// pub struct BashArrayElement {
// pub ind: ArrayindT,
// pub value: *mut c_char,
// pub next: *mut BashArrayElement,
// pub prev: *mut BashArrayElement,
// }
// #[repr(C)]
// pub struct BashArray {
// pub max_index: ArrayindT,
// pub num_elements: ArrayindT,
// pub head: *mut BashArrayElement,
// pub lastref: *mut BashArrayElement,
// }
// pcomplete.h
#[repr(C)]
#[allow(dead_code)]
#[derive(Debug)]
pub struct CompSpec {
pub refcount: c_int,
pub actions: libc::c_ulong,
pub options: libc::c_ulong,
pub globpat: *mut c_char,
pub words: *mut c_char,
pub prefix: *mut c_char,
pub suffix: *mut c_char,
pub funcname: *mut c_char,
pub command: *mut c_char,
pub lcommand: *mut c_char,
pub filterpat: *mut c_char,
}
// alias.h
#[repr(C)]
#[allow(dead_code)]
#[derive(Debug)]
pub struct Alias {
pub name: *mut c_char,
pub value: *mut c_char,
pub flags: c_char,
}
// command.h (Function definition command)
#[repr(C)]
#[allow(dead_code)]
#[derive(Debug)]
pub struct FunctionDef {
pub flags: c_int,
pub line: c_int,
pub name: *mut libc::c_void, // WORD_DESC* - opaque
pub command: *mut libc::c_void, // COMMAND* - opaque
pub source_file: *mut c_char, // char*
}
// variables.h
#[repr(C)]
#[allow(dead_code)]
#[derive(Clone)]
pub struct ShellVar {
pub name: *mut c_char, // Symbol that the user types.
pub value: *mut c_char, // Value that is returned.
pub exportstr: *mut c_char, // String for the environment.
pub dynamic_value: Option<extern "C" fn() -> *mut c_char>, // Function called to return a `dynamic' value for a variable, like $SECONDS or $RANDOM.
pub assign_func: Option<extern "C" fn(*const c_char)>, // Function called when this `special variable' is assigned a value in bind_variable.
pub attributes: c_int, // export, readonly, array, invisible...
pub context: c_int, // Which context this variable belongs to.
}
impl ShellVar {
#[cfg(not(test))]
pub fn get_array_elements(&self) -> Vec<String> {
if !self.is_array() || self.value.is_null() {
return Vec::new();
}
let mut result = Vec::new();
unsafe {
let wl_ptr = array_to_word_list(self.value as *mut libc::c_void);
if wl_ptr.is_null() {
return result;
}
let mut curr = wl_ptr;
while !curr.is_null() {
let word_desc = (*curr).word;
if !word_desc.is_null() && !(*word_desc).word.is_null() {
let val_cstr = std::ffi::CStr::from_ptr((*word_desc).word);
result.push(val_cstr.to_string_lossy().into_owned());
}
curr = (*curr).next;
}
dispose_words(wl_ptr);
}
result
}
#[cfg(test)]
pub fn get_array_elements(&self) -> Vec<String> {
Vec::new()
}
pub fn get_value(&self) -> Option<String> {
if self.value.is_null() {
None
} else {
unsafe {
Some(
std::ffi::CStr::from_ptr(self.value)
.to_string_lossy()
.into_owned(),
)
}
}
}
pub fn get_name(&self) -> Option<String> {
if self.name.is_null() {
None
} else {
unsafe {
Some(
std::ffi::CStr::from_ptr(self.name)
.to_string_lossy()
.into_owned(),
)
}
}
}
pub fn get_exportstr(&self) -> Option<String> {
if self.exportstr.is_null() {
None
} else {
unsafe {
Some(
std::ffi::CStr::from_ptr(self.exportstr)
.to_string_lossy()
.into_owned(),
)
}
}
}
pub fn is_exported(&self) -> bool {
self.attributes & ATT_EXPORTED as c_int != 0
}
pub fn is_readonly(&self) -> bool {
self.attributes & ATT_READONLY as c_int != 0
}
pub fn is_array(&self) -> bool {
self.attributes & ATT_ARRAY as c_int != 0
}
pub fn is_function(&self) -> bool {
self.attributes & ATT_FUNCTION as c_int != 0
}
pub fn is_integer(&self) -> bool {
self.attributes & ATT_INTEGER as c_int != 0
}
pub fn is_local(&self) -> bool {
self.attributes & ATT_LOCAL as c_int != 0
}
pub fn is_associative_array(&self) -> bool {
self.attributes & ATT_ASSOC as c_int != 0
}
}
impl Debug for ShellVar {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
f.debug_struct("ShellVar")
.field("name", &self.get_name())
.field("value", &self.get_value())
.field("exportstr", &self.get_exportstr())
.field("attributes", &self.attributes)
.field("is_exported", &self.is_exported())
.field("is_readonly", &self.is_readonly())
.field("is_array", &self.is_array())
.field("is_function", &self.is_function())
.field("is_integer", &self.is_integer())
.field("is_local", &self.is_local())
.field("is_associative_array", &self.is_associative_array())
.finish()
}
}
/* The various attributes that a given variable can have. */
/* First, the user-visible attributes */
#[allow(unused)]
pub const ATT_EXPORTED: libc::c_ulong = 0x0000001; /* export to environment */
#[allow(unused)]
pub const ATT_READONLY: libc::c_ulong = 0x0000002; /* cannot change */
#[allow(unused)]
pub const ATT_ARRAY: libc::c_ulong = 0x0000004; /* value is an array */
#[allow(unused)]
pub const ATT_FUNCTION: libc::c_ulong = 0x0000008; /* value is a function */
#[allow(unused)]
pub const ATT_INTEGER: libc::c_ulong = 0x0000010; /* internal representation is int */
#[allow(unused)]
pub const ATT_LOCAL: libc::c_ulong = 0x0000020; /* variable is local to a function */
#[allow(unused)]
pub const ATT_ASSOC: libc::c_ulong = 0x0000040; /* variable is an associative array */
#[allow(unused)]
pub const ATT_TRACE: libc::c_ulong = 0x0000080; /* function is traced with DEBUG trap */
#[allow(unused)]
pub const ATT_UPPERCASE: libc::c_ulong = 0x0000100; /* word converted to uppercase on assignment */
#[allow(unused)]
pub const ATT_LOWERCASE: libc::c_ulong = 0x0000200; /* word converted to lowercase on assignment */
#[allow(unused)]
pub const ATT_CAPCASE: libc::c_ulong = 0x0000400; /* word capitalized on assignment */
#[allow(unused)]
pub const ATT_NAMEREF: libc::c_ulong = 0x0000800; /* word is a name reference */
/* Internal attributes used for bookkeeping */
#[allow(unused)]
pub const ATT_INVISIBLE: libc::c_ulong = 0x0001000; /* cannot see */
#[allow(unused)]
pub const ATT_NOUNSET: libc::c_ulong = 0x0002000; /* cannot unset */
#[allow(unused)]
pub const ATT_NOASSIGN: libc::c_ulong = 0x0004000; /* assignment not allowed */
#[allow(unused)]
pub const ATT_IMPORTED: libc::c_ulong = 0x0008000; /* came from environment */
#[allow(unused)]
pub const ATT_SPECIAL: libc::c_ulong = 0x0010000; /* requires special handling */
#[allow(unused)]
pub const ATT_NOFREE: libc::c_ulong = 0x0020000; /* do not free value on unset */
#[allow(unused)]
pub const ATT_REGENERATE: libc::c_ulong = 0x0040000; /* regenerate when exported */
/* Internal attributes used for variable scoping. */
#[allow(unused)]
pub const ATT_TEMPVAR: libc::c_ulong = 0x0100000; /* variable came from the temp environment */
#[allow(unused)]
pub const ATT_PROPAGATE: libc::c_ulong = 0x0200000; /* propagate to previous scope */
// builtins.h
// };
#[repr(C)]
#[allow(dead_code)]
pub struct BashBuiltinType {
pub name: *mut c_char, // The name that the user types.
pub function: Option<extern "C" fn(c_int, *mut *mut c_char, *mut c_char) -> c_int>, // The address of the invoked function.
pub flags: c_int, // One of the #defines above.
pub long_doc: *mut *mut c_char, // NULL terminated array of strings.
pub short_doc: *mut c_char, // Short version of documentation.
pub handle: *mut c_char, // for future use
}
// externs.h
#[repr(C)]
#[allow(dead_code)]
#[derive(Debug)]
pub struct StringList {
pub list: *mut *mut c_char,
pub list_size: c_uint, // TODO verify this is the correct type
pub list_len: c_uint,
}
#[cfg(not(test))]
pub fn set_readline_state(state: libc::c_ulong) {
let _guard = BASH_LOCK.lock();
unsafe {
rl_readline_state |= state;
}
}
#[cfg(not(test))]
pub fn clear_readline_state(state: libc::c_ulong) {
let _guard = BASH_LOCK.lock();
unsafe {
rl_readline_state &= !state;
}
}
#[allow(unused)]
pub const RL_STATE_NONE: libc::c_ulong = 0x0000000; /* no state; before first call */
#[allow(unused)]
pub const RL_STATE_INITIALIZING: libc::c_ulong = 0x00000001; /* initializing */
#[allow(unused)]
pub const RL_STATE_INITIALIZED: libc::c_ulong = 0x00000002; /* initialization done */
#[allow(unused)]
pub const RL_STATE_TERMPREPPED: libc::c_ulong = 0x00000004; /* terminal is prepped */
#[allow(unused)]
pub const RL_STATE_READCMD: libc::c_ulong = 0x00000008; /* reading a command key */
#[allow(unused)]
pub const RL_STATE_METANEXT: libc::c_ulong = 0x00000010; /* reading input after ESC */
#[allow(unused)]
pub const RL_STATE_DISPATCHING: libc::c_ulong = 0x00000020; /* dispatching to a command */
#[allow(unused)]
pub const RL_STATE_MOREINPUT: libc::c_ulong = 0x00000040; /* reading more input in a command function */
#[allow(unused)]
pub const RL_STATE_ISEARCH: libc::c_ulong = 0x00000080; /* doing incremental search */
#[allow(unused)]
pub const RL_STATE_NSEARCH: libc::c_ulong = 0x00000100; /* doing non-inc search */
#[allow(unused)]
pub const RL_STATE_SEARCH: libc::c_ulong = 0x00000200; /* doing a history search */
#[allow(unused)]
pub const RL_STATE_NUMERICARG: libc::c_ulong = 0x00000400; /* reading numeric argument */
#[allow(unused)]
pub const RL_STATE_MACROINPUT: libc::c_ulong = 0x00000800; /* getting input from a macro */
#[allow(unused)]
pub const RL_STATE_MACRODEF: libc::c_ulong = 0x00001000; /* defining keyboard macro */
#[allow(unused)]
pub const RL_STATE_OVERWRITE: libc::c_ulong = 0x00002000; /* overwrite mode */
#[allow(unused)]
pub const RL_STATE_COMPLETING: libc::c_ulong = 0x00004000; /* doing completion */
#[allow(unused)]
pub const RL_STATE_SIGHANDLER: libc::c_ulong = 0x00008000; /* in readline sighandler */
#[allow(unused)]
pub const RL_STATE_UNDOING: libc::c_ulong = 0x00010000; /* doing an undo */
#[allow(unused)]
pub const RL_STATE_INPUTPENDING: libc::c_ulong = 0x00020000; /* rl_execute_next called */
#[allow(unused)]
pub const RL_STATE_TTYCSAVED: libc::c_ulong = 0x00040000; /* tty special chars saved */
#[allow(unused)]
pub const RL_STATE_CALLBACK: libc::c_ulong = 0x00080000; /* using the callback interface */
#[allow(unused)]
pub const RL_STATE_VIMOTION: libc::c_ulong = 0x00100000; /* reading vi motion arg */
#[allow(unused)]
pub const RL_STATE_MULTIKEY: libc::c_ulong = 0x00200000; /* reading multiple-key command */
#[allow(unused)]
pub const RL_STATE_VICMDONCE: libc::c_ulong = 0x00400000; /* entered vi command mode at least once */
#[allow(unused)]
pub const RL_STATE_CHARSEARCH: libc::c_ulong = 0x00800000; /* vi mode char search */
#[allow(unused)]
pub const RL_STATE_REDISPLAYING: libc::c_ulong = 0x01000000; /* updating terminal display */
#[allow(unused)]
pub const RL_STATE_DONE: libc::c_ulong = 0x02000000; /* done; accepted line */
#[allow(unused)]
pub const RL_STATE_TIMEOUT: libc::c_ulong = 0x04000000; /* done; timed out */
#[allow(unused)]
pub const RL_STATE_EOF: libc::c_ulong = 0x08000000; /* done; got eof on read */
#[allow(unused)]
pub const RL_STATE_READSTR: libc::c_ulong = 0x10000000; /* reading a string for M-x */