Windows global stuff:
[krb5.git] / src / util / et / com_err.c
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 <string.h>
9 #include "mit-sipb-copyright.h"
10
11 #if defined(HAVE_STDARG_H) || defined(_WINDOWS)
12 #include <stdarg.h>
13 #else
14 #include <varargs.h>
15 #define VARARGS
16 #endif
17
18 #include "error_table.h"
19 #include "internal.h"
20
21 #ifdef notdef
22 /*
23  * Protect us from header version (externally visible) of com_err, so
24  * we can survive in a <varargs.h> environment.  I think.
25  */
26 #define com_err com_err_external
27 #include "com_err.h"
28 #undef com_err
29 #endif
30
31 /* We have problems with varargs definitions if we include com_err.h */
32
33 /*
34  * XXX for now, we define error_message by hand.  Ultimately, we
35  * should fix up com_err.h so that it's safe to #include here 
36  * directly.
37  */
38 #if defined(__STDC__) || defined(_WINDOWS)
39 extern char const * INTERFACE error_message (long);
40 #else
41 extern char * INTERFACE error_message ();
42 #endif
43
44 static void
45 #if defined(__STDC__) || defined(_WINDOWS)
46     default_com_err_proc (const char *whoami, long code, const char *fmt, va_list args)
47 #else
48     default_com_err_proc (whoami, code, fmt, args)
49     const char *whoami;
50     long code;
51     const char *fmt;
52     va_list args;
53 #endif
54 {
55     static char errbuf[1024];                   /* For those w/o stdio */
56
57     *errbuf = '\0';
58     if (whoami) {
59         strcat (errbuf, whoami);
60         strcat (errbuf, ": ");
61     }
62     if (code) {
63         strcat (errbuf, error_message(code));
64         strcat (errbuf, " ");
65     }
66     if (fmt) {
67         vsprintf (errbuf + strlen (errbuf), fmt, args);
68     }
69 #ifdef _WINDOWS
70     MessageBox (NULL, errbuf, "Kerboros", MB_ICONEXCLAMATION);
71 #else
72     fputs (errbuf, stderr);
73     /* should do this only on a tty in raw mode */
74     putc('\r', stderr);
75     putc('\n', stderr);
76     fflush(stderr);
77 #endif
78 }
79
80 #if defined(__STDC__) || defined(_WINDOWS)
81 typedef void (*errf) (const char *, long, const char *, va_list);
82 #else
83 typedef void (*errf) ();
84 #endif
85
86 errf com_err_hook = default_com_err_proc;
87
88 void com_err_va (whoami, code, fmt, args)
89     const char *whoami;
90     long code;
91     const char *fmt;
92     va_list args;
93 {
94     (*com_err_hook) (whoami, code, fmt, args);
95 }
96
97 #ifndef VARARGS
98 void INTERFACE_C com_err (const char *whoami,
99               long code,
100               const char *fmt, ...)
101 {
102 #else
103 void INTERFACE_C com_err (va_alist)
104     va_dcl
105 {
106     const char *whoami, *fmt;
107     long code;
108 #endif
109     va_list pvar;
110
111     if (!com_err_hook)
112         com_err_hook = default_com_err_proc;
113 #ifdef VARARGS
114     va_start (pvar);
115     whoami = va_arg (pvar, const char *);
116     code = va_arg (pvar, long);
117     fmt = va_arg (pvar, const char *);
118 #else
119     va_start(pvar, fmt);
120 #endif
121     com_err_va (whoami, code, fmt, pvar);
122     va_end(pvar);
123 }
124
125 errf set_com_err_hook (new_proc)
126     errf new_proc;
127 {
128     errf x = com_err_hook;
129
130     if (new_proc)
131         com_err_hook = new_proc;
132     else
133         com_err_hook = default_com_err_proc;
134
135     return x;
136 }
137
138 errf reset_com_err_hook () {
139     errf x = com_err_hook;
140     com_err_hook = default_com_err_proc;
141     return x;
142 }