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
use encoding_rs::GB18030;
use std::io::{self, Read, Write};
use std::process::{Command, Stdio};
use std::sync::{Arc, Mutex};
use std::thread;
use std::time::Duration;
#[derive(Debug, PartialEq, Copy, Clone)]
pub enum CommandStatus {
Running,
Terminated,
}
pub struct CommandRunner {
child: Option<std::process::Child>,
output: Arc<Mutex<Vec<String>>>,
running: Arc<std::sync::atomic::AtomicBool>,
}
impl CommandRunner {
pub fn new(command: &str, max_output_size: usize) -> Result<Self, io::Error> {
let (cmd_exe, param1) = if cfg!(target_os = "windows") {
("cmd", "/C")
} else {
("sh", "-c")
};
let mut child = Command::new(cmd_exe)
.arg(param1)
.arg(command)
.stdout(Stdio::piped())
.stderr(Stdio::piped())
.stdin(Stdio::piped())
.spawn()?;
let output = Arc::new(Mutex::new(Vec::new()));
let running = Arc::new(std::sync::atomic::AtomicBool::new(true));
thread::sleep(Duration::from_millis(100));
match child.try_wait() {
Ok(Some(status)) => {
if !status.success() {
return Err(io::Error::new(
io::ErrorKind::Other,
format!("Command failed with exit code: {}", status),
));
}
}
Ok(None) => {} // Process is still running, this is normal
Err(e) => return Err(e.into()),
}
let output_clone = Arc::clone(&output);
let running_clone = Arc::clone(&running);
let stdout = child.stdout.take();
thread::spawn(move || {
let mut buffer = [0; 128];
if let Some(mut stdout) = stdout {
while running_clone.load(std::sync::atomic::Ordering::SeqCst) {
match stdout.read(&mut buffer) {
Ok(0) | Err(_) => break,
Ok(n) => {
let mut output = output_clone.lock().unwrap();
if output.len() < max_output_size {
let (decoded, _, _) = GB18030.decode(&buffer[..n]);
output.push(decoded.into_owned());
}
}
}
}
}
});
let output_clone = Arc::clone(&output);
let running_clone = Arc::clone(&running);
let stderr = child.stderr.take();
thread::spawn(move || {
let mut buffer = [0; 128];
if let Some(mut stderr) = stderr {
while running_clone.load(std::sync::atomic::Ordering::SeqCst) {
match stderr.read(&mut buffer) {
Ok(0) | Err(_) => break,
Ok(n) => {
let mut output = output_clone.lock().unwrap();
if output.len() < max_output_size {
let (decoded, _, _) = GB18030.decode(&buffer[..n]);
output.push(decoded.into_owned());
}
}
}
}
}
});
Ok(CommandRunner {
child: Some(child),
output,
running,
})
}
pub fn get_output(&self) -> Option<String> {
let mut output = self.output.lock().unwrap();
output.pop()
}
pub fn get_status(&mut self) -> CommandStatus {
if let Some(child) = &mut self.child {
match child.try_wait() {
Ok(Some(_)) => {
self.running
.store(false, std::sync::atomic::Ordering::SeqCst);
CommandStatus::Terminated
}
Ok(None) => CommandStatus::Running,
Err(_) => {
self.running
.store(false, std::sync::atomic::Ordering::SeqCst);
CommandStatus::Terminated
}
}
} else {
CommandStatus::Terminated
}
}
pub fn terminate(&mut self) -> Result<CommandStatus, io::Error> {
if let Some(child) = &mut self.child {
child.kill()?;
self.running
.store(false, std::sync::atomic::Ordering::SeqCst);
child.wait()?;
Ok(CommandStatus::Terminated)
} else {
Err(io::Error::new(
io::ErrorKind::Other,
"No running command to terminate",
))
}
}
pub fn provide_input(&mut self, input: &str) -> Result<(), io::Error> {
if let Some(child) = &mut self.child {
if let Some(stdin) = &mut child.stdin {
stdin.write_all(input.as_bytes())?;
stdin.flush()?;
}
}
Ok(())
}
}
#[cfg(test)]
mod tests {
use encoding_rs::GBK;
use super::*;
use std::time::Duration;
#[test]
fn test_gbk() {
let output = Command::new("ping")
.arg("-n")
.arg("2")
.arg("google.com")
.stdout(Stdio::piped())
.output()
.expect("Failed to execute command");
let (decoded_output, _, _) = GBK.decode(&output.stdout);
println!("Decoded Output: {}", decoded_output);
}
#[test]
fn test_successful_command() {
let ping_count_option = if cfg!(target_os = "windows") {
"-n"
} else {
"-c"
};
let ping_num = 2;
let ping_command = format!("ping {} {} google.com", ping_count_option, ping_num);
let mut runner =
CommandRunner::new(&ping_command, 10000).expect("Failed to create CommandRunner");
let mut output_count = 0;
loop {
if let Some(output) = runner.get_output() {
println!("Got terminal Output:\n{}", output);
output_count += 1;
}
let status = runner.get_status();
println!("Got terminal status: {:?}", status);
if status == CommandStatus::Terminated {
break;
}
thread::sleep(Duration::from_millis(500));
}
assert!(
output_count >= ping_num,
"Only received {output_count} outputs"
);
assert_eq!(runner.get_status(), CommandStatus::Terminated);
}
#[test]
fn test_panic_command() {
let result = CommandRunner::new("nonexistent_command", 10000);
assert!(result.is_err());
}
// TODO:
// #[test]
// fn test_provide_input() {
// // 假设 guessing_game 项目已经编译为可执行文件
// let app = "python";
// let mut runner =
// CommandRunner::new(app, 10000).expect("Failed to create CommandRunner");
// // 检查初始反馈是否为 `>>>`
// // let mut initial_feedback_received = false;
// // loop {
// // if let Some(output) = runner.get_output() {
// // println!("Got Output: {}", output);
// // if output.trim() == ">>>" {
// // initial_feedback_received = true;
// // break;
// // }
// // }
// // let status = runner.get_status();
// // println!("Current status: {:?}", status);
// // if status == CommandStatus::Terminated {
// // break;
// // }
// // thread::sleep(Duration::from_millis(500));
// // }
// // assert!(initial_feedback_received, "Initial feedback not received");
// // 输入 `exit()`
// runner
// .provide_input("exit()\n")
// .expect("Failed to provide input");
// // 检查命令是否终止
// loop {
// let status = runner.get_status();
// println!("Current status: {:?}", status);
// if status == CommandStatus::Terminated {
// break;
// }
// thread::sleep(Duration::from_millis(500));
// }
// assert_eq!(runner.get_status(), CommandStatus::Terminated);
// }
}