11ab64890Smrg/*
21ab64890Smrg */
31ab64890Smrg
41ab64890Smrg/***********************************************************
51ab64890Smrg
61ab64890SmrgCopyright 1987, 1988, 1998  The Open Group
71ab64890Smrg
81ab64890SmrgPermission to use, copy, modify, distribute, and sell this software and its
91ab64890Smrgdocumentation for any purpose is hereby granted without fee, provided that
101ab64890Smrgthe above copyright notice appear in all copies and that both that
111ab64890Smrgcopyright notice and this permission notice appear in supporting
121ab64890Smrgdocumentation.
131ab64890Smrg
141ab64890SmrgThe above copyright notice and this permission notice shall be included in
151ab64890Smrgall copies or substantial portions of the Software.
161ab64890Smrg
171ab64890SmrgTHE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
181ab64890SmrgIMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
191ab64890SmrgFITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL THE
201ab64890SmrgOPEN GROUP BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN
211ab64890SmrgAN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
221ab64890SmrgCONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
231ab64890Smrg
241ab64890SmrgExcept as contained in this notice, the name of The Open Group shall not be
251ab64890Smrgused in advertising or otherwise to promote the sale, use or other dealings
261ab64890Smrgin this Software without prior written authorization from The Open Group.
271ab64890Smrg
281ab64890Smrg
291ab64890SmrgCopyright 1987, 1988 by Digital Equipment Corporation, Maynard, Massachusetts.
301ab64890Smrg
311ab64890Smrg                        All Rights Reserved
321ab64890Smrg
3361b2299dSmrgPermission to use, copy, modify, and distribute this software and its
3461b2299dSmrgdocumentation for any purpose and without fee is hereby granted,
351ab64890Smrgprovided that the above copyright notice appear in all copies and that
3661b2299dSmrgboth that copyright notice and this permission notice appear in
371ab64890Smrgsupporting documentation, and that the name of Digital not be
381ab64890Smrgused in advertising or publicity pertaining to distribution of the
3961b2299dSmrgsoftware without specific, written prior permission.
401ab64890Smrg
411ab64890SmrgDIGITAL DISCLAIMS ALL WARRANTIES WITH REGARD TO THIS SOFTWARE, INCLUDING
421ab64890SmrgALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS, IN NO EVENT SHALL
431ab64890SmrgDIGITAL BE LIABLE FOR ANY SPECIAL, INDIRECT OR CONSEQUENTIAL DAMAGES OR
441ab64890SmrgANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS,
451ab64890SmrgWHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION,
461ab64890SmrgARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS
471ab64890SmrgSOFTWARE.
481ab64890Smrg
491ab64890Smrg******************************************************************/
501ab64890Smrg
511ab64890Smrg#ifdef HAVE_CONFIG_H
521ab64890Smrg#include <config.h>
531ab64890Smrg#endif
541ab64890Smrg#include "Xlibint.h"
551ab64890Smrg#include <X11/Xos.h>
561ab64890Smrg#include "Xresource.h"
571ab64890Smrg#include <stdio.h>
581ab64890Smrg
591ab64890Smrg#ifndef ERRORDB
601ab64890Smrg#ifndef XERRORDB
611ab64890Smrg#define ERRORDB "/usr/lib/X11/XErrorDB"
621ab64890Smrg#else
631ab64890Smrg#define ERRORDB XERRORDB
641ab64890Smrg#endif
651ab64890Smrg#endif
661ab64890Smrg
671ab64890Smrg/*
681ab64890Smrg * descriptions of errors in Section 4 of Protocol doc (pp. 350-351); more
691ab64890Smrg * verbose descriptions are given in the error database
701ab64890Smrg */
711ab64890Smrgstatic const char _XErrorList[] =
721ab64890Smrg    /* No error */          "no error\0"
731ab64890Smrg    /* BadRequest */        "BadRequest\0"
741ab64890Smrg    /* BadValue */          "BadValue\0"
751ab64890Smrg    /* BadWindow */         "BadWindow\0"
761ab64890Smrg    /* BadPixmap */         "BadPixmap\0"
771ab64890Smrg    /* BadAtom */           "BadAtom\0"
781ab64890Smrg    /* BadCursor */         "BadCursor\0"
791ab64890Smrg    /* BadFont */           "BadFont\0"
801ab64890Smrg    /* BadMatch */          "BadMatch\0"
811ab64890Smrg    /* BadDrawable */       "BadDrawable\0"
821ab64890Smrg    /* BadAccess */         "BadAccess\0"
831ab64890Smrg    /* BadAlloc */          "BadAlloc\0"
841ab64890Smrg    /* BadColor */          "BadColor\0"
851ab64890Smrg    /* BadGC */             "BadGC\0"
861ab64890Smrg    /* BadIDChoice */       "BadIDChoice\0"
871ab64890Smrg    /* BadName */           "BadName\0"
881ab64890Smrg    /* BadLength */         "BadLength\0"
891ab64890Smrg    /* BadImplementation */ "BadImplementation"
901ab64890Smrg;
911ab64890Smrg
921ab64890Smrg/* offsets into _XErrorList */
931ab64890Smrgstatic const unsigned char _XErrorOffsets[] = {
941ab64890Smrg    0, 9, 20, 29, 39, 49, 57, 67, 75, 84, 96,
951ab64890Smrg    106, 115, 124, 130, 142, 150, 160
961ab64890Smrg};
971ab64890Smrg
981ab64890Smrg
991ab64890Smrgint
1001ab64890SmrgXGetErrorText(
1011ab64890Smrg    register Display *dpy,
1021ab64890Smrg    register int code,
1031ab64890Smrg    char *buffer,
1041ab64890Smrg    int nbytes)
1051ab64890Smrg{
1061ab64890Smrg    char buf[150];
1071ab64890Smrg    register _XExtension *ext;
1081ab64890Smrg    _XExtension *bext = (_XExtension *)NULL;
1091ab64890Smrg
1101ab64890Smrg    if (nbytes == 0) return 0;
1111ab64890Smrg    if (code <= BadImplementation && code > 0) {
112eb411b4bSmrg        snprintf(buf, sizeof(buf), "%d", code);
1131ab64890Smrg        (void) XGetErrorDatabaseText(dpy, "XProtoError", buf,
1141ab64890Smrg                                     _XErrorList + _XErrorOffsets[code],
1151ab64890Smrg				     buffer, nbytes);
1161ab64890Smrg    } else
1171ab64890Smrg	buffer[0] = '\0';
1181ab64890Smrg    /* call out to any extensions interested */
1191ab64890Smrg    for (ext = dpy->ext_procs; ext; ext = ext->next) {
1201ab64890Smrg 	if (ext->error_string)
1211ab64890Smrg 	    (*ext->error_string)(dpy, code, &ext->codes, buffer, nbytes);
1221ab64890Smrg	if (ext->codes.first_error &&
1232e9c7c8cSmrg	    ext->codes.first_error <= code &&
1241ab64890Smrg	    (!bext || ext->codes.first_error > bext->codes.first_error))
1251ab64890Smrg	    bext = ext;
12661b2299dSmrg    }
1271ab64890Smrg    if (!buffer[0] && bext) {
128eb411b4bSmrg	snprintf(buf, sizeof(buf), "%s.%d",
129eb411b4bSmrg                 bext->name, code - bext->codes.first_error);
1301ab64890Smrg	(void) XGetErrorDatabaseText(dpy, "XProtoError", buf, "", buffer, nbytes);
1311ab64890Smrg    }
1321ab64890Smrg    if (!buffer[0])
133eb411b4bSmrg	snprintf(buffer, nbytes, "%d", code);
1341ab64890Smrg    return 0;
1351ab64890Smrg}
1361ab64890Smrg
1371ab64890Smrgint
1381ab64890Smrg/*ARGSUSED*/
1391ab64890SmrgXGetErrorDatabaseText(
1401ab64890Smrg    Display *dpy,
1411ab64890Smrg    register _Xconst char *name,
1421ab64890Smrg    register _Xconst char *type,
1431ab64890Smrg    _Xconst char *defaultp,
1441ab64890Smrg    char *buffer,
1451ab64890Smrg    int nbytes)
1461ab64890Smrg{
1471ab64890Smrg
1481ab64890Smrg    static XrmDatabase db = NULL;
1491ab64890Smrg    XrmString type_str;
1501ab64890Smrg    XrmValue result;
1511ab64890Smrg    char temp[BUFSIZ];
1521ab64890Smrg    char* tptr;
1531ab64890Smrg    unsigned long tlen;
1541ab64890Smrg
1551ab64890Smrg    if (nbytes == 0) return 0;
1561ab64890Smrg
1571ab64890Smrg    if (!db) {
1581ab64890Smrg	/* the Xrm routines expect to be called with the global
1591ab64890Smrg	   mutex unlocked. */
1601ab64890Smrg	XrmDatabase temp_db;
1611ab64890Smrg	int do_destroy;
1621ab64890Smrg	const char *dbname;
1631ab64890Smrg
1641ab64890Smrg	XrmInitialize();
1651ab64890Smrg#ifdef WIN32
1661ab64890Smrg	dbname = getenv("XERRORDB");
1671ab64890Smrg	if (!dbname)
1681ab64890Smrg	    dbname = ERRORDB;
1691ab64890Smrg#else
1701ab64890Smrg    dbname = ERRORDB;
1711ab64890Smrg#endif
1721ab64890Smrg	temp_db = XrmGetFileDatabase(dbname);
1731ab64890Smrg
1741ab64890Smrg	_XLockMutex(_Xglobal_lock);
1751ab64890Smrg	if (!db) {
1761ab64890Smrg	    db = temp_db;
1771ab64890Smrg	    do_destroy = 0;
1781ab64890Smrg	} else
1791ab64890Smrg	    do_destroy = 1;	/* we didn't need to get it after all */
1801ab64890Smrg	_XUnlockMutex(_Xglobal_lock);
1811ab64890Smrg
1821ab64890Smrg	if (do_destroy)
1831ab64890Smrg	    XrmDestroyDatabase(temp_db);
1841ab64890Smrg    }
1851ab64890Smrg
1861ab64890Smrg    if (db)
1871ab64890Smrg    {
1881ab64890Smrg	tlen = strlen (name) + strlen (type) + 2;
1891ab64890Smrg	if (tlen <= sizeof(temp))
1901ab64890Smrg	    tptr = temp;
1911ab64890Smrg	else
1921ab64890Smrg	    tptr = Xmalloc (tlen);
1931ab64890Smrg	if (tptr) {
194eb411b4bSmrg	    snprintf(tptr, tlen, "%s.%s", name, type);
19561b2299dSmrg	    XrmGetResource(db, tptr, "ErrorType.ErrorNumber",
1961ab64890Smrg	      &type_str, &result);
1971ab64890Smrg	    if (tptr != temp)
1981ab64890Smrg		Xfree (tptr);
1991ab64890Smrg	} else {
2001ab64890Smrg	    result.addr = (XPointer) NULL;
2011ab64890Smrg	}
2021ab64890Smrg    }
2031ab64890Smrg    else
2041ab64890Smrg	result.addr = (XPointer)NULL;
2051ab64890Smrg    if (!result.addr) {
2061ab64890Smrg	result.addr = (XPointer) defaultp;
2079c019ec5Smaya	result.size = (unsigned)strlen(defaultp) + 1;
2081ab64890Smrg    }
2099c019ec5Smaya    (void) strncpy (buffer, (char *) result.addr, (size_t)nbytes);
2101ab64890Smrg    if (result.size > nbytes) buffer[nbytes-1] = '\0';
2111ab64890Smrg    return 0;
2121ab64890Smrg}
213