@@ -28,8 +28,10 @@ pub fn handle_command(args: Vec<String>) -> (Commands,usize,usize,CommandPriorit
28
28
}
29
29
30
30
31
- pub fn run ( args : Vec < String > , session_context : & mut SessionContext ) -> String {
32
- let ( commands, pid, tid, priority) = handle_command ( args) ;
31
+ pub fn run < T > ( input : T , session_context : & mut SessionContext ) -> ( usize , String )
32
+ where Vec < String > : From < T >
33
+ {
34
+ let ( commands, pid, tid, priority) = handle_command ( input. into ( ) ) ;
33
35
let semaphore = semaphore_new ( ) ;
34
36
let mut tcb = ThreadControlBlock :: new ( ) ;
35
37
let mut pcb = ProcessManager :: new ( ) ;
@@ -49,17 +51,17 @@ pub fn run(args: Vec<String>,session_context: &mut SessionContext) -> String{
49
51
"ps" => {
50
52
pcb. kill ( pid) ;
51
53
tcb. stop_thread ( priority_tid) ;
52
- ps ( )
54
+ ( tid , ps ( ) )
53
55
}
54
56
"kill" => {
55
57
tcb. stop_thread ( priority_tid) ;
56
58
pcb. kill ( pid) ;
57
- pcb. kill ( arg[ 0 ] . parse :: < usize > ( ) . unwrap ( ) )
59
+ ( tid , pcb. kill ( arg[ 0 ] . parse :: < usize > ( ) . unwrap ( ) ) )
58
60
} ,
59
61
"sleep" => {
60
62
pcb. kill ( pid) ;
61
63
tcb. stop_thread ( priority_tid) ;
62
- sleep ( & mut tcb, commands. arg [ 0 ] . parse :: < usize > ( ) . unwrap ( ) )
64
+ ( tid , sleep ( & mut tcb, commands. arg [ 0 ] . parse :: < usize > ( ) . unwrap ( ) ) )
63
65
}
64
66
_ =>{
65
67
// start process and thread
@@ -70,41 +72,36 @@ pub fn run(args: Vec<String>,session_context: &mut SessionContext) -> String{
70
72
let status = res. 0 . clone ( ) ;
71
73
let result = res. 1 . clone ( ) ;
72
74
if status==0 {
73
- //println!("[{}] Done\n{}",tid,result);
74
75
pcb. kill ( pid) ;
75
76
tcb. stop_thread ( priority_tid) ;
76
- result
77
+ ( tid , result)
77
78
} else {
78
- error_log ( res. 1 . clone ( ) ) ;
79
- res . 1
79
+ error_log ( res. 1 . clone ( ) ) ;
80
+ ( tid , result )
80
81
}
81
- } else { String :: new ( ) }
82
+ } else { ( tid , String :: from ( "Error: Not found" ) ) }
82
83
} else if !session_context. user_state . root . check_permission ( ) && !session_context. root . allowed_commands . contains ( & commands. command ) {
83
84
// Execute normal commands
84
85
// Handle commands normally when user is not in root mode
85
86
if let Ok ( res) = command_match ( commands, session_context) {
86
87
let status = res. 0 . clone ( ) ;
87
88
let result = res. 1 . clone ( ) ;
88
89
if status==0 {
89
- //println!("[{}] Done\n{}",tid,result);
90
90
pcb. kill ( pid) ;
91
91
tcb. stop_thread ( priority_tid) ;
92
- result
92
+ ( tid , result)
93
93
} else {
94
94
error_log ( res. 1 . clone ( ) ) ;
95
- //println!("{}",res.1);
96
- result
95
+ ( tid, result)
97
96
}
98
97
} else {
99
- String :: new ( )
98
+ ( tid , String :: from ( "Error: Not found" ) )
100
99
}
101
100
} else {
102
- eprintln ! ( "Permission not support" ) ;
103
- String :: new ( )
101
+ ( tid, String :: from ( "Error: Permission not support" ) )
104
102
}
105
103
}
106
104
}
107
-
108
105
}
109
106
110
107
@@ -147,20 +144,21 @@ pub fn init_shell(session_context: &mut SessionContext){
147
144
let args: Box < Vec < String > > = Box :: new ( line. split_whitespace ( ) . map ( |s| s. to_string ( ) ) . collect ( ) ) ;
148
145
if args. contains ( & "&&" . to_string ( ) ) {
149
146
let res = and ( * args, session_context) ;
150
- for r in res{
151
- println ! ( "Done[1 ]: {r}" )
147
+ for ( t , r ) in res{
148
+ println ! ( "Done[{t} ]: \n {r}" )
152
149
}
153
150
} else if args. contains ( & "|" . to_string ( ) ) {
154
- let s = pipe ( * args) . unwrap ( ) ;
155
- println ! ( "{}" , s . 1 ) ;
151
+ let res = pipe ( * args) . unwrap ( ) ;
152
+ println ! ( "{}" , res . 1 )
156
153
} else if args. contains ( & "&" . to_string ( ) ) {
157
154
let res = priority_run ( * args, session_context) ;
158
- for r in res{
159
- println ! ( "Done[1]: {r}" )
155
+ for ( t, r) in res{
156
+ println ! ( "Done[{t}]: \n {r}" )
157
+
160
158
}
161
159
} else {
162
- let res = run ( * args, session_context) ;
163
- println ! ( "{res}" )
160
+ let ( t , res) = run ( * args, session_context) ;
161
+ println ! ( "Done[{t}]: \n {res}" )
164
162
}
165
163
166
164
}
0 commit comments