This commit is contained in:
nora 2024-02-22 19:58:05 +01:00
parent 18b5d0b158
commit e38e0ba37b
4 changed files with 75 additions and 6 deletions

48
src/execute.rs Normal file
View file

@ -0,0 +1,48 @@
use std::process::ExitStatus;
use crate::parse::Command;
pub fn execute(cmds: &[Command<'_>]) {
for cmd in cmds {
execute_command(cmd);
}
}
fn execute_command(cmd: &Command<'_>) -> ExitStatus {
match cmd {
Command::Single(cmd) => {
if cmd.exclamation {
todo!()
}
if cmd.time {
todo!()
}
if cmd.terminator.is_some() {
todo!()
}
if !cmd.rest.is_empty() {
todo!()
}
let cmds = &cmd.initial;
if !cmds.rest.is_empty() {
todo!()
}
let program = &cmds.initial.tokens[0].value;
let mut proc = std::process::Command::new(program.as_ref());
proc.args(
cmds.initial.tokens[1..]
.iter()
.map(|t| -> &str { &t.value.as_ref() }),
);
let mut proc = proc.spawn().unwrap();
let status = proc.wait().unwrap();
status
}
Command::Subshell(_) => todo!(),
Command::Block(_) => todo!(),
}
}

View file

@ -1,13 +1,17 @@
pub mod parse; pub mod parse;
pub mod execute;
use std::sync::Arc; use std::sync::Arc;
use anyhow::{anyhow, bail, Context, Result}; use anyhow::{anyhow, bail, Context, Result};
use execute::execute;
pub fn bash_it(args: impl Iterator<Item = String>) -> Result<()> { pub fn bash_it(args: impl Iterator<Item = String>) -> Result<()> {
let (filename, src) = parse_args_into_src(args)?; let (filename, src) = parse_args_into_src(args)?;
parse::parse(filename, &src)?; let command = parse::parse(filename, &src)?;
execute(&command);
Ok(()) Ok(())
} }

View file

@ -1,3 +1,3 @@
fn main() -> anyhow::Result<()> { fn main() -> anyhow::Result<()> {
brash::bash_it(std::env::args()) brash::bash_it(std::env::args().skip(1))
} }

View file

@ -4,10 +4,27 @@ use std::{borrow::Cow, iter, sync::Arc};
use anyhow::{bail, Result}; use anyhow::{bail, Result};
pub fn parse(filename: Arc<str>, src: &str) -> Result<()> { pub fn parse(filename: Arc<str>, src: &str) -> Result<Vec<Command>> {
let tokens = lex(filename, src)?; let mut tokens = lex(filename, src)?;
dbg!(tokens); dbg!(&tokens);
Ok(())
if tokens.get(0).map_or(false, |t| t.value.is_empty()) {
// bad hack or whatever, everything in here is a bad hack
tokens.remove(0);
}
let command = SimpleCommand { tokens };
Ok(vec![Command::Single(Pipeline {
time: false,
exclamation: false,
initial: CommandList {
initial: command,
rest: vec![],
},
rest: vec![],
terminator: None,
})])
} }
/* /*