xmlconfig.c revision 01e04c3f
1/* 2 * XML DRI client-side driver configuration 3 * Copyright (C) 2003 Felix Kuehling 4 * 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: 11 * 12 * The above copyright notice and this permission notice shall be included 13 * in all copies or substantial portions of the Software. 14 * 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. 22 * 23 */ 24/** 25 * \file xmlconfig.c 26 * \brief Driver-independent client-side part of the XML configuration 27 * \author Felix Kuehling 28 */ 29 30#include <limits.h> 31#include <stdarg.h> 32#include <stdio.h> 33#include <string.h> 34#include <assert.h> 35#include <expat.h> 36#include <fcntl.h> 37#include <math.h> 38#include <unistd.h> 39#include <errno.h> 40#include <dirent.h> 41#include <fnmatch.h> 42#include "xmlconfig.h" 43#include "u_process.h" 44 45 46/** \brief Find an option in an option cache with the name as key */ 47static uint32_t 48findOption(const driOptionCache *cache, const char *name) 49{ 50 uint32_t len = strlen (name); 51 uint32_t size = 1 << cache->tableSize, mask = size - 1; 52 uint32_t hash = 0; 53 uint32_t i, shift; 54 55 /* compute a hash from the variable length name */ 56 for (i = 0, shift = 0; i < len; ++i, shift = (shift+8) & 31) 57 hash += (uint32_t)name[i] << shift; 58 hash *= hash; 59 hash = (hash >> (16-cache->tableSize/2)) & mask; 60 61 /* this is just the starting point of the linear search for the option */ 62 for (i = 0; i < size; ++i, hash = (hash+1) & mask) { 63 /* if we hit an empty entry then the option is not defined (yet) */ 64 if (cache->info[hash].name == 0) 65 break; 66 else if (!strcmp (name, cache->info[hash].name)) 67 break; 68 } 69 /* this assertion fails if the hash table is full */ 70 assert (i < size); 71 72 return hash; 73} 74 75/** \brief Like strdup but using malloc and with error checking. */ 76#define XSTRDUP(dest,source) do { \ 77 uint32_t len = strlen (source); \ 78 if (!(dest = malloc(len+1))) { \ 79 fprintf (stderr, "%s: %d: out of memory.\n", __FILE__, __LINE__); \ 80 abort(); \ 81 } \ 82 memcpy (dest, source, len+1); \ 83} while (0) 84 85static int compare (const void *a, const void *b) { 86 return strcmp (*(char *const*)a, *(char *const*)b); 87} 88/** \brief Binary search in a string array. */ 89static uint32_t 90bsearchStr (const XML_Char *name, const XML_Char *elems[], uint32_t count) 91{ 92 const XML_Char **found; 93 found = bsearch (&name, elems, count, sizeof (XML_Char *), compare); 94 if (found) 95 return found - elems; 96 else 97 return count; 98} 99 100/** \brief Locale-independent integer parser. 101 * 102 * Works similar to strtol. Leading space is NOT skipped. The input 103 * number may have an optional sign. Radix is specified by base. If 104 * base is 0 then decimal is assumed unless the input number is 105 * prefixed by 0x or 0X for hexadecimal or 0 for octal. After 106 * returning tail points to the first character that is not part of 107 * the integer number. If no number was found then tail points to the 108 * start of the input string. */ 109static int 110strToI(const XML_Char *string, const XML_Char **tail, int base) 111{ 112 int radix = base == 0 ? 10 : base; 113 int result = 0; 114 int sign = 1; 115 bool numberFound = false; 116 const XML_Char *start = string; 117 118 assert (radix >= 2 && radix <= 36); 119 120 if (*string == '-') { 121 sign = -1; 122 string++; 123 } else if (*string == '+') 124 string++; 125 if (base == 0 && *string == '0') { 126 numberFound = true; 127 if (*(string+1) == 'x' || *(string+1) == 'X') { 128 radix = 16; 129 string += 2; 130 } else { 131 radix = 8; 132 string++; 133 } 134 } 135 do { 136 int digit = -1; 137 if (radix <= 10) { 138 if (*string >= '0' && *string < '0' + radix) 139 digit = *string - '0'; 140 } else { 141 if (*string >= '0' && *string <= '9') 142 digit = *string - '0'; 143 else if (*string >= 'a' && *string < 'a' + radix - 10) 144 digit = *string - 'a' + 10; 145 else if (*string >= 'A' && *string < 'A' + radix - 10) 146 digit = *string - 'A' + 10; 147 } 148 if (digit != -1) { 149 numberFound = true; 150 result = radix*result + digit; 151 string++; 152 } else 153 break; 154 } while (true); 155 *tail = numberFound ? string : start; 156 return sign * result; 157} 158 159/** \brief Locale-independent floating-point parser. 160 * 161 * Works similar to strtod. Leading space is NOT skipped. The input 162 * number may have an optional sign. '.' is interpreted as decimal 163 * point and may occur at most once. Optionally the number may end in 164 * [eE]<exponent>, where <exponent> is an integer as recognized by 165 * strToI. In that case the result is number * 10^exponent. After 166 * returning tail points to the first character that is not part of 167 * the floating point number. If no number was found then tail points 168 * to the start of the input string. 169 * 170 * Uses two passes for maximum accuracy. */ 171static float 172strToF(const XML_Char *string, const XML_Char **tail) 173{ 174 int nDigits = 0, pointPos, exponent; 175 float sign = 1.0f, result = 0.0f, scale; 176 const XML_Char *start = string, *numStart; 177 178 /* sign */ 179 if (*string == '-') { 180 sign = -1.0f; 181 string++; 182 } else if (*string == '+') 183 string++; 184 185 /* first pass: determine position of decimal point, number of 186 * digits, exponent and the end of the number. */ 187 numStart = string; 188 while (*string >= '0' && *string <= '9') { 189 string++; 190 nDigits++; 191 } 192 pointPos = nDigits; 193 if (*string == '.') { 194 string++; 195 while (*string >= '0' && *string <= '9') { 196 string++; 197 nDigits++; 198 } 199 } 200 if (nDigits == 0) { 201 /* no digits, no number */ 202 *tail = start; 203 return 0.0f; 204 } 205 *tail = string; 206 if (*string == 'e' || *string == 'E') { 207 const XML_Char *expTail; 208 exponent = strToI (string+1, &expTail, 10); 209 if (expTail == string+1) 210 exponent = 0; 211 else 212 *tail = expTail; 213 } else 214 exponent = 0; 215 string = numStart; 216 217 /* scale of the first digit */ 218 scale = sign * (float)pow (10.0, (double)(pointPos-1 + exponent)); 219 220 /* second pass: parse digits */ 221 do { 222 if (*string != '.') { 223 assert (*string >= '0' && *string <= '9'); 224 result += scale * (float)(*string - '0'); 225 scale *= 0.1f; 226 nDigits--; 227 } 228 string++; 229 } while (nDigits > 0); 230 231 return result; 232} 233 234/** \brief Parse a value of a given type. */ 235static unsigned char 236parseValue(driOptionValue *v, driOptionType type, const XML_Char *string) 237{ 238 const XML_Char *tail = NULL; 239 /* skip leading white-space */ 240 string += strspn (string, " \f\n\r\t\v"); 241 switch (type) { 242 case DRI_BOOL: 243 if (!strcmp (string, "false")) { 244 v->_bool = false; 245 tail = string + 5; 246 } else if (!strcmp (string, "true")) { 247 v->_bool = true; 248 tail = string + 4; 249 } 250 else 251 return false; 252 break; 253 case DRI_ENUM: /* enum is just a special integer */ 254 case DRI_INT: 255 v->_int = strToI (string, &tail, 0); 256 break; 257 case DRI_FLOAT: 258 v->_float = strToF (string, &tail); 259 break; 260 case DRI_STRING: 261 free (v->_string); 262 v->_string = strndup(string, STRING_CONF_MAXLEN); 263 return true; 264 } 265 266 if (tail == string) 267 return false; /* empty string (or containing only white-space) */ 268 /* skip trailing white space */ 269 if (*tail) 270 tail += strspn (tail, " \f\n\r\t\v"); 271 if (*tail) 272 return false; /* something left over that is not part of value */ 273 274 return true; 275} 276 277/** \brief Parse a list of ranges of type info->type. */ 278static unsigned char 279parseRanges(driOptionInfo *info, const XML_Char *string) 280{ 281 XML_Char *cp, *range; 282 uint32_t nRanges, i; 283 driOptionRange *ranges; 284 285 XSTRDUP (cp, string); 286 /* pass 1: determine the number of ranges (number of commas + 1) */ 287 range = cp; 288 for (nRanges = 1; *range; ++range) 289 if (*range == ',') 290 ++nRanges; 291 292 if ((ranges = malloc(nRanges*sizeof(driOptionRange))) == NULL) { 293 fprintf (stderr, "%s: %d: out of memory.\n", __FILE__, __LINE__); 294 abort(); 295 } 296 297 /* pass 2: parse all ranges into preallocated array */ 298 range = cp; 299 for (i = 0; i < nRanges; ++i) { 300 XML_Char *end, *sep; 301 assert (range); 302 end = strchr (range, ','); 303 if (end) 304 *end = '\0'; 305 sep = strchr (range, ':'); 306 if (sep) { /* non-empty interval */ 307 *sep = '\0'; 308 if (!parseValue (&ranges[i].start, info->type, range) || 309 !parseValue (&ranges[i].end, info->type, sep+1)) 310 break; 311 if (info->type == DRI_INT && 312 ranges[i].start._int > ranges[i].end._int) 313 break; 314 if (info->type == DRI_FLOAT && 315 ranges[i].start._float > ranges[i].end._float) 316 break; 317 } else { /* empty interval */ 318 if (!parseValue (&ranges[i].start, info->type, range)) 319 break; 320 ranges[i].end = ranges[i].start; 321 } 322 if (end) 323 range = end+1; 324 else 325 range = NULL; 326 } 327 free(cp); 328 if (i < nRanges) { 329 free(ranges); 330 return false; 331 } else 332 assert (range == NULL); 333 334 info->nRanges = nRanges; 335 info->ranges = ranges; 336 return true; 337} 338 339/** \brief Check if a value is in one of info->ranges. */ 340static bool 341checkValue(const driOptionValue *v, const driOptionInfo *info) 342{ 343 uint32_t i; 344 assert (info->type != DRI_BOOL); /* should be caught by the parser */ 345 if (info->nRanges == 0) 346 return true; 347 switch (info->type) { 348 case DRI_ENUM: /* enum is just a special integer */ 349 case DRI_INT: 350 for (i = 0; i < info->nRanges; ++i) 351 if (v->_int >= info->ranges[i].start._int && 352 v->_int <= info->ranges[i].end._int) 353 return true; 354 break; 355 case DRI_FLOAT: 356 for (i = 0; i < info->nRanges; ++i) 357 if (v->_float >= info->ranges[i].start._float && 358 v->_float <= info->ranges[i].end._float) 359 return true; 360 break; 361 case DRI_STRING: 362 break; 363 default: 364 assert (0); /* should never happen */ 365 } 366 return false; 367} 368 369/** 370 * Print message to \c stderr if the \c LIBGL_DEBUG environment variable 371 * is set. 372 * 373 * Is called from the drivers. 374 * 375 * \param f \c printf like format string. 376 */ 377static void 378__driUtilMessage(const char *f, ...) 379{ 380 va_list args; 381 const char *libgl_debug; 382 383 libgl_debug=getenv("LIBGL_DEBUG"); 384 if (libgl_debug && !strstr(libgl_debug, "quiet")) { 385 fprintf(stderr, "libGL: "); 386 va_start(args, f); 387 vfprintf(stderr, f, args); 388 va_end(args); 389 fprintf(stderr, "\n"); 390 } 391} 392 393/** \brief Output a warning message. */ 394#define XML_WARNING1(msg) do {\ 395 __driUtilMessage ("Warning in %s line %d, column %d: "msg, data->name, \ 396 (int) XML_GetCurrentLineNumber(data->parser), \ 397 (int) XML_GetCurrentColumnNumber(data->parser)); \ 398} while (0) 399#define XML_WARNING(msg, ...) do { \ 400 __driUtilMessage ("Warning in %s line %d, column %d: "msg, data->name, \ 401 (int) XML_GetCurrentLineNumber(data->parser), \ 402 (int) XML_GetCurrentColumnNumber(data->parser), \ 403 ##__VA_ARGS__); \ 404} while (0) 405/** \brief Output an error message. */ 406#define XML_ERROR1(msg) do { \ 407 __driUtilMessage ("Error in %s line %d, column %d: "msg, data->name, \ 408 (int) XML_GetCurrentLineNumber(data->parser), \ 409 (int) XML_GetCurrentColumnNumber(data->parser)); \ 410} while (0) 411#define XML_ERROR(msg, ...) do { \ 412 __driUtilMessage ("Error in %s line %d, column %d: "msg, data->name, \ 413 (int) XML_GetCurrentLineNumber(data->parser), \ 414 (int) XML_GetCurrentColumnNumber(data->parser), \ 415 ##__VA_ARGS__); \ 416} while (0) 417/** \brief Output a fatal error message and abort. */ 418#define XML_FATAL1(msg) do { \ 419 fprintf (stderr, "Fatal error in %s line %d, column %d: "msg"\n", \ 420 data->name, \ 421 (int) XML_GetCurrentLineNumber(data->parser), \ 422 (int) XML_GetCurrentColumnNumber(data->parser)); \ 423 abort();\ 424} while (0) 425#define XML_FATAL(msg, ...) do { \ 426 fprintf (stderr, "Fatal error in %s line %d, column %d: "msg"\n", \ 427 data->name, \ 428 (int) XML_GetCurrentLineNumber(data->parser), \ 429 (int) XML_GetCurrentColumnNumber(data->parser), \ 430 ##__VA_ARGS__); \ 431 abort();\ 432} while (0) 433 434/** \brief Parser context for __driConfigOptions. */ 435struct OptInfoData { 436 const char *name; 437 XML_Parser parser; 438 driOptionCache *cache; 439 bool inDriInfo; 440 bool inSection; 441 bool inDesc; 442 bool inOption; 443 bool inEnum; 444 int curOption; 445}; 446 447/** \brief Elements in __driConfigOptions. */ 448enum OptInfoElem { 449 OI_DESCRIPTION = 0, OI_DRIINFO, OI_ENUM, OI_OPTION, OI_SECTION, OI_COUNT 450}; 451static const XML_Char *OptInfoElems[] = { 452 "description", "driinfo", "enum", "option", "section" 453}; 454 455/** \brief Parse attributes of an enum element. 456 * 457 * We're not actually interested in the data. Just make sure this is ok 458 * for external configuration tools. 459 */ 460static void 461parseEnumAttr(struct OptInfoData *data, const XML_Char **attr) 462{ 463 uint32_t i; 464 const XML_Char *value = NULL, *text = NULL; 465 driOptionValue v; 466 uint32_t opt = data->curOption; 467 for (i = 0; attr[i]; i += 2) { 468 if (!strcmp (attr[i], "value")) value = attr[i+1]; 469 else if (!strcmp (attr[i], "text")) text = attr[i+1]; 470 else XML_FATAL("illegal enum attribute: %s.", attr[i]); 471 } 472 if (!value) XML_FATAL1 ("value attribute missing in enum."); 473 if (!text) XML_FATAL1 ("text attribute missing in enum."); 474 if (!parseValue (&v, data->cache->info[opt].type, value)) 475 XML_FATAL ("illegal enum value: %s.", value); 476 if (!checkValue (&v, &data->cache->info[opt])) 477 XML_FATAL ("enum value out of valid range: %s.", value); 478} 479 480/** \brief Parse attributes of a description element. 481 * 482 * We're not actually interested in the data. Just make sure this is ok 483 * for external configuration tools. 484 */ 485static void 486parseDescAttr(struct OptInfoData *data, const XML_Char **attr) 487{ 488 uint32_t i; 489 const XML_Char *lang = NULL, *text = NULL; 490 for (i = 0; attr[i]; i += 2) { 491 if (!strcmp (attr[i], "lang")) lang = attr[i+1]; 492 else if (!strcmp (attr[i], "text")) text = attr[i+1]; 493 else XML_FATAL("illegal description attribute: %s.", attr[i]); 494 } 495 if (!lang) XML_FATAL1 ("lang attribute missing in description."); 496 if (!text) XML_FATAL1 ("text attribute missing in description."); 497} 498 499/** \brief Parse attributes of an option element. */ 500static void 501parseOptInfoAttr(struct OptInfoData *data, const XML_Char **attr) 502{ 503 enum OptAttr {OA_DEFAULT = 0, OA_NAME, OA_TYPE, OA_VALID, OA_COUNT}; 504 static const XML_Char *optAttr[] = {"default", "name", "type", "valid"}; 505 const XML_Char *attrVal[OA_COUNT] = {NULL, NULL, NULL, NULL}; 506 const char *defaultVal; 507 driOptionCache *cache = data->cache; 508 uint32_t opt, i; 509 for (i = 0; attr[i]; i += 2) { 510 uint32_t attrName = bsearchStr (attr[i], optAttr, OA_COUNT); 511 if (attrName >= OA_COUNT) 512 XML_FATAL ("illegal option attribute: %s", attr[i]); 513 attrVal[attrName] = attr[i+1]; 514 } 515 if (!attrVal[OA_NAME]) XML_FATAL1 ("name attribute missing in option."); 516 if (!attrVal[OA_TYPE]) XML_FATAL1 ("type attribute missing in option."); 517 if (!attrVal[OA_DEFAULT]) XML_FATAL1 ("default attribute missing in option."); 518 519 opt = findOption (cache, attrVal[OA_NAME]); 520 if (cache->info[opt].name) 521 XML_FATAL ("option %s redefined.", attrVal[OA_NAME]); 522 data->curOption = opt; 523 524 XSTRDUP (cache->info[opt].name, attrVal[OA_NAME]); 525 526 if (!strcmp (attrVal[OA_TYPE], "bool")) 527 cache->info[opt].type = DRI_BOOL; 528 else if (!strcmp (attrVal[OA_TYPE], "enum")) 529 cache->info[opt].type = DRI_ENUM; 530 else if (!strcmp (attrVal[OA_TYPE], "int")) 531 cache->info[opt].type = DRI_INT; 532 else if (!strcmp (attrVal[OA_TYPE], "float")) 533 cache->info[opt].type = DRI_FLOAT; 534 else if (!strcmp (attrVal[OA_TYPE], "string")) 535 cache->info[opt].type = DRI_STRING; 536 else 537 XML_FATAL ("illegal type in option: %s.", attrVal[OA_TYPE]); 538 539 defaultVal = getenv (cache->info[opt].name); 540 if (defaultVal != NULL) { 541 /* don't use XML_WARNING, we want the user to see this! */ 542 fprintf (stderr, 543 "ATTENTION: default value of option %s overridden by environment.\n", 544 cache->info[opt].name); 545 } else 546 defaultVal = attrVal[OA_DEFAULT]; 547 if (!parseValue (&cache->values[opt], cache->info[opt].type, defaultVal)) 548 XML_FATAL ("illegal default value for %s: %s.", cache->info[opt].name, defaultVal); 549 550 if (attrVal[OA_VALID]) { 551 if (cache->info[opt].type == DRI_BOOL) 552 XML_FATAL1 ("boolean option with valid attribute."); 553 if (!parseRanges (&cache->info[opt], attrVal[OA_VALID])) 554 XML_FATAL ("illegal valid attribute: %s.", attrVal[OA_VALID]); 555 if (!checkValue (&cache->values[opt], &cache->info[opt])) 556 XML_FATAL ("default value out of valid range '%s': %s.", 557 attrVal[OA_VALID], defaultVal); 558 } else if (cache->info[opt].type == DRI_ENUM) { 559 XML_FATAL1 ("valid attribute missing in option (mandatory for enums)."); 560 } else { 561 cache->info[opt].nRanges = 0; 562 cache->info[opt].ranges = NULL; 563 } 564} 565 566/** \brief Handler for start element events. */ 567static void 568optInfoStartElem(void *userData, const XML_Char *name, const XML_Char **attr) 569{ 570 struct OptInfoData *data = (struct OptInfoData *)userData; 571 enum OptInfoElem elem = bsearchStr (name, OptInfoElems, OI_COUNT); 572 switch (elem) { 573 case OI_DRIINFO: 574 if (data->inDriInfo) 575 XML_FATAL1 ("nested <driinfo> elements."); 576 if (attr[0]) 577 XML_FATAL1 ("attributes specified on <driinfo> element."); 578 data->inDriInfo = true; 579 break; 580 case OI_SECTION: 581 if (!data->inDriInfo) 582 XML_FATAL1 ("<section> must be inside <driinfo>."); 583 if (data->inSection) 584 XML_FATAL1 ("nested <section> elements."); 585 if (attr[0]) 586 XML_FATAL1 ("attributes specified on <section> element."); 587 data->inSection = true; 588 break; 589 case OI_DESCRIPTION: 590 if (!data->inSection && !data->inOption) 591 XML_FATAL1 ("<description> must be inside <description> or <option."); 592 if (data->inDesc) 593 XML_FATAL1 ("nested <description> elements."); 594 data->inDesc = true; 595 parseDescAttr (data, attr); 596 break; 597 case OI_OPTION: 598 if (!data->inSection) 599 XML_FATAL1 ("<option> must be inside <section>."); 600 if (data->inDesc) 601 XML_FATAL1 ("<option> nested in <description> element."); 602 if (data->inOption) 603 XML_FATAL1 ("nested <option> elements."); 604 data->inOption = true; 605 parseOptInfoAttr (data, attr); 606 break; 607 case OI_ENUM: 608 if (!(data->inOption && data->inDesc)) 609 XML_FATAL1 ("<enum> must be inside <option> and <description>."); 610 if (data->inEnum) 611 XML_FATAL1 ("nested <enum> elements."); 612 data->inEnum = true; 613 parseEnumAttr (data, attr); 614 break; 615 default: 616 XML_FATAL ("unknown element: %s.", name); 617 } 618} 619 620/** \brief Handler for end element events. */ 621static void 622optInfoEndElem(void *userData, const XML_Char *name) 623{ 624 struct OptInfoData *data = (struct OptInfoData *)userData; 625 enum OptInfoElem elem = bsearchStr (name, OptInfoElems, OI_COUNT); 626 switch (elem) { 627 case OI_DRIINFO: 628 data->inDriInfo = false; 629 break; 630 case OI_SECTION: 631 data->inSection = false; 632 break; 633 case OI_DESCRIPTION: 634 data->inDesc = false; 635 break; 636 case OI_OPTION: 637 data->inOption = false; 638 break; 639 case OI_ENUM: 640 data->inEnum = false; 641 break; 642 default: 643 assert (0); /* should have been caught by StartElem */ 644 } 645} 646 647void 648driParseOptionInfo(driOptionCache *info, const char *configOptions) 649{ 650 XML_Parser p; 651 int status; 652 struct OptInfoData userData; 653 struct OptInfoData *data = &userData; 654 655 /* Make the hash table big enough to fit more than the maximum number of 656 * config options we've ever seen in a driver. 657 */ 658 info->tableSize = 6; 659 info->info = calloc(1 << info->tableSize, sizeof (driOptionInfo)); 660 info->values = calloc(1 << info->tableSize, sizeof (driOptionValue)); 661 if (info->info == NULL || info->values == NULL) { 662 fprintf (stderr, "%s: %d: out of memory.\n", __FILE__, __LINE__); 663 abort(); 664 } 665 666 p = XML_ParserCreate ("UTF-8"); /* always UTF-8 */ 667 XML_SetElementHandler (p, optInfoStartElem, optInfoEndElem); 668 XML_SetUserData (p, data); 669 670 userData.name = "__driConfigOptions"; 671 userData.parser = p; 672 userData.cache = info; 673 userData.inDriInfo = false; 674 userData.inSection = false; 675 userData.inDesc = false; 676 userData.inOption = false; 677 userData.inEnum = false; 678 userData.curOption = -1; 679 680 status = XML_Parse (p, configOptions, strlen (configOptions), 1); 681 if (!status) 682 XML_FATAL ("%s.", XML_ErrorString(XML_GetErrorCode(p))); 683 684 XML_ParserFree (p); 685} 686 687/** \brief Parser context for configuration files. */ 688struct OptConfData { 689 const char *name; 690 XML_Parser parser; 691 driOptionCache *cache; 692 int screenNum; 693 const char *driverName, *execName; 694 const char *kernelDriverName; 695 uint32_t ignoringDevice; 696 uint32_t ignoringApp; 697 uint32_t inDriConf; 698 uint32_t inDevice; 699 uint32_t inApp; 700 uint32_t inOption; 701}; 702 703/** \brief Elements in configuration files. */ 704enum OptConfElem { 705 OC_APPLICATION = 0, OC_DEVICE, OC_DRICONF, OC_OPTION, OC_COUNT 706}; 707static const XML_Char *OptConfElems[] = { 708 [OC_APPLICATION] = "application", 709 [OC_DEVICE] = "device", 710 [OC_DRICONF] = "driconf", 711 [OC_OPTION] = "option", 712}; 713 714/** \brief Parse attributes of a device element. */ 715static void 716parseDeviceAttr(struct OptConfData *data, const XML_Char **attr) 717{ 718 uint32_t i; 719 const XML_Char *driver = NULL, *screen = NULL, *kernel = NULL; 720 for (i = 0; attr[i]; i += 2) { 721 if (!strcmp (attr[i], "driver")) driver = attr[i+1]; 722 else if (!strcmp (attr[i], "screen")) screen = attr[i+1]; 723 else if (!strcmp (attr[i], "kernel_driver")) kernel = attr[i+1]; 724 else XML_WARNING("unknown device attribute: %s.", attr[i]); 725 } 726 if (driver && strcmp (driver, data->driverName)) 727 data->ignoringDevice = data->inDevice; 728 else if (kernel && (!data->kernelDriverName || strcmp (kernel, data->kernelDriverName))) 729 data->ignoringDevice = data->inDevice; 730 else if (screen) { 731 driOptionValue screenNum; 732 if (!parseValue (&screenNum, DRI_INT, screen)) 733 XML_WARNING("illegal screen number: %s.", screen); 734 else if (screenNum._int != data->screenNum) 735 data->ignoringDevice = data->inDevice; 736 } 737} 738 739/** \brief Parse attributes of an application element. */ 740static void 741parseAppAttr(struct OptConfData *data, const XML_Char **attr) 742{ 743 uint32_t i; 744 const XML_Char *exec = NULL; 745 for (i = 0; attr[i]; i += 2) { 746 if (!strcmp (attr[i], "name")) /* not needed here */; 747 else if (!strcmp (attr[i], "executable")) exec = attr[i+1]; 748 else XML_WARNING("unknown application attribute: %s.", attr[i]); 749 } 750 if (exec && strcmp (exec, data->execName)) 751 data->ignoringApp = data->inApp; 752} 753 754/** \brief Parse attributes of an option element. */ 755static void 756parseOptConfAttr(struct OptConfData *data, const XML_Char **attr) 757{ 758 uint32_t i; 759 const XML_Char *name = NULL, *value = NULL; 760 for (i = 0; attr[i]; i += 2) { 761 if (!strcmp (attr[i], "name")) name = attr[i+1]; 762 else if (!strcmp (attr[i], "value")) value = attr[i+1]; 763 else XML_WARNING("unknown option attribute: %s.", attr[i]); 764 } 765 if (!name) XML_WARNING1 ("name attribute missing in option."); 766 if (!value) XML_WARNING1 ("value attribute missing in option."); 767 if (name && value) { 768 driOptionCache *cache = data->cache; 769 uint32_t opt = findOption (cache, name); 770 if (cache->info[opt].name == NULL) 771 /* don't use XML_WARNING, drirc defines options for all drivers, 772 * but not all drivers support them */ 773 return; 774 else if (getenv (cache->info[opt].name)) 775 /* don't use XML_WARNING, we want the user to see this! */ 776 fprintf (stderr, "ATTENTION: option value of option %s ignored.\n", 777 cache->info[opt].name); 778 else if (!parseValue (&cache->values[opt], cache->info[opt].type, value)) 779 XML_WARNING ("illegal option value: %s.", value); 780 } 781} 782 783/** \brief Handler for start element events. */ 784static void 785optConfStartElem(void *userData, const XML_Char *name, 786 const XML_Char **attr) 787{ 788 struct OptConfData *data = (struct OptConfData *)userData; 789 enum OptConfElem elem = bsearchStr (name, OptConfElems, OC_COUNT); 790 switch (elem) { 791 case OC_DRICONF: 792 if (data->inDriConf) 793 XML_WARNING1 ("nested <driconf> elements."); 794 if (attr[0]) 795 XML_WARNING1 ("attributes specified on <driconf> element."); 796 data->inDriConf++; 797 break; 798 case OC_DEVICE: 799 if (!data->inDriConf) 800 XML_WARNING1 ("<device> should be inside <driconf>."); 801 if (data->inDevice) 802 XML_WARNING1 ("nested <device> elements."); 803 data->inDevice++; 804 if (!data->ignoringDevice && !data->ignoringApp) 805 parseDeviceAttr (data, attr); 806 break; 807 case OC_APPLICATION: 808 if (!data->inDevice) 809 XML_WARNING1 ("<application> should be inside <device>."); 810 if (data->inApp) 811 XML_WARNING1 ("nested <application> elements."); 812 data->inApp++; 813 if (!data->ignoringDevice && !data->ignoringApp) 814 parseAppAttr (data, attr); 815 break; 816 case OC_OPTION: 817 if (!data->inApp) 818 XML_WARNING1 ("<option> should be inside <application>."); 819 if (data->inOption) 820 XML_WARNING1 ("nested <option> elements."); 821 data->inOption++; 822 if (!data->ignoringDevice && !data->ignoringApp) 823 parseOptConfAttr (data, attr); 824 break; 825 default: 826 XML_WARNING ("unknown element: %s.", name); 827 } 828} 829 830/** \brief Handler for end element events. */ 831static void 832optConfEndElem(void *userData, const XML_Char *name) 833{ 834 struct OptConfData *data = (struct OptConfData *)userData; 835 enum OptConfElem elem = bsearchStr (name, OptConfElems, OC_COUNT); 836 switch (elem) { 837 case OC_DRICONF: 838 data->inDriConf--; 839 break; 840 case OC_DEVICE: 841 if (data->inDevice-- == data->ignoringDevice) 842 data->ignoringDevice = 0; 843 break; 844 case OC_APPLICATION: 845 if (data->inApp-- == data->ignoringApp) 846 data->ignoringApp = 0; 847 break; 848 case OC_OPTION: 849 data->inOption--; 850 break; 851 default: 852 /* unknown element, warning was produced on start tag */; 853 } 854} 855 856/** \brief Initialize an option cache based on info */ 857static void 858initOptionCache(driOptionCache *cache, const driOptionCache *info) 859{ 860 unsigned i, size = 1 << info->tableSize; 861 cache->info = info->info; 862 cache->tableSize = info->tableSize; 863 cache->values = malloc((1<<info->tableSize) * sizeof (driOptionValue)); 864 if (cache->values == NULL) { 865 fprintf (stderr, "%s: %d: out of memory.\n", __FILE__, __LINE__); 866 abort(); 867 } 868 memcpy (cache->values, info->values, 869 (1<<info->tableSize) * sizeof (driOptionValue)); 870 for (i = 0; i < size; ++i) { 871 if (cache->info[i].type == DRI_STRING) 872 XSTRDUP(cache->values[i]._string, info->values[i]._string); 873 } 874} 875 876static void 877_parseOneConfigFile(XML_Parser p) 878{ 879#define BUF_SIZE 0x1000 880 struct OptConfData *data = (struct OptConfData *)XML_GetUserData (p); 881 int status; 882 int fd; 883 884 if ((fd = open (data->name, O_RDONLY)) == -1) { 885 __driUtilMessage ("Can't open configuration file %s: %s.", 886 data->name, strerror (errno)); 887 return; 888 } 889 890 while (1) { 891 int bytesRead; 892 void *buffer = XML_GetBuffer (p, BUF_SIZE); 893 if (!buffer) { 894 __driUtilMessage ("Can't allocate parser buffer."); 895 break; 896 } 897 bytesRead = read (fd, buffer, BUF_SIZE); 898 if (bytesRead == -1) { 899 __driUtilMessage ("Error reading from configuration file %s: %s.", 900 data->name, strerror (errno)); 901 break; 902 } 903 status = XML_ParseBuffer (p, bytesRead, bytesRead == 0); 904 if (!status) { 905 XML_ERROR ("%s.", XML_ErrorString(XML_GetErrorCode(p))); 906 break; 907 } 908 if (bytesRead == 0) 909 break; 910 } 911 912 close (fd); 913#undef BUF_SIZE 914} 915 916/** \brief Parse the named configuration file */ 917static void 918parseOneConfigFile(struct OptConfData *data, const char *filename) 919{ 920 XML_Parser p; 921 922 p = XML_ParserCreate (NULL); /* use encoding specified by file */ 923 XML_SetElementHandler (p, optConfStartElem, optConfEndElem); 924 XML_SetUserData (p, data); 925 data->parser = p; 926 data->name = filename; 927 data->ignoringDevice = 0; 928 data->ignoringApp = 0; 929 data->inDriConf = 0; 930 data->inDevice = 0; 931 data->inApp = 0; 932 data->inOption = 0; 933 934 _parseOneConfigFile (p); 935 XML_ParserFree (p); 936} 937 938static int 939scandir_filter(const struct dirent *ent) 940{ 941#ifndef DT_REG /* systems without d_type in dirent results */ 942 struct stat st; 943 944 if ((lstat(ent->d_name, &st) != 0) || 945 (!S_ISREG(st.st_mode) && !S_ISLNK(st.st_mode))) 946 return 0; 947#else 948 if (ent->d_type != DT_REG && ent->d_type != DT_LNK) 949 return 0; 950#endif 951 952 if (fnmatch("*.conf", ent->d_name, 0)) 953 return 0; 954 955 return 1; 956} 957 958/** \brief Parse configuration files in a directory */ 959static void 960parseConfigDir(struct OptConfData *data, const char *dirname) 961{ 962 int i, count; 963 struct dirent **entries = NULL; 964 965 count = scandir(dirname, &entries, scandir_filter, alphasort); 966 if (count < 0) 967 return; 968 969 for (i = 0; i < count; i++) { 970 char filename[PATH_MAX]; 971 972 snprintf(filename, PATH_MAX, "%s/%s", dirname, entries[i]->d_name); 973 free(entries[i]); 974 975 parseOneConfigFile(data, filename); 976 } 977 978 free(entries); 979} 980 981#ifndef SYSCONFDIR 982#define SYSCONFDIR "/etc" 983#endif 984 985#ifndef DATADIR 986#define DATADIR "/usr/share" 987#endif 988 989void 990driParseConfigFiles(driOptionCache *cache, const driOptionCache *info, 991 int screenNum, const char *driverName, 992 const char *kernelDriverName) 993{ 994 char *home; 995 struct OptConfData userData; 996 997 initOptionCache (cache, info); 998 999 userData.cache = cache; 1000 userData.screenNum = screenNum; 1001 userData.driverName = driverName; 1002 userData.kernelDriverName = kernelDriverName; 1003 userData.execName = util_get_process_name(); 1004 1005 parseConfigDir(&userData, DATADIR "/drirc.d"); 1006 parseOneConfigFile(&userData, SYSCONFDIR "/drirc"); 1007 1008 if ((home = getenv ("HOME"))) { 1009 char filename[PATH_MAX]; 1010 1011 snprintf(filename, PATH_MAX, "%s/.drirc", home); 1012 parseOneConfigFile(&userData, filename); 1013 } 1014} 1015 1016void 1017driDestroyOptionInfo(driOptionCache *info) 1018{ 1019 driDestroyOptionCache(info); 1020 if (info->info) { 1021 uint32_t i, size = 1 << info->tableSize; 1022 for (i = 0; i < size; ++i) { 1023 if (info->info[i].name) { 1024 free(info->info[i].name); 1025 free(info->info[i].ranges); 1026 } 1027 } 1028 free(info->info); 1029 } 1030} 1031 1032void 1033driDestroyOptionCache(driOptionCache *cache) 1034{ 1035 if (cache->info) { 1036 unsigned i, size = 1 << cache->tableSize; 1037 for (i = 0; i < size; ++i) { 1038 if (cache->info[i].type == DRI_STRING) 1039 free(cache->values[i]._string); 1040 } 1041 } 1042 free(cache->values); 1043} 1044 1045unsigned char 1046driCheckOption(const driOptionCache *cache, const char *name, 1047 driOptionType type) 1048{ 1049 uint32_t i = findOption (cache, name); 1050 return cache->info[i].name != NULL && cache->info[i].type == type; 1051} 1052 1053unsigned char 1054driQueryOptionb(const driOptionCache *cache, const char *name) 1055{ 1056 uint32_t i = findOption (cache, name); 1057 /* make sure the option is defined and has the correct type */ 1058 assert (cache->info[i].name != NULL); 1059 assert (cache->info[i].type == DRI_BOOL); 1060 return cache->values[i]._bool; 1061} 1062 1063int 1064driQueryOptioni(const driOptionCache *cache, const char *name) 1065{ 1066 uint32_t i = findOption (cache, name); 1067 /* make sure the option is defined and has the correct type */ 1068 assert (cache->info[i].name != NULL); 1069 assert (cache->info[i].type == DRI_INT || cache->info[i].type == DRI_ENUM); 1070 return cache->values[i]._int; 1071} 1072 1073float 1074driQueryOptionf(const driOptionCache *cache, const char *name) 1075{ 1076 uint32_t i = findOption (cache, name); 1077 /* make sure the option is defined and has the correct type */ 1078 assert (cache->info[i].name != NULL); 1079 assert (cache->info[i].type == DRI_FLOAT); 1080 return cache->values[i]._float; 1081} 1082 1083char * 1084driQueryOptionstr(const driOptionCache *cache, const char *name) 1085{ 1086 uint32_t i = findOption (cache, name); 1087 /* make sure the option is defined and has the correct type */ 1088 assert (cache->info[i].name != NULL); 1089 assert (cache->info[i].type == DRI_STRING); 1090 return cache->values[i]._string; 1091} 1092