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
47 #include "xmlconfig.h"
48 #include "u_process.h"
50 /* For systems like Hurd */
55 /** \brief Find an option in an option cache with the name as key */
57 findOption(const driOptionCache
*cache
, const char *name
)
59 uint32_t len
= strlen (name
);
60 uint32_t size
= 1 << cache
->tableSize
, mask
= size
- 1;
64 /* compute a hash from the variable length name */
65 for (i
= 0, shift
= 0; i
< len
; ++i
, shift
= (shift
+8) & 31)
66 hash
+= (uint32_t)name
[i
] << shift
;
68 hash
= (hash
>> (16-cache
->tableSize
/2)) & mask
;
70 /* this is just the starting point of the linear search for the option */
71 for (i
= 0; i
< size
; ++i
, hash
= (hash
+1) & mask
) {
72 /* if we hit an empty entry then the option is not defined (yet) */
73 if (cache
->info
[hash
].name
== 0)
75 else if (!strcmp (name
, cache
->info
[hash
].name
))
78 /* this assertion fails if the hash table is full */
84 /** \brief Like strdup but using malloc and with error checking. */
85 #define XSTRDUP(dest,source) do { \
86 uint32_t len = strlen (source); \
87 if (!(dest = malloc(len+1))) { \
88 fprintf (stderr, "%s: %d: out of memory.\n", __FILE__, __LINE__); \
91 memcpy (dest, source, len+1); \
94 static int compare (const void *a
, const void *b
) {
95 return strcmp (*(char *const*)a
, *(char *const*)b
);
97 /** \brief Binary search in a string array. */
99 bsearchStr (const XML_Char
*name
, const XML_Char
*elems
[], uint32_t count
)
101 const XML_Char
**found
;
102 found
= bsearch (&name
, elems
, count
, sizeof (XML_Char
*), compare
);
104 return found
- elems
;
109 /** \brief Locale-independent integer parser.
111 * Works similar to strtol. Leading space is NOT skipped. The input
112 * number may have an optional sign. Radix is specified by base. If
113 * base is 0 then decimal is assumed unless the input number is
114 * prefixed by 0x or 0X for hexadecimal or 0 for octal. After
115 * returning tail points to the first character that is not part of
116 * the integer number. If no number was found then tail points to the
117 * start of the input string. */
119 strToI(const XML_Char
*string
, const XML_Char
**tail
, int base
)
121 int radix
= base
== 0 ? 10 : base
;
124 bool numberFound
= false;
125 const XML_Char
*start
= string
;
127 assert (radix
>= 2 && radix
<= 36);
129 if (*string
== '-') {
132 } else if (*string
== '+')
134 if (base
== 0 && *string
== '0') {
136 if (*(string
+1) == 'x' || *(string
+1) == 'X') {
147 if (*string
>= '0' && *string
< '0' + radix
)
148 digit
= *string
- '0';
150 if (*string
>= '0' && *string
<= '9')
151 digit
= *string
- '0';
152 else if (*string
>= 'a' && *string
< 'a' + radix
- 10)
153 digit
= *string
- 'a' + 10;
154 else if (*string
>= 'A' && *string
< 'A' + radix
- 10)
155 digit
= *string
- 'A' + 10;
159 result
= radix
*result
+ digit
;
164 *tail
= numberFound
? string
: start
;
165 return sign
* result
;
168 /** \brief Locale-independent floating-point parser.
170 * Works similar to strtod. Leading space is NOT skipped. The input
171 * number may have an optional sign. '.' is interpreted as decimal
172 * point and may occur at most once. Optionally the number may end in
173 * [eE]<exponent>, where <exponent> is an integer as recognized by
174 * strToI. In that case the result is number * 10^exponent. After
175 * returning tail points to the first character that is not part of
176 * the floating point number. If no number was found then tail points
177 * to the start of the input string.
179 * Uses two passes for maximum accuracy. */
181 strToF(const XML_Char
*string
, const XML_Char
**tail
)
183 int nDigits
= 0, pointPos
, exponent
;
184 float sign
= 1.0f
, result
= 0.0f
, scale
;
185 const XML_Char
*start
= string
, *numStart
;
188 if (*string
== '-') {
191 } else if (*string
== '+')
194 /* first pass: determine position of decimal point, number of
195 * digits, exponent and the end of the number. */
197 while (*string
>= '0' && *string
<= '9') {
202 if (*string
== '.') {
204 while (*string
>= '0' && *string
<= '9') {
210 /* no digits, no number */
215 if (*string
== 'e' || *string
== 'E') {
216 const XML_Char
*expTail
;
217 exponent
= strToI (string
+1, &expTail
, 10);
218 if (expTail
== string
+1)
226 /* scale of the first digit */
227 scale
= sign
* (float)pow (10.0, (double)(pointPos
-1 + exponent
));
229 /* second pass: parse digits */
231 if (*string
!= '.') {
232 assert (*string
>= '0' && *string
<= '9');
233 result
+= scale
* (float)(*string
- '0');
238 } while (nDigits
> 0);
243 /** \brief Parse a value of a given type. */
245 parseValue(driOptionValue
*v
, driOptionType type
, const XML_Char
*string
)
247 const XML_Char
*tail
= NULL
;
248 /* skip leading white-space */
249 string
+= strspn (string
, " \f\n\r\t\v");
252 if (!strcmp (string
, "false")) {
255 } else if (!strcmp (string
, "true")) {
262 case DRI_ENUM
: /* enum is just a special integer */
264 v
->_int
= strToI (string
, &tail
, 0);
267 v
->_float
= strToF (string
, &tail
);
271 v
->_string
= strndup(string
, STRING_CONF_MAXLEN
);
276 return false; /* empty string (or containing only white-space) */
277 /* skip trailing white space */
279 tail
+= strspn (tail
, " \f\n\r\t\v");
281 return false; /* something left over that is not part of value */
286 /** \brief Parse a list of ranges of type info->type. */
288 parseRanges(driOptionInfo
*info
, const XML_Char
*string
)
290 XML_Char
*cp
, *range
;
292 driOptionRange
*ranges
;
294 XSTRDUP (cp
, string
);
295 /* pass 1: determine the number of ranges (number of commas + 1) */
297 for (nRanges
= 1; *range
; ++range
)
301 if ((ranges
= malloc(nRanges
*sizeof(driOptionRange
))) == NULL
) {
302 fprintf (stderr
, "%s: %d: out of memory.\n", __FILE__
, __LINE__
);
306 /* pass 2: parse all ranges into preallocated array */
308 for (i
= 0; i
< nRanges
; ++i
) {
311 end
= strchr (range
, ',');
314 sep
= strchr (range
, ':');
315 if (sep
) { /* non-empty interval */
317 if (!parseValue (&ranges
[i
].start
, info
->type
, range
) ||
318 !parseValue (&ranges
[i
].end
, info
->type
, sep
+1))
320 if (info
->type
== DRI_INT
&&
321 ranges
[i
].start
._int
> ranges
[i
].end
._int
)
323 if (info
->type
== DRI_FLOAT
&&
324 ranges
[i
].start
._float
> ranges
[i
].end
._float
)
326 } else { /* empty interval */
327 if (!parseValue (&ranges
[i
].start
, info
->type
, range
))
329 ranges
[i
].end
= ranges
[i
].start
;
341 assert (range
== NULL
);
343 info
->nRanges
= nRanges
;
344 info
->ranges
= ranges
;
348 /** \brief Check if a value is in one of info->ranges. */
350 checkValue(const driOptionValue
*v
, const driOptionInfo
*info
)
353 assert (info
->type
!= DRI_BOOL
); /* should be caught by the parser */
354 if (info
->nRanges
== 0)
356 switch (info
->type
) {
357 case DRI_ENUM
: /* enum is just a special integer */
359 for (i
= 0; i
< info
->nRanges
; ++i
)
360 if (v
->_int
>= info
->ranges
[i
].start
._int
&&
361 v
->_int
<= info
->ranges
[i
].end
._int
)
365 for (i
= 0; i
< info
->nRanges
; ++i
)
366 if (v
->_float
>= info
->ranges
[i
].start
._float
&&
367 v
->_float
<= info
->ranges
[i
].end
._float
)
373 assert (0); /* should never happen */
379 * Print message to \c stderr if the \c LIBGL_DEBUG environment variable
382 * Is called from the drivers.
384 * \param f \c printf like format string.
387 __driUtilMessage(const char *f
, ...)
390 const char *libgl_debug
;
392 libgl_debug
=getenv("LIBGL_DEBUG");
393 if (libgl_debug
&& !strstr(libgl_debug
, "quiet")) {
394 fprintf(stderr
, "libGL: ");
396 vfprintf(stderr
, f
, args
);
398 fprintf(stderr
, "\n");
402 /** \brief Output a warning message. */
403 #define XML_WARNING1(msg) do {\
404 __driUtilMessage ("Warning in %s line %d, column %d: "msg, data->name, \
405 (int) XML_GetCurrentLineNumber(data->parser), \
406 (int) XML_GetCurrentColumnNumber(data->parser)); \
408 #define XML_WARNING(msg, ...) do { \
409 __driUtilMessage ("Warning in %s line %d, column %d: "msg, data->name, \
410 (int) XML_GetCurrentLineNumber(data->parser), \
411 (int) XML_GetCurrentColumnNumber(data->parser), \
414 /** \brief Output an error message. */
415 #define XML_ERROR1(msg) do { \
416 __driUtilMessage ("Error in %s line %d, column %d: "msg, data->name, \
417 (int) XML_GetCurrentLineNumber(data->parser), \
418 (int) XML_GetCurrentColumnNumber(data->parser)); \
420 #define XML_ERROR(msg, ...) do { \
421 __driUtilMessage ("Error in %s line %d, column %d: "msg, data->name, \
422 (int) XML_GetCurrentLineNumber(data->parser), \
423 (int) XML_GetCurrentColumnNumber(data->parser), \
426 /** \brief Output a fatal error message and abort. */
427 #define XML_FATAL1(msg) do { \
428 fprintf (stderr, "Fatal error in %s line %d, column %d: "msg"\n", \
430 (int) XML_GetCurrentLineNumber(data->parser), \
431 (int) XML_GetCurrentColumnNumber(data->parser)); \
434 #define XML_FATAL(msg, ...) do { \
435 fprintf (stderr, "Fatal error in %s line %d, column %d: "msg"\n", \
437 (int) XML_GetCurrentLineNumber(data->parser), \
438 (int) XML_GetCurrentColumnNumber(data->parser), \
443 /** \brief Parser context for __driConfigOptions. */
447 driOptionCache
*cache
;
456 /** \brief Elements in __driConfigOptions. */
458 OI_DESCRIPTION
= 0, OI_DRIINFO
, OI_ENUM
, OI_OPTION
, OI_SECTION
, OI_COUNT
460 static const XML_Char
*OptInfoElems
[] = {
461 "description", "driinfo", "enum", "option", "section"
464 /** \brief Parse attributes of an enum element.
466 * We're not actually interested in the data. Just make sure this is ok
467 * for external configuration tools.
470 parseEnumAttr(struct OptInfoData
*data
, const XML_Char
**attr
)
473 const XML_Char
*value
= NULL
, *text
= NULL
;
475 uint32_t opt
= data
->curOption
;
476 for (i
= 0; attr
[i
]; i
+= 2) {
477 if (!strcmp (attr
[i
], "value")) value
= attr
[i
+1];
478 else if (!strcmp (attr
[i
], "text")) text
= attr
[i
+1];
479 else XML_FATAL("illegal enum attribute: %s.", attr
[i
]);
481 if (!value
) XML_FATAL1 ("value attribute missing in enum.");
482 if (!text
) XML_FATAL1 ("text attribute missing in enum.");
483 if (!parseValue (&v
, data
->cache
->info
[opt
].type
, value
))
484 XML_FATAL ("illegal enum value: %s.", value
);
485 if (!checkValue (&v
, &data
->cache
->info
[opt
]))
486 XML_FATAL ("enum value out of valid range: %s.", value
);
489 /** \brief Parse attributes of a description element.
491 * We're not actually interested in the data. Just make sure this is ok
492 * for external configuration tools.
495 parseDescAttr(struct OptInfoData
*data
, const XML_Char
**attr
)
498 const XML_Char
*lang
= NULL
, *text
= NULL
;
499 for (i
= 0; attr
[i
]; i
+= 2) {
500 if (!strcmp (attr
[i
], "lang")) lang
= attr
[i
+1];
501 else if (!strcmp (attr
[i
], "text")) text
= attr
[i
+1];
502 else XML_FATAL("illegal description attribute: %s.", attr
[i
]);
504 if (!lang
) XML_FATAL1 ("lang attribute missing in description.");
505 if (!text
) XML_FATAL1 ("text attribute missing in description.");
508 /** \brief Parse attributes of an option element. */
510 parseOptInfoAttr(struct OptInfoData
*data
, const XML_Char
**attr
)
512 enum OptAttr
{OA_DEFAULT
= 0, OA_NAME
, OA_TYPE
, OA_VALID
, OA_COUNT
};
513 static const XML_Char
*optAttr
[] = {"default", "name", "type", "valid"};
514 const XML_Char
*attrVal
[OA_COUNT
] = {NULL
, NULL
, NULL
, NULL
};
515 const char *defaultVal
;
516 driOptionCache
*cache
= data
->cache
;
518 for (i
= 0; attr
[i
]; i
+= 2) {
519 uint32_t attrName
= bsearchStr (attr
[i
], optAttr
, OA_COUNT
);
520 if (attrName
>= OA_COUNT
)
521 XML_FATAL ("illegal option attribute: %s", attr
[i
]);
522 attrVal
[attrName
] = attr
[i
+1];
524 if (!attrVal
[OA_NAME
]) XML_FATAL1 ("name attribute missing in option.");
525 if (!attrVal
[OA_TYPE
]) XML_FATAL1 ("type attribute missing in option.");
526 if (!attrVal
[OA_DEFAULT
]) XML_FATAL1 ("default attribute missing in option.");
528 opt
= findOption (cache
, attrVal
[OA_NAME
]);
529 if (cache
->info
[opt
].name
)
530 XML_FATAL ("option %s redefined.", attrVal
[OA_NAME
]);
531 data
->curOption
= opt
;
533 XSTRDUP (cache
->info
[opt
].name
, attrVal
[OA_NAME
]);
535 if (!strcmp (attrVal
[OA_TYPE
], "bool"))
536 cache
->info
[opt
].type
= DRI_BOOL
;
537 else if (!strcmp (attrVal
[OA_TYPE
], "enum"))
538 cache
->info
[opt
].type
= DRI_ENUM
;
539 else if (!strcmp (attrVal
[OA_TYPE
], "int"))
540 cache
->info
[opt
].type
= DRI_INT
;
541 else if (!strcmp (attrVal
[OA_TYPE
], "float"))
542 cache
->info
[opt
].type
= DRI_FLOAT
;
543 else if (!strcmp (attrVal
[OA_TYPE
], "string"))
544 cache
->info
[opt
].type
= DRI_STRING
;
546 XML_FATAL ("illegal type in option: %s.", attrVal
[OA_TYPE
]);
548 defaultVal
= getenv (cache
->info
[opt
].name
);
549 if (defaultVal
!= NULL
) {
550 /* don't use XML_WARNING, we want the user to see this! */
552 "ATTENTION: default value of option %s overridden by environment.\n",
553 cache
->info
[opt
].name
);
555 defaultVal
= attrVal
[OA_DEFAULT
];
556 if (!parseValue (&cache
->values
[opt
], cache
->info
[opt
].type
, defaultVal
))
557 XML_FATAL ("illegal default value for %s: %s.", cache
->info
[opt
].name
, defaultVal
);
559 if (attrVal
[OA_VALID
]) {
560 if (cache
->info
[opt
].type
== DRI_BOOL
)
561 XML_FATAL1 ("boolean option with valid attribute.");
562 if (!parseRanges (&cache
->info
[opt
], attrVal
[OA_VALID
]))
563 XML_FATAL ("illegal valid attribute: %s.", attrVal
[OA_VALID
]);
564 if (!checkValue (&cache
->values
[opt
], &cache
->info
[opt
]))
565 XML_FATAL ("default value out of valid range '%s': %s.",
566 attrVal
[OA_VALID
], defaultVal
);
567 } else if (cache
->info
[opt
].type
== DRI_ENUM
) {
568 XML_FATAL1 ("valid attribute missing in option (mandatory for enums).");
570 cache
->info
[opt
].nRanges
= 0;
571 cache
->info
[opt
].ranges
= NULL
;
575 /** \brief Handler for start element events. */
577 optInfoStartElem(void *userData
, const XML_Char
*name
, const XML_Char
**attr
)
579 struct OptInfoData
*data
= (struct OptInfoData
*)userData
;
580 enum OptInfoElem elem
= bsearchStr (name
, OptInfoElems
, OI_COUNT
);
584 XML_FATAL1 ("nested <driinfo> elements.");
586 XML_FATAL1 ("attributes specified on <driinfo> element.");
587 data
->inDriInfo
= true;
590 if (!data
->inDriInfo
)
591 XML_FATAL1 ("<section> must be inside <driinfo>.");
593 XML_FATAL1 ("nested <section> elements.");
595 XML_FATAL1 ("attributes specified on <section> element.");
596 data
->inSection
= true;
599 if (!data
->inSection
&& !data
->inOption
)
600 XML_FATAL1 ("<description> must be inside <description> or <option.");
602 XML_FATAL1 ("nested <description> elements.");
604 parseDescAttr (data
, attr
);
607 if (!data
->inSection
)
608 XML_FATAL1 ("<option> must be inside <section>.");
610 XML_FATAL1 ("<option> nested in <description> element.");
612 XML_FATAL1 ("nested <option> elements.");
613 data
->inOption
= true;
614 parseOptInfoAttr (data
, attr
);
617 if (!(data
->inOption
&& data
->inDesc
))
618 XML_FATAL1 ("<enum> must be inside <option> and <description>.");
620 XML_FATAL1 ("nested <enum> elements.");
622 parseEnumAttr (data
, attr
);
625 XML_FATAL ("unknown element: %s.", name
);
629 /** \brief Handler for end element events. */
631 optInfoEndElem(void *userData
, const XML_Char
*name
)
633 struct OptInfoData
*data
= (struct OptInfoData
*)userData
;
634 enum OptInfoElem elem
= bsearchStr (name
, OptInfoElems
, OI_COUNT
);
637 data
->inDriInfo
= false;
640 data
->inSection
= false;
643 data
->inDesc
= false;
646 data
->inOption
= false;
649 data
->inEnum
= false;
652 assert (0); /* should have been caught by StartElem */
657 driParseOptionInfo(driOptionCache
*info
, const char *configOptions
)
661 struct OptInfoData userData
;
662 struct OptInfoData
*data
= &userData
;
664 /* Make the hash table big enough to fit more than the maximum number of
665 * config options we've ever seen in a driver.
668 info
->info
= calloc(1 << info
->tableSize
, sizeof (driOptionInfo
));
669 info
->values
= calloc(1 << info
->tableSize
, sizeof (driOptionValue
));
670 if (info
->info
== NULL
|| info
->values
== NULL
) {
671 fprintf (stderr
, "%s: %d: out of memory.\n", __FILE__
, __LINE__
);
675 p
= XML_ParserCreate ("UTF-8"); /* always UTF-8 */
676 XML_SetElementHandler (p
, optInfoStartElem
, optInfoEndElem
);
677 XML_SetUserData (p
, data
);
679 userData
.name
= "__driConfigOptions";
681 userData
.cache
= info
;
682 userData
.inDriInfo
= false;
683 userData
.inSection
= false;
684 userData
.inDesc
= false;
685 userData
.inOption
= false;
686 userData
.inEnum
= false;
687 userData
.curOption
= -1;
689 status
= XML_Parse (p
, configOptions
, strlen (configOptions
), 1);
691 XML_FATAL ("%s.", XML_ErrorString(XML_GetErrorCode(p
)));
696 /** \brief Parser context for configuration files. */
700 driOptionCache
*cache
;
702 const char *driverName
, *execName
;
703 const char *kernelDriverName
;
704 const char *engineName
;
705 uint32_t engineVersion
;
706 uint32_t ignoringDevice
;
707 uint32_t ignoringApp
;
714 /** \brief Elements in configuration files. */
716 OC_APPLICATION
= 0, OC_DEVICE
, OC_DRICONF
, OC_ENGINE
, OC_OPTION
, OC_COUNT
718 static const XML_Char
*OptConfElems
[] = {
719 [OC_APPLICATION
] = "application",
720 [OC_DEVICE
] = "device",
721 [OC_DRICONF
] = "driconf",
722 [OC_ENGINE
] = "engine",
723 [OC_OPTION
] = "option",
726 /** \brief Parse attributes of a device element. */
728 parseDeviceAttr(struct OptConfData
*data
, const XML_Char
**attr
)
731 const XML_Char
*driver
= NULL
, *screen
= NULL
, *kernel
= NULL
;
732 for (i
= 0; attr
[i
]; i
+= 2) {
733 if (!strcmp (attr
[i
], "driver")) driver
= attr
[i
+1];
734 else if (!strcmp (attr
[i
], "screen")) screen
= attr
[i
+1];
735 else if (!strcmp (attr
[i
], "kernel_driver")) kernel
= attr
[i
+1];
736 else XML_WARNING("unknown device attribute: %s.", attr
[i
]);
738 if (driver
&& strcmp (driver
, data
->driverName
))
739 data
->ignoringDevice
= data
->inDevice
;
740 else if (kernel
&& (!data
->kernelDriverName
|| strcmp (kernel
, data
->kernelDriverName
)))
741 data
->ignoringDevice
= data
->inDevice
;
743 driOptionValue screenNum
;
744 if (!parseValue (&screenNum
, DRI_INT
, screen
))
745 XML_WARNING("illegal screen number: %s.", screen
);
746 else if (screenNum
._int
!= data
->screenNum
)
747 data
->ignoringDevice
= data
->inDevice
;
752 valueInRanges(const driOptionInfo
*info
, uint32_t value
)
756 for (i
= 0; i
< info
->nRanges
; i
++) {
757 if (info
->ranges
[i
].start
._int
<= value
&&
758 info
->ranges
[i
].end
._int
>= value
)
765 /** \brief Parse attributes of an application element. */
767 parseAppAttr(struct OptConfData
*data
, const XML_Char
**attr
)
770 const XML_Char
*exec
= NULL
;
771 for (i
= 0; attr
[i
]; i
+= 2) {
772 if (!strcmp (attr
[i
], "name")) /* not needed here */;
773 else if (!strcmp (attr
[i
], "executable")) exec
= attr
[i
+1];
774 else XML_WARNING("unknown application attribute: %s.", attr
[i
]);
776 if (exec
&& strcmp (exec
, data
->execName
))
777 data
->ignoringApp
= data
->inApp
;
780 /** \brief Parse attributes of an application element. */
782 parseEngineAttr(struct OptConfData
*data
, const XML_Char
**attr
)
785 const XML_Char
*engine_name_match
= NULL
, *engine_versions
= NULL
;
786 driOptionInfo version_ranges
= {
789 for (i
= 0; attr
[i
]; i
+= 2) {
790 if (!strcmp (attr
[i
], "name")) /* not needed here */;
791 else if (!strcmp (attr
[i
], "engine_name_match")) engine_name_match
= attr
[i
+1];
792 else if (!strcmp (attr
[i
], "engine_versions")) engine_versions
= attr
[i
+1];
793 else XML_WARNING("unknown application attribute: %s.", attr
[i
]);
795 if (engine_name_match
) {
798 if (regcomp (&re
, engine_name_match
, REG_EXTENDED
|REG_NOSUB
) == 0) {
799 if (regexec (&re
, data
->engineName
, 0, NULL
, 0) == REG_NOMATCH
)
800 data
->ignoringApp
= data
->inApp
;
803 XML_WARNING ("Invalid engine_name_match=\"%s\".", engine_name_match
);
805 if (engine_versions
) {
806 if (parseRanges (&version_ranges
, engine_versions
) &&
807 !valueInRanges (&version_ranges
, data
->engineVersion
))
808 data
->ignoringApp
= data
->inApp
;
811 free(version_ranges
.ranges
);
814 /** \brief Parse attributes of an option element. */
816 parseOptConfAttr(struct OptConfData
*data
, const XML_Char
**attr
)
819 const XML_Char
*name
= NULL
, *value
= NULL
;
820 for (i
= 0; attr
[i
]; i
+= 2) {
821 if (!strcmp (attr
[i
], "name")) name
= attr
[i
+1];
822 else if (!strcmp (attr
[i
], "value")) value
= attr
[i
+1];
823 else XML_WARNING("unknown option attribute: %s.", attr
[i
]);
825 if (!name
) XML_WARNING1 ("name attribute missing in option.");
826 if (!value
) XML_WARNING1 ("value attribute missing in option.");
828 driOptionCache
*cache
= data
->cache
;
829 uint32_t opt
= findOption (cache
, name
);
830 if (cache
->info
[opt
].name
== NULL
)
831 /* don't use XML_WARNING, drirc defines options for all drivers,
832 * but not all drivers support them */
834 else if (getenv (cache
->info
[opt
].name
))
835 /* don't use XML_WARNING, we want the user to see this! */
836 fprintf (stderr
, "ATTENTION: option value of option %s ignored.\n",
837 cache
->info
[opt
].name
);
838 else if (!parseValue (&cache
->values
[opt
], cache
->info
[opt
].type
, value
))
839 XML_WARNING ("illegal option value: %s.", value
);
843 /** \brief Handler for start element events. */
845 optConfStartElem(void *userData
, const XML_Char
*name
,
846 const XML_Char
**attr
)
848 struct OptConfData
*data
= (struct OptConfData
*)userData
;
849 enum OptConfElem elem
= bsearchStr (name
, OptConfElems
, OC_COUNT
);
853 XML_WARNING1 ("nested <driconf> elements.");
855 XML_WARNING1 ("attributes specified on <driconf> element.");
859 if (!data
->inDriConf
)
860 XML_WARNING1 ("<device> should be inside <driconf>.");
862 XML_WARNING1 ("nested <device> elements.");
864 if (!data
->ignoringDevice
&& !data
->ignoringApp
)
865 parseDeviceAttr (data
, attr
);
869 XML_WARNING1 ("<application> should be inside <device>.");
871 XML_WARNING1 ("nested <application> or <engine> elements.");
873 if (!data
->ignoringDevice
&& !data
->ignoringApp
)
874 parseAppAttr (data
, attr
);
878 XML_WARNING1 ("<engine> should be inside <device>.");
880 XML_WARNING1 ("nested <application> or <engine> elements.");
882 if (!data
->ignoringDevice
&& !data
->ignoringApp
)
883 parseEngineAttr (data
, attr
);
887 XML_WARNING1 ("<option> should be inside <application>.");
889 XML_WARNING1 ("nested <option> elements.");
891 if (!data
->ignoringDevice
&& !data
->ignoringApp
)
892 parseOptConfAttr (data
, attr
);
895 XML_WARNING ("unknown element: %s.", name
);
899 /** \brief Handler for end element events. */
901 optConfEndElem(void *userData
, const XML_Char
*name
)
903 struct OptConfData
*data
= (struct OptConfData
*)userData
;
904 enum OptConfElem elem
= bsearchStr (name
, OptConfElems
, OC_COUNT
);
910 if (data
->inDevice
-- == data
->ignoringDevice
)
911 data
->ignoringDevice
= 0;
915 if (data
->inApp
-- == data
->ignoringApp
)
916 data
->ignoringApp
= 0;
922 /* unknown element, warning was produced on start tag */;
926 /** \brief Initialize an option cache based on info */
928 initOptionCache(driOptionCache
*cache
, const driOptionCache
*info
)
930 unsigned i
, size
= 1 << info
->tableSize
;
931 cache
->info
= info
->info
;
932 cache
->tableSize
= info
->tableSize
;
933 cache
->values
= malloc((1<<info
->tableSize
) * sizeof (driOptionValue
));
934 if (cache
->values
== NULL
) {
935 fprintf (stderr
, "%s: %d: out of memory.\n", __FILE__
, __LINE__
);
938 memcpy (cache
->values
, info
->values
,
939 (1<<info
->tableSize
) * sizeof (driOptionValue
));
940 for (i
= 0; i
< size
; ++i
) {
941 if (cache
->info
[i
].type
== DRI_STRING
)
942 XSTRDUP(cache
->values
[i
]._string
, info
->values
[i
]._string
);
947 _parseOneConfigFile(XML_Parser p
)
949 #define BUF_SIZE 0x1000
950 struct OptConfData
*data
= (struct OptConfData
*)XML_GetUserData (p
);
954 if ((fd
= open (data
->name
, O_RDONLY
)) == -1) {
955 __driUtilMessage ("Can't open configuration file %s: %s.",
956 data
->name
, strerror (errno
));
962 void *buffer
= XML_GetBuffer (p
, BUF_SIZE
);
964 __driUtilMessage ("Can't allocate parser buffer.");
967 bytesRead
= read (fd
, buffer
, BUF_SIZE
);
968 if (bytesRead
== -1) {
969 __driUtilMessage ("Error reading from configuration file %s: %s.",
970 data
->name
, strerror (errno
));
973 status
= XML_ParseBuffer (p
, bytesRead
, bytesRead
== 0);
975 XML_ERROR ("%s.", XML_ErrorString(XML_GetErrorCode(p
)));
986 /** \brief Parse the named configuration file */
988 parseOneConfigFile(struct OptConfData
*data
, const char *filename
)
992 p
= XML_ParserCreate (NULL
); /* use encoding specified by file */
993 XML_SetElementHandler (p
, optConfStartElem
, optConfEndElem
);
994 XML_SetUserData (p
, data
);
996 data
->name
= filename
;
997 data
->ignoringDevice
= 0;
998 data
->ignoringApp
= 0;
1004 _parseOneConfigFile (p
);
1009 scandir_filter(const struct dirent
*ent
)
1011 #ifndef DT_REG /* systems without d_type in dirent results */
1014 if ((lstat(ent
->d_name
, &st
) != 0) ||
1015 (!S_ISREG(st
.st_mode
) && !S_ISLNK(st
.st_mode
)))
1018 if (ent
->d_type
!= DT_REG
&& ent
->d_type
!= DT_LNK
)
1022 if (fnmatch("*.conf", ent
->d_name
, 0))
1028 /** \brief Parse configuration files in a directory */
1030 parseConfigDir(struct OptConfData
*data
, const char *dirname
)
1033 struct dirent
**entries
= NULL
;
1035 count
= scandir(dirname
, &entries
, scandir_filter
, alphasort
);
1039 for (i
= 0; i
< count
; i
++) {
1040 char filename
[PATH_MAX
];
1042 snprintf(filename
, PATH_MAX
, "%s/%s", dirname
, entries
[i
]->d_name
);
1045 parseOneConfigFile(data
, filename
);
1052 #define SYSCONFDIR "/etc"
1056 #define DATADIR "/usr/share"
1060 driParseConfigFiles(driOptionCache
*cache
, const driOptionCache
*info
,
1061 int screenNum
, const char *driverName
,
1062 const char *kernelDriverName
,
1063 const char *engineName
, uint32_t engineVersion
)
1066 struct OptConfData userData
;
1068 initOptionCache (cache
, info
);
1070 userData
.cache
= cache
;
1071 userData
.screenNum
= screenNum
;
1072 userData
.driverName
= driverName
;
1073 userData
.kernelDriverName
= kernelDriverName
;
1074 userData
.engineName
= engineName
? engineName
: "";
1075 userData
.engineVersion
= engineVersion
;
1076 userData
.execName
= util_get_process_name();
1078 parseConfigDir(&userData
, DATADIR
"/drirc.d");
1079 parseOneConfigFile(&userData
, SYSCONFDIR
"/drirc");
1081 if ((home
= getenv ("HOME"))) {
1082 char filename
[PATH_MAX
];
1084 snprintf(filename
, PATH_MAX
, "%s/.drirc", home
);
1085 parseOneConfigFile(&userData
, filename
);
1090 driDestroyOptionInfo(driOptionCache
*info
)
1092 driDestroyOptionCache(info
);
1094 uint32_t i
, size
= 1 << info
->tableSize
;
1095 for (i
= 0; i
< size
; ++i
) {
1096 if (info
->info
[i
].name
) {
1097 free(info
->info
[i
].name
);
1098 free(info
->info
[i
].ranges
);
1106 driDestroyOptionCache(driOptionCache
*cache
)
1109 unsigned i
, size
= 1 << cache
->tableSize
;
1110 for (i
= 0; i
< size
; ++i
) {
1111 if (cache
->info
[i
].type
== DRI_STRING
)
1112 free(cache
->values
[i
]._string
);
1115 free(cache
->values
);
1119 driCheckOption(const driOptionCache
*cache
, const char *name
,
1122 uint32_t i
= findOption (cache
, name
);
1123 return cache
->info
[i
].name
!= NULL
&& cache
->info
[i
].type
== type
;
1127 driQueryOptionb(const driOptionCache
*cache
, const char *name
)
1129 uint32_t i
= findOption (cache
, name
);
1130 /* make sure the option is defined and has the correct type */
1131 assert (cache
->info
[i
].name
!= NULL
);
1132 assert (cache
->info
[i
].type
== DRI_BOOL
);
1133 return cache
->values
[i
]._bool
;
1137 driQueryOptioni(const driOptionCache
*cache
, const char *name
)
1139 uint32_t i
= findOption (cache
, name
);
1140 /* make sure the option is defined and has the correct type */
1141 assert (cache
->info
[i
].name
!= NULL
);
1142 assert (cache
->info
[i
].type
== DRI_INT
|| cache
->info
[i
].type
== DRI_ENUM
);
1143 return cache
->values
[i
]._int
;
1147 driQueryOptionf(const driOptionCache
*cache
, const char *name
)
1149 uint32_t i
= findOption (cache
, name
);
1150 /* make sure the option is defined and has the correct type */
1151 assert (cache
->info
[i
].name
!= NULL
);
1152 assert (cache
->info
[i
].type
== DRI_FLOAT
);
1153 return cache
->values
[i
]._float
;
1157 driQueryOptionstr(const driOptionCache
*cache
, const char *name
)
1159 uint32_t i
= findOption (cache
, name
);
1160 /* make sure the option is defined and has the correct type */
1161 assert (cache
->info
[i
].name
!= NULL
);
1162 assert (cache
->info
[i
].type
== DRI_STRING
);
1163 return cache
->values
[i
]._string
;