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 const char *applicationName
;
719 uint32_t engineVersion
;
720 uint32_t applicationVersion
;
721 uint32_t ignoringDevice
;
722 uint32_t ignoringApp
;
729 /** \brief Elements in configuration files. */
731 OC_APPLICATION
= 0, OC_DEVICE
, OC_DRICONF
, OC_ENGINE
, OC_OPTION
, OC_COUNT
733 static const XML_Char
*OptConfElems
[] = {
734 [OC_APPLICATION
] = "application",
735 [OC_DEVICE
] = "device",
736 [OC_DRICONF
] = "driconf",
737 [OC_ENGINE
] = "engine",
738 [OC_OPTION
] = "option",
741 /** \brief Parse attributes of a device element. */
743 parseDeviceAttr(struct OptConfData
*data
, const XML_Char
**attr
)
746 const XML_Char
*driver
= NULL
, *screen
= NULL
, *kernel
= NULL
;
747 for (i
= 0; attr
[i
]; i
+= 2) {
748 if (!strcmp (attr
[i
], "driver")) driver
= attr
[i
+1];
749 else if (!strcmp (attr
[i
], "screen")) screen
= attr
[i
+1];
750 else if (!strcmp (attr
[i
], "kernel_driver")) kernel
= attr
[i
+1];
751 else XML_WARNING("unknown device attribute: %s.", attr
[i
]);
753 if (driver
&& strcmp (driver
, data
->driverName
))
754 data
->ignoringDevice
= data
->inDevice
;
755 else if (kernel
&& (!data
->kernelDriverName
|| strcmp (kernel
, data
->kernelDriverName
)))
756 data
->ignoringDevice
= data
->inDevice
;
758 driOptionValue screenNum
;
759 if (!parseValue (&screenNum
, DRI_INT
, screen
))
760 XML_WARNING("illegal screen number: %s.", screen
);
761 else if (screenNum
._int
!= data
->screenNum
)
762 data
->ignoringDevice
= data
->inDevice
;
767 valueInRanges(const driOptionInfo
*info
, uint32_t value
)
771 for (i
= 0; i
< info
->nRanges
; i
++) {
772 if (info
->ranges
[i
].start
._int
<= value
&&
773 info
->ranges
[i
].end
._int
>= value
)
780 /** \brief Parse attributes of an application element. */
782 parseAppAttr(struct OptConfData
*data
, const XML_Char
**attr
)
785 const XML_Char
*exec
= NULL
;
786 const XML_Char
*sha1
= NULL
;
787 const XML_Char
*application_name_match
= NULL
;
788 const XML_Char
*application_versions
= NULL
;
789 driOptionInfo version_ranges
= {
793 for (i
= 0; attr
[i
]; i
+= 2) {
794 if (!strcmp (attr
[i
], "name")) /* not needed here */;
795 else if (!strcmp (attr
[i
], "executable")) exec
= attr
[i
+1];
796 else if (!strcmp (attr
[i
], "sha1")) sha1
= attr
[i
+1];
797 else if (!strcmp (attr
[i
], "application_name_match"))
798 application_name_match
= attr
[i
+1];
799 else if (!strcmp (attr
[i
], "application_versions"))
800 application_versions
= attr
[i
+1];
801 else XML_WARNING("unknown application attribute: %s.", attr
[i
]);
803 if (exec
&& strcmp (exec
, data
->execName
)) {
804 data
->ignoringApp
= data
->inApp
;
806 /* SHA1_DIGEST_STRING_LENGTH includes terminating null byte */
807 if (strlen(sha1
) != (SHA1_DIGEST_STRING_LENGTH
- 1)) {
808 XML_WARNING("Incorrect sha1 application attribute");
809 data
->ignoringApp
= data
->inApp
;
814 if (util_get_process_exec_path(path
, ARRAY_SIZE(path
)) > 0 &&
815 (content
= os_read_file(path
, &len
))) {
816 uint8_t sha1x
[SHA1_DIGEST_LENGTH
];
817 char sha1s
[SHA1_DIGEST_STRING_LENGTH
];
818 _mesa_sha1_compute(content
, len
, sha1x
);
819 _mesa_sha1_format((char*) sha1s
, sha1x
);
822 if (strcmp(sha1
, sha1s
)) {
823 data
->ignoringApp
= data
->inApp
;
826 data
->ignoringApp
= data
->inApp
;
829 } else if (application_name_match
) {
832 if (regcomp (&re
, application_name_match
, REG_EXTENDED
|REG_NOSUB
) == 0) {
833 if (regexec (&re
, data
->applicationName
, 0, NULL
, 0) == REG_NOMATCH
)
834 data
->ignoringApp
= data
->inApp
;
837 XML_WARNING ("Invalid application_name_match=\"%s\".", application_name_match
);
839 if (application_versions
) {
840 if (parseRanges (&version_ranges
, application_versions
) &&
841 !valueInRanges (&version_ranges
, data
->applicationVersion
))
842 data
->ignoringApp
= data
->inApp
;
846 /** \brief Parse attributes of an application element. */
848 parseEngineAttr(struct OptConfData
*data
, const XML_Char
**attr
)
851 const XML_Char
*engine_name_match
= NULL
, *engine_versions
= NULL
;
852 driOptionInfo version_ranges
= {
855 for (i
= 0; attr
[i
]; i
+= 2) {
856 if (!strcmp (attr
[i
], "name")) /* not needed here */;
857 else if (!strcmp (attr
[i
], "engine_name_match")) engine_name_match
= attr
[i
+1];
858 else if (!strcmp (attr
[i
], "engine_versions")) engine_versions
= attr
[i
+1];
859 else XML_WARNING("unknown application attribute: %s.", attr
[i
]);
861 if (engine_name_match
) {
864 if (regcomp (&re
, engine_name_match
, REG_EXTENDED
|REG_NOSUB
) == 0) {
865 if (regexec (&re
, data
->engineName
, 0, NULL
, 0) == REG_NOMATCH
)
866 data
->ignoringApp
= data
->inApp
;
869 XML_WARNING ("Invalid engine_name_match=\"%s\".", engine_name_match
);
871 if (engine_versions
) {
872 if (parseRanges (&version_ranges
, engine_versions
) &&
873 !valueInRanges (&version_ranges
, data
->engineVersion
))
874 data
->ignoringApp
= data
->inApp
;
877 free(version_ranges
.ranges
);
880 /** \brief Parse attributes of an option element. */
882 parseOptConfAttr(struct OptConfData
*data
, const XML_Char
**attr
)
885 const XML_Char
*name
= NULL
, *value
= NULL
;
886 for (i
= 0; attr
[i
]; i
+= 2) {
887 if (!strcmp (attr
[i
], "name")) name
= attr
[i
+1];
888 else if (!strcmp (attr
[i
], "value")) value
= attr
[i
+1];
889 else XML_WARNING("unknown option attribute: %s.", attr
[i
]);
891 if (!name
) XML_WARNING1 ("name attribute missing in option.");
892 if (!value
) XML_WARNING1 ("value attribute missing in option.");
894 driOptionCache
*cache
= data
->cache
;
895 uint32_t opt
= findOption (cache
, name
);
896 if (cache
->info
[opt
].name
== NULL
)
897 /* don't use XML_WARNING, drirc defines options for all drivers,
898 * but not all drivers support them */
900 else if (getenv (cache
->info
[opt
].name
)) {
901 /* don't use XML_WARNING, we want the user to see this! */
904 "ATTENTION: option value of option %s ignored.\n",
905 cache
->info
[opt
].name
);
907 } else if (!parseValue (&cache
->values
[opt
], cache
->info
[opt
].type
, value
))
908 XML_WARNING ("illegal option value: %s.", value
);
912 /** \brief Handler for start element events. */
914 optConfStartElem(void *userData
, const XML_Char
*name
,
915 const XML_Char
**attr
)
917 struct OptConfData
*data
= (struct OptConfData
*)userData
;
918 enum OptConfElem elem
= bsearchStr (name
, OptConfElems
, OC_COUNT
);
922 XML_WARNING1 ("nested <driconf> elements.");
924 XML_WARNING1 ("attributes specified on <driconf> element.");
928 if (!data
->inDriConf
)
929 XML_WARNING1 ("<device> should be inside <driconf>.");
931 XML_WARNING1 ("nested <device> elements.");
933 if (!data
->ignoringDevice
&& !data
->ignoringApp
)
934 parseDeviceAttr (data
, attr
);
938 XML_WARNING1 ("<application> should be inside <device>.");
940 XML_WARNING1 ("nested <application> or <engine> elements.");
942 if (!data
->ignoringDevice
&& !data
->ignoringApp
)
943 parseAppAttr (data
, attr
);
947 XML_WARNING1 ("<engine> should be inside <device>.");
949 XML_WARNING1 ("nested <application> or <engine> elements.");
951 if (!data
->ignoringDevice
&& !data
->ignoringApp
)
952 parseEngineAttr (data
, attr
);
956 XML_WARNING1 ("<option> should be inside <application>.");
958 XML_WARNING1 ("nested <option> elements.");
960 if (!data
->ignoringDevice
&& !data
->ignoringApp
)
961 parseOptConfAttr (data
, attr
);
964 XML_WARNING ("unknown element: %s.", name
);
968 /** \brief Handler for end element events. */
970 optConfEndElem(void *userData
, const XML_Char
*name
)
972 struct OptConfData
*data
= (struct OptConfData
*)userData
;
973 enum OptConfElem elem
= bsearchStr (name
, OptConfElems
, OC_COUNT
);
979 if (data
->inDevice
-- == data
->ignoringDevice
)
980 data
->ignoringDevice
= 0;
984 if (data
->inApp
-- == data
->ignoringApp
)
985 data
->ignoringApp
= 0;
991 /* unknown element, warning was produced on start tag */;
995 /** \brief Initialize an option cache based on info */
997 initOptionCache(driOptionCache
*cache
, const driOptionCache
*info
)
999 unsigned i
, size
= 1 << info
->tableSize
;
1000 cache
->info
= info
->info
;
1001 cache
->tableSize
= info
->tableSize
;
1002 cache
->values
= malloc((1<<info
->tableSize
) * sizeof (driOptionValue
));
1003 if (cache
->values
== NULL
) {
1004 fprintf (stderr
, "%s: %d: out of memory.\n", __FILE__
, __LINE__
);
1007 memcpy (cache
->values
, info
->values
,
1008 (1<<info
->tableSize
) * sizeof (driOptionValue
));
1009 for (i
= 0; i
< size
; ++i
) {
1010 if (cache
->info
[i
].type
== DRI_STRING
)
1011 XSTRDUP(cache
->values
[i
]._string
, info
->values
[i
]._string
);
1016 _parseOneConfigFile(XML_Parser p
)
1018 #define BUF_SIZE 0x1000
1019 struct OptConfData
*data
= (struct OptConfData
*)XML_GetUserData (p
);
1023 if ((fd
= open (data
->name
, O_RDONLY
)) == -1) {
1024 __driUtilMessage ("Can't open configuration file %s: %s.",
1025 data
->name
, strerror (errno
));
1031 void *buffer
= XML_GetBuffer (p
, BUF_SIZE
);
1033 __driUtilMessage ("Can't allocate parser buffer.");
1036 bytesRead
= read (fd
, buffer
, BUF_SIZE
);
1037 if (bytesRead
== -1) {
1038 __driUtilMessage ("Error reading from configuration file %s: %s.",
1039 data
->name
, strerror (errno
));
1042 status
= XML_ParseBuffer (p
, bytesRead
, bytesRead
== 0);
1044 XML_ERROR ("%s.", XML_ErrorString(XML_GetErrorCode(p
)));
1055 /** \brief Parse the named configuration file */
1057 parseOneConfigFile(struct OptConfData
*data
, const char *filename
)
1061 p
= XML_ParserCreate (NULL
); /* use encoding specified by file */
1062 XML_SetElementHandler (p
, optConfStartElem
, optConfEndElem
);
1063 XML_SetUserData (p
, data
);
1065 data
->name
= filename
;
1066 data
->ignoringDevice
= 0;
1067 data
->ignoringApp
= 0;
1068 data
->inDriConf
= 0;
1073 _parseOneConfigFile (p
);
1078 scandir_filter(const struct dirent
*ent
)
1080 #ifndef DT_REG /* systems without d_type in dirent results */
1083 if ((lstat(ent
->d_name
, &st
) != 0) ||
1084 (!S_ISREG(st
.st_mode
) && !S_ISLNK(st
.st_mode
)))
1087 if (ent
->d_type
!= DT_REG
&& ent
->d_type
!= DT_LNK
)
1091 if (fnmatch("*.conf", ent
->d_name
, 0))
1097 /** \brief Parse configuration files in a directory */
1099 parseConfigDir(struct OptConfData
*data
, const char *dirname
)
1102 struct dirent
**entries
= NULL
;
1104 count
= scandir(dirname
, &entries
, scandir_filter
, alphasort
);
1108 for (i
= 0; i
< count
; i
++) {
1109 char filename
[PATH_MAX
];
1111 snprintf(filename
, PATH_MAX
, "%s/%s", dirname
, entries
[i
]->d_name
);
1114 parseOneConfigFile(data
, filename
);
1121 #define SYSCONFDIR "/etc"
1125 #define DATADIR "/usr/share"
1129 driParseConfigFiles(driOptionCache
*cache
, const driOptionCache
*info
,
1130 int screenNum
, const char *driverName
,
1131 const char *kernelDriverName
,
1132 const char *applicationName
, uint32_t applicationVersion
,
1133 const char *engineName
, uint32_t engineVersion
)
1136 struct OptConfData userData
;
1138 initOptionCache (cache
, info
);
1140 userData
.cache
= cache
;
1141 userData
.screenNum
= screenNum
;
1142 userData
.driverName
= driverName
;
1143 userData
.kernelDriverName
= kernelDriverName
;
1144 userData
.applicationName
= applicationName
? applicationName
: "";
1145 userData
.applicationVersion
= applicationVersion
;
1146 userData
.engineName
= engineName
? engineName
: "";
1147 userData
.engineVersion
= engineVersion
;
1148 userData
.execName
= util_get_process_name();
1150 parseConfigDir(&userData
, DATADIR
"/drirc.d");
1151 parseOneConfigFile(&userData
, SYSCONFDIR
"/drirc");
1153 if ((home
= getenv ("HOME"))) {
1154 char filename
[PATH_MAX
];
1156 snprintf(filename
, PATH_MAX
, "%s/.drirc", home
);
1157 parseOneConfigFile(&userData
, filename
);
1162 driDestroyOptionInfo(driOptionCache
*info
)
1164 driDestroyOptionCache(info
);
1166 uint32_t i
, size
= 1 << info
->tableSize
;
1167 for (i
= 0; i
< size
; ++i
) {
1168 if (info
->info
[i
].name
) {
1169 free(info
->info
[i
].name
);
1170 free(info
->info
[i
].ranges
);
1178 driDestroyOptionCache(driOptionCache
*cache
)
1181 unsigned i
, size
= 1 << cache
->tableSize
;
1182 for (i
= 0; i
< size
; ++i
) {
1183 if (cache
->info
[i
].type
== DRI_STRING
)
1184 free(cache
->values
[i
]._string
);
1187 free(cache
->values
);
1191 driCheckOption(const driOptionCache
*cache
, const char *name
,
1194 uint32_t i
= findOption (cache
, name
);
1195 return cache
->info
[i
].name
!= NULL
&& cache
->info
[i
].type
== type
;
1199 driQueryOptionb(const driOptionCache
*cache
, const char *name
)
1201 uint32_t i
= findOption (cache
, name
);
1202 /* make sure the option is defined and has the correct type */
1203 assert (cache
->info
[i
].name
!= NULL
);
1204 assert (cache
->info
[i
].type
== DRI_BOOL
);
1205 return cache
->values
[i
]._bool
;
1209 driQueryOptioni(const driOptionCache
*cache
, const char *name
)
1211 uint32_t i
= findOption (cache
, name
);
1212 /* make sure the option is defined and has the correct type */
1213 assert (cache
->info
[i
].name
!= NULL
);
1214 assert (cache
->info
[i
].type
== DRI_INT
|| cache
->info
[i
].type
== DRI_ENUM
);
1215 return cache
->values
[i
]._int
;
1219 driQueryOptionf(const driOptionCache
*cache
, const char *name
)
1221 uint32_t i
= findOption (cache
, name
);
1222 /* make sure the option is defined and has the correct type */
1223 assert (cache
->info
[i
].name
!= NULL
);
1224 assert (cache
->info
[i
].type
== DRI_FLOAT
);
1225 return cache
->values
[i
]._float
;
1229 driQueryOptionstr(const driOptionCache
*cache
, const char *name
)
1231 uint32_t i
= findOption (cache
, name
);
1232 /* make sure the option is defined and has the correct type */
1233 assert (cache
->info
[i
].name
!= NULL
);
1234 assert (cache
->info
[i
].type
== DRI_STRING
);
1235 return cache
->values
[i
]._string
;