home *** CD-ROM | disk | FTP | other *** search
/ OS/2 Shareware BBS: 10 Tools / 10-Tools.zip / lifeos2.zip / LIFE-1.02 / SOURCE / ERROR.C < prev    next >
C/C++ Source or Header  |  1996-06-18  |  11KB  |  572 lines

  1. /* Copyright 1991 Digital Equipment Corporation.
  2.  * All Rights Reserved.
  3.  *
  4.  * History:
  5.  *  SCG  21  Tue Jun  2 14:15:36 1992
  6.  *    added newTrace which allows a trace line to be one function call
  7.  *  SCG  14  Wed May 27 13:37:51 1992
  8.  *    added reportAndAbort() which is like report_error followed by
  9.  *    an c_abort.
  10. *****************************************************************/
  11. /*     $Id: error.c,v 1.3 1995/07/11 01:53:03 duchier Exp $     */
  12.  
  13. #ifndef lint
  14. static char vcid[] = "$Id: error.c,v 1.3 1995/07/11 01:53:03 duchier Exp $";
  15. #endif /* lint */
  16.  
  17. #include "extern.h"
  18. #include "print.h"
  19. #include "types.h"
  20. #include "login.h"
  21. #include "lefun.h"
  22. #include "parser.h" /*  RM: Feb  1 1993  */
  23. #ifndef OS2_PORT
  24. #include "built_ins.h"
  25. #else
  26. #include "built_in.h"
  27. #endif
  28.  
  29. #ifdef OS2_PORT
  30. #define VarArgInit(format) va_start(VarArg, format); 
  31.  
  32. #define VarArgNext(t)    va_arg(VarArg, t)
  33. #define VarArgEnd()    va_end(VarArg)
  34. #define vinfoline(format, outfile, xxxx)  { \
  35.   for (p=format;p &&  *p; p++) \
  36.   { \
  37.     if (*p == '%') \
  38.     { \ 
  39.       p++; \
  40.       switch (*p) \
  41.       { \
  42.       case 'd': \
  43.       case 'x': \
  44.         buffer[0] = '%'; \
  45.         buffer[1] = *p; \
  46.         buffer[2] = 0; \
  47.         lng2 = VarArgNext(unsigned long); \
  48.         fprintf(outfile, buffer, lng2); \
  49.         break; \
  50.       case 's': \
  51.         buffer[0] = '%'; \
  52.         buffer[1] = *p; \
  53.         buffer[2] = 0; \
  54.         cptr = VarArgNext(char *); \
  55.         fprintf(outfile, buffer, cptr); \
  56.         break; \
  57.       case 'C': \
  58.         /* type coding as bin string */ \
  59.         pil = VarArgNext(ptr_int_list); \
  60.         print_code(outfile,pil); \
  61.         break; \
  62.       case 'P': \
  63.         psi = VarArgNext(ptr_psi_term); \
  64.         display_psi(outfile,psi); \
  65.         break; \
  66.       case 'O': \
  67.         kind = VarArgNext(operator); \
  68.         print_operator_kind(outfile,kind); \
  69.         break; \
  70.       case 'T': \
  71.         assert(outfile==stderr); \
  72.         t = VarArgNext(def_type); \
  73.         print_def_type(t); \
  74.         break; \
  75.       case 'E': \
  76.         assert(outfile==stderr); \
  77.         psi_term_error(); \
  78.         break; \
  79.       case '%': \
  80.         putc(*p,outfile); \
  81.         break; \
  82.       default: \
  83.         fprintf(outfile,"<%c follows %% : report bug >", *p); \
  84.         break; \
  85.       } \
  86.     } \
  87.     else \
  88.       putc(*p,outfile); \
  89.   } \
  90.   VarArgEnd(); \
  91. #endif
  92.  
  93.  
  94. #include "error.h"
  95.  
  96. long warningflag=TRUE;
  97. long quietflag=FALSE; /* 21.1 */
  98. long trace=FALSE;
  99. long verbose=FALSE; /* 21.1 */
  100. long stepflag;
  101. long steptrace;
  102. long stepcount;
  103.  
  104. /* Depth of goal stack */
  105. static long depth_gs()
  106. {
  107.   long i=0;
  108.   ptr_goal g=goal_stack;
  109.  
  110.   while (g) { i++; g=g->next; }
  111.   return i;
  112. }
  113.  
  114.  
  115. /* Depth of choice point stack */
  116. static long depth_cs()
  117. {
  118.   long i=0;
  119.   ptr_choice_point c=choice_stack;
  120.  
  121.   while (c) { i++; c=c->next; }
  122.   return i;
  123. }
  124.  
  125.  
  126. /* Depth of trail (undo) stack */
  127. static long depth_ts()
  128. {
  129.   ptr_stack t=undo_stack;
  130.   long i=0;
  131.  
  132.   while (t) { i++; t=t->next; }
  133.   return i;
  134. }
  135.  
  136.  
  137. void stack_info(outfile)
  138. FILE *outfile;
  139. {
  140.   /* Information about size of embedded stacks */
  141.   if (verbose) {
  142.     long gn,cn,tn;
  143.     fprintf(outfile,"*** Stack depths [");
  144.     gn=depth_gs();
  145.     cn=depth_cs();
  146.     tn=depth_ts();
  147.     fprintf(outfile,"%ld goal%s, %ld choice point%s, %ld trail entr%s",
  148.             gn,(gn!=1?"s":""),
  149.             cn,(cn!=1?"s":""),
  150.             tn,(tn!=1?"ies":"y"));
  151.     fprintf(outfile,"]\n");
  152.   }
  153. }
  154.  
  155.  
  156.  
  157. /* void vinfoline ARGS((char *format, VarArgBaseDecl)); */
  158. #ifndef OS2_PORT
  159. void vinfoline(); /*  RM: Feb 15 1993  */
  160. #endif
  161.  
  162. #ifndef OS2_PORT
  163. void outputline(format,VarArgBase)
  164. char *format;
  165. VarArgBaseDecl
  166. #else
  167. void outputline(char *format,VarArgBase)
  168. #endif
  169. {
  170.   VarArgDecl;
  171.   VarArgInit(format);
  172.   vinfoline(format,output_stream, VarArg);
  173. }
  174. #ifndef OS2_PORT
  175. void traceline(format, VarArgBase)
  176. char *format;
  177. VarArgBaseDecl
  178. #else
  179. void traceline(char *format,VarArgBase)
  180. #endif
  181. {
  182.   VarArgDecl;
  183.  
  184.  
  185.  /* RM: Nov 10 1993  */
  186.   VarArgInit(format);
  187.  
  188.   if ((trace == 2) && (format[0] != 'p')) return;
  189.   tracing();
  190.  
  191.   vinfoline(format, stdout, VarArg);
  192. }
  193. #ifndef OS2_PORT
  194. void infoline(format, VarArgBase)
  195. char *format;
  196. VarArgBaseDecl
  197. #else
  198. void infoline(char *format,VarArgBase)
  199. #endif
  200.  
  201. {
  202.   VarArgDecl;
  203.  
  204.   VarArgInit(format);
  205.  
  206.   vinfoline(format, stdout, VarArg);
  207. }
  208. #ifndef OS2_PORT
  209. void warningline(format, VarArgBase)
  210. char *format;
  211. VarArgBaseDecl
  212. #else
  213. void warningline(char *format,VarArgBase)
  214. #endif
  215. {
  216.   VarArgDecl;
  217.  
  218.  
  219.   VarArgInit(format);
  220.  
  221.   if(quietflag) return; /*  RM: Sep 24 1993  */
  222.   fprintf(stderr,"*** Warning: ");
  223.   vinfoline(format, stderr, VarArg);
  224. }
  225.  
  226. /* New error printing routine */
  227. #ifndef OS2_PORT
  228. void Errorline(format, VarArgBase)
  229. char *format;
  230. VarArgBaseDecl
  231. #else
  232. void Errorline(char *format,VarArgBase)
  233. #endif
  234.  
  235. {
  236.   VarArgDecl;
  237. #ifdef DJD_DEBUG
  238. printf("format = %x %s\n",format,format);fflush(stdout);
  239. #endif
  240.  
  241.   VarArgInit(format);
  242.  
  243.   fprintf(stderr,"*** Error: ");
  244. #ifdef DJD_DEBUG
  245. printf("format2 = %x %s\n",format,format);
  246. #endif
  247.   vinfoline(format, stderr, VarArg);
  248.  
  249. #ifdef CLIFE
  250.   exit(0);
  251. #endif
  252. }
  253. #ifndef OS2_PORT
  254. void Syntaxerrorline(format, VarArgBase)
  255. char *format;
  256. VarArgBaseDecl
  257. #else
  258. void Syntaxerrorline(char *format,VarArgBase)
  259. #endif
  260.  
  261. {
  262.   VarArgDecl;
  263.  
  264.   VarArgInit(format);
  265.  
  266.   if(parse_ok) { /*  RM: Feb  1 1993  */
  267.     parse_ok=FALSE; /*  RM: Feb  1 1993  */
  268.     fprintf(stderr,"*** Syntax error: ");
  269.     vinfoline(format, stderr, VarArg);
  270.   }
  271. }
  272.  
  273.  
  274. /********************************************************************/
  275.  
  276. /* Utilities for tracing and single stepping */
  277.  
  278. /* Initialize all tracing variables */
  279. void init_trace()
  280. {
  281.   trace=FALSE;
  282.   stepflag=FALSE;
  283.   stepcount=0;
  284. }
  285.  
  286. /* Reset stepcount to zero */
  287. /* Should be called when prompt is printed */
  288. void reset_step()
  289. {
  290.   if (stepcount>0) {
  291.     stepcount=0;
  292.     stepflag=TRUE;
  293.   }
  294. }
  295.  
  296. void tracing()
  297. {
  298.   long i;
  299.   long indent;
  300.  
  301.   printf("T%04ld",goal_count);
  302.   printf(" C%02ld",depth_cs());
  303.   indent=depth_gs();
  304.   if (indent>=MAX_TRACE_INDENT) printf(" G%02ld",indent);
  305.   indent = indent % MAX_TRACE_INDENT;
  306.   for (i=indent; i>=0; i--) printf(" ");
  307.   steptrace=TRUE;
  308. }
  309.  
  310.  
  311. void new_trace(newtrace)
  312. long newtrace;
  313. {
  314.   trace = newtrace;
  315.   printf("*** Tracing is turned ");
  316.   printf(trace?"on.":"off.");
  317.   if (trace == 2) printf(" Only for Proves");
  318.   printf("\n");
  319. }
  320.  
  321. void new_step(newstep)
  322. long newstep;
  323. {
  324.   stepflag = newstep;
  325.   printf("*** Single stepping is turned ");
  326.   printf(stepflag?"on.\n":"off.\n");
  327.   new_trace(stepflag);
  328.   steptrace=FALSE;
  329. }
  330.  
  331. void set_trace_to_prove()
  332. {
  333.   new_trace(2);
  334. }
  335.  
  336. void toggle_trace()
  337. {
  338.   new_trace(trace?0:1);
  339. }
  340.  
  341.  
  342. void toggle_step()
  343. {
  344.   new_step(!stepflag);
  345. }
  346.  
  347. /********************************************************************/
  348.  
  349. /* Old error printing routines -- these should be superceded by Errorline */
  350.  
  351. void perr(str)
  352. char *str;
  353. {
  354.   fprintf(stderr,str);
  355. }
  356.  
  357. void perr_s(s1,s2)
  358. char *s1,*s2;
  359. {
  360.   fprintf(stderr,s1,s2);
  361. }
  362.  
  363. void perr_s2(s1,s2,s3)
  364. char *s1,*s2,*s3;
  365. {
  366.   fprintf(stderr,s1,s2,s3);
  367. }
  368.  
  369. void perr_i(str,i)
  370. char *str;
  371. long i;
  372. {
  373.   fprintf(stderr,str,i);
  374. }
  375.  
  376.  
  377. long warning()
  378. {
  379.   if (warningflag) perr("*** Warning: ");
  380.   return warningflag;
  381. }
  382.  
  383.  
  384. long warningx()
  385. {
  386.   if (warningflag) perr("*** Warning");
  387.   return warningflag;
  388. }
  389.  
  390.  
  391. /* Main routine for report_error and report_warning */
  392. void report_error_main(g,s,s2)
  393. ptr_psi_term g;
  394. char *s, *s2;
  395. {
  396.   FILE *f;
  397.  
  398.   perr_s2("*** %s: %s in '",s2,s);
  399.   display_psi_stderr(g);
  400.   perr("'.\n");
  401. }
  402.  
  403.  
  404.  
  405. /******** REPORT_ERROR(g,s)
  406.   Print an appropriate error message. G is the
  407.   psi-term which caused the error, S a message to print.
  408.   Format: '*** Error: %s in 'g'.'
  409. */
  410. void report_error(g,s)
  411. ptr_psi_term g;
  412. char *s;
  413. {
  414.   report_error_main(g,s,"Error");
  415. }
  416.  
  417.  
  418.  
  419. /******** REPORTANDABORT(g,s)
  420.   Print an appropriate error message. G is the
  421.   psi-term which caused the error, S a message to print.
  422.   Format: '*** Error: %s in 'g'.'
  423. */
  424. long reportAndAbort(g,s)
  425. ptr_psi_term g;
  426. char *s;
  427. {
  428.   report_error_main(g,s,"Error");
  429.   return abort_life();
  430. }
  431.  
  432.  
  433. /******** REPORT_WARNING(g,s)
  434.   Print an appropriate error message. G is the
  435.   psi-term which caused the error, S a message to print.
  436.   Format: '*** Warning: %s in 'g'.'
  437. */
  438. void report_warning(g,s)
  439. ptr_psi_term g;
  440. char *s;
  441. {
  442.   if (warningflag) report_error_main(g,s,"Warning");
  443. }
  444.  
  445.  
  446. /* Main routine for report_error2 and report_warning2 */
  447. void report_error2_main(g,s,s2)
  448. ptr_psi_term g;
  449. char *s, *s2;
  450. {
  451.   FILE *f;
  452.  
  453.   perr_s("*** %s: argument '",s2);
  454.   display_psi_stderr(g);
  455.   perr_s("' %s.\n",s);
  456. }
  457.  
  458.  
  459.  
  460. /********* REPORT_ERROR2(g,s)
  461.   Like report_error, with a slightly different format.
  462.   Format: '*** Error: argument 'g' %s.'
  463. */
  464. void report_error2(g,s)
  465. ptr_psi_term g;
  466. char *s;
  467. {
  468.   report_error2_main(g,s,"Error");
  469. }
  470.  
  471.  
  472.  
  473. /********* REPORT_WARNING2(g,s)
  474.   Like report_warning, with a slightly different format.
  475.   Format: '*** Warning: argument 'g' %s.'
  476. */
  477. void report_warning2(g,s)
  478. ptr_psi_term g;
  479. char *s;
  480. {
  481.   if (warningflag) report_error2_main(g,s,"Warning");
  482. }
  483.  
  484.  
  485.  
  486. /* Give error message if there is an argument which cannot unify with */
  487. /* a real number. */
  488. void nonnum_warning(t,arg1,arg2)
  489. ptr_psi_term t,arg1,arg2;
  490. {
  491.   if (!curried && /* PVR 15.9.93 */
  492.       ((arg1 && !overlap_type(arg1->type,real)) ||
  493.        (arg2 && !overlap_type(arg2->type,real)))) {
  494.     report_warning(t,"non-numeric argument(s)");
  495.   }
  496. }
  497.  
  498. /********************************************************************/
  499.  
  500. /* Error checking routines for bit_and, bit_or, shift, and modulo */
  501.  
  502. long nonint_warning(arg, val, msg)
  503. ptr_psi_term arg;
  504. REAL val;
  505. char *msg;
  506. {
  507.   long err=FALSE;
  508.  
  509.   if (val!=floor(val)) {
  510.     report_warning2(arg, msg);
  511.     err=TRUE;
  512.   }
  513.   return err;
  514. }
  515.  
  516. long bit_and_warning(arg, val)
  517. ptr_psi_term arg;
  518. REAL val;
  519. {
  520.   return nonint_warning(arg,val,"of bitwise 'and' operation is not an integer");
  521. }
  522.  
  523. long bit_or_warning(arg, val)
  524. ptr_psi_term arg;
  525. REAL val;
  526. {
  527.   return nonint_warning(arg,val,"of bitwise 'or' operation is not an integer");
  528. }
  529.  
  530. long bit_not_warning(arg, val)
  531. ptr_psi_term arg;
  532. REAL val;
  533. {
  534.   return nonint_warning(arg,val,"of bitwise 'not' operation is not an integer");
  535. }
  536.  
  537. long int_div_warning(arg, val)
  538. ptr_psi_term arg;
  539. REAL val;
  540. {
  541.   return nonint_warning(arg,val,"of integer division is not an integer");
  542. }
  543.  
  544. long mod_warning(arg, val,zero)
  545. ptr_psi_term arg;
  546. REAL val;
  547. int zero;
  548. {
  549.   int err;
  550.  
  551.   err=nonint_warning(arg,val,"of modulo operation is not an integer");
  552.   if(!err && zero && val==0) {
  553.     Errorline("division by 0 in modulo operation\n");
  554.     err=TRUE;
  555.   }
  556.   return err;
  557. }
  558.  
  559. long shift_warning(dir, arg, val)
  560. long dir;
  561. ptr_psi_term arg;
  562. REAL val;
  563. {
  564.   if (dir)
  565.     return nonint_warning(arg,val,"of right shift operation is not an integer");
  566.   else
  567.     return nonint_warning(arg,val,"of left shift operation is not an integer");
  568. }
  569.  
  570. /********************************************************************/
  571.