home *** CD-ROM | disk | FTP | other *** search
/ Education Sampler 1992 [NeXTSTEP] / Education_1992_Sampler.iso / Programming / Source / WAIS / ir / panic.c < prev    next >
Encoding:
C/C++ Source or Header  |  1992-02-02  |  3.1 KB  |  150 lines

  1. /* WIDE AREA INFORMATION SERVER SOFTWARE:
  2.    No guarantees or restrictions.  See the readme file for the full standard
  3.    disclaimer.
  4.  
  5.    Morris@think.com
  6. */
  7.  
  8. /* panic is an error system interface.  On the Mac, it will pop
  9.  * up a little window to explain the problem.
  10.  * On a unix box, it will print out the error and call perror()
  11.  */
  12.  
  13. #include "panic.h"
  14. #include "futil.h"
  15.  
  16. #include <ctype.h>
  17.  
  18. #ifdef ANSI_LIKE
  19. #include <stdarg.h>
  20. #else    
  21. #include <varargs.h>
  22. #endif
  23.  
  24. #ifndef EXIT_FAILURE  /* should be in stdlib */
  25. #define EXIT_FAILURE (-1)
  26. #endif /* ndef EXIT_FAILURE */
  27.  
  28. /*----------------------------------------------------------------------*/
  29.  
  30. static void exitAction _AP((long error));
  31.  
  32. static void
  33. exitAction(error)
  34. long error;
  35. {
  36.   long i;
  37. #ifdef THINK_C
  38.   Debugger();
  39. #else
  40.   for (i = 0; i < 100000; i++)
  41.     ;
  42. #endif
  43.   abort();
  44. }
  45.  
  46. /*----------------------------------------------------------------------*/
  47.  
  48. #define PANIC_HEADER "Fatal Error:  "
  49. #define BELL "\007"
  50.  
  51. #ifdef ANSI_LIKE /* use ansi varargs */
  52.  
  53. #ifdef THINK_C /* pop up a dialog box */
  54. #include "CDynamicError.h"
  55. #endif
  56.  
  57. extern char* log_file_name;
  58. extern FILE* logfile;
  59.  
  60. void
  61. panic(format)
  62. char* format;
  63. {
  64.   va_list ap;            /* the variable arguments */
  65.  
  66. #ifdef THINK_C            /* pop up a dialog box */
  67.  
  68.   char buffer[1000];        /* hope this is enough space! */
  69.   long i;
  70.   strncpy(buffer,PANIC_HEADER,1000);
  71.   SysBeep(5);
  72.   va_start(ap, format);        /* init ap */
  73.   vsprintf(buffer + strlen(PANIC_HEADER),format,ap);
  74.   va_end(ap);            /* free ap */
  75.   for (i = 0L; buffer[i] != '\0'; i++)
  76.     { if (buffer[i] == '\n' || buffer[i] == '\r')
  77.     buffer[i] = ' ';
  78.       }
  79.   gError->PostAlertMessage(buffer);
  80.  
  81. #else                /* print in the shell window */
  82.  
  83.   if(logfile == NULL && log_file_name != NULL)
  84.     logfile = fopen(log_file_name, "a");
  85.  
  86.   if(logfile == NULL) logfile = stderr;
  87.   fprintf(logfile, "%d: 999999: %s: -1: %s  ", 
  88.       getpid(), printable_time(),PANIC_HEADER);
  89.   /* fprintf(stderr,BELL); taken out by brewster 7/91 */
  90.   va_start(ap, format);    /* init ap */
  91.   vfprintf(logfile,format,ap); /* print the contents */
  92.   va_end(ap);            /* free ap */
  93.   fflush(logfile);
  94. #endif
  95.   
  96.   if(logfile != stderr) {
  97.     fclose(logfile);
  98.     logfile = NULL;
  99.   }
  100.  
  101.   exitAction(0);
  102. }
  103.  
  104. #else /* use k&r varargs */
  105.  
  106. extern char* log_file_name;
  107. extern FILE* logfile;
  108.  
  109. void
  110. panic(va_alist)
  111. va_dcl
  112. {
  113.   va_list ap;            /* the variable arguments */
  114.   char* format;
  115.   
  116.   if(logfile == NULL && log_file_name != NULL)
  117.     logfile = fopen(log_file_name, "a");
  118.  
  119.   if(logfile == NULL) logfile = stderr;
  120.   fprintf(logfile, "%d: 999999: %s: -1: %s  ", 
  121.       getpid(), printable_time(),PANIC_HEADER);
  122.   /* fprintf(stderr,BELL); taken out by brewster 7/91 */
  123.   
  124.   va_start(ap);            /* init ap */
  125.  
  126.   format = va_arg(ap,char*);    /* get the format */
  127.   
  128. #ifdef BSD            /* some folks can't do vfprintf */
  129.   fprintf(logfile, "%s",format); /* just print the first thing */
  130. #else
  131.   vfprintf(logfile ,format,ap); /* print the contents */
  132. #endif
  133.  
  134.   va_end(ap);            /* free ap */
  135.  
  136.   fprintf(logfile,"\n");
  137.   fflush(logfile);
  138.  
  139.   if(logfile != stderr) {
  140.     fclose(logfile);
  141.     logfile = NULL;
  142.   }
  143.  
  144.   exitAction(0);
  145. }
  146.   
  147. #endif
  148.  
  149. /*----------------------------------------------------------------------*/
  150.