DefErrMsg.c

Go to the documentation of this file.
00001 /* $Xorg: DefErrMsg.c,v 1.4 2001/02/09 02:03:52 xorgcvs Exp $ */
00002 
00003 /*
00004 
00005 Copyright 1988, 1998  The Open Group
00006 
00007 Permission to use, copy, modify, distribute, and sell this software and its
00008 documentation for any purpose is hereby granted without fee, provided that
00009 the above copyright notice appear in all copies and that both that
00010 copyright notice and this permission notice appear in supporting
00011 documentation.
00012 
00013 The above copyright notice and this permission notice shall be included in
00014 all copies or substantial portions of the Software.
00015 
00016 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
00017 IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
00018 FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL THE
00019 OPEN GROUP BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN
00020 AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
00021 CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
00022 
00023 Except as contained in this notice, the name of The Open Group shall not be
00024 used in advertising or otherwise to promote the sale, use or other dealings
00025 in this Software without prior written authorization from The Open Group.
00026 
00027 */
00028 /* $XFree86: xc/lib/Xmu/DefErrMsg.c,v 1.7 2001/01/17 19:42:54 dawes Exp $ */
00029 
00030 #include <stdio.h>
00031 #define NEED_EVENTS
00032 #include <X11/Xlibint.h>
00033 #include <X11/Xproto.h>
00034 #include <X11/Xmu/Error.h>
00035 #include <X11/Xmu/SysUtil.h>
00036 
00037 /*
00038  * XmuPrintDefaultErrorMessage - print a nice error that looks like the usual 
00039  * message.  Returns 1 if the caller should consider exitting else 0.
00040  */
00041 int
00042 XmuPrintDefaultErrorMessage(Display *dpy, XErrorEvent *event, FILE *fp)
00043 {
00044     char buffer[BUFSIZ];
00045     char mesg[BUFSIZ];
00046     char number[32];
00047     char *mtype = "XlibMessage";
00048     register _XExtension *ext = (_XExtension *)NULL;
00049     _XExtension *bext = (_XExtension *)NULL;
00050     XGetErrorText(dpy, event->error_code, buffer, BUFSIZ);
00051     XGetErrorDatabaseText(dpy, mtype, "XError", "X Error", mesg, BUFSIZ);
00052     (void) fprintf(fp, "%s:  %s\n  ", mesg, buffer);
00053     XGetErrorDatabaseText(dpy, mtype, "MajorCode", "Request Major code %d", 
00054         mesg, BUFSIZ);
00055     (void) fprintf(fp, mesg, event->request_code);
00056     if (event->request_code < 128) {
00057         XmuSnprintf(number, sizeof(number), "%d", event->request_code);
00058         XGetErrorDatabaseText(dpy, "XRequest", number, "", buffer, BUFSIZ);
00059     } else {
00060         /* XXX this is non-portable */
00061         for (ext = dpy->ext_procs;
00062              ext && (ext->codes.major_opcode != event->request_code);
00063              ext = ext->next)
00064           ;
00065         if (ext)
00066           XmuSnprintf(buffer, sizeof(buffer), "%s", ext->name);
00067         else
00068             buffer[0] = '\0';
00069     }
00070     (void) fprintf(fp, " (%s)", buffer);
00071     fputs("\n  ", fp);
00072     if (event->request_code >= 128) {
00073         XGetErrorDatabaseText(dpy, mtype, "MinorCode", "Request Minor code %d",
00074                               mesg, BUFSIZ);
00075         (void) fprintf(fp, mesg, event->minor_code);
00076         if (ext) {
00077             XmuSnprintf(mesg, sizeof(mesg),
00078                         "%s.%d", ext->name, event->minor_code);
00079             XGetErrorDatabaseText(dpy, "XRequest", mesg, "", buffer, BUFSIZ);
00080             (void) fprintf(fp, " (%s)", buffer);
00081         }
00082         fputs("\n  ", fp);
00083     }
00084     if (event->error_code >= 128) {
00085         /* kludge, try to find the extension that caused it */
00086         buffer[0] = '\0';
00087         for (ext = dpy->ext_procs; ext; ext = ext->next) {
00088             if (ext->error_string) 
00089                 (*ext->error_string)(dpy, event->error_code, &ext->codes,
00090                                      buffer, BUFSIZ);
00091             if (buffer[0]) {
00092                 bext = ext;
00093                 break;
00094             }
00095             if (ext->codes.first_error &&
00096                 ext->codes.first_error < event->error_code &&
00097                 (!bext || ext->codes.first_error > bext->codes.first_error))
00098                 bext = ext;
00099         }    
00100         if (bext)
00101             XmuSnprintf(buffer, sizeof(buffer), "%s.%d", bext->name,
00102                         event->error_code - bext->codes.first_error);
00103         else
00104             strcpy(buffer, "Value");
00105         XGetErrorDatabaseText(dpy, mtype, buffer, "", mesg, BUFSIZ);
00106         if (mesg[0]) {
00107             fputs("  ", fp);
00108             (void) fprintf(fp, mesg, event->resourceid);
00109             fputs("\n", fp);
00110         }
00111         /* let extensions try to print the values */
00112         for (ext = dpy->ext_procs; ext; ext = ext->next) {
00113             if (ext->error_values)
00114                 (*ext->error_values)(dpy, event, fp);
00115         }
00116     } else if ((event->error_code == BadWindow) ||
00117                (event->error_code == BadPixmap) ||
00118                (event->error_code == BadCursor) ||
00119                (event->error_code == BadFont) ||
00120                (event->error_code == BadDrawable) ||
00121                (event->error_code == BadColor) ||
00122                (event->error_code == BadGC) ||
00123                (event->error_code == BadIDChoice) ||
00124                (event->error_code == BadValue) ||
00125                (event->error_code == BadAtom)) {
00126         if (event->error_code == BadValue)
00127             XGetErrorDatabaseText(dpy, mtype, "Value", "Value 0x%x",
00128                                   mesg, BUFSIZ);
00129         else if (event->error_code == BadAtom)
00130             XGetErrorDatabaseText(dpy, mtype, "AtomID", "AtomID 0x%x",
00131                                   mesg, BUFSIZ);
00132         else
00133             XGetErrorDatabaseText(dpy, mtype, "ResourceID", "ResourceID 0x%x",
00134                                   mesg, BUFSIZ);
00135         (void) fprintf(fp, mesg, event->resourceid);
00136         fputs("\n  ", fp);
00137     }
00138     XGetErrorDatabaseText(dpy, mtype, "ErrorSerial", "Error Serial #%d", 
00139         mesg, BUFSIZ);
00140     (void) fprintf(fp, mesg, event->serial);
00141     fputs("\n  ", fp);
00142     XGetErrorDatabaseText(dpy, mtype, "CurrentSerial", "Current Serial #%d",
00143         mesg, BUFSIZ);
00144     (void) fprintf(fp, mesg, NextRequest(dpy)-1);
00145     fputs("\n", fp);
00146     if (event->error_code == BadImplementation) return 0;
00147     return 1;
00148 }
00149 
00150 
00151 /*
00152  * XmuSimpleErrorHandler - ignore errors for XQueryTree, XGetWindowAttributes,
00153  * and XGetGeometry; print a message for everything else.  In all case, do
00154  * not exit.
00155  */
00156 int
00157 XmuSimpleErrorHandler(Display *dpy, XErrorEvent *errorp)
00158 {
00159     switch (errorp->request_code) {
00160       case X_QueryTree:
00161       case X_GetWindowAttributes:
00162         if (errorp->error_code == BadWindow) return 0;
00163         break;
00164       case X_GetGeometry:
00165         if (errorp->error_code == BadDrawable) return 0;
00166         break;
00167     }
00168     /* got a "real" X error */
00169     return XmuPrintDefaultErrorMessage (dpy, errorp, stderr);
00170 }       

Generated on Sun Mar 4 15:03:56 2007 for Scilab [trunk] by  doxygen 1.5.1