From 944a3ce9e3930162c7ddebccfb2669219c9af128 Mon Sep 17 00:00:00 2001 From: Bexx Modebadze Date: Sat, 17 Jul 2021 11:12:22 -0400 Subject: [PATCH] simple piping (without redirections) is working.. Still needs error handling and testing --- src/main.rs | 21 +++++++++++++++++++-- src/tokenizer.rs | 12 ++++++++++++ 2 files changed, 31 insertions(+), 2 deletions(-) diff --git a/src/main.rs b/src/main.rs index 6af9621..c77ce56 100644 --- a/src/main.rs +++ b/src/main.rs @@ -6,7 +6,7 @@ use signal_hook::{ consts::{SIGINT, SIGQUIT}, iterator, }; -use std::fs::{File, OpenOptions}; +use std::{fs::{File, OpenOptions}, io::Read}; use std::{ error::Error, io::{self, Write}, @@ -49,7 +49,24 @@ fn execute_shell() { let mut cmd_line = get_user_commands(); - if cmd_line.has_redirection() { + if cmd_line.is_pipe() { + let mut before_pipe_cmd = cmd_line.commands_before_pipe(); + let cmd = before_pipe_cmd.get_args(); + let cmd2 = cmd_line.get_args(); + let mut proc2 = process::Command::new(&cmd2[0]) + .args(&cmd2[1..]) + .stdin(process::Stdio::piped()) + .spawn() + .unwrap(); + + let mut proc1 = process::Command::new(&cmd[0]) + .args(&cmd[1..]) + .stdout(proc2.stdin.unwrap()) + .output() + .unwrap(); + } + + else if cmd_line.has_redirection() { let cmd = cmd_line.peek().clone(); let mut proc = match redirect_cmd_execution(&mut cmd_line) { Ok(p) => p, diff --git a/src/tokenizer.rs b/src/tokenizer.rs index 79b61a1..9347ecc 100644 --- a/src/tokenizer.rs +++ b/src/tokenizer.rs @@ -21,6 +21,18 @@ impl Tokenizer { args } + pub fn commands_before_pipe(&mut self) -> Tokenizer { + let mut before = String::new(); + while let Some(a) = self.next() { + if a.eq("|") { + break; + } + before.push_str(&a); + before.push_str(" "); + } + Tokenizer::new(&before) + } + pub fn get_args(&mut self) -> Vec { let mut args = vec![]; while let Some(a) = self.next() {