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:
iridiumR 2022-09-20 22:29:59 +08:00
parent 87eb989d79
commit f99caf42f3
No known key found for this signature in database
GPG Key ID: 5574BE4450D55618
4 changed files with 21 additions and 22 deletions

6
exec.c
View File

@ -2,7 +2,7 @@
* @Author: 1ridic * @Author: 1ridic
* @Date: 2022-09-18 14:14:23 * @Date: 2022-09-18 14:14:23
* @Last Modified by: 1ridic * @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 "builtin.h"
#include <stdio.h> #include <stdio.h>
@ -31,17 +31,13 @@ int forkExec(char **args) {
/* fork error */ /* fork error */
perror("dish"); perror("dish");
} else { } else {
extern char volatile isWaiting;
isWaiting = 1;
/* parent process: wait child process*/ /* parent process: wait child process*/
int stat_val; int stat_val;
waitpid(pid, &stat_val, 0); waitpid(pid, &stat_val, 0);
if (WIFEXITED(stat_val)) { if (WIFEXITED(stat_val)) {
isWaiting = 0;
return WEXITSTATUS(stat_val); return WEXITSTATUS(stat_val);
} }
else if (WIFSIGNALED(stat_val)) { else if (WIFSIGNALED(stat_val)) {
isWaiting = 1;
return WTERMSIG(stat_val); return WTERMSIG(stat_val);
} }
} }

16
loop.c
View File

@ -2,7 +2,7 @@
* @Author: 1ridic * @Author: 1ridic
* @Date: 2022-09-18 14:13:53 * @Date: 2022-09-18 14:13:53
* @Last Modified by: 1ridic * @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 <stdio.h>
#include <readline/readline.h> #include <readline/readline.h>
@ -15,6 +15,20 @@
int status; 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() { int loop() {
char *line; char *line;

5
loop.h
View File

@ -1,11 +1,12 @@
/* /*
* @Author: 1ridic * @Author: 1ridic
* @Date: 2022-09-18 14:14:19 * @Date: 2022-09-18 14:14:19
* @Last Modified by: 1ridic * @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_ #ifndef _LOOP_H_
#define _LOOP_H_ #define _LOOP_H_
#include "config.h" #include "config.h"
void SIGINT_Handler(int dummy);
int loop(); int loop();
#endif #endif

16
main.c
View File

@ -2,7 +2,7 @@
* @Author: 1ridic * @Author: 1ridic
* @Date: 2022-09-18 14:13:59 * @Date: 2022-09-18 14:13:59
* @Last Modified by: 1ridic * @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 <signal.h>
#include <stdio.h> #include <stdio.h>
@ -11,22 +11,10 @@
#include <string.h> #include <string.h>
#include "loop.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[]) { int main(int argc, char *argv[]) {
/* soft irq */ /* soft irq */
signal(SIGINT, intHandler); signal(SIGINT, SIGINT_Handler);
/* clear screen */ /* clear screen */
fprintf(stdout,"\033[H\033[J"); fprintf(stdout,"\033[H\033[J");
#ifdef DEBUG #ifdef DEBUG