home *** CD-ROM | disk | FTP | other *** search
/ OS/2 Shareware BBS: 35 Internet / 35-Internet.zip / fingercl.zip / error.c < prev    next >
C/C++ Source or Header  |  1997-08-29  |  3KB  |  124 lines

  1. /* error.c -- Simple error handling. */
  2.  
  3. /* Copyright (C) 1988, 1990, 1992  Free Software Foundation, Inc.
  4.  
  5.    This file is part of GNU Finger.
  6.  
  7.    This program is free software; you can redistribute it and/or modify
  8.    it under the terms of the GNU General Public License as published by
  9.    the Free Software Foundation; either version 2, or (at your option)
  10.    any later version.
  11.  
  12.    This program is distributed in the hope that it will be useful,
  13.    but WITHOUT ANY WARRANTY; without even the implied warranty of
  14.    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  15.    GNU General Public License for more details.
  16.  
  17.    You should have received a copy of the GNU General Public License
  18.    along with this program; if not, write to the Free Software
  19.    Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.  */
  20.  
  21. #include <stdio.h>
  22. #ifdef __EMX__
  23. #include <stdlib.h>
  24. #endif
  25. #include <setjmp.h>
  26. #include <config.h>
  27. #include <general.h>
  28. #include <error.h>
  29.  
  30. /* These can be filled in `manually' by callers, but the easiest way
  31.    is to call default_error_handling (argv[0]). */
  32. char *progname = "finger:-(";
  33. jmp_buf top_level;
  34.  
  35. /* **************************************************************** */
  36. /*                                                                  */
  37. /*                      Error Handler                               */
  38. /*                                                                  */
  39. /* **************************************************************** */
  40.  
  41. default_error_handling (program_name)
  42.      char *program_name;
  43. {
  44.   if ((char *)rindex (program_name, '/') != (char *)NULL)
  45.     {
  46.       program_name = (char *)rindex (program_name, '/');
  47.       program_name++;
  48.     }
  49.  
  50.   progname = savestring (program_name);
  51.   strcat(progname,":");
  52.   if (setjmp (top_level))
  53.     exit (1);
  54. }
  55.  
  56. /* Hack to handle previous bad setjmp (). */
  57. longjmperror ()
  58. {
  59.   exit (1);
  60. }
  61.  
  62. /* Handle some error. */
  63. void
  64. handle_error (severity, format, arg1, arg2, arg3)
  65.      int severity;
  66.      char *format, *arg1, *arg2, *arg3;
  67. {
  68.   fprintf (stderr, "%s  ", progname);
  69.   fprintf (stderr, format, arg1, arg2, arg3);
  70.   fprintf (stderr, "\n");
  71.  
  72.   switch (severity)
  73.     {
  74.     case FATAL:
  75.       exit (1);
  76.  
  77.     case ERROR:
  78.       longjmp (top_level, 0);
  79.  
  80.     case WARNING:
  81.       break;
  82.  
  83.     default:
  84.       fprintf (stderr, "\
  85. handle_error () called with bad severity (%d).\n\
  86. Who knows what else is wrong with the code?  Stopping.\n", severity);
  87.       exit (2);
  88.     }
  89. }
  90.  
  91. /* Handle a file error.  You pass severity and filename. If FILENAME is null,
  92.    then don't print it. */
  93. void
  94. file_error (severity, filename)
  95.      int severity;
  96.      char *filename;
  97. {
  98.   extern int errno, sys_nerr;
  99. #ifndef __EMX__
  100.   extern char *sys_errlist[];
  101. #endif
  102.   char *error_text;
  103.  
  104.   if (errno) {
  105.     if (errno < sys_nerr)
  106.       error_text = sys_errlist[errno];
  107.     else
  108.       error_text = "(large errno?)";
  109.   } else {
  110.     error_text = "(Hmm, no error?)";
  111.   }
  112.  
  113.   if (filename == (char *)NULL)
  114.     handle_error (severity, "%s", error_text);
  115.   else
  116.     handle_error (severity, "%s: %s", filename, error_text);
  117. }
  118.  
  119. warning (format, arg1, arg2, arg3)
  120.      char *format, *arg1, *arg2, *arg3;
  121. {
  122.   handle_error (WARNING, format, arg1, arg2, arg3);
  123. }
  124.