home *** CD-ROM | disk | FTP | other *** search
/ InfoMagic Source Code 1993 July / THE_SOURCE_CODE_CD_ROM.iso / X / mit / lib / Xmu / DefErrMsg.c < prev    next >
Encoding:
C/C++ Source or Header  |  1990-12-09  |  5.1 KB  |  153 lines

  1. /*
  2.  * $XConsortium: DefErrMsg.c,v 1.8 90/12/09 18:05:14 rws Exp $
  3.  *
  4.  * Copyright 1988 by the Massachusetts Institute of Technology
  5.  *
  6.  * Permission to use, copy, modify, and distribute this software and its
  7.  * documentation for any purpose and without fee is hereby granted, provided
  8.  * that the above copyright notice appear in all copies and that both that
  9.  * copyright notice and this permission notice appear in supporting
  10.  * documentation, and that the name of M.I.T. not be used in advertising
  11.  * or publicity pertaining to distribution of the software without specific,
  12.  * written prior permission. M.I.T. makes no representations about the
  13.  * suitability of this software for any purpose.  It is provided "as is"
  14.  * without express or implied warranty.
  15.  *
  16.  */
  17.  
  18. #include <stdio.h>
  19. #define NEED_EVENTS
  20. #include <X11/Xlibint.h>
  21. #include <X11/Xproto.h>
  22.  
  23. /*
  24.  * XmuPrintDefaultErrorMessage - print a nice error that looks like the usual 
  25.  * message.  Returns 1 if the caller should consider exitting else 0.
  26.  */
  27. int XmuPrintDefaultErrorMessage (dpy, event, fp)
  28.     Display *dpy;
  29.     XErrorEvent *event;
  30.     FILE *fp;
  31. {
  32.     char buffer[BUFSIZ];
  33.     char mesg[BUFSIZ];
  34.     char number[32];
  35.     char *mtype = "XlibMessage";
  36.     register _XExtension *ext = (_XExtension *)NULL;
  37.     XGetErrorText(dpy, event->error_code, buffer, BUFSIZ);
  38.     XGetErrorDatabaseText(dpy, mtype, "XError", "X Error", mesg, BUFSIZ);
  39.     (void) fprintf(fp, "%s:  %s\n  ", mesg, buffer);
  40.     XGetErrorDatabaseText(dpy, mtype, "MajorCode", "Request Major code %d", 
  41.     mesg, BUFSIZ);
  42.     (void) fprintf(fp, mesg, event->request_code);
  43.     if (event->request_code < 128) {
  44.     sprintf(number, "%d", event->request_code);
  45.     XGetErrorDatabaseText(dpy, "XRequest", number, "", buffer, BUFSIZ);
  46.     } else {
  47.     /* XXX this is non-portable */
  48.     for (ext = dpy->ext_procs;
  49.          ext && (ext->codes.major_opcode != event->request_code);
  50.          ext = ext->next)
  51.       ;
  52.     if (ext)
  53.         strcpy(buffer, ext->name);
  54.     else
  55.         buffer[0] = '\0';
  56.     }
  57.     (void) fprintf(fp, " (%s)", buffer);
  58.     fputs("\n  ", fp);
  59.     if (event->request_code >= 128) {
  60.     XGetErrorDatabaseText(dpy, mtype, "MinorCode", "Request Minor code %d",
  61.                   mesg, BUFSIZ);
  62.     (void) fprintf(fp, mesg, event->minor_code);
  63.     if (ext) {
  64.         sprintf(mesg, "%s.%d", ext->name, event->minor_code);
  65.         XGetErrorDatabaseText(dpy, "XRequest", mesg, "", buffer, BUFSIZ);
  66.         (void) fprintf(fp, " (%s)", buffer);
  67.     }
  68.     fputs("\n  ", fp);
  69.     }
  70.     if (event->error_code >= 128) {
  71.     /* let extensions try to print the values */
  72.     /* XXX this is non-portable code */
  73.     for (ext = dpy->ext_procs; ext; ext = ext->next) {
  74.         if (ext->error_values)
  75.         (*ext->error_values)(dpy, event, fp);
  76.     }
  77.     /* the rest is a fallback, providing a simple default */
  78.     /* kludge, try to find the extension that caused it */
  79.     buffer[0] = '\0';
  80.     for (ext = dpy->ext_procs; ext; ext = ext->next) {
  81.         if (ext->error_string) 
  82.         (*ext->error_string)(dpy, event->error_code, &ext->codes,
  83.                      buffer, BUFSIZ);
  84.         if (buffer[0])
  85.         break;
  86.     }    
  87.     if (buffer[0])
  88.         sprintf(buffer, "%s.%d", ext->name,
  89.             event->error_code - ext->codes.first_error);
  90.     else
  91.         strcpy(buffer, "Value");
  92.     XGetErrorDatabaseText(dpy, mtype, buffer, "", mesg, BUFSIZ);
  93.     if (*mesg) {
  94.         (void) fprintf(fp, mesg, event->resourceid);
  95.         fputs("\n  ", fp);
  96.     }
  97.     } else if ((event->error_code == BadWindow) ||
  98.            (event->error_code == BadPixmap) ||
  99.            (event->error_code == BadCursor) ||
  100.            (event->error_code == BadFont) ||
  101.            (event->error_code == BadDrawable) ||
  102.            (event->error_code == BadColor) ||
  103.            (event->error_code == BadGC) ||
  104.            (event->error_code == BadIDChoice) ||
  105.            (event->error_code == BadValue) ||
  106.            (event->error_code == BadAtom)) {
  107.     if (event->error_code == BadValue)
  108.         XGetErrorDatabaseText(dpy, mtype, "Value", "Value 0x%x",
  109.                   mesg, BUFSIZ);
  110.     else if (event->error_code == BadAtom)
  111.         XGetErrorDatabaseText(dpy, mtype, "AtomID", "AtomID 0x%x",
  112.                   mesg, BUFSIZ);
  113.     else
  114.         XGetErrorDatabaseText(dpy, mtype, "ResourceID", "ResourceID 0x%x",
  115.                   mesg, BUFSIZ);
  116.     (void) fprintf(fp, mesg, event->resourceid);
  117.     fputs("\n  ", fp);
  118.     }
  119.     XGetErrorDatabaseText(dpy, mtype, "ErrorSerial", "Error Serial #%d", 
  120.     mesg, BUFSIZ);
  121.     (void) fprintf(fp, mesg, event->serial);
  122.     fputs("\n  ", fp);
  123.     XGetErrorDatabaseText(dpy, mtype, "CurrentSerial", "Current Serial #%d",
  124.     mesg, BUFSIZ);
  125.     (void) fprintf(fp, mesg, NextRequest(dpy)-1);
  126.     fputs("\n", fp);
  127.     if (event->error_code == BadImplementation) return 0;
  128.     return 1;
  129. }
  130.  
  131.  
  132. /*
  133.  * XmuSimpleErrorHandler - ignore errors for XQueryTree, XGetWindowAttributes,
  134.  * and XGetGeometry; print a message for everything else.  In all case, do
  135.  * not exit.
  136.  */
  137. int XmuSimpleErrorHandler (dpy, errorp)
  138.     Display *dpy;
  139.     XErrorEvent *errorp;
  140. {
  141.     switch (errorp->request_code) {
  142.       case X_QueryTree:
  143.       case X_GetWindowAttributes:
  144.         if (errorp->error_code == BadWindow) return 0;
  145.     break;
  146.       case X_GetGeometry:
  147.     if (errorp->error_code == BadDrawable) return 0;
  148.     break;
  149.     }
  150.     /* got a "real" X error */
  151.     return XmuPrintDefaultErrorMessage (dpy, errorp, stderr);
  152. }    
  153.