14130Sdlw /* STARTUP PROCEDURE FOR UNIX FORTRAN PROGRAMS */ 2*22021Slibs char id_libF77[] = "@(#)main.c 2.17 06/04/85"; 34130Sdlw 44130Sdlw #include <stdio.h> 54130Sdlw #include <signal.h> 64131Sdlw #include "../libI77/fiodefs.h" 74130Sdlw 820197Slibs extern int errno; 920197Slibs char *getenv(); 1020197Slibs 114130Sdlw int xargc; 124130Sdlw char **xargv; 134130Sdlw 144130Sdlw main(argc, argv, arge) 154130Sdlw int argc; 164130Sdlw char **argv; 174130Sdlw char **arge; 184130Sdlw { 194134Sdlw int sigdie(); 204130Sdlw long int (*sigf)(); 214168Sdlw int signum; 224130Sdlw 234130Sdlw xargc = argc; 244130Sdlw xargv = argv; 254130Sdlw 264168Sdlw for (signum=1; signum<=16; signum++) 274168Sdlw { 284168Sdlw if((sigf=signal(signum, sigdie)) != SIG_DFL) signal(signum, sigf); 294168Sdlw } 304168Sdlw 314130Sdlw #ifdef pdp11 324130Sdlw ldfps(01200); /* detect overflow as an exception */ 334130Sdlw #endif 344130Sdlw 354130Sdlw f_init(); 3612580Sfortran MAIN_(); 374130Sdlw f_exit(); 3820197Slibs return 0; 394130Sdlw } 404130Sdlw 414134Sdlw struct action { 424134Sdlw char *mesg; 434134Sdlw int core; 444134Sdlw } sig_act[16] = { 454168Sdlw {"Hangup", 0}, /* SIGHUP */ 464134Sdlw {"Interrupt!", 0}, /* SIGINT */ 474134Sdlw {"Quit!", 1}, /* SIGQUIT */ 484170Sdlw {"Illegal ", 1}, /* SIGILL */ 494168Sdlw {"Trace Trap", 1}, /* SIGTRAP */ 504134Sdlw {"IOT Trap", 1}, /* SIGIOT */ 514140Sdlw {"EMT Trap", 1}, /* SIGEMT */ 524137Sdlw {"Arithmetic Exception", 1}, /* SIGFPE */ 534134Sdlw { 0, 0}, /* SIGKILL */ 544134Sdlw {"Bus error", 1}, /* SIGBUS */ 554134Sdlw {"Segmentation violation", 1}, /* SIGSEGV */ 564168Sdlw {"Sys arg", 1}, /* SIGSYS */ 574168Sdlw {"Open pipe", 0}, /* SIGPIPE */ 584168Sdlw {"Alarm", 0}, /* SIGALRM */ 594134Sdlw {"Terminated", 0}, /* SIGTERM */ 604168Sdlw {"Sig 16", 0}, /* unassigned */ 614134Sdlw }; 624130Sdlw 634137Sdlw struct action act_fpe[] = { 644137Sdlw {"Integer overflow", 1}, 654137Sdlw {"Integer divide by 0", 1}, 6613232Sdlw {"Floating point overflow trap", 1}, 6713232Sdlw {"Floating divide by zero trap", 1}, 6813232Sdlw {"Floating point underflow trap", 1}, 694137Sdlw {"Decimal overflow", 1}, 704137Sdlw {"Subscript range", 1}, 714137Sdlw {"Floating point overflow", 0}, 724137Sdlw {"Floating divide by zero", 0}, 734137Sdlw {"Floating point underflow", 0}, 744137Sdlw }; 754170Sdlw 764170Sdlw struct action act_ill[] = { 774170Sdlw {"addr mode", 1}, 784170Sdlw {"instruction", 1}, 794170Sdlw {"operand", 0}, 804170Sdlw }; 814130Sdlw 8214399Sdlw #if vax 8314399Sdlw sigdie(s, t, sc) 8414399Sdlw int s; int t; struct sigcontext *sc; 8514399Sdlw 8614399Sdlw #else pdp11 874137Sdlw sigdie(s, t, pc) 884137Sdlw int s; int t; long pc; 8914399Sdlw 9014399Sdlw #endif 914130Sdlw { 924131Sdlw extern unit units[]; 934134Sdlw register struct action *act = &sig_act[s-1]; 944726Sdlw /* print error message, then flush buffers */ 954726Sdlw 9614840Sdlw if (s == SIGHUP || s == SIGINT || s == SIGQUIT) 9714840Sdlw signal(s, SIG_IGN); /* don't allow it again */ 9814840Sdlw else 9914840Sdlw signal(s, SIG_DFL); /* shouldn't happen again, but ... */ 10014840Sdlw 1014137Sdlw if (act->mesg) 1024137Sdlw { 1034173Sdlw fprintf(units[STDERR].ufd, "*** %s", act->mesg); 1044137Sdlw if (s == SIGFPE) 1054168Sdlw { 1064168Sdlw if (t >= 1 && t <= 10) 1074168Sdlw fprintf(units[STDERR].ufd, ": %s", act_fpe[t-1].mesg); 1084168Sdlw else 1094168Sdlw fprintf(units[STDERR].ufd, ": Type=%d?", t); 1104168Sdlw } 1114168Sdlw else if (s == SIGILL) 1124170Sdlw { 1134170Sdlw if (t == 4) t = 2; /* 4.0bsd botch */ 1144170Sdlw if (t >= 0 && t <= 2) 1154170Sdlw fprintf(units[STDERR].ufd, "%s", act_ill[t].mesg); 1164170Sdlw else 1174170Sdlw fprintf(units[STDERR].ufd, "compat mode: Code=%d", t); 1184170Sdlw } 1194168Sdlw putc('\n', units[STDERR].ufd); 1204137Sdlw } 12120197Slibs f77_abort( s, act->core ); 12220197Slibs } 1234130Sdlw 124*22021Slibs extern int _dbsubc; /* dbsubc is non-zero if -lg was specified to ld */ 12520197Slibs f77_abort( err_val, act_core ) 12620197Slibs { 12720197Slibs char first_char, *env_var; 12820197Slibs int core_dump; 12920197Slibs 13020197Slibs env_var = getenv("f77_dump_flag"); 13120197Slibs first_char = (env_var == NULL) ? 0 : *env_var; 13220197Slibs 1334137Sdlw signal(SIGILL, SIG_DFL); 13420197Slibs sigsetmask(0); /* don't block */ 13520197Slibs 136*22021Slibs /* see if we want a core dump: 137*22021Slibs first line checks for signals like hangup - don't dump then. 138*22021Slibs second line checks if -lg specified to ld (e.g. by saying 139*22021Slibs -g to f77) and checks the f77_dump_flag var. */ 14020197Slibs core_dump = ((nargs() != 2) || act_core) && 141*22021Slibs ( (_dbsubc && (first_char != 'n')) || first_char == 'y'); 14220197Slibs 14320197Slibs if( !core_dump ) 14420197Slibs fprintf(units[STDERR].ufd,"*** Execution terminated\n"); 14520197Slibs 14620197Slibs f_exit(); 14720197Slibs _cleanup(); 14820197Slibs if( nargs() ) errno = err_val; 14920197Slibs else errno = -2; /* prior value will be meaningless, 15020197Slibs so set it to undefined value */ 15120197Slibs 15220197Slibs if( core_dump ) abort(); 15320197Slibs else exit( errno ); 1544130Sdlw } 155