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__) || defined(ANDROID)
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 */
424 * Print message to \c stderr if the \c LIBGL_DEBUG environment variable
427 * Is called from the drivers.
429 * \param f \c printf like format string.
432 __driUtilMessage(const char *f
, ...)
436 if (getenv("LIBGL_DEBUG")) {
437 fprintf(stderr
, "libGL: ");
439 vfprintf(stderr
, f
, args
);
441 fprintf(stderr
, "\n");
445 /** \brief Output a warning message. */
446 #define XML_WARNING1(msg) do {\
447 __driUtilMessage ("Warning in %s line %d, column %d: "msg, data->name, \
448 (int) XML_GetCurrentLineNumber(data->parser), \
449 (int) XML_GetCurrentColumnNumber(data->parser)); \
451 #define XML_WARNING(msg,args...) do { \
452 __driUtilMessage ("Warning in %s line %d, column %d: "msg, data->name, \
453 (int) XML_GetCurrentLineNumber(data->parser), \
454 (int) XML_GetCurrentColumnNumber(data->parser), \
457 /** \brief Output an error message. */
458 #define XML_ERROR1(msg) do { \
459 __driUtilMessage ("Error in %s line %d, column %d: "msg, data->name, \
460 (int) XML_GetCurrentLineNumber(data->parser), \
461 (int) XML_GetCurrentColumnNumber(data->parser)); \
463 #define XML_ERROR(msg,args...) do { \
464 __driUtilMessage ("Error in %s line %d, column %d: "msg, data->name, \
465 (int) XML_GetCurrentLineNumber(data->parser), \
466 (int) XML_GetCurrentColumnNumber(data->parser), \
469 /** \brief Output a fatal error message and abort. */
470 #define XML_FATAL1(msg) do { \
471 fprintf (stderr, "Fatal error in %s line %d, column %d: "msg"\n", \
473 (int) XML_GetCurrentLineNumber(data->parser), \
474 (int) XML_GetCurrentColumnNumber(data->parser)); \
477 #define XML_FATAL(msg,args...) do { \
478 fprintf (stderr, "Fatal error in %s line %d, column %d: "msg"\n", \
480 (int) XML_GetCurrentLineNumber(data->parser), \
481 (int) XML_GetCurrentColumnNumber(data->parser), \
486 /** \brief Parser context for __driConfigOptions. */
490 driOptionCache
*cache
;
499 /** \brief Elements in __driConfigOptions. */
501 OI_DESCRIPTION
= 0, OI_DRIINFO
, OI_ENUM
, OI_OPTION
, OI_SECTION
, OI_COUNT
503 static const XML_Char
*OptInfoElems
[] = {
504 "description", "driinfo", "enum", "option", "section"
507 /** \brief Parse attributes of an enum element.
509 * We're not actually interested in the data. Just make sure this is ok
510 * for external configuration tools.
512 static void parseEnumAttr (struct OptInfoData
*data
, const XML_Char
**attr
) {
514 const XML_Char
*value
= NULL
, *text
= NULL
;
516 GLuint opt
= data
->curOption
;
517 for (i
= 0; attr
[i
]; i
+= 2) {
518 if (!strcmp (attr
[i
], "value")) value
= attr
[i
+1];
519 else if (!strcmp (attr
[i
], "text")) text
= attr
[i
+1];
520 else XML_FATAL("illegal enum attribute: %s.", attr
[i
]);
522 if (!value
) XML_FATAL1 ("value attribute missing in enum.");
523 if (!text
) XML_FATAL1 ("text attribute missing in enum.");
524 if (!parseValue (&v
, data
->cache
->info
[opt
].type
, value
))
525 XML_FATAL ("illegal enum value: %s.", value
);
526 if (!checkValue (&v
, &data
->cache
->info
[opt
]))
527 XML_FATAL ("enum value out of valid range: %s.", value
);
530 /** \brief Parse attributes of a description element.
532 * We're not actually interested in the data. Just make sure this is ok
533 * for external configuration tools.
535 static void parseDescAttr (struct OptInfoData
*data
, const XML_Char
**attr
) {
537 const XML_Char
*lang
= NULL
, *text
= NULL
;
538 for (i
= 0; attr
[i
]; i
+= 2) {
539 if (!strcmp (attr
[i
], "lang")) lang
= attr
[i
+1];
540 else if (!strcmp (attr
[i
], "text")) text
= attr
[i
+1];
541 else XML_FATAL("illegal description attribute: %s.", attr
[i
]);
543 if (!lang
) XML_FATAL1 ("lang attribute missing in description.");
544 if (!text
) XML_FATAL1 ("text attribute missing in description.");
547 /** \brief Parse attributes of an option element. */
548 static void parseOptInfoAttr (struct OptInfoData
*data
, const XML_Char
**attr
) {
549 enum OptAttr
{OA_DEFAULT
= 0, OA_NAME
, OA_TYPE
, OA_VALID
, OA_COUNT
};
550 static const XML_Char
*optAttr
[] = {"default", "name", "type", "valid"};
551 const XML_Char
*attrVal
[OA_COUNT
] = {NULL
, NULL
, NULL
, NULL
};
552 const char *defaultVal
;
553 driOptionCache
*cache
= data
->cache
;
555 for (i
= 0; attr
[i
]; i
+= 2) {
556 GLuint attrName
= bsearchStr (attr
[i
], optAttr
, OA_COUNT
);
557 if (attrName
>= OA_COUNT
)
558 XML_FATAL ("illegal option attribute: %s", attr
[i
]);
559 attrVal
[attrName
] = attr
[i
+1];
561 if (!attrVal
[OA_NAME
]) XML_FATAL1 ("name attribute missing in option.");
562 if (!attrVal
[OA_TYPE
]) XML_FATAL1 ("type attribute missing in option.");
563 if (!attrVal
[OA_DEFAULT
]) XML_FATAL1 ("default attribute missing in option.");
565 opt
= findOption (cache
, attrVal
[OA_NAME
]);
566 if (cache
->info
[opt
].name
)
567 XML_FATAL ("option %s redefined.", attrVal
[OA_NAME
]);
568 data
->curOption
= opt
;
570 XSTRDUP (cache
->info
[opt
].name
, attrVal
[OA_NAME
]);
572 if (!strcmp (attrVal
[OA_TYPE
], "bool"))
573 cache
->info
[opt
].type
= DRI_BOOL
;
574 else if (!strcmp (attrVal
[OA_TYPE
], "enum"))
575 cache
->info
[opt
].type
= DRI_ENUM
;
576 else if (!strcmp (attrVal
[OA_TYPE
], "int"))
577 cache
->info
[opt
].type
= DRI_INT
;
578 else if (!strcmp (attrVal
[OA_TYPE
], "float"))
579 cache
->info
[opt
].type
= DRI_FLOAT
;
581 XML_FATAL ("illegal type in option: %s.", attrVal
[OA_TYPE
]);
583 defaultVal
= getenv (cache
->info
[opt
].name
);
584 if (defaultVal
!= NULL
) {
585 /* don't use XML_WARNING, we want the user to see this! */
587 "ATTENTION: default value of option %s overridden by environment.\n",
588 cache
->info
[opt
].name
);
590 defaultVal
= attrVal
[OA_DEFAULT
];
591 if (!parseValue (&cache
->values
[opt
], cache
->info
[opt
].type
, defaultVal
))
592 XML_FATAL ("illegal default value for %s: %s.", cache
->info
[opt
].name
, defaultVal
);
594 if (attrVal
[OA_VALID
]) {
595 if (cache
->info
[opt
].type
== DRI_BOOL
)
596 XML_FATAL1 ("boolean option with valid attribute.");
597 if (!parseRanges (&cache
->info
[opt
], attrVal
[OA_VALID
]))
598 XML_FATAL ("illegal valid attribute: %s.", attrVal
[OA_VALID
]);
599 if (!checkValue (&cache
->values
[opt
], &cache
->info
[opt
]))
600 XML_FATAL ("default value out of valid range '%s': %s.",
601 attrVal
[OA_VALID
], defaultVal
);
602 } else if (cache
->info
[opt
].type
== DRI_ENUM
) {
603 XML_FATAL1 ("valid attribute missing in option (mandatory for enums).");
605 cache
->info
[opt
].nRanges
= 0;
606 cache
->info
[opt
].ranges
= NULL
;
610 /** \brief Handler for start element events. */
611 static void optInfoStartElem (void *userData
, const XML_Char
*name
,
612 const XML_Char
**attr
) {
613 struct OptInfoData
*data
= (struct OptInfoData
*)userData
;
614 enum OptInfoElem elem
= bsearchStr (name
, OptInfoElems
, OI_COUNT
);
618 XML_FATAL1 ("nested <driinfo> elements.");
620 XML_FATAL1 ("attributes specified on <driinfo> element.");
621 data
->inDriInfo
= GL_TRUE
;
624 if (!data
->inDriInfo
)
625 XML_FATAL1 ("<section> must be inside <driinfo>.");
627 XML_FATAL1 ("nested <section> elements.");
629 XML_FATAL1 ("attributes specified on <section> element.");
630 data
->inSection
= GL_TRUE
;
633 if (!data
->inSection
&& !data
->inOption
)
634 XML_FATAL1 ("<description> must be inside <description> or <option.");
636 XML_FATAL1 ("nested <description> elements.");
637 data
->inDesc
= GL_TRUE
;
638 parseDescAttr (data
, attr
);
641 if (!data
->inSection
)
642 XML_FATAL1 ("<option> must be inside <section>.");
644 XML_FATAL1 ("<option> nested in <description> element.");
646 XML_FATAL1 ("nested <option> elements.");
647 data
->inOption
= GL_TRUE
;
648 parseOptInfoAttr (data
, attr
);
651 if (!(data
->inOption
&& data
->inDesc
))
652 XML_FATAL1 ("<enum> must be inside <option> and <description>.");
654 XML_FATAL1 ("nested <enum> elements.");
655 data
->inEnum
= GL_TRUE
;
656 parseEnumAttr (data
, attr
);
659 XML_FATAL ("unknown element: %s.", name
);
663 /** \brief Handler for end element events. */
664 static void optInfoEndElem (void *userData
, const XML_Char
*name
) {
665 struct OptInfoData
*data
= (struct OptInfoData
*)userData
;
666 enum OptInfoElem elem
= bsearchStr (name
, OptInfoElems
, OI_COUNT
);
669 data
->inDriInfo
= GL_FALSE
;
672 data
->inSection
= GL_FALSE
;
675 data
->inDesc
= GL_FALSE
;
678 data
->inOption
= GL_FALSE
;
681 data
->inEnum
= GL_FALSE
;
684 assert (0); /* should have been caught by StartElem */
688 void driParseOptionInfo (driOptionCache
*info
,
689 const char *configOptions
, GLuint nConfigOptions
) {
692 struct OptInfoData userData
;
693 struct OptInfoData
*data
= &userData
;
696 /* determine hash table size and allocate memory:
697 * 3/2 of the number of options, rounded up, so there remains always
698 * at least one free entry. This is needed for detecting undefined
699 * options in configuration files without getting a hash table overflow.
700 * Round this up to a power of two. */
701 GLuint minSize
= (nConfigOptions
*3 + 1) / 2;
702 GLuint size
, log2size
;
703 for (size
= 1, log2size
= 0; size
< minSize
; size
<<= 1, ++log2size
);
704 info
->tableSize
= log2size
;
705 info
->info
= calloc(size
, sizeof (driOptionInfo
));
706 info
->values
= calloc(size
, sizeof (driOptionValue
));
707 if (info
->info
== NULL
|| info
->values
== NULL
) {
708 fprintf (stderr
, "%s: %d: out of memory.\n", __FILE__
, __LINE__
);
712 p
= XML_ParserCreate ("UTF-8"); /* always UTF-8 */
713 XML_SetElementHandler (p
, optInfoStartElem
, optInfoEndElem
);
714 XML_SetUserData (p
, data
);
716 userData
.name
= "__driConfigOptions";
718 userData
.cache
= info
;
719 userData
.inDriInfo
= GL_FALSE
;
720 userData
.inSection
= GL_FALSE
;
721 userData
.inDesc
= GL_FALSE
;
722 userData
.inOption
= GL_FALSE
;
723 userData
.inEnum
= GL_FALSE
;
724 userData
.curOption
= -1;
726 status
= XML_Parse (p
, configOptions
, strlen (configOptions
), 1);
728 XML_FATAL ("%s.", XML_ErrorString(XML_GetErrorCode(p
)));
732 /* Check if the actual number of options matches nConfigOptions.
733 * A mismatch is not fatal (a hash table overflow would be) but we
734 * want the driver developer's attention anyway. */
735 realNoptions
= countOptions (info
);
736 if (realNoptions
!= nConfigOptions
) {
738 "Error: nConfigOptions (%u) does not match the actual number of options in\n"
739 " __driConfigOptions (%u).\n",
740 nConfigOptions
, realNoptions
);
744 /** \brief Parser context for configuration files. */
748 driOptionCache
*cache
;
750 const char *driverName
, *execName
;
751 GLuint ignoringDevice
;
759 /** \brief Elements in configuration files. */
761 OC_APPLICATION
= 0, OC_DEVICE
, OC_DRICONF
, OC_OPTION
, OC_COUNT
763 static const XML_Char
*OptConfElems
[] = {
764 "application", "device", "driconf", "option"
767 /** \brief Parse attributes of a device element. */
768 static void parseDeviceAttr (struct OptConfData
*data
, const XML_Char
**attr
) {
770 const XML_Char
*driver
= NULL
, *screen
= NULL
;
771 for (i
= 0; attr
[i
]; i
+= 2) {
772 if (!strcmp (attr
[i
], "driver")) driver
= attr
[i
+1];
773 else if (!strcmp (attr
[i
], "screen")) screen
= attr
[i
+1];
774 else XML_WARNING("unknown device attribute: %s.", attr
[i
]);
776 if (driver
&& strcmp (driver
, data
->driverName
))
777 data
->ignoringDevice
= data
->inDevice
;
779 driOptionValue screenNum
;
780 if (!parseValue (&screenNum
, DRI_INT
, screen
))
781 XML_WARNING("illegal screen number: %s.", screen
);
782 else if (screenNum
._int
!= data
->screenNum
)
783 data
->ignoringDevice
= data
->inDevice
;
787 /** \brief Parse attributes of an application element. */
788 static void parseAppAttr (struct OptConfData
*data
, const XML_Char
**attr
) {
790 const XML_Char
*exec
= NULL
;
791 for (i
= 0; attr
[i
]; i
+= 2) {
792 if (!strcmp (attr
[i
], "name")) /* not needed here */;
793 else if (!strcmp (attr
[i
], "executable")) exec
= attr
[i
+1];
794 else XML_WARNING("unknown application attribute: %s.", attr
[i
]);
796 if (exec
&& strcmp (exec
, data
->execName
))
797 data
->ignoringApp
= data
->inApp
;
800 /** \brief Parse attributes of an option element. */
801 static void parseOptConfAttr (struct OptConfData
*data
, const XML_Char
**attr
) {
803 const XML_Char
*name
= NULL
, *value
= NULL
;
804 for (i
= 0; attr
[i
]; i
+= 2) {
805 if (!strcmp (attr
[i
], "name")) name
= attr
[i
+1];
806 else if (!strcmp (attr
[i
], "value")) value
= attr
[i
+1];
807 else XML_WARNING("unknown option attribute: %s.", attr
[i
]);
809 if (!name
) XML_WARNING1 ("name attribute missing in option.");
810 if (!value
) XML_WARNING1 ("value attribute missing in option.");
812 driOptionCache
*cache
= data
->cache
;
813 GLuint opt
= findOption (cache
, name
);
814 if (cache
->info
[opt
].name
== NULL
)
815 XML_WARNING ("undefined option: %s.", name
);
816 else if (getenv (cache
->info
[opt
].name
))
817 /* don't use XML_WARNING, we want the user to see this! */
818 fprintf (stderr
, "ATTENTION: option value of option %s ignored.\n",
819 cache
->info
[opt
].name
);
820 else if (!parseValue (&cache
->values
[opt
], cache
->info
[opt
].type
, value
))
821 XML_WARNING ("illegal option value: %s.", value
);
825 /** \brief Handler for start element events. */
826 static void optConfStartElem (void *userData
, const XML_Char
*name
,
827 const XML_Char
**attr
) {
828 struct OptConfData
*data
= (struct OptConfData
*)userData
;
829 enum OptConfElem elem
= bsearchStr (name
, OptConfElems
, OC_COUNT
);
833 XML_WARNING1 ("nested <driconf> elements.");
835 XML_WARNING1 ("attributes specified on <driconf> element.");
839 if (!data
->inDriConf
)
840 XML_WARNING1 ("<device> should be inside <driconf>.");
842 XML_WARNING1 ("nested <device> elements.");
844 if (!data
->ignoringDevice
&& !data
->ignoringApp
)
845 parseDeviceAttr (data
, attr
);
849 XML_WARNING1 ("<application> should be inside <device>.");
851 XML_WARNING1 ("nested <application> elements.");
853 if (!data
->ignoringDevice
&& !data
->ignoringApp
)
854 parseAppAttr (data
, attr
);
858 XML_WARNING1 ("<option> should be inside <application>.");
860 XML_WARNING1 ("nested <option> elements.");
862 if (!data
->ignoringDevice
&& !data
->ignoringApp
)
863 parseOptConfAttr (data
, attr
);
866 XML_WARNING ("unknown element: %s.", name
);
870 /** \brief Handler for end element events. */
871 static void optConfEndElem (void *userData
, const XML_Char
*name
) {
872 struct OptConfData
*data
= (struct OptConfData
*)userData
;
873 enum OptConfElem elem
= bsearchStr (name
, OptConfElems
, OC_COUNT
);
879 if (data
->inDevice
-- == data
->ignoringDevice
)
880 data
->ignoringDevice
= 0;
883 if (data
->inApp
-- == data
->ignoringApp
)
884 data
->ignoringApp
= 0;
890 /* unknown element, warning was produced on start tag */;
894 /** \brief Initialize an option cache based on info */
895 static void initOptionCache (driOptionCache
*cache
, const driOptionCache
*info
) {
896 cache
->info
= info
->info
;
897 cache
->tableSize
= info
->tableSize
;
898 cache
->values
= malloc((1<<info
->tableSize
) * sizeof (driOptionValue
));
899 if (cache
->values
== NULL
) {
900 fprintf (stderr
, "%s: %d: out of memory.\n", __FILE__
, __LINE__
);
903 memcpy (cache
->values
, info
->values
,
904 (1<<info
->tableSize
) * sizeof (driOptionValue
));
907 /** \brief Parse the named configuration file */
908 static void parseOneConfigFile (XML_Parser p
) {
909 #define BUF_SIZE 0x1000
910 struct OptConfData
*data
= (struct OptConfData
*)XML_GetUserData (p
);
914 if ((fd
= open (data
->name
, O_RDONLY
)) == -1) {
915 __driUtilMessage ("Can't open configuration file %s: %s.",
916 data
->name
, strerror (errno
));
922 void *buffer
= XML_GetBuffer (p
, BUF_SIZE
);
924 __driUtilMessage ("Can't allocate parser buffer.");
927 bytesRead
= read (fd
, buffer
, BUF_SIZE
);
928 if (bytesRead
== -1) {
929 __driUtilMessage ("Error reading from configuration file %s: %s.",
930 data
->name
, strerror (errno
));
933 status
= XML_ParseBuffer (p
, bytesRead
, bytesRead
== 0);
935 XML_ERROR ("%s.", XML_ErrorString(XML_GetErrorCode(p
)));
946 void driParseConfigFiles (driOptionCache
*cache
, const driOptionCache
*info
,
947 GLint screenNum
, const char *driverName
) {
948 char *filenames
[2] = {"/etc/drirc", NULL
};
951 struct OptConfData userData
;
953 initOptionCache (cache
, info
);
955 userData
.cache
= cache
;
956 userData
.screenNum
= screenNum
;
957 userData
.driverName
= driverName
;
958 userData
.execName
= GET_PROGRAM_NAME();
960 if ((home
= getenv ("HOME"))) {
961 GLuint len
= strlen (home
);
962 filenames
[1] = malloc(len
+ 7+1);
963 if (filenames
[1] == NULL
)
964 __driUtilMessage ("Can't allocate memory for %s/.drirc.", home
);
966 memcpy (filenames
[1], home
, len
);
967 memcpy (filenames
[1] + len
, "/.drirc", 7+1);
971 for (i
= 0; i
< 2; ++i
) {
973 if (filenames
[i
] == NULL
)
976 p
= XML_ParserCreate (NULL
); /* use encoding specified by file */
977 XML_SetElementHandler (p
, optConfStartElem
, optConfEndElem
);
978 XML_SetUserData (p
, &userData
);
980 userData
.name
= filenames
[i
];
981 userData
.ignoringDevice
= 0;
982 userData
.ignoringApp
= 0;
983 userData
.inDriConf
= 0;
984 userData
.inDevice
= 0;
986 userData
.inOption
= 0;
988 parseOneConfigFile (p
);
995 void driDestroyOptionInfo (driOptionCache
*info
) {
996 driDestroyOptionCache (info
);
998 GLuint i
, size
= 1 << info
->tableSize
;
999 for (i
= 0; i
< size
; ++i
) {
1000 if (info
->info
[i
].name
) {
1001 free(info
->info
[i
].name
);
1002 free(info
->info
[i
].ranges
);
1009 void driDestroyOptionCache (driOptionCache
*cache
) {
1010 free(cache
->values
);
1013 GLboolean
driCheckOption (const driOptionCache
*cache
, const char *name
,
1014 driOptionType type
) {
1015 GLuint i
= findOption (cache
, name
);
1016 return cache
->info
[i
].name
!= NULL
&& cache
->info
[i
].type
== type
;
1019 GLboolean
driQueryOptionb (const driOptionCache
*cache
, const char *name
) {
1020 GLuint i
= findOption (cache
, name
);
1021 /* make sure the option is defined and has the correct type */
1022 assert (cache
->info
[i
].name
!= NULL
);
1023 assert (cache
->info
[i
].type
== DRI_BOOL
);
1024 return cache
->values
[i
]._bool
;
1027 GLint
driQueryOptioni (const driOptionCache
*cache
, const char *name
) {
1028 GLuint i
= findOption (cache
, name
);
1029 /* make sure the option is defined and has the correct type */
1030 assert (cache
->info
[i
].name
!= NULL
);
1031 assert (cache
->info
[i
].type
== DRI_INT
|| cache
->info
[i
].type
== DRI_ENUM
);
1032 return cache
->values
[i
]._int
;
1035 GLfloat
driQueryOptionf (const driOptionCache
*cache
, const char *name
) {
1036 GLuint i
= findOption (cache
, name
);
1037 /* make sure the option is defined and has the correct type */
1038 assert (cache
->info
[i
].name
!= NULL
);
1039 assert (cache
->info
[i
].type
== DRI_FLOAT
);
1040 return cache
->values
[i
]._float
;