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"
51 /* For systems like Hurd */
59 const char *s
= getenv("MESA_DEBUG");
63 return strstr(s
, "silent") == NULL
;
66 /** \brief Find an option in an option cache with the name as key */
68 findOption(const driOptionCache
*cache
, const char *name
)
70 uint32_t len
= strlen (name
);
71 uint32_t size
= 1 << cache
->tableSize
, mask
= size
- 1;
75 /* compute a hash from the variable length name */
76 for (i
= 0, shift
= 0; i
< len
; ++i
, shift
= (shift
+8) & 31)
77 hash
+= (uint32_t)name
[i
] << shift
;
79 hash
= (hash
>> (16-cache
->tableSize
/2)) & mask
;
81 /* this is just the starting point of the linear search for the option */
82 for (i
= 0; i
< size
; ++i
, hash
= (hash
+1) & mask
) {
83 /* if we hit an empty entry then the option is not defined (yet) */
84 if (cache
->info
[hash
].name
== 0)
86 else if (!strcmp (name
, cache
->info
[hash
].name
))
89 /* this assertion fails if the hash table is full */
95 /** \brief Like strdup but using malloc and with error checking. */
96 #define XSTRDUP(dest,source) do { \
97 uint32_t len = strlen (source); \
98 if (!(dest = malloc(len+1))) { \
99 fprintf (stderr, "%s: %d: out of memory.\n", __FILE__, __LINE__); \
102 memcpy (dest, source, len+1); \
105 static int compare (const void *a
, const void *b
) {
106 return strcmp (*(char *const*)a
, *(char *const*)b
);
108 /** \brief Binary search in a string array. */
110 bsearchStr (const XML_Char
*name
, const XML_Char
*elems
[], uint32_t count
)
112 const XML_Char
**found
;
113 found
= bsearch (&name
, elems
, count
, sizeof (XML_Char
*), compare
);
115 return found
- elems
;
120 /** \brief Locale-independent integer parser.
122 * Works similar to strtol. Leading space is NOT skipped. The input
123 * number may have an optional sign. Radix is specified by base. If
124 * base is 0 then decimal is assumed unless the input number is
125 * prefixed by 0x or 0X for hexadecimal or 0 for octal. After
126 * returning tail points to the first character that is not part of
127 * the integer number. If no number was found then tail points to the
128 * start of the input string. */
130 strToI(const XML_Char
*string
, const XML_Char
**tail
, int base
)
132 int radix
= base
== 0 ? 10 : base
;
135 bool numberFound
= false;
136 const XML_Char
*start
= string
;
138 assert (radix
>= 2 && radix
<= 36);
140 if (*string
== '-') {
143 } else if (*string
== '+')
145 if (base
== 0 && *string
== '0') {
147 if (*(string
+1) == 'x' || *(string
+1) == 'X') {
158 if (*string
>= '0' && *string
< '0' + radix
)
159 digit
= *string
- '0';
161 if (*string
>= '0' && *string
<= '9')
162 digit
= *string
- '0';
163 else if (*string
>= 'a' && *string
< 'a' + radix
- 10)
164 digit
= *string
- 'a' + 10;
165 else if (*string
>= 'A' && *string
< 'A' + radix
- 10)
166 digit
= *string
- 'A' + 10;
170 result
= radix
*result
+ digit
;
175 *tail
= numberFound
? string
: start
;
176 return sign
* result
;
179 /** \brief Locale-independent floating-point parser.
181 * Works similar to strtod. Leading space is NOT skipped. The input
182 * number may have an optional sign. '.' is interpreted as decimal
183 * point and may occur at most once. Optionally the number may end in
184 * [eE]<exponent>, where <exponent> is an integer as recognized by
185 * strToI. In that case the result is number * 10^exponent. After
186 * returning tail points to the first character that is not part of
187 * the floating point number. If no number was found then tail points
188 * to the start of the input string.
190 * Uses two passes for maximum accuracy. */
192 strToF(const XML_Char
*string
, const XML_Char
**tail
)
194 int nDigits
= 0, pointPos
, exponent
;
195 float sign
= 1.0f
, result
= 0.0f
, scale
;
196 const XML_Char
*start
= string
, *numStart
;
199 if (*string
== '-') {
202 } else if (*string
== '+')
205 /* first pass: determine position of decimal point, number of
206 * digits, exponent and the end of the number. */
208 while (*string
>= '0' && *string
<= '9') {
213 if (*string
== '.') {
215 while (*string
>= '0' && *string
<= '9') {
221 /* no digits, no number */
226 if (*string
== 'e' || *string
== 'E') {
227 const XML_Char
*expTail
;
228 exponent
= strToI (string
+1, &expTail
, 10);
229 if (expTail
== string
+1)
237 /* scale of the first digit */
238 scale
= sign
* (float)pow (10.0, (double)(pointPos
-1 + exponent
));
240 /* second pass: parse digits */
242 if (*string
!= '.') {
243 assert (*string
>= '0' && *string
<= '9');
244 result
+= scale
* (float)(*string
- '0');
249 } while (nDigits
> 0);
254 /** \brief Parse a value of a given type. */
256 parseValue(driOptionValue
*v
, driOptionType type
, const XML_Char
*string
)
258 const XML_Char
*tail
= NULL
;
259 /* skip leading white-space */
260 string
+= strspn (string
, " \f\n\r\t\v");
263 if (!strcmp (string
, "false")) {
266 } else if (!strcmp (string
, "true")) {
273 case DRI_ENUM
: /* enum is just a special integer */
275 v
->_int
= strToI (string
, &tail
, 0);
278 v
->_float
= strToF (string
, &tail
);
282 v
->_string
= strndup(string
, STRING_CONF_MAXLEN
);
287 return false; /* empty string (or containing only white-space) */
288 /* skip trailing white space */
290 tail
+= strspn (tail
, " \f\n\r\t\v");
292 return false; /* something left over that is not part of value */
297 /** \brief Parse a list of ranges of type info->type. */
299 parseRanges(driOptionInfo
*info
, const XML_Char
*string
)
301 XML_Char
*cp
, *range
;
303 driOptionRange
*ranges
;
305 XSTRDUP (cp
, string
);
306 /* pass 1: determine the number of ranges (number of commas + 1) */
308 for (nRanges
= 1; *range
; ++range
)
312 if ((ranges
= malloc(nRanges
*sizeof(driOptionRange
))) == NULL
) {
313 fprintf (stderr
, "%s: %d: out of memory.\n", __FILE__
, __LINE__
);
317 /* pass 2: parse all ranges into preallocated array */
319 for (i
= 0; i
< nRanges
; ++i
) {
322 end
= strchr (range
, ',');
325 sep
= strchr (range
, ':');
326 if (sep
) { /* non-empty interval */
328 if (!parseValue (&ranges
[i
].start
, info
->type
, range
) ||
329 !parseValue (&ranges
[i
].end
, info
->type
, sep
+1))
331 if (info
->type
== DRI_INT
&&
332 ranges
[i
].start
._int
> ranges
[i
].end
._int
)
334 if (info
->type
== DRI_FLOAT
&&
335 ranges
[i
].start
._float
> ranges
[i
].end
._float
)
337 } else { /* empty interval */
338 if (!parseValue (&ranges
[i
].start
, info
->type
, range
))
340 ranges
[i
].end
= ranges
[i
].start
;
352 assert (range
== NULL
);
354 info
->nRanges
= nRanges
;
355 info
->ranges
= ranges
;
359 /** \brief Check if a value is in one of info->ranges. */
361 checkValue(const driOptionValue
*v
, const driOptionInfo
*info
)
364 assert (info
->type
!= DRI_BOOL
); /* should be caught by the parser */
365 if (info
->nRanges
== 0)
367 switch (info
->type
) {
368 case DRI_ENUM
: /* enum is just a special integer */
370 for (i
= 0; i
< info
->nRanges
; ++i
)
371 if (v
->_int
>= info
->ranges
[i
].start
._int
&&
372 v
->_int
<= info
->ranges
[i
].end
._int
)
376 for (i
= 0; i
< info
->nRanges
; ++i
)
377 if (v
->_float
>= info
->ranges
[i
].start
._float
&&
378 v
->_float
<= info
->ranges
[i
].end
._float
)
384 assert (0); /* should never happen */
390 * Print message to \c stderr if the \c LIBGL_DEBUG environment variable
393 * Is called from the drivers.
395 * \param f \c printf like format string.
398 __driUtilMessage(const char *f
, ...)
401 const char *libgl_debug
;
403 libgl_debug
=getenv("LIBGL_DEBUG");
404 if (libgl_debug
&& !strstr(libgl_debug
, "quiet")) {
405 fprintf(stderr
, "libGL: ");
407 vfprintf(stderr
, f
, args
);
409 fprintf(stderr
, "\n");
413 /** \brief Output a warning message. */
414 #define XML_WARNING1(msg) do {\
415 __driUtilMessage ("Warning in %s line %d, column %d: "msg, data->name, \
416 (int) XML_GetCurrentLineNumber(data->parser), \
417 (int) XML_GetCurrentColumnNumber(data->parser)); \
419 #define XML_WARNING(msg, ...) do { \
420 __driUtilMessage ("Warning in %s line %d, column %d: "msg, data->name, \
421 (int) XML_GetCurrentLineNumber(data->parser), \
422 (int) XML_GetCurrentColumnNumber(data->parser), \
425 /** \brief Output an error message. */
426 #define XML_ERROR1(msg) do { \
427 __driUtilMessage ("Error in %s line %d, column %d: "msg, data->name, \
428 (int) XML_GetCurrentLineNumber(data->parser), \
429 (int) XML_GetCurrentColumnNumber(data->parser)); \
431 #define XML_ERROR(msg, ...) do { \
432 __driUtilMessage ("Error in %s line %d, column %d: "msg, data->name, \
433 (int) XML_GetCurrentLineNumber(data->parser), \
434 (int) XML_GetCurrentColumnNumber(data->parser), \
437 /** \brief Output a fatal error message and abort. */
438 #define XML_FATAL1(msg) do { \
439 fprintf (stderr, "Fatal error in %s line %d, column %d: "msg"\n", \
441 (int) XML_GetCurrentLineNumber(data->parser), \
442 (int) XML_GetCurrentColumnNumber(data->parser)); \
445 #define XML_FATAL(msg, ...) do { \
446 fprintf (stderr, "Fatal error in %s line %d, column %d: "msg"\n", \
448 (int) XML_GetCurrentLineNumber(data->parser), \
449 (int) XML_GetCurrentColumnNumber(data->parser), \
454 /** \brief Parser context for __driConfigOptions. */
458 driOptionCache
*cache
;
467 /** \brief Elements in __driConfigOptions. */
469 OI_DESCRIPTION
= 0, OI_DRIINFO
, OI_ENUM
, OI_OPTION
, OI_SECTION
, OI_COUNT
471 static const XML_Char
*OptInfoElems
[] = {
472 "description", "driinfo", "enum", "option", "section"
475 /** \brief Parse attributes of an enum element.
477 * We're not actually interested in the data. Just make sure this is ok
478 * for external configuration tools.
481 parseEnumAttr(struct OptInfoData
*data
, const XML_Char
**attr
)
484 const XML_Char
*value
= NULL
, *text
= NULL
;
486 uint32_t opt
= data
->curOption
;
487 for (i
= 0; attr
[i
]; i
+= 2) {
488 if (!strcmp (attr
[i
], "value")) value
= attr
[i
+1];
489 else if (!strcmp (attr
[i
], "text")) text
= attr
[i
+1];
490 else XML_FATAL("illegal enum attribute: %s.", attr
[i
]);
492 if (!value
) XML_FATAL1 ("value attribute missing in enum.");
493 if (!text
) XML_FATAL1 ("text attribute missing in enum.");
494 if (!parseValue (&v
, data
->cache
->info
[opt
].type
, value
))
495 XML_FATAL ("illegal enum value: %s.", value
);
496 if (!checkValue (&v
, &data
->cache
->info
[opt
]))
497 XML_FATAL ("enum value out of valid range: %s.", value
);
500 /** \brief Parse attributes of a description element.
502 * We're not actually interested in the data. Just make sure this is ok
503 * for external configuration tools.
506 parseDescAttr(struct OptInfoData
*data
, const XML_Char
**attr
)
509 const XML_Char
*lang
= NULL
, *text
= NULL
;
510 for (i
= 0; attr
[i
]; i
+= 2) {
511 if (!strcmp (attr
[i
], "lang")) lang
= attr
[i
+1];
512 else if (!strcmp (attr
[i
], "text")) text
= attr
[i
+1];
513 else XML_FATAL("illegal description attribute: %s.", attr
[i
]);
515 if (!lang
) XML_FATAL1 ("lang attribute missing in description.");
516 if (!text
) XML_FATAL1 ("text attribute missing in description.");
519 /** \brief Parse attributes of an option element. */
521 parseOptInfoAttr(struct OptInfoData
*data
, const XML_Char
**attr
)
523 enum OptAttr
{OA_DEFAULT
= 0, OA_NAME
, OA_TYPE
, OA_VALID
, OA_COUNT
};
524 static const XML_Char
*optAttr
[] = {"default", "name", "type", "valid"};
525 const XML_Char
*attrVal
[OA_COUNT
] = {NULL
, NULL
, NULL
, NULL
};
526 const char *defaultVal
;
527 driOptionCache
*cache
= data
->cache
;
529 for (i
= 0; attr
[i
]; i
+= 2) {
530 uint32_t attrName
= bsearchStr (attr
[i
], optAttr
, OA_COUNT
);
531 if (attrName
>= OA_COUNT
)
532 XML_FATAL ("illegal option attribute: %s", attr
[i
]);
533 attrVal
[attrName
] = attr
[i
+1];
535 if (!attrVal
[OA_NAME
]) XML_FATAL1 ("name attribute missing in option.");
536 if (!attrVal
[OA_TYPE
]) XML_FATAL1 ("type attribute missing in option.");
537 if (!attrVal
[OA_DEFAULT
]) XML_FATAL1 ("default attribute missing in option.");
539 opt
= findOption (cache
, attrVal
[OA_NAME
]);
540 if (cache
->info
[opt
].name
)
541 XML_FATAL ("option %s redefined.", attrVal
[OA_NAME
]);
542 data
->curOption
= opt
;
544 XSTRDUP (cache
->info
[opt
].name
, attrVal
[OA_NAME
]);
546 if (!strcmp (attrVal
[OA_TYPE
], "bool"))
547 cache
->info
[opt
].type
= DRI_BOOL
;
548 else if (!strcmp (attrVal
[OA_TYPE
], "enum"))
549 cache
->info
[opt
].type
= DRI_ENUM
;
550 else if (!strcmp (attrVal
[OA_TYPE
], "int"))
551 cache
->info
[opt
].type
= DRI_INT
;
552 else if (!strcmp (attrVal
[OA_TYPE
], "float"))
553 cache
->info
[opt
].type
= DRI_FLOAT
;
554 else if (!strcmp (attrVal
[OA_TYPE
], "string"))
555 cache
->info
[opt
].type
= DRI_STRING
;
557 XML_FATAL ("illegal type in option: %s.", attrVal
[OA_TYPE
]);
559 defaultVal
= getenv (cache
->info
[opt
].name
);
560 if (defaultVal
!= NULL
) {
561 /* don't use XML_WARNING, we want the user to see this! */
564 "ATTENTION: default value of option %s overridden by environment.\n",
565 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 for %s: %s.", cache
->info
[opt
].name
, 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. */
590 optInfoStartElem(void *userData
, const XML_Char
*name
, const XML_Char
**attr
)
592 struct OptInfoData
*data
= (struct OptInfoData
*)userData
;
593 enum OptInfoElem elem
= bsearchStr (name
, OptInfoElems
, OI_COUNT
);
597 XML_FATAL1 ("nested <driinfo> elements.");
599 XML_FATAL1 ("attributes specified on <driinfo> element.");
600 data
->inDriInfo
= true;
603 if (!data
->inDriInfo
)
604 XML_FATAL1 ("<section> must be inside <driinfo>.");
606 XML_FATAL1 ("nested <section> elements.");
608 XML_FATAL1 ("attributes specified on <section> element.");
609 data
->inSection
= true;
612 if (!data
->inSection
&& !data
->inOption
)
613 XML_FATAL1 ("<description> must be inside <description> or <option.");
615 XML_FATAL1 ("nested <description> elements.");
617 parseDescAttr (data
, attr
);
620 if (!data
->inSection
)
621 XML_FATAL1 ("<option> must be inside <section>.");
623 XML_FATAL1 ("<option> nested in <description> element.");
625 XML_FATAL1 ("nested <option> elements.");
626 data
->inOption
= true;
627 parseOptInfoAttr (data
, attr
);
630 if (!(data
->inOption
&& data
->inDesc
))
631 XML_FATAL1 ("<enum> must be inside <option> and <description>.");
633 XML_FATAL1 ("nested <enum> elements.");
635 parseEnumAttr (data
, attr
);
638 XML_FATAL ("unknown element: %s.", name
);
642 /** \brief Handler for end element events. */
644 optInfoEndElem(void *userData
, const XML_Char
*name
)
646 struct OptInfoData
*data
= (struct OptInfoData
*)userData
;
647 enum OptInfoElem elem
= bsearchStr (name
, OptInfoElems
, OI_COUNT
);
650 data
->inDriInfo
= false;
653 data
->inSection
= false;
656 data
->inDesc
= false;
659 data
->inOption
= false;
662 data
->inEnum
= false;
665 assert (0); /* should have been caught by StartElem */
670 driParseOptionInfo(driOptionCache
*info
, const char *configOptions
)
674 struct OptInfoData userData
;
675 struct OptInfoData
*data
= &userData
;
677 /* Make the hash table big enough to fit more than the maximum number of
678 * config options we've ever seen in a driver.
681 info
->info
= calloc(1 << info
->tableSize
, sizeof (driOptionInfo
));
682 info
->values
= calloc(1 << info
->tableSize
, sizeof (driOptionValue
));
683 if (info
->info
== NULL
|| info
->values
== NULL
) {
684 fprintf (stderr
, "%s: %d: out of memory.\n", __FILE__
, __LINE__
);
688 p
= XML_ParserCreate ("UTF-8"); /* always UTF-8 */
689 XML_SetElementHandler (p
, optInfoStartElem
, optInfoEndElem
);
690 XML_SetUserData (p
, data
);
692 userData
.name
= "__driConfigOptions";
694 userData
.cache
= info
;
695 userData
.inDriInfo
= false;
696 userData
.inSection
= false;
697 userData
.inDesc
= false;
698 userData
.inOption
= false;
699 userData
.inEnum
= false;
700 userData
.curOption
= -1;
702 status
= XML_Parse (p
, configOptions
, strlen (configOptions
), 1);
704 XML_FATAL ("%s.", XML_ErrorString(XML_GetErrorCode(p
)));
709 /** \brief Parser context for configuration files. */
713 driOptionCache
*cache
;
715 const char *driverName
, *execName
;
716 const char *kernelDriverName
;
717 const char *engineName
;
718 uint32_t engineVersion
;
719 uint32_t ignoringDevice
;
720 uint32_t ignoringApp
;
727 /** \brief Elements in configuration files. */
729 OC_APPLICATION
= 0, OC_DEVICE
, OC_DRICONF
, OC_ENGINE
, OC_OPTION
, OC_COUNT
731 static const XML_Char
*OptConfElems
[] = {
732 [OC_APPLICATION
] = "application",
733 [OC_DEVICE
] = "device",
734 [OC_DRICONF
] = "driconf",
735 [OC_ENGINE
] = "engine",
736 [OC_OPTION
] = "option",
739 /** \brief Parse attributes of a device element. */
741 parseDeviceAttr(struct OptConfData
*data
, const XML_Char
**attr
)
744 const XML_Char
*driver
= NULL
, *screen
= NULL
, *kernel
= NULL
;
745 for (i
= 0; attr
[i
]; i
+= 2) {
746 if (!strcmp (attr
[i
], "driver")) driver
= attr
[i
+1];
747 else if (!strcmp (attr
[i
], "screen")) screen
= attr
[i
+1];
748 else if (!strcmp (attr
[i
], "kernel_driver")) kernel
= attr
[i
+1];
749 else XML_WARNING("unknown device attribute: %s.", attr
[i
]);
751 if (driver
&& strcmp (driver
, data
->driverName
))
752 data
->ignoringDevice
= data
->inDevice
;
753 else if (kernel
&& (!data
->kernelDriverName
|| strcmp (kernel
, data
->kernelDriverName
)))
754 data
->ignoringDevice
= data
->inDevice
;
756 driOptionValue screenNum
;
757 if (!parseValue (&screenNum
, DRI_INT
, screen
))
758 XML_WARNING("illegal screen number: %s.", screen
);
759 else if (screenNum
._int
!= data
->screenNum
)
760 data
->ignoringDevice
= data
->inDevice
;
765 valueInRanges(const driOptionInfo
*info
, uint32_t value
)
769 for (i
= 0; i
< info
->nRanges
; i
++) {
770 if (info
->ranges
[i
].start
._int
<= value
&&
771 info
->ranges
[i
].end
._int
>= value
)
778 /** \brief Parse attributes of an application element. */
780 parseAppAttr(struct OptConfData
*data
, const XML_Char
**attr
)
783 const XML_Char
*exec
= NULL
;
784 const XML_Char
*sha1
= NULL
;
785 for (i
= 0; attr
[i
]; i
+= 2) {
786 if (!strcmp (attr
[i
], "name")) /* not needed here */;
787 else if (!strcmp (attr
[i
], "executable")) exec
= attr
[i
+1];
788 else if (!strcmp (attr
[i
], "sha1")) sha1
= attr
[i
+1];
789 else XML_WARNING("unknown application attribute: %s.", attr
[i
]);
791 if (exec
&& strcmp (exec
, data
->execName
)) {
792 data
->ignoringApp
= data
->inApp
;
794 /* SHA1_DIGEST_STRING_LENGTH includes terminating null byte */
795 if (strlen(sha1
) != (SHA1_DIGEST_STRING_LENGTH
- 1)) {
796 XML_WARNING("Incorrect sha1 application attribute");
797 data
->ignoringApp
= data
->inApp
;
802 if (util_get_process_exec_path(path
, ARRAY_SIZE(path
)) > 0 &&
803 (content
= os_read_file(path
, &len
))) {
804 uint8_t sha1x
[SHA1_DIGEST_LENGTH
];
805 char sha1s
[SHA1_DIGEST_STRING_LENGTH
];
806 _mesa_sha1_compute(content
, len
, sha1x
);
807 _mesa_sha1_format((char*) sha1s
, sha1x
);
810 if (strcmp(sha1
, sha1s
)) {
811 data
->ignoringApp
= data
->inApp
;
814 data
->ignoringApp
= data
->inApp
;
820 /** \brief Parse attributes of an application element. */
822 parseEngineAttr(struct OptConfData
*data
, const XML_Char
**attr
)
825 const XML_Char
*engine_name_match
= NULL
, *engine_versions
= NULL
;
826 driOptionInfo version_ranges
= {
829 for (i
= 0; attr
[i
]; i
+= 2) {
830 if (!strcmp (attr
[i
], "name")) /* not needed here */;
831 else if (!strcmp (attr
[i
], "engine_name_match")) engine_name_match
= attr
[i
+1];
832 else if (!strcmp (attr
[i
], "engine_versions")) engine_versions
= attr
[i
+1];
833 else XML_WARNING("unknown application attribute: %s.", attr
[i
]);
835 if (engine_name_match
) {
838 if (regcomp (&re
, engine_name_match
, REG_EXTENDED
|REG_NOSUB
) == 0) {
839 if (regexec (&re
, data
->engineName
, 0, NULL
, 0) == REG_NOMATCH
)
840 data
->ignoringApp
= data
->inApp
;
843 XML_WARNING ("Invalid engine_name_match=\"%s\".", engine_name_match
);
845 if (engine_versions
) {
846 if (parseRanges (&version_ranges
, engine_versions
) &&
847 !valueInRanges (&version_ranges
, data
->engineVersion
))
848 data
->ignoringApp
= data
->inApp
;
851 free(version_ranges
.ranges
);
854 /** \brief Parse attributes of an option element. */
856 parseOptConfAttr(struct OptConfData
*data
, const XML_Char
**attr
)
859 const XML_Char
*name
= NULL
, *value
= NULL
;
860 for (i
= 0; attr
[i
]; i
+= 2) {
861 if (!strcmp (attr
[i
], "name")) name
= attr
[i
+1];
862 else if (!strcmp (attr
[i
], "value")) value
= attr
[i
+1];
863 else XML_WARNING("unknown option attribute: %s.", attr
[i
]);
865 if (!name
) XML_WARNING1 ("name attribute missing in option.");
866 if (!value
) XML_WARNING1 ("value attribute missing in option.");
868 driOptionCache
*cache
= data
->cache
;
869 uint32_t opt
= findOption (cache
, name
);
870 if (cache
->info
[opt
].name
== NULL
)
871 /* don't use XML_WARNING, drirc defines options for all drivers,
872 * but not all drivers support them */
874 else if (getenv (cache
->info
[opt
].name
)) {
875 /* don't use XML_WARNING, we want the user to see this! */
878 "ATTENTION: option value of option %s ignored.\n",
879 cache
->info
[opt
].name
);
881 } else if (!parseValue (&cache
->values
[opt
], cache
->info
[opt
].type
, value
))
882 XML_WARNING ("illegal option value: %s.", value
);
886 /** \brief Handler for start element events. */
888 optConfStartElem(void *userData
, const XML_Char
*name
,
889 const XML_Char
**attr
)
891 struct OptConfData
*data
= (struct OptConfData
*)userData
;
892 enum OptConfElem elem
= bsearchStr (name
, OptConfElems
, OC_COUNT
);
896 XML_WARNING1 ("nested <driconf> elements.");
898 XML_WARNING1 ("attributes specified on <driconf> element.");
902 if (!data
->inDriConf
)
903 XML_WARNING1 ("<device> should be inside <driconf>.");
905 XML_WARNING1 ("nested <device> elements.");
907 if (!data
->ignoringDevice
&& !data
->ignoringApp
)
908 parseDeviceAttr (data
, attr
);
912 XML_WARNING1 ("<application> should be inside <device>.");
914 XML_WARNING1 ("nested <application> or <engine> elements.");
916 if (!data
->ignoringDevice
&& !data
->ignoringApp
)
917 parseAppAttr (data
, attr
);
921 XML_WARNING1 ("<engine> should be inside <device>.");
923 XML_WARNING1 ("nested <application> or <engine> elements.");
925 if (!data
->ignoringDevice
&& !data
->ignoringApp
)
926 parseEngineAttr (data
, attr
);
930 XML_WARNING1 ("<option> should be inside <application>.");
932 XML_WARNING1 ("nested <option> elements.");
934 if (!data
->ignoringDevice
&& !data
->ignoringApp
)
935 parseOptConfAttr (data
, attr
);
938 XML_WARNING ("unknown element: %s.", name
);
942 /** \brief Handler for end element events. */
944 optConfEndElem(void *userData
, const XML_Char
*name
)
946 struct OptConfData
*data
= (struct OptConfData
*)userData
;
947 enum OptConfElem elem
= bsearchStr (name
, OptConfElems
, OC_COUNT
);
953 if (data
->inDevice
-- == data
->ignoringDevice
)
954 data
->ignoringDevice
= 0;
958 if (data
->inApp
-- == data
->ignoringApp
)
959 data
->ignoringApp
= 0;
965 /* unknown element, warning was produced on start tag */;
969 /** \brief Initialize an option cache based on info */
971 initOptionCache(driOptionCache
*cache
, const driOptionCache
*info
)
973 unsigned i
, size
= 1 << info
->tableSize
;
974 cache
->info
= info
->info
;
975 cache
->tableSize
= info
->tableSize
;
976 cache
->values
= malloc((1<<info
->tableSize
) * sizeof (driOptionValue
));
977 if (cache
->values
== NULL
) {
978 fprintf (stderr
, "%s: %d: out of memory.\n", __FILE__
, __LINE__
);
981 memcpy (cache
->values
, info
->values
,
982 (1<<info
->tableSize
) * sizeof (driOptionValue
));
983 for (i
= 0; i
< size
; ++i
) {
984 if (cache
->info
[i
].type
== DRI_STRING
)
985 XSTRDUP(cache
->values
[i
]._string
, info
->values
[i
]._string
);
990 _parseOneConfigFile(XML_Parser p
)
992 #define BUF_SIZE 0x1000
993 struct OptConfData
*data
= (struct OptConfData
*)XML_GetUserData (p
);
997 if ((fd
= open (data
->name
, O_RDONLY
)) == -1) {
998 __driUtilMessage ("Can't open configuration file %s: %s.",
999 data
->name
, strerror (errno
));
1005 void *buffer
= XML_GetBuffer (p
, BUF_SIZE
);
1007 __driUtilMessage ("Can't allocate parser buffer.");
1010 bytesRead
= read (fd
, buffer
, BUF_SIZE
);
1011 if (bytesRead
== -1) {
1012 __driUtilMessage ("Error reading from configuration file %s: %s.",
1013 data
->name
, strerror (errno
));
1016 status
= XML_ParseBuffer (p
, bytesRead
, bytesRead
== 0);
1018 XML_ERROR ("%s.", XML_ErrorString(XML_GetErrorCode(p
)));
1029 /** \brief Parse the named configuration file */
1031 parseOneConfigFile(struct OptConfData
*data
, const char *filename
)
1035 p
= XML_ParserCreate (NULL
); /* use encoding specified by file */
1036 XML_SetElementHandler (p
, optConfStartElem
, optConfEndElem
);
1037 XML_SetUserData (p
, data
);
1039 data
->name
= filename
;
1040 data
->ignoringDevice
= 0;
1041 data
->ignoringApp
= 0;
1042 data
->inDriConf
= 0;
1047 _parseOneConfigFile (p
);
1052 scandir_filter(const struct dirent
*ent
)
1054 #ifndef DT_REG /* systems without d_type in dirent results */
1057 if ((lstat(ent
->d_name
, &st
) != 0) ||
1058 (!S_ISREG(st
.st_mode
) && !S_ISLNK(st
.st_mode
)))
1061 if (ent
->d_type
!= DT_REG
&& ent
->d_type
!= DT_LNK
)
1065 if (fnmatch("*.conf", ent
->d_name
, 0))
1071 /** \brief Parse configuration files in a directory */
1073 parseConfigDir(struct OptConfData
*data
, const char *dirname
)
1076 struct dirent
**entries
= NULL
;
1078 count
= scandir(dirname
, &entries
, scandir_filter
, alphasort
);
1082 for (i
= 0; i
< count
; i
++) {
1083 char filename
[PATH_MAX
];
1085 snprintf(filename
, PATH_MAX
, "%s/%s", dirname
, entries
[i
]->d_name
);
1088 parseOneConfigFile(data
, filename
);
1095 #define SYSCONFDIR "/etc"
1099 #define DATADIR "/usr/share"
1103 driParseConfigFiles(driOptionCache
*cache
, const driOptionCache
*info
,
1104 int screenNum
, const char *driverName
,
1105 const char *kernelDriverName
,
1106 const char *engineName
, uint32_t engineVersion
)
1109 struct OptConfData userData
;
1111 initOptionCache (cache
, info
);
1113 userData
.cache
= cache
;
1114 userData
.screenNum
= screenNum
;
1115 userData
.driverName
= driverName
;
1116 userData
.kernelDriverName
= kernelDriverName
;
1117 userData
.engineName
= engineName
? engineName
: "";
1118 userData
.engineVersion
= engineVersion
;
1119 userData
.execName
= util_get_process_name();
1121 parseConfigDir(&userData
, DATADIR
"/drirc.d");
1122 parseOneConfigFile(&userData
, SYSCONFDIR
"/drirc");
1124 if ((home
= getenv ("HOME"))) {
1125 char filename
[PATH_MAX
];
1127 snprintf(filename
, PATH_MAX
, "%s/.drirc", home
);
1128 parseOneConfigFile(&userData
, filename
);
1133 driDestroyOptionInfo(driOptionCache
*info
)
1135 driDestroyOptionCache(info
);
1137 uint32_t i
, size
= 1 << info
->tableSize
;
1138 for (i
= 0; i
< size
; ++i
) {
1139 if (info
->info
[i
].name
) {
1140 free(info
->info
[i
].name
);
1141 free(info
->info
[i
].ranges
);
1149 driDestroyOptionCache(driOptionCache
*cache
)
1152 unsigned i
, size
= 1 << cache
->tableSize
;
1153 for (i
= 0; i
< size
; ++i
) {
1154 if (cache
->info
[i
].type
== DRI_STRING
)
1155 free(cache
->values
[i
]._string
);
1158 free(cache
->values
);
1162 driCheckOption(const driOptionCache
*cache
, const char *name
,
1165 uint32_t i
= findOption (cache
, name
);
1166 return cache
->info
[i
].name
!= NULL
&& cache
->info
[i
].type
== type
;
1170 driQueryOptionb(const driOptionCache
*cache
, const char *name
)
1172 uint32_t i
= findOption (cache
, name
);
1173 /* make sure the option is defined and has the correct type */
1174 assert (cache
->info
[i
].name
!= NULL
);
1175 assert (cache
->info
[i
].type
== DRI_BOOL
);
1176 return cache
->values
[i
]._bool
;
1180 driQueryOptioni(const driOptionCache
*cache
, const char *name
)
1182 uint32_t i
= findOption (cache
, name
);
1183 /* make sure the option is defined and has the correct type */
1184 assert (cache
->info
[i
].name
!= NULL
);
1185 assert (cache
->info
[i
].type
== DRI_INT
|| cache
->info
[i
].type
== DRI_ENUM
);
1186 return cache
->values
[i
]._int
;
1190 driQueryOptionf(const driOptionCache
*cache
, const char *name
)
1192 uint32_t i
= findOption (cache
, name
);
1193 /* make sure the option is defined and has the correct type */
1194 assert (cache
->info
[i
].name
!= NULL
);
1195 assert (cache
->info
[i
].type
== DRI_FLOAT
);
1196 return cache
->values
[i
]._float
;
1200 driQueryOptionstr(const driOptionCache
*cache
, const char *name
)
1202 uint32_t i
= findOption (cache
, name
);
1203 /* make sure the option is defined and has the correct type */
1204 assert (cache
->info
[i
].name
!= NULL
);
1205 assert (cache
->info
[i
].type
== DRI_STRING
);
1206 return cache
->values
[i
]._string
;