1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
#![allow(dead_code)]
extern crate errno;
extern crate exec;
extern crate glob;
extern crate libc;
extern crate linefeed;
extern crate nix;
extern crate os_type;
extern crate regex;
extern crate sqlite;
extern crate time;

#[macro_use]
extern crate nom;

use std::collections::HashMap;

#[macro_use]
mod tools;

mod shell;
mod libs;
mod history;
mod builtins;
mod execute;
mod parsers;

/// Parse command line to multiple commands.
///
/// # Examples
///
/// ```no-run
/// >>> 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"]
/// ```
pub fn line_to_cmds(line: &str) -> Vec<String> {
    return parsers::parser_line::line_to_cmds(line);
}


/// Parse a command to tokens.
///
/// # Examples
///
/// ```no-run
/// >>> cmd_to_tokens("echo 'hi yoo' | `which wc`");
/// vec![
///     ("", "echo"),
///     ("'", "hi yoo"),
///     ("", "|"),
///     ("`", "which wc"),
/// ]
/// ```
pub fn cmd_to_tokens(cmd: &str) -> Vec<(String, String)> {
    return parsers::parser_line::cmd_to_tokens(cmd);
}


/// Determine whether line a valid input.
///
/// # Examples
///
/// ```no-run
/// is_valid_input("foo");  // true
/// is_valid_input("foo bar");  // true
/// is_valid_input("foo ;");  // true
/// is_valid_input("ls | wc -l");  // true
/// is_valid_input("foo; bar");  // true
/// is_valid_input("foo || bar");  // true
///
/// is_valid_input("foo |");  // false
/// is_valid_input("foo ||");  // false
/// is_valid_input("foo &&");  // false
/// is_valid_input("foo || && bar ");  // false
/// ```
pub fn is_valid_input(line: &str) -> bool {
    return parsers::parser_line::is_valid_input(line);
}


/// Run a command or a pipeline.
///
/// # Example
///
/// File content of src/main.rs:
///
/// ```no-run
/// extern crate cicada;
///
/// fn main() {
///     let out1 = cicada::run("ls");
///     println!("out1: {:?}", out1);
///
///     let out2 = cicada::run("ls | wc");
///     println!("out2: {:?}", out2);
///
///     let out3 = cicada::run("date >> out.txt");
///     println!("out3: {:?}", out3);
///
///     let out4 = cicada::run("cat out.txt");
///     println!("out4: {:?}", out4);
/// }
/// ```
///
/// Output:
///
/// ```no-run
/// out1: Ok("Cargo.lock\nCargo.toml\nsrc\ntarget\n")
/// out2: Ok("       4       4      33\n")
/// out3: Ok("")
/// out4: Ok("Fri Oct  6 14:53:25 CST 2017\n")
/// ```
pub fn run(line: &str) -> Result<String, &str> {
    let mut envs = HashMap::new();
    let cmd_line = tools::remove_envs_from_line(line, &mut envs);

    let mut tokens = parsers::parser_line::cmd_to_tokens(&cmd_line);
    if tokens.is_empty() {
        return Ok(String::new());
    }

    let mut len = tokens.len();
    if len > 1 && tokens[len - 1].1 == "&" {
        tokens.pop();
        len -= 1;
    }
    let mut redirect_from = String::new();
    let has_redirect_from = tokens.iter().any(|x| x.1 == "<");
    if has_redirect_from {
        if let Some(idx) = tokens.iter().position(|x| x.1 == "<") {
            tokens.remove(idx);
            len -= 1;
            if len >= idx + 1 {
                redirect_from = tokens.remove(idx).1;
                len -= 1;
            } else {
                return Err("cicada: invalid command: cannot get redirect from");
            }
        }
    }
    if len == 0 {
        return Ok(String::new());
    }

    let (_, _, output) =
        if len > 2 && (tokens[len - 2].1 == ">" || tokens[len - 2].1 == ">>") {
            let append = tokens[len - 2].1 == ">>";
            let redirect_to;
            match tokens.pop() {
                Some(x) => redirect_to = x.1,
                None => {
                    return Err("cicada: redirect_to pop error");
                }
            }
            tokens.pop(); // pop '>>' or '>'
            execute::run_pipeline(
                tokens,
                redirect_from.as_str(),
                redirect_to.as_str(),
                append,
                false,
                false,
                true,
                Some(envs),
            )
        } else {
            execute::run_pipeline(
                tokens.clone(),
                redirect_from.as_str(),
                "",
                false,
                false,
                false,
                true,
                Some(envs),
            )
        };

    match output {
        Some(x) => {
            return Ok(String::from_utf8_lossy(&x.stdout).into_owned());
        }
        None => {
            return Err("no output");
        }
    }
}