home *** CD-ROM | disk | FTP | other *** search
/ GEMini Atari / GEMini_Atari_CD-ROM_Walnut_Creek_December_1993.iso / zip / gnu / gperf.lzh / GPERF / STD-ERR.CC < prev    next >
C/C++ Source or Header  |  1993-07-30  |  3KB  |  84 lines

  1. /* Provides a useful variable-length argument error handling abstraction.
  2.  
  3.    Copyright (C) 1989 Free Software Foundation, Inc.
  4.    written by Douglas C. Schmidt (schmidt@ics.uci.edu)
  5.  
  6. This file is part of GNU GPERF.
  7.  
  8. GNU GPERF is free software; you can redistribute it and/or modify
  9. it under the terms of the GNU General Public License as published by
  10. the Free Software Foundation; either version 1, or (at your option)
  11. any later version.
  12.  
  13. GNU GPERF is distributed in the hope that it will be useful,
  14. but WITHOUT ANY WARRANTY; without even the implied warranty of
  15. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  16. GNU General Public License for more details.
  17.  
  18. You should have received a copy of the GNU General Public License
  19. along with GNU GPERF; see the file COPYING.  If not, write to
  20. the Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA.  */
  21.  
  22. #include <stdio.h>
  23. #include <std.h>
  24. #include <stdarg.h>
  25. #include "std-err.h"
  26. #include "trace.h"
  27.  
  28. /* Sets name of program. */
  29.  
  30. void 
  31. Std_Err::set_program_name (char *prog_name) 
  32.   T (Trace t ("Std_Err::set_program_name");)
  33.   program_name = prog_name;
  34. }
  35.  
  36. /* Valid Options (prefixed by '%', as in printf format strings) include:
  37.    'a': exit the program at this point (var-argument is the exit status!)
  38.    'c': print a character
  39.    'd': print a decimal number
  40.    'e': call the function pointed to by the corresponding argument
  41.    'f','g': print a double
  42.    'n': print the name of the program (NULL if not set in constructor or elsewhere)
  43.    'p': print out the appropriate errno value from sys_errlist
  44.    's': print out a character string
  45.    '%': print out a single percent sign, '%' */
  46.  
  47. void 
  48. Std_Err::report_error (char *format, ...) 
  49.   T (Trace t ("Std_Err::report_error");)
  50.   extern int errno, sys_nerr;
  51.   extern char *sys_errlist[];
  52.   typedef void (*PTF)();
  53.   va_list argp;
  54.  
  55.   for (va_start (argp, format); *format; format++) 
  56.     {
  57.       if (*format != '%') 
  58.         putc (*format, stderr);
  59.       else 
  60.         {
  61.           switch (*++format) 
  62.             {
  63.             case '%' : putc ('%', stderr); break;
  64.             case 'a' : exit (va_arg (argp, int));
  65.             case 'c' : putc (va_arg (argp, int), stderr); break;
  66.             case 'd' : fprintf (stderr, "%d", va_arg (argp, int)); break;
  67.             case 'e' : (*va_arg (argp, PTF))(); break;
  68.             case 'f' : fprintf (stderr, "%g", va_arg (argp, double)); break;
  69.             case 'n' : fputs (program_name ? program_name : "error", stderr); break;
  70.             case 'p' : 
  71.               if (errno >= 0 && errno < sys_nerr) 
  72.                 fprintf (stderr, "%s: %s", va_arg (argp, char *), sys_errlist[errno]);
  73.               else 
  74.                 fprintf (stderr, "<unknown error> %d", errno);
  75.               break;
  76.             case 's' : fputs (va_arg (argp, char *), stderr); break;
  77.             }
  78.         }
  79.     }
  80.   va_end (argp);
  81. }
  82.