home *** CD-ROM | disk | FTP | other *** search
/ Geek Gadgets 1 / ADE-1.bin / ade-dist / octave-1.1.1p1-base.tgz / octave-1.1.1p1-base.tar / fsf / octave / src / error.cc < prev    next >
C/C++ Source or Header  |  1995-01-03  |  5KB  |  247 lines

  1. // error.cc                                             -*- C++ -*-
  2. /*
  3.  
  4. Copyright (C) 1992, 1993, 1994, 1995 John W. Eaton
  5.  
  6. This file is part of Octave.
  7.  
  8. Octave is free software; you can redistribute it and/or modify it
  9. under the terms of the GNU General Public License as published by the
  10. Free Software Foundation; either version 2, or (at your option) any
  11. later version.
  12.  
  13. Octave is distributed in the hope that it will be useful, but WITHOUT
  14. ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  15. FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
  16. for more details.
  17.  
  18. You should have received a copy of the GNU General Public License
  19. along with Octave; see the file COPYING.  If not, write to the Free
  20. Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA.
  21.  
  22. */
  23.  
  24. #ifdef HAVE_CONFIG_H
  25. #include "config.h"
  26. #endif
  27.  
  28. #include <strstream.h>
  29. #include <stdarg.h>
  30.  
  31. #include "utils.h"
  32. #include "error.h"
  33. #include "pager.h"
  34. #include "oct-obj.h"
  35. #include "tree-const.h"
  36. #include "defun.h"
  37.  
  38. // Current error state.
  39. int error_state = 0;
  40.  
  41. // XXX FIXME XXX
  42. int suppress_octave_error_messages = 0;
  43.  
  44. static void
  45. verror (const char *name, const char *fmt, va_list args)
  46. {
  47.   flush_output_to_pager ();
  48.  
  49.   if (name)
  50.     cerr << name << ": ";
  51.   cerr.vform (fmt, args);
  52.   cerr << endl;
  53.  
  54.   ostrstream output_buf;
  55.  
  56.   if (name)
  57.     output_buf << name << ": ";
  58.   output_buf.vform (fmt, args);
  59.   output_buf << endl;
  60.  
  61.   char *msg = output_buf.str ();
  62.  
  63.   maybe_write_to_diary_file (msg);
  64.  
  65.   delete [] msg;
  66. }
  67.  
  68. static void
  69. error_1 (const char *name, const char *fmt, va_list args)
  70. {
  71.   if (error_state != -2)
  72.     {
  73.       if (! error_state)
  74.     error_state = 1;
  75.  
  76.       if (! suppress_octave_error_messages)
  77.     {
  78.       int len = 0;
  79.       if (fmt && *fmt && fmt[(len = strlen (fmt)) - 1] == '\n')
  80.         {
  81.           error_state = -2;
  82.           char *tmp_fmt = strsave (fmt);
  83.           tmp_fmt[len - 1] = '\0';
  84.           verror (name, tmp_fmt, args);
  85.           delete [] tmp_fmt;
  86.         }
  87.       else
  88.         verror (name, fmt, args);
  89.     }
  90.     }
  91. }
  92.  
  93. void
  94. message (const char *name, const char *fmt, ...)
  95. {
  96.   va_list args;
  97.   va_start (args, fmt);
  98.   verror (name, fmt, args);
  99.   va_end (args);
  100. }
  101.  
  102. void
  103. usage (const char *fmt, ...)
  104. {
  105.   va_list args;
  106.   va_start (args, fmt);
  107.   error_state = -1;
  108.   verror ("usage", fmt, args);
  109.   va_end (args);
  110. }
  111.  
  112. void
  113. warning (const char *fmt, ...)
  114. {
  115.   va_list args;
  116.   va_start (args, fmt);
  117.   verror ("warning", fmt, args);
  118.   va_end (args);
  119. }
  120.  
  121. void
  122. error (const char *fmt, ...)
  123. {
  124.   va_list args;
  125.   va_start (args, fmt);
  126.   error_1 ("error", fmt, args);
  127.   va_end (args);
  128. }
  129.  
  130. void
  131. parse_error (const char *fmt, ...)
  132. {
  133.   va_list args;
  134.   va_start (args, fmt);
  135.   error_1 (0, fmt, args);
  136.   va_end (args);
  137. }
  138.  
  139. void
  140. panic (const char *fmt, ...)
  141. {
  142.   flush_output_to_pager ();
  143.  
  144.   va_list args;
  145.   va_start (args, fmt);
  146.   verror ("panic", fmt, args);
  147.   va_end (args);
  148.   abort ();
  149. }
  150.  
  151. DEFUN ("error", Ferror, Serror, 1, 1,
  152.   "error (MESSAGE): print MESSAGE and set the error state.\n\
  153. This should eventually take us up to the top level, possibly\n\
  154. printing traceback messages as we go.\n\
  155. \n\
  156. If MESSAGE ends in a newline character, traceback messages are not\n\
  157. printed.") 
  158. {
  159.   Octave_object retval;
  160.  
  161.   char *msg = "unspecified error";
  162.  
  163.   int nargin = args.length ();
  164.  
  165.   if (nargin == 1 && args(0).is_defined ())
  166.     {
  167.       if (args(0).is_string ())
  168.     {
  169.       msg = args(0).string_value ();
  170.  
  171.       if (! msg || ! *msg)
  172.         return retval;
  173.     }
  174.       else if (args(0).is_empty ())
  175.     return retval;
  176.     }
  177.  
  178.   error (msg);
  179.  
  180.   return retval;
  181. }
  182.  
  183. DEFUN ("warning", Fwarning, Swarning, 1, 1,
  184.   "warning (MESSAGE): print a warning MESSAGE.\n\
  185. \n\
  186. See also: error")
  187. {
  188.   Octave_object retval;
  189.  
  190.   char *msg = "unspecified warning";
  191.  
  192.   int nargin = args.length ();
  193.  
  194.   if (nargin == 1 && args(0).is_defined ())
  195.     {
  196.       if (args(0).is_string ())
  197.     {
  198.       msg = args(0).string_value ();
  199.  
  200.       if (! msg || ! *msg)
  201.         return retval;
  202.     }
  203.       else if (args(0).is_empty ())
  204.     return retval;
  205.     }
  206.  
  207.   warning (msg);
  208.  
  209.   return retval;
  210. }
  211.  
  212. DEFUN ("usage", Fusage, Susage, 1, 1,
  213.   "usage (MESSAGE): print a usage MESSAGE.\n\
  214. \n\
  215. See also: error")
  216. {
  217.   Octave_object retval;
  218.  
  219.   char *msg = "unknown";
  220.  
  221.   int nargin = args.length ();
  222.  
  223.   if (nargin == 1 && args(0).is_defined ())
  224.     {
  225.       if (args(0).is_string ())
  226.     {
  227.       msg = args(0).string_value ();
  228.  
  229.       if (! msg || ! *msg)
  230.         return retval;
  231.     }
  232.       else if (args(0).is_empty ())
  233.     return retval;
  234.     }
  235.  
  236.   usage (msg);
  237.  
  238.   return retval;
  239. }
  240.  
  241. /*
  242. ;;; Local Variables: ***
  243. ;;; mode: C++ ***
  244. ;;; page-delimiter: "^/\\*" ***
  245. ;;; End: ***
  246. */
  247.