summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorJesse Luehrs <doy@tozt.net>2022-01-09 22:56:33 -0500
committerJesse Luehrs <doy@tozt.net>2022-01-09 22:56:33 -0500
commitde77b9e370341fd2fdad06113779ef187a0f5f9d (patch)
treef68f6b01786f9d1fb06ed2d2dd3860e21a62cf02
parent3c30ee54cd8dc8c9bf43beb54ee1fe0292c07c90 (diff)
downloadnbsh-de77b9e370341fd2fdad06113779ef187a0f5f9d.tar.gz
nbsh-de77b9e370341fd2fdad06113779ef187a0f5f9d.zip
stop parsing command lines at all in the main shell process
-rw-r--r--src/parse.rs4
-rw-r--r--src/parse/ast.rs7
-rw-r--r--src/shell/history/mod.rs51
-rw-r--r--src/shell/mod.rs51
4 files changed, 23 insertions, 90 deletions
diff --git a/src/parse.rs b/src/parse.rs
index f2bee2c..7abc525 100644
--- a/src/parse.rs
+++ b/src/parse.rs
@@ -112,10 +112,6 @@ impl Error {
e,
}
}
-
- pub fn into_input(self) -> String {
- self.input
- }
}
impl std::fmt::Display for Error {
diff --git a/src/parse/ast.rs b/src/parse/ast.rs
index ca568eb..37e7fe0 100644
--- a/src/parse/ast.rs
+++ b/src/parse/ast.rs
@@ -9,7 +9,6 @@ struct Shell;
#[derive(Debug, PartialEq, Eq)]
pub struct Commands {
commands: Vec<Command>,
- input_string: String,
}
impl Commands {
@@ -29,16 +28,10 @@ impl Commands {
&self.commands
}
- pub fn input_string(&self) -> &str {
- &self.input_string
- }
-
fn build_ast(commands: pest::iterators::Pair<Rule>) -> Self {
assert!(matches!(commands.as_rule(), Rule::commands));
- let input_string = commands.as_str().to_string();
Self {
commands: commands.into_inner().map(Command::build_ast).collect(),
- input_string,
}
}
}
diff --git a/src/shell/history/mod.rs b/src/shell/history/mod.rs
index b241ca5..e206a9b 100644
--- a/src/shell/history/mod.rs
+++ b/src/shell/history/mod.rs
@@ -88,7 +88,7 @@ impl History {
pub async fn run(
&mut self,
- ast: crate::parse::ast::Commands,
+ cmdline: &str,
env: &Env,
event_w: async_std::channel::Sender<Event>,
) -> anyhow::Result<usize> {
@@ -96,14 +96,14 @@ impl History {
let (resize_w, resize_r) = async_std::channel::unbounded();
let entry = crate::mutex::new(Entry::new(
- ast.input_string().to_string(),
+ cmdline.to_string(),
env.clone(),
self.size,
input_w,
resize_w,
));
run_commands(
- ast,
+ cmdline.to_string(),
crate::mutex::clone(&entry),
env.clone(),
input_r,
@@ -115,39 +115,6 @@ impl History {
Ok(self.entries.len() - 1)
}
- pub async fn parse_error(
- &mut self,
- e: crate::parse::Error,
- env: &Env,
- event_w: async_std::channel::Sender<Event>,
- ) -> anyhow::Result<usize> {
- // XXX would be great to not have to do this
- let (input_w, input_r) = async_std::channel::unbounded();
- let (resize_w, resize_r) = async_std::channel::unbounded();
- input_w.close();
- input_r.close();
- resize_w.close();
- resize_r.close();
-
- let err_str = format!("{}", e);
- let entry = crate::mutex::new(Entry::new(
- e.into_input(),
- env.clone(),
- self.size,
- input_w,
- resize_w,
- ));
- self.entries.push(crate::mutex::clone(&entry));
-
- let mut entry = entry.lock_arc().await;
- entry.process(err_str.replace('\n', "\r\n").as_bytes());
- let mut env = env.clone();
- env.set_status(async_std::process::ExitStatus::from_raw(1 << 8));
- entry.finish(env, event_w).await;
-
- Ok(self.entries.len() - 1)
- }
-
pub async fn entry(&self, idx: usize) -> crate::mutex::Guard<Entry> {
self.entries[idx].lock_arc().await
}
@@ -255,7 +222,7 @@ impl std::iter::DoubleEndedIterator for VisibleEntries {
}
fn run_commands(
- ast: crate::parse::ast::Commands,
+ cmdline: String,
entry: crate::mutex::Mutex<Entry>,
mut env: Env,
input_r: async_std::channel::Receiver<Vec<u8>>,
@@ -286,7 +253,8 @@ fn run_commands(
};
let status =
- match spawn_commands(&ast, &pty, &mut env, event_w.clone()).await
+ match spawn_commands(&cmdline, &pty, &mut env, event_w.clone())
+ .await
{
Ok(status) => status,
Err(e) => {
@@ -294,8 +262,7 @@ fn run_commands(
entry.process(
format!(
"nbsh: failed to spawn {}: {}\r\n",
- ast.input_string(),
- e
+ cmdline, e
)
.as_bytes(),
);
@@ -314,7 +281,7 @@ fn run_commands(
}
async fn spawn_commands(
- commands: &crate::parse::ast::Commands,
+ cmdline: &str,
pty: &pty::Pty,
env: &mut Env,
event_w: async_std::channel::Sender<Event>,
@@ -340,7 +307,7 @@ async fn spawn_commands(
// be used after this because from_raw_fd takes it by move
write_env(
unsafe { async_std::fs::File::from_raw_fd(to_w) },
- commands.input_string(),
+ cmdline,
env,
)
.await?;
diff --git a/src/shell/mod.rs b/src/shell/mod.rs
index 4501ce9..629f482 100644
--- a/src/shell/mod.rs
+++ b/src/shell/mod.rs
@@ -320,25 +320,13 @@ impl Shell {
self.readline.clear_input();
let entry = self.history.entry(idx).await;
let input = entry.cmd();
- let idx = match crate::parse::ast::Commands::parse(input)
- {
- Ok(ast) => {
- let idx = self
- .history
- .run(ast, &self.env, event_w.clone())
- .await
- .unwrap();
- self.set_focus(Focus::History(idx), Some(entry))
- .await;
- self.hide_readline = true;
- idx
- }
- Err(e) => self
- .history
- .parse_error(e, &self.env, event_w.clone())
- .await
- .unwrap(),
- };
+ let idx = self
+ .history
+ .run(input, &self.env, event_w.clone())
+ .await
+ .unwrap();
+ self.set_focus(Focus::History(idx), Some(entry)).await;
+ self.hide_readline = true;
self.env.set_idx(idx + 1);
} else {
self.set_focus(Focus::Readline, None).await;
@@ -439,24 +427,13 @@ impl Shell {
textmode::Key::Ctrl(b'm') => {
let input = self.readline.input();
if !input.is_empty() {
- let idx = match crate::parse::ast::Commands::parse(input)
- {
- Ok(ast) => {
- let idx = self
- .history
- .run(ast, &self.env, event_w.clone())
- .await
- .unwrap();
- self.set_focus(Focus::History(idx), None).await;
- self.hide_readline = true;
- idx
- }
- Err(e) => self
- .history
- .parse_error(e, &self.env, event_w.clone())
- .await
- .unwrap(),
- };
+ let idx = self
+ .history
+ .run(input, &self.env, event_w.clone())
+ .await
+ .unwrap();
+ self.set_focus(Focus::History(idx), None).await;
+ self.hide_readline = true;
self.env.set_idx(idx + 1);
self.readline.clear_input();
}