Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 13 additions & 0 deletions src/commands.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ pub(crate) struct BuiltinResult {
pub status: i32,
pub stdout: Vec<u8>,
pub stderr: Vec<u8>,
pub exit: bool,
}

impl BuiltinResult {
Expand All @@ -28,6 +29,7 @@ impl BuiltinResult {
status,
stdout: Vec::new(),
stderr: Vec::new(),
exit: false,
}
}

Expand All @@ -36,6 +38,7 @@ impl BuiltinResult {
status,
stdout,
stderr: Vec::new(),
exit: false,
}
}

Expand All @@ -44,6 +47,16 @@ impl BuiltinResult {
status,
stdout: Vec::new(),
stderr,
exit: false,
}
}

fn exit(status: i32) -> Self {
Self {
status,
stdout: Vec::new(),
stderr: Vec::new(),
exit: true,
}
}
}
Expand Down
2 changes: 1 addition & 1 deletion src/commands/exit.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,5 +5,5 @@ pub(crate) fn run(argv: &[String]) -> BuiltinResult {
.first()
.and_then(|s| s.parse::<i32>().ok())
.unwrap_or(0);
BuiltinResult::status(code)
BuiltinResult::exit(code)
}
2 changes: 2 additions & 0 deletions src/commands/introspection.rs
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ pub(crate) fn type_(shell: &Shell, argv: &[String]) -> BuiltinResult {
status,
stdout,
stderr,
exit: false,
}
}

Expand All @@ -58,5 +59,6 @@ fn command_v(shell: &Shell, names: &[String]) -> BuiltinResult {
status,
stdout,
stderr: Vec::new(),
exit: false,
}
}
22 changes: 14 additions & 8 deletions src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,33 +4,36 @@ use std::env;
use std::io::{self, Write};

fn main() {
if let Err(err) = run() {
eprintln!("rysh: {err:#}");
std::process::exit(1);
match run() {
Ok(status) => std::process::exit(status),
Err(err) => {
eprintln!("rysh: {err:#}");
std::process::exit(1);
}
}
}

fn run() -> Result<()> {
fn run() -> Result<i32> {
let mut args = env::args().skip(1);
let mut shell = Shell::new();

match args.next().as_deref() {
Some("-c") => {
let source = args.next().context("missing command after -c")?;
let status = shell.run_script(&source, RunOptions::default())?;
std::process::exit(status);
Ok(status)
}
Some(path) => {
let source = std::fs::read_to_string(rysh::path_for_cli(path))
.with_context(|| format!("failed to read script {path}"))?;
let status = shell.run_script(&source, RunOptions::default())?;
std::process::exit(status);
Ok(status)
}
None => repl(shell),
}
}

fn repl(mut shell: Shell) -> Result<()> {
fn repl(mut shell: Shell) -> Result<i32> {
let stdin = io::stdin();
let mut line = String::new();

Expand All @@ -41,13 +44,16 @@ fn repl(mut shell: Shell) -> Result<()> {
line.clear();
if stdin.read_line(&mut line)? == 0 {
println!();
return Ok(());
return Ok(0);
}

let trimmed = line.trim_end_matches(['\r', '\n']);
if trimmed.is_empty() {
continue;
}
shell.run_script(trimmed, RunOptions::default())?;
if let Some(status) = shell.take_exit_status() {
return Ok(status);
}
}
}
39 changes: 38 additions & 1 deletion src/runtime.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ pub struct RunOptions {}
pub struct Shell {
pub(crate) vars: HashMap<String, String>,
last_status: i32,
exit_status: Option<i32>,
}

impl Default for Shell {
Expand All @@ -29,6 +30,7 @@ impl Shell {
Self {
vars: env::vars().collect(),
last_status: 0,
exit_status: None,
}
}

Expand All @@ -45,6 +47,9 @@ impl Shell {
if should_run {
status = self.run_pipeline(&pipeline)?;
self.last_status = status;
if self.exit_status.is_some() {
break;
}
}
}

Expand All @@ -67,6 +72,9 @@ impl Shell {
status = output.status;
stdout.extend(output.stdout);
self.last_status = status;
if self.exit_status.is_some() {
break;
}
}
}

Expand All @@ -77,6 +85,10 @@ impl Shell {
resolve_program(name, &self.vars).ok()
}

pub fn take_exit_status(&mut self) -> Option<i32> {
self.exit_status.take()
}

fn run_pipeline(&mut self, pipeline: &Pipeline) -> Result<i32> {
Ok(self.run_pipeline_inner(pipeline, false)?.status)
}
Expand All @@ -101,13 +113,20 @@ impl Shell {
let is_last = idx + 1 == pipeline.commands.len();
let output = self.run_command(command, input.take(), !is_last || capture_stdout)?;
status = output.status;
if output.exit {
return Ok(output);
}
if is_last {
stdout = output.stdout;
} else {
input = Some(output.stdout);
}
}
Ok(CommandOutput { status, stdout })
Ok(CommandOutput {
status,
stdout,
exit: false,
})
}

fn run_command(
Expand Down Expand Up @@ -160,9 +179,13 @@ impl Shell {
};

write_builtin_streams(command, capture_stdout, &result.stdout, &result.stderr)?;
if result.exit {
self.exit_status = Some(result.status);
}
Ok(Some(CommandOutput {
status: result.status,
stdout: result.stdout,
exit: result.exit,
}))
}

Expand Down Expand Up @@ -210,6 +233,7 @@ impl Shell {
Ok(CommandOutput {
status: output.status.code().unwrap_or(1),
stdout: output.stdout,
exit: false,
})
}

Expand Down Expand Up @@ -294,6 +318,7 @@ impl Shell {
Self {
vars: self.vars.clone(),
last_status: self.last_status,
exit_status: None,
}
}
}
Expand Down Expand Up @@ -503,13 +528,15 @@ fn resolve_program(name: &str, vars: &HashMap<String, String>) -> Result<PathBuf
struct CommandOutput {
status: i32,
stdout: Vec<u8>,
exit: bool,
}

impl CommandOutput {
fn status(status: i32) -> Self {
Self {
status,
stdout: Vec::new(),
exit: false,
}
}
}
Expand Down Expand Up @@ -541,6 +568,16 @@ mod tests {
);
}

#[test]
fn exit_stops_following_commands() {
let mut shell = Shell::new();
let (status, stdout) = shell.run_script_capture("exit 7; echo no").unwrap();

assert_eq!(status, 7);
assert_eq!(shell.take_exit_status(), Some(7));
assert!(stdout.is_empty());
}

#[cfg(windows)]
#[test]
fn cd_accepts_msys_drive_path() {
Expand Down
11 changes: 11 additions & 0 deletions tests/cli.rs
Original file line number Diff line number Diff line change
Expand Up @@ -88,3 +88,14 @@ fn type_reports_builtins() {
"cd is a shell builtin\n"
);
}

#[test]
fn exit_stops_following_commands_and_sets_status() {
let output = Command::new(env!("CARGO_BIN_EXE_rysh"))
.args(["-c", "exit 7; echo no"])
.output()
.unwrap();

assert_eq!(output.status.code(), Some(7));
assert!(output.stdout.is_empty());
}
Loading