pub fn line_to_cmds(line: &str) -> Vec<String> {
    // Special characters: http://tldp.org/LDP/abs/html/special-chars.html
    let mut result = Vec::new();
    let mut sep = String::new();
    let mut token = String::new();
    let len = line.len();
    for (i, c) in line.chars().enumerate() {
        if c == '#' {
            if sep.is_empty() {
                break;
            } else {
                token.push(c);
                continue;
            }
        }
        if c == '\'' || c == '"' || c == '`' {
            if sep.is_empty() {
                sep.push(c);
                token.push(c);
                continue;
            } else if sep == c.to_string() {
                token.push(c);
                sep = String::new();
                continue;
            } else {
                token.push(c);
                continue;
            }
        }
        if c == '&' || c == '|' {
            // needs watch ahead here
            if sep.is_empty() {
                if i + 1 == len {
                    // for bg commands, e.g. `ls &`
                    token.push(c);
                    continue;
                } else {
                    let c_next;
                    match line.chars().nth(i + 1) {
                        Some(x) => c_next = x,
                        None => {
                            println!("chars nth error - should never happen");
                            continue;
                        }
                    }
                    if c_next != c {
                        token.push(c);
                        continue;
                    }
                }
            }

            if sep.is_empty() {
                sep.push(c);
                continue;
            } else if c.to_string() == sep {
                let _token = token.trim().to_string();
                if !_token.is_empty() {
                    result.push(_token);
                }
                token = String::new();
                result.push(format!("{}{}", sep, sep));
                sep = String::new();
                continue;
            } else {
                token.push(c);
                continue;
            }
        }
        if c == ';' {
            if sep.is_empty() {
                let _token = token.trim().to_string();
                if !_token.is_empty() {
                    result.push(_token);
                }
                result.push(String::from(";"));
                token = String::new();
                continue;
            } else {
                token.push(c);
                continue;
            }
        }
        token.push(c);
    }
    if !token.is_empty() {
        result.push(token.trim().to_string());
    }
    result
}
/// Parse command line for multiple commands. Examples:
/// >>> line_to_cmds("echo foo && echo bar; echo end");
/// vec!["echo foo", "&&", "echo bar", ";", "echo end"]
/// >>> line_to_cmds("man awk | grep version");
/// vec!["man awk | grep version"]
Parse command line for multiple commands.
Parse command line for multiple commands .
pub fn line_to_cmds(line: &str) -> Vec<String> {
    // Special characters: http://tldp.org/LDP/abs/html/special-chars.html
    let mut result = Vec::new();
    let mut sep = String::new();
    let mut token = String::new();
    let len = line.len();
    for (i, c) in line.chars().enumerate() {
        if c == '#' {
            if sep.is_empty() {
                break;
            } else {
                token.push(c);
                continue;
            }
        }
        if c == '\'' || c == '"' || c == '`' {
            if sep.is_empty() {
                sep.push(c);
                token.push(c);
                continue;
            } else if sep == c.to_string() {
                token.push(c);
                sep = String::new();
                continue;
            } else {
                token.push(c);
                continue;
            }
        }
        if c == '&' || c == '|' {
            // needs watch ahead here
            if sep.is_empty() {
                if i + 1 == len {
                    // for bg commands, e.g. `ls &`
                    token.push(c);
                    continue;
                } else {
                    let c_next;
                    match line.chars().nth(i + 1) {
                        Some(x) => c_next = x,
                        None => {
                            println!("chars nth error - should never happen");
                            continue;
                        }
                    }
                    if c_next != c {
                        token.push(c);
                        continue;
                    }
                }
            }
            if sep.is_empty() {
                sep.push(c);
                continue;
            } else if c.to_string() == sep {
                let _token = token.trim().to_string();
                if !_token.is_empty() {
                    result.push(_token);
                }
                token = String::new();
                result.push(format!("{}{}", sep, sep));
                sep = String::new();
                continue;
            } else {
                token.push(c);
                continue;
            }
        }
        if c == ';' {
            if sep.is_empty() {
                let _token = token.trim().to_string();
                if !_token.is_empty() {
                    result.push(_token);
                }
                result.push(String::from(";"));
                token = String::new();
                continue;
            } else {
                token.push(c);
                continue;
            }
        }
        token.push(c);
    }
    if !token.is_empty() {
        result.push(token.trim().to_string());
    }
    result
}
pub fn line_to_cmds ( line : & str ) -> Vec < String > { // Special characters: http://tldp.org/LDP/abs/html/special-chars.html let mut result = Vec :: new ( ) ; let mut sep = String :: new ( ) ; let mut token = String :: new ( ) ; let len = line . len ( ) ; for ( i , c ) in line . chars ( ) . enumerate ( ) { if c == '#' { if sep . is_empty ( ) { break ; } else { token . push ( c ) ; continue ; } } if c == '\'' || c == '"' || c == '`' { if sep . is_empty ( ) { sep . push ( c ) ; token . push ( c ) ; continue ; } else if sep == c . to_string ( ) { token . push ( c ) ; sep = String :: new ( ) ; continue ; } else { token . push ( c ) ; continue ; } } if c == '&' || c == '|' { // needs watch ahead here if sep . is_empty ( ) { if i + 1 == len { // for bg commands, e.g. `ls &` token . push ( c ) ; continue ; } else { let c_next ; match line . chars ( ) . nth ( i + 1 ) { Some ( x ) => c_next = x , None => { println ! ( " " ) ; continue ; } } if c_next != c { token . push ( c ) ; continue ; } } } if sep . is_empty ( ) { sep . push ( c ) ; continue ; } else if c . to_string ( ) == sep { let _token = token . trim ( ) . to_string ( ) ; if ! _token . is_empty ( ) { result . push ( _token ) ; } token = String :: new ( ) ; result . push ( format ! ( " " sep sep ) ) ; sep = String :: new ( ) ; continue ; } else { token . push ( c ) ; continue ; } } if c == ';' { if sep . is_empty ( ) { let _token = token . trim ( ) . to_string ( ) ; if ! _token . is_empty ( ) { result . push ( _token ) ; } result . push ( String :: from ( " " ) ) ; token = String :: new ( ) ; continue ; } else { token . push ( c ) ; continue ; } } token . push ( c ) ; } if ! token . is_empty ( ) { result . push ( token . trim ( ) . to_string ( ) ) ; } result }
Parse command line for multiple commands.
Parse command line for multiple commands .
line
&str
line
&str