summaryrefslogtreecommitdiffstats
path: root/src/main.rs
blob: db349d364029dd97c1865f060fe3eb588fda1bf6 (plain)
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
mod recite;
use ctrlc;
use recite::path::prefresh;
use recite::Poem;
use std::io::{self, Write};
use std::path::Path;

/// Starts the main shell loop
///
/// # Arguments
/// * `paths` - A reference to a vector that holds a list to the shell $PATHs
/// * `prompt` - A string slice indicating the shell's prompt
///
/// # Examples
/// ```
/// fn main() {
///     let path = vec!["/bin"];
///     let path = path.into_iter().map(Path::new).collect();
///     let prompt = "|> ";
///     ...
///     repl(&path, prompt);
/// }
/// ```
fn repl(path: &Vec<&Path>, prompt: &str) {
    // Initial path refresh on startup
    let mut bins: Vec<String> = prefresh(path);

    // Main shell loop
    loop {
        // Output the prompt
        io::stdout().flush().unwrap();
        print!("{}", prompt);
        io::stdout().flush().unwrap();

        // Wait for user input
        let mut poetry = String::new();
        let bytes = io::stdin()
            .read_line(&mut poetry)
            .expect("dwvsh: error: unable to evaluate the input string");

        // Check if we've reached EOF (i.e. <C-d>)
        if bytes == 0 {
            println!("");
            break;
        }

        // Trim the input
        let poetry = String::from(poetry.trim());

        // Skip parsing if there is no poetry
        if poetry.is_empty() {
            continue;
        }

        // Parse a poem
        let poem = Poem::read(poetry);
        match poem {
            Some(poem) => {
                poem.recite(path, &mut bins);
            }
            None => {}
        }
    }
}

/// Shell entry
///
/// Shell setup and entry
fn main() {
    // Define paths
    // TODO: Hardcoded path should only be the fallback
    let path = vec![
        "/bin",
        "/sbin",
        "/usr/bin",
        "/usr/sbin",
        "/usr/local/bin",
        "/usr/local/sbin",
    ];
    let path = path.into_iter().map(Path::new).collect();

    // Set the prompt
    let prompt = "|> ";

    // Handle signals
    ctrlc::set_handler(move || {
        print!("\n{}", prompt);
        io::stdout().flush().unwrap();
    })
    .expect("dwvsh: signals: unable to set sigint handler");

    // Begin evaluating commands
    repl(&path, prompt);
}