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() */
67 # define GET_PROGRAM_NAME() basename(getexecname())
70 #if !defined(GET_PROGRAM_NAME)
71 # if defined(__OpenBSD__) || defined(NetBSD) || defined(__UCLIBC__)
72 /* This is a hack. It's said to work on OpenBSD, NetBSD and GNU.
73 * Rogelio M.Serrano Jr. reported it's also working with UCLIBC. It's
74 * used as a last resort, if there is no documented facility available. */
75 static const char *__getProgramName () {
76 extern const char *__progname
;
77 char * arg
= strrchr(__progname
, '/');
83 # define GET_PROGRAM_NAME() __getProgramName()
85 # define GET_PROGRAM_NAME() ""
86 # warning "Per application configuration won't work with your OS version."
90 /** \brief Find an option in an option cache with the name as key */
91 static GLuint
findOption (const driOptionCache
*cache
, const char *name
) {
92 GLuint len
= strlen (name
);
93 GLuint size
= 1 << cache
->tableSize
, mask
= size
- 1;
97 /* compute a hash from the variable length name */
98 for (i
= 0, shift
= 0; i
< len
; ++i
, shift
= (shift
+8) & 31)
99 hash
+= (GLuint
)name
[i
] << shift
;
101 hash
= (hash
>> (16-cache
->tableSize
/2)) & mask
;
103 /* this is just the starting point of the linear search for the option */
104 for (i
= 0; i
< size
; ++i
, hash
= (hash
+1) & mask
) {
105 /* if we hit an empty entry then the option is not defined (yet) */
106 if (cache
->info
[hash
].name
== 0)
108 else if (!strcmp (name
, cache
->info
[hash
].name
))
111 /* this assertion fails if the hash table is full */
117 /** \brief Count the real number of options in an option cache */
118 static GLuint
countOptions (const driOptionCache
*cache
) {
119 GLuint size
= 1 << cache
->tableSize
;
121 for (i
= 0; i
< size
; ++i
)
122 if (cache
->info
[i
].name
)
127 /** \brief Like strdup but using MALLOC and with error checking. */
128 #define XSTRDUP(dest,source) do { \
129 GLuint len = strlen (source); \
130 if (!(dest = MALLOC (len+1))) { \
131 fprintf (stderr, "%s: %d: out of memory.\n", __FILE__, __LINE__); \
134 memcpy (dest, source, len+1); \
137 static int compare (const void *a
, const void *b
) {
138 return strcmp (*(char *const*)a
, *(char *const*)b
);
140 /** \brief Binary search in a string array. */
141 static GLuint
bsearchStr (const XML_Char
*name
,
142 const XML_Char
*elems
[], GLuint count
) {
143 const XML_Char
**found
;
144 found
= bsearch (&name
, elems
, count
, sizeof (XML_Char
*), compare
);
146 return found
- elems
;
151 /** \brief Locale-independent integer parser.
153 * Works similar to strtol. Leading space is NOT skipped. The input
154 * number may have an optional sign. Radix is specified by base. If
155 * base is 0 then decimal is assumed unless the input number is
156 * prefixed by 0x or 0X for hexadecimal or 0 for octal. After
157 * returning tail points to the first character that is not part of
158 * the integer number. If no number was found then tail points to the
159 * start of the input string. */
160 static GLint
strToI (const XML_Char
*string
, const XML_Char
**tail
, int base
) {
161 GLint radix
= base
== 0 ? 10 : base
;
164 GLboolean numberFound
= GL_FALSE
;
165 const XML_Char
*start
= string
;
167 assert (radix
>= 2 && radix
<= 36);
169 if (*string
== '-') {
172 } else if (*string
== '+')
174 if (base
== 0 && *string
== '0') {
175 numberFound
= GL_TRUE
;
176 if (*(string
+1) == 'x' || *(string
+1) == 'X') {
187 if (*string
>= '0' && *string
< '0' + radix
)
188 digit
= *string
- '0';
190 if (*string
>= '0' && *string
<= '9')
191 digit
= *string
- '0';
192 else if (*string
>= 'a' && *string
< 'a' + radix
- 10)
193 digit
= *string
- 'a' + 10;
194 else if (*string
>= 'A' && *string
< 'A' + radix
- 10)
195 digit
= *string
- 'A' + 10;
198 numberFound
= GL_TRUE
;
199 result
= radix
*result
+ digit
;
204 *tail
= numberFound
? string
: start
;
205 return sign
* result
;
208 /** \brief Locale-independent floating-point parser.
210 * Works similar to strtod. Leading space is NOT skipped. The input
211 * number may have an optional sign. '.' is interpreted as decimal
212 * point and may occur at most once. Optionally the number may end in
213 * [eE]<exponent>, where <exponent> is an integer as recognized by
214 * strToI. In that case the result is number * 10^exponent. After
215 * returning tail points to the first character that is not part of
216 * the floating point number. If no number was found then tail points
217 * to the start of the input string.
219 * Uses two passes for maximum accuracy. */
220 static GLfloat
strToF (const XML_Char
*string
, const XML_Char
**tail
) {
221 GLint nDigits
= 0, pointPos
, exponent
;
222 GLfloat sign
= 1.0f
, result
= 0.0f
, scale
;
223 const XML_Char
*start
= string
, *numStart
;
226 if (*string
== '-') {
229 } else if (*string
== '+')
232 /* first pass: determine position of decimal point, number of
233 * digits, exponent and the end of the number. */
235 while (*string
>= '0' && *string
<= '9') {
240 if (*string
== '.') {
242 while (*string
>= '0' && *string
<= '9') {
248 /* no digits, no number */
253 if (*string
== 'e' || *string
== 'E') {
254 const XML_Char
*expTail
;
255 exponent
= strToI (string
+1, &expTail
, 10);
256 if (expTail
== string
+1)
264 /* scale of the first digit */
265 scale
= sign
* (GLfloat
)pow (10.0, (GLdouble
)(pointPos
-1 + exponent
));
267 /* second pass: parse digits */
269 if (*string
!= '.') {
270 assert (*string
>= '0' && *string
<= '9');
271 result
+= scale
* (GLfloat
)(*string
- '0');
276 } while (nDigits
> 0);
281 /** \brief Parse a value of a given type. */
282 static GLboolean
parseValue (driOptionValue
*v
, driOptionType type
,
283 const XML_Char
*string
) {
284 const XML_Char
*tail
= NULL
;
285 /* skip leading white-space */
286 string
+= strspn (string
, " \f\n\r\t\v");
289 if (!strcmp (string
, "false")) {
292 } else if (!strcmp (string
, "true")) {
299 case DRI_ENUM
: /* enum is just a special integer */
301 v
->_int
= strToI (string
, &tail
, 0);
304 v
->_float
= strToF (string
, &tail
);
309 return GL_FALSE
; /* empty string (or containing only white-space) */
310 /* skip trailing white space */
312 tail
+= strspn (tail
, " \f\n\r\t\v");
314 return GL_FALSE
; /* something left over that is not part of value */
319 /** \brief Parse a list of ranges of type info->type. */
320 static GLboolean
parseRanges (driOptionInfo
*info
, const XML_Char
*string
) {
321 XML_Char
*cp
, *range
;
323 driOptionRange
*ranges
;
325 XSTRDUP (cp
, string
);
326 /* pass 1: determine the number of ranges (number of commas + 1) */
328 for (nRanges
= 1; *range
; ++range
)
332 if ((ranges
= MALLOC (nRanges
*sizeof(driOptionRange
))) == NULL
) {
333 fprintf (stderr
, "%s: %d: out of memory.\n", __FILE__
, __LINE__
);
337 /* pass 2: parse all ranges into preallocated array */
339 for (i
= 0; i
< nRanges
; ++i
) {
342 end
= strchr (range
, ',');
345 sep
= strchr (range
, ':');
346 if (sep
) { /* non-empty interval */
348 if (!parseValue (&ranges
[i
].start
, info
->type
, range
) ||
349 !parseValue (&ranges
[i
].end
, info
->type
, sep
+1))
351 if (info
->type
== DRI_INT
&&
352 ranges
[i
].start
._int
> ranges
[i
].end
._int
)
354 if (info
->type
== DRI_FLOAT
&&
355 ranges
[i
].start
._float
> ranges
[i
].end
._float
)
357 } else { /* empty interval */
358 if (!parseValue (&ranges
[i
].start
, info
->type
, range
))
360 ranges
[i
].end
= ranges
[i
].start
;
372 assert (range
== NULL
);
374 info
->nRanges
= nRanges
;
375 info
->ranges
= ranges
;
379 /** \brief Check if a value is in one of info->ranges. */
380 static GLboolean
checkValue (const driOptionValue
*v
, const driOptionInfo
*info
) {
382 assert (info
->type
!= DRI_BOOL
); /* should be caught by the parser */
383 if (info
->nRanges
== 0)
385 switch (info
->type
) {
386 case DRI_ENUM
: /* enum is just a special integer */
388 for (i
= 0; i
< info
->nRanges
; ++i
)
389 if (v
->_int
>= info
->ranges
[i
].start
._int
&&
390 v
->_int
<= info
->ranges
[i
].end
._int
)
394 for (i
= 0; i
< info
->nRanges
; ++i
)
395 if (v
->_float
>= info
->ranges
[i
].start
._float
&&
396 v
->_float
<= info
->ranges
[i
].end
._float
)
400 assert (0); /* should never happen */
405 /** \brief Output a warning message. */
406 #define XML_WARNING1(msg) do {\
407 __driUtilMessage ("Warning in %s line %d, column %d: "msg, data->name, \
408 (int) XML_GetCurrentLineNumber(data->parser), \
409 (int) XML_GetCurrentColumnNumber(data->parser)); \
411 #define XML_WARNING(msg,args...) do { \
412 __driUtilMessage ("Warning in %s line %d, column %d: "msg, data->name, \
413 (int) XML_GetCurrentLineNumber(data->parser), \
414 (int) XML_GetCurrentColumnNumber(data->parser), \
417 /** \brief Output an error message. */
418 #define XML_ERROR1(msg) do { \
419 __driUtilMessage ("Error in %s line %d, column %d: "msg, data->name, \
420 (int) XML_GetCurrentLineNumber(data->parser), \
421 (int) XML_GetCurrentColumnNumber(data->parser)); \
423 #define XML_ERROR(msg,args...) do { \
424 __driUtilMessage ("Error in %s line %d, column %d: "msg, data->name, \
425 (int) XML_GetCurrentLineNumber(data->parser), \
426 (int) XML_GetCurrentColumnNumber(data->parser), \
429 /** \brief Output a fatal error message and abort. */
430 #define XML_FATAL1(msg) do { \
431 fprintf (stderr, "Fatal error in %s line %d, column %d: "msg"\n", \
433 (int) XML_GetCurrentLineNumber(data->parser), \
434 (int) XML_GetCurrentColumnNumber(data->parser)); \
437 #define XML_FATAL(msg,args...) do { \
438 fprintf (stderr, "Fatal error in %s line %d, column %d: "msg"\n", \
440 (int) XML_GetCurrentLineNumber(data->parser), \
441 (int) XML_GetCurrentColumnNumber(data->parser), \
446 /** \brief Parser context for __driConfigOptions. */
450 driOptionCache
*cache
;
459 /** \brief Elements in __driConfigOptions. */
461 OI_DESCRIPTION
= 0, OI_DRIINFO
, OI_ENUM
, OI_OPTION
, OI_SECTION
, OI_COUNT
463 static const XML_Char
*OptInfoElems
[] = {
464 "description", "driinfo", "enum", "option", "section"
467 /** \brief Parse attributes of an enum element.
469 * We're not actually interested in the data. Just make sure this is ok
470 * for external configuration tools.
472 static void parseEnumAttr (struct OptInfoData
*data
, const XML_Char
**attr
) {
474 const XML_Char
*value
= NULL
, *text
= NULL
;
476 GLuint opt
= data
->curOption
;
477 for (i
= 0; attr
[i
]; i
+= 2) {
478 if (!strcmp (attr
[i
], "value")) value
= attr
[i
+1];
479 else if (!strcmp (attr
[i
], "text")) text
= attr
[i
+1];
480 else XML_FATAL("illegal enum attribute: %s.", attr
[i
]);
482 if (!value
) XML_FATAL1 ("value attribute missing in enum.");
483 if (!text
) XML_FATAL1 ("text attribute missing in enum.");
484 if (!parseValue (&v
, data
->cache
->info
[opt
].type
, value
))
485 XML_FATAL ("illegal enum value: %s.", value
);
486 if (!checkValue (&v
, &data
->cache
->info
[opt
]))
487 XML_FATAL ("enum value out of valid range: %s.", value
);
490 /** \brief Parse attributes of a description element.
492 * We're not actually interested in the data. Just make sure this is ok
493 * for external configuration tools.
495 static void parseDescAttr (struct OptInfoData
*data
, const XML_Char
**attr
) {
497 const XML_Char
*lang
= NULL
, *text
= NULL
;
498 for (i
= 0; attr
[i
]; i
+= 2) {
499 if (!strcmp (attr
[i
], "lang")) lang
= attr
[i
+1];
500 else if (!strcmp (attr
[i
], "text")) text
= attr
[i
+1];
501 else XML_FATAL("illegal description attribute: %s.", attr
[i
]);
503 if (!lang
) XML_FATAL1 ("lang attribute missing in description.");
504 if (!text
) XML_FATAL1 ("text attribute missing in description.");
507 /** \brief Parse attributes of an option element. */
508 static void parseOptInfoAttr (struct OptInfoData
*data
, const XML_Char
**attr
) {
509 enum OptAttr
{OA_DEFAULT
= 0, OA_NAME
, OA_TYPE
, OA_VALID
, OA_COUNT
};
510 static const XML_Char
*optAttr
[] = {"default", "name", "type", "valid"};
511 const XML_Char
*attrVal
[OA_COUNT
] = {NULL
, NULL
, NULL
, NULL
};
512 const char *defaultVal
;
513 driOptionCache
*cache
= data
->cache
;
515 for (i
= 0; attr
[i
]; i
+= 2) {
516 GLuint attrName
= bsearchStr (attr
[i
], optAttr
, OA_COUNT
);
517 if (attrName
>= OA_COUNT
)
518 XML_FATAL ("illegal option attribute: %s", attr
[i
]);
519 attrVal
[attrName
] = attr
[i
+1];
521 if (!attrVal
[OA_NAME
]) XML_FATAL1 ("name attribute missing in option.");
522 if (!attrVal
[OA_TYPE
]) XML_FATAL1 ("type attribute missing in option.");
523 if (!attrVal
[OA_DEFAULT
]) XML_FATAL1 ("default attribute missing in option.");
525 opt
= findOption (cache
, attrVal
[OA_NAME
]);
526 if (cache
->info
[opt
].name
)
527 XML_FATAL ("option %s redefined.", attrVal
[OA_NAME
]);
528 data
->curOption
= opt
;
530 XSTRDUP (cache
->info
[opt
].name
, attrVal
[OA_NAME
]);
532 if (!strcmp (attrVal
[OA_TYPE
], "bool"))
533 cache
->info
[opt
].type
= DRI_BOOL
;
534 else if (!strcmp (attrVal
[OA_TYPE
], "enum"))
535 cache
->info
[opt
].type
= DRI_ENUM
;
536 else if (!strcmp (attrVal
[OA_TYPE
], "int"))
537 cache
->info
[opt
].type
= DRI_INT
;
538 else if (!strcmp (attrVal
[OA_TYPE
], "float"))
539 cache
->info
[opt
].type
= DRI_FLOAT
;
541 XML_FATAL ("illegal type in option: %s.", attrVal
[OA_TYPE
]);
543 defaultVal
= getenv (cache
->info
[opt
].name
);
544 if (defaultVal
!= NULL
) {
545 /* don't use XML_WARNING, we want the user to see this! */
547 "ATTENTION: default value of option %s overridden by environment.\n",
548 cache
->info
[opt
].name
);
550 defaultVal
= attrVal
[OA_DEFAULT
];
551 if (!parseValue (&cache
->values
[opt
], cache
->info
[opt
].type
, defaultVal
))
552 XML_FATAL ("illegal default value: %s.", defaultVal
);
554 if (attrVal
[OA_VALID
]) {
555 if (cache
->info
[opt
].type
== DRI_BOOL
)
556 XML_FATAL1 ("boolean option with valid attribute.");
557 if (!parseRanges (&cache
->info
[opt
], attrVal
[OA_VALID
]))
558 XML_FATAL ("illegal valid attribute: %s.", attrVal
[OA_VALID
]);
559 if (!checkValue (&cache
->values
[opt
], &cache
->info
[opt
]))
560 XML_FATAL ("default value out of valid range '%s': %s.",
561 attrVal
[OA_VALID
], defaultVal
);
562 } else if (cache
->info
[opt
].type
== DRI_ENUM
) {
563 XML_FATAL1 ("valid attribute missing in option (mandatory for enums).");
565 cache
->info
[opt
].nRanges
= 0;
566 cache
->info
[opt
].ranges
= NULL
;
570 /** \brief Handler for start element events. */
571 static void optInfoStartElem (void *userData
, const XML_Char
*name
,
572 const XML_Char
**attr
) {
573 struct OptInfoData
*data
= (struct OptInfoData
*)userData
;
574 enum OptInfoElem elem
= bsearchStr (name
, OptInfoElems
, OI_COUNT
);
578 XML_FATAL1 ("nested <driinfo> elements.");
580 XML_FATAL1 ("attributes specified on <driinfo> element.");
581 data
->inDriInfo
= GL_TRUE
;
584 if (!data
->inDriInfo
)
585 XML_FATAL1 ("<section> must be inside <driinfo>.");
587 XML_FATAL1 ("nested <section> elements.");
589 XML_FATAL1 ("attributes specified on <section> element.");
590 data
->inSection
= GL_TRUE
;
593 if (!data
->inSection
&& !data
->inOption
)
594 XML_FATAL1 ("<description> must be inside <description> or <option.");
596 XML_FATAL1 ("nested <description> elements.");
597 data
->inDesc
= GL_TRUE
;
598 parseDescAttr (data
, attr
);
601 if (!data
->inSection
)
602 XML_FATAL1 ("<option> must be inside <section>.");
604 XML_FATAL1 ("<option> nested in <description> element.");
606 XML_FATAL1 ("nested <option> elements.");
607 data
->inOption
= GL_TRUE
;
608 parseOptInfoAttr (data
, attr
);
611 if (!(data
->inOption
&& data
->inDesc
))
612 XML_FATAL1 ("<enum> must be inside <option> and <description>.");
614 XML_FATAL1 ("nested <enum> elements.");
615 data
->inEnum
= GL_TRUE
;
616 parseEnumAttr (data
, attr
);
619 XML_FATAL ("unknown element: %s.", name
);
623 /** \brief Handler for end element events. */
624 static void optInfoEndElem (void *userData
, const XML_Char
*name
) {
625 struct OptInfoData
*data
= (struct OptInfoData
*)userData
;
626 enum OptInfoElem elem
= bsearchStr (name
, OptInfoElems
, OI_COUNT
);
629 data
->inDriInfo
= GL_FALSE
;
632 data
->inSection
= GL_FALSE
;
635 data
->inDesc
= GL_FALSE
;
638 data
->inOption
= GL_FALSE
;
641 data
->inEnum
= GL_FALSE
;
644 assert (0); /* should have been caught by StartElem */
648 void driParseOptionInfo (driOptionCache
*info
,
649 const char *configOptions
, GLuint nConfigOptions
) {
652 struct OptInfoData userData
;
653 struct OptInfoData
*data
= &userData
;
656 /* determine hash table size and allocate memory:
657 * 3/2 of the number of options, rounded up, so there remains always
658 * at least one free entry. This is needed for detecting undefined
659 * options in configuration files without getting a hash table overflow.
660 * Round this up to a power of two. */
661 GLuint minSize
= (nConfigOptions
*3 + 1) / 2;
662 GLuint size
, log2size
;
663 for (size
= 1, log2size
= 0; size
< minSize
; size
<<= 1, ++log2size
);
664 info
->tableSize
= log2size
;
665 info
->info
= CALLOC (size
* sizeof (driOptionInfo
));
666 info
->values
= CALLOC (size
* sizeof (driOptionValue
));
667 if (info
->info
== NULL
|| info
->values
== NULL
) {
668 fprintf (stderr
, "%s: %d: out of memory.\n", __FILE__
, __LINE__
);
672 p
= XML_ParserCreate ("UTF-8"); /* always UTF-8 */
673 XML_SetElementHandler (p
, optInfoStartElem
, optInfoEndElem
);
674 XML_SetUserData (p
, data
);
676 userData
.name
= "__driConfigOptions";
678 userData
.cache
= info
;
679 userData
.inDriInfo
= GL_FALSE
;
680 userData
.inSection
= GL_FALSE
;
681 userData
.inDesc
= GL_FALSE
;
682 userData
.inOption
= GL_FALSE
;
683 userData
.inEnum
= GL_FALSE
;
684 userData
.curOption
= -1;
686 status
= XML_Parse (p
, configOptions
, strlen (configOptions
), 1);
688 XML_FATAL ("%s.", XML_ErrorString(XML_GetErrorCode(p
)));
692 /* Check if the actual number of options matches nConfigOptions.
693 * A mismatch is not fatal (a hash table overflow would be) but we
694 * want the driver developer's attention anyway. */
695 realNoptions
= countOptions (info
);
696 if (realNoptions
!= nConfigOptions
) {
698 "Error: nConfigOptions (%u) does not match the actual number of options in\n"
699 " __driConfigOptions (%u).\n",
700 nConfigOptions
, realNoptions
);
704 /** \brief Parser context for configuration files. */
708 driOptionCache
*cache
;
710 const char *driverName
, *execName
;
711 GLuint ignoringDevice
;
719 /** \brief Elements in configuration files. */
721 OC_APPLICATION
= 0, OC_DEVICE
, OC_DRICONF
, OC_OPTION
, OC_COUNT
723 static const XML_Char
*OptConfElems
[] = {
724 "application", "device", "driconf", "option"
727 /** \brief Parse attributes of a device element. */
728 static void parseDeviceAttr (struct OptConfData
*data
, const XML_Char
**attr
) {
730 const XML_Char
*driver
= NULL
, *screen
= NULL
;
731 for (i
= 0; attr
[i
]; i
+= 2) {
732 if (!strcmp (attr
[i
], "driver")) driver
= attr
[i
+1];
733 else if (!strcmp (attr
[i
], "screen")) screen
= attr
[i
+1];
734 else XML_WARNING("unknown device attribute: %s.", attr
[i
]);
736 if (driver
&& strcmp (driver
, data
->driverName
))
737 data
->ignoringDevice
= data
->inDevice
;
739 driOptionValue screenNum
;
740 if (!parseValue (&screenNum
, DRI_INT
, screen
))
741 XML_WARNING("illegal screen number: %s.", screen
);
742 else if (screenNum
._int
!= data
->screenNum
)
743 data
->ignoringDevice
= data
->inDevice
;
747 /** \brief Parse attributes of an application element. */
748 static void parseAppAttr (struct OptConfData
*data
, const XML_Char
**attr
) {
750 const XML_Char
*name
= NULL
, *exec
= NULL
;
751 for (i
= 0; attr
[i
]; i
+= 2) {
752 if (!strcmp (attr
[i
], "name")) name
= attr
[i
+1];
753 else if (!strcmp (attr
[i
], "executable")) exec
= attr
[i
+1];
754 else XML_WARNING("unknown application attribute: %s.", attr
[i
]);
756 if (exec
&& strcmp (exec
, data
->execName
))
757 data
->ignoringApp
= data
->inApp
;
760 /** \brief Parse attributes of an option element. */
761 static void parseOptConfAttr (struct OptConfData
*data
, const XML_Char
**attr
) {
763 const XML_Char
*name
= NULL
, *value
= NULL
;
764 for (i
= 0; attr
[i
]; i
+= 2) {
765 if (!strcmp (attr
[i
], "name")) name
= attr
[i
+1];
766 else if (!strcmp (attr
[i
], "value")) value
= attr
[i
+1];
767 else XML_WARNING("unknown option attribute: %s.", attr
[i
]);
769 if (!name
) XML_WARNING1 ("name attribute missing in option.");
770 if (!value
) XML_WARNING1 ("value attribute missing in option.");
772 driOptionCache
*cache
= data
->cache
;
773 GLuint opt
= findOption (cache
, name
);
774 if (cache
->info
[opt
].name
== NULL
)
775 XML_WARNING ("undefined option: %s.", name
);
776 else if (getenv (cache
->info
[opt
].name
))
777 /* don't use XML_WARNING, we want the user to see this! */
778 fprintf (stderr
, "ATTENTION: option value of option %s ignored.\n",
779 cache
->info
[opt
].name
);
780 else if (!parseValue (&cache
->values
[opt
], cache
->info
[opt
].type
, value
))
781 XML_WARNING ("illegal option value: %s.", value
);
785 /** \brief Handler for start element events. */
786 static void optConfStartElem (void *userData
, const XML_Char
*name
,
787 const XML_Char
**attr
) {
788 struct OptConfData
*data
= (struct OptConfData
*)userData
;
789 enum OptConfElem elem
= bsearchStr (name
, OptConfElems
, OC_COUNT
);
793 XML_WARNING1 ("nested <driconf> elements.");
795 XML_WARNING1 ("attributes specified on <driconf> element.");
799 if (!data
->inDriConf
)
800 XML_WARNING1 ("<device> should be inside <driconf>.");
802 XML_WARNING1 ("nested <device> elements.");
804 if (!data
->ignoringDevice
&& !data
->ignoringApp
)
805 parseDeviceAttr (data
, attr
);
809 XML_WARNING1 ("<application> should be inside <device>.");
811 XML_WARNING1 ("nested <application> elements.");
813 if (!data
->ignoringDevice
&& !data
->ignoringApp
)
814 parseAppAttr (data
, attr
);
818 XML_WARNING1 ("<option> should be inside <application>.");
820 XML_WARNING1 ("nested <option> elements.");
822 if (!data
->ignoringDevice
&& !data
->ignoringApp
)
823 parseOptConfAttr (data
, attr
);
826 XML_WARNING ("unknown element: %s.", name
);
830 /** \brief Handler for end element events. */
831 static void optConfEndElem (void *userData
, const XML_Char
*name
) {
832 struct OptConfData
*data
= (struct OptConfData
*)userData
;
833 enum OptConfElem elem
= bsearchStr (name
, OptConfElems
, OC_COUNT
);
839 if (data
->inDevice
-- == data
->ignoringDevice
)
840 data
->ignoringDevice
= 0;
843 if (data
->inApp
-- == data
->ignoringApp
)
844 data
->ignoringApp
= 0;
850 /* unknown element, warning was produced on start tag */;
854 /** \brief Initialize an option cache based on info */
855 static void initOptionCache (driOptionCache
*cache
, const driOptionCache
*info
) {
856 cache
->info
= info
->info
;
857 cache
->tableSize
= info
->tableSize
;
858 cache
->values
= MALLOC ((1<<info
->tableSize
) * sizeof (driOptionValue
));
859 if (cache
->values
== NULL
) {
860 fprintf (stderr
, "%s: %d: out of memory.\n", __FILE__
, __LINE__
);
863 memcpy (cache
->values
, info
->values
,
864 (1<<info
->tableSize
) * sizeof (driOptionValue
));
867 /** \brief Parse the named configuration file */
868 static void parseOneConfigFile (XML_Parser p
) {
869 #define BUF_SIZE 0x1000
870 struct OptConfData
*data
= (struct OptConfData
*)XML_GetUserData (p
);
874 if ((fd
= open (data
->name
, O_RDONLY
)) == -1) {
875 __driUtilMessage ("Can't open configuration file %s: %s.",
876 data
->name
, strerror (errno
));
882 void *buffer
= XML_GetBuffer (p
, BUF_SIZE
);
884 __driUtilMessage ("Can't allocate parser buffer.");
887 bytesRead
= read (fd
, buffer
, BUF_SIZE
);
888 if (bytesRead
== -1) {
889 __driUtilMessage ("Error reading from configuration file %s: %s.",
890 data
->name
, strerror (errno
));
893 status
= XML_ParseBuffer (p
, bytesRead
, bytesRead
== 0);
895 XML_ERROR ("%s.", XML_ErrorString(XML_GetErrorCode(p
)));
906 void driParseConfigFiles (driOptionCache
*cache
, const driOptionCache
*info
,
907 GLint screenNum
, const char *driverName
) {
908 char *filenames
[2] = {"/etc/drirc", NULL
};
911 struct OptConfData userData
;
913 initOptionCache (cache
, info
);
915 userData
.cache
= cache
;
916 userData
.screenNum
= screenNum
;
917 userData
.driverName
= driverName
;
918 userData
.execName
= GET_PROGRAM_NAME();
920 if ((home
= getenv ("HOME"))) {
921 GLuint len
= strlen (home
);
922 filenames
[1] = MALLOC (len
+ 7+1);
923 if (filenames
[1] == NULL
)
924 __driUtilMessage ("Can't allocate memory for %s/.drirc.", home
);
926 memcpy (filenames
[1], home
, len
);
927 memcpy (filenames
[1] + len
, "/.drirc", 7+1);
931 for (i
= 0; i
< 2; ++i
) {
933 if (filenames
[i
] == NULL
)
936 p
= XML_ParserCreate (NULL
); /* use encoding specified by file */
937 XML_SetElementHandler (p
, optConfStartElem
, optConfEndElem
);
938 XML_SetUserData (p
, &userData
);
940 userData
.name
= filenames
[i
];
941 userData
.ignoringDevice
= 0;
942 userData
.ignoringApp
= 0;
943 userData
.inDriConf
= 0;
944 userData
.inDevice
= 0;
946 userData
.inOption
= 0;
948 parseOneConfigFile (p
);
956 void driDestroyOptionInfo (driOptionCache
*info
) {
957 driDestroyOptionCache (info
);
959 GLuint i
, size
= 1 << info
->tableSize
;
960 for (i
= 0; i
< size
; ++i
) {
961 if (info
->info
[i
].name
) {
962 FREE (info
->info
[i
].name
);
963 if (info
->info
[i
].ranges
)
964 FREE (info
->info
[i
].ranges
);
971 void driDestroyOptionCache (driOptionCache
*cache
) {
973 FREE (cache
->values
);
976 GLboolean
driCheckOption (const driOptionCache
*cache
, const char *name
,
977 driOptionType type
) {
978 GLuint i
= findOption (cache
, name
);
979 return cache
->info
[i
].name
!= NULL
&& cache
->info
[i
].type
== type
;
982 GLboolean
driQueryOptionb (const driOptionCache
*cache
, const char *name
) {
983 GLuint i
= findOption (cache
, name
);
984 /* make sure the option is defined and has the correct type */
985 assert (cache
->info
[i
].name
!= NULL
);
986 assert (cache
->info
[i
].type
== DRI_BOOL
);
987 return cache
->values
[i
]._bool
;
990 GLint
driQueryOptioni (const driOptionCache
*cache
, const char *name
) {
991 GLuint i
= findOption (cache
, name
);
992 /* make sure the option is defined and has the correct type */
993 assert (cache
->info
[i
].name
!= NULL
);
994 assert (cache
->info
[i
].type
== DRI_INT
|| cache
->info
[i
].type
== DRI_ENUM
);
995 return cache
->values
[i
]._int
;
998 GLfloat
driQueryOptionf (const driOptionCache
*cache
, const char *name
) {
999 GLuint i
= findOption (cache
, name
);
1000 /* make sure the option is defined and has the correct type */
1001 assert (cache
->info
[i
].name
!= NULL
);
1002 assert (cache
->info
[i
].type
== DRI_FLOAT
);
1003 return cache
->values
[i
]._float
;