2 * XML DRI client-side driver configuration
3 * Copyright (C) 2003 Felix Kuehling
5 * Permission is hereby granted, free of charge, to any person obtaining a
6 * copy of this software and associated documentation files (the "Software"),
7 * to deal in the Software without restriction, including without limitation
8 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
9 * and/or sell copies of the Software, and to permit persons to whom the
10 * Software is furnished to do so, subject to the following conditions:
12 * The above copyright notice and this permission notice shall be included
13 * in all copies or substantial portions of the Software.
15 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
16 * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
18 * FELIX KUEHLING, OR ANY OTHER CONTRIBUTORS BE LIABLE FOR ANY CLAIM,
19 * DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR
20 * OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE
21 * OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
26 * \brief Driver-independent client-side part of the XML configuration
27 * \author Felix Kuehling
30 #include "main/glheader.h"
38 #include "main/imports.h"
40 #include "xmlconfig.h"
42 #undef GET_PROGRAM_NAME
44 #if (defined(__GNU_LIBRARY__) || defined(__GLIBC__)) && !defined(__UCLIBC__)
45 # if !defined(__GLIBC__) || (__GLIBC__ < 2)
46 /* These aren't declared in any libc5 header */
47 extern char *program_invocation_name, *program_invocation_short_name;
49 # define GET_PROGRAM_NAME() program_invocation_short_name
50 #elif defined(__FreeBSD__) && (__FreeBSD__ >= 2)
51 # include <osreldate.h>
52 # if (__FreeBSD_version >= 440000)
54 # define GET_PROGRAM_NAME() getprogname()
56 #elif defined(__NetBSD__) && defined(__NetBSD_Version) && (__NetBSD_Version >= 106000100)
58 # define GET_PROGRAM_NAME() getprogname()
59 #elif defined(__APPLE__)
61 # define GET_PROGRAM_NAME() getprogname()
63 /* Solaris has getexecname() which returns the full path - return just
64 the basename to match BSD getprogname() */
68 static const char *__getProgramName () {
69 static const char *progname;
71 if (progname == NULL) {
72 const char *e = getexecname();
74 /* Have to make a copy since getexecname can return a readonly
75 string, but basename expects to be able to modify its arg. */
78 progname = basename(n);
85 # define GET_PROGRAM_NAME() __getProgramName()
88 #if !defined(GET_PROGRAM_NAME)
89 # if defined(__OpenBSD__) || defined(NetBSD) || defined(__UCLIBC__)
90 /* This is a hack. It's said to work on OpenBSD, NetBSD and GNU.
91 * Rogelio M.Serrano Jr. reported it's also working with UCLIBC. It's
92 * used as a last resort, if there is no documented facility available. */
93 static const char *__getProgramName () {
94 extern const char *__progname;
95 char * arg = strrchr(__progname, '/');
101 # define GET_PROGRAM_NAME() __getProgramName()
103 # define GET_PROGRAM_NAME() ""
104 # warning "Per application configuration won't work with your OS version."
108 /** \brief Find an option in an option cache with the name as key */
109 static GLuint findOption (const driOptionCache *cache, const char *name) {
110 GLuint len = strlen (name);
111 GLuint size = 1 << cache->tableSize, mask = size - 1;
115 /* compute a hash from the variable length name */
116 for (i = 0, shift = 0; i < len; ++i, shift = (shift+8) & 31)
117 hash += (GLuint)name[i] << shift;
119 hash = (hash >> (16-cache->tableSize/2)) & mask;
121 /* this is just the starting point of the linear search for the option */
122 for (i = 0; i < size; ++i, hash = (hash+1) & mask) {
123 /* if we hit an empty entry then the option is not defined (yet) */
124 if (cache->info[hash].name == 0)
126 else if (!strcmp (name, cache->info[hash].name))
129 /* this assertion fails if the hash table is full */
135 /** \brief Count the real number of options in an option cache */
136 static GLuint countOptions (const driOptionCache *cache) {
137 GLuint size = 1 << cache->tableSize;
139 for (i = 0; i < size; ++i)
140 if (cache->info[i].name)
145 /** \brief Like strdup but using MALLOC and with error checking. */
146 #define XSTRDUP(dest,source) do { \
147 GLuint len = strlen (source); \
148 if (!(dest = MALLOC (len+1))) { \
149 fprintf (stderr, "%s: %d: out of memory.\n", __FILE__, __LINE__); \
152 memcpy (dest, source, len+1); \
155 static int compare (const void *a, const void *b) {
156 return strcmp (*(char *const*)a, *(char *const*)b);
158 /** \brief Binary search in a string array. */
159 static GLuint bsearchStr (const XML_Char *name,
160 const XML_Char *elems[], GLuint count) {
161 const XML_Char **found;
162 found = bsearch (&name, elems, count, sizeof (XML_Char *), compare);
164 return found - elems;
169 /** \brief Locale-independent integer parser.
171 * Works similar to strtol. Leading space is NOT skipped. The input
172 * number may have an optional sign. Radix is specified by base. If
173 * base is 0 then decimal is assumed unless the input number is
174 * prefixed by 0x or 0X for hexadecimal or 0 for octal. After
175 * returning tail points to the first character that is not part of
176 * the integer number. If no number was found then tail points to the
177 * start of the input string. */
178 static GLint strToI (const XML_Char *string, const XML_Char **tail, int base) {
179 GLint radix = base == 0 ? 10 : base;
182 GLboolean numberFound = GL_FALSE;
183 const XML_Char *start = string;
185 assert (radix >= 2 && radix <= 36);
187 if (*string == '-') {
190 } else if (*string == '+')
192 if (base == 0 && *string == '0') {
193 numberFound = GL_TRUE;
194 if (*(string+1) == 'x' || *(string+1) == 'X') {
205 if (*string >= '0' && *string < '0' + radix)
206 digit = *string - '0';
208 if (*string >= '0' && *string <= '9')
209 digit = *string - '0';
210 else if (*string >= 'a' && *string < 'a' + radix - 10)
211 digit = *string - 'a' + 10;
212 else if (*string >= 'A' && *string < 'A' + radix - 10)
213 digit = *string - 'A' + 10;
216 numberFound = GL_TRUE;
217 result = radix*result + digit;
222 *tail = numberFound ? string : start;
223 return sign * result;
226 /** \brief Locale-independent floating-point parser.
228 * Works similar to strtod. Leading space is NOT skipped. The input
229 * number may have an optional sign. '.' is interpreted as decimal
230 * point and may occur at most once. Optionally the number may end in
231 * [eE]<exponent>, where <exponent> is an integer as recognized by
232 * strToI. In that case the result is number * 10^exponent. After
233 * returning tail points to the first character that is not part of
234 * the floating point number. If no number was found then tail points
235 * to the start of the input string.
237 * Uses two passes for maximum accuracy. */
238 static GLfloat strToF (const XML_Char *string, const XML_Char **tail) {
239 GLint nDigits = 0, pointPos, exponent;
240 GLfloat sign = 1.0f, result = 0.0f, scale;
241 const XML_Char *start = string, *numStart;
244 if (*string == '-') {
247 } else if (*string == '+')
250 /* first pass: determine position of decimal point, number of
251 * digits, exponent and the end of the number. */
253 while (*string >= '0' && *string <= '9') {
258 if (*string == '.') {
260 while (*string >= '0' && *string <= '9') {
266 /* no digits, no number */
271 if (*string == 'e' || *string == 'E') {
272 const XML_Char *expTail;
273 exponent = strToI (string+1, &expTail, 10);
274 if (expTail == string+1)
282 /* scale of the first digit */
283 scale = sign * (GLfloat)pow (10.0, (GLdouble)(pointPos-1 + exponent));
285 /* second pass: parse digits */
287 if (*string != '.') {
288 assert (*string >= '0' && *string <= '9');
289 result += scale * (GLfloat)(*string - '0');
294 } while (nDigits > 0);
299 /** \brief Parse a value of a given type. */
300 static GLboolean parseValue (driOptionValue *v, driOptionType type,
301 const XML_Char *string) {
302 const XML_Char *tail = NULL;
303 /* skip leading white-space */
304 string += strspn (string, " \f\n\r\t\v");
307 if (!strcmp (string, "false")) {
310 } else if (!strcmp (string, "true")) {
317 case DRI_ENUM: /* enum is just a special integer */
319 v->_int = strToI (string, &tail, 0);
322 v->_float = strToF (string, &tail);
327 return GL_FALSE; /* empty string (or containing only white-space) */
328 /* skip trailing white space */
330 tail += strspn (tail, " \f\n\r\t\v");
332 return GL_FALSE; /* something left over that is not part of value */
337 /** \brief Parse a list of ranges of type info->type. */
338 static GLboolean parseRanges (driOptionInfo *info, const XML_Char *string) {
339 XML_Char *cp, *range;
341 driOptionRange *ranges;
343 XSTRDUP (cp, string);
344 /* pass 1: determine the number of ranges (number of commas + 1) */
346 for (nRanges = 1; *range; ++range)
350 if ((ranges = MALLOC (nRanges*sizeof(driOptionRange))) == NULL) {
351 fprintf (stderr, "%s: %d: out of memory.\n", __FILE__, __LINE__);
355 /* pass 2: parse all ranges into preallocated array */
357 for (i = 0; i < nRanges; ++i) {
360 end = strchr (range, ',');
363 sep = strchr (range, ':');
364 if (sep) { /* non-empty interval */
366 if (!parseValue (&ranges[i].start, info->type, range) ||
367 !parseValue (&ranges[i].end, info->type, sep+1))
369 if (info->type == DRI_INT &&
370 ranges[i].start._int > ranges[i].end._int)
372 if (info->type == DRI_FLOAT &&
373 ranges[i].start._float > ranges[i].end._float)
375 } else { /* empty interval */
376 if (!parseValue (&ranges[i].start, info->type, range))
378 ranges[i].end = ranges[i].start;
390 assert (range == NULL);
392 info->nRanges = nRanges;
393 info->ranges = ranges;
397 /** \brief Check if a value is in one of info->ranges. */
398 static GLboolean checkValue (const driOptionValue *v, const driOptionInfo *info) {
400 assert (info->type != DRI_BOOL); /* should be caught by the parser */
401 if (info->nRanges == 0)
403 switch (info->type) {
404 case DRI_ENUM: /* enum is just a special integer */
406 for (i = 0; i < info->nRanges; ++i)
407 if (v->_int >= info->ranges[i].start._int &&
408 v->_int <= info->ranges[i].end._int)
412 for (i = 0; i < info->nRanges; ++i)
413 if (v->_float >= info->ranges[i].start._float &&
414 v->_float <= info->ranges[i].end._float)
418 assert (0); /* should never happen */
423 /** \brief Output a warning message. */
424 #define XML_WARNING1(msg) do {\
425 __driUtilMessage ("Warning in %s line %d, column %d: "msg, data->name, \
426 (int) XML_GetCurrentLineNumber(data->parser), \
427 (int) XML_GetCurrentColumnNumber(data->parser)); \
429 #define XML_WARNING(msg,args...) do { \
430 __driUtilMessage ("Warning in %s line %d, column %d: "msg, data->name, \
431 (int) XML_GetCurrentLineNumber(data->parser), \
432 (int) XML_GetCurrentColumnNumber(data->parser), \
435 /** \brief Output an error message. */
436 #define XML_ERROR1(msg) do { \
437 __driUtilMessage ("Error in %s line %d, column %d: "msg, data->name, \
438 (int) XML_GetCurrentLineNumber(data->parser), \
439 (int) XML_GetCurrentColumnNumber(data->parser)); \
441 #define XML_ERROR(msg,args...) do { \
442 __driUtilMessage ("Error in %s line %d, column %d: "msg, data->name, \
443 (int) XML_GetCurrentLineNumber(data->parser), \
444 (int) XML_GetCurrentColumnNumber(data->parser), \
447 /** \brief Output a fatal error message and abort. */
448 #define XML_FATAL1(msg) do { \
449 fprintf (stderr, "Fatal error in %s line %d, column %d: "msg"\n", \
451 (int) XML_GetCurrentLineNumber(data->parser), \
452 (int) XML_GetCurrentColumnNumber(data->parser)); \
455 #define XML_FATAL(msg,args...) do { \
456 fprintf (stderr, "Fatal error in %s line %d, column %d: "msg"\n", \
458 (int) XML_GetCurrentLineNumber(data->parser), \
459 (int) XML_GetCurrentColumnNumber(data->parser), \
464 /** \brief Parser context for __driConfigOptions. */
468 driOptionCache *cache;
477 /** \brief Elements in __driConfigOptions. */
479 OI_DESCRIPTION = 0, OI_DRIINFO, OI_ENUM, OI_OPTION, OI_SECTION, OI_COUNT
481 static const XML_Char *OptInfoElems[] = {
482 "description", "driinfo", "enum", "option", "section"
485 /** \brief Parse attributes of an enum element.
487 * We're not actually interested in the data. Just make sure this is ok
488 * for external configuration tools.
490 static void parseEnumAttr (struct OptInfoData *data, const XML_Char **attr) {
492 const XML_Char *value = NULL, *text = NULL;
494 GLuint opt = data->curOption;
495 for (i = 0; attr[i]; i += 2) {
496 if (!strcmp (attr[i], "value")) value = attr[i+1];
497 else if (!strcmp (attr[i], "text")) text = attr[i+1];
498 else XML_FATAL("illegal enum attribute: %s.", attr[i]);
500 if (!value) XML_FATAL1 ("value attribute missing in enum.");
501 if (!text) XML_FATAL1 ("text attribute missing in enum.");
502 if (!parseValue (&v, data->cache->info[opt].type, value))
503 XML_FATAL ("illegal enum value: %s.", value);
504 if (!checkValue (&v, &data->cache->info[opt]))
505 XML_FATAL ("enum value out of valid range: %s.", value);
508 /** \brief Parse attributes of a description element.
510 * We're not actually interested in the data. Just make sure this is ok
511 * for external configuration tools.
513 static void parseDescAttr (struct OptInfoData *data, const XML_Char **attr) {
515 const XML_Char *lang = NULL, *text = NULL;
516 for (i = 0; attr[i]; i += 2) {
517 if (!strcmp (attr[i], "lang")) lang = attr[i+1];
518 else if (!strcmp (attr[i], "text")) text = attr[i+1];
519 else XML_FATAL("illegal description attribute: %s.", attr[i]);
521 if (!lang) XML_FATAL1 ("lang attribute missing in description.");
522 if (!text) XML_FATAL1 ("text attribute missing in description.");
525 /** \brief Parse attributes of an option element. */
526 static void parseOptInfoAttr (struct OptInfoData *data, const XML_Char **attr) {
527 enum OptAttr {OA_DEFAULT = 0, OA_NAME, OA_TYPE, OA_VALID, OA_COUNT};
528 static const XML_Char *optAttr[] = {"default", "name", "type", "valid"};
529 const XML_Char *attrVal[OA_COUNT] = {NULL, NULL, NULL, NULL};
530 const char *defaultVal;
531 driOptionCache *cache = data->cache;
533 for (i = 0; attr[i]; i += 2) {
534 GLuint attrName = bsearchStr (attr[i], optAttr, OA_COUNT);
535 if (attrName >= OA_COUNT)
536 XML_FATAL ("illegal option attribute: %s", attr[i]);
537 attrVal[attrName] = attr[i+1];
539 if (!attrVal[OA_NAME]) XML_FATAL1 ("name attribute missing in option.");
540 if (!attrVal[OA_TYPE]) XML_FATAL1 ("type attribute missing in option.");
541 if (!attrVal[OA_DEFAULT]) XML_FATAL1 ("default attribute missing in option.");
543 opt = findOption (cache, attrVal[OA_NAME]);
544 if (cache->info[opt].name)
545 XML_FATAL ("option %s redefined.", attrVal[OA_NAME]);
546 data->curOption = opt;
548 XSTRDUP (cache->info[opt].name, attrVal[OA_NAME]);
550 if (!strcmp (attrVal[OA_TYPE], "bool"))
551 cache->info[opt].type = DRI_BOOL;
552 else if (!strcmp (attrVal[OA_TYPE], "enum"))
553 cache->info[opt].type = DRI_ENUM;
554 else if (!strcmp (attrVal[OA_TYPE], "int"))
555 cache->info[opt].type = DRI_INT;
556 else if (!strcmp (attrVal[OA_TYPE], "float"))
557 cache->info[opt].type = DRI_FLOAT;
559 XML_FATAL ("illegal type in option: %s.", attrVal[OA_TYPE]);
561 defaultVal = getenv (cache->info[opt].name);
562 if (defaultVal != NULL) {
563 /* don't use XML_WARNING, we want the user to see this! */
565 "ATTENTION: default value of option %s overridden by environment.\n",
566 cache->info[opt].name);
568 defaultVal = attrVal[OA_DEFAULT];
569 if (!parseValue (&cache->values[opt], cache->info[opt].type, defaultVal))
570 XML_FATAL ("illegal default value: %s.", defaultVal);
572 if (attrVal[OA_VALID]) {
573 if (cache->info[opt].type == DRI_BOOL)
574 XML_FATAL1 ("boolean option with valid attribute.");
575 if (!parseRanges (&cache->info[opt], attrVal[OA_VALID]))
576 XML_FATAL ("illegal valid attribute: %s.", attrVal[OA_VALID]);
577 if (!checkValue (&cache->values[opt], &cache->info[opt]))
578 XML_FATAL ("default value out of valid range '%s': %s.",
579 attrVal[OA_VALID], defaultVal);
580 } else if (cache->info[opt].type == DRI_ENUM) {
581 XML_FATAL1 ("valid attribute missing in option (mandatory for enums).");
583 cache->info[opt].nRanges = 0;
584 cache->info[opt].ranges = NULL;
588 /** \brief Handler for start element events. */
589 static void optInfoStartElem (void *userData, const XML_Char *name,
590 const XML_Char **attr) {
591 struct OptInfoData *data = (struct OptInfoData *)userData;
592 enum OptInfoElem elem = bsearchStr (name, OptInfoElems, OI_COUNT);
596 XML_FATAL1 ("nested <driinfo> elements.");
598 XML_FATAL1 ("attributes specified on <driinfo> element.");
599 data->inDriInfo = GL_TRUE;
602 if (!data->inDriInfo)
603 XML_FATAL1 ("<section> must be inside <driinfo>.");
605 XML_FATAL1 ("nested <section> elements.");
607 XML_FATAL1 ("attributes specified on <section> element.");
608 data->inSection = GL_TRUE;
611 if (!data->inSection && !data->inOption)
612 XML_FATAL1 ("<description> must be inside <description> or <option.");
614 XML_FATAL1 ("nested <description> elements.");
615 data->inDesc = GL_TRUE;
616 parseDescAttr (data, attr);
619 if (!data->inSection)
620 XML_FATAL1 ("<option> must be inside <section>.");
622 XML_FATAL1 ("<option> nested in <description> element.");
624 XML_FATAL1 ("nested <option> elements.");
625 data->inOption = GL_TRUE;
626 parseOptInfoAttr (data, attr);
629 if (!(data->inOption && data->inDesc))
630 XML_FATAL1 ("<enum> must be inside <option> and <description>.");
632 XML_FATAL1 ("nested <enum> elements.");
633 data->inEnum = GL_TRUE;
634 parseEnumAttr (data, attr);
637 XML_FATAL ("unknown element: %s.", name);
641 /** \brief Handler for end element events. */
642 static void optInfoEndElem (void *userData, const XML_Char *name) {
643 struct OptInfoData *data = (struct OptInfoData *)userData;
644 enum OptInfoElem elem = bsearchStr (name, OptInfoElems, OI_COUNT);
647 data->inDriInfo = GL_FALSE;
650 data->inSection = GL_FALSE;
653 data->inDesc = GL_FALSE;
656 data->inOption = GL_FALSE;
659 data->inEnum = GL_FALSE;
662 assert (0); /* should have been caught by StartElem */
666 void driParseOptionInfo (driOptionCache *info,
667 const char *configOptions, GLuint nConfigOptions) {
670 struct OptInfoData userData;
671 struct OptInfoData *data = &userData;
674 /* determine hash table size and allocate memory:
675 * 3/2 of the number of options, rounded up, so there remains always
676 * at least one free entry. This is needed for detecting undefined
677 * options in configuration files without getting a hash table overflow.
678 * Round this up to a power of two. */
679 GLuint minSize = (nConfigOptions*3 + 1) / 2;
680 GLuint size, log2size;
681 for (size = 1, log2size = 0; size < minSize; size <<= 1, ++log2size);
682 info->tableSize = log2size;
683 info->info = CALLOC (size * sizeof (driOptionInfo));
684 info->values = CALLOC (size * sizeof (driOptionValue));
685 if (info->info == NULL || info->values == NULL) {
686 fprintf (stderr, "%s: %d: out of memory.\n", __FILE__, __LINE__);
690 p = XML_ParserCreate ("UTF-8"); /* always UTF-8 */
691 XML_SetElementHandler (p, optInfoStartElem, optInfoEndElem);
692 XML_SetUserData (p, data);
694 userData.name = "__driConfigOptions";
696 userData.cache = info;
697 userData.inDriInfo = GL_FALSE;
698 userData.inSection = GL_FALSE;
699 userData.inDesc = GL_FALSE;
700 userData.inOption = GL_FALSE;
701 userData.inEnum = GL_FALSE;
702 userData.curOption = -1;
704 status = XML_Parse (p, configOptions, strlen (configOptions), 1);
706 XML_FATAL ("%s.", XML_ErrorString(XML_GetErrorCode(p)));
710 /* Check if the actual number of options matches nConfigOptions.
711 * A mismatch is not fatal (a hash table overflow would be) but we
712 * want the driver developer's attention anyway. */
713 realNoptions = countOptions (info);
714 if (realNoptions != nConfigOptions) {
716 "Error: nConfigOptions (%u) does not match the actual number of options in\n"
717 " __driConfigOptions (%u).\n",
718 nConfigOptions, realNoptions);
722 /** \brief Parser context for configuration files. */
726 driOptionCache *cache;
728 const char *driverName, *execName;
729 GLuint ignoringDevice;
737 /** \brief Elements in configuration files. */
739 OC_APPLICATION = 0, OC_DEVICE, OC_DRICONF, OC_OPTION, OC_COUNT
741 static const XML_Char *OptConfElems[] = {
742 "application", "device", "driconf", "option"
745 /** \brief Parse attributes of a device element. */
746 static void parseDeviceAttr (struct OptConfData *data, const XML_Char **attr) {
748 const XML_Char *driver = NULL, *screen = NULL;
749 for (i = 0; attr[i]; i += 2) {
750 if (!strcmp (attr[i], "driver")) driver = attr[i+1];
751 else if (!strcmp (attr[i], "screen")) screen = attr[i+1];
752 else XML_WARNING("unknown device attribute: %s.", attr[i]);
754 if (driver && strcmp (driver, data->driverName))
755 data->ignoringDevice = data->inDevice;
757 driOptionValue screenNum;
758 if (!parseValue (&screenNum, DRI_INT, screen))
759 XML_WARNING("illegal screen number: %s.", screen);
760 else if (screenNum._int != data->screenNum)
761 data->ignoringDevice = data->inDevice;
765 /** \brief Parse attributes of an application element. */
766 static void parseAppAttr (struct OptConfData *data, const XML_Char **attr) {
768 const XML_Char *name = NULL, *exec = NULL;
769 for (i = 0; attr[i]; i += 2) {
770 if (!strcmp (attr[i], "name")) name = attr[i+1];
771 else if (!strcmp (attr[i], "executable")) exec = attr[i+1];
772 else XML_WARNING("unknown application attribute: %s.", attr[i]);
774 if (exec && strcmp (exec, data->execName))
775 data->ignoringApp = data->inApp;
778 /** \brief Parse attributes of an option element. */
779 static void parseOptConfAttr (struct OptConfData *data, const XML_Char **attr) {
781 const XML_Char *name = NULL, *value = NULL;
782 for (i = 0; attr[i]; i += 2) {
783 if (!strcmp (attr[i], "name")) name = attr[i+1];
784 else if (!strcmp (attr[i], "value")) value = attr[i+1];
785 else XML_WARNING("unknown option attribute: %s.", attr[i]);
787 if (!name) XML_WARNING1 ("name attribute missing in option.");
788 if (!value) XML_WARNING1 ("value attribute missing in option.");
790 driOptionCache *cache = data->cache;
791 GLuint opt = findOption (cache, name);
792 if (cache->info[opt].name == NULL)
793 XML_WARNING ("undefined option: %s.", name);
794 else if (getenv (cache->info[opt].name))
795 /* don't use XML_WARNING, we want the user to see this! */
796 fprintf (stderr, "ATTENTION: option value of option %s ignored.\n",
797 cache->info[opt].name);
798 else if (!parseValue (&cache->values[opt], cache->info[opt].type, value))
799 XML_WARNING ("illegal option value: %s.", value);
803 /** \brief Handler for start element events. */
804 static void optConfStartElem (void *userData, const XML_Char *name,
805 const XML_Char **attr) {
806 struct OptConfData *data = (struct OptConfData *)userData;
807 enum OptConfElem elem = bsearchStr (name, OptConfElems, OC_COUNT);
811 XML_WARNING1 ("nested <driconf> elements.");
813 XML_WARNING1 ("attributes specified on <driconf> element.");
817 if (!data->inDriConf)
818 XML_WARNING1 ("<device> should be inside <driconf>.");
820 XML_WARNING1 ("nested <device> elements.");
822 if (!data->ignoringDevice && !data->ignoringApp)
823 parseDeviceAttr (data, attr);
827 XML_WARNING1 ("<application> should be inside <device>.");
829 XML_WARNING1 ("nested <application> elements.");
831 if (!data->ignoringDevice && !data->ignoringApp)
832 parseAppAttr (data, attr);
836 XML_WARNING1 ("<option> should be inside <application>.");
838 XML_WARNING1 ("nested <option> elements.");
840 if (!data->ignoringDevice && !data->ignoringApp)
841 parseOptConfAttr (data, attr);
844 XML_WARNING ("unknown element: %s.", name);
848 /** \brief Handler for end element events. */
849 static void optConfEndElem (void *userData, const XML_Char *name) {
850 struct OptConfData *data = (struct OptConfData *)userData;
851 enum OptConfElem elem = bsearchStr (name, OptConfElems, OC_COUNT);
857 if (data->inDevice-- == data->ignoringDevice)
858 data->ignoringDevice = 0;
861 if (data->inApp-- == data->ignoringApp)
862 data->ignoringApp = 0;
868 /* unknown element, warning was produced on start tag */;
872 /** \brief Initialize an option cache based on info */
873 static void initOptionCache (driOptionCache *cache, const driOptionCache *info) {
874 cache->info = info->info;
875 cache->tableSize = info->tableSize;
876 cache->values = MALLOC ((1<<info->tableSize) * sizeof (driOptionValue));
877 if (cache->values == NULL) {
878 fprintf (stderr, "%s: %d: out of memory.\n", __FILE__, __LINE__);
881 memcpy (cache->values, info->values,
882 (1<<info->tableSize) * sizeof (driOptionValue));
885 /** \brief Parse the named configuration file */
886 static void parseOneConfigFile (XML_Parser p) {
887 #define BUF_SIZE 0x1000
888 struct OptConfData *data = (struct OptConfData *)XML_GetUserData (p);
892 if ((fd = open (data->name, O_RDONLY)) == -1) {
893 __driUtilMessage ("Can't open configuration file %s: %s.",
894 data->name, strerror (errno));
900 void *buffer = XML_GetBuffer (p, BUF_SIZE);
902 __driUtilMessage ("Can't allocate parser buffer.");
905 bytesRead = read (fd, buffer, BUF_SIZE);
906 if (bytesRead == -1) {
907 __driUtilMessage ("Error reading from configuration file %s: %s.",
908 data->name, strerror (errno));
911 status = XML_ParseBuffer (p, bytesRead, bytesRead == 0);
913 XML_ERROR ("%s.", XML_ErrorString(XML_GetErrorCode(p)));
924 void driParseConfigFiles (driOptionCache *cache, const driOptionCache *info,
925 GLint screenNum, const char *driverName) {
926 char *filenames[2] = {"/etc/drirc", NULL};
929 struct OptConfData userData;
931 initOptionCache (cache, info);
933 userData.cache = cache;
934 userData.screenNum = screenNum;
935 userData.driverName = driverName;
936 userData.execName = GET_PROGRAM_NAME();
938 if ((home = getenv ("HOME"))) {
939 GLuint len = strlen (home);
940 filenames[1] = MALLOC (len + 7+1);
941 if (filenames[1] == NULL)
942 __driUtilMessage ("Can't allocate memory for %s/.drirc.", home);
944 memcpy (filenames[1], home, len);
945 memcpy (filenames[1] + len, "/.drirc", 7+1);
949 for (i = 0; i < 2; ++i) {
951 if (filenames[i] == NULL)
954 p = XML_ParserCreate (NULL); /* use encoding specified by file */
955 XML_SetElementHandler (p, optConfStartElem, optConfEndElem);
956 XML_SetUserData (p, &userData);
958 userData.name = filenames[i];
959 userData.ignoringDevice = 0;
960 userData.ignoringApp = 0;
961 userData.inDriConf = 0;
962 userData.inDevice = 0;
964 userData.inOption = 0;
966 parseOneConfigFile (p);
974 void driDestroyOptionInfo (driOptionCache *info) {
975 driDestroyOptionCache (info);
977 GLuint i, size = 1 << info->tableSize;
978 for (i = 0; i < size; ++i) {
979 if (info->info[i].name) {
980 FREE (info->info[i].name);
981 if (info->info[i].ranges)
982 FREE (info->info[i].ranges);
989 void driDestroyOptionCache (driOptionCache *cache) {
991 FREE (cache->values);
994 GLboolean driCheckOption (const driOptionCache *cache, const char *name,
995 driOptionType type) {
996 GLuint i = findOption (cache, name);
997 return cache->info[i].name != NULL && cache->info[i].type == type;
1000 GLboolean driQueryOptionb (const driOptionCache *cache, const char *name) {
1001 GLuint i = findOption (cache, name);
1002 /* make sure the option is defined and has the correct type */
1003 assert (cache->info[i].name != NULL);
1004 assert (cache->info[i].type == DRI_BOOL);
1005 return cache->values[i]._bool;
1008 GLint driQueryOptioni (const driOptionCache *cache, const char *name) {
1009 GLuint i = findOption (cache, name);
1010 /* make sure the option is defined and has the correct type */
1011 assert (cache->info[i].name != NULL);
1012 assert (cache->info[i].type == DRI_INT || cache->info[i].type == DRI_ENUM);
1013 return cache->values[i]._int;
1016 GLfloat driQueryOptionf (const driOptionCache *cache, const char *name) {
1017 GLuint i = findOption (cache, name);
1018 /* make sure the option is defined and has the correct type */
1019 assert (cache->info[i].name != NULL);
1020 assert (cache->info[i].type == DRI_FLOAT);
1021 return cache->values[i]._float;