Error.c revision 249c3046
1444c061aSmrg/***********************************************************
2249c3046SmrgCopyright (c) 1993, Oracle and/or its affiliates. All rights reserved.
31477040fSmrg
41477040fSmrgPermission is hereby granted, free of charge, to any person obtaining a
51477040fSmrgcopy of this software and associated documentation files (the "Software"),
61477040fSmrgto deal in the Software without restriction, including without limitation
71477040fSmrgthe rights to use, copy, modify, merge, publish, distribute, sublicense,
81477040fSmrgand/or sell copies of the Software, and to permit persons to whom the
91477040fSmrgSoftware is furnished to do so, subject to the following conditions:
101477040fSmrg
111477040fSmrgThe above copyright notice and this permission notice (including the next
121477040fSmrgparagraph) shall be included in all copies or substantial portions of the
131477040fSmrgSoftware.
141477040fSmrg
151477040fSmrgTHE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
161477040fSmrgIMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
171477040fSmrgFITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL
181477040fSmrgTHE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
191477040fSmrgLIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
201477040fSmrgFROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
211477040fSmrgDEALINGS IN THE SOFTWARE.
221477040fSmrg
231477040fSmrgCopyright 1987, 1988 by Digital Equipment Corporation, Maynard, Massachusetts.
24444c061aSmrg
25444c061aSmrg                        All Rights Reserved
26444c061aSmrg
27444c061aSmrgPermission to use, copy, modify, and distribute this software and its
28444c061aSmrgdocumentation for any purpose and without fee is hereby granted,
29444c061aSmrgprovided that the above copyright notice appear in all copies and that
30444c061aSmrgboth that copyright notice and this permission notice appear in
311477040fSmrgsupporting documentation, and that the name of Digital not be
32444c061aSmrgused in advertising or publicity pertaining to distribution of the
33444c061aSmrgsoftware without specific, written prior permission.
34444c061aSmrg
35444c061aSmrgDIGITAL DISCLAIMS ALL WARRANTIES WITH REGARD TO THIS SOFTWARE, INCLUDING
36444c061aSmrgALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS, IN NO EVENT SHALL
37444c061aSmrgDIGITAL BE LIABLE FOR ANY SPECIAL, INDIRECT OR CONSEQUENTIAL DAMAGES OR
38444c061aSmrgANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS,
39444c061aSmrgWHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION,
40444c061aSmrgARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS
41444c061aSmrgSOFTWARE.
42444c061aSmrg
43444c061aSmrg******************************************************************/
44444c061aSmrg
45444c061aSmrg/*
46444c061aSmrg
47444c061aSmrgCopyright 1987, 1988, 1998  The Open Group
48444c061aSmrg
49444c061aSmrgPermission to use, copy, modify, distribute, and sell this software and its
50444c061aSmrgdocumentation for any purpose is hereby granted without fee, provided that
51444c061aSmrgthe above copyright notice appear in all copies and that both that
52444c061aSmrgcopyright notice and this permission notice appear in supporting
53444c061aSmrgdocumentation.
54444c061aSmrg
55444c061aSmrgThe above copyright notice and this permission notice shall be included in
56444c061aSmrgall copies or substantial portions of the Software.
57444c061aSmrg
58444c061aSmrgTHE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
59444c061aSmrgIMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
60444c061aSmrgFITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL THE
61444c061aSmrgOPEN GROUP BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN
62444c061aSmrgAN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
63444c061aSmrgCONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
64444c061aSmrg
65444c061aSmrgExcept as contained in this notice, the name of The Open Group shall not be
66444c061aSmrgused in advertising or otherwise to promote the sale, use or other dealings
67444c061aSmrgin this Software without prior written authorization from The Open Group.
68444c061aSmrg
69444c061aSmrg*/
70444c061aSmrg
71444c061aSmrg#ifdef HAVE_CONFIG_H
72444c061aSmrg#include <config.h>
73444c061aSmrg#endif
74444c061aSmrg#include "IntrinsicI.h"
75444c061aSmrg#include <stdio.h>
76444c061aSmrg#include <stdlib.h>
77444c061aSmrg
78444c061aSmrg/* The error handlers in the application context aren't used since we can't
79444c061aSmrg   come up with a uniform way of using them.  If you can, define
80444c061aSmrg   GLOBALERRORS to be FALSE (or 0). */
81444c061aSmrg
82444c061aSmrg#ifndef GLOBALERRORS
83444c061aSmrg#define GLOBALERRORS 1
84444c061aSmrg#endif
85444c061aSmrg
86444c061aSmrgstatic void InitErrorHandling(XrmDatabase *);
87444c061aSmrg#if GLOBALERRORS
88444c061aSmrgstatic XrmDatabase errorDB = NULL;
89444c061aSmrgstatic Boolean error_inited = FALSE;
90444c061aSmrgvoid _XtDefaultErrorMsg(String, String, String, String, String*, Cardinal*);
91444c061aSmrgvoid _XtDefaultWarningMsg(String, String, String, String, String*, Cardinal*);
92444c061aSmrgvoid _XtDefaultError(String);
93444c061aSmrgvoid _XtDefaultWarning(String);
94444c061aSmrgstatic XtErrorMsgHandler errorMsgHandler = _XtDefaultErrorMsg;
95444c061aSmrgstatic XtErrorMsgHandler warningMsgHandler = _XtDefaultWarningMsg;
96444c061aSmrgstatic XtErrorHandler errorHandler = _XtDefaultError;
97444c061aSmrgstatic XtErrorHandler warningHandler = _XtDefaultWarning;
98444c061aSmrg#endif /* GLOBALERRORS */
99444c061aSmrg
100444c061aSmrgXrmDatabase *XtGetErrorDatabase(void)
101444c061aSmrg{
102444c061aSmrg    XrmDatabase* retval;
103444c061aSmrg#if GLOBALERRORS
104444c061aSmrg    LOCK_PROCESS;
105444c061aSmrg    retval = &errorDB;
106444c061aSmrg    UNLOCK_PROCESS;
107444c061aSmrg#else
108444c061aSmrg    retval = XtAppGetErrorDatabase(_XtDefaultAppContext());
109444c061aSmrg#endif /* GLOBALERRORS */
110444c061aSmrg    return retval;
111444c061aSmrg}
112444c061aSmrg
113444c061aSmrgXrmDatabase *XtAppGetErrorDatabase(
114444c061aSmrg	XtAppContext app)
115444c061aSmrg{
116444c061aSmrg    XrmDatabase* retval;
117444c061aSmrg#if GLOBALERRORS
118444c061aSmrg    LOCK_PROCESS;
119444c061aSmrg    retval = &errorDB;
120444c061aSmrg    UNLOCK_PROCESS;
121444c061aSmrg#else
122444c061aSmrg    LOCK_APP(app);
123444c061aSmrg    retval= &app->errorDB;
124444c061aSmrg    UNLOCK_APP(app);
125444c061aSmrg#endif /* GLOBALERRORS */
126444c061aSmrg    return retval;
127444c061aSmrg}
128444c061aSmrg
129444c061aSmrgvoid XtGetErrorDatabaseText(
130444c061aSmrg    register _Xconst char* name,
131444c061aSmrg    register _Xconst char* type,
132444c061aSmrg    register _Xconst char* class,
133444c061aSmrg    _Xconst char* defaultp,
134444c061aSmrg    String buffer,
135444c061aSmrg    int nbytes)
136444c061aSmrg{
137444c061aSmrg#if GLOBALERRORS
138444c061aSmrg    XtAppGetErrorDatabaseText(NULL,
139444c061aSmrg	    name,type,class,defaultp, buffer, nbytes, NULL);
140444c061aSmrg#else
141444c061aSmrg    XtAppGetErrorDatabaseText(_XtDefaultAppContext(),
142444c061aSmrg	    name,type,class,defaultp, buffer, nbytes, NULL);
143444c061aSmrg#endif /* GLOBALERRORS */
144444c061aSmrg}
145444c061aSmrg
146444c061aSmrgvoid XtAppGetErrorDatabaseText(
147444c061aSmrg    XtAppContext app,
148444c061aSmrg    register _Xconst char* name,
149444c061aSmrg    register _Xconst char* type,
150444c061aSmrg    register _Xconst char* class,
151444c061aSmrg    _Xconst char* defaultp,
152444c061aSmrg    String buffer,
153444c061aSmrg    int nbytes,
154444c061aSmrg    XrmDatabase db)
155444c061aSmrg{
156444c061aSmrg    String str_class;
157444c061aSmrg    String type_str;
158444c061aSmrg    XrmValue result;
159444c061aSmrg    char *str_name = NULL;
160444c061aSmrg    char *temp = NULL;
161444c061aSmrg
162444c061aSmrg#if GLOBALERRORS
163444c061aSmrg    LOCK_PROCESS;
164444c061aSmrg    if (error_inited == FALSE) {
165444c061aSmrg        InitErrorHandling (&errorDB);
166444c061aSmrg        error_inited = TRUE;
167444c061aSmrg    }
168444c061aSmrg#else
169444c061aSmrg    LOCK_APP(app);
170444c061aSmrg    if (app->error_inited == FALSE) {
171444c061aSmrg        InitErrorHandling (&app->errorDB);
172444c061aSmrg        app->error_inited = TRUE;
173444c061aSmrg    }
174444c061aSmrg#endif /* GLOBALERRORS */
175444c061aSmrg    if (!(str_name = ALLOCATE_LOCAL(strlen(name) + strlen(type) + 2)))
176444c061aSmrg	_XtAllocError(NULL);
177444c061aSmrg    (void) sprintf(str_name, "%s.%s", name, type);
178444c061aSmrg    /* XrmGetResource requires the name and class to be fully qualified
179444c061aSmrg     * and to have the same number of components. */
180444c061aSmrg    str_class = (char *)class;
181444c061aSmrg    if (! strchr(class, '.')) {
182444c061aSmrg	if (!(temp = ALLOCATE_LOCAL(2 * strlen(class) + 2)))
183444c061aSmrg	    _XtAllocError(NULL);
184444c061aSmrg	(void) sprintf(temp, "%s.%s", class, class);
185444c061aSmrg	str_class = temp;
186444c061aSmrg    }
187444c061aSmrg    if (db == NULL) {
188444c061aSmrg#if GLOBALERRORS
189444c061aSmrg	(void) XrmGetResource(errorDB, str_name, str_class, &type_str,
190444c061aSmrg			      &result);
191444c061aSmrg#else
192444c061aSmrg	(void) XrmGetResource(app->errorDB, str_name, str_class, &type_str,
193444c061aSmrg			      &result);
194444c061aSmrg#endif /* GLOBALERRORS */
195444c061aSmrg    } else (void) XrmGetResource(db, str_name, str_class, &type_str, &result);
196444c061aSmrg    if (result.addr) {
197444c061aSmrg        (void) strncpy (buffer, result.addr, nbytes);
198444c061aSmrg        if (result.size > (unsigned) nbytes) buffer[nbytes-1] = 0;
199444c061aSmrg    } else {
200444c061aSmrg	int len = strlen(defaultp);
201444c061aSmrg	if (len >= nbytes) len = nbytes-1;
202444c061aSmrg	(void) memmove(buffer, defaultp, len);
203444c061aSmrg	buffer[len] = '\0';
204444c061aSmrg    }
205444c061aSmrg    if (str_name)
206444c061aSmrg	DEALLOCATE_LOCAL(str_name);
207444c061aSmrg    if (temp)
208444c061aSmrg	DEALLOCATE_LOCAL(temp);
209444c061aSmrg#if GLOBALERRORS
210444c061aSmrg    UNLOCK_PROCESS;
211444c061aSmrg#else
212444c061aSmrg    UNLOCK_APP(app);
213444c061aSmrg#endif
214444c061aSmrg}
215444c061aSmrg
216444c061aSmrgstatic void InitErrorHandling (
217444c061aSmrg    XrmDatabase *db)
218444c061aSmrg{
219444c061aSmrg    XrmDatabase errordb;
220444c061aSmrg
221444c061aSmrg    errordb = XrmGetFileDatabase(ERRORDB);
222444c061aSmrg    XrmMergeDatabases(errordb, db);
223444c061aSmrg}
224444c061aSmrg
225444c061aSmrgstatic void DefaultMsg (
226444c061aSmrg    String name,
227444c061aSmrg    String type,
228444c061aSmrg    String class,
229444c061aSmrg    String defaultp,
230444c061aSmrg    String* params,
231444c061aSmrg    Cardinal* num_params,
232444c061aSmrg    Bool error,
233444c061aSmrg    void (*fn)(_Xconst _XtString))
234444c061aSmrg{
235444c061aSmrg#define BIGBUF 1024
236444c061aSmrg#ifdef notyet /* older versions don't, might want to wait until more do */
237444c061aSmrg#if defined(__linux__)  || defined(CSRG_BASED) /* everyone else needs to get with the program */
238444c061aSmrg#define USE_SNPRINTF
239444c061aSmrg#endif
240444c061aSmrg#endif
241444c061aSmrg    char buffer[BIGBUF];
242444c061aSmrg    char* message;
243444c061aSmrg    XtGetErrorDatabaseText(name,type,class,defaultp, buffer, BIGBUF);
244444c061aSmrg/*need better solution here, perhaps use lower level printf primitives? */
245444c061aSmrg    if (params == NULL || num_params == NULL || *num_params == 0)
246444c061aSmrg	(*fn)(buffer);
247444c061aSmrg#ifndef WIN32 /* and OS/2 */
248444c061aSmrg    else if ((getuid () != geteuid ()) || getuid() == 0) {
249444c061aSmrg	if ((error && errorHandler == _XtDefaultError) ||
250444c061aSmrg	    (!error && warningHandler == _XtDefaultWarning)) {
251444c061aSmrg	    /*
252444c061aSmrg	     * if it's just going to go to stderr anyway, then we'll
253444c061aSmrg	     * fprintf to stderr ourselves and skip the insecure sprintf.
254444c061aSmrg	     */
255444c061aSmrg	    Cardinal i = *num_params;
256444c061aSmrg	    String par[10];
257444c061aSmrg	    if (i > 10) i = 10;
258444c061aSmrg	    (void) memmove((char*)par, (char*)params, i * sizeof(String) );
259444c061aSmrg	    bzero( &par[i], (10-i) * sizeof(String) );
260444c061aSmrg	    (void) fprintf (stderr, "%s%s",
261444c061aSmrg			    error ? XTERROR_PREFIX : XTWARNING_PREFIX,
262444c061aSmrg			    error ? "Error: " : "Warning: ");
263444c061aSmrg	    (void) fprintf (stderr, buffer,
264444c061aSmrg			    par[0], par[1], par[2], par[3], par[4],
265444c061aSmrg			    par[5], par[6], par[7], par[8], par[9]);
266444c061aSmrg	    (void) fprintf (stderr, "%c", '\n');
267444c061aSmrg	    if (i != *num_params)
268444c061aSmrg		(*fn) ( "Some arguments in previous message were lost" );
269444c061aSmrg	    else if (error) exit (1);
270444c061aSmrg	} else {
271444c061aSmrg	    /*
272444c061aSmrg	     * can't tell what it might do, so we'll play it safe
273444c061aSmrg	     */
274444c061aSmrg	    XtWarning ("\
275444c061aSmrgThis program is an suid-root program or is being run by the root user.\n\
276444c061aSmrgThe full text of the error or warning message cannot be safely formatted\n\
277444c061aSmrgin this environment. You may get a more descriptive message by running the\n\
278444c061aSmrgprogram as a non-root user or by removing the suid bit on the executable.");
279444c061aSmrg	    (*fn)(buffer); /* if *fn is an ErrorHandler it should exit */
280444c061aSmrg	}
281444c061aSmrg    }
282444c061aSmrg#endif
283444c061aSmrg    else {
284444c061aSmrg	/*
285444c061aSmrg	 * If you have snprintf the worst thing that could happen is you'd
286444c061aSmrg	 * lose some information. Without snprintf you're probably going to
287444c061aSmrg	 * scramble your heap and perhaps SEGV -- sooner or later.
288444c061aSmrg	 * If it hurts when you go like this then don't go like this! :-)
289444c061aSmrg	 */
290444c061aSmrg	Cardinal i = *num_params;
291444c061aSmrg	String par[10];
292444c061aSmrg	if (i > 10) i = 10;
293444c061aSmrg	(void) memmove((char*)par, (char*)params, i * sizeof(String) );
294444c061aSmrg	bzero( &par[i], (10-i) * sizeof(String) );
295444c061aSmrg	if (i != *num_params)
296444c061aSmrg	    XtWarning( "Some arguments in following message were lost" );
297444c061aSmrg	/*
298444c061aSmrg	 * resist any temptation you might have to make `message' a
299444c061aSmrg	 * local buffer on the stack. Doing so is a security hole
300444c061aSmrg	 * in programs executing as root. Error and Warning
301444c061aSmrg	 * messages shouldn't be called frequently enough for this
302444c061aSmrg	 * to be a performance issue.
303444c061aSmrg	 */
304444c061aSmrg	if ((message = __XtMalloc (BIGBUF))) {
305444c061aSmrg#ifndef USE_SNPRINTF
306444c061aSmrg	    message[BIGBUF-1] = 0;
307444c061aSmrg	    (void) sprintf (message, buffer,
308444c061aSmrg#else
309444c061aSmrg	    (void) snprintf (message, BIGBUF, buffer,
310444c061aSmrg#endif
311444c061aSmrg			    par[0], par[1], par[2], par[3], par[4],
312444c061aSmrg			    par[5], par[6], par[7], par[8], par[9]);
313444c061aSmrg#ifndef USE_SNPRINTF
314444c061aSmrg	    if (message[BIGBUF-1] != '\0')
315444c061aSmrg		XtWarning ("Possible heap corruption in Xt{Error,Warning}MsgHandler");
316444c061aSmrg#endif
317444c061aSmrg	    (*fn)(message);
318444c061aSmrg	    XtFree(message);
319444c061aSmrg	} else {
320444c061aSmrg	    XtWarning ("Memory allocation failed, arguments in the following message were lost");
321444c061aSmrg	    (*fn)(buffer);
322444c061aSmrg	}
323444c061aSmrg    }
324444c061aSmrg}
325444c061aSmrg
326444c061aSmrgvoid _XtDefaultErrorMsg (
327444c061aSmrg    String name,
328444c061aSmrg    String type,
329444c061aSmrg    String class,
330444c061aSmrg    String defaultp,
331444c061aSmrg    String* params,
332444c061aSmrg    Cardinal* num_params)
333444c061aSmrg{
334444c061aSmrg    DefaultMsg (name,type,class,defaultp,params,num_params,True,XtError);
335444c061aSmrg}
336444c061aSmrg
337444c061aSmrgvoid _XtDefaultWarningMsg (
338444c061aSmrg    String name,
339444c061aSmrg    String type,
340444c061aSmrg    String class,
341444c061aSmrg    String defaultp,
342444c061aSmrg    String* params,
343444c061aSmrg    Cardinal* num_params)
344444c061aSmrg{
345444c061aSmrg    DefaultMsg (name,type,class,defaultp,params,num_params,False,XtWarning);
346444c061aSmrg}
347444c061aSmrg
348444c061aSmrgvoid XtErrorMsg(
349444c061aSmrg    _Xconst char* name,
350444c061aSmrg    _Xconst char* type,
351444c061aSmrg    _Xconst char* class,
352444c061aSmrg    _Xconst char* defaultp,
353444c061aSmrg    String* params,
354444c061aSmrg    Cardinal* num_params)
355444c061aSmrg{
356444c061aSmrg#if GLOBALERRORS
357444c061aSmrg    LOCK_PROCESS;
358444c061aSmrg    (*errorMsgHandler)((String)name,(String)type,(String)class,
359444c061aSmrg		       (String)defaultp,params,num_params);
360444c061aSmrg    UNLOCK_PROCESS;
361444c061aSmrg#else
362444c061aSmrg    XtAppErrorMsg(_XtDefaultAppContext(),name,type,class,
363444c061aSmrg	    defaultp,params,num_params);
364444c061aSmrg#endif /* GLOBALERRORS */
365444c061aSmrg}
366444c061aSmrg
367444c061aSmrgvoid XtAppErrorMsg(
368444c061aSmrg    XtAppContext app,
369444c061aSmrg    _Xconst char* name,
370444c061aSmrg    _Xconst char* type,
371444c061aSmrg    _Xconst char* class,
372444c061aSmrg    _Xconst char* defaultp,
373444c061aSmrg    String* params,
374444c061aSmrg    Cardinal* num_params)
375444c061aSmrg{
376444c061aSmrg#if GLOBALERRORS
377444c061aSmrg    LOCK_PROCESS;
378444c061aSmrg    (*errorMsgHandler)((String)name,(String)type,(String)class,
379444c061aSmrg		       (String)defaultp,params,num_params);
380444c061aSmrg    UNLOCK_PROCESS;
381444c061aSmrg#else
382444c061aSmrg    LOCK_APP(app);
383444c061aSmrg    (*app->errorMsgHandler)(name,type,class,defaultp,params,num_params);
384444c061aSmrg    UNLOCK_APP(app);
385444c061aSmrg#endif /* GLOBALERRORS */
386444c061aSmrg}
387444c061aSmrg
388444c061aSmrgvoid XtWarningMsg(
389444c061aSmrg    _Xconst char* name,
390444c061aSmrg    _Xconst char* type,
391444c061aSmrg    _Xconst char* class,
392444c061aSmrg    _Xconst char* defaultp,
393444c061aSmrg    String* params,
394444c061aSmrg    Cardinal* num_params)
395444c061aSmrg{
396444c061aSmrg#if GLOBALERRORS
397444c061aSmrg    LOCK_PROCESS;
398444c061aSmrg    (*warningMsgHandler)((String)name,(String)type,(String)class,
399444c061aSmrg			 (String)defaultp,params,num_params);
400444c061aSmrg    UNLOCK_PROCESS;
401444c061aSmrg#else
402444c061aSmrg    XtAppWarningMsg(_XtDefaultAppContext(),name,type,class,
403444c061aSmrg	    defaultp,params,num_params);
404444c061aSmrg#endif /* GLOBALERRORS */
405444c061aSmrg}
406444c061aSmrg
407444c061aSmrgvoid XtAppWarningMsg(
408444c061aSmrg    XtAppContext app,
409444c061aSmrg    _Xconst char* name,
410444c061aSmrg    _Xconst char* type,
411444c061aSmrg    _Xconst char* class,
412444c061aSmrg    _Xconst char* defaultp,
413444c061aSmrg    String* params,
414444c061aSmrg    Cardinal* num_params)
415444c061aSmrg{
416444c061aSmrg#if GLOBALERRORS
417444c061aSmrg    LOCK_PROCESS;
418444c061aSmrg    (*warningMsgHandler)((String)name,(String)type,(String)class,
419444c061aSmrg			 (String)defaultp,params,num_params);
420444c061aSmrg    UNLOCK_PROCESS;
421444c061aSmrg#else
422444c061aSmrg    LOCK_APP(app);
423444c061aSmrg    (*app->warningMsgHandler)(name,type,class,defaultp,params,num_params);
424444c061aSmrg    UNLOCK_APP(app);
425444c061aSmrg#endif /* GLOBALERRORS */
426444c061aSmrg}
427444c061aSmrg
428444c061aSmrgvoid XtSetErrorMsgHandler(
429444c061aSmrg    XtErrorMsgHandler handler)
430444c061aSmrg{
431444c061aSmrg#if GLOBALERRORS
432444c061aSmrg    LOCK_PROCESS;
433444c061aSmrg    if (handler != NULL) errorMsgHandler = handler;
434444c061aSmrg    else errorMsgHandler  = _XtDefaultErrorMsg;
435444c061aSmrg    UNLOCK_PROCESS;
436444c061aSmrg#else
437444c061aSmrg    XtAppSetErrorMsgHandler(_XtDefaultAppContext(), handler);
438444c061aSmrg#endif /* GLOBALERRORS */
439444c061aSmrg}
440444c061aSmrg
441444c061aSmrgXtErrorMsgHandler XtAppSetErrorMsgHandler(
442444c061aSmrg    XtAppContext app,
443444c061aSmrg    XtErrorMsgHandler handler)
444444c061aSmrg{
445444c061aSmrg    XtErrorMsgHandler old;
446444c061aSmrg#if GLOBALERRORS
447444c061aSmrg    LOCK_PROCESS;
448444c061aSmrg    old = errorMsgHandler;
449444c061aSmrg    if (handler != NULL) errorMsgHandler = handler;
450444c061aSmrg    else errorMsgHandler  = _XtDefaultErrorMsg;
451444c061aSmrg    UNLOCK_PROCESS;
452444c061aSmrg#else
453444c061aSmrg    LOCK_APP(app);
454444c061aSmrg    old = app->errorMsgHandler;
455444c061aSmrg    if (handler != NULL) app->errorMsgHandler = handler;
456444c061aSmrg    else app->errorMsgHandler  = _XtDefaultErrorMsg;
457444c061aSmrg    UNLOCK_APP(app);
458444c061aSmrg#endif /* GLOBALERRORS */
459444c061aSmrg    return old;
460444c061aSmrg}
461444c061aSmrg
462444c061aSmrgvoid XtSetWarningMsgHandler(
463444c061aSmrg    XtErrorMsgHandler handler)
464444c061aSmrg{
465444c061aSmrg#if GLOBALERRORS
466444c061aSmrg    LOCK_PROCESS;
467444c061aSmrg    if (handler != NULL) warningMsgHandler  = handler;
468444c061aSmrg    else warningMsgHandler = _XtDefaultWarningMsg;
469444c061aSmrg    UNLOCK_PROCESS;
470444c061aSmrg#else
471444c061aSmrg    XtAppSetWarningMsgHandler(_XtDefaultAppContext(),handler);
472444c061aSmrg#endif /* GLOBALERRORS */
473444c061aSmrg}
474444c061aSmrg
475444c061aSmrgXtErrorMsgHandler XtAppSetWarningMsgHandler(
476444c061aSmrg    XtAppContext app,
477444c061aSmrg    XtErrorMsgHandler handler)
478444c061aSmrg{
479444c061aSmrg    XtErrorMsgHandler old;
480444c061aSmrg#if GLOBALERRORS
481444c061aSmrg    LOCK_PROCESS;
482444c061aSmrg    old = warningMsgHandler;
483444c061aSmrg    if (handler != NULL) warningMsgHandler  = handler;
484444c061aSmrg    else warningMsgHandler = _XtDefaultWarningMsg;
485444c061aSmrg    UNLOCK_PROCESS;
486444c061aSmrg#else
487444c061aSmrg    LOCK_APP(app);
488444c061aSmrg    old = app->warningMsgHandler;
489444c061aSmrg    if (handler != NULL) app->warningMsgHandler  = handler;
490444c061aSmrg    else app->warningMsgHandler = _XtDefaultWarningMsg;
491444c061aSmrg    UNLOCK_APP(app);
492444c061aSmrg#endif /* GLOBALERRORS */
493444c061aSmrg    return old;
494444c061aSmrg}
495444c061aSmrg
496444c061aSmrgvoid _XtDefaultError(String message)
497444c061aSmrg{
498444c061aSmrg    if (message && *message)
499444c061aSmrg	(void)fprintf(stderr, "%sError: %s\n", XTERROR_PREFIX, message);
500444c061aSmrg    exit(1);
501444c061aSmrg}
502444c061aSmrg
503444c061aSmrgvoid _XtDefaultWarning(String message)
504444c061aSmrg{
505444c061aSmrg    if (message && *message)
506444c061aSmrg       (void)fprintf(stderr, "%sWarning: %s\n", XTWARNING_PREFIX, message);
507444c061aSmrg    return;
508444c061aSmrg}
509444c061aSmrg
510444c061aSmrgvoid XtError(
511444c061aSmrg    _Xconst char* message)
512444c061aSmrg{
513444c061aSmrg#if GLOBALERRORS
514444c061aSmrg    LOCK_PROCESS;
515444c061aSmrg    (*errorHandler)((String)message);
516444c061aSmrg    UNLOCK_PROCESS;
517444c061aSmrg#else
518444c061aSmrg    XtAppError(_XtDefaultAppContext(),message);
519444c061aSmrg#endif /* GLOBALERRORS */
520444c061aSmrg}
521444c061aSmrg
522444c061aSmrgvoid XtAppError(
523444c061aSmrg    XtAppContext app,
524444c061aSmrg    _Xconst char* message)
525444c061aSmrg{
526444c061aSmrg#if GLOBALERRORS
527444c061aSmrg    LOCK_PROCESS;
528444c061aSmrg    (*errorHandler)((String)message);
529444c061aSmrg    UNLOCK_PROCESS;
530444c061aSmrg#else
531444c061aSmrg    LOCK_APP(app);
532444c061aSmrg    (*app->errorHandler)(message);
533444c061aSmrg    UNLOCK_APP(app);
534444c061aSmrg#endif /* GLOBALERRORS */
535444c061aSmrg}
536444c061aSmrg
537444c061aSmrgvoid XtWarning(
538444c061aSmrg    _Xconst char* message)
539444c061aSmrg{
540444c061aSmrg#if GLOBALERRORS
541444c061aSmrg    LOCK_PROCESS;
542444c061aSmrg    (*warningHandler)((String)message);
543444c061aSmrg    UNLOCK_PROCESS;
544444c061aSmrg#else
545444c061aSmrg    XtAppWarning(_XtDefaultAppContext(),message);
546444c061aSmrg#endif /* GLOBALERRORS */
547444c061aSmrg}
548444c061aSmrg
549444c061aSmrgvoid XtAppWarning(
550444c061aSmrg    XtAppContext app,
551444c061aSmrg    _Xconst char* message)
552444c061aSmrg{
553444c061aSmrg#if GLOBALERRORS
554444c061aSmrg    LOCK_PROCESS;
555444c061aSmrg    (*warningHandler)((String)message);
556444c061aSmrg    UNLOCK_PROCESS;
557444c061aSmrg#else
558444c061aSmrg    LOCK_APP(app);
559444c061aSmrg    (*app->warningHandler)(message);
560444c061aSmrg    UNLOCK_APP(app);
561444c061aSmrg#endif /* GLOBALERRORS */
562444c061aSmrg}
563444c061aSmrg
564444c061aSmrgvoid XtSetErrorHandler(XtErrorHandler handler)
565444c061aSmrg{
566444c061aSmrg#if GLOBALERRORS
567444c061aSmrg    LOCK_PROCESS;
568444c061aSmrg    if (handler != NULL) errorHandler = handler;
569444c061aSmrg    else errorHandler  = _XtDefaultError;
570444c061aSmrg    UNLOCK_PROCESS;
571444c061aSmrg#else
572444c061aSmrg    XtAppSetErrorHandler(_XtDefaultAppContext(),handler);
573444c061aSmrg#endif /* GLOBALERRORS */
574444c061aSmrg}
575444c061aSmrg
576444c061aSmrgXtErrorHandler XtAppSetErrorHandler(
577444c061aSmrg    XtAppContext app,
578444c061aSmrg    XtErrorHandler handler)
579444c061aSmrg{
580444c061aSmrg    XtErrorHandler old;
581444c061aSmrg#if GLOBALERRORS
582444c061aSmrg    LOCK_PROCESS;
583444c061aSmrg    old = errorHandler;
584444c061aSmrg    if (handler != NULL) errorHandler = handler;
585444c061aSmrg    else errorHandler  = _XtDefaultError;
586444c061aSmrg    UNLOCK_PROCESS;
587444c061aSmrg#else
588444c061aSmrg    LOCK_APP(app);
589444c061aSmrg    old = app->errorHandler;
590444c061aSmrg    if (handler != NULL) app->errorHandler = handler;
591444c061aSmrg    else app->errorHandler  = _XtDefaultError;
592444c061aSmrg    UNLOCK_APP(app);
593444c061aSmrg#endif /* GLOBALERRORS */
594444c061aSmrg    return old;
595444c061aSmrg}
596444c061aSmrg
597444c061aSmrgvoid XtSetWarningHandler(XtErrorHandler handler)
598444c061aSmrg{
599444c061aSmrg#if GLOBALERRORS
600444c061aSmrg    LOCK_PROCESS;
601444c061aSmrg    if (handler != NULL) warningHandler = handler;
602444c061aSmrg    else warningHandler = _XtDefaultWarning;
603444c061aSmrg    UNLOCK_PROCESS;
604444c061aSmrg#else
605444c061aSmrg    XtAppSetWarningHandler(_XtDefaultAppContext(),handler);
606444c061aSmrg#endif /* GLOBALERRORS */
607444c061aSmrg}
608444c061aSmrg
609444c061aSmrgXtErrorHandler XtAppSetWarningHandler(
610444c061aSmrg    XtAppContext app,
611444c061aSmrg    XtErrorHandler handler)
612444c061aSmrg{
613444c061aSmrg    XtErrorHandler old;
614444c061aSmrg#if GLOBALERRORS
615444c061aSmrg    LOCK_PROCESS;
616444c061aSmrg    old = warningHandler;
617444c061aSmrg    if (handler != NULL) warningHandler  = handler;
618444c061aSmrg    else warningHandler = _XtDefaultWarning;
619444c061aSmrg    UNLOCK_PROCESS;
620444c061aSmrg#else
621444c061aSmrg    LOCK_APP(app);
622444c061aSmrg    old = app->warningHandler;
623444c061aSmrg    if (handler != NULL) app->warningHandler  = handler;
624444c061aSmrg    else app->warningHandler = _XtDefaultWarning;
625444c061aSmrg    UNLOCK_APP(app);
626444c061aSmrg#endif /* GLOBALERRORS */
627444c061aSmrg    return old;
628444c061aSmrg}
629444c061aSmrg
630444c061aSmrgvoid _XtSetDefaultErrorHandlers(
631444c061aSmrg    XtErrorMsgHandler *errMsg,
632444c061aSmrg    XtErrorMsgHandler *warnMsg,
633444c061aSmrg    XtErrorHandler *err,
634444c061aSmrg    XtErrorHandler *warn)
635444c061aSmrg{
636444c061aSmrg#ifndef GLOBALERRORS
637444c061aSmrg    LOCK_PROCESS;
638444c061aSmrg    *errMsg = _XtDefaultErrorMsg;
639444c061aSmrg    *warnMsg = _XtDefaultWarningMsg;
640444c061aSmrg    *err = _XtDefaultError;
641444c061aSmrg    *warn = _XtDefaultWarning;
642444c061aSmrg    UNLOCK_PROCESS;
643444c061aSmrg#endif /* GLOBALERRORS */
644444c061aSmrg}
645