mirror of
https://github.com/Noratrieb/brash.git
synced 2026-01-14 13:45:02 +01:00
x
This commit is contained in:
parent
18b5d0b158
commit
e38e0ba37b
4 changed files with 75 additions and 6 deletions
48
src/execute.rs
Normal file
48
src/execute.rs
Normal 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!(),
|
||||
}
|
||||
}
|
||||
|
|
@ -1,13 +1,17 @@
|
|||
pub mod parse;
|
||||
pub mod execute;
|
||||
|
||||
use std::sync::Arc;
|
||||
|
||||
use anyhow::{anyhow, bail, Context, Result};
|
||||
use execute::execute;
|
||||
|
||||
pub fn bash_it(args: impl Iterator<Item = String>) -> Result<()> {
|
||||
let (filename, src) = parse_args_into_src(args)?;
|
||||
|
||||
parse::parse(filename, &src)?;
|
||||
let command = parse::parse(filename, &src)?;
|
||||
|
||||
execute(&command);
|
||||
|
||||
Ok(())
|
||||
}
|
||||
|
|
|
|||
|
|
@ -1,3 +1,3 @@
|
|||
fn main() -> anyhow::Result<()> {
|
||||
brash::bash_it(std::env::args())
|
||||
brash::bash_it(std::env::args().skip(1))
|
||||
}
|
||||
|
|
|
|||
25
src/parse.rs
25
src/parse.rs
|
|
@ -4,10 +4,27 @@ use std::{borrow::Cow, iter, sync::Arc};
|
|||
|
||||
use anyhow::{bail, Result};
|
||||
|
||||
pub fn parse(filename: Arc<str>, src: &str) -> Result<()> {
|
||||
let tokens = lex(filename, src)?;
|
||||
dbg!(tokens);
|
||||
Ok(())
|
||||
pub fn parse(filename: Arc<str>, src: &str) -> Result<Vec<Command>> {
|
||||
let mut tokens = lex(filename, src)?;
|
||||
dbg!(&tokens);
|
||||
|
||||
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,
|
||||
})])
|
||||
}
|
||||
|
||||
/*
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue