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

View File

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