upload tizen2.0 source
[framework/uifw/xorg/lib/libice.git] / src / error.c
1 /******************************************************************************
2
3
4 Copyright 1993, 1998  The Open Group
5
6 Permission to use, copy, modify, distribute, and sell this software and its
7 documentation for any purpose is hereby granted without fee, provided that
8 the above copyright notice appear in all copies and that both that
9 copyright notice and this permission notice appear in supporting
10 documentation.
11
12 The above copyright notice and this permission notice shall be included in
13 all copies or substantial portions of the Software.
14
15 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16 IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17 FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL THE
18 OPEN GROUP BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN
19 AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
20 CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
21
22 Except as contained in this notice, the name of The Open Group shall not be
23 used in advertising or otherwise to promote the sale, use or other dealings
24 in this Software without prior written authorization from The Open Group.
25
26 Author: Ralph Mor, X Consortium
27 ******************************************************************************/
28
29 #ifdef HAVE_CONFIG_H
30 #include <config.h>
31 #endif
32 #include <X11/ICE/ICElib.h>
33 #include "ICElibint.h"
34 #include <stdio.h>
35
36 #include <errno.h>
37
38
39 void
40 _IceErrorBadMinor (
41         IceConn iceConn,
42         int     majorOpcode,
43         int     offendingMinor,
44         int     severity
45 )
46 {
47     IceErrorHeader (iceConn,
48         majorOpcode, offendingMinor,
49         iceConn->receive_sequence,
50         severity,
51         IceBadMinor,
52         0);
53
54     IceFlush (iceConn);
55 }
56
57
58 void
59 _IceErrorBadState (
60         IceConn iceConn,
61         int     majorOpcode,
62         int     offendingMinor,
63         int     severity
64 )
65 {
66     IceErrorHeader (iceConn,
67         majorOpcode, offendingMinor,
68         iceConn->receive_sequence,
69         severity,
70         IceBadState,
71         0);
72
73     IceFlush (iceConn);
74 }
75
76
77 void
78 _IceErrorBadLength (
79         IceConn iceConn,
80         int     majorOpcode,
81         int     offendingMinor,
82         int     severity
83 )
84 {
85     IceErrorHeader (iceConn,
86         majorOpcode, offendingMinor,
87         iceConn->receive_sequence,
88         severity,
89         IceBadLength,
90         0);
91
92     IceFlush (iceConn);
93 }
94
95
96 void
97 _IceErrorBadValue (
98         IceConn         iceConn,
99         int             majorOpcode,
100         int             offendingMinor,
101         int             offset,
102         int             length,         /* in bytes */
103         IcePointer      value
104 )
105 {
106     IceErrorHeader (iceConn,
107         majorOpcode, offendingMinor,
108         iceConn->receive_sequence,
109         IceCanContinue,
110         IceBadValue,
111         WORD64COUNT (8 + length));
112
113     IceWriteData32 (iceConn, 4, &offset);
114     IceWriteData32 (iceConn, 4, &length);
115     IceWriteData (iceConn, length, (char *) value);
116
117     if (PAD64 (length))
118         IceWritePad (iceConn, PAD64 (length));
119
120     IceFlush (iceConn);
121 }
122
123
124 void
125 _IceErrorNoAuthentication (
126         IceConn iceConn,
127         int     offendingMinor
128 )
129 {
130     int severity = (offendingMinor == ICE_ConnectionSetup) ?
131         IceFatalToConnection : IceFatalToProtocol;
132
133     IceErrorHeader (iceConn,
134         0, offendingMinor,
135         iceConn->receive_sequence,
136         severity,
137         IceNoAuth,
138         0);
139
140     IceFlush (iceConn);
141 }
142
143
144 void
145 _IceErrorNoVersion (
146         IceConn iceConn,
147         int     offendingMinor
148 )
149 {
150     int severity = (offendingMinor == ICE_ConnectionSetup) ?
151         IceFatalToConnection : IceFatalToProtocol;
152
153     IceErrorHeader (iceConn,
154         0, offendingMinor,
155         iceConn->receive_sequence,
156         severity,
157         IceNoVersion,
158         0);
159
160     IceFlush (iceConn);
161 }
162
163
164 void
165 _IceErrorSetupFailed (
166         IceConn iceConn,
167         int     offendingMinor,
168         const char      *reason
169 )
170 {
171     char *pBuf, *pStart;
172     int bytes;
173     int severity = (offendingMinor == ICE_ConnectionSetup) ?
174         IceFatalToConnection : IceFatalToProtocol;
175
176     if (!reason)
177         reason = "";
178     bytes = STRING_BYTES (reason);
179
180     IceErrorHeader (iceConn,
181         0, offendingMinor,
182         iceConn->receive_sequence,
183         severity,
184         IceSetupFailed,
185         WORD64COUNT (bytes));
186
187     pBuf = pStart = IceAllocScratch (iceConn, PADDED_BYTES64 (bytes));
188     STORE_STRING (pBuf, reason);
189
190     IceWriteData (iceConn, PADDED_BYTES64 (bytes), pStart);
191     IceFlush (iceConn);
192 }
193
194
195 void
196 _IceErrorAuthenticationRejected (
197         IceConn iceConn,
198         int     offendingMinor,
199         const char      *reason
200 )
201 {
202     char *pBuf, *pStart;
203     int bytes;
204
205     if (!reason)
206         reason = "";
207     bytes = STRING_BYTES (reason);
208
209     IceErrorHeader (iceConn,
210         0, offendingMinor,
211         iceConn->receive_sequence,
212         IceFatalToProtocol,
213         IceAuthRejected,
214         WORD64COUNT (bytes));
215
216     pBuf = pStart = IceAllocScratch (iceConn, PADDED_BYTES64 (bytes));
217     STORE_STRING (pBuf, reason);
218
219     IceWriteData (iceConn, PADDED_BYTES64 (bytes), pStart);
220     IceFlush (iceConn);
221 }
222
223
224 void
225 _IceErrorAuthenticationFailed (
226         IceConn iceConn,
227         int     offendingMinor,
228         const char      *reason
229 )
230 {
231     char *pBuf, *pStart;
232     int bytes;
233
234     if (!reason)
235         reason = "";
236     bytes = STRING_BYTES (reason);
237
238     IceErrorHeader (iceConn,
239         0, offendingMinor,
240         iceConn->receive_sequence,
241         IceFatalToProtocol,
242         IceAuthFailed,
243         WORD64COUNT (bytes));
244
245     pBuf = pStart = IceAllocScratch (iceConn, PADDED_BYTES64 (bytes));
246     STORE_STRING (pBuf, reason);
247
248     IceWriteData (iceConn, PADDED_BYTES64 (bytes), pStart);
249     IceFlush (iceConn);
250 }
251
252
253 void
254 _IceErrorProtocolDuplicate (
255         IceConn iceConn,
256         const char      *protocolName
257 )
258 {
259     char *pBuf, *pStart;
260     int bytes;
261
262     if (!protocolName)
263         protocolName = "";
264     bytes = STRING_BYTES (protocolName);
265
266     IceErrorHeader (iceConn,
267         0, ICE_ProtocolSetup,
268         iceConn->receive_sequence,
269         IceFatalToProtocol,
270         IceProtocolDuplicate,
271         WORD64COUNT (bytes));
272
273     pBuf = pStart = IceAllocScratch (iceConn, PADDED_BYTES64 (bytes));
274     STORE_STRING (pBuf, protocolName);
275
276     IceWriteData (iceConn, PADDED_BYTES64 (bytes), pStart);
277     IceFlush (iceConn);
278 }
279
280
281 void
282 _IceErrorMajorOpcodeDuplicate (
283         IceConn iceConn,
284         int     majorOpcode
285 )
286 {
287     char mOp[8] = { (char) majorOpcode };
288
289     IceErrorHeader (iceConn,
290         0, ICE_ProtocolSetup,
291         iceConn->receive_sequence,
292         IceFatalToProtocol,
293         IceMajorOpcodeDuplicate,
294         1 /* length */);
295
296     IceWriteData (iceConn, 8, mOp);
297     IceFlush (iceConn);
298 }
299
300
301 void
302 _IceErrorUnknownProtocol (
303         IceConn iceConn,
304         const char      *protocolName
305 )
306 {
307     char *pBuf, *pStart;
308     int bytes;
309
310     if (!protocolName)
311         protocolName = "";
312     bytes = STRING_BYTES (protocolName);
313
314     IceErrorHeader (iceConn,
315         0, ICE_ProtocolSetup,
316         iceConn->receive_sequence,
317         IceFatalToProtocol,
318         IceUnknownProtocol,
319         WORD64COUNT (bytes));
320
321     pBuf = pStart = IceAllocScratch (iceConn, PADDED_BYTES64 (bytes));
322     STORE_STRING (pBuf, protocolName);
323
324     IceWriteData (iceConn, PADDED_BYTES64 (bytes), pStart);
325     IceFlush (iceConn);
326 }
327
328
329 void
330 _IceErrorBadMajor (
331         IceConn iceConn,
332         int     offendingMajor,
333         int     offendingMinor,
334         int     severity
335 )
336 {
337     char maj[8] = { (char) offendingMajor };
338
339     IceErrorHeader (iceConn,
340         0, offendingMinor,
341         iceConn->receive_sequence,
342         severity,
343         IceBadMajor,
344         1 /* length */);
345
346     IceWriteData (iceConn, 8, maj);
347     IceFlush (iceConn);
348 }
349
350
351 \f
352 /*
353  * Default error handler.
354  */
355
356 static void
357 _IceDefaultErrorHandler (
358         IceConn         iceConn,
359         Bool            swap,
360         int             offendingMinorOpcode,
361         unsigned long   offendingSequence,
362         int             errorClass,
363         int             severity,
364         IcePointer      values
365 )
366 {
367     const char *str;
368     char *estr;
369     char *pData = (char *) values;
370
371     switch (offendingMinorOpcode)
372     {
373         case ICE_ConnectionSetup:
374             str = "ConnectionSetup";
375             break;
376         case ICE_AuthRequired:
377             str = "AuthRequired";
378             break;
379         case ICE_AuthReply:
380             str = "AuthReply";
381             break;
382         case ICE_AuthNextPhase:
383             str = "AuthNextPhase";
384             break;
385         case ICE_ConnectionReply:
386             str = "ConnectionReply";
387             break;
388         case ICE_ProtocolSetup:
389             str = "ProtocolSetup";
390             break;
391         case ICE_ProtocolReply:
392             str = "ProtocolReply";
393             break;
394         case ICE_Ping:
395             str = "Ping";
396             break;
397         case ICE_PingReply:
398             str = "PingReply";
399             break;
400         case ICE_WantToClose:
401             str = "WantToClose";
402             break;
403         case ICE_NoClose:
404             str = "NoClose";
405             break;
406         default:
407             str = "";
408         }
409
410     fprintf (stderr, "\n");
411
412     fprintf (stderr, "ICE error:  Offending minor opcode    = %d (%s)\n",
413         offendingMinorOpcode, str);
414
415     fprintf (stderr, "            Offending sequence number = %lu\n",
416         offendingSequence);
417
418     switch (errorClass)
419     {
420         case IceBadMinor:
421             str = "BadMinor";
422             break;
423         case IceBadState:
424             str = "BadState";
425             break;
426         case IceBadLength:
427             str = "BadLength";
428             break;
429         case IceBadValue:
430             str = "BadValue";
431             break;
432         case IceBadMajor:
433             str = "BadMajor";
434             break;
435         case IceNoAuth:
436             str = "NoAuthentication";
437             break;
438         case IceNoVersion:
439             str = "NoVersion";
440             break;
441         case IceSetupFailed:
442             str = "SetupFailed";
443             break;
444         case IceAuthRejected:
445             str = "AuthenticationRejected";
446             break;
447         case IceAuthFailed:
448             str = "AuthenticationFailed";
449             break;
450         case IceProtocolDuplicate:
451             str = "ProtocolDuplicate";
452             break;
453         case IceMajorOpcodeDuplicate:
454             str = "MajorOpcodeDuplicate";
455             break;
456         case IceUnknownProtocol:
457             str = "UnknownProtocol";
458             break;
459         default:
460             str = "???";
461     }
462
463     fprintf (stderr, "            Error class               = %s\n", str);
464
465     if (severity == IceCanContinue)
466         str = "CanContinue";
467     else if (severity == IceFatalToProtocol)
468         str = "FatalToProtocol";
469     else if (severity == IceFatalToConnection)
470         str = "FatalToConnection";
471     else
472         str = "???";
473
474     fprintf (stderr, "            Severity                  = %s\n", str);
475
476     switch (errorClass)
477     {
478         case IceBadValue:
479         {
480             int offset, length, val;
481
482             EXTRACT_CARD32 (pData, swap, offset);
483             EXTRACT_CARD32 (pData, swap, length);
484
485             fprintf (stderr,
486                 "            BadValue Offset           = %d\n", offset);
487             fprintf (stderr,
488                 "            BadValue Length           = %d\n", length);
489
490             if (length <= 4)
491             {
492                 if (length == 1)
493                     val = (int) *pData;
494                 else if (length == 2)
495                 {
496                     EXTRACT_CARD16 (pData, swap, val);
497                 }
498                 else
499                 {
500                     EXTRACT_CARD32 (pData, swap, val);
501                 }
502
503                 fprintf (stderr,
504                     "            BadValue                  = %d\n", val);
505             }
506             break;
507         }
508
509         case IceBadMajor:
510
511             fprintf (stderr, "Major opcode : %d\n", (int) *pData);
512             break;
513
514         case IceSetupFailed:
515
516             EXTRACT_STRING (pData, swap, estr);
517             fprintf (stderr, "Reason : %s\n", estr);
518             free(estr);
519             break;
520
521         case IceAuthRejected:
522
523             EXTRACT_STRING (pData, swap, estr);
524             fprintf (stderr, "Reason : %s\n", estr);
525             free(estr);
526             break;
527
528         case IceAuthFailed:
529
530             EXTRACT_STRING (pData, swap, estr);
531             fprintf (stderr, "Reason : %s\n", estr);
532             free(estr);
533             break;
534
535         case IceProtocolDuplicate:
536
537             EXTRACT_STRING (pData, swap, estr);
538             fprintf (stderr, "Protocol name : %s\n", estr);
539             free(estr);
540             break;
541
542         case IceMajorOpcodeDuplicate:
543
544             fprintf (stderr, "Major opcode : %d\n", (int) *pData);
545             break;
546
547         case IceUnknownProtocol:
548
549             EXTRACT_STRING (pData, swap, estr);
550             fprintf (stderr, "Protocol name : %s\n", estr);
551             free(estr);
552             break;
553
554         default:
555             break;
556     }
557
558     fprintf (stderr, "\n");
559
560     if (severity != IceCanContinue)
561         exit (1);
562 }
563
564 IceErrorHandler   _IceErrorHandler   = _IceDefaultErrorHandler;
565
566 \f
567 /*
568  * This procedure sets the ICE error handler to be the specified
569  * routine.  If NULL is passed in the default error handler is restored.
570  * The function's return value is the previous error handler.
571  */
572
573 IceErrorHandler
574 IceSetErrorHandler (
575         IceErrorHandler handler
576 )
577 {
578     IceErrorHandler oldHandler = _IceErrorHandler;
579
580     if (handler != NULL)
581         _IceErrorHandler = handler;
582     else
583         _IceErrorHandler = _IceDefaultErrorHandler;
584
585     return (oldHandler);
586 }
587
588
589 \f
590 /*
591  * Default IO error handler.
592  */
593
594 static void
595 _IceDefaultIOErrorHandler (
596         IceConn         iceConn
597 )
598 {
599     fprintf (stderr,
600         "ICE default IO error handler doing an exit(), pid = %ld, errno = %d\n",
601         (long)getpid(), errno);
602
603     exit (1);
604 }
605
606 IceIOErrorHandler _IceIOErrorHandler = _IceDefaultIOErrorHandler;
607
608 \f
609 /*
610  * This procedure sets the ICE fatal I/O error handler to be the
611  * specified routine.  If NULL is passed in the default error
612  * handler is restored.   The function's return value is the
613  * previous error handler.
614  */
615
616 IceIOErrorHandler
617 IceSetIOErrorHandler (
618         IceIOErrorHandler handler
619 )
620 {
621     IceIOErrorHandler oldHandler = _IceIOErrorHandler;
622
623     if (handler != NULL)
624         _IceIOErrorHandler = handler;
625     else
626         _IceIOErrorHandler = _IceDefaultIOErrorHandler;
627
628     return (oldHandler);
629 }