Permalink
Browse files

Initial Ctrl+C Signal Handling Support (#85)

This change implements the foundations for handling signals.
Specifically, only the SIGINT signal is being handled right now.
SIGINT will kill active child processes, but it will not yet
break control flow.
  • Loading branch information...
mmstick committed Jun 20, 2017
1 parent 0b26e17 commit 9e02244a959f94280400c164f97d8d3a4834bdd1
Showing with 243 additions and 20 deletions.
  1. +191 −0 Cargo.lock
  2. +4 −1 Cargo.toml
  3. +41 −18 src/main.rs
  4. +7 −1 src/shell/mod.rs
View

Some generated files are not rendered by default. Learn more.

Oops, something went wrong.
View
@@ -19,12 +19,15 @@ name = "ion"
[dependencies]
fnv = "1.0"
futures = "0.1"
glob = "0.2"
liner = "0.2"
permutate = "0.3"
unicode-segmentation = "1.2"
smallvec = "0.4"
smallstring = "0.1"
tokio-core = "0.1"
tokio-signal = "0.1"
[target.'cfg(all(unix, not(target_os = "redox")))'.dependencies]
users = "0.5.1"
@@ -35,7 +38,7 @@ ansi_term = "0.9"
version_check = "0.1"
[profile.release]
# debug = true
debug = true
# rustflags = [ "-C", "target-cpu=native"]
lto = true
panic = "abort"
View
@@ -6,10 +6,13 @@
// extern crate alloc_system;
extern crate fnv;
extern crate futures;
extern crate glob;
extern crate liner;
extern crate smallvec;
extern crate smallstring;
extern crate tokio_core;
extern crate tokio_signal;
#[cfg(all(unix, not(target_os = "redox")))]
extern crate users as users_unix;
@@ -21,28 +24,48 @@ mod ascii_helpers;
mod types;
use std::io::{stderr, Write, ErrorKind};
use builtins::Builtin;
use shell::Shell;
use tokio_core::reactor::Core;
use futures::{Future, Stream};
use std::sync::mpsc;
use std::thread;
fn main() {
let builtins = Builtin::map();
let mut shell = Shell::new(&builtins);
shell.evaluate_init_file();
if "1" == shell.variables.get_var_or_empty("HISTORY_FILE_ENABLED") {
shell.context.history.set_file_name(shell.variables.get_var("HISTORY_FILE"));
match shell.context.history.load_history() {
Ok(()) => {
// pass
}
Err(ref err) if err.kind() == ErrorKind::NotFound => {
let history_filename = shell.variables.get_var_or_empty("HISTORY_FILE");
let _ = writeln!(stderr(), "ion: failed to find history file {}: {}", history_filename, err);
},
Err(err) => {
let _ = writeln!(stderr(), "ion: failed to load history: {}", err);
let (sigint_tx, sigint_rx) = mpsc::channel();
thread::spawn(move || {
let builtins = Builtin::map();
let mut shell = Shell::new(&builtins, sigint_rx);
shell.evaluate_init_file();
if "1" == shell.variables.get_var_or_empty("HISTORY_FILE_ENABLED") {
shell.context.history.set_file_name(shell.variables.get_var("HISTORY_FILE"));
match shell.context.history.load_history() {
Ok(()) => {
// pass
}
Err(ref err) if err.kind() == ErrorKind::NotFound => {
let history_filename = shell.variables.get_var_or_empty("HISTORY_FILE");
let _ = writeln!(stderr(), "ion: failed to find history file {}: {}", history_filename, err);
},
Err(err) => {
let _ = writeln!(stderr(), "ion: failed to load history: {}", err);
}
}
}
}
shell.execute();
shell.execute();
});
let mut core = Core::new().unwrap();
let handle = core.handle();
let ctrl_c = tokio_signal::ctrl_c(&handle).flatten_stream();
let signal_handler = ctrl_c.for_each(|()| {
eprintln!("ion: received SIGINT (Ctrl+C)");
let _ = sigint_tx.send(true);
Ok(())
});
core.run(signal_handler).unwrap();
}
Oops, something went wrong.

0 comments on commit 9e02244

Please sign in to comment.