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
#![allow(dead_code)]
extern crate errno;
extern crate exec;
extern crate glob;
extern crate libc;
extern crate linefeed;
extern crate nix;
extern crate regex;
extern crate sqlite;
extern crate time;

#[macro_use]
extern crate nom;

mod types;

#[macro_use]
mod tools;

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

use tools::CommandResult;

/// 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").unwrap();
///     println!("out1: {:?}", out1.stdout);
///
///     let out2 = cicada::run("ls | wc").unwrap();
///     println!("out2: {:?}", out2.stdout);
///
///     let out3 = cicada::run("date >> out.txt").unwrap();
///     println!("out3: {:?}", out3.stdout);
///
///     let out4 = cicada::run("cat out.txt").unwrap();
///     println!("out4: {:?}", out4.stdout);
/// }
/// ```
///
/// Output:
///
/// ```no-run
/// out1: "Cargo.lock\nCargo.toml\nsrc\ntarget\n"
/// out2: "       4       4      33\n"
/// out3: ""
/// out4: "Fri Oct  6 14:53:25 CST 2017\n"
/// ```
pub fn run(line: &str) -> Result<CommandResult, &str> {
    execute::run(line)
}