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