home *** CD-ROM | disk | FTP | other *** search
/ RISC DISC 2 / RISC_DISC_2.iso / pd_share / program / language / as / source / c / error < prev    next >
Encoding:
Text File  |  1993-05-19  |  3.4 KB  |  159 lines

  1. /* 
  2.  * error.c
  3.  * Copyright © 1992 Niklas Röjemo
  4.  */
  5.  
  6. #include <setjmp.h>
  7. #include <stdarg.h>
  8. #include <stdlib.h>
  9. #include <stdio.h>
  10. #include <string.h>
  11. #include <ctype.h>
  12. #include "error.h"
  13. #include "input.h"
  14. #include "output.h"
  15. #include "riscos.h"
  16.  
  17. #define MAXERR 100
  18.  
  19. extern int pedantic;
  20.  
  21. static int no_errors;
  22. static int no_warnings;
  23.  
  24. #ifndef UNIX
  25. static int ThrowbackStarted;
  26. #endif
  27. static char *Filename;
  28.  
  29. static char errbuf[1024];
  30.  
  31. void errorInit(int throwback,char *filename)
  32. {
  33.   if(throwback)
  34.     Filename = CanonicalisePath(filename);
  35.   else
  36.     Filename = 0;
  37. }
  38.  
  39. void errorFinish(void)
  40. {
  41. #ifndef UNIX
  42.   if(ThrowbackStarted>0)
  43.     ThrowbackEnd();
  44. #endif
  45. }
  46.  
  47. #ifndef UNIX
  48. char *LF(char *buf)
  49. {
  50.   char *p = buf;
  51.   p--;
  52.   while(*++p)
  53.     if(*p == '\n' || *p == '\r') *p = ' ';
  54.   while(*--p == ' ' && p > buf)
  55.     *p = 0;
  56.   *++p = '\n';
  57.   *++p = 0;
  58.   return buf;
  59. }
  60.  
  61. static void TB(int level, int lineno,char *error)
  62. {
  63.   os_error *err;
  64.   if(!Filename)
  65.     return;
  66.   if(!ThrowbackStarted) {
  67.     err = ThrowbackStart();
  68.     if(err) {
  69.       fprintf(stderr,"ThrowbackStart %s\n",err->errmess);
  70.       exit(-1);
  71.     }
  72.     err = ThrowbackSendStart(Filename);
  73.     if(err) {
  74.         if(pedantic)
  75.            fprintf(stderr,"ThrowbackSendStart %s",err->errmess);
  76.         ThrowbackStarted = -1;
  77.     } else 
  78.         ThrowbackStarted = 1;
  79.   }
  80.   if(ThrowbackStarted>0)
  81.     ThrowbackSendError(level,lineno,error);
  82. }
  83. #endif
  84.  
  85. int noerrors(void)
  86. {
  87.    if(no_errors)
  88.      outputRemove();
  89.    return no_errors;
  90. }
  91.  
  92. int nowarnings(void)
  93. {
  94.   return no_warnings;
  95. }
  96.  
  97. extern jmp_buf asmContinue;
  98. extern jmp_buf asmAbort;
  99.  
  100. static void fixup(ErrorTag t)
  101. {
  102.   skiprest();
  103.   if(t == ErrorAbort || no_errors>MAXERR)
  104.     longjmp(asmAbort,1);
  105.   else
  106.     longjmp(asmContinue,1);
  107. }
  108.  
  109. void error(ErrorTag e, BOOL c, const char *format, ...)
  110. {
  111.   char *str;
  112.   va_list ap;
  113. #ifndef UNIX
  114.   int t;
  115.   switch(e) {
  116.     case ErrorInfo:    str = "INFO";                       t = ThrowbackInfo;         break;
  117.     case ErrorWarning: str = "WARNING";     no_warnings++; t = ThrowbackWarning;      break;
  118.     case ErrorError:   str = "ERROR";       no_errors++;   t = ThrowbackError;        break;
  119.     default:           str = "FATAL ERROR"; no_errors++;   t = ThrowbackSeriousError; break;
  120.   }
  121. #else
  122.   switch(e) {
  123.     case ErrorInfo:    str = "INFO";                       break;
  124.     case ErrorWarning: str = "WARNING";     no_warnings++; break;
  125.     case ErrorError:   str = "ERROR";       no_errors++;   break;
  126.     default:           str = "FATAL ERROR"; no_errors++;   break;
  127.   }
  128. #endif
  129.   va_start(ap,format);
  130.   vsprintf(errbuf,format,ap);
  131.   fprintf(stderr,"%s %5d: %s\n",str,inputLineNo,errbuf);
  132.   va_end(ap);
  133. #ifndef UNIX
  134.   TB(t,inputLineNo,errbuf);
  135. #endif
  136.   if(!c || no_errors > MAXERR) fixup(e);
  137. }
  138.  
  139. void errorLine(int lineno,ErrorTag e, BOOL c, const char *format, ...)
  140. {
  141.   char *str;
  142.   int t;
  143.   va_list ap;
  144.   switch(e) {
  145.     case ErrorInfo:    str = "INFO";        t = ThrowbackInfo;         break;
  146.     case ErrorWarning: str = "WARNING";     t = ThrowbackWarning;      no_warnings++; break;
  147.     case ErrorError:   str = "ERROR";       t = ThrowbackError;        no_errors++;   break;
  148.     default:           str = "FATAL ERROR"; t = ThrowbackSeriousError; no_errors++;   break;
  149.   }
  150.   va_start(ap,format);
  151.   vsprintf(errbuf,format,ap);
  152.   fprintf(stderr,"%s %5d: %s\n",str,lineno,errbuf);
  153.   va_end(ap);
  154. #ifndef UNIX
  155.   TB(t,lineno,errbuf);
  156. #endif
  157.   if(!c || no_errors > MAXERR) fixup(e);
  158. }
  159.