Readme and clippy cleanups

This commit is contained in:
Ava Apples Affine 2023-05-28 23:22:49 +00:00
parent cbd52de91b
commit 8cc0202a7b
13 changed files with 159 additions and 197 deletions

View file

@ -37,10 +37,7 @@ use {
},
std::{
collections::VecDeque,
cell::{
RefCell, RefMut,
BorrowMutError,
},
cell::RefCell,
io::Result as IOResult,
rc::Rc,
fs::{
@ -109,9 +106,9 @@ pub fn unign_sigs() {
// TODO: trigger this on SIGCHLD instead of traditional shell once per input
pub fn check_jobs(state: &mut ShellState) {
let mut idx = 0 as usize;
let mut idx = 0usize;
while idx < state.children.len() {
if let Ok(_) = state.children[idx].try_wait() {
if state.children[idx].try_wait().is_ok() {
state.children.remove(idx);
continue;
}
@ -134,7 +131,7 @@ pub fn args_from_ast(ast: &Seg, syms: &mut SymTable) -> Vec<String> {
match *res_ctr {
Ctr::Lambda(_) => false,
Ctr::Seg(_) => false,
Ctr::String(s) => args.push(s.clone()) == (),
Ctr::String(s) => args.push(s) == (),
_ => args.push(res_ctr.to_string()) == (),
}
} else {
@ -178,14 +175,12 @@ fn launch_command(
newchld = Command::new(path)
.pre_exec(move || -> IOResult<()> {
let pid = unistd::getpid();
if let Err(_) = unistd::setpgid(pid, pid) {
if unistd::setpgid(pid, pid).is_err() {
// crying would be nice... if you had eyes
}
if !background {
if let Err(_) = unistd::tcsetpgrp(0, pid) {
// you wish to scream, but fork() has taken from you your throat
}
if !background &&unistd::tcsetpgrp(0, pid).is_err() {
// you wish to scream, but fork() has taken from you your throat
}
unign_sigs();
@ -255,8 +250,7 @@ fn make_foreground(pid: u32, state: &mut ShellState) -> Result<(), String>{
state.last_exit_code = exit
.code()
.or(Some(-1))
.unwrap();
.unwrap_or(-1);
break;
}
@ -298,28 +292,26 @@ fn load_callback(ast: &Seg, syms: &mut SymTable, state: &mut ShellState) -> Resu
Err(start_trace(
("load", "empty command")
.into()))
} else {
if let Some(filepath) = run::find_on_path(args.pop_front().unwrap()) {
if let Err(e) = launch_command(
filepath,
&Vec::from(args.make_contiguous()),
Stdio::inherit(),
Stdio::inherit(),
Stdio::inherit(),
false,
state,
) {
Err(start_trace(
("load", e)
.into()))
} else {
Ok(Ctr::Integer(state.last_exit_code.into()))
}
} else {
} else if let Some(filepath) = run::find_on_path(args.pop_front().unwrap()) {
if let Err(e) = launch_command(
filepath,
&Vec::from(args.make_contiguous()),
Stdio::inherit(),
Stdio::inherit(),
Stdio::inherit(),
false,
state,
) {
Err(start_trace(
("load", "binary not found on PATH")
("load", e)
.into()))
} else {
Ok(Ctr::Integer(state.last_exit_code.into()))
}
} else {
Err(start_trace(
("load", "binary not found on PATH")
.into()))
}
}
}
@ -385,32 +377,30 @@ fn pipe_callback(ast: &Seg, syms: &mut SymTable, state: &mut ShellState) -> Resu
Err(start_trace(
("pipe", err)
.into()))
} else {
if lastpid > 0 {
if let Some(pos) = state.children
.iter()
.position(|x| x.id() == lastpid)
{
let chld = state.children.remove(pos);
let exit = chld.wait_with_output()
.expect("failed to wait on last child");
state.last_exit_code = exit.status
.code()
.or_else(|| {Some(-1)})
.unwrap();
} else if lastpid > 0 {
if let Some(pos) = state.children
.iter()
.position(|x| x.id() == lastpid)
{
let chld = state.children.remove(pos);
let exit = chld.wait_with_output()
.expect("failed to wait on last child");
state.last_exit_code = exit.status
.code()
.or(Some(-1))
.unwrap();
println!("{}", String::from_utf8_lossy(&exit.stdout));
Ok(Ctr::Integer(state.last_exit_code.into()))
} else {
Err(start_trace(
("pipe", format!("lost last child (pid {})", lastpid))
.into()))
}
println!("{}", String::from_utf8_lossy(&exit.stdout));
Ok(Ctr::Integer(state.last_exit_code.into()))
} else {
Err(start_trace(
("pipe", "impossible error state".to_string())
("pipe", format!("lost last child (pid {})", lastpid))
.into()))
}
} else {
Err(start_trace(
("pipe", "impossible error state".to_string())
.into()))
}
}
@ -437,30 +427,28 @@ fn load_to_string_callback(ast: &Seg, syms: &mut SymTable, state: &mut ShellStat
Err(start_trace(
("load-to-string", "command is empty")
.into()))
} else {
if let Some(filepath) = run::find_on_path(args.pop_front().unwrap()) {
let res = Command::new(filepath).args(args).output();
if let Ok(output) = res {
if let Some(code) = output.status.code() {
state.last_exit_code = code;
}
if let Ok(string) = String::from_utf8(output.stdout) {
Ok(Ctr::String(string.trim_end().to_string()))
} else {
Err(start_trace(
("load-to-string", format!("couldn't marshall utf-8 command output into a string"))
.into()))
}
} else if let Some(filepath) = run::find_on_path(args.pop_front().unwrap()) {
let res = Command::new(filepath).args(args).output();
if let Ok(output) = res {
if let Some(code) = output.status.code() {
state.last_exit_code = code;
}
if let Ok(string) = String::from_utf8(output.stdout) {
Ok(Ctr::String(string.trim_end().to_string()))
} else {
Err(start_trace(
("load-to-string", format!("{}", res.err().unwrap()))
("load-to-string", "couldn't marshall utf-8 command output into a string")
.into()))
}
} else {
Err(start_trace(
("load-to-string", "binary not found on PATH")
("load-to-string", format!("{}", res.err().unwrap()))
.into()))
}
} else {
Err(start_trace(
("load-to-string", "binary not found on PATH")
.into()))
}
}
}
@ -514,7 +502,7 @@ fn load_with_callback(ast: &Seg, syms: &mut SymTable, state: &mut ShellState) ->
return false
}
stdin = Some(Stdio::from(input.ok().unwrap()));
return true
true
},
"stdout" => {
if !stdout.is_none() {
@ -531,7 +519,7 @@ fn load_with_callback(ast: &Seg, syms: &mut SymTable, state: &mut ShellState) ->
return false
}
stdout = Some(Stdio::from(out.ok().unwrap()));
return true
true
},
"stderr" => {
if !stderr.is_none() {
@ -548,7 +536,7 @@ fn load_with_callback(ast: &Seg, syms: &mut SymTable, state: &mut ShellState) ->
return false
}
stderr = Some(Stdio::from(err.ok().unwrap()));
return true
true
},
_ => {
e = Some(format!("can only override stdin, stdout, or stderr"));
@ -668,28 +656,26 @@ fn bg_callback(ast: &Seg, syms: &mut SymTable, state: &mut ShellState) -> Result
Err(start_trace(
("bg", "empty command")
.into()))
} else {
if let Some(filepath) = run::find_on_path(args.pop_front().unwrap()) {
if let Err(e) = launch_command(
filepath,
&Vec::from(args.make_contiguous()),
Stdio::inherit(),
Stdio::inherit(),
Stdio::inherit(),
true,
state,
) {
Err(start_trace(
("bg", e)
.into()))
} else {
Ok(Ctr::Integer(state.last_exit_code.into()))
}
} else {
} else if let Some(filepath) = run::find_on_path(args.pop_front().unwrap()) {
if let Err(e) = launch_command(
filepath,
&Vec::from(args.make_contiguous()),
Stdio::inherit(),
Stdio::inherit(),
Stdio::inherit(),
true,
state,
) {
Err(start_trace(
("bg", "binary not found on PATH")
("bg", e)
.into()))
} else {
Ok(Ctr::Integer(state.last_exit_code.into()))
}
} else {
Err(start_trace(
("bg", "binary not found on PATH")
.into()))
}
}
}
@ -727,7 +713,7 @@ const CD_DOCSTRING: &str =
fn cd_callback(ast: &Seg, _syms: &mut SymTable) -> Result<Ctr, Traceback> {
if let Ctr::String(ref dir) = *ast.car {
let dirp = Path::new(dir);
if let Err(s) = set_current_dir(&dirp) {
if let Err(s) = set_current_dir(dirp) {
Err(start_trace(
("cd", s.to_string())
.into()))
@ -746,7 +732,7 @@ pub fn load_posix_shell(syms: &mut SymTable, shell_state: Rc<RefCell<ShellState>
let pgid_res = unistd::getpgid(Some(pid));
let sid_res = unistd::getsid(Some(pid));
if !pgid_res.is_ok() || !sid_res.is_ok() {
if pgid_res.is_err() || sid_res.is_err() {
panic!("couldn't get pgid")
}
let pgid = pgid_res.ok().unwrap();
@ -760,14 +746,14 @@ pub fn load_posix_shell(syms: &mut SymTable, shell_state: Rc<RefCell<ShellState>
state.parent_sid = sid;
if let Ok(attr) = tcgetattr(0) {
state.attr = Some(attr.clone());
shattr = attr.clone();
shattr = attr;
} else {
panic!("couldn't get term attrs");
}
}
let term_pgrp_res = unistd::tcgetpgrp(0);
if !term_pgrp_res.is_ok() {
if term_pgrp_res.is_err() {
panic!("couldn't get terminal's pgrp: {:?}", term_pgrp_res)
}
@ -898,16 +884,13 @@ pub fn load_posix_shell(syms: &mut SymTable, shell_state: Rc<RefCell<ShellState>
symtable,
&mut lts_ss
.try_borrow_mut()
.or(Ok::<RefMut<'_, ShellState>, BorrowMutError>(
RefCell::from(ShellState{
.unwrap_or(RefCell::from(ShellState{
parent_pid: pid,
parent_sid: pgid,
children: vec![],
last_exit_code: 0,
attr: Some(shattr.clone()),
}).borrow_mut()))
.unwrap()
)
})),
..Default::default()
},