Error.c revision bdf0f55d
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    char buffer[BIGBUF];
237444c061aSmrg    char* message;
238444c061aSmrg    XtGetErrorDatabaseText(name,type,class,defaultp, buffer, BIGBUF);
239444c061aSmrg/*need better solution here, perhaps use lower level printf primitives? */
240444c061aSmrg    if (params == NULL || num_params == NULL || *num_params == 0)
241444c061aSmrg	(*fn)(buffer);
242444c061aSmrg#ifndef WIN32 /* and OS/2 */
243444c061aSmrg    else if ((getuid () != geteuid ()) || getuid() == 0) {
244444c061aSmrg	if ((error && errorHandler == _XtDefaultError) ||
245444c061aSmrg	    (!error && warningHandler == _XtDefaultWarning)) {
246444c061aSmrg	    /*
247444c061aSmrg	     * if it's just going to go to stderr anyway, then we'll
248444c061aSmrg	     * fprintf to stderr ourselves and skip the insecure sprintf.
249444c061aSmrg	     */
250444c061aSmrg	    Cardinal i = *num_params;
251444c061aSmrg	    String par[10];
252444c061aSmrg	    if (i > 10) i = 10;
253444c061aSmrg	    (void) memmove((char*)par, (char*)params, i * sizeof(String) );
254444c061aSmrg	    bzero( &par[i], (10-i) * sizeof(String) );
255444c061aSmrg	    (void) fprintf (stderr, "%s%s",
256444c061aSmrg			    error ? XTERROR_PREFIX : XTWARNING_PREFIX,
257444c061aSmrg			    error ? "Error: " : "Warning: ");
258444c061aSmrg	    (void) fprintf (stderr, buffer,
259444c061aSmrg			    par[0], par[1], par[2], par[3], par[4],
260444c061aSmrg			    par[5], par[6], par[7], par[8], par[9]);
261444c061aSmrg	    (void) fprintf (stderr, "%c", '\n');
262444c061aSmrg	    if (i != *num_params)
263444c061aSmrg		(*fn) ( "Some arguments in previous message were lost" );
264444c061aSmrg	    else if (error) exit (1);
265444c061aSmrg	} else {
266444c061aSmrg	    /*
267444c061aSmrg	     * can't tell what it might do, so we'll play it safe
268444c061aSmrg	     */
269444c061aSmrg	    XtWarning ("\
270444c061aSmrgThis program is an suid-root program or is being run by the root user.\n\
271444c061aSmrgThe full text of the error or warning message cannot be safely formatted\n\
272444c061aSmrgin this environment. You may get a more descriptive message by running the\n\
273444c061aSmrgprogram as a non-root user or by removing the suid bit on the executable.");
274444c061aSmrg	    (*fn)(buffer); /* if *fn is an ErrorHandler it should exit */
275444c061aSmrg	}
276444c061aSmrg    }
277444c061aSmrg#endif
278444c061aSmrg    else {
279444c061aSmrg	/*
280444c061aSmrg	 * If you have snprintf the worst thing that could happen is you'd
281444c061aSmrg	 * lose some information. Without snprintf you're probably going to
282444c061aSmrg	 * scramble your heap and perhaps SEGV -- sooner or later.
283444c061aSmrg	 * If it hurts when you go like this then don't go like this! :-)
284444c061aSmrg	 */
285444c061aSmrg	Cardinal i = *num_params;
286444c061aSmrg	String par[10];
287444c061aSmrg	if (i > 10) i = 10;
288444c061aSmrg	(void) memmove((char*)par, (char*)params, i * sizeof(String) );
289444c061aSmrg	bzero( &par[i], (10-i) * sizeof(String) );
290444c061aSmrg	if (i != *num_params)
291444c061aSmrg	    XtWarning( "Some arguments in following message were lost" );
292444c061aSmrg	/*
293444c061aSmrg	 * resist any temptation you might have to make `message' a
294444c061aSmrg	 * local buffer on the stack. Doing so is a security hole
295444c061aSmrg	 * in programs executing as root. Error and Warning
296444c061aSmrg	 * messages shouldn't be called frequently enough for this
297444c061aSmrg	 * to be a performance issue.
298444c061aSmrg	 */
299444c061aSmrg	if ((message = __XtMalloc (BIGBUF))) {
300444c061aSmrg	    (void) snprintf (message, BIGBUF, buffer,
301bdf0f55dSmrg			     par[0], par[1], par[2], par[3], par[4],
302bdf0f55dSmrg			     par[5], par[6], par[7], par[8], par[9]);
303444c061aSmrg	    (*fn)(message);
304444c061aSmrg	    XtFree(message);
305444c061aSmrg	} else {
306444c061aSmrg	    XtWarning ("Memory allocation failed, arguments in the following message were lost");
307444c061aSmrg	    (*fn)(buffer);
308444c061aSmrg	}
309444c061aSmrg    }
310444c061aSmrg}
311444c061aSmrg
312444c061aSmrgvoid _XtDefaultErrorMsg (
313444c061aSmrg    String name,
314444c061aSmrg    String type,
315444c061aSmrg    String class,
316444c061aSmrg    String defaultp,
317444c061aSmrg    String* params,
318444c061aSmrg    Cardinal* num_params)
319444c061aSmrg{
320444c061aSmrg    DefaultMsg (name,type,class,defaultp,params,num_params,True,XtError);
321444c061aSmrg}
322444c061aSmrg
323444c061aSmrgvoid _XtDefaultWarningMsg (
324444c061aSmrg    String name,
325444c061aSmrg    String type,
326444c061aSmrg    String class,
327444c061aSmrg    String defaultp,
328444c061aSmrg    String* params,
329444c061aSmrg    Cardinal* num_params)
330444c061aSmrg{
331444c061aSmrg    DefaultMsg (name,type,class,defaultp,params,num_params,False,XtWarning);
332444c061aSmrg}
333444c061aSmrg
334444c061aSmrgvoid XtErrorMsg(
335444c061aSmrg    _Xconst char* name,
336444c061aSmrg    _Xconst char* type,
337444c061aSmrg    _Xconst char* class,
338444c061aSmrg    _Xconst char* defaultp,
339444c061aSmrg    String* params,
340444c061aSmrg    Cardinal* num_params)
341444c061aSmrg{
342444c061aSmrg#if GLOBALERRORS
343444c061aSmrg    LOCK_PROCESS;
344444c061aSmrg    (*errorMsgHandler)((String)name,(String)type,(String)class,
345444c061aSmrg		       (String)defaultp,params,num_params);
346444c061aSmrg    UNLOCK_PROCESS;
347444c061aSmrg#else
348444c061aSmrg    XtAppErrorMsg(_XtDefaultAppContext(),name,type,class,
349444c061aSmrg	    defaultp,params,num_params);
350444c061aSmrg#endif /* GLOBALERRORS */
351444c061aSmrg}
352444c061aSmrg
353444c061aSmrgvoid XtAppErrorMsg(
354444c061aSmrg    XtAppContext app,
355444c061aSmrg    _Xconst char* name,
356444c061aSmrg    _Xconst char* type,
357444c061aSmrg    _Xconst char* class,
358444c061aSmrg    _Xconst char* defaultp,
359444c061aSmrg    String* params,
360444c061aSmrg    Cardinal* num_params)
361444c061aSmrg{
362444c061aSmrg#if GLOBALERRORS
363444c061aSmrg    LOCK_PROCESS;
364444c061aSmrg    (*errorMsgHandler)((String)name,(String)type,(String)class,
365444c061aSmrg		       (String)defaultp,params,num_params);
366444c061aSmrg    UNLOCK_PROCESS;
367444c061aSmrg#else
368444c061aSmrg    LOCK_APP(app);
369444c061aSmrg    (*app->errorMsgHandler)(name,type,class,defaultp,params,num_params);
370444c061aSmrg    UNLOCK_APP(app);
371444c061aSmrg#endif /* GLOBALERRORS */
372444c061aSmrg}
373444c061aSmrg
374444c061aSmrgvoid XtWarningMsg(
375444c061aSmrg    _Xconst char* name,
376444c061aSmrg    _Xconst char* type,
377444c061aSmrg    _Xconst char* class,
378444c061aSmrg    _Xconst char* defaultp,
379444c061aSmrg    String* params,
380444c061aSmrg    Cardinal* num_params)
381444c061aSmrg{
382444c061aSmrg#if GLOBALERRORS
383444c061aSmrg    LOCK_PROCESS;
384444c061aSmrg    (*warningMsgHandler)((String)name,(String)type,(String)class,
385444c061aSmrg			 (String)defaultp,params,num_params);
386444c061aSmrg    UNLOCK_PROCESS;
387444c061aSmrg#else
388444c061aSmrg    XtAppWarningMsg(_XtDefaultAppContext(),name,type,class,
389444c061aSmrg	    defaultp,params,num_params);
390444c061aSmrg#endif /* GLOBALERRORS */
391444c061aSmrg}
392444c061aSmrg
393444c061aSmrgvoid XtAppWarningMsg(
394444c061aSmrg    XtAppContext app,
395444c061aSmrg    _Xconst char* name,
396444c061aSmrg    _Xconst char* type,
397444c061aSmrg    _Xconst char* class,
398444c061aSmrg    _Xconst char* defaultp,
399444c061aSmrg    String* params,
400444c061aSmrg    Cardinal* num_params)
401444c061aSmrg{
402444c061aSmrg#if GLOBALERRORS
403444c061aSmrg    LOCK_PROCESS;
404444c061aSmrg    (*warningMsgHandler)((String)name,(String)type,(String)class,
405444c061aSmrg			 (String)defaultp,params,num_params);
406444c061aSmrg    UNLOCK_PROCESS;
407444c061aSmrg#else
408444c061aSmrg    LOCK_APP(app);
409444c061aSmrg    (*app->warningMsgHandler)(name,type,class,defaultp,params,num_params);
410444c061aSmrg    UNLOCK_APP(app);
411444c061aSmrg#endif /* GLOBALERRORS */
412444c061aSmrg}
413444c061aSmrg
414444c061aSmrgvoid XtSetErrorMsgHandler(
415444c061aSmrg    XtErrorMsgHandler handler)
416444c061aSmrg{
417444c061aSmrg#if GLOBALERRORS
418444c061aSmrg    LOCK_PROCESS;
419444c061aSmrg    if (handler != NULL) errorMsgHandler = handler;
420444c061aSmrg    else errorMsgHandler  = _XtDefaultErrorMsg;
421444c061aSmrg    UNLOCK_PROCESS;
422444c061aSmrg#else
423444c061aSmrg    XtAppSetErrorMsgHandler(_XtDefaultAppContext(), handler);
424444c061aSmrg#endif /* GLOBALERRORS */
425444c061aSmrg}
426444c061aSmrg
427444c061aSmrgXtErrorMsgHandler XtAppSetErrorMsgHandler(
428444c061aSmrg    XtAppContext app,
429444c061aSmrg    XtErrorMsgHandler handler)
430444c061aSmrg{
431444c061aSmrg    XtErrorMsgHandler old;
432444c061aSmrg#if GLOBALERRORS
433444c061aSmrg    LOCK_PROCESS;
434444c061aSmrg    old = errorMsgHandler;
435444c061aSmrg    if (handler != NULL) errorMsgHandler = handler;
436444c061aSmrg    else errorMsgHandler  = _XtDefaultErrorMsg;
437444c061aSmrg    UNLOCK_PROCESS;
438444c061aSmrg#else
439444c061aSmrg    LOCK_APP(app);
440444c061aSmrg    old = app->errorMsgHandler;
441444c061aSmrg    if (handler != NULL) app->errorMsgHandler = handler;
442444c061aSmrg    else app->errorMsgHandler  = _XtDefaultErrorMsg;
443444c061aSmrg    UNLOCK_APP(app);
444444c061aSmrg#endif /* GLOBALERRORS */
445444c061aSmrg    return old;
446444c061aSmrg}
447444c061aSmrg
448444c061aSmrgvoid XtSetWarningMsgHandler(
449444c061aSmrg    XtErrorMsgHandler handler)
450444c061aSmrg{
451444c061aSmrg#if GLOBALERRORS
452444c061aSmrg    LOCK_PROCESS;
453444c061aSmrg    if (handler != NULL) warningMsgHandler  = handler;
454444c061aSmrg    else warningMsgHandler = _XtDefaultWarningMsg;
455444c061aSmrg    UNLOCK_PROCESS;
456444c061aSmrg#else
457444c061aSmrg    XtAppSetWarningMsgHandler(_XtDefaultAppContext(),handler);
458444c061aSmrg#endif /* GLOBALERRORS */
459444c061aSmrg}
460444c061aSmrg
461444c061aSmrgXtErrorMsgHandler XtAppSetWarningMsgHandler(
462444c061aSmrg    XtAppContext app,
463444c061aSmrg    XtErrorMsgHandler handler)
464444c061aSmrg{
465444c061aSmrg    XtErrorMsgHandler old;
466444c061aSmrg#if GLOBALERRORS
467444c061aSmrg    LOCK_PROCESS;
468444c061aSmrg    old = warningMsgHandler;
469444c061aSmrg    if (handler != NULL) warningMsgHandler  = handler;
470444c061aSmrg    else warningMsgHandler = _XtDefaultWarningMsg;
471444c061aSmrg    UNLOCK_PROCESS;
472444c061aSmrg#else
473444c061aSmrg    LOCK_APP(app);
474444c061aSmrg    old = app->warningMsgHandler;
475444c061aSmrg    if (handler != NULL) app->warningMsgHandler  = handler;
476444c061aSmrg    else app->warningMsgHandler = _XtDefaultWarningMsg;
477444c061aSmrg    UNLOCK_APP(app);
478444c061aSmrg#endif /* GLOBALERRORS */
479444c061aSmrg    return old;
480444c061aSmrg}
481444c061aSmrg
482444c061aSmrgvoid _XtDefaultError(String message)
483444c061aSmrg{
484444c061aSmrg    if (message && *message)
485444c061aSmrg	(void)fprintf(stderr, "%sError: %s\n", XTERROR_PREFIX, message);
486444c061aSmrg    exit(1);
487444c061aSmrg}
488444c061aSmrg
489444c061aSmrgvoid _XtDefaultWarning(String message)
490444c061aSmrg{
491444c061aSmrg    if (message && *message)
492444c061aSmrg       (void)fprintf(stderr, "%sWarning: %s\n", XTWARNING_PREFIX, message);
493444c061aSmrg    return;
494444c061aSmrg}
495444c061aSmrg
496444c061aSmrgvoid XtError(
497444c061aSmrg    _Xconst char* message)
498444c061aSmrg{
499444c061aSmrg#if GLOBALERRORS
500444c061aSmrg    LOCK_PROCESS;
501444c061aSmrg    (*errorHandler)((String)message);
502444c061aSmrg    UNLOCK_PROCESS;
503444c061aSmrg#else
504444c061aSmrg    XtAppError(_XtDefaultAppContext(),message);
505444c061aSmrg#endif /* GLOBALERRORS */
506444c061aSmrg}
507444c061aSmrg
508444c061aSmrgvoid XtAppError(
509444c061aSmrg    XtAppContext app,
510444c061aSmrg    _Xconst char* message)
511444c061aSmrg{
512444c061aSmrg#if GLOBALERRORS
513444c061aSmrg    LOCK_PROCESS;
514444c061aSmrg    (*errorHandler)((String)message);
515444c061aSmrg    UNLOCK_PROCESS;
516444c061aSmrg#else
517444c061aSmrg    LOCK_APP(app);
518444c061aSmrg    (*app->errorHandler)(message);
519444c061aSmrg    UNLOCK_APP(app);
520444c061aSmrg#endif /* GLOBALERRORS */
521444c061aSmrg}
522444c061aSmrg
523444c061aSmrgvoid XtWarning(
524444c061aSmrg    _Xconst char* message)
525444c061aSmrg{
526444c061aSmrg#if GLOBALERRORS
527444c061aSmrg    LOCK_PROCESS;
528444c061aSmrg    (*warningHandler)((String)message);
529444c061aSmrg    UNLOCK_PROCESS;
530444c061aSmrg#else
531444c061aSmrg    XtAppWarning(_XtDefaultAppContext(),message);
532444c061aSmrg#endif /* GLOBALERRORS */
533444c061aSmrg}
534444c061aSmrg
535444c061aSmrgvoid XtAppWarning(
536444c061aSmrg    XtAppContext app,
537444c061aSmrg    _Xconst char* message)
538444c061aSmrg{
539444c061aSmrg#if GLOBALERRORS
540444c061aSmrg    LOCK_PROCESS;
541444c061aSmrg    (*warningHandler)((String)message);
542444c061aSmrg    UNLOCK_PROCESS;
543444c061aSmrg#else
544444c061aSmrg    LOCK_APP(app);
545444c061aSmrg    (*app->warningHandler)(message);
546444c061aSmrg    UNLOCK_APP(app);
547444c061aSmrg#endif /* GLOBALERRORS */
548444c061aSmrg}
549444c061aSmrg
550444c061aSmrgvoid XtSetErrorHandler(XtErrorHandler handler)
551444c061aSmrg{
552444c061aSmrg#if GLOBALERRORS
553444c061aSmrg    LOCK_PROCESS;
554444c061aSmrg    if (handler != NULL) errorHandler = handler;
555444c061aSmrg    else errorHandler  = _XtDefaultError;
556444c061aSmrg    UNLOCK_PROCESS;
557444c061aSmrg#else
558444c061aSmrg    XtAppSetErrorHandler(_XtDefaultAppContext(),handler);
559444c061aSmrg#endif /* GLOBALERRORS */
560444c061aSmrg}
561444c061aSmrg
562444c061aSmrgXtErrorHandler XtAppSetErrorHandler(
563444c061aSmrg    XtAppContext app,
564444c061aSmrg    XtErrorHandler handler)
565444c061aSmrg{
566444c061aSmrg    XtErrorHandler old;
567444c061aSmrg#if GLOBALERRORS
568444c061aSmrg    LOCK_PROCESS;
569444c061aSmrg    old = errorHandler;
570444c061aSmrg    if (handler != NULL) errorHandler = handler;
571444c061aSmrg    else errorHandler  = _XtDefaultError;
572444c061aSmrg    UNLOCK_PROCESS;
573444c061aSmrg#else
574444c061aSmrg    LOCK_APP(app);
575444c061aSmrg    old = app->errorHandler;
576444c061aSmrg    if (handler != NULL) app->errorHandler = handler;
577444c061aSmrg    else app->errorHandler  = _XtDefaultError;
578444c061aSmrg    UNLOCK_APP(app);
579444c061aSmrg#endif /* GLOBALERRORS */
580444c061aSmrg    return old;
581444c061aSmrg}
582444c061aSmrg
583444c061aSmrgvoid XtSetWarningHandler(XtErrorHandler handler)
584444c061aSmrg{
585444c061aSmrg#if GLOBALERRORS
586444c061aSmrg    LOCK_PROCESS;
587444c061aSmrg    if (handler != NULL) warningHandler = handler;
588444c061aSmrg    else warningHandler = _XtDefaultWarning;
589444c061aSmrg    UNLOCK_PROCESS;
590444c061aSmrg#else
591444c061aSmrg    XtAppSetWarningHandler(_XtDefaultAppContext(),handler);
592444c061aSmrg#endif /* GLOBALERRORS */
593444c061aSmrg}
594444c061aSmrg
595444c061aSmrgXtErrorHandler XtAppSetWarningHandler(
596444c061aSmrg    XtAppContext app,
597444c061aSmrg    XtErrorHandler handler)
598444c061aSmrg{
599444c061aSmrg    XtErrorHandler old;
600444c061aSmrg#if GLOBALERRORS
601444c061aSmrg    LOCK_PROCESS;
602444c061aSmrg    old = warningHandler;
603444c061aSmrg    if (handler != NULL) warningHandler  = handler;
604444c061aSmrg    else warningHandler = _XtDefaultWarning;
605444c061aSmrg    UNLOCK_PROCESS;
606444c061aSmrg#else
607444c061aSmrg    LOCK_APP(app);
608444c061aSmrg    old = app->warningHandler;
609444c061aSmrg    if (handler != NULL) app->warningHandler  = handler;
610444c061aSmrg    else app->warningHandler = _XtDefaultWarning;
611444c061aSmrg    UNLOCK_APP(app);
612444c061aSmrg#endif /* GLOBALERRORS */
613444c061aSmrg    return old;
614444c061aSmrg}
615444c061aSmrg
616444c061aSmrgvoid _XtSetDefaultErrorHandlers(
617444c061aSmrg    XtErrorMsgHandler *errMsg,
618444c061aSmrg    XtErrorMsgHandler *warnMsg,
619444c061aSmrg    XtErrorHandler *err,
620444c061aSmrg    XtErrorHandler *warn)
621444c061aSmrg{
622444c061aSmrg#ifndef GLOBALERRORS
623444c061aSmrg    LOCK_PROCESS;
624444c061aSmrg    *errMsg = _XtDefaultErrorMsg;
625444c061aSmrg    *warnMsg = _XtDefaultWarningMsg;
626444c061aSmrg    *err = _XtDefaultError;
627444c061aSmrg    *warn = _XtDefaultWarning;
628444c061aSmrg    UNLOCK_PROCESS;
629444c061aSmrg#endif /* GLOBALERRORS */
630444c061aSmrg}
631