Imported Upstream version 1.1.1
[platform/upstream/libXmu.git] / src / DefErrMsg.c
1 /*
2
3 Copyright 1988, 1998  The Open Group
4
5 Permission to use, copy, modify, distribute, and sell this software and its
6 documentation for any purpose is hereby granted without fee, provided that
7 the above copyright notice appear in all copies and that both that
8 copyright notice and this permission notice appear in supporting
9 documentation.
10
11 The above copyright notice and this permission notice shall be included in
12 all copies or substantial portions of the Software.
13
14 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
15 IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
16 FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL THE
17 OPEN GROUP BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN
18 AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
19 CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
20
21 Except as contained in this notice, the name of The Open Group shall not be
22 used in advertising or otherwise to promote the sale, use or other dealings
23 in 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 <stdio.h>
31 #include <X11/Xlibint.h>
32 #include <X11/Xproto.h>
33 #include <X11/Xmu/Error.h>
34 #include <X11/Xmu/SysUtil.h>
35
36 /*
37  * XmuPrintDefaultErrorMessage - print a nice error that looks like the usual
38  * message.  Returns 1 if the caller should consider exitting else 0.
39  */
40 int
41 XmuPrintDefaultErrorMessage(Display *dpy, XErrorEvent *event, FILE *fp)
42 {
43     char buffer[BUFSIZ];
44     char mesg[BUFSIZ];
45     char number[32];
46     _Xconst char *mtype = "XlibMessage";
47     register _XExtension *ext = (_XExtension *)NULL;
48     _XExtension *bext = (_XExtension *)NULL;
49     XGetErrorText(dpy, event->error_code, buffer, BUFSIZ);
50     XGetErrorDatabaseText(dpy, mtype, "XError", "X Error", mesg, BUFSIZ);
51     (void) fprintf(fp, "%s:  %s\n  ", mesg, buffer);
52     XGetErrorDatabaseText(dpy, mtype, "MajorCode", "Request Major code %d",
53         mesg, BUFSIZ);
54     (void) fprintf(fp, mesg, event->request_code);
55     if (event->request_code < 128) {
56         XmuSnprintf(number, sizeof(number), "%d", event->request_code);
57         XGetErrorDatabaseText(dpy, "XRequest", number, "", buffer, BUFSIZ);
58     } else {
59         /* XXX this is non-portable */
60         for (ext = dpy->ext_procs;
61              ext && (ext->codes.major_opcode != event->request_code);
62              ext = ext->next)
63           ;
64         if (ext)
65           XmuSnprintf(buffer, sizeof(buffer), "%s", ext->name);
66         else
67             buffer[0] = '\0';
68     }
69     (void) fprintf(fp, " (%s)", buffer);
70     fputs("\n  ", fp);
71     if (event->request_code >= 128) {
72         XGetErrorDatabaseText(dpy, mtype, "MinorCode", "Request Minor code %d",
73                               mesg, BUFSIZ);
74         (void) fprintf(fp, mesg, event->minor_code);
75         if (ext) {
76             XmuSnprintf(mesg, sizeof(mesg),
77                         "%s.%d", ext->name, event->minor_code);
78             XGetErrorDatabaseText(dpy, "XRequest", mesg, "", buffer, BUFSIZ);
79             (void) fprintf(fp, " (%s)", buffer);
80         }
81         fputs("\n  ", fp);
82     }
83     if (event->error_code >= 128) {
84         /* kludge, try to find the extension that caused it */
85         buffer[0] = '\0';
86         for (ext = dpy->ext_procs; ext; ext = ext->next) {
87             if (ext->error_string)
88                 (*ext->error_string)(dpy, event->error_code, &ext->codes,
89                                      buffer, BUFSIZ);
90             if (buffer[0]) {
91                 bext = ext;
92                 break;
93             }
94             if (ext->codes.first_error &&
95                 ext->codes.first_error < event->error_code &&
96                 (!bext || ext->codes.first_error > bext->codes.first_error))
97                 bext = ext;
98         }
99         if (bext)
100             XmuSnprintf(buffer, sizeof(buffer), "%s.%d", bext->name,
101                         event->error_code - bext->codes.first_error);
102         else
103             strcpy(buffer, "Value");
104         XGetErrorDatabaseText(dpy, mtype, buffer, "", mesg, BUFSIZ);
105         if (mesg[0]) {
106             fputs("  ", fp);
107             (void) fprintf(fp, mesg, event->resourceid);
108             fputs("\n", fp);
109         }
110         /* let extensions try to print the values */
111         for (ext = dpy->ext_procs; ext; ext = ext->next) {
112             if (ext->error_values)
113                 (*ext->error_values)(dpy, event, fp);
114         }
115     } else if ((event->error_code == BadWindow) ||
116                (event->error_code == BadPixmap) ||
117                (event->error_code == BadCursor) ||
118                (event->error_code == BadFont) ||
119                (event->error_code == BadDrawable) ||
120                (event->error_code == BadColor) ||
121                (event->error_code == BadGC) ||
122                (event->error_code == BadIDChoice) ||
123                (event->error_code == BadValue) ||
124                (event->error_code == BadAtom)) {
125         if (event->error_code == BadValue)
126             XGetErrorDatabaseText(dpy, mtype, "Value", "Value 0x%x",
127                                   mesg, BUFSIZ);
128         else if (event->error_code == BadAtom)
129             XGetErrorDatabaseText(dpy, mtype, "AtomID", "AtomID 0x%x",
130                                   mesg, BUFSIZ);
131         else
132             XGetErrorDatabaseText(dpy, mtype, "ResourceID", "ResourceID 0x%x",
133                                   mesg, BUFSIZ);
134         (void) fprintf(fp, mesg, event->resourceid);
135         fputs("\n  ", fp);
136     }
137     XGetErrorDatabaseText(dpy, mtype, "ErrorSerial", "Error Serial #%d",
138         mesg, BUFSIZ);
139     (void) fprintf(fp, mesg, event->serial);
140     fputs("\n  ", fp);
141     XGetErrorDatabaseText(dpy, mtype, "CurrentSerial", "Current Serial #%d",
142         mesg, BUFSIZ);
143     (void) fprintf(fp, mesg, NextRequest(dpy)-1);
144     fputs("\n", fp);
145     if (event->error_code == BadImplementation) return 0;
146     return 1;
147 }
148
149
150 /*
151  * XmuSimpleErrorHandler - ignore errors for XQueryTree, XGetWindowAttributes,
152  * and XGetGeometry; print a message for everything else.  In all case, do
153  * not exit.
154  */
155 int
156 XmuSimpleErrorHandler(Display *dpy, XErrorEvent *errorp)
157 {
158     switch (errorp->request_code) {
159       case X_QueryTree:
160       case X_GetWindowAttributes:
161         if (errorp->error_code == BadWindow) return 0;
162         break;
163       case X_GetGeometry:
164         if (errorp->error_code == BadDrawable) return 0;
165         break;
166     }
167     /* got a "real" X error */
168     return XmuPrintDefaultErrorMessage (dpy, errorp, stderr);
169 }