home *** CD-ROM | disk | FTP | other *** search
/ Usenet 1994 January / usenetsourcesnewsgroupsinfomagicjanuary1994.iso / sources / misc / volume17 / com_err / part01 / com_err.c < prev    next >
C/C++ Source or Header  |  1991-02-25  |  3KB  |  136 lines

  1. /*
  2.  * Copyright 1987, 1988 by MIT Student Information Processing Board.
  3.  *
  4.  * For copyright info, see mit-sipb-copyright.h.
  5.  */
  6.  
  7. #include <stdio.h>
  8. #include "mit-sipb-copyright.h"
  9.  
  10. /*
  11.  * Our environment only provides for ANSI's <stdarg.h> when using GNU
  12.  * C.  Grump grump...
  13.  */
  14. #if ! __GNUC__
  15. #define VARARGS 1
  16. #endif
  17.  
  18. /* We don't have the v*printf routines... */
  19. #define vfprintf(stream,fmt,args) _doprnt(fmt,args,stream)
  20.  
  21. #if __STDC__ && !VARARGS
  22. #    include <stdarg.h>
  23. #else /* varargs: not STDC or no <stdarg> */
  24.     /* Non-ANSI, always take <varargs.h> path. */
  25. #    undef VARARGS
  26. #    define VARARGS 1
  27. #    include <varargs.h>
  28. #    undef vfprintf
  29. #    define vfprintf(stream,fmt,args) _doprnt(fmt,args,stream)
  30. #endif /* varargs */
  31.  
  32. #include "error_table.h"
  33. #include "internal.h"
  34.  
  35. /*
  36.  * Protect us from header version (externally visible) of com_err, so
  37.  * we can survive in a <varargs.h> environment.  I think.
  38.  */
  39. #define com_err com_err_external
  40. #include "com_err.h"
  41. #undef com_err
  42.  
  43. /* BSD. sigh. */
  44. #undef vfprintf
  45. #define vfprintf(stream,fmt,args) _doprnt(fmt,args,stream)
  46.  
  47. #if ! lint
  48. static const char rcsid[] =
  49.     "$Header: /afs/athena.mit.edu/user/j/jik/src/delete/et/RCS/com_err.c,v 1.2 89/11/07 18:57:42 jik Exp $";
  50. #endif    /* ! lint */
  51.  
  52. static void
  53. #ifdef __STDC__
  54. default_com_err_proc (const char *whoami, long code, const char *fmt, va_list args)
  55. #else
  56. default_com_err_proc (whoami, code, fmt, args)
  57.     const char *whoami;
  58.     long code;
  59.     const char *fmt;
  60.     va_list args;
  61. #endif
  62. {
  63.     if (whoami) {
  64.     fputs(whoami, stderr);
  65.     fputs(": ", stderr);
  66.     }
  67.     if (code) {
  68.     fputs(error_message(code), stderr);
  69.     fputs(" ", stderr);
  70.     }
  71.     if (fmt) {
  72.         vfprintf (stderr, fmt, args);
  73.     }
  74.     putc('\n', stderr);
  75.     /* should do this only on a tty in raw mode */
  76.     putc('\r', stderr);
  77.     fflush(stderr);
  78. }
  79.  
  80. errf com_err_hook = default_com_err_proc;
  81.  
  82. void com_err_va (whoami, code, fmt, args)
  83.     const char *whoami;
  84.     long code;
  85.     const char *fmt;
  86.     va_list args;
  87. {
  88.     (*com_err_hook) (whoami, code, fmt, args);
  89. }
  90.  
  91. #if ! VARARGS
  92. void com_err (const char *whoami,
  93.           long code,
  94.           const char *fmt, ...)
  95. {
  96. #else
  97. void com_err (va_alist)
  98.     va_dcl
  99. {
  100.     const char *whoami, *fmt;
  101.     long code;
  102. #endif
  103.     va_list pvar;
  104.  
  105.     if (!com_err_hook)
  106.     com_err_hook = default_com_err_proc;
  107. #if VARARGS
  108.     va_start (pvar);
  109.     whoami = va_arg (pvar, const char *);
  110.     code = va_arg (pvar, long);
  111.     fmt = va_arg (pvar, const char *);
  112. #else
  113.     va_start(pvar, fmt);
  114. #endif
  115.     com_err_va (whoami, code, fmt, pvar);
  116.     va_end(pvar);
  117. }
  118.  
  119. #ifdef __STDC__
  120. errf set_com_err_hook (errf new_proc)
  121. #else
  122. errf set_com_err_hook (new_proc)
  123.     errf new_proc;
  124. #endif
  125. {
  126.     errf x = com_err_hook;
  127.     com_err_hook = new_proc ? new_proc : (errf) default_com_err_proc;
  128.     return x;
  129. }
  130.  
  131. errf reset_com_err_hook () {
  132.     errf x = com_err_hook;
  133.     com_err_hook = default_com_err_proc;
  134.     return x;
  135. }
  136.