|
From: Peep P. <so...@us...> - 2004-07-23 17:08:14
|
Update of /cvsroot/agd/server/src In directory sc8-pr-cvs1.sourceforge.net:/tmp/cvs-serv14169 Modified Files: main.c Log Message: master flag() apply; general cleanup Index: main.c =================================================================== RCS file: /cvsroot/agd/server/src/main.c,v retrieving revision 1.22 retrieving revision 1.23 diff -u -d -r1.22 -r1.23 --- main.c 21 Jul 2004 11:57:22 -0000 1.22 +++ main.c 23 Jul 2004 17:07:59 -0000 1.23 @@ -34,27 +34,47 @@ extern object_t *master; static char *conf_file; time_t startup_time; -extern variable_t *fp, *sp; -void crash(int sig, char *reason, int retval) +void sighandler(int num) +{ + switch(num) { + case SIGINT: + crash(SIGINT, "Interrupted", 4); + break; + case SIGSEGV: + crash(SIGSEGV, "Segmentation fault", 4); + break; + case SIGPIPE: + crash(SIGPIPE, "Broken pipe", 4); + break; + } +} + +void catch_signals() +{ + signal(SIGINT, sighandler); + signal(SIGSEGV, sighandler); + signal(SIGPIPE, sighandler); +} + +void stop_signals() { -#ifdef DEBUG - extern int dont_debug_interpreter; -#endif - - fprintf(stderr, "Crashing: %s\n", reason); signal(SIGINT, SIG_IGN); signal(SIGSEGV, SIG_IGN); signal(SIGPIPE, SIG_IGN); -#ifdef DEBUG - /* We generally do not want to debug the crash function. Causes unnecessary spam. */ - dont_debug_interpreter = 0; -#endif +} + +void crash(int sig, char *reason, int retval) +{ + extern variable_t *fp, *sp; + + fprintf(stderr, "Crashing: %s\n", reason); + stop_signals(); if(master) { int ret; fp = sp; - ret = apply(master, "crash", "i", sig); + ret = apply(master, "crash", "is", sig, reason); if(!ret) fprintf(stderr, "master::crash() failed.\n"); else @@ -63,32 +83,14 @@ exit(retval); } -/* system (maybe virtual?) header <signal.h> for crash() to identify signals - by name, not number. */ -void sighandler(int num) -{ - switch(num) { - case SIGINT: - crash(SIGINT, "Interrupted", 4); - break; - case SIGSEGV: - crash(SIGSEGV, "Segmentation fault", 4); - break; - case SIGPIPE: - crash(SIGPIPE, "Broken pipe", 4); - break; - } -} - void usage(char *s) { - printf("Usage: %s [options] [configuration file]\n", s); - printf("\tOptions:\n\ - \t\t-d:\n\ - \t\t Increase debug level\n\ - \t\t-?\n\ - \t\t-v:\n\ - \t\t Show version and quit\n"); + printf("Usage: %s [options] <configuration file>\n", s); + printf("Options:\n" + "\t-d: Increase debug level\n" + "\t-f<text>: pass flag to master object\n" + "\t-? or -h: Show this text\n" + "\t-v: Show version and quit\n"); exit(5); } @@ -97,12 +99,31 @@ printf("%s for %s starting at %s", PACKAGE_STRING, PLATFORM, ctime(&startup_time)); #ifdef ARCH_README printf("Your computer architecture is not fully supported. " - "Please read README.ARCH in the source directory.\n"); + "Please read README.ARCH in the source directory.\n"); #endif } -#define JUMPTONEXT() if(argv[i][j+1] == '=') { i++; if(i < argc) goto next_i; else return; } +void do_flag(char *s) +{ + static char **flags; + static int numflags; + if(!s) { + /* apply them. */ + int i; + for(i=0;i<numflags;i++) { + apply(master, "flag", "s", flags[i]); + } + free(flags); + } else { + /* add one. */ + numflags++; + flags = xrealloc(flags, sizeof(char *) * numflags); + flags[numflags-1] = s; + } +} + +#define NEXTARG() { i++; if(i<argc) goto next_i; else return; } void parse_args(int argc, char **argv) { int i, j; @@ -126,7 +147,8 @@ #ifdef DEBUG if(argv[i][j+1] == '=') { conf.debuglevel = atoi(argv[i]+j+2); - JUMPTONEXT() + if(argv[i][j+1] == '=') + NEXTARG() } else { conf.debuglevel++; } @@ -134,10 +156,23 @@ if(conf.debuglevel >= 0) { printf("-d only works if the driver is compiled with debugging on.\n"); conf.debuglevel = -1; - JUMPTONEXT() + if(argv[i][j+1] == '=') + NEXTARG() } #endif break; + case 'f': + if(argv[i][j+1]) { + do_flag(&argv[i][j+1]); + NEXTARG() + } else if(argv[i+1]) { + do_flag(argv[i+1]); + NEXTARG() + } else { + printf("-f needs an argument\n"); + exit(5); + } + break; default: printf("invalid argument '%c'\n", argv[i][j]); usage(argv[0]); @@ -181,20 +216,18 @@ } setvbuf(stdout, NULL, _IONBF, 0); - signal(SIGINT, sighandler); - signal(SIGSEGV, sighandler); - signal(SIGPIPE, sighandler); + catch_signals(); parse_config(conf_file); - check_logfile(); dfuns_init(); -/* init_interpreter();*/ master = load_object(conf.master); if(!master) crash(0, "Master object doesn't load", 3); /* master->numref++; Is this really necessary? */ + do_flag(NULL); /* Apply flags. */ + net_listen(conf.port); printf("Accepting connections on port %d.\n", conf.port); |