Merge branch 'cde-fixups-1' of ssh://git.code.sf.net/p/cdesktopenv/code into cde...
[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 libraries 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 #include <Dt/UserMsg.h>
49
50 int 
51 _DtPrintDefaultError(
52         Display *dpy,
53         XErrorEvent *event,
54         char *msg )
55 {
56     _DtPrintDefaultErrorSafe(dpy, event, msg, BUFSIZ);
57     /* XXX retval? */
58     
59     /* COV: 87468 nothing actually checks this return value */
60     return(1);
61 }
62
63 #define _DTP_STRNCAT(s1, s2, nb, ec) \
64 { \
65  strncat((s1),(s2),(nb)); \
66  (nb)-=strlen(s2); \
67  if (0>=(nb)) return (ec); \
68 }
69
70 int 
71 _DtPrintDefaultErrorSafe(
72         Display *dpy,
73         XErrorEvent *event,
74         char *msg,
75         int bytes)
76 {
77     char buffer[BUFSIZ];
78     char fpBuf[BUFSIZ];
79     char *fp;
80     char mesg[BUFSIZ];
81     char number[32];
82     char *mtype = "XlibMessage";
83     int nbytes = bytes-1;
84     register _XExtension *ext = (_XExtension *)NULL;
85
86     memset(msg, 0, bytes);
87
88
89     {
90       XGetErrorText(dpy, event->error_code, buffer, BUFSIZ);
91       XGetErrorDatabaseText(dpy, mtype, "XError", "X Error", mesg, BUFSIZ);
92
93       _DTP_STRNCAT(msg, mesg,   nbytes, event->error_code);
94       _DTP_STRNCAT(msg, ":  ",  nbytes, event->error_code);
95       _DTP_STRNCAT(msg, buffer, nbytes, event->error_code);
96       _DTP_STRNCAT(msg, "\n  ", nbytes, event->error_code);
97     }
98
99     {
100       XGetErrorDatabaseText(
101                 dpy, mtype, "MajorCode", "Request Major code %d", mesg, BUFSIZ);
102
103       if (strlen(mesg) < BUFSIZ-10)
104         fp = fpBuf;
105       else
106         fp = malloc(strlen(mesg) + 10);
107
108       (void) sprintf(fp, mesg, event->request_code);
109
110       _DTP_STRNCAT(msg, fp,     nbytes, event->error_code);
111
112       if (fp != fpBuf) free(fp);
113     }
114
115     {
116       if (event->request_code < 128)
117       {
118           sprintf(number, "%d", event->request_code);
119           XGetErrorDatabaseText(dpy, "XRequest", number, "", buffer, BUFSIZ);
120       }
121       else
122       {
123           for (ext = dpy->ext_procs;
124                ext && (ext->codes.major_opcode != event->request_code);
125                ext = ext->next)
126             ;
127           if (ext)
128           {
129               strncpy(buffer, ext->name, BUFSIZ-1);
130               buffer[BUFSIZ-1] = '\0';
131           }
132           else
133             buffer[0] = '\0';
134       }
135
136       _DTP_STRNCAT(msg, " (",    nbytes, event->error_code);
137       _DTP_STRNCAT(msg, buffer,  nbytes, event->error_code);
138       _DTP_STRNCAT(msg, ")\n  ", nbytes, event->error_code);
139     }
140
141     {
142       if (event->request_code >= 128)
143       {
144           XGetErrorDatabaseText(
145                 dpy, mtype, "MinorCode", "Request Minor code %d", mesg, BUFSIZ);
146           if (strlen(mesg) < BUFSIZ-10)
147             fp = fpBuf;
148           else
149             fp = malloc(strlen(mesg) + 10);
150
151           (void) sprintf(fp, mesg, event->minor_code);
152          
153           _DTP_STRNCAT(msg, fp,     nbytes, event->error_code);
154
155           if (fp != fpBuf) free(fp);
156
157           if (ext)
158           {
159               if (strlen(ext->name) < BUFSIZ-10)
160                 fp = fpBuf;
161               else
162                 fp = malloc(strlen(ext->name) + 10);
163
164               sprintf(fp, "%s.%d", ext->name, event->minor_code);
165
166               XGetErrorDatabaseText(dpy, "XRequest", fp, "", buffer, BUFSIZ);
167
168               if (fp != fpBuf) free(fp);
169           }
170
171           _DTP_STRNCAT(msg, "\n  (", nbytes, event->error_code);
172           _DTP_STRNCAT(msg, buffer,  nbytes, event->error_code);
173           _DTP_STRNCAT(msg, ")",     nbytes, event->error_code);
174       }
175     }
176
177     if (event->error_code >= 128)
178     {
179         /* kludge, try to find the extension that caused it */
180         buffer[0] = '\0';
181         for (ext = dpy->ext_procs; ext; ext = ext->next)
182         {
183             if (ext->error_string)
184               (*ext->error_string)(dpy, event->error_code, &ext->codes,
185                                      buffer, BUFSIZ);
186             if (buffer[0])
187                 break;
188         }
189         if (buffer[0])
190         {
191             if (strlen(buffer) < BUFSIZ-10)
192               sprintf(buffer,
193                 "%s.%d",
194                 ext->name,
195                 event->error_code - ext->codes.first_error);
196         }
197         else
198             strcpy(buffer, "Value");
199         XGetErrorDatabaseText(dpy, mtype, buffer, "Value 0x%x", mesg, BUFSIZ);
200         if (*mesg)
201         {
202             if (strlen(mesg) < BUFSIZ-10)
203               fp = fpBuf;
204             else
205               fp = malloc(strlen(mesg) + 10);
206
207             (void) sprintf(fp, mesg, event->resourceid);
208
209             _DTP_STRNCAT(msg, fp,     nbytes, event->error_code);
210             _DTP_STRNCAT(msg, "\n  ", nbytes, event->error_code);
211
212             if (fp != fpBuf) free(fp);
213         }
214     }
215     else if ((event->error_code == BadWindow) ||
216                (event->error_code == BadPixmap) ||
217                (event->error_code == BadCursor) ||
218                (event->error_code == BadFont) ||
219                (event->error_code == BadDrawable) ||
220                (event->error_code == BadColor) ||
221                (event->error_code == BadGC) ||
222                (event->error_code == BadIDChoice) ||
223                (event->error_code == BadValue) ||
224                (event->error_code == BadAtom))
225     {
226         if (event->error_code == BadValue)
227             XGetErrorDatabaseText(dpy, mtype, "Value", "Value 0x%x",
228                                   mesg, BUFSIZ);
229         else if (event->error_code == BadAtom)
230             XGetErrorDatabaseText(dpy, mtype, "AtomID", "AtomID 0x%x",
231                                   mesg, BUFSIZ);
232         else
233             XGetErrorDatabaseText(dpy, mtype, "ResourceID", "ResourceID 0x%x",
234                                   mesg, BUFSIZ);
235
236         if (strlen(mesg) < BUFSIZ-10)
237           fp = fpBuf;
238         else
239           fp = malloc(strlen(mesg) + 10);
240
241         (void) sprintf(fp, mesg, event->resourceid);
242
243         _DTP_STRNCAT(msg, fp,     nbytes, event->error_code);
244         _DTP_STRNCAT(msg, "\n  ", nbytes, event->error_code);
245
246         if (fp != fpBuf) free(fp);
247     }    
248
249     {
250         XGetErrorDatabaseText(
251           dpy, mtype, "ErrorSerial", "Error Serial #%d", mesg, BUFSIZ);
252
253         if (strlen(mesg) < BUFSIZ-10)
254           fp = fpBuf;
255         else
256           fp = malloc(strlen(mesg) + 10);
257
258         (void) sprintf(fp, mesg, event->serial);
259
260         _DTP_STRNCAT(msg, fp,     nbytes, event->error_code);
261         _DTP_STRNCAT(msg, "\n  ", nbytes, event->error_code);
262
263         if (fp != fpBuf) free(fp);
264     }    
265
266     {
267         XGetErrorDatabaseText(
268           dpy, mtype, "CurrentSerial", "Current Serial #%d", mesg, BUFSIZ);
269
270         if (strlen(mesg) < BUFSIZ-10)
271           fp = fpBuf;
272         else
273           fp = malloc(strlen(mesg) + 10);
274
275         (void) sprintf(fp, mesg, dpy->request);
276
277         _DTP_STRNCAT(msg, fp,     nbytes, event->error_code);
278         _DTP_STRNCAT(msg, "\n  ", nbytes, event->error_code);
279
280         if (fp != fpBuf) free(fp);
281     }    
282
283     if (event->error_code == BadImplementation) return 0;
284     return 1;
285 }