1 /******************************************************************************
2 
3  #    #   ####    ####   #          #    #####            ####
4  ##  ##  #       #    #  #          #    #    #          #    #
5  # ## #   ####   #       #          #    #####           #
6  #    #       #  #  ###  #          #    #    #   ###    #
7  #    #  #    #  #    #  #          #    #    #   ###    #    #
8  #    #   ####    ####   ######     #    #####    ###     ####
9 
10 ******************************************************************************/
11 /* This file is part of MAPMAKER 3.0b, Copyright 1987-1992, Whitehead Institute
12    for Biomedical Research. All rights reserved. See READ.ME for license. */
13 
14 /***** MESSAGE (EXCEPTION) HANDLING ROUTINES ******/
15 
16 #define INC_IO
17 #define INC_MSG
18 #define INC_MEM
19 #define INC_STR
20 #define INC_MATH
21 #define INC_HELP_DEFS
22 #include "system.h"
23 
24 int msg; 	/* user-accessible - valid for last message sent */
25 char *msgname;  /* see list of lib messages in helpers.h - HAS MOVED? */
26 char *msgstr;
27 bool exiting1, exiting0;
28 bool in_tty_gets, hit_interrupt;
29 
30 /* message vars - valid for specific messages */
31 char *MATHERROR_type;
32 real  MATHERROR_arg1, MATHERROR_arg2;
33 int   NOMEMORY_num_cells, NOMEMORY_cell_size;
34 char *CANTOPEN_path, CANTOPEN_modechar;
35 char *IOERROR_errmsg, *IOERROR_filename, IOERROR_modechar, *IOERROR_linecopy;
36 char *SYSERROR_errmsg;
37 char *QUIT_errmsg;
38 char *BADEQN_errmsg;
39 int   BADEQN_errpos;
40 
41 
strmsg_default(str)42 void strmsg_default(str) char *str;
43  { str[0]='\0'; }
strmsg_MATHERROR(str)44 void strmsg_MATHERROR(str) char *str;
45  { sf(str,"error type=%-60s\narg1=%lf\narg2=%lf",
46       MATHERROR_type,MATHERROR_arg1,MATHERROR_arg2); }
strmsg_NOMEMORY(str)47 void strmsg_NOMEMORY(str) char *str;
48  { sf(str,"cell size=%d\nnum cells=%d",NOMEMORY_cell_size,NOMEMORY_num_cells);}
strmsg_CANTOPEN(str)49 void strmsg_CANTOPEN(str) char *str;
50  { sf(str,"filename=%-60s\nmode=%c",CANTOPEN_path,CANTOPEN_modechar); }
strmsg_IOERROR(str)51 void strmsg_IOERROR(str) char *str;
52  { sf(str,"filename=%-60s\nline=%-60s\nerror=%-60s",
53      IOERROR_filename,truncstr(IOERROR_linecopy,60),IOERROR_errmsg); }
strmsg_SYSERROR(str)54 void strmsg_SYSERROR(str) char *str;
55  { sf(str,"errmsg=%-60s",SYSERROR_errmsg); }
56 
57 jmp_buf stk[TRAP_DEPTH];     /* stack of message traps */
58 int lvl;       	             /* trap stack pointer */
59 int last; 		     /* last msg num assigned */
60 char **mname;                /* [msg] => name string */
61 void (*(maction[MSGS]))();   /* [msg] => function which does whatever */
62 void (*(mstrmsg[MSGS]))();   /* [msg] => function which sets its (char*) arg */
63 void sighandle();
64 
msg_init()65 void msg_init()
66 {
67 	msgname=NULL; mname= NULL; lvl=0;
68 	matrix(mname,MSGS,TOKLEN+1,char);
69 	array(msgstr,MAXLINE+1,char);
70 
71 	for(zz=0;zz<MSGS;zz++) {
72 		strcpy(mname[zz],"???");
73 		maction[zz]= default_action;
74 		mstrmsg[zz]= strmsg_default;
75 	}
76 
77 	array(MATHERROR_type,LINE,char);
78 	array(CANTOPEN_path,PATH_LENGTH,char);
79 	array(IOERROR_errmsg,LINE,char);
80 	array(IOERROR_filename,PATH_LENGTH,char);
81 	array(IOERROR_linecopy,LINE,char);
82 	array(SYSERROR_errmsg,LINE,char);
83 	array(QUIT_errmsg,LINE,char);
84 
85 	setmsg(RERUN,"attempt to RERUN failed",sender,strmsg_default);
86 	setmsg(PUNT,"attempt to PUNT failed",sender,strmsg_default);
87 
88 	setmsg(CRASH,"internal software error",sender,strmsg_default);
89 	setmsg(QUIT,"QUIT signal",sender,strmsg_default);
90 	setmsg(NOMEMORY,"request for memory failed",sender,strmsg_NOMEMORY);
91 	setmsg(ENDOINPUT,"end of input",sender,strmsg_default);
92 	setmsg(INTERRUPT,"interrupt signal",sender,strmsg_default);
93 	setmsg(SOFTABORT,"software abort message",sender,strmsg_default);
94 	setmsg(MATHERROR,"math error",sender,strmsg_MATHERROR);
95 	setmsg(IOERROR,"I/O error",sender,strmsg_IOERROR);
96 
97 	setmsg(ENDOFILE,"end of file",sender,strmsg_default);
98 	setmsg(CANTOPEN,"unable to open file - maybe no write permission",
99 	       sender,strmsg_CANTOPEN);
100 	setmsg(SYSERROR,"segmentation violation or bus error",sender,
101 	      strmsg_SYSERROR);
102 	setmsg(BADEQN,"error in equation",sender,strmsg_default);
103 	setmsg(CANTCLOSE,"unable to close file - disk may be full",
104 	       sender,strmsg_default);
105 
106 	last= USER_MESSAGE(0);
107 	signal_trap_init(); /* in syscode.c */
108 }
109 
110 
setmsg(var,nam,action,disp)111 void setmsg(var,nam,action,disp) /* for a pre-assigned msg number */
112 int var;
113 char *nam;
114 void (*action)();
115 void (*disp)();
116 { if (nam!=NULL) nstrcpy(mname[var],nam,MSGNAMLEN);
117   if (action!=NULL) maction[var]=action;
118   if (disp!=NULL) mstrmsg[var]=disp; }
119 
120 
lvl_plus_plus()121 int lvl_plus_plus()
122 { if (lvl<TRAP_DEPTH) return(lvl++);
123   msg=CRASH; untrapped_msg(); abnormal_exit(); return(0); }
124 
125 
126 
sender(num)127 void sender(num)
128 int num;
129 {
130   if (num==0) return;
131 
132   if (num>0 && num<MSGS) msg=num; else msg=CRASH;
133   msgname=mname[msg];
134   if (--lvl>=0) { longjmp(stk[lvl],msg); return; }
135   else { untrapped_msg(); abnormal_exit(); } /* see syscode.c */
136 }
137 
138 
punter(num)139 void punter(num) int num; /* unused */
140 { if (--lvl>=0) longjmp(stk[lvl],QUIT); else {msg=CRASH; untrapped_msg();} }
141 
trapper(num)142 void trapper(num) int num;
143 { void do_trap(); msg=num; do_trap(); } /* do_trap() is in syscode.c */
144 
default_action(num)145 void default_action(num) int num;
146 { msg=num; untrapped_msg(); abnormal_exit(); }
147 
148 
handle_interrupt(n)149 SIGHANDLE handle_interrupt(n) int n;
150 { signal(n,handle_interrupt);
151   if(in_tty_gets) hit_interrupt = TRUE;
152   else send(INTERRUPT); }
153 
handle_quit(n)154 SIGHANDLE handle_quit(n) int n;
155 { sprintf(QUIT_errmsg,"received QUIT signal (#%d) from system",n); send(QUIT);}
156 
handle_matherror(n)157 SIGHANDLE handle_matherror(n) int n;
158 { signal(n,handle_matherror); sigcounter(); MATHERROR_arg1=MATHERROR_arg2=0.0;
159   strcpy(MATHERROR_type,"unknown math error (SIGFPE)"); send(MATHERROR); }
160 
handle_buserror(n)161 SIGHANDLE handle_buserror(n) int n;
162 { signal(n,handle_buserror); sigcounter();
163   strcpy(SYSERROR_errmsg,"segmentation-violation or bus-error");
164   send(SYSERROR); }
165 
handle_weird_signal(n)166 SIGHANDLE handle_weird_signal(n) int n;
167 { signal(n,handle_weird_signal); sigcounter();
168   sprintf(SYSERROR_errmsg,"system signal #%d",n); send(SYSERROR); }
169 
170 
171 #ifdef MATHERR_DEFINED
172 
matherr(ex)173 int matherr(ex) 	 	/* System V std math error trap */
174 struct exception *ex;
175 { switch (ex->type) {
176     case DOMAIN:    strcpy(MATHERROR_type,"DOMAIN");      break;
177     case SING:	    strcpy(MATHERROR_type,"SINGULARITY"); break;
178     case OVERFLOW:  strcpy(MATHERROR_type,"OVERFLOW");    break;
179     case UNDERFLOW: strcpy(MATHERROR_type,"UNDERFLOW");   break;
180     case TLOSS:	    strcpy(MATHERROR_type,"Total-Loss of significance"); break;
181     case PLOSS:	strcpy(MATHERROR_type,"Partial-Loss of significance");   break;
182     default:	strcpy(MATHERROR_type,"Unknown");         break;
183   }
184   MATHERROR_arg1= ex->arg1; MATHERROR_arg2= ex->arg2;
185   send(MATHERROR);
186   return(0);  /*  never reached */
187 }
188 
189 #endif
190 
stack_check(var)191 bool stack_check(var)
192 int *var;
193 {
194      if (*var==-1) { *var=lvl; return(TRUE); }
195      if (lvl== *var) return(TRUE);
196      else return(FALSE);
197 }
198 
199 
200 
201