summaryrefslogtreecommitdiffstats
path: root/src/main.rs
blob: 898d19ec9be6bf1c3159a59cebd2cbe2b1393e26 (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
95
96
97
98
99
100
101
102
103
104
105
106
107
use std::io::{self, Write};
use std::sync::{Arc, Mutex};
mod path;
mod poem;
use poem::{read::Readable, recite::Reciteable, Poem};
mod compose;
use compose::Environment;

/// Starts the main shell loop
///
/// # Arguments
/// * `prompt` - A string slice indicating the shell's prompt
/// * `at_prompt` - A mutex, indicating whether or not user is at the prompt
///
/// # Examples
/// ```
/// fn main() {
///     let prompt = "|> ";
///     let mut at_prompt = Arc::new(Mutex::new(false));
///     ...
///     repl(prompt, &mut at_prompt);
/// }
/// ```
fn repl(prompt: &str, at_prompt: &mut Arc<Mutex<bool>>, env: &mut Environment) {
    // Initial path refresh on startup
    env.bins = path::refresh();

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

        // At the prompt
        *at_prompt.lock().unwrap() = true;

        // 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;
        }

        // Not at the prompt
        *at_prompt.lock().unwrap() = false;

        // Parse the poem
        let poem = Poem::read(poetry);
        let poem = match poem {
            Ok(poem) => poem,
            Err(e) => {
                eprintln!("dwvsh: {}", e.to_string().to_lowercase());
                continue;
            }
        };

        // Recite the poem
        match poem.recite(env, None) {
            Ok(_) => {}
            Err(e) => eprintln!("dwvsh: {}", e.to_string().to_lowercase()),
        }
    }
}

/// Shell entry
///
/// Shell setup and entry
fn main() {
    // Compose the environment for dwvsh
    // TODO: All instances of `env!("HOME")` need to be changed to use env::var
    // TODO: Will probably need to set $HOME in dwv{profile,login} via passwd
    let mut env = compose::env();

    // Set the prompt
    let prompt = "|> ";
    let mut at_prompt = Arc::new(Mutex::new(false));

    // Handle signals
    unsafe {
        let at_prompt = Arc::clone(&at_prompt);
        signal_hook::low_level::register(signal_hook::consts::SIGINT, move || {
            if *at_prompt.lock().unwrap() {
                print!("\n{}", prompt);
                io::stdout().flush().unwrap();
            } else {
                println!();
            }
        })
        .unwrap();
    };

    // Begin evaluating commands
    repl(prompt, &mut at_prompt, &mut env);
}