Skip to content

Commit

Permalink
fix: pipe opeartor not working on windows
Browse files Browse the repository at this point in the history
  • Loading branch information
kruserr committed Oct 12, 2024
1 parent 5905636 commit 4f242ea
Showing 1 changed file with 75 additions and 22 deletions.
97 changes: 75 additions & 22 deletions i6-shell/src/lang.rs
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ impl Lexer for DefaultLexer {

for word in input.split_whitespace() {
let token_type = match word {
"&&" | "||" | "|" | ">" | ">>" => TokenType::Operator,
"&&" | "||" | "|" | ">" | ">>" | "<" => TokenType::Operator,
_ if tokens.is_empty()
|| matches!(
tokens.last().unwrap().token_type,
Expand Down Expand Up @@ -237,7 +237,7 @@ impl Interpreter for DefaultInterpreter {
_ => eprintln!("Unknown operator"),
},
ASTNode::Pipe { left, right } => {
let first_command = if let ASTNode::Command { name, args } = *left {
let first_command_output = if let ASTNode::Command { name, args } = *left {
let args: Vec<String> = args
.into_iter()
.map(|arg| {
Expand All @@ -250,16 +250,41 @@ impl Interpreter for DefaultInterpreter {
})
.collect();

std::process::Command::new(name)
.args(args)
.stdout(std::process::Stdio::piped())
.spawn()
.expect("Failed to start first command")
let args_str = &args.join(" ");

if let Some(custom_command) = custom_commands.get(&name) {
custom_command.run(args_str).unwrap_or_default()
} else if let Ok(lock) = crate::command::DEFAULT_COMMANDS.lock() {
if let Ok(commands) = lock.as_ref() {
if let Some(command) = commands.get(&name) {
command.run(args_str).unwrap_or_default()
} else {
{
let child = std::process::Command::new(&name).args(args).spawn();

match child {
Ok(mut child) => {
child.wait().unwrap();
}
Err(e) => eprintln!("{}", e),
}
}

return;
}
} else {
eprintln!("Failed to acquire lock");
return;
}
} else {
eprintln!("Failed to acquire lock");
return;
}
} else {
return;
};

let second_command = if let ASTNode::Command { name, args } = *right {
if let ASTNode::Command { name, args } = *right {
let args: Vec<String> = args
.into_iter()
.map(|arg| {
Expand All @@ -272,23 +297,51 @@ impl Interpreter for DefaultInterpreter {
})
.collect();

println!("{:?}", args);
let args_str = &args.join(" ");

std::process::Command::new(name)
.args(args)
.stdin(first_command.stdout.unwrap())
.stdout(std::process::Stdio::piped())
.spawn()
.expect("Failed to start second command")
} else {
return;
};
if let Some(custom_command) = custom_commands.get(&name) {
let _ = custom_command
.run(&format!("\"{}\" \"{}\"", args_str, first_command_output))
.map(|output| {
if !output.is_empty() {
println!("{output}");
}
})
.map_err(|e| eprintln!("{e}"));
} else if let Ok(lock) = crate::command::DEFAULT_COMMANDS.lock() {
if let Ok(commands) = lock.as_ref() {
if let Some(command) = commands.get(&name) {
let _ = command
.run(&format!("\"{}\" \"{}\"", args_str, first_command_output))
.map(|output| {
if !output.is_empty() {
println!("{output}");
}
})
.map_err(|e| eprintln!("{e}"));
} else {
{
let child = std::process::Command::new(&name).args(args).spawn();

let output = second_command
.wait_with_output()
.expect("Failed to wait on second command");
match child {
Ok(mut child) => {
child.wait().unwrap();
}
Err(e) => eprintln!("{}", e),
}
}

println!("{}", String::from_utf8_lossy(&output.stdout));
return;
}
} else {
eprintln!("Failed to acquire lock");
}
} else {
eprintln!("Failed to acquire lock");
}
} else {
return;
}
}
_ => eprintln!("Invalid syntax"),
}
Expand Down

0 comments on commit 4f242ea

Please sign in to comment.