home *** CD-ROM | disk | FTP | other *** search
/ OS/2 Shareware BBS: 10 Tools / 10-Tools.zip / excptn.zip / excptn / EXCPT.C next >
C/C++ Source or Header  |  1994-11-23  |  5KB  |  158 lines

  1. #include "bas_incl.h"
  2.  
  3. #ifdef __OS2__
  4. #pragma off (unreferenced)
  5.  
  6. static char exceptions_off;
  7.  
  8. ULONG APIENTRY ExcptHndlr(PEXCEPTIONREPORTRECORD p1,
  9.                             PEXCEPTIONREGISTRATIONRECORD p2,
  10.                             PCONTEXTRECORD p3,
  11.                             PVOID p4)
  12. {   pEXCPT_INFO exi, zexi;
  13.     if(!exceptions_off)
  14.     {  zexi = NULL;
  15.        for(exi = ((pEXCPT_STRUCT)p2)->excpt_info; exi; exi = exi->next)
  16.        {   if(p1->ExceptionNum == exi->exception_code)
  17.            {   (*(exi->hndlr))((const char *)(p1->ExceptionInfo),p1->ExceptionNum);
  18.                return XCPT_CONTINUE_EXECUTION;
  19.            }
  20.            else if(!exi->exception_code && (p1->ExceptionNum & 0x60000000))
  21.                 zexi = exi;
  22.        }
  23.        if(zexi)
  24.        {    switch ((*(zexi->hndlr))((const char *)(p1->ExceptionInfo),p1->ExceptionNum)){
  25.             case EXCPT_UNLINK: zexi->exception_code = 0xFFFFFFFF;
  26.             case EXCPT_CONT:   return XCPT_CONTINUE_EXECUTION;
  27.     }   }   }
  28.     return XCPT_CONTINUE_SEARCH;
  29. }
  30.  
  31. void COMMON ExcptPost(unsigned long excpt, unsigned len,
  32.                             const char *info, unsigned long flags)
  33. {   char buff[500];
  34.     PEXCEPTIONREPORTRECORD pERR = (PEXCEPTIONREPORTRECORD)buff;
  35.     if(info && len)
  36.         memcpy(pERR->ExceptionInfo,info,len);
  37.     pERR->cParameters = len;
  38.     pERR->fHandlerFlags = (flags == NON_CONTINUABLE ? EH_NONCONTINUABLE : 0);
  39.     pERR->ExceptionNum = excpt;
  40.     (void)DosRaiseException(pERR);
  41. }
  42.  
  43. void COMMON _ExcptLinkHandler(ULONG expt_code, void (_System *hndlr)(), pEXCPT_INFO *exi)
  44. {   pEXCPT_INFO cur, prev;
  45.     APIRET err;
  46.     if((err = DosEnterCritSec()) != NO_ERROR)
  47.         ExcptPost(EXCPT_SYS_ERR,sizeof(err),(const char *)&err,NON_CONTINUABLE);
  48.     for(prev=NULL,cur=*exi; cur; prev=cur,cur=cur->next)
  49.     {   if(expt_code == cur->exception_code)
  50.         {   if(hndlr)
  51.                 cur->hndlr = hndlr;
  52.             else
  53.             {   if(prev)
  54.                     prev->next = cur->next;
  55.                 else
  56.                     *exi = cur->next;
  57.                 MemoryRls(cur,sizeof(EXCPT_INFO));
  58.             }
  59.             goto gof;
  60.     }   }
  61.     cur = MemoryAlloc(sizeof(EXCPT_INFO),1);
  62.     cur->next = *exi;
  63.     *exi = cur;
  64.     cur->hndlr = hndlr;
  65.     cur->exception_code = expt_code;
  66.     gof:
  67.     if((err = DosExitCritSec()) != NO_ERROR)
  68.         ExcptPost(EXCPT_SYS_ERR,sizeof(err),(const char *)&err,NON_CONTINUABLE);
  69. }
  70.  
  71. void COMMON ExcptInitialize(pEXCPT_STRUCT exs)
  72. {   exs->exrr.ExceptionHandler = ExcptHndlr;
  73.     (void)DosSetExceptionHandler(&exs->exrr);
  74.     exs->excpt_info = NULL;
  75.     exceptions_off = 0;
  76. }
  77.  
  78. void COMMON ExcptTerminate(pEXCPT_STRUCT exs)
  79. {   while(exs->excpt_info)
  80.     {   pEXCPT_INFO temp = exs->excpt_info;
  81.         exs->excpt_info = temp->next;
  82.         MemoryRls(temp,sizeof(EXCPT_INFO));
  83.     }
  84.     (void)DosUnsetExceptionHandler(&exs->exrr);
  85.     exceptions_off = 1;
  86. }
  87.  
  88.  
  89. void COMMON Fail(const char *s1, const char *s2, const char *s3, int num_par)
  90. {   char buff[500];
  91.     pFAIL_EXCPT pfe = (pFAIL_EXCPT)buff;
  92.     int tot_len;
  93.     pfe->num_par = num_par;
  94.  
  95.     tot_len = 0;
  96.     if(pfe->len1 = (s1 ? strlen(s1) : 0))
  97.     {   memcpy(pfe->texts+tot_len,s1,pfe->len1);
  98.         tot_len += pfe->len1;
  99.     }
  100.  
  101.     if(pfe->len2 = (s2 ? strlen(s2): 0))
  102.     {   memcpy(pfe->texts+tot_len,s2,pfe->len2);
  103.         tot_len += pfe->len2;
  104.     }
  105.  
  106.     if(pfe->len3 = (s3 ? strlen(s3) : 0))
  107.     {   memcpy(pfe->texts+tot_len,s3,pfe->len3);
  108.         tot_len += pfe->len3;
  109.     }
  110.     ExcptPost(EXCPT_FAILURE,tot_len + (sizeof(FAIL_EXCPT) - 1),buff,
  111.                                                 NON_CONTINUABLE);
  112. }
  113. #else
  114. #include "system.h"
  115. #include "excptn.h"
  116. #include <stdio.h>
  117.  
  118. #ifdef __WINDOWS__
  119. #include <string.h>
  120. void COMMON Fail(const char *str1,const char *str2, const char *str3,int stat)
  121. {
  122.         int             Length;
  123.         char    *Message;
  124.         
  125.         MessageBeep(MB_ICONEXCLAMATION);
  126.         Length = strlen(str1) + strlen(str2) + strlen(str3) + 16;
  127.         Message = malloc(Length);
  128.         wsprintf(Message,"%s %s %s Status=%d",str1,str2,str3,stat);
  129.         MessageBox(NULL,Message,"Sirius Failure",MB_APPLMODAL | MB_ICONEXCLAMATION | MB_OK);
  130.         free(Message);
  131. }
  132. #else
  133. void COMMON Fail(const char *str1,const char *str2, const char *str3,int stat)
  134. {
  135.         printf("\nFail SIGNAL occured!\n");
  136.         if (str1)
  137.             printf("%s\n",str1);
  138.         if (str2)
  139.             printf("%s\n",str2);
  140.         if (str3)
  141.             printf("%s\n",str3);
  142.         if (stat)
  143.             printf("%d\n",stat);
  144.         DioBeep();
  145.         exit(99);
  146. }
  147. #endif
  148.  
  149. void COMMON ExcptPost(unsigned long excpt, unsigned len,
  150.                             const char *info, unsigned long flags)
  151. {
  152.         char str[20];
  153.         sprintf(str,"\n EXCEPTION: %x \n",excpt);
  154.         Fail(str,NULL,NULL,0);
  155.         return;
  156. }
  157. #endif
  158.