Skip to content
Snippets Groups Projects
Unverified Commit 34be5bd7 authored by Michael Aaron Murphy's avatar Michael Aaron Murphy Committed by GitHub
Browse files

Merge pull request #677 from jD91mZM2/i_always_have_a_difficult_time_naming_branches

Move binary::main into actual main
parents d3194e58 9971c610
No related branches found
No related tags found
No related merge requests found
...@@ -47,6 +47,8 @@ mod builtins; ...@@ -47,6 +47,8 @@ mod builtins;
mod shell; mod shell;
mod ascii_helpers; mod ascii_helpers;
pub use shell::{Binary, Capture, Fork, IonError, IonResult, Shell, ShellBuilder}; pub use shell::binary::MAN_ION;
pub use shell::flags; pub use shell::flags;
pub use shell::status; pub use shell::status;
pub use shell::{Binary, Capture, Fork, IonError, IonResult, Shell, ShellBuilder};
pub use shell::pipe_exec::job_control::JobControl;
...@@ -7,22 +7,18 @@ mod terminate; ...@@ -7,22 +7,18 @@ mod terminate;
use self::prompt::{prompt, prompt_fn}; use self::prompt::{prompt, prompt_fn};
use self::readln::readln; use self::readln::readln;
use self::terminate::{terminate_quotes, terminate_script_quotes}; use self::terminate::{terminate_quotes, terminate_script_quotes};
use super::{FlowLogic, JobControl, Shell, ShellHistory}; use super::{FlowLogic, Shell, ShellHistory};
use super::flags::*;
use super::flow_control::Statement; use super::flow_control::Statement;
use super::status::*; use super::status::*;
use liner::{Buffer, Context}; use liner::{Buffer, Context};
use smallvec::SmallVec;
use std::env; use std::env;
use std::error::Error;
use std::fs::File; use std::fs::File;
use std::io::{stdout, Write};
use std::io::ErrorKind; use std::io::ErrorKind;
use std::iter::{self, FromIterator}; use std::iter;
use std::path::Path; use std::path::Path;
use std::process; use std::process;
const MAN_ION: &'static str = r#"NAME pub const MAN_ION: &'static str = r#"NAME
ion - ion shell ion - ion shell
SYNOPSIS SYNOPSIS
...@@ -44,9 +40,6 @@ OPTIONS ...@@ -44,9 +40,6 @@ OPTIONS
"#; "#;
pub trait Binary { pub trait Binary {
/// Launches the shell, parses arguments, and then diverges into one of the `execution`
/// paths.
fn main(self);
/// Parses and executes the arguments that were supplied to the shell. /// Parses and executes the arguments that were supplied to the shell.
fn execute_arguments<A: Iterator<Item = String>>(&mut self, args: A); fn execute_arguments<A: Iterator<Item = String>>(&mut self, args: A);
/// Creates an interactive session that reads from a prompt provided by /// Creates an interactive session that reads from a prompt provided by
...@@ -192,47 +185,6 @@ impl Binary for Shell { ...@@ -192,47 +185,6 @@ impl Binary for Shell {
} }
} }
fn main(mut self) {
let mut args = env::args().skip(1);
while let Some(path) = args.next() {
match path.as_str() {
"-n" | "--no-execute" => {
self.flags |= NO_EXEC;
continue;
}
"-c" => self.execute_arguments(args),
"-v" | "--version" => self.display_version(),
"-h" | "--help" => {
let stdout = stdout();
let mut stdout = stdout.lock();
match stdout
.write_all(MAN_ION.as_bytes())
.and_then(|_| stdout.flush())
{
Ok(_) => return,
Err(err) => panic!("{}", err.description().to_owned()),
}
}
_ => {
let mut array = SmallVec::from_iter(Some(path.clone().into()));
for arg in args {
array.push(arg.into());
}
self.variables.set_array("args", array);
if let Err(err) = self.execute_script(&path) {
eprintln!("ion: {}", err);
}
}
}
self.wait_for_background();
let previous_status = self.previous_status;
self.exit(previous_status);
}
self.execute_interactive();
}
fn display_version(&self) { fn display_version(&self) {
println!("{}", include!(concat!(env!("OUT_DIR"), "/version_string"))); println!("{}", include!(concat!(env!("OUT_DIR"), "/version_string")));
process::exit(0); process::exit(0);
......
mod assignments; mod assignments;
mod binary;
mod completer; mod completer;
mod flow; mod flow;
mod fork; mod fork;
mod history; mod history;
mod job; mod job;
mod pipe_exec;
pub mod flags; pub mod flags;
pub mod fork_function; pub mod fork_function;
pub mod status; pub mod status;
pub mod variables; pub mod variables;
pub(crate) mod binary;
pub(crate) mod colors; pub(crate) mod colors;
pub(crate) mod directory_stack; pub(crate) mod directory_stack;
pub(crate) mod flow_control; pub(crate) mod flow_control;
pub(crate) mod pipe_exec;
pub(crate) mod plugins; pub(crate) mod plugins;
pub(crate) mod signals; pub(crate) mod signals;
...@@ -71,7 +71,7 @@ pub struct Shell { ...@@ -71,7 +71,7 @@ pub struct Shell {
/// Note that the context is only available in an interactive session. /// Note that the context is only available in an interactive session.
pub(crate) context: Option<Context>, pub(crate) context: Option<Context>,
/// Contains the aliases, strings, and array variable maps. /// Contains the aliases, strings, and array variable maps.
pub(crate) variables: Variables, pub variables: Variables,
/// Contains the current state of flow control parameters. /// Contains the current state of flow control parameters.
flow_control: FlowControl, flow_control: FlowControl,
/// Contains the directory stack parameters. /// Contains the directory stack parameters.
...@@ -203,7 +203,8 @@ impl<'a> Shell { ...@@ -203,7 +203,8 @@ impl<'a> Shell {
} }
} }
pub(crate) fn exit(&mut self, status: i32) -> ! { /// Cleanly exit ion
pub fn exit(&mut self, status: i32) -> ! {
self.prep_for_exit(); self.prep_for_exit();
process::exit(status); process::exit(status);
} }
......
...@@ -20,7 +20,7 @@ pub(crate) fn set_foreground_as(pid: u32) { ...@@ -20,7 +20,7 @@ pub(crate) fn set_foreground_as(pid: u32) {
signals::unblock(); signals::unblock();
} }
pub(crate) trait JobControl { pub trait JobControl {
/// Waits for background jobs to finish before returning. /// Waits for background jobs to finish before returning.
fn wait_for_background(&mut self); fn wait_for_background(&mut self);
/// Takes a background tasks's PID and whether or not it needs to be continued; resumes the /// Takes a background tasks's PID and whether or not it needs to be continued; resumes the
......
extern crate ion_shell; extern crate ion_shell;
extern crate smallvec;
use ion_shell::JobControl;
use ion_shell::MAN_ION;
use ion_shell::flags::NO_EXEC;
use ion_shell::{Binary, ShellBuilder}; use ion_shell::{Binary, ShellBuilder};
use smallvec::SmallVec;
use std::env;
use std::error::Error;
use std::io::{stdout, Write};
use std::iter::FromIterator;
fn main() { fn main() {
ShellBuilder::new() let mut shell = ShellBuilder::new()
.install_signal_handler() .install_signal_handler()
.block_signals() .block_signals()
.set_unique_pid() .set_unique_pid()
.as_binary() .as_binary();
.main();
} let mut args = env::args().skip(1);
while let Some(path) = args.next() {
match path.as_str() {
"-n" | "--no-execute" => {
shell.flags |= NO_EXEC;
continue;
}
"-c" => shell.execute_arguments(args),
"-v" | "--version" => shell.display_version(),
"-h" | "--help" => {
let stdout = stdout();
let mut stdout = stdout.lock();
match stdout
.write_all(MAN_ION.as_bytes())
.and_then(|_| stdout.flush())
{
Ok(_) => return,
Err(err) => panic!("{}", err.description().to_owned()),
}
}
_ => {
let mut array = SmallVec::from_iter(Some(path.clone().into()));
for arg in args {
array.push(arg.into());
}
shell.variables.set_array("args", array);
if let Err(err) = shell.execute_script(&path) {
eprintln!("ion: {}", err);
}
}
}
// TODO: The `Binary` / `main()` logic should be implemented here, and not within the library. shell.wait_for_background();
\ No newline at end of file let previous_status = shell.previous_status;
shell.exit(previous_status);
}
shell.execute_interactive();
}
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment