@@ -11,13 +11,14 @@ use crate::cli::args::{parse_args, ExecutionMode};
1111use crate :: cli:: terminal:: is_attached_to_terminal;
1212use crate :: cli:: { clear_line, set_cursor_pos} ;
1313use crate :: shell:: Shell ;
14- use crate :: signals:: setup_signal_handling;
14+ use crate :: signals:: {
15+ handle_signal_ignore, handle_signal_write_to_signal_buffer, setup_signal_handling, Signal ,
16+ } ;
1517use crate :: utils:: is_process_in_foreground;
1618use cli:: terminal:: read_nonblocking_char;
1719use cli:: vi:: { Action , ViEditor } ;
1820use gettextrs:: { bind_textdomain_codeset, setlocale, textdomain, LocaleCategory } ;
19- use nix:: sys:: signal:: { sigaction, SaFlags , SigAction , SigSet } ;
20- use nix:: sys:: signal:: { SigHandler , Signal as NixSignal } ;
21+ use nix:: libc;
2122use std:: error:: Error ;
2223use std:: io;
2324use std:: io:: Write ;
@@ -140,7 +141,14 @@ fn standard_repl(shell: &mut Shell) {
140141 flush_stdout ( ) ;
141142 }
142143 std:: thread:: sleep ( Duration :: from_millis ( 16 ) ) ;
144+ shell. signal_manager . reset_sigint_count ( ) ;
143145 shell. handle_async_events ( ) ;
146+ if shell. signal_manager . get_sigint_count ( ) > 0 {
147+ program_buffer. clear ( ) ;
148+ line_buffer. clear ( ) ;
149+ println ! ( ) ;
150+ eprint ! ( "{}" , shell. get_ps1( ) ) ;
151+ }
144152 if shell. set_options . vi {
145153 return ;
146154 }
@@ -149,7 +157,7 @@ fn standard_repl(shell: &mut Shell) {
149157
150158fn vi_repl ( shell : & mut Shell ) {
151159 let mut editor = ViEditor :: default ( ) ;
152- let mut buffer = Vec :: new ( ) ;
160+ let mut program_buffer = Vec :: new ( ) ;
153161 let mut print_ps2 = false ;
154162 clear_line ( ) ;
155163 flush_stdout ( ) ;
@@ -158,29 +166,29 @@ fn vi_repl(shell: &mut Shell) {
158166 while let Some ( c) = read_nonblocking_char ( ) {
159167 match editor. process_new_input ( c, shell) {
160168 Ok ( Action :: Execute ( command) ) => {
161- buffer . extend ( command. iter ( ) ) ;
162- if buffer . ends_with ( b"\\ \n " ) {
169+ program_buffer . extend ( command. iter ( ) ) ;
170+ if program_buffer . ends_with ( b"\\ \n " ) {
163171 continue ;
164172 }
165- let program_string = match std:: str:: from_utf8 ( & buffer ) {
173+ let program_string = match std:: str:: from_utf8 ( & program_buffer ) {
166174 Ok ( buf) => buf,
167175 Err ( _) => {
168176 eprintln ! ( "sh: invalid utf-8 sequence" ) ;
169- buffer . clear ( ) ;
177+ program_buffer . clear ( ) ;
170178 continue ;
171179 }
172180 } ;
173181 println ! ( ) ;
174182 shell. terminal . reset ( ) ;
175183 match shell. execute_program ( program_string) {
176184 Ok ( _) => {
177- buffer . clear ( ) ;
185+ program_buffer . clear ( ) ;
178186 print_ps2 = false ;
179187 }
180188 Err ( syntax_err) => {
181189 if !syntax_err. could_be_resolved_with_more_input {
182190 eprintln ! ( "sh: syntax error: {}" , syntax_err. message) ;
183- buffer . clear ( ) ;
191+ program_buffer . clear ( ) ;
184192 } else {
185193 print_ps2 = true ;
186194 }
@@ -205,7 +213,14 @@ fn vi_repl(shell: &mut Shell) {
205213 flush_stdout ( )
206214 }
207215 std:: thread:: sleep ( Duration :: from_millis ( 16 ) ) ;
216+ shell. signal_manager . reset_sigint_count ( ) ;
208217 shell. handle_async_events ( ) ;
218+ if shell. signal_manager . get_sigint_count ( ) > 0 {
219+ program_buffer. clear ( ) ;
220+ editor. reset_current_line ( ) ;
221+ println ! ( ) ;
222+ eprint ! ( "{}" , shell. get_ps1( ) ) ;
223+ }
209224 if !shell. set_options . vi {
210225 return ;
211226 }
@@ -218,24 +233,14 @@ fn interactive_shell(shell: &mut Shell) {
218233 nix:: unistd:: tcsetpgrp ( io:: stdin ( ) . as_fd ( ) , pgid) . unwrap ( ) ;
219234 }
220235 shell. terminal . set_nonblocking_no_echo ( ) ;
221- let ignore_action = SigAction :: new ( SigHandler :: SigIgn , SaFlags :: empty ( ) , SigSet :: empty ( ) ) ;
222- unsafe {
223- sigaction ( NixSignal :: SIGQUIT , & ignore_action) . unwrap ( ) ;
224- }
225- unsafe {
226- sigaction ( NixSignal :: SIGTERM , & ignore_action) . unwrap ( ) ;
227- }
236+ unsafe { handle_signal_ignore ( Signal :: SigQuit ) }
237+ unsafe { handle_signal_ignore ( Signal :: SigTerm ) }
238+ unsafe { handle_signal_write_to_signal_buffer ( Signal :: SigInt ) }
228239 if shell. set_options . monitor {
229240 // job control signals
230- unsafe {
231- sigaction ( NixSignal :: SIGTTIN , & ignore_action) . unwrap ( ) ;
232- }
233- unsafe {
234- sigaction ( NixSignal :: SIGTTOU , & ignore_action) . unwrap ( ) ;
235- }
236- unsafe {
237- sigaction ( NixSignal :: SIGTSTP , & ignore_action) . unwrap ( ) ;
238- }
241+ unsafe { handle_signal_ignore ( Signal :: SigTtin ) }
242+ unsafe { handle_signal_ignore ( Signal :: SigTtou ) }
243+ unsafe { handle_signal_ignore ( Signal :: SigTstp ) }
239244 }
240245 loop {
241246 if shell. set_options . vi {
0 commit comments