1/*
2
3Copyright 1986, 1998  The Open Group
4
5Permission to use, copy, modify, distribute, and sell this software and its
6documentation for any purpose is hereby granted without fee, provided that
7the above copyright notice appear in all copies and that both that
8copyright notice and this permission notice appear in supporting
9documentation.
10
11The above copyright notice and this permission notice shall be included in
12all copies or substantial portions of the Software.
13
14THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
15IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
16FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL THE
17OPEN GROUP BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN
18AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
19CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
20
21Except as contained in this notice, the name of The Open Group shall not be
22used in advertising or otherwise to promote the sale, use or other dealings
23in this Software without prior written authorization from The Open Group.
24
25*/
26
27#ifdef HAVE_CONFIG_H
28#include <config.h>
29#endif
30#include "Xlibint.h"
31
32/*
33 * XErrorHandler - This procedure sets the X non-fatal error handler
34 * (_XErrorFunction) to be the specified routine.  If NULL is passed in
35 * the original error handler is restored.
36 */
37
38XErrorHandler
39XSetErrorHandler(XErrorHandler handler)
40{
41    int (*oldhandler)(Display *dpy, XErrorEvent *event);
42
43    _XLockMutex(_Xglobal_lock);
44    oldhandler = _XErrorFunction;
45
46    if (!oldhandler)
47	oldhandler = _XDefaultError;
48
49    if (handler != NULL) {
50	_XErrorFunction = handler;
51    }
52    else {
53	_XErrorFunction = _XDefaultError;
54    }
55    _XUnlockMutex(_Xglobal_lock);
56
57    return (XErrorHandler) oldhandler;
58}
59
60/*
61 * XIOErrorHandler - This procedure sets the X fatal I/O error handler
62 * (_XIOErrorFunction) to be the specified routine.  If NULL is passed in
63 * the original error handler is restored.
64 */
65
66XIOErrorHandler
67XSetIOErrorHandler(XIOErrorHandler handler)
68{
69    int (*oldhandler)(Display *dpy);
70
71    _XLockMutex(_Xglobal_lock);
72    oldhandler = _XIOErrorFunction;
73
74    if (!oldhandler)
75	oldhandler = _XDefaultIOError;
76
77    if (handler != NULL) {
78	_XIOErrorFunction = handler;
79    }
80    else {
81	_XIOErrorFunction = _XDefaultIOError;
82    }
83    _XUnlockMutex(_Xglobal_lock);
84
85    return (XIOErrorHandler) oldhandler;
86}
87
88/*
89 * XSetIOErrorExitHandler - This procedure sets the X fatal I/O error
90 * exit function to be the specified routine. If NULL is passed in
91 * the original error exit function is restored. The default routine
92 * calls exit(3).
93 */
94void
95XSetIOErrorExitHandler(
96    Display *dpy,
97    XIOErrorExitHandler handler,
98    void *user_data)
99{
100    LockDisplay(dpy);
101
102    if (handler != NULL) {
103	dpy->exit_handler = handler;
104	dpy->exit_handler_data = user_data;
105    }
106    else {
107	dpy->exit_handler = _XDefaultIOErrorExit;
108	dpy->exit_handler_data = NULL;
109    }
110    UnlockDisplay(dpy);
111}
112