home *** CD-ROM | disk | FTP | other *** search
/ InfoMagic Source Code 1993 July / THE_SOURCE_CODE_CD_ROM.iso / gnu / cperf-2.1 / src / stderr.c < prev    next >
Encoding:
C/C++ Source or Header  |  1989-11-11  |  2.8 KB  |  91 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 "stderr.h"
  24.  
  25. /* Holds the name of the currently active program. */
  26. static char *program_name;
  27.  
  28. /* Sets name of program. */
  29.  
  30. void 
  31. set_program_name (prog_name)
  32.      char *prog_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
  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. report_error (va_alist) 
  49.      va_dcl
  50. {
  51.   extern int errno, sys_nerr;
  52.   extern char *sys_errlist[];
  53.   typedef void (*PTF)();
  54.     typedef char *CHARP;
  55.   va_list argp;
  56.   int     abort = 0;
  57.   char   *format;
  58.  
  59.   va_start (argp);
  60.  
  61.   for (format = va_arg (argp, char *); *format; format++) 
  62.     {
  63.       if (*format != '%') 
  64.         putc (*format, stderr);
  65.       else 
  66.         {
  67.           switch(*++format) 
  68.             {
  69.             case '%' : putc ('%', stderr); break;
  70.             case 'a' : abort = 1; break;
  71.             case 'c' : putc (va_arg (argp, int), stderr); break;
  72.             case 'd' : fprintf (stderr, "%d", va_arg (argp, int)); break;
  73.             case 'e' : (*va_arg (argp, PTF))(); break;
  74.             case 'f' : fprintf (stderr, "%g", va_arg (argp, double)); break;
  75.             case 'n' : fputs (program_name ? program_name : "error", stderr); break;
  76.             case 'p' : 
  77.               if (errno < sys_nerr) 
  78.                 fprintf (stderr, "%s: %s", va_arg (argp, CHARP), sys_errlist[errno]);
  79.               else 
  80.                 fprintf (stderr, "<unknown error> %d", errno);
  81.               break;
  82.             case 's' : fputs (va_arg (argp, CHARP), stderr); break;
  83.             }
  84.         }
  85.       if (abort) 
  86.         exit (1);
  87.     }
  88.   va_end (argp);
  89. }
  90.