home *** CD-ROM | disk | FTP | other *** search
/ rtsi.com / 2014.01.www.rtsi.com.tar / www.rtsi.com / OS9 / OSK / APPS / lout2.lzh / LOUT2 / z28.c < prev    next >
Text File  |  1994-01-23  |  8KB  |  176 lines

  1. /*@z28.c:Error Service:ErrorInit(), ErrorSeen()@******************************/
  2. /*                                                                           */
  3. /*  LOUT: A HIGH-LEVEL LANGUAGE FOR DOCUMENT FORMATTING (VERSION 2.05)       */
  4. /*  COPYRIGHT (C) 1993 Jeffrey H. Kingston                                   */
  5. /*                                                                           */
  6. /*  Jeffrey H. Kingston (jeff@cs.su.oz.au)                                   */
  7. /*  Basser Department of Computer Science                                    */
  8. /*  The University of Sydney 2006                                            */
  9. /*  AUSTRALIA                                                                */
  10. /*                                                                           */
  11. /*  This program is free software; you can redistribute it and/or modify     */
  12. /*  it under the terms of the GNU General Public License as published by     */
  13. /*  the Free Software Foundation; either version 1, or (at your option)      */
  14. /*  any later version.                                                       */
  15. /*                                                                           */
  16. /*  This program is distributed in the hope that it will be useful,          */
  17. /*  but WITHOUT ANY WARRANTY; without even the implied warranty of           */
  18. /*  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the            */
  19. /*  GNU General Public License for more details.                             */
  20. /*                                                                           */
  21. /*  You should have received a copy of the GNU General Public License        */
  22. /*  along with this program; if not, write to the Free Software              */
  23. /*  Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.                */
  24. /*                                                                           */
  25. /*  FILE:         z28.c                                                      */
  26. /*  MODULE:       Error Service                                              */
  27. /*  EXTERNS:      ErrorInit(), Error(), ErrorSeen()                          */
  28. /*                                                                           */
  29. /*****************************************************************************/
  30. #include "externs"
  31.  
  32. #define    MAX_BLOCKS     20        /* max number of error blocks        */
  33. #define    MAX_ERRORS     20        /* max number of held error messages */
  34.  
  35. static BOOLEAN    print_block[MAX_BLOCKS];    /* TRUE if print this block  */
  36. static int    start_block[MAX_BLOCKS];    /* first message of block    */
  37. static char    message[MAX_ERRORS][MAX_LINE];    /* the error messages    */
  38. static FILE    *fp = NULL;            /* file pointer of log file  */
  39. static BOOLEAN    error_seen = FALSE;        /* TRUE after first error    */
  40. static int    block_top = 0;            /* first free error block    */
  41. static int    mess_top = 0;            /* first free error message  */
  42.  
  43.  
  44. /*****************************************************************************/
  45. /*                                                                           */
  46. /*  ErrorInit(str)                                                           */
  47. /*                                                                           */
  48. /*  Open log file str and initialise this module.                            */
  49. /*                                                                           */
  50. /*****************************************************************************/
  51.  
  52. ErrorInit(str)
  53. FULL_CHAR *str;
  54. { if( fp != NULL )
  55.     Error(FATAL, no_fpos, "-e argument appears twice in command line");
  56.   fp = StringFOpen(str, "w");
  57.   if( fp == NULL )
  58.     Error(FATAL, no_fpos, "cannot open error file \"%s\"", str);
  59. } /* end ErrorInit */
  60.  
  61.  
  62. /*****************************************************************************/
  63. /*                                                                           */
  64. /*  BOOLEAN ErrorSeen()                                                      */
  65. /*                                                                           */
  66. /*  TRUE once an error has been found.                                       */
  67. /*                                                                           */
  68. /*****************************************************************************/
  69.  
  70. BOOLEAN ErrorSeen()
  71. { return error_seen;
  72. } /* end ErrorSeen */
  73.  
  74.  
  75. /*@::EnterErrorBlock(), LeaveErrorBlock()@************************************/
  76. /*                                                                           */
  77. /*  EnterErrorBlock(ok_to_print)                                             */
  78. /*                                                                           */
  79. /*  Start off a new block of error messages.  If ok_to_print, they do not    */
  80. /*  need to be held for a later commit.                                      */
  81. /*                                                                           */
  82. /*****************************************************************************/
  83.  
  84. EnterErrorBlock(ok_to_print)
  85. BOOLEAN ok_to_print;
  86. { if( block_top < MAX_BLOCKS )
  87.   { print_block[block_top] = ok_to_print;
  88.     start_block[block_top] = mess_top;
  89.     block_top++;
  90.   }
  91.   else Error(FATAL, no_fpos, "too many levels of error messages");
  92. } /* end EnterErrorBlock */
  93.  
  94.  
  95. /*****************************************************************************/
  96. /*                                                                           */
  97. /*  LeaveErrorBlock(commit)                                                  */
  98. /*                                                                           */
  99. /*  Finish off a block or error messages.  If commit is true, print them,    */
  100. /*  otherwise discard them.                                                  */
  101. /*                                                                           */
  102. /*****************************************************************************/
  103.  
  104. LeaveErrorBlock(commit)
  105. BOOLEAN commit;
  106. { int i;
  107.   assert( block_top > 0, "LeaveErrorBlock: no matching EnterErrorBlock!" );
  108.   assert( commit || !print_block[block_top - 1], "LeaveErrorBlock: commit!" );
  109.   if( fp == NULL )  fp = stderr;
  110.   if( commit )
  111.   { for( i = start_block[block_top - 1];  i < mess_top;  i++ )
  112.       fputs(message[i], fp);
  113.   }
  114.   block_top--;
  115.   mess_top = start_block[block_top];
  116. } /* end LeaveErrorBlock */
  117.  
  118.  
  119. /*@::Error()@*****************************************************************/
  120. /*                                                                           */
  121. /*  Error(etype, pos, str, p1, p2, p3, p4, p5, p6)                           */
  122. /*                                                                           */
  123. /*  Report error of type etype at position *pos in input.                    */
  124. /*  The error message is str with parameters p1 - p6.                        */
  125. /*                                                                           */
  126. /*****************************************************************************/
  127.  
  128. /*VARARGS3*/
  129. Error(etype, pos, str, p1, p2, p3, p4, p5, p6)
  130. int etype;  FILE_POS *pos;  char *str, *p1, *p2, *p3, *p4, *p5, *p6;
  131. { char val[MAX_LINE];
  132.   sprintf(val, str, p1, p2, p3, p4, p5, p6);
  133.   if( fp == NULL )  fp = stderr;
  134.   switch( etype )
  135.   {
  136.  
  137.     case INTERN:
  138.     
  139.       while( block_top > 0 )  LeaveErrorBlock(TRUE);
  140.       fprintf(fp, "lout%s internal error: %s\n", EchoFilePos(pos), val);
  141. #if DEBUG_ON
  142.       abort();
  143. #else
  144.       exit(1);
  145. #endif
  146.       break;
  147.  
  148.  
  149.     case FATAL:
  150.     
  151.       while( block_top > 0 )  LeaveErrorBlock(TRUE);
  152.       fprintf(fp, "lout%s fatal error: %s\n", EchoFilePos(pos), val);
  153.       exit(1);
  154.       break;
  155.  
  156.  
  157.     case WARN:
  158.     
  159.       if( block_top == 0 || print_block[block_top - 1] )
  160.     fprintf(fp, "lout%s: %s\n", EchoFilePos(pos), val);
  161.       else if( mess_top < MAX_ERRORS )
  162.     sprintf(message[mess_top++], "lout%s: %s\n", EchoFilePos(pos), val);
  163.       else Error(FATAL, pos, "too many error messages");
  164.       error_seen = TRUE;
  165.       break;
  166.  
  167.  
  168.     default:
  169.     
  170.       Error(INTERN, no_fpos, "invalid error type");
  171.       exit(1);
  172.       break;
  173.  
  174.   }
  175. } /* end Error */
  176.