catch ctrl c

This commit is contained in:
Ben Harris 2016-11-07 17:11:14 -05:00
parent 236db39240
commit 0f51870e63
2 changed files with 25 additions and 20 deletions

37
bish.cc
View File

@ -21,9 +21,7 @@ using namespace std;
int main(int argc, char **argv){
struct sigaction act;
act.sa_handler = ctrlCHandler;
sigaction(SIGINT, &act, NULL);
signal(SIGINT, ctrlCHandler);
stringstream prompt;
static char* line = (char*)NULL;
@ -134,22 +132,23 @@ int main(int argc, char **argv){
int status;
if (!cmd->background)
do {
if (waitpid(kidpid, &status, WUNTRACED | WCONTINUED) == -1) {
perror("waitpid");
exit(1);
}
if (WIFEXITED(status)) {
cout << "(" << WEXITSTATUS(status) << "):";
} else if (WIFSIGNALED(status)) {
cout << endl << "killed by signal " << WTERMSIG(status) << endl;
} else if (WIFSTOPPED(status)) {
cout << endl << "stopped by signal " << WSTOPSIG(status) << endl;
} else if (WIFCONTINUED(status)) {
cout << endl << "continued" << endl;
}
} while (!WIFEXITED(status) && !WIFSIGNALED(status));
if (!cmd->background){
do {
if (waitpid(kidpid, &status, WUNTRACED | WCONTINUED) == -1) {
perror("waitpid");
exit(1);
}
if (WIFEXITED(status)) {
cout << "(" << WEXITSTATUS(status) << "):";
} else if (WIFSIGNALED(status)) {
cout << endl << "killed by signal " << WTERMSIG(status) << endl;
} else if (WIFSTOPPED(status)) {
cout << endl << "stopped by signal " << WSTOPSIG(status) << endl;
} else if (WIFCONTINUED(status)) {
cout << endl << "continued" << endl;
}
} while (!WIFEXITED(status) && !WIFSIGNALED(status));
}
}

View File

@ -2,13 +2,19 @@
#include <vector>
#include <string>
#include <signal.h>
#include <iostream>
#include <string.h>
// #include "util_fns.h"
using namespace std;
void ctrlCHandler(int sig) {
if (sig == SIGINT) {
// cout << "SIGINT received" << endl;
// return;
cout << endl;
}
}
// util methods