2
2
//SPDX-License-Identifier: BSD-3-Clause
3
3
4
4
pub mod builtins;
5
+ pub mod data;
5
6
pub mod jobtable;
6
- pub mod parameter;
7
7
8
+ use self :: data:: Data ;
8
9
use std:: collections:: HashMap ;
9
10
use std:: os:: fd:: { FromRawFd , OwnedFd } ;
10
11
use std:: { io, env, path, process} ;
@@ -19,9 +20,8 @@ use std::sync::atomic::AtomicBool;
19
20
use std:: sync:: atomic:: Ordering :: Relaxed ;
20
21
21
22
pub struct ShellCore {
23
+ pub data : Data ,
22
24
pub history : Vec < String > ,
23
- pub flags : String ,
24
- parameters : HashMap < String , String > ,
25
25
pub builtins : HashMap < String , fn ( & mut ShellCore , & mut Vec < String > ) -> i32 > ,
26
26
pub sigint : Arc < AtomicBool > ,
27
27
pub is_subshell : bool ,
@@ -50,9 +50,8 @@ fn restore_signal(sig: Signal) {
50
50
impl ShellCore {
51
51
pub fn new ( ) -> ShellCore {
52
52
let mut core = ShellCore {
53
+ data : Data :: new ( ) ,
53
54
history : Vec :: new ( ) ,
54
- flags : String :: new ( ) ,
55
- parameters : HashMap :: new ( ) ,
56
55
builtins : HashMap :: new ( ) ,
57
56
sigint : Arc :: new ( AtomicBool :: new ( false ) ) ,
58
57
is_subshell : false ,
@@ -66,7 +65,7 @@ impl ShellCore {
66
65
core. set_builtins ( ) ;
67
66
68
67
if is_interactive ( ) {
69
- core. flags += "i" ;
68
+ core. data . flags += "i" ;
70
69
let fd = fcntl:: fcntl ( 2 , fcntl:: F_DUPFD_CLOEXEC ( 255 ) )
71
70
. expect ( "sush(fatal): Can't allocate fd for tty FD" ) ;
72
71
core. tty_fd = Some ( unsafe { OwnedFd :: from_raw_fd ( fd) } ) ;
@@ -76,15 +75,15 @@ impl ShellCore {
76
75
}
77
76
78
77
fn set_initial_parameters ( & mut self ) {
79
- self . set_param ( "$" , & process:: id ( ) . to_string ( ) ) ;
80
- self . set_param ( "BASHPID" , & process:: id ( ) . to_string ( ) ) ;
81
- self . set_param ( "BASH_SUBSHELL" , "0" ) ;
82
- self . set_param ( "?" , "0" ) ;
83
- self . set_param ( "HOME" , & env:: var ( "HOME" ) . unwrap_or ( "/" . to_string ( ) ) ) ;
78
+ self . data . set_param ( "$" , & process:: id ( ) . to_string ( ) ) ;
79
+ self . data . set_param ( "BASHPID" , & process:: id ( ) . to_string ( ) ) ;
80
+ self . data . set_param ( "BASH_SUBSHELL" , "0" ) ;
81
+ self . data . set_param ( "?" , "0" ) ;
82
+ self . data . set_param ( "HOME" , & env:: var ( "HOME" ) . unwrap_or ( "/" . to_string ( ) ) ) ;
84
83
}
85
84
86
85
pub fn has_flag ( & self , flag : char ) -> bool {
87
- self . flags . find ( flag) != None
86
+ self . data . flags . find ( flag) != None
88
87
}
89
88
90
89
pub fn wait_process ( & mut self , child : Pid ) {
@@ -108,7 +107,7 @@ impl ShellCore {
108
107
if exit_status == 130 {
109
108
self . sigint . store ( true , Relaxed ) ;
110
109
}
111
- self . parameters . insert ( "?" . to_string ( ) , exit_status. to_string ( ) ) ; //追加
110
+ self . data . parameters . insert ( "?" . to_string ( ) , exit_status. to_string ( ) ) ; //追加
112
111
}
113
112
114
113
fn set_foreground ( & self ) {
@@ -151,15 +150,15 @@ impl ShellCore {
151
150
152
151
let func = self . builtins [ & args[ 0 ] ] ;
153
152
let status = func ( self , args) ;
154
- self . parameters . insert ( "?" . to_string ( ) , status. to_string ( ) ) ;
153
+ self . data . parameters . insert ( "?" . to_string ( ) , status. to_string ( ) ) ;
155
154
true
156
155
}
157
156
158
157
pub fn exit ( & self ) -> ! {
159
- let exit_status = match self . parameters [ "?" ] . parse :: < i32 > ( ) {
158
+ let exit_status = match self . data . parameters [ "?" ] . parse :: < i32 > ( ) {
160
159
Ok ( n) => n%256 ,
161
160
Err ( _) => {
162
- eprintln ! ( "sush: exit: {}: numeric argument required" , self . parameters[ "?" ] ) ;
161
+ eprintln ! ( "sush: exit: {}: numeric argument required" , self . data . parameters[ "?" ] ) ;
163
162
2
164
163
} ,
165
164
} ;
@@ -169,10 +168,10 @@ impl ShellCore {
169
168
170
169
fn set_subshell_parameters ( & mut self ) {
171
170
let pid = nix:: unistd:: getpid ( ) ;
172
- self . parameters . insert ( "BASHPID" . to_string ( ) , pid. to_string ( ) ) ;
173
- match self . parameters [ "BASH_SUBSHELL" ] . parse :: < usize > ( ) {
174
- Ok ( num) => self . parameters . insert ( "BASH_SUBSHELL" . to_string ( ) , ( num+1 ) . to_string ( ) ) ,
175
- Err ( _) => self . parameters . insert ( "BASH_SUBSHELL" . to_string ( ) , "0" . to_string ( ) ) ,
171
+ self . data . parameters . insert ( "BASHPID" . to_string ( ) , pid. to_string ( ) ) ;
172
+ match self . data . parameters [ "BASH_SUBSHELL" ] . parse :: < usize > ( ) {
173
+ Ok ( num) => self . data . parameters . insert ( "BASH_SUBSHELL" . to_string ( ) , ( num+1 ) . to_string ( ) ) ,
174
+ Err ( _) => self . data . parameters . insert ( "BASH_SUBSHELL" . to_string ( ) , "0" . to_string ( ) ) ,
176
175
} ;
177
176
}
178
177
0 commit comments