Add GNU LGPL headers to all .c .C and .h files
[oweals/cde.git] / cde / programs / dtprintinfo / libUI / MotifUI / Debug.c
1 /*
2  * CDE - Common Desktop Environment
3  *
4  * Copyright (c) 1993-2012, The Open Group. All rights reserved.
5  *
6  * These libraries and programs are free software; you can
7  * redistribute them and/or modify them under the terms of the GNU
8  * Lesser General Public License as published by the Free Software
9  * Foundation; either version 2 of the License, or (at your option)
10  * any later version.
11  *
12  * These libraries and programs are distributed in the hope that
13  * they will be useful, but WITHOUT ANY WARRANTY; without even the
14  * implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
15  * PURPOSE. See the GNU Lesser General Public License for more
16  * details.
17  *
18  * You should have received a copy of the GNU Lesser General Public
19  * License along with these librararies and programs; if not, write
20  * to the Free Software Foundation, Inc., 51 Franklin Street, Fifth
21  * Floor, Boston, MA 02110-1301 USA
22  */
23 /* $TOG: Debug.c /main/5 1998/04/06 13:32:19 mgreess $ */
24 #include <stdlib.h>
25 #include <stdio.h>
26 #include <signal.h>
27 #include <errno.h>
28 #include <X11/Intrinsic.h>
29 #include <X11/Xlibint.h>
30
31 #define INT_MESSAGE3    "XIO:  fatal IO error %d (%s) on X server \"%s\"\r\n"
32 #define INT_MESSAGE4    "      after %lu requests (%lu known processed) with %d events remaining.\r\n"
33 #define INT_MESSAGE5    "    The connection was probably broken by a server shudown or KillClient.\r\n"
34
35 static int PrintXError(Display *dpy, XErrorEvent *event, FILE *fp);
36 static  char *SysErrorMsg(int n);
37
38 /* Error Handlers */
39 static  int XIOError(Display *dpy);
40 static  int XError(Display *dpy, XErrorEvent *event);
41 static  void _XtError(String);
42
43 static Boolean G_DumpCore;
44
45 void
46 Debug(
47     XtAppContext app_context
48     )
49 {
50     XSetErrorHandler(XError);
51     XSetIOErrorHandler(XIOError);
52     XtAppSetErrorHandler(app_context, _XtError);
53     G_DumpCore = True;
54 }
55
56 /*
57  *   NAME:     XError
58  *   FUNCTION:
59  *   RETURNS:
60  */
61 static int
62 XError(
63    Display *dpy,
64    XErrorEvent *event
65    )
66 {
67     if (PrintXError(dpy, event, stderr) == 0)
68         return 0;
69     if (G_DumpCore)
70         kill(getpid(), SIGQUIT);
71     else
72         exit(1);
73 }
74
75 /*
76  *   NAME:     XIOError
77  *   FUNCTION:
78  *   RETURNS:
79  */
80 static int
81 XIOError(
82    Display *dpy
83    )
84 {
85     fprintf(stderr, INT_MESSAGE3,
86         errno, SysErrorMsg (errno), DisplayString (dpy));
87     fprintf(stderr, INT_MESSAGE4,
88         NextRequest(dpy) - 1, LastKnownRequestProcessed(dpy), QLength(dpy));
89
90     if (errno == EPIPE)
91         fprintf(stderr, INT_MESSAGE5);
92     if (G_DumpCore)
93         kill(getpid(), SIGQUIT);
94     else
95         exit(1);
96 }
97
98 /*
99  *   NAME:     SysErrorMsg
100  *   FUNCTION:
101  *   RETURNS:
102  */
103 static char *
104 SysErrorMsg(
105    int n
106    )
107 {
108 #if !defined(linux)
109     extern char *sys_errlist[];
110     extern int sys_nerr;
111 #endif
112     char *s = ((n >= 0 && n < sys_nerr) ? sys_errlist[n] : "unknown error");
113     return (s ? s : "no such error");
114 }
115
116 /*
117  *   NAME:     PrintXError
118  *   FUNCTION:
119  *   RETURNS:
120  */
121 static int
122 PrintXError(
123    Display *dpy,
124    XErrorEvent *event,
125    FILE *fp
126    )
127 {
128     char buffer[BUFSIZ];
129     char mesg[BUFSIZ];
130     char number[32];
131     char *mtype = "XlibMessage";
132     register _XExtension *ext = (_XExtension *)NULL;
133     _XExtension *bext = (_XExtension *)NULL;
134     XGetErrorText(dpy, event->error_code, buffer, BUFSIZ);
135     XGetErrorDatabaseText(dpy, mtype, "XError", "X Error", mesg, BUFSIZ);
136     fprintf(fp, "%s:  %s\n  ", mesg, buffer);
137     XGetErrorDatabaseText(dpy, mtype, "MajorCode", "Request Major code %d",
138         mesg, BUFSIZ);
139     fprintf(fp, mesg, event->request_code);
140     if (event->request_code < 128)
141       {
142         sprintf(number, "%d", event->request_code);
143         XGetErrorDatabaseText(dpy, "XRequest", number, "", buffer, BUFSIZ);
144       }
145     else
146       {
147         for (ext = dpy->ext_procs;
148             ext && (ext->codes.major_opcode != event->request_code);
149             ext = ext->next)
150             ;
151         if (ext)
152             strcpy(buffer, ext->name);
153         else
154             buffer[0] = '\0';
155       }
156     fprintf(fp, " (%s)\n", buffer);
157     if (event->request_code >= 128)
158       {
159         XGetErrorDatabaseText(dpy, mtype, "MinorCode", "Request Minor code %d",
160             mesg, BUFSIZ);
161         fputs("  ", fp);
162         fprintf(fp, mesg, event->minor_code);
163         if (ext)
164           {
165             sprintf(mesg, "%s.%d", ext->name, event->minor_code);
166             XGetErrorDatabaseText(dpy, "XRequest", mesg, "", buffer, BUFSIZ);
167             fprintf(fp, " (%s)", buffer);
168           }
169         fputs("\n", fp);
170       }
171     if (event->error_code >= 128)
172       {
173         /* kludge, try to find the extension that caused it */
174         buffer[0] = '\0';
175         for (ext = dpy->ext_procs; ext; ext = ext->next)
176           {
177             if (ext->error_string)
178                 (*ext->error_string)(dpy, event->error_code, &ext->codes,
179                     buffer, BUFSIZ);
180             if (buffer[0])
181               {
182                 bext = ext;
183                 break;
184               }
185             if (ext->codes.first_error &&
186                 ext->codes.first_error < (int)event->error_code &&
187                 (!bext || ext->codes.first_error > bext->codes.first_error))
188                 bext = ext;
189           }
190         if (bext)
191             sprintf(buffer, "%s.%d", bext->name,
192                 event->error_code - bext->codes.first_error);
193         else
194             strcpy(buffer, "Value");
195         XGetErrorDatabaseText(dpy, mtype, buffer, "", mesg, BUFSIZ);
196         if (mesg[0])
197           {
198             fputs("  ", fp);
199             fprintf(fp, mesg, event->resourceid);
200             fputs("\n", fp);
201           }
202         /* let extensions try to print the values */
203         for (ext = dpy->ext_procs; ext; ext = ext->next)
204           {
205             if (ext->error_values)
206                 (*ext->error_values)(dpy, event, fp);
207           }
208       }
209     else if ((event->error_code == BadWindow) ||
210         (event->error_code == BadPixmap) ||
211         (event->error_code == BadCursor) ||
212         (event->error_code == BadFont) ||
213         (event->error_code == BadDrawable) ||
214         (event->error_code == BadColor) ||
215         (event->error_code == BadGC) ||
216         (event->error_code == BadIDChoice) ||
217         (event->error_code == BadValue) ||
218         (event->error_code == BadAtom))
219       {
220         if (event->error_code == BadValue)
221             XGetErrorDatabaseText(dpy, mtype, "Value", "Value 0x%lx",
222                 mesg, BUFSIZ);
223         else if (event->error_code == BadAtom)
224             XGetErrorDatabaseText(dpy, mtype, "AtomID", "AtomID 0x%lx",
225                 mesg, BUFSIZ);
226         else
227             XGetErrorDatabaseText(dpy, mtype, "ResourceID", "ResourceID 0x%lx",
228                 mesg, BUFSIZ);
229         fputs("  ", fp);
230         fprintf(fp, mesg, event->resourceid);
231         fputs("\n", fp);
232       }
233     XGetErrorDatabaseText(dpy, mtype, "ErrorSerial", "Error Serial #%ld",
234         mesg, BUFSIZ);
235     fputs("  ", fp);
236     fprintf(fp, mesg, event->serial);
237     XGetErrorDatabaseText(dpy, mtype, "CurrentSerial", "Current Serial #%ld",
238         mesg, BUFSIZ);
239     fputs("\n  ", fp);
240     fprintf(fp, mesg, dpy->request);
241     fputs("\n", fp);
242     if (event->error_code == BadImplementation)
243         return 0;
244     return 1;
245 }
246
247 /*
248  *   NAME:     XtError
249  *   FUNCTION:
250  *   RETURNS:
251  */
252 static void
253 _XtError(
254     String string
255     )
256 {
257     (void)fprintf(stderr, "XtToolkit Error: %s\n", string);
258     if (G_DumpCore)
259         kill(getpid(), SIGQUIT);
260     else
261         exit(1);
262 }