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
|
use snafu::{OptionExt as _, ResultExt as _};
use std::os::unix::process::ExitStatusExt as _;
#[derive(Debug, snafu::Snafu)]
pub enum Error {
#[snafu(display("unknown builtin {}", cmd))]
UnknownBuiltin { cmd: String },
#[snafu(display(
"not enough parameters for {} (got {}, expected {})",
cmd, args.len(), expected
))]
NotEnoughParams {
cmd: String,
args: Vec<String>,
expected: u32,
},
#[snafu(display(
"too many parameters for {} (got {}, expected {})",
cmd, args.len(), expected
))]
TooManyParams {
cmd: String,
args: Vec<String>,
expected: u32,
},
#[snafu(display("failed to cd to {}: {}", dir, source))]
Chdir { dir: String, source: nix::Error },
#[snafu(display("failed to cd: $HOME not set"))]
ChdirUnknownHome,
}
pub type Result<T> = std::result::Result<T, Error>;
pub fn exec(cmd: &str, args: &[String]) -> Result<Builtin> {
Builtin::new(cmd, args)
}
pub struct Builtin {
cmd: String,
args: Vec<String>,
started: bool,
done: bool,
}
impl Builtin {
fn new(cmd: &str, args: &[String]) -> Result<Self> {
match cmd {
"cd" => Ok(Self {
cmd: cmd.to_string(),
args: args.to_vec(),
started: false,
done: false,
}),
_ => Err(Error::UnknownBuiltin {
cmd: cmd.to_string(),
}),
}
}
}
#[must_use = "streams do nothing unless polled"]
impl futures::stream::Stream for Builtin {
type Item = crate::eval::CommandEvent;
type Error = Error;
fn poll(&mut self) -> futures::Poll<Option<Self::Item>, Self::Error> {
if !self.started {
self.started = true;
Ok(futures::Async::Ready(Some(
crate::eval::CommandEvent::CommandStart(
self.cmd.clone(),
self.args.clone(),
),
)))
} else if !self.done {
self.done = true;
let res = match self.cmd.as_ref() {
"cd" => cd(&self.args),
_ => Err(Error::UnknownBuiltin {
cmd: self.cmd.clone(),
}),
};
res.map(|_| {
futures::Async::Ready(Some(
crate::eval::CommandEvent::CommandExit(
std::process::ExitStatus::from_raw(0),
),
))
})
.or_else(|e| match e {
Error::UnknownBuiltin { .. } => Err(e),
_ => Ok(futures::Async::Ready(Some(
crate::eval::CommandEvent::CommandExit(
std::process::ExitStatus::from_raw(256),
),
))),
})
} else {
Ok(futures::Async::Ready(None))
}
}
}
fn cd(args: &[String]) -> Result<()> {
snafu::ensure!(
args.len() <= 1,
TooManyParams {
cmd: "cd",
args,
expected: 1_u32,
}
);
let dir = if let Some(dir) = args.get(0) {
std::convert::From::from(dir)
} else {
std::env::var_os("HOME").context(ChdirUnknownHome)?
};
nix::unistd::chdir(dir.as_os_str()).context(Chdir {
dir: dir.to_string_lossy(),
})
}
|