1 /*
2 * Copyright 1987, 1988 by MIT Student Information Processing Board.
3 *
4 * Permission to use, copy, modify, and distribute this software and
5 * its documentation for any purpose is hereby granted, provided that
6 * the names of M.I.T. and the M.I.T. S.I.P.B. not be used in
7 * advertising or publicity pertaining to distribution of the software
8 * without specific, written prior permission. M.I.T. and the
9 * M.I.T. S.I.P.B. make no representations about the suitability of
10 * this software for any purpose. It is provided "as is" without
11 * express or implied warranty.
12 */
13
14 #include <stdio.h>
15 #include "com_err.h"
16 #include "error_table.h"
17 #include "internal.h"
18
19 static void
20 default_com_err_proc (const char *whoami, errcode_t code, const
21 char *fmt, va_list args)
22 COM_ERR_ATTR((format(printf, 3, 0)));
23
24 static void
default_com_err_proc(const char * whoami,errcode_t code,const char * fmt,va_list args)25 default_com_err_proc (const char *whoami, errcode_t code, const
26 char *fmt, va_list args)
27 {
28 if (whoami) {
29 fputs(whoami, stderr);
30 fputs(": ", stderr);
31 }
32 if (code) {
33 fputs(error_message(code), stderr);
34 fputs(" ", stderr);
35 }
36 if (fmt) {
37 vfprintf (stderr, fmt, args);
38 }
39 /* should output \r only if using a tty in raw mode */
40 fputs("\r\n", stderr);
41 fflush(stderr);
42 }
43
44 typedef void (*errf) (const char *, errcode_t, const char *, va_list);
45
46 errf com_err_hook = default_com_err_proc;
47
com_err_va(const char * whoami,errcode_t code,const char * fmt,va_list args)48 void com_err_va (const char *whoami, errcode_t code, const char *fmt,
49 va_list args)
50 {
51 (*com_err_hook) (whoami, code, fmt, args);
52 }
53
com_err(const char * whoami,errcode_t code,const char * fmt,...)54 void com_err (const char *whoami,
55 errcode_t code,
56 const char *fmt, ...)
57 {
58 va_list pvar;
59
60 if (!com_err_hook)
61 com_err_hook = default_com_err_proc;
62 va_start(pvar, fmt);
63 com_err_va (whoami, code, fmt, pvar);
64 va_end(pvar);
65 }
66
set_com_err_hook(new_proc)67 errf set_com_err_hook (new_proc)
68 errf new_proc;
69 {
70 errf x = com_err_hook;
71
72 if (new_proc)
73 com_err_hook = new_proc;
74 else
75 com_err_hook = default_com_err_proc;
76
77 return x;
78 }
79
reset_com_err_hook()80 errf reset_com_err_hook () {
81 errf x = com_err_hook;
82 com_err_hook = default_com_err_proc;
83 return x;
84 }
85