summaryrefslogtreecommitdiffstats
path: root/src/main.rs
blob: 48d2b2b27bd8aad2ebbe2888f1649f48b07f9811 (plain) (blame)
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
// will uncomment this once it is closer to release
// #![warn(clippy::cargo)]
#![warn(clippy::pedantic)]
#![warn(clippy::nursery)]
#![warn(clippy::as_conversions)]
#![warn(clippy::get_unwrap)]
#![allow(clippy::cognitive_complexity)]
#![allow(clippy::missing_const_for_fn)]
#![allow(clippy::similar_names)]
#![allow(clippy::struct_excessive_bools)]
#![allow(clippy::too_many_arguments)]
#![allow(clippy::too_many_lines)]
#![allow(clippy::type_complexity)]
// this isn't super relevant in a binary - if it's actually a problem, we'll
// just get a compilation failure
#![allow(clippy::future_not_send)]

mod env;
mod format;
mod info;
mod mutex;
mod parse;
mod prelude;
mod runner;
mod shell;

use prelude::*;

async fn async_main(
    shell_write: Option<&async_std::fs::File>,
) -> anyhow::Result<i32> {
    if std::env::args().nth(1).as_deref() == Some("-c") {
        return runner::run(
            std::env::args().nth(2).as_deref().unwrap(),
            shell_write,
        )
        .await;
    }

    shell::main().await
}

fn main() {
    // need to do this here because the async-std executor allocates some fds,
    // and so in the case where we aren't being called from the main shell and
    // fd 3 wasn't preallocated in advance, we need to be able to tell that
    // before async-std opens something on fd 3
    let shell_write = if nix::sys::stat::fstat(3).is_ok() {
        nix::fcntl::fcntl(
            3,
            nix::fcntl::FcntlArg::F_SETFD(nix::fcntl::FdFlag::FD_CLOEXEC),
        )
        .unwrap();
        // Safety: we don't create File instances for or read/write data on fd
        // 3 anywhere else
        Some(unsafe { async_std::fs::File::from_raw_fd(3) })
    } else {
        None
    };

    match async_std::task::block_on(async_main(shell_write.as_ref())) {
        Ok(code) => {
            std::process::exit(code);
        }
        Err(e) => {
            eprintln!("nbsh: {}", e);
            std::process::exit(1);
        }
    };
}