ErrDes.c revision 61b2299d
1/*
2 * $Xorg: ErrDes.c,v 1.4 2001/02/09 02:03:32 xorgcvs Exp $
3 * $XdotOrg: lib/X11/src/ErrDes.c,v 1.8 2005-08-26 05:16:46 daniels Exp $
4 */
5
6/***********************************************************
7
8Copyright 1987, 1988, 1998  The Open Group
9
10Permission to use, copy, modify, distribute, and sell this software and its
11documentation for any purpose is hereby granted without fee, provided that
12the above copyright notice appear in all copies and that both that
13copyright notice and this permission notice appear in supporting
14documentation.
15
16The above copyright notice and this permission notice shall be included in
17all copies or substantial portions of the Software.
18
19THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
20IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
21FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL THE
22OPEN GROUP BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN
23AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
24CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
25
26Except as contained in this notice, the name of The Open Group shall not be
27used in advertising or otherwise to promote the sale, use or other dealings
28in this Software without prior written authorization from The Open Group.
29
30
31Copyright 1987, 1988 by Digital Equipment Corporation, Maynard, Massachusetts.
32
33                        All Rights Reserved
34
35Permission to use, copy, modify, and distribute this software and its
36documentation for any purpose and without fee is hereby granted,
37provided that the above copyright notice appear in all copies and that
38both that copyright notice and this permission notice appear in
39supporting documentation, and that the name of Digital not be
40used in advertising or publicity pertaining to distribution of the
41software without specific, written prior permission.
42
43DIGITAL DISCLAIMS ALL WARRANTIES WITH REGARD TO THIS SOFTWARE, INCLUDING
44ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS, IN NO EVENT SHALL
45DIGITAL BE LIABLE FOR ANY SPECIAL, INDIRECT OR CONSEQUENTIAL DAMAGES OR
46ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS,
47WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION,
48ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS
49SOFTWARE.
50
51******************************************************************/
52/* $XFree86: xc/lib/X11/ErrDes.c,v 3.11 2003/08/06 14:03:59 eich Exp $ */
53
54#ifdef HAVE_CONFIG_H
55#include <config.h>
56#endif
57#include "Xlibint.h"
58#include <X11/Xos.h>
59#include "Xresource.h"
60#include <stdio.h>
61
62#ifndef ERRORDB
63#ifndef XERRORDB
64#define ERRORDB "/usr/lib/X11/XErrorDB"
65#else
66#define ERRORDB XERRORDB
67#endif
68#endif
69
70/*
71 * descriptions of errors in Section 4 of Protocol doc (pp. 350-351); more
72 * verbose descriptions are given in the error database
73 */
74static const char _XErrorList[] =
75    /* No error */          "no error\0"
76    /* BadRequest */        "BadRequest\0"
77    /* BadValue */          "BadValue\0"
78    /* BadWindow */         "BadWindow\0"
79    /* BadPixmap */         "BadPixmap\0"
80    /* BadAtom */           "BadAtom\0"
81    /* BadCursor */         "BadCursor\0"
82    /* BadFont */           "BadFont\0"
83    /* BadMatch */          "BadMatch\0"
84    /* BadDrawable */       "BadDrawable\0"
85    /* BadAccess */         "BadAccess\0"
86    /* BadAlloc */          "BadAlloc\0"
87    /* BadColor */          "BadColor\0"
88    /* BadGC */             "BadGC\0"
89    /* BadIDChoice */       "BadIDChoice\0"
90    /* BadName */           "BadName\0"
91    /* BadLength */         "BadLength\0"
92    /* BadImplementation */ "BadImplementation"
93;
94
95/* offsets into _XErrorList */
96static const unsigned char _XErrorOffsets[] = {
97    0, 9, 20, 29, 39, 49, 57, 67, 75, 84, 96,
98    106, 115, 124, 130, 142, 150, 160
99};
100
101
102int
103XGetErrorText(
104    register Display *dpy,
105    register int code,
106    char *buffer,
107    int nbytes)
108{
109    char buf[150];
110    register _XExtension *ext;
111    _XExtension *bext = (_XExtension *)NULL;
112
113    if (nbytes == 0) return 0;
114    if (code <= BadImplementation && code > 0) {
115	sprintf(buf, "%d", code);
116        (void) XGetErrorDatabaseText(dpy, "XProtoError", buf,
117                                     _XErrorList + _XErrorOffsets[code],
118				     buffer, nbytes);
119    } else
120	buffer[0] = '\0';
121    /* call out to any extensions interested */
122    for (ext = dpy->ext_procs; ext; ext = ext->next) {
123 	if (ext->error_string)
124 	    (*ext->error_string)(dpy, code, &ext->codes, buffer, nbytes);
125	if (ext->codes.first_error &&
126	    ext->codes.first_error < code &&
127	    (!bext || ext->codes.first_error > bext->codes.first_error))
128	    bext = ext;
129    }
130    if (!buffer[0] && bext) {
131	sprintf(buf, "%s.%d", bext->name, code - bext->codes.first_error);
132	(void) XGetErrorDatabaseText(dpy, "XProtoError", buf, "", buffer, nbytes);
133    }
134    if (!buffer[0])
135	sprintf(buffer, "%d", code);
136    return 0;
137}
138
139int
140/*ARGSUSED*/
141XGetErrorDatabaseText(
142    Display *dpy,
143    register _Xconst char *name,
144    register _Xconst char *type,
145    _Xconst char *defaultp,
146    char *buffer,
147    int nbytes)
148{
149
150    static XrmDatabase db = NULL;
151    XrmString type_str;
152    XrmValue result;
153    char temp[BUFSIZ];
154    char* tptr;
155    unsigned long tlen;
156
157    if (nbytes == 0) return 0;
158
159    if (!db) {
160	/* the Xrm routines expect to be called with the global
161	   mutex unlocked. */
162	XrmDatabase temp_db;
163	int do_destroy;
164	const char *dbname;
165
166	XrmInitialize();
167#ifdef WIN32
168	dbname = getenv("XERRORDB");
169	if (!dbname)
170	    dbname = ERRORDB;
171#else
172    dbname = ERRORDB;
173#endif
174	temp_db = XrmGetFileDatabase(dbname);
175
176	_XLockMutex(_Xglobal_lock);
177	if (!db) {
178	    db = temp_db;
179	    do_destroy = 0;
180	} else
181	    do_destroy = 1;	/* we didn't need to get it after all */
182	_XUnlockMutex(_Xglobal_lock);
183
184	if (do_destroy)
185	    XrmDestroyDatabase(temp_db);
186    }
187
188    if (db)
189    {
190	tlen = strlen (name) + strlen (type) + 2;
191	if (tlen <= sizeof(temp))
192	    tptr = temp;
193	else
194	    tptr = Xmalloc (tlen);
195	if (tptr) {
196	    sprintf(tptr, "%s.%s", name, type);
197	    XrmGetResource(db, tptr, "ErrorType.ErrorNumber",
198	      &type_str, &result);
199	    if (tptr != temp)
200		Xfree (tptr);
201	} else {
202	    result.addr = (XPointer) NULL;
203	}
204    }
205    else
206	result.addr = (XPointer)NULL;
207    if (!result.addr) {
208	result.addr = (XPointer) defaultp;
209	result.size = strlen(defaultp) + 1;
210    }
211    (void) strncpy (buffer, (char *) result.addr, nbytes);
212    if (result.size > nbytes) buffer[nbytes-1] = '\0';
213    return 0;
214}
215