Add GNU LGPL headers to all .c .C and .h files
[oweals/cde.git] / cde / lib / DtSvc / DtUtil2 / PrintXErr.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 /*
24  * (c) Copyright 1993, 1994 Hewlett-Packard Company                     *
25  * (c) Copyright 1993, 1994 International Business Machines Corp.       *
26  * (c) Copyright 1993, 1994 Sun Microsystems, Inc.                      *
27  * (c) Copyright 1993, 1994 Novell, Inc.                                *
28  */
29
30 /* Copyright    Massachusetts Institute of Technology    1985, 1986, 1987 */
31
32 /*
33  * $TOG: PrintXErr.c /main/7 1998/04/10 07:46:38 mgreess $
34  */
35
36 /* **  (c) Copyright Hewlett-Packard Company, 1990.*/
37
38 /*+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++*/
39 /*Lifted from xlib code.  How to print a reasonably complete message */
40 /*and NOT exit.*/
41 /*+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++*/
42 /*$TOG: PrintXErr.c /main/7 1998/04/10 07:46:38 mgreess $*/
43
44 #include <stdio.h>
45 #include <X11/Xlib.h>
46 #include <X11/Xlibint.h>
47
48 int 
49 _DtPrintDefaultError(
50         Display *dpy,
51         XErrorEvent *event,
52         char *msg )
53 {
54     _DtPrintDefaultErrorSafe(dpy, event, msg, BUFSIZ);
55 }
56
57 #define _DTP_STRNCAT(s1, s2, nb, ec) \
58 { \
59  strncat((s1),(s2),(nb)); \
60  (nb)-=strlen(s2); \
61  if (0>=(nb)) return (ec); \
62 }
63
64 int 
65 _DtPrintDefaultErrorSafe(
66         Display *dpy,
67         XErrorEvent *event,
68         char *msg,
69         int bytes)
70 {
71     char buffer[BUFSIZ];
72     char fpBuf[BUFSIZ];
73     char *fp;
74     char mesg[BUFSIZ];
75     char number[32];
76     char *mtype = "XlibMessage";
77     int nbytes = bytes-1;
78     register _XExtension *ext = (_XExtension *)NULL;
79
80     memset(msg, 0, bytes);
81
82
83     {
84       XGetErrorText(dpy, event->error_code, buffer, BUFSIZ);
85       XGetErrorDatabaseText(dpy, mtype, "XError", "X Error", mesg, BUFSIZ);
86
87       _DTP_STRNCAT(msg, mesg,   nbytes, event->error_code);
88       _DTP_STRNCAT(msg, ":  ",  nbytes, event->error_code);
89       _DTP_STRNCAT(msg, buffer, nbytes, event->error_code);
90       _DTP_STRNCAT(msg, "\n  ", nbytes, event->error_code);
91     }
92
93     {
94       XGetErrorDatabaseText(
95                 dpy, mtype, "MajorCode", "Request Major code %d", mesg, BUFSIZ);
96
97       if (strlen(mesg) < BUFSIZ-10)
98         fp = fpBuf;
99       else
100         fp = malloc(strlen(mesg) + 10);
101
102       (void) sprintf(fp, mesg, event->request_code);
103
104       _DTP_STRNCAT(msg, fp,     nbytes, event->error_code);
105
106       if (fp != fpBuf) free(fp);
107     }
108
109     {
110       if (event->request_code < 128)
111       {
112           sprintf(number, "%d", event->request_code);
113           XGetErrorDatabaseText(dpy, "XRequest", number, "", buffer, BUFSIZ);
114       }
115       else
116       {
117           for (ext = dpy->ext_procs;
118                ext && (ext->codes.major_opcode != event->request_code);
119                ext = ext->next)
120             ;
121           if (ext)
122           {
123               strncpy(buffer, ext->name, BUFSIZ-1);
124               buffer[BUFSIZ-1] = '\0';
125           }
126           else
127             buffer[0] = '\0';
128       }
129
130       _DTP_STRNCAT(msg, " (",    nbytes, event->error_code);
131       _DTP_STRNCAT(msg, buffer,  nbytes, event->error_code);
132       _DTP_STRNCAT(msg, ")\n  ", nbytes, event->error_code);
133     }
134
135     {
136       if (event->request_code >= 128)
137       {
138           XGetErrorDatabaseText(
139                 dpy, mtype, "MinorCode", "Request Minor code %d", mesg, BUFSIZ);
140           if (strlen(mesg) < BUFSIZ-10)
141             fp = fpBuf;
142           else
143             fp = malloc(strlen(mesg) + 10);
144
145           (void) sprintf(fp, mesg, event->minor_code);
146          
147           _DTP_STRNCAT(msg, fp,     nbytes, event->error_code);
148
149           if (fp != fpBuf) free(fp);
150
151           if (ext)
152           {
153               if (strlen(ext->name) < BUFSIZ-10)
154                 fp = fpBuf;
155               else
156                 fp = malloc(strlen(ext->name) + 10);
157
158               sprintf(fp, "%s.%d", ext->name, event->minor_code);
159
160               XGetErrorDatabaseText(dpy, "XRequest", fp, "", buffer, BUFSIZ);
161
162               if (fp != fpBuf) free(fp);
163           }
164
165           _DTP_STRNCAT(msg, "\n  (", nbytes, event->error_code);
166           _DTP_STRNCAT(msg, buffer,  nbytes, event->error_code);
167           _DTP_STRNCAT(msg, ")",     nbytes, event->error_code);
168       }
169     }
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                 break;
182         }
183         if (buffer[0])
184         {
185             if (strlen(buffer) < BUFSIZ-10)
186               sprintf(buffer,
187                 "%s.%d",
188                 ext->name,
189                 event->error_code - ext->codes.first_error);
190         }
191         else
192             strcpy(buffer, "Value");
193         XGetErrorDatabaseText(dpy, mtype, buffer, "Value 0x%x", mesg, BUFSIZ);
194         if (*mesg)
195         {
196             if (strlen(mesg) < BUFSIZ-10)
197               fp = fpBuf;
198             else
199               fp = malloc(strlen(mesg) + 10);
200
201             (void) sprintf(fp, mesg, event->resourceid);
202
203             _DTP_STRNCAT(msg, fp,     nbytes, event->error_code);
204             _DTP_STRNCAT(msg, "\n  ", nbytes, event->error_code);
205
206             if (fp != fpBuf) free(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%x",
222                                   mesg, BUFSIZ);
223         else if (event->error_code == BadAtom)
224             XGetErrorDatabaseText(dpy, mtype, "AtomID", "AtomID 0x%x",
225                                   mesg, BUFSIZ);
226         else
227             XGetErrorDatabaseText(dpy, mtype, "ResourceID", "ResourceID 0x%x",
228                                   mesg, BUFSIZ);
229
230         if (strlen(mesg) < BUFSIZ-10)
231           fp = fpBuf;
232         else
233           fp = malloc(strlen(mesg) + 10);
234
235         (void) sprintf(fp, mesg, event->resourceid);
236
237         _DTP_STRNCAT(msg, fp,     nbytes, event->error_code);
238         _DTP_STRNCAT(msg, "\n  ", nbytes, event->error_code);
239
240         if (fp != fpBuf) free(fp);
241     }    
242
243     {
244         XGetErrorDatabaseText(
245           dpy, mtype, "ErrorSerial", "Error Serial #%d", mesg, BUFSIZ);
246
247         if (strlen(mesg) < BUFSIZ-10)
248           fp = fpBuf;
249         else
250           fp = malloc(strlen(mesg) + 10);
251
252         (void) sprintf(fp, mesg, event->serial);
253
254         _DTP_STRNCAT(msg, fp,     nbytes, event->error_code);
255         _DTP_STRNCAT(msg, "\n  ", nbytes, event->error_code);
256
257         if (fp != fpBuf) free(fp);
258     }    
259
260     {
261         XGetErrorDatabaseText(
262           dpy, mtype, "CurrentSerial", "Current Serial #%d", mesg, BUFSIZ);
263
264         if (strlen(mesg) < BUFSIZ-10)
265           fp = fpBuf;
266         else
267           fp = malloc(strlen(mesg) + 10);
268
269         (void) sprintf(fp, mesg, dpy->request);
270
271         _DTP_STRNCAT(msg, fp,     nbytes, event->error_code);
272         _DTP_STRNCAT(msg, "\n  ", nbytes, event->error_code);
273
274         if (fp != fpBuf) free(fp);
275     }    
276
277     if (event->error_code == BadImplementation) return 0;
278     return 1;
279 }