From c9c3b2f4357e30dd078075b3259d86adca6958e3 Mon Sep 17 00:00:00 2001 From: _ov4 Date: Tue, 1 Sep 2026 06:21:08 +0000 Subject: 1 --- src/handler.cc | 23 ++++++++++++- src/init.cc | 22 +++++++++++++ src/main.cc | 102 +++------------------------------------------------------ src/util/io.cc | 30 +++++++++++++++++ 4 files changed, 78 insertions(+), 99 deletions(-) create mode 100644 src/init.cc (limited to 'src') diff --git a/src/handler.cc b/src/handler.cc index afc1fc1..5356d47 100644 --- a/src/handler.cc +++ b/src/handler.cc @@ -6,12 +6,33 @@ namespace ov4 extern char **environ; sigset_t BLOCK_HANDLER; -void init() +void signal_init() { sigemptyset(&BLOCK_HANDLER); sigaddset(&BLOCK_HANDLER, SIGINT); sigaddset(&BLOCK_HANDLER, SIGTSTP); sigaddset(&BLOCK_HANDLER, SIGCHLD); + + Signal(SIGINT, sigint_handler); /* ctrl-c */ + Signal(SIGTSTP, sigtstp_handler); /* ctrl-z */ + Signal(SIGCHLD, sigchld_handler); /* Terminated or stopped child */ +} + +/* + * Signal - wrapper for the sigaction function + */ + +handler_t *Signal(int signum, handler_t *handler) +{ + struct sigaction action, old_action; + + action.sa_handler = handler; + sigemptyset(&action.sa_mask); /* block sigs of type being handled */ + action.sa_flags = SA_RESTART; /* restart syscalls if possible */ + + if (sigaction(signum, &action, &old_action) < 0) + unix_error("Signal error"); + return (old_action.sa_handler); } /* diff --git a/src/init.cc b/src/init.cc new file mode 100644 index 0000000..06b66e1 --- /dev/null +++ b/src/init.cc @@ -0,0 +1,22 @@ +#include "init.h" + +namespace ov4 +{ + +using namespace std; + +void init() +{ + cout << "\ +You are using ccshell. \n\ +This is free software; see the source for copying conditions. \ +There is NO warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. \n\ +Published under GNU AFFERO GENERAL PUBLIC LICENSE (AGPL) Version 3." << endl; + + signal_init(); + + + +} + +} diff --git a/src/main.cc b/src/main.cc index d7719b2..f983590 100644 --- a/src/main.cc +++ b/src/main.cc @@ -11,52 +11,24 @@ #include #include -#include "util/io.h" -#include "job.h" -#include "parse.h" -#include "handler.h" -#include "ground.h" -#include "eval.h" -#include "path.h" -#include "util/string.h" +#include "all.h" #include "test.h" using namespace std; using namespace ov4; - - /* Global variables */ char prompt[] = "$ "; /* command line prompt (DO NOT CHANGE) */ char sbuf[MAXLINE]; /* for composing sprintf messages */ - - -int builtin_cmd(char *c_str); - - - -void usage(void); -void unix_error(char *msg); -void app_error(char *msg); -typedef void handler_t(int); -handler_t *Signal(int signum, handler_t *handler); - -// built-in commands -void builtin_command_quit(); - /* * main - The shell's main routine */ int main(int argc, char **argv) { - cout << "\ -You are using ccshell. \n\ -This is free software; see the source for copying conditions. \ -There is NO warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. \n\ -Published under GNU AFFERO GENERAL PUBLIC LICENSE (AGPL) Version 3." << endl; + init(); //test(argv); - init(); + char c; char cmdline[MAXLINE]; int emit_prompt = 1; /* emit prompt (default) */ @@ -84,10 +56,7 @@ Published under GNU AFFERO GENERAL PUBLIC LICENSE (AGPL) Version 3." << endl; /* Install the signal handlers */ - /* These are the ones you will need to implement */ - Signal(SIGINT, sigint_handler); /* ctrl-c */ - Signal(SIGTSTP, sigtstp_handler); /* ctrl-z */ - Signal(SIGCHLD, sigchld_handler); /* Terminated or stopped child */ + /* This one provides a clean way to kill the shell */ Signal(SIGQUIT, sigquit_handler); @@ -119,66 +88,3 @@ Published under GNU AFFERO GENERAL PUBLIC LICENSE (AGPL) Version 3." << endl; exit(0); /* control never reaches here */ } - - - - - - - - - -/*********************** - * Other helper routines - ***********************/ - -/* - * usage - print a help message - */ -void usage(void) -{ - printf("Usage: shell [-hvp]\n"); - printf(" -h print this message\n"); - printf(" -v print additional diagnostic information\n"); - printf(" -p do not emit a command prompt\n"); - exit(1); -} - -/* - * unix_error - unix-style error routine - */ -void unix_error(char *msg) -{ - fprintf(stdout, "%s: %s\n", msg, strerror(errno)); - exit(1); -} - -/* - * app_error - application-style error routine - */ -void app_error(char *msg) -{ - fprintf(stdout, "%s\n", msg); - exit(1); -} - -/* - * Signal - wrapper for the sigaction function - */ -handler_t *Signal(int signum, handler_t *handler) -{ - struct sigaction action, old_action; - - action.sa_handler = handler; - sigemptyset(&action.sa_mask); /* block sigs of type being handled */ - action.sa_flags = SA_RESTART; /* restart syscalls if possible */ - - if (sigaction(signum, &action, &old_action) < 0) - unix_error("Signal error"); - return (old_action.sa_handler); -} - - - - - diff --git a/src/util/io.cc b/src/util/io.cc index 531bd47..1261684 100644 --- a/src/util/io.cc +++ b/src/util/io.cc @@ -96,4 +96,34 @@ char *itoa(int num, char* str, int base) return str; } +/* + * usage - print a help message + */ +void usage(void) +{ + printf("Usage: shell [-hvp]\n"); + printf(" -h print this message\n"); + printf(" -v print additional diagnostic information\n"); + printf(" -p do not emit a command prompt\n"); + exit(1); +} + +/* + * unix_error - unix-style error routine + */ +void unix_error(char *msg) +{ + fprintf(stdout, "%s: %s\n", msg, strerror(errno)); + exit(1); +} + +/* + * app_error - application-style error routine + */ +void app_error(char *msg) +{ + fprintf(stdout, "%s\n", msg); + exit(1); +} + } -- cgit v1.3.1-4-g156e