pub fn execute_piped_cmd_chain(cmds: &CmdChain) -> Vec<ProcessState> {
    let mut pids: Vec<libc::pid_t> = vec![];

    let mut pipe_to_current: Option<Pipe> = Option::None;
    let mut pipe_to_next: Option<Pipe> = Option::None;
    for i in 0..cmds.length() {
        let cmd = &cmds.cmds()[i];

        if pipe_to_next.is_some() {
            pipe_to_current.replace(
                pipe_to_next.take().unwrap()
            );
        }

        if !cmd.is_last() {
            pipe_to_next.replace(Pipe::new());
        }

        let pid = unsafe { libc::fork() };
        if pid == -1 {
            panic!("Fork failed! {}", errno::errno());
        }

        // parent code
        if pid > 0 {
            pids.push(pid);

            // We MUST close all FDs in the Parent
            if pipe_to_current.is_some() {
                pipe_to_current.as_mut().unwrap().parent_close_all();
            }
        }
        // child code
        else {
            // handle optional initial '< in.file' redirect
            if cmd.is_first() && cmd.in_red_path().is_some() {
                initial_ir(cmd);
            }
            // handle optional final '> out.file' redirect
            if cmd.is_last() && cmd.out_red_path().is_some() {
                final_or(cmd);
            }

            if pipe_to_current.is_some() {
                pipe_to_current.as_mut().unwrap().as_read_end();
            }
            if pipe_to_next.is_some() {
                pipe_to_next.as_mut().unwrap().as_write_end();
            }

            let _res = unsafe {
                libc::execvp(
                    cmd.executable_cstring().as_ptr(),
                    cmd.args_to_c_argv()
                )
            };
            panic!("Exec failed! {}", errno::errno());
        }
    }

    let mut i = 0;
    let mut process_states: Vec<ProcessState> = pids.into_iter()
        .map(|pid| {
            let executable = cmds.cmds()[i].executable().to_owned();
            i += 1;
            ProcessState::new(executable, pid)
        })
        .collect();

    update_process_states(&mut process_states, cmds.background());

    process_states
}
/// Runs a command chain. The parent process creates n childs and
/// connects them (stdout => stdin) together via pipes.
Runs a command chain. The parent process creates n childs and
connects them (stdout => stdin) together via pipes.
Runs a command chain . The parent process creates n childs and connects them ( stdout = > stdin ) together via pipes .
pub fn execute_piped_cmd_chain(cmds: &CmdChain) -> Vec<ProcessState> {
    let mut pids: Vec<libc::pid_t> = vec![];
    let mut pipe_to_current: Option<Pipe> = Option::None;
    let mut pipe_to_next: Option<Pipe> = Option::None;
    for i in 0..cmds.length() {
        let cmd = &cmds.cmds()[i];
        if pipe_to_next.is_some() {
            pipe_to_current.replace(
                pipe_to_next.take().unwrap()
            );
        }
        if !cmd.is_last() {
            pipe_to_next.replace(Pipe::new());
        }
        let pid = unsafe { libc::fork() };
        if pid == -1 {
            panic!("Fork failed! {}", errno::errno());
        }
        // parent code
        if pid > 0 {
            pids.push(pid);
            // We MUST close all FDs in the Parent
            if pipe_to_current.is_some() {
                pipe_to_current.as_mut().unwrap().parent_close_all();
            }
        }
        // child code
        else {
            // handle optional initial '< in.file' redirect
            if cmd.is_first() && cmd.in_red_path().is_some() {
                initial_ir(cmd);
            }
            // handle optional final '> out.file' redirect
            if cmd.is_last() && cmd.out_red_path().is_some() {
                final_or(cmd);
            }
            if pipe_to_current.is_some() {
                pipe_to_current.as_mut().unwrap().as_read_end();
            }
            if pipe_to_next.is_some() {
                pipe_to_next.as_mut().unwrap().as_write_end();
            }
            let _res = unsafe {
                libc::execvp(
                    cmd.executable_cstring().as_ptr(),
                    cmd.args_to_c_argv()
                )
            };
            panic!("Exec failed! {}", errno::errno());
        }
    }
    let mut i = 0;
    let mut process_states: Vec<ProcessState> = pids.into_iter()
        .map(|pid| {
            let executable = cmds.cmds()[i].executable().to_owned();
            i += 1;
            ProcessState::new(executable, pid)
        })
        .collect();
    update_process_states(&mut process_states, cmds.background());
    process_states
}
pub fn execute_piped_cmd_chain ( cmds : & CmdChain ) -> Vec < ProcessState > { let mut pids : Vec < libc :: pid_t > = vec ! [ ] ; let mut pipe_to_current : Option < Pipe > = Option :: None ; let mut pipe_to_next : Option < Pipe > = Option :: None ; for i in 0 .. cmds . length ( ) { let cmd = & cmds . cmds ( ) [ i ] ; if pipe_to_next . is_some ( ) { pipe_to_current . replace ( pipe_to_next . take ( ) . unwrap ( ) ) ; } if ! cmd . is_last ( ) { pipe_to_next . replace ( Pipe :: new ( ) ) ; } let pid = unsafe { libc :: fork ( ) } ; if pid == - 1 { panic ! ( " " errno errno ( ) ) ; } // parent code if pid > 0 { pids . push ( pid ) ; // We MUST close all FDs in the Parent if pipe_to_current . is_some ( ) { pipe_to_current . as_mut ( ) . unwrap ( ) . parent_close_all ( ) ; } } // child code else { // handle optional initial '< in.file' redirect if cmd . is_first ( ) && cmd . in_red_path ( ) . is_some ( ) { initial_ir ( cmd ) ; } // handle optional final '> out.file' redirect if cmd . is_last ( ) && cmd . out_red_path ( ) . is_some ( ) { final_or ( cmd ) ; } if pipe_to_current . is_some ( ) { pipe_to_current . as_mut ( ) . unwrap ( ) . as_read_end ( ) ; } if pipe_to_next . is_some ( ) { pipe_to_next . as_mut ( ) . unwrap ( ) . as_write_end ( ) ; } let _res = unsafe { libc :: execvp ( cmd . executable_cstring ( ) . as_ptr ( ) , cmd . args_to_c_argv ( ) ) } ; panic ! ( " " errno errno ( ) ) ; } } let mut i = 0 ; let mut process_states : Vec < ProcessState > = pids . into_iter ( ) . map ( | pid | { let executable = cmds . cmds ( ) [ i ] . executable ( ) . to_owned ( ) ; i += 1 ; ProcessState :: new ( executable , pid ) } ) . collect ( ) ; update_process_states ( & mut process_states , cmds . background ( ) ) ; process_states }
Runs a command chain.
Runs a command chain .
cmds
&CmdChain
cmds
&CmdChain