feat(signal): change the behaviour of SIGINT
In previous versions, when a SIGINT was received, the program would exit if there were no child processes, now the behavior is changed to not exit and start a new line.
This commit is contained in:
parent
87eb989d79
commit
f99caf42f3
4 changed files with 21 additions and 22 deletions
6
exec.c
6
exec.c
|
@ -2,7 +2,7 @@
|
|||
* @Author: 1ridic
|
||||
* @Date: 2022-09-18 14:14:23
|
||||
* @Last Modified by: 1ridic
|
||||
* @Last Modified time: 2022-09-18 23:31:39
|
||||
* @Last Modified time: 2022-09-20 22:24:51
|
||||
*/
|
||||
#include "builtin.h"
|
||||
#include <stdio.h>
|
||||
|
@ -31,17 +31,13 @@ int forkExec(char **args) {
|
|||
/* fork error */
|
||||
perror("dish");
|
||||
} else {
|
||||
extern char volatile isWaiting;
|
||||
isWaiting = 1;
|
||||
/* parent process: wait child process*/
|
||||
int stat_val;
|
||||
waitpid(pid, &stat_val, 0);
|
||||
if (WIFEXITED(stat_val)) {
|
||||
isWaiting = 0;
|
||||
return WEXITSTATUS(stat_val);
|
||||
}
|
||||
else if (WIFSIGNALED(stat_val)) {
|
||||
isWaiting = 1;
|
||||
return WTERMSIG(stat_val);
|
||||
}
|
||||
}
|
||||
|
|
16
loop.c
16
loop.c
|
@ -2,7 +2,7 @@
|
|||
* @Author: 1ridic
|
||||
* @Date: 2022-09-18 14:13:53
|
||||
* @Last Modified by: 1ridic
|
||||
* @Last Modified time: 2022-09-18 23:32:02
|
||||
* @Last Modified time: 2022-09-20 22:26:59
|
||||
*/
|
||||
#include <stdio.h>
|
||||
#include <readline/readline.h>
|
||||
|
@ -15,6 +15,20 @@
|
|||
|
||||
int status;
|
||||
|
||||
void SIGINT_Handler(int dummy) {
|
||||
#ifdef DEBUG
|
||||
fprintf(stdout,"\ndebug: SIGINT\n");
|
||||
#endif
|
||||
/* Move to a new line */
|
||||
fprintf(stdout,"\n");
|
||||
/* Regenerate the prompt on a newline */
|
||||
rl_on_new_line();
|
||||
/* Clear the previous text */
|
||||
rl_replace_line("", 0);
|
||||
rl_redisplay();
|
||||
return;
|
||||
}
|
||||
|
||||
int loop() {
|
||||
|
||||
char *line;
|
||||
|
|
3
loop.h
3
loop.h
|
@ -2,10 +2,11 @@
|
|||
* @Author: 1ridic
|
||||
* @Date: 2022-09-18 14:14:19
|
||||
* @Last Modified by: 1ridic
|
||||
* @Last Modified time: 2022-09-18 14:14:19
|
||||
* @Last Modified time: 2022-09-20 22:24:24
|
||||
*/
|
||||
#ifndef _LOOP_H_
|
||||
#define _LOOP_H_
|
||||
#include "config.h"
|
||||
void SIGINT_Handler(int dummy);
|
||||
int loop();
|
||||
#endif
|
16
main.c
16
main.c
|
@ -2,7 +2,7 @@
|
|||
* @Author: 1ridic
|
||||
* @Date: 2022-09-18 14:13:59
|
||||
* @Last Modified by: 1ridic
|
||||
* @Last Modified time: 2022-09-18 23:21:37
|
||||
* @Last Modified time: 2022-09-20 22:23:59
|
||||
*/
|
||||
#include <signal.h>
|
||||
#include <stdio.h>
|
||||
|
@ -11,22 +11,10 @@
|
|||
#include <string.h>
|
||||
#include "loop.h"
|
||||
|
||||
char volatile isWaiting = 0;
|
||||
void intHandler(int dummy) {
|
||||
#ifdef DEBUG
|
||||
fprintf(stdout,"\ndebug: enter soft-irq. isWaiting=%d\n",isWaiting);
|
||||
#endif
|
||||
if (isWaiting==1) {
|
||||
return;
|
||||
}
|
||||
fprintf(stderr,"\nSIGINT exit.\n");
|
||||
exit(EXIT_FAILURE);
|
||||
|
||||
}
|
||||
|
||||
int main(int argc, char *argv[]) {
|
||||
/* soft irq */
|
||||
signal(SIGINT, intHandler);
|
||||
signal(SIGINT, SIGINT_Handler);
|
||||
/* clear screen */
|
||||
fprintf(stdout,"\033[H\033[J");
|
||||
#ifdef DEBUG
|
||||
|
|
Loading…
Reference in a new issue