Skip to content

Commit

Permalink
Split main file
Browse files Browse the repository at this point in the history
  • Loading branch information
ryuichiueda committed Sep 10, 2024
1 parent 1fcba78 commit 5147fb2
Show file tree
Hide file tree
Showing 2 changed files with 51 additions and 43 deletions.
47 changes: 4 additions & 43 deletions src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,19 +5,17 @@ mod core;
mod feeder;
mod elements;
mod error_message;
mod signal;
mod utils;

use std::{env, process, thread, time};
use std::{env, process};
use std::fs::File;
use std::os::fd::IntoRawFd;
use std::sync::Arc;
use std::sync::atomic::Ordering::Relaxed;
use crate::core::{builtins, ShellCore};
use crate::elements::io;
use crate::elements::script::Script;
use crate::feeder::{Feeder, InputError};
use signal_hook::consts;
use signal_hook::iterator::Signals;
use utils::file_check;

fn show_version() {
Expand All @@ -32,32 +30,6 @@ There is no warranty, to the extent permitted by law.", V);
process::exit(0);
}

fn run_signal_check(core: &mut ShellCore) {
for fd in 3..10 { //use FD 3~9 to prevent signal-hool from using these FDs
nix::unistd::dup2(2, fd).expect("sush(fatal): init error");
}

let sigint = Arc::clone(&core.sigint); //追加

thread::spawn(move || {
let mut signals = Signals::new(vec![consts::SIGINT])
.expect("sush(fatal): cannot prepare signal data");

for fd in 3..10 { // release FD 3~9
nix::unistd::close(fd).expect("sush(fatal): init error");
}

loop {
thread::sleep(time::Duration::from_millis(100)); //0.1秒周期に変更
for signal in signals.pending() {
if signal == consts::SIGINT {
sigint.store(true, Relaxed);
}
}
}
});
} //thanks: https://dev.to/talzvon/handling-unix-kill-signals-in-rust-55g6

fn read_rc_file(core: &mut ShellCore) {
if ! core.data.flags.contains("i") {
return;
Expand Down Expand Up @@ -129,7 +101,7 @@ fn main() {
core.script_name = script.clone();
builtins::option_commands::set(&mut core, &mut options);
builtins::option_commands::set_parameters(&mut core, &mut parameters);
run_signal_check(&mut core);
signal::run_signal_check(&mut core);

if c_flag {
main_c_option(&mut core, &script);
Expand All @@ -152,17 +124,6 @@ fn set_history(core: &mut ShellCore, s: &str) {
}
}

fn input_interrupt_check(feeder: &mut Feeder, core: &mut ShellCore) -> bool {
if ! core.sigint.load(Relaxed) { //core.input_interrupt {
return false;
}

core.sigint.store(false, Relaxed); //core.input_interrupt = false;
core.data.set_param("?", "130");
feeder.consume(feeder.len());
true
}

fn main_loop(core: &mut ShellCore) {
let mut feeder = Feeder::new("");
loop {
Expand All @@ -172,7 +133,7 @@ fn main_loop(core: &mut ShellCore) {
match feeder.feed_line(core) {
Ok(()) => {},
Err(InputError::Interrupt) => {
input_interrupt_check(&mut feeder, core);
signal::input_interrupt_check(&mut feeder, core);
continue;
},
_ => break,
Expand Down
47 changes: 47 additions & 0 deletions src/signal.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
//SPDX-FileCopyrightText: 2024 Ryuichi Ueda ryuichiueda@gmail.com
//SPDX-License-Identifier: BSD-3-Clause

use std::{thread, time};
use std::sync::Arc;
use std::sync::atomic::Ordering::Relaxed;
use crate::core::ShellCore;
use crate::feeder::Feeder;
use signal_hook::consts;
use signal_hook::iterator::Signals;

pub fn run_signal_check(core: &mut ShellCore) {
for fd in 3..10 { //use FD 3~9 to prevent signal-hool from using these FDs
nix::unistd::dup2(2, fd).expect("sush(fatal): init error");
}

let sigint = Arc::clone(&core.sigint); //追加

thread::spawn(move || {
let mut signals = Signals::new(vec![consts::SIGINT])
.expect("sush(fatal): cannot prepare signal data");

for fd in 3..10 { // release FD 3~9
nix::unistd::close(fd).expect("sush(fatal): init error");
}

loop {
thread::sleep(time::Duration::from_millis(100)); //0.1秒周期に変更
for signal in signals.pending() {
if signal == consts::SIGINT {
sigint.store(true, Relaxed);
}
}
}
});
} //thanks: https://dev.to/talzvon/handling-unix-kill-signals-in-rust-55g6

pub fn input_interrupt_check(feeder: &mut Feeder, core: &mut ShellCore) -> bool {
if ! core.sigint.load(Relaxed) { //core.input_interrupt {
return false;
}

core.sigint.store(false, Relaxed); //core.input_interrupt = false;
core.data.set_param("?", "130");
feeder.consume(feeder.len());
true
}

0 comments on commit 5147fb2

Please sign in to comment.