Skip to content

Commit 0fe0f52

Browse files
fix stdout error
1 parent aae4f2d commit 0fe0f52

File tree

2 files changed

+48
-8
lines changed

2 files changed

+48
-8
lines changed

command/src/commands/arg.rs

Lines changed: 31 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ use crate::state_code::{missing_pattern, not_found};
55
use super::code::*;
66
use super::command::*;
77

8-
#[derive(Clone)]
8+
#[derive(Clone,Debug)]
99
pub struct Commands{
1010
pub command: String,
1111
pub option: String,
@@ -20,6 +20,19 @@ impl Commands {
2020
let mut option = String::new();
2121
let mut arg = Vec::new();
2222

23+
// get > len
24+
let parts: Vec<&str> = commands.iter().map(|s| s.as_str()).collect();
25+
let redirect_index = parts.iter().position(|&x| x == ">");
26+
if let Some(redirect_index) = redirect_index {
27+
if redirect_index > 1 {
28+
arg = parts.iter().map(|&s| s.to_string()).collect();
29+
return Commands {
30+
command: String::new(),
31+
option: ">".to_string(),
32+
arg,
33+
};
34+
}
35+
}
2336
match commands.as_slice() {
2437
[cmd] => {
2538
command = cmd.clone();
@@ -40,9 +53,9 @@ impl Commands {
4053
arg.push(opt.clone());
4154
}
4255
arg.extend_from_slice(args);
43-
}
56+
},
4457
_ => {
45-
if commands.iter().any(|x| x == "|" || x == "&" || x == "&&") {
58+
if commands.iter().any(|x| x == "|" || x == "&" || x == "&&" || x==">") {
4659
arg = commands;
4760
}
4861
}
@@ -217,4 +230,19 @@ pub fn split(commands: Commands) -> (String,String,Vec<String>){
217230
let option = commands.option.clone();
218231
let arg = commands.arg.clone();
219232
(command,option,arg)
233+
}
234+
235+
// ["cat", "README.md", ">", "a.txt"]
236+
237+
//command: ["cat", "README.md"]
238+
//file: a.txt
239+
pub fn stdout_other(arg: &Vec<String>) -> (String,Commands){
240+
let mut s = arg.split(|s| s==">");
241+
let output = s.next().unwrap().to_vec();
242+
let file = &s.next().unwrap()[0];
243+
244+
245+
let command = turn_command(output);
246+
247+
(file.to_string(),command)
220248
}

command/src/commands/command.rs

Lines changed: 17 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -370,7 +370,7 @@ use tar::Archive;
370370
use flate2::read::GzDecoder;
371371
use flate2::Compression;
372372
use flate2::write::GzEncoder;
373-
use super::arg::{execute_command, execute_other_command, split, Commands};
373+
use super::arg::{execute_command, execute_other_command, split, stdout_other, Commands};
374374

375375

376376
pub fn zxvf(file: &str, to: &str) -> Result<(usize,String),std::io::Error>{
@@ -398,11 +398,23 @@ pub fn xvf(to: &str) -> Result<(usize,String),std::io::Error>{
398398

399399

400400
// 重定向输出 >
401+
#[allow(unused_variables)]
402+
#[allow(unused_assignments)]
401403
pub fn stdout_file(commands: Commands,session_context: &mut SessionContext) -> Result<(usize,String), std::io::Error>{
402-
let command = commands.command.clone();
403-
let arg = commands.arg.clone();
404-
let result = execute_command(&command, "", &arg, session_context)?.1;
405-
let mut file = File::create(arg[arg.len()-1].clone())?;
404+
let mut command = commands.command.clone();
405+
let mut option = String::new();
406+
let mut arg = commands.arg.clone();
407+
let mut file = String::new();
408+
if command.is_empty(){
409+
let (f,new_command) = stdout_other(&arg);
410+
file = f;
411+
let (command_v,option_v,arg_v) = split(new_command);
412+
(command,option,arg) = (command_v,option_v,arg_v);
413+
}else{
414+
file = arg[arg.len()-1].clone();
415+
}
416+
let result = execute_command(&command, &option, &arg, session_context)?.1;
417+
let mut file = File::create(file)?;
406418
file.write(result.as_bytes())?;
407419
Ok((STATUE_CODE,"write over!".to_string()))
408420
}

0 commit comments

Comments
 (0)