c7c72fd9dedd9d13cc52257cb44d51432fda51db
[openafs.git] / src / comerr / com_err.c
1 /*
2  * Copyright 1987, 1988 by MIT Student Information Processing Board.
3  *
4  * For copyright info, see mit-sipb-cr.h.
5  */
6
7 #include "internal.h"
8 #include <stdio.h>
9 #include <stdarg.h>
10 #include "error_table.h"
11
12
13 /*
14  * Protect us from header version (externally visible) of com_err, so
15  * we can survive in a <varargs.h> environment.  I think.
16  */
17 #define com_err com_err_external
18 /* #include "com_err.h" */
19 char *error_message();
20 #undef com_err
21
22 extern char *error_message ();
23
24 static void
25     default_com_err_proc (const char *whoami, afs_int32 code, const char *fmt, va_list args)
26 {
27     if (whoami) {
28         fputs(whoami, stderr);
29         fputs(": ", stderr);
30     }
31     if (code) {
32         fputs(error_message(code), stderr);
33         fputs(" ", stderr);
34     }
35     if (fmt) {
36         vfprintf (stderr, fmt, args);
37     }
38     putc('\n', stderr);
39     /* should do this only on a tty in raw mode */
40     putc('\r', stderr);
41     fflush(stderr);
42 }
43
44 typedef void (*errf) (const char *, afs_int32, const char *, va_list);
45
46 static errf com_err_hook = default_com_err_proc;
47
48 void com_err_va (whoami, code, fmt, args)
49     const char *whoami;
50     afs_int32 code;
51     const char *fmt;
52     va_list args;
53 {
54     (*com_err_hook) (whoami, code, fmt, args);
55 }
56
57 void com_err (const char *whoami,
58               afs_int32 code,
59               const char *fmt, ...)
60 {
61     va_list pvar;
62
63     if (!com_err_hook)
64         com_err_hook = default_com_err_proc;
65     va_start(pvar, fmt);
66     com_err_va (whoami, code, fmt, pvar);
67     va_end(pvar);
68 }
69
70 errf set_com_err_hook (new_proc)
71     errf new_proc;
72 {
73     errf x = com_err_hook;
74     if (new_proc) com_err_hook = new_proc;
75     else com_err_hook = default_com_err_proc;
76     return x;
77 }
78
79 errf reset_com_err_hook () {
80     errf x = com_err_hook;
81     com_err_hook = default_com_err_proc;
82     return x;
83 }