xmlconfig.c revision 8a1362ad
101e04c3fSmrg/* 201e04c3fSmrg * XML DRI client-side driver configuration 301e04c3fSmrg * Copyright (C) 2003 Felix Kuehling 401e04c3fSmrg * 501e04c3fSmrg * Permission is hereby granted, free of charge, to any person obtaining a 601e04c3fSmrg * copy of this software and associated documentation files (the "Software"), 701e04c3fSmrg * to deal in the Software without restriction, including without limitation 801e04c3fSmrg * the rights to use, copy, modify, merge, publish, distribute, sublicense, 901e04c3fSmrg * and/or sell copies of the Software, and to permit persons to whom the 1001e04c3fSmrg * Software is furnished to do so, subject to the following conditions: 1101e04c3fSmrg * 1201e04c3fSmrg * The above copyright notice and this permission notice shall be included 1301e04c3fSmrg * in all copies or substantial portions of the Software. 1401e04c3fSmrg * 1501e04c3fSmrg * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS 1601e04c3fSmrg * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 1701e04c3fSmrg * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL 1801e04c3fSmrg * FELIX KUEHLING, OR ANY OTHER CONTRIBUTORS BE LIABLE FOR ANY CLAIM, 1901e04c3fSmrg * DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR 2001e04c3fSmrg * OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE 2101e04c3fSmrg * OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. 2201e04c3fSmrg * 2301e04c3fSmrg */ 2401e04c3fSmrg/** 2501e04c3fSmrg * \file xmlconfig.c 2601e04c3fSmrg * \brief Driver-independent client-side part of the XML configuration 2701e04c3fSmrg * \author Felix Kuehling 2801e04c3fSmrg */ 2901e04c3fSmrg 3001e04c3fSmrg#include <limits.h> 3101e04c3fSmrg#include <stdarg.h> 3201e04c3fSmrg#include <stdio.h> 3301e04c3fSmrg#include <string.h> 3401e04c3fSmrg#include <assert.h> 3501e04c3fSmrg#include <expat.h> 3601e04c3fSmrg#include <fcntl.h> 3701e04c3fSmrg#include <math.h> 3801e04c3fSmrg#include <unistd.h> 3901e04c3fSmrg#include <errno.h> 4001e04c3fSmrg#include <dirent.h> 4101e04c3fSmrg#include <fnmatch.h> 4201e04c3fSmrg#include "xmlconfig.h" 4301e04c3fSmrg#include "u_process.h" 4401e04c3fSmrg 458a1362adSmaya/* For systems like Hurd */ 468a1362adSmaya#ifndef PATH_MAX 478a1362adSmaya#define PATH_MAX 4096 488a1362adSmaya#endif 4901e04c3fSmrg 5001e04c3fSmrg/** \brief Find an option in an option cache with the name as key */ 5101e04c3fSmrgstatic uint32_t 5201e04c3fSmrgfindOption(const driOptionCache *cache, const char *name) 5301e04c3fSmrg{ 5401e04c3fSmrg uint32_t len = strlen (name); 5501e04c3fSmrg uint32_t size = 1 << cache->tableSize, mask = size - 1; 5601e04c3fSmrg uint32_t hash = 0; 5701e04c3fSmrg uint32_t i, shift; 5801e04c3fSmrg 5901e04c3fSmrg /* compute a hash from the variable length name */ 6001e04c3fSmrg for (i = 0, shift = 0; i < len; ++i, shift = (shift+8) & 31) 6101e04c3fSmrg hash += (uint32_t)name[i] << shift; 6201e04c3fSmrg hash *= hash; 6301e04c3fSmrg hash = (hash >> (16-cache->tableSize/2)) & mask; 6401e04c3fSmrg 6501e04c3fSmrg /* this is just the starting point of the linear search for the option */ 6601e04c3fSmrg for (i = 0; i < size; ++i, hash = (hash+1) & mask) { 6701e04c3fSmrg /* if we hit an empty entry then the option is not defined (yet) */ 6801e04c3fSmrg if (cache->info[hash].name == 0) 6901e04c3fSmrg break; 7001e04c3fSmrg else if (!strcmp (name, cache->info[hash].name)) 7101e04c3fSmrg break; 7201e04c3fSmrg } 7301e04c3fSmrg /* this assertion fails if the hash table is full */ 7401e04c3fSmrg assert (i < size); 7501e04c3fSmrg 7601e04c3fSmrg return hash; 7701e04c3fSmrg} 7801e04c3fSmrg 7901e04c3fSmrg/** \brief Like strdup but using malloc and with error checking. */ 8001e04c3fSmrg#define XSTRDUP(dest,source) do { \ 8101e04c3fSmrg uint32_t len = strlen (source); \ 8201e04c3fSmrg if (!(dest = malloc(len+1))) { \ 8301e04c3fSmrg fprintf (stderr, "%s: %d: out of memory.\n", __FILE__, __LINE__); \ 8401e04c3fSmrg abort(); \ 8501e04c3fSmrg } \ 8601e04c3fSmrg memcpy (dest, source, len+1); \ 8701e04c3fSmrg} while (0) 8801e04c3fSmrg 8901e04c3fSmrgstatic int compare (const void *a, const void *b) { 9001e04c3fSmrg return strcmp (*(char *const*)a, *(char *const*)b); 9101e04c3fSmrg} 9201e04c3fSmrg/** \brief Binary search in a string array. */ 9301e04c3fSmrgstatic uint32_t 9401e04c3fSmrgbsearchStr (const XML_Char *name, const XML_Char *elems[], uint32_t count) 9501e04c3fSmrg{ 9601e04c3fSmrg const XML_Char **found; 9701e04c3fSmrg found = bsearch (&name, elems, count, sizeof (XML_Char *), compare); 9801e04c3fSmrg if (found) 9901e04c3fSmrg return found - elems; 10001e04c3fSmrg else 10101e04c3fSmrg return count; 10201e04c3fSmrg} 10301e04c3fSmrg 10401e04c3fSmrg/** \brief Locale-independent integer parser. 10501e04c3fSmrg * 10601e04c3fSmrg * Works similar to strtol. Leading space is NOT skipped. The input 10701e04c3fSmrg * number may have an optional sign. Radix is specified by base. If 10801e04c3fSmrg * base is 0 then decimal is assumed unless the input number is 10901e04c3fSmrg * prefixed by 0x or 0X for hexadecimal or 0 for octal. After 11001e04c3fSmrg * returning tail points to the first character that is not part of 11101e04c3fSmrg * the integer number. If no number was found then tail points to the 11201e04c3fSmrg * start of the input string. */ 11301e04c3fSmrgstatic int 11401e04c3fSmrgstrToI(const XML_Char *string, const XML_Char **tail, int base) 11501e04c3fSmrg{ 11601e04c3fSmrg int radix = base == 0 ? 10 : base; 11701e04c3fSmrg int result = 0; 11801e04c3fSmrg int sign = 1; 11901e04c3fSmrg bool numberFound = false; 12001e04c3fSmrg const XML_Char *start = string; 12101e04c3fSmrg 12201e04c3fSmrg assert (radix >= 2 && radix <= 36); 12301e04c3fSmrg 12401e04c3fSmrg if (*string == '-') { 12501e04c3fSmrg sign = -1; 12601e04c3fSmrg string++; 12701e04c3fSmrg } else if (*string == '+') 12801e04c3fSmrg string++; 12901e04c3fSmrg if (base == 0 && *string == '0') { 13001e04c3fSmrg numberFound = true; 13101e04c3fSmrg if (*(string+1) == 'x' || *(string+1) == 'X') { 13201e04c3fSmrg radix = 16; 13301e04c3fSmrg string += 2; 13401e04c3fSmrg } else { 13501e04c3fSmrg radix = 8; 13601e04c3fSmrg string++; 13701e04c3fSmrg } 13801e04c3fSmrg } 13901e04c3fSmrg do { 14001e04c3fSmrg int digit = -1; 14101e04c3fSmrg if (radix <= 10) { 14201e04c3fSmrg if (*string >= '0' && *string < '0' + radix) 14301e04c3fSmrg digit = *string - '0'; 14401e04c3fSmrg } else { 14501e04c3fSmrg if (*string >= '0' && *string <= '9') 14601e04c3fSmrg digit = *string - '0'; 14701e04c3fSmrg else if (*string >= 'a' && *string < 'a' + radix - 10) 14801e04c3fSmrg digit = *string - 'a' + 10; 14901e04c3fSmrg else if (*string >= 'A' && *string < 'A' + radix - 10) 15001e04c3fSmrg digit = *string - 'A' + 10; 15101e04c3fSmrg } 15201e04c3fSmrg if (digit != -1) { 15301e04c3fSmrg numberFound = true; 15401e04c3fSmrg result = radix*result + digit; 15501e04c3fSmrg string++; 15601e04c3fSmrg } else 15701e04c3fSmrg break; 15801e04c3fSmrg } while (true); 15901e04c3fSmrg *tail = numberFound ? string : start; 16001e04c3fSmrg return sign * result; 16101e04c3fSmrg} 16201e04c3fSmrg 16301e04c3fSmrg/** \brief Locale-independent floating-point parser. 16401e04c3fSmrg * 16501e04c3fSmrg * Works similar to strtod. Leading space is NOT skipped. The input 16601e04c3fSmrg * number may have an optional sign. '.' is interpreted as decimal 16701e04c3fSmrg * point and may occur at most once. Optionally the number may end in 16801e04c3fSmrg * [eE]<exponent>, where <exponent> is an integer as recognized by 16901e04c3fSmrg * strToI. In that case the result is number * 10^exponent. After 17001e04c3fSmrg * returning tail points to the first character that is not part of 17101e04c3fSmrg * the floating point number. If no number was found then tail points 17201e04c3fSmrg * to the start of the input string. 17301e04c3fSmrg * 17401e04c3fSmrg * Uses two passes for maximum accuracy. */ 17501e04c3fSmrgstatic float 17601e04c3fSmrgstrToF(const XML_Char *string, const XML_Char **tail) 17701e04c3fSmrg{ 17801e04c3fSmrg int nDigits = 0, pointPos, exponent; 17901e04c3fSmrg float sign = 1.0f, result = 0.0f, scale; 18001e04c3fSmrg const XML_Char *start = string, *numStart; 18101e04c3fSmrg 18201e04c3fSmrg /* sign */ 18301e04c3fSmrg if (*string == '-') { 18401e04c3fSmrg sign = -1.0f; 18501e04c3fSmrg string++; 18601e04c3fSmrg } else if (*string == '+') 18701e04c3fSmrg string++; 18801e04c3fSmrg 18901e04c3fSmrg /* first pass: determine position of decimal point, number of 19001e04c3fSmrg * digits, exponent and the end of the number. */ 19101e04c3fSmrg numStart = string; 19201e04c3fSmrg while (*string >= '0' && *string <= '9') { 19301e04c3fSmrg string++; 19401e04c3fSmrg nDigits++; 19501e04c3fSmrg } 19601e04c3fSmrg pointPos = nDigits; 19701e04c3fSmrg if (*string == '.') { 19801e04c3fSmrg string++; 19901e04c3fSmrg while (*string >= '0' && *string <= '9') { 20001e04c3fSmrg string++; 20101e04c3fSmrg nDigits++; 20201e04c3fSmrg } 20301e04c3fSmrg } 20401e04c3fSmrg if (nDigits == 0) { 20501e04c3fSmrg /* no digits, no number */ 20601e04c3fSmrg *tail = start; 20701e04c3fSmrg return 0.0f; 20801e04c3fSmrg } 20901e04c3fSmrg *tail = string; 21001e04c3fSmrg if (*string == 'e' || *string == 'E') { 21101e04c3fSmrg const XML_Char *expTail; 21201e04c3fSmrg exponent = strToI (string+1, &expTail, 10); 21301e04c3fSmrg if (expTail == string+1) 21401e04c3fSmrg exponent = 0; 21501e04c3fSmrg else 21601e04c3fSmrg *tail = expTail; 21701e04c3fSmrg } else 21801e04c3fSmrg exponent = 0; 21901e04c3fSmrg string = numStart; 22001e04c3fSmrg 22101e04c3fSmrg /* scale of the first digit */ 22201e04c3fSmrg scale = sign * (float)pow (10.0, (double)(pointPos-1 + exponent)); 22301e04c3fSmrg 22401e04c3fSmrg /* second pass: parse digits */ 22501e04c3fSmrg do { 22601e04c3fSmrg if (*string != '.') { 22701e04c3fSmrg assert (*string >= '0' && *string <= '9'); 22801e04c3fSmrg result += scale * (float)(*string - '0'); 22901e04c3fSmrg scale *= 0.1f; 23001e04c3fSmrg nDigits--; 23101e04c3fSmrg } 23201e04c3fSmrg string++; 23301e04c3fSmrg } while (nDigits > 0); 23401e04c3fSmrg 23501e04c3fSmrg return result; 23601e04c3fSmrg} 23701e04c3fSmrg 23801e04c3fSmrg/** \brief Parse a value of a given type. */ 23901e04c3fSmrgstatic unsigned char 24001e04c3fSmrgparseValue(driOptionValue *v, driOptionType type, const XML_Char *string) 24101e04c3fSmrg{ 24201e04c3fSmrg const XML_Char *tail = NULL; 24301e04c3fSmrg /* skip leading white-space */ 24401e04c3fSmrg string += strspn (string, " \f\n\r\t\v"); 24501e04c3fSmrg switch (type) { 24601e04c3fSmrg case DRI_BOOL: 24701e04c3fSmrg if (!strcmp (string, "false")) { 24801e04c3fSmrg v->_bool = false; 24901e04c3fSmrg tail = string + 5; 25001e04c3fSmrg } else if (!strcmp (string, "true")) { 25101e04c3fSmrg v->_bool = true; 25201e04c3fSmrg tail = string + 4; 25301e04c3fSmrg } 25401e04c3fSmrg else 25501e04c3fSmrg return false; 25601e04c3fSmrg break; 25701e04c3fSmrg case DRI_ENUM: /* enum is just a special integer */ 25801e04c3fSmrg case DRI_INT: 25901e04c3fSmrg v->_int = strToI (string, &tail, 0); 26001e04c3fSmrg break; 26101e04c3fSmrg case DRI_FLOAT: 26201e04c3fSmrg v->_float = strToF (string, &tail); 26301e04c3fSmrg break; 26401e04c3fSmrg case DRI_STRING: 26501e04c3fSmrg free (v->_string); 26601e04c3fSmrg v->_string = strndup(string, STRING_CONF_MAXLEN); 26701e04c3fSmrg return true; 26801e04c3fSmrg } 26901e04c3fSmrg 27001e04c3fSmrg if (tail == string) 27101e04c3fSmrg return false; /* empty string (or containing only white-space) */ 27201e04c3fSmrg /* skip trailing white space */ 27301e04c3fSmrg if (*tail) 27401e04c3fSmrg tail += strspn (tail, " \f\n\r\t\v"); 27501e04c3fSmrg if (*tail) 27601e04c3fSmrg return false; /* something left over that is not part of value */ 27701e04c3fSmrg 27801e04c3fSmrg return true; 27901e04c3fSmrg} 28001e04c3fSmrg 28101e04c3fSmrg/** \brief Parse a list of ranges of type info->type. */ 28201e04c3fSmrgstatic unsigned char 28301e04c3fSmrgparseRanges(driOptionInfo *info, const XML_Char *string) 28401e04c3fSmrg{ 28501e04c3fSmrg XML_Char *cp, *range; 28601e04c3fSmrg uint32_t nRanges, i; 28701e04c3fSmrg driOptionRange *ranges; 28801e04c3fSmrg 28901e04c3fSmrg XSTRDUP (cp, string); 29001e04c3fSmrg /* pass 1: determine the number of ranges (number of commas + 1) */ 29101e04c3fSmrg range = cp; 29201e04c3fSmrg for (nRanges = 1; *range; ++range) 29301e04c3fSmrg if (*range == ',') 29401e04c3fSmrg ++nRanges; 29501e04c3fSmrg 29601e04c3fSmrg if ((ranges = malloc(nRanges*sizeof(driOptionRange))) == NULL) { 29701e04c3fSmrg fprintf (stderr, "%s: %d: out of memory.\n", __FILE__, __LINE__); 29801e04c3fSmrg abort(); 29901e04c3fSmrg } 30001e04c3fSmrg 30101e04c3fSmrg /* pass 2: parse all ranges into preallocated array */ 30201e04c3fSmrg range = cp; 30301e04c3fSmrg for (i = 0; i < nRanges; ++i) { 30401e04c3fSmrg XML_Char *end, *sep; 30501e04c3fSmrg assert (range); 30601e04c3fSmrg end = strchr (range, ','); 30701e04c3fSmrg if (end) 30801e04c3fSmrg *end = '\0'; 30901e04c3fSmrg sep = strchr (range, ':'); 31001e04c3fSmrg if (sep) { /* non-empty interval */ 31101e04c3fSmrg *sep = '\0'; 31201e04c3fSmrg if (!parseValue (&ranges[i].start, info->type, range) || 31301e04c3fSmrg !parseValue (&ranges[i].end, info->type, sep+1)) 31401e04c3fSmrg break; 31501e04c3fSmrg if (info->type == DRI_INT && 31601e04c3fSmrg ranges[i].start._int > ranges[i].end._int) 31701e04c3fSmrg break; 31801e04c3fSmrg if (info->type == DRI_FLOAT && 31901e04c3fSmrg ranges[i].start._float > ranges[i].end._float) 32001e04c3fSmrg break; 32101e04c3fSmrg } else { /* empty interval */ 32201e04c3fSmrg if (!parseValue (&ranges[i].start, info->type, range)) 32301e04c3fSmrg break; 32401e04c3fSmrg ranges[i].end = ranges[i].start; 32501e04c3fSmrg } 32601e04c3fSmrg if (end) 32701e04c3fSmrg range = end+1; 32801e04c3fSmrg else 32901e04c3fSmrg range = NULL; 33001e04c3fSmrg } 33101e04c3fSmrg free(cp); 33201e04c3fSmrg if (i < nRanges) { 33301e04c3fSmrg free(ranges); 33401e04c3fSmrg return false; 33501e04c3fSmrg } else 33601e04c3fSmrg assert (range == NULL); 33701e04c3fSmrg 33801e04c3fSmrg info->nRanges = nRanges; 33901e04c3fSmrg info->ranges = ranges; 34001e04c3fSmrg return true; 34101e04c3fSmrg} 34201e04c3fSmrg 34301e04c3fSmrg/** \brief Check if a value is in one of info->ranges. */ 34401e04c3fSmrgstatic bool 34501e04c3fSmrgcheckValue(const driOptionValue *v, const driOptionInfo *info) 34601e04c3fSmrg{ 34701e04c3fSmrg uint32_t i; 34801e04c3fSmrg assert (info->type != DRI_BOOL); /* should be caught by the parser */ 34901e04c3fSmrg if (info->nRanges == 0) 35001e04c3fSmrg return true; 35101e04c3fSmrg switch (info->type) { 35201e04c3fSmrg case DRI_ENUM: /* enum is just a special integer */ 35301e04c3fSmrg case DRI_INT: 35401e04c3fSmrg for (i = 0; i < info->nRanges; ++i) 35501e04c3fSmrg if (v->_int >= info->ranges[i].start._int && 35601e04c3fSmrg v->_int <= info->ranges[i].end._int) 35701e04c3fSmrg return true; 35801e04c3fSmrg break; 35901e04c3fSmrg case DRI_FLOAT: 36001e04c3fSmrg for (i = 0; i < info->nRanges; ++i) 36101e04c3fSmrg if (v->_float >= info->ranges[i].start._float && 36201e04c3fSmrg v->_float <= info->ranges[i].end._float) 36301e04c3fSmrg return true; 36401e04c3fSmrg break; 36501e04c3fSmrg case DRI_STRING: 36601e04c3fSmrg break; 36701e04c3fSmrg default: 36801e04c3fSmrg assert (0); /* should never happen */ 36901e04c3fSmrg } 37001e04c3fSmrg return false; 37101e04c3fSmrg} 37201e04c3fSmrg 37301e04c3fSmrg/** 37401e04c3fSmrg * Print message to \c stderr if the \c LIBGL_DEBUG environment variable 37501e04c3fSmrg * is set. 37601e04c3fSmrg * 37701e04c3fSmrg * Is called from the drivers. 37801e04c3fSmrg * 37901e04c3fSmrg * \param f \c printf like format string. 38001e04c3fSmrg */ 38101e04c3fSmrgstatic void 38201e04c3fSmrg__driUtilMessage(const char *f, ...) 38301e04c3fSmrg{ 38401e04c3fSmrg va_list args; 38501e04c3fSmrg const char *libgl_debug; 38601e04c3fSmrg 38701e04c3fSmrg libgl_debug=getenv("LIBGL_DEBUG"); 38801e04c3fSmrg if (libgl_debug && !strstr(libgl_debug, "quiet")) { 38901e04c3fSmrg fprintf(stderr, "libGL: "); 39001e04c3fSmrg va_start(args, f); 39101e04c3fSmrg vfprintf(stderr, f, args); 39201e04c3fSmrg va_end(args); 39301e04c3fSmrg fprintf(stderr, "\n"); 39401e04c3fSmrg } 39501e04c3fSmrg} 39601e04c3fSmrg 39701e04c3fSmrg/** \brief Output a warning message. */ 39801e04c3fSmrg#define XML_WARNING1(msg) do {\ 39901e04c3fSmrg __driUtilMessage ("Warning in %s line %d, column %d: "msg, data->name, \ 40001e04c3fSmrg (int) XML_GetCurrentLineNumber(data->parser), \ 40101e04c3fSmrg (int) XML_GetCurrentColumnNumber(data->parser)); \ 40201e04c3fSmrg} while (0) 40301e04c3fSmrg#define XML_WARNING(msg, ...) do { \ 40401e04c3fSmrg __driUtilMessage ("Warning in %s line %d, column %d: "msg, data->name, \ 40501e04c3fSmrg (int) XML_GetCurrentLineNumber(data->parser), \ 40601e04c3fSmrg (int) XML_GetCurrentColumnNumber(data->parser), \ 40701e04c3fSmrg ##__VA_ARGS__); \ 40801e04c3fSmrg} while (0) 40901e04c3fSmrg/** \brief Output an error message. */ 41001e04c3fSmrg#define XML_ERROR1(msg) do { \ 41101e04c3fSmrg __driUtilMessage ("Error in %s line %d, column %d: "msg, data->name, \ 41201e04c3fSmrg (int) XML_GetCurrentLineNumber(data->parser), \ 41301e04c3fSmrg (int) XML_GetCurrentColumnNumber(data->parser)); \ 41401e04c3fSmrg} while (0) 41501e04c3fSmrg#define XML_ERROR(msg, ...) do { \ 41601e04c3fSmrg __driUtilMessage ("Error in %s line %d, column %d: "msg, data->name, \ 41701e04c3fSmrg (int) XML_GetCurrentLineNumber(data->parser), \ 41801e04c3fSmrg (int) XML_GetCurrentColumnNumber(data->parser), \ 41901e04c3fSmrg ##__VA_ARGS__); \ 42001e04c3fSmrg} while (0) 42101e04c3fSmrg/** \brief Output a fatal error message and abort. */ 42201e04c3fSmrg#define XML_FATAL1(msg) do { \ 42301e04c3fSmrg fprintf (stderr, "Fatal error in %s line %d, column %d: "msg"\n", \ 42401e04c3fSmrg data->name, \ 42501e04c3fSmrg (int) XML_GetCurrentLineNumber(data->parser), \ 42601e04c3fSmrg (int) XML_GetCurrentColumnNumber(data->parser)); \ 42701e04c3fSmrg abort();\ 42801e04c3fSmrg} while (0) 42901e04c3fSmrg#define XML_FATAL(msg, ...) do { \ 43001e04c3fSmrg fprintf (stderr, "Fatal error in %s line %d, column %d: "msg"\n", \ 43101e04c3fSmrg data->name, \ 43201e04c3fSmrg (int) XML_GetCurrentLineNumber(data->parser), \ 43301e04c3fSmrg (int) XML_GetCurrentColumnNumber(data->parser), \ 43401e04c3fSmrg ##__VA_ARGS__); \ 43501e04c3fSmrg abort();\ 43601e04c3fSmrg} while (0) 43701e04c3fSmrg 43801e04c3fSmrg/** \brief Parser context for __driConfigOptions. */ 43901e04c3fSmrgstruct OptInfoData { 44001e04c3fSmrg const char *name; 44101e04c3fSmrg XML_Parser parser; 44201e04c3fSmrg driOptionCache *cache; 44301e04c3fSmrg bool inDriInfo; 44401e04c3fSmrg bool inSection; 44501e04c3fSmrg bool inDesc; 44601e04c3fSmrg bool inOption; 44701e04c3fSmrg bool inEnum; 44801e04c3fSmrg int curOption; 44901e04c3fSmrg}; 45001e04c3fSmrg 45101e04c3fSmrg/** \brief Elements in __driConfigOptions. */ 45201e04c3fSmrgenum OptInfoElem { 45301e04c3fSmrg OI_DESCRIPTION = 0, OI_DRIINFO, OI_ENUM, OI_OPTION, OI_SECTION, OI_COUNT 45401e04c3fSmrg}; 45501e04c3fSmrgstatic const XML_Char *OptInfoElems[] = { 45601e04c3fSmrg "description", "driinfo", "enum", "option", "section" 45701e04c3fSmrg}; 45801e04c3fSmrg 45901e04c3fSmrg/** \brief Parse attributes of an enum element. 46001e04c3fSmrg * 46101e04c3fSmrg * We're not actually interested in the data. Just make sure this is ok 46201e04c3fSmrg * for external configuration tools. 46301e04c3fSmrg */ 46401e04c3fSmrgstatic void 46501e04c3fSmrgparseEnumAttr(struct OptInfoData *data, const XML_Char **attr) 46601e04c3fSmrg{ 46701e04c3fSmrg uint32_t i; 46801e04c3fSmrg const XML_Char *value = NULL, *text = NULL; 46901e04c3fSmrg driOptionValue v; 47001e04c3fSmrg uint32_t opt = data->curOption; 47101e04c3fSmrg for (i = 0; attr[i]; i += 2) { 47201e04c3fSmrg if (!strcmp (attr[i], "value")) value = attr[i+1]; 47301e04c3fSmrg else if (!strcmp (attr[i], "text")) text = attr[i+1]; 47401e04c3fSmrg else XML_FATAL("illegal enum attribute: %s.", attr[i]); 47501e04c3fSmrg } 47601e04c3fSmrg if (!value) XML_FATAL1 ("value attribute missing in enum."); 47701e04c3fSmrg if (!text) XML_FATAL1 ("text attribute missing in enum."); 47801e04c3fSmrg if (!parseValue (&v, data->cache->info[opt].type, value)) 47901e04c3fSmrg XML_FATAL ("illegal enum value: %s.", value); 48001e04c3fSmrg if (!checkValue (&v, &data->cache->info[opt])) 48101e04c3fSmrg XML_FATAL ("enum value out of valid range: %s.", value); 48201e04c3fSmrg} 48301e04c3fSmrg 48401e04c3fSmrg/** \brief Parse attributes of a description element. 48501e04c3fSmrg * 48601e04c3fSmrg * We're not actually interested in the data. Just make sure this is ok 48701e04c3fSmrg * for external configuration tools. 48801e04c3fSmrg */ 48901e04c3fSmrgstatic void 49001e04c3fSmrgparseDescAttr(struct OptInfoData *data, const XML_Char **attr) 49101e04c3fSmrg{ 49201e04c3fSmrg uint32_t i; 49301e04c3fSmrg const XML_Char *lang = NULL, *text = NULL; 49401e04c3fSmrg for (i = 0; attr[i]; i += 2) { 49501e04c3fSmrg if (!strcmp (attr[i], "lang")) lang = attr[i+1]; 49601e04c3fSmrg else if (!strcmp (attr[i], "text")) text = attr[i+1]; 49701e04c3fSmrg else XML_FATAL("illegal description attribute: %s.", attr[i]); 49801e04c3fSmrg } 49901e04c3fSmrg if (!lang) XML_FATAL1 ("lang attribute missing in description."); 50001e04c3fSmrg if (!text) XML_FATAL1 ("text attribute missing in description."); 50101e04c3fSmrg} 50201e04c3fSmrg 50301e04c3fSmrg/** \brief Parse attributes of an option element. */ 50401e04c3fSmrgstatic void 50501e04c3fSmrgparseOptInfoAttr(struct OptInfoData *data, const XML_Char **attr) 50601e04c3fSmrg{ 50701e04c3fSmrg enum OptAttr {OA_DEFAULT = 0, OA_NAME, OA_TYPE, OA_VALID, OA_COUNT}; 50801e04c3fSmrg static const XML_Char *optAttr[] = {"default", "name", "type", "valid"}; 50901e04c3fSmrg const XML_Char *attrVal[OA_COUNT] = {NULL, NULL, NULL, NULL}; 51001e04c3fSmrg const char *defaultVal; 51101e04c3fSmrg driOptionCache *cache = data->cache; 51201e04c3fSmrg uint32_t opt, i; 51301e04c3fSmrg for (i = 0; attr[i]; i += 2) { 51401e04c3fSmrg uint32_t attrName = bsearchStr (attr[i], optAttr, OA_COUNT); 51501e04c3fSmrg if (attrName >= OA_COUNT) 51601e04c3fSmrg XML_FATAL ("illegal option attribute: %s", attr[i]); 51701e04c3fSmrg attrVal[attrName] = attr[i+1]; 51801e04c3fSmrg } 51901e04c3fSmrg if (!attrVal[OA_NAME]) XML_FATAL1 ("name attribute missing in option."); 52001e04c3fSmrg if (!attrVal[OA_TYPE]) XML_FATAL1 ("type attribute missing in option."); 52101e04c3fSmrg if (!attrVal[OA_DEFAULT]) XML_FATAL1 ("default attribute missing in option."); 52201e04c3fSmrg 52301e04c3fSmrg opt = findOption (cache, attrVal[OA_NAME]); 52401e04c3fSmrg if (cache->info[opt].name) 52501e04c3fSmrg XML_FATAL ("option %s redefined.", attrVal[OA_NAME]); 52601e04c3fSmrg data->curOption = opt; 52701e04c3fSmrg 52801e04c3fSmrg XSTRDUP (cache->info[opt].name, attrVal[OA_NAME]); 52901e04c3fSmrg 53001e04c3fSmrg if (!strcmp (attrVal[OA_TYPE], "bool")) 53101e04c3fSmrg cache->info[opt].type = DRI_BOOL; 53201e04c3fSmrg else if (!strcmp (attrVal[OA_TYPE], "enum")) 53301e04c3fSmrg cache->info[opt].type = DRI_ENUM; 53401e04c3fSmrg else if (!strcmp (attrVal[OA_TYPE], "int")) 53501e04c3fSmrg cache->info[opt].type = DRI_INT; 53601e04c3fSmrg else if (!strcmp (attrVal[OA_TYPE], "float")) 53701e04c3fSmrg cache->info[opt].type = DRI_FLOAT; 53801e04c3fSmrg else if (!strcmp (attrVal[OA_TYPE], "string")) 53901e04c3fSmrg cache->info[opt].type = DRI_STRING; 54001e04c3fSmrg else 54101e04c3fSmrg XML_FATAL ("illegal type in option: %s.", attrVal[OA_TYPE]); 54201e04c3fSmrg 54301e04c3fSmrg defaultVal = getenv (cache->info[opt].name); 54401e04c3fSmrg if (defaultVal != NULL) { 54501e04c3fSmrg /* don't use XML_WARNING, we want the user to see this! */ 54601e04c3fSmrg fprintf (stderr, 54701e04c3fSmrg "ATTENTION: default value of option %s overridden by environment.\n", 54801e04c3fSmrg cache->info[opt].name); 54901e04c3fSmrg } else 55001e04c3fSmrg defaultVal = attrVal[OA_DEFAULT]; 55101e04c3fSmrg if (!parseValue (&cache->values[opt], cache->info[opt].type, defaultVal)) 55201e04c3fSmrg XML_FATAL ("illegal default value for %s: %s.", cache->info[opt].name, defaultVal); 55301e04c3fSmrg 55401e04c3fSmrg if (attrVal[OA_VALID]) { 55501e04c3fSmrg if (cache->info[opt].type == DRI_BOOL) 55601e04c3fSmrg XML_FATAL1 ("boolean option with valid attribute."); 55701e04c3fSmrg if (!parseRanges (&cache->info[opt], attrVal[OA_VALID])) 55801e04c3fSmrg XML_FATAL ("illegal valid attribute: %s.", attrVal[OA_VALID]); 55901e04c3fSmrg if (!checkValue (&cache->values[opt], &cache->info[opt])) 56001e04c3fSmrg XML_FATAL ("default value out of valid range '%s': %s.", 56101e04c3fSmrg attrVal[OA_VALID], defaultVal); 56201e04c3fSmrg } else if (cache->info[opt].type == DRI_ENUM) { 56301e04c3fSmrg XML_FATAL1 ("valid attribute missing in option (mandatory for enums)."); 56401e04c3fSmrg } else { 56501e04c3fSmrg cache->info[opt].nRanges = 0; 56601e04c3fSmrg cache->info[opt].ranges = NULL; 56701e04c3fSmrg } 56801e04c3fSmrg} 56901e04c3fSmrg 57001e04c3fSmrg/** \brief Handler for start element events. */ 57101e04c3fSmrgstatic void 57201e04c3fSmrgoptInfoStartElem(void *userData, const XML_Char *name, const XML_Char **attr) 57301e04c3fSmrg{ 57401e04c3fSmrg struct OptInfoData *data = (struct OptInfoData *)userData; 57501e04c3fSmrg enum OptInfoElem elem = bsearchStr (name, OptInfoElems, OI_COUNT); 57601e04c3fSmrg switch (elem) { 57701e04c3fSmrg case OI_DRIINFO: 57801e04c3fSmrg if (data->inDriInfo) 57901e04c3fSmrg XML_FATAL1 ("nested <driinfo> elements."); 58001e04c3fSmrg if (attr[0]) 58101e04c3fSmrg XML_FATAL1 ("attributes specified on <driinfo> element."); 58201e04c3fSmrg data->inDriInfo = true; 58301e04c3fSmrg break; 58401e04c3fSmrg case OI_SECTION: 58501e04c3fSmrg if (!data->inDriInfo) 58601e04c3fSmrg XML_FATAL1 ("<section> must be inside <driinfo>."); 58701e04c3fSmrg if (data->inSection) 58801e04c3fSmrg XML_FATAL1 ("nested <section> elements."); 58901e04c3fSmrg if (attr[0]) 59001e04c3fSmrg XML_FATAL1 ("attributes specified on <section> element."); 59101e04c3fSmrg data->inSection = true; 59201e04c3fSmrg break; 59301e04c3fSmrg case OI_DESCRIPTION: 59401e04c3fSmrg if (!data->inSection && !data->inOption) 59501e04c3fSmrg XML_FATAL1 ("<description> must be inside <description> or <option."); 59601e04c3fSmrg if (data->inDesc) 59701e04c3fSmrg XML_FATAL1 ("nested <description> elements."); 59801e04c3fSmrg data->inDesc = true; 59901e04c3fSmrg parseDescAttr (data, attr); 60001e04c3fSmrg break; 60101e04c3fSmrg case OI_OPTION: 60201e04c3fSmrg if (!data->inSection) 60301e04c3fSmrg XML_FATAL1 ("<option> must be inside <section>."); 60401e04c3fSmrg if (data->inDesc) 60501e04c3fSmrg XML_FATAL1 ("<option> nested in <description> element."); 60601e04c3fSmrg if (data->inOption) 60701e04c3fSmrg XML_FATAL1 ("nested <option> elements."); 60801e04c3fSmrg data->inOption = true; 60901e04c3fSmrg parseOptInfoAttr (data, attr); 61001e04c3fSmrg break; 61101e04c3fSmrg case OI_ENUM: 61201e04c3fSmrg if (!(data->inOption && data->inDesc)) 61301e04c3fSmrg XML_FATAL1 ("<enum> must be inside <option> and <description>."); 61401e04c3fSmrg if (data->inEnum) 61501e04c3fSmrg XML_FATAL1 ("nested <enum> elements."); 61601e04c3fSmrg data->inEnum = true; 61701e04c3fSmrg parseEnumAttr (data, attr); 61801e04c3fSmrg break; 61901e04c3fSmrg default: 62001e04c3fSmrg XML_FATAL ("unknown element: %s.", name); 62101e04c3fSmrg } 62201e04c3fSmrg} 62301e04c3fSmrg 62401e04c3fSmrg/** \brief Handler for end element events. */ 62501e04c3fSmrgstatic void 62601e04c3fSmrgoptInfoEndElem(void *userData, const XML_Char *name) 62701e04c3fSmrg{ 62801e04c3fSmrg struct OptInfoData *data = (struct OptInfoData *)userData; 62901e04c3fSmrg enum OptInfoElem elem = bsearchStr (name, OptInfoElems, OI_COUNT); 63001e04c3fSmrg switch (elem) { 63101e04c3fSmrg case OI_DRIINFO: 63201e04c3fSmrg data->inDriInfo = false; 63301e04c3fSmrg break; 63401e04c3fSmrg case OI_SECTION: 63501e04c3fSmrg data->inSection = false; 63601e04c3fSmrg break; 63701e04c3fSmrg case OI_DESCRIPTION: 63801e04c3fSmrg data->inDesc = false; 63901e04c3fSmrg break; 64001e04c3fSmrg case OI_OPTION: 64101e04c3fSmrg data->inOption = false; 64201e04c3fSmrg break; 64301e04c3fSmrg case OI_ENUM: 64401e04c3fSmrg data->inEnum = false; 64501e04c3fSmrg break; 64601e04c3fSmrg default: 64701e04c3fSmrg assert (0); /* should have been caught by StartElem */ 64801e04c3fSmrg } 64901e04c3fSmrg} 65001e04c3fSmrg 65101e04c3fSmrgvoid 65201e04c3fSmrgdriParseOptionInfo(driOptionCache *info, const char *configOptions) 65301e04c3fSmrg{ 65401e04c3fSmrg XML_Parser p; 65501e04c3fSmrg int status; 65601e04c3fSmrg struct OptInfoData userData; 65701e04c3fSmrg struct OptInfoData *data = &userData; 65801e04c3fSmrg 65901e04c3fSmrg /* Make the hash table big enough to fit more than the maximum number of 66001e04c3fSmrg * config options we've ever seen in a driver. 66101e04c3fSmrg */ 66201e04c3fSmrg info->tableSize = 6; 66301e04c3fSmrg info->info = calloc(1 << info->tableSize, sizeof (driOptionInfo)); 66401e04c3fSmrg info->values = calloc(1 << info->tableSize, sizeof (driOptionValue)); 66501e04c3fSmrg if (info->info == NULL || info->values == NULL) { 66601e04c3fSmrg fprintf (stderr, "%s: %d: out of memory.\n", __FILE__, __LINE__); 66701e04c3fSmrg abort(); 66801e04c3fSmrg } 66901e04c3fSmrg 67001e04c3fSmrg p = XML_ParserCreate ("UTF-8"); /* always UTF-8 */ 67101e04c3fSmrg XML_SetElementHandler (p, optInfoStartElem, optInfoEndElem); 67201e04c3fSmrg XML_SetUserData (p, data); 67301e04c3fSmrg 67401e04c3fSmrg userData.name = "__driConfigOptions"; 67501e04c3fSmrg userData.parser = p; 67601e04c3fSmrg userData.cache = info; 67701e04c3fSmrg userData.inDriInfo = false; 67801e04c3fSmrg userData.inSection = false; 67901e04c3fSmrg userData.inDesc = false; 68001e04c3fSmrg userData.inOption = false; 68101e04c3fSmrg userData.inEnum = false; 68201e04c3fSmrg userData.curOption = -1; 68301e04c3fSmrg 68401e04c3fSmrg status = XML_Parse (p, configOptions, strlen (configOptions), 1); 68501e04c3fSmrg if (!status) 68601e04c3fSmrg XML_FATAL ("%s.", XML_ErrorString(XML_GetErrorCode(p))); 68701e04c3fSmrg 68801e04c3fSmrg XML_ParserFree (p); 68901e04c3fSmrg} 69001e04c3fSmrg 69101e04c3fSmrg/** \brief Parser context for configuration files. */ 69201e04c3fSmrgstruct OptConfData { 69301e04c3fSmrg const char *name; 69401e04c3fSmrg XML_Parser parser; 69501e04c3fSmrg driOptionCache *cache; 69601e04c3fSmrg int screenNum; 69701e04c3fSmrg const char *driverName, *execName; 69801e04c3fSmrg const char *kernelDriverName; 69901e04c3fSmrg uint32_t ignoringDevice; 70001e04c3fSmrg uint32_t ignoringApp; 70101e04c3fSmrg uint32_t inDriConf; 70201e04c3fSmrg uint32_t inDevice; 70301e04c3fSmrg uint32_t inApp; 70401e04c3fSmrg uint32_t inOption; 70501e04c3fSmrg}; 70601e04c3fSmrg 70701e04c3fSmrg/** \brief Elements in configuration files. */ 70801e04c3fSmrgenum OptConfElem { 70901e04c3fSmrg OC_APPLICATION = 0, OC_DEVICE, OC_DRICONF, OC_OPTION, OC_COUNT 71001e04c3fSmrg}; 71101e04c3fSmrgstatic const XML_Char *OptConfElems[] = { 71201e04c3fSmrg [OC_APPLICATION] = "application", 71301e04c3fSmrg [OC_DEVICE] = "device", 71401e04c3fSmrg [OC_DRICONF] = "driconf", 71501e04c3fSmrg [OC_OPTION] = "option", 71601e04c3fSmrg}; 71701e04c3fSmrg 71801e04c3fSmrg/** \brief Parse attributes of a device element. */ 71901e04c3fSmrgstatic void 72001e04c3fSmrgparseDeviceAttr(struct OptConfData *data, const XML_Char **attr) 72101e04c3fSmrg{ 72201e04c3fSmrg uint32_t i; 72301e04c3fSmrg const XML_Char *driver = NULL, *screen = NULL, *kernel = NULL; 72401e04c3fSmrg for (i = 0; attr[i]; i += 2) { 72501e04c3fSmrg if (!strcmp (attr[i], "driver")) driver = attr[i+1]; 72601e04c3fSmrg else if (!strcmp (attr[i], "screen")) screen = attr[i+1]; 72701e04c3fSmrg else if (!strcmp (attr[i], "kernel_driver")) kernel = attr[i+1]; 72801e04c3fSmrg else XML_WARNING("unknown device attribute: %s.", attr[i]); 72901e04c3fSmrg } 73001e04c3fSmrg if (driver && strcmp (driver, data->driverName)) 73101e04c3fSmrg data->ignoringDevice = data->inDevice; 73201e04c3fSmrg else if (kernel && (!data->kernelDriverName || strcmp (kernel, data->kernelDriverName))) 73301e04c3fSmrg data->ignoringDevice = data->inDevice; 73401e04c3fSmrg else if (screen) { 73501e04c3fSmrg driOptionValue screenNum; 73601e04c3fSmrg if (!parseValue (&screenNum, DRI_INT, screen)) 73701e04c3fSmrg XML_WARNING("illegal screen number: %s.", screen); 73801e04c3fSmrg else if (screenNum._int != data->screenNum) 73901e04c3fSmrg data->ignoringDevice = data->inDevice; 74001e04c3fSmrg } 74101e04c3fSmrg} 74201e04c3fSmrg 74301e04c3fSmrg/** \brief Parse attributes of an application element. */ 74401e04c3fSmrgstatic void 74501e04c3fSmrgparseAppAttr(struct OptConfData *data, const XML_Char **attr) 74601e04c3fSmrg{ 74701e04c3fSmrg uint32_t i; 74801e04c3fSmrg const XML_Char *exec = NULL; 74901e04c3fSmrg for (i = 0; attr[i]; i += 2) { 75001e04c3fSmrg if (!strcmp (attr[i], "name")) /* not needed here */; 75101e04c3fSmrg else if (!strcmp (attr[i], "executable")) exec = attr[i+1]; 75201e04c3fSmrg else XML_WARNING("unknown application attribute: %s.", attr[i]); 75301e04c3fSmrg } 75401e04c3fSmrg if (exec && strcmp (exec, data->execName)) 75501e04c3fSmrg data->ignoringApp = data->inApp; 75601e04c3fSmrg} 75701e04c3fSmrg 75801e04c3fSmrg/** \brief Parse attributes of an option element. */ 75901e04c3fSmrgstatic void 76001e04c3fSmrgparseOptConfAttr(struct OptConfData *data, const XML_Char **attr) 76101e04c3fSmrg{ 76201e04c3fSmrg uint32_t i; 76301e04c3fSmrg const XML_Char *name = NULL, *value = NULL; 76401e04c3fSmrg for (i = 0; attr[i]; i += 2) { 76501e04c3fSmrg if (!strcmp (attr[i], "name")) name = attr[i+1]; 76601e04c3fSmrg else if (!strcmp (attr[i], "value")) value = attr[i+1]; 76701e04c3fSmrg else XML_WARNING("unknown option attribute: %s.", attr[i]); 76801e04c3fSmrg } 76901e04c3fSmrg if (!name) XML_WARNING1 ("name attribute missing in option."); 77001e04c3fSmrg if (!value) XML_WARNING1 ("value attribute missing in option."); 77101e04c3fSmrg if (name && value) { 77201e04c3fSmrg driOptionCache *cache = data->cache; 77301e04c3fSmrg uint32_t opt = findOption (cache, name); 77401e04c3fSmrg if (cache->info[opt].name == NULL) 77501e04c3fSmrg /* don't use XML_WARNING, drirc defines options for all drivers, 77601e04c3fSmrg * but not all drivers support them */ 77701e04c3fSmrg return; 77801e04c3fSmrg else if (getenv (cache->info[opt].name)) 77901e04c3fSmrg /* don't use XML_WARNING, we want the user to see this! */ 78001e04c3fSmrg fprintf (stderr, "ATTENTION: option value of option %s ignored.\n", 78101e04c3fSmrg cache->info[opt].name); 78201e04c3fSmrg else if (!parseValue (&cache->values[opt], cache->info[opt].type, value)) 78301e04c3fSmrg XML_WARNING ("illegal option value: %s.", value); 78401e04c3fSmrg } 78501e04c3fSmrg} 78601e04c3fSmrg 78701e04c3fSmrg/** \brief Handler for start element events. */ 78801e04c3fSmrgstatic void 78901e04c3fSmrgoptConfStartElem(void *userData, const XML_Char *name, 79001e04c3fSmrg const XML_Char **attr) 79101e04c3fSmrg{ 79201e04c3fSmrg struct OptConfData *data = (struct OptConfData *)userData; 79301e04c3fSmrg enum OptConfElem elem = bsearchStr (name, OptConfElems, OC_COUNT); 79401e04c3fSmrg switch (elem) { 79501e04c3fSmrg case OC_DRICONF: 79601e04c3fSmrg if (data->inDriConf) 79701e04c3fSmrg XML_WARNING1 ("nested <driconf> elements."); 79801e04c3fSmrg if (attr[0]) 79901e04c3fSmrg XML_WARNING1 ("attributes specified on <driconf> element."); 80001e04c3fSmrg data->inDriConf++; 80101e04c3fSmrg break; 80201e04c3fSmrg case OC_DEVICE: 80301e04c3fSmrg if (!data->inDriConf) 80401e04c3fSmrg XML_WARNING1 ("<device> should be inside <driconf>."); 80501e04c3fSmrg if (data->inDevice) 80601e04c3fSmrg XML_WARNING1 ("nested <device> elements."); 80701e04c3fSmrg data->inDevice++; 80801e04c3fSmrg if (!data->ignoringDevice && !data->ignoringApp) 80901e04c3fSmrg parseDeviceAttr (data, attr); 81001e04c3fSmrg break; 81101e04c3fSmrg case OC_APPLICATION: 81201e04c3fSmrg if (!data->inDevice) 81301e04c3fSmrg XML_WARNING1 ("<application> should be inside <device>."); 81401e04c3fSmrg if (data->inApp) 81501e04c3fSmrg XML_WARNING1 ("nested <application> elements."); 81601e04c3fSmrg data->inApp++; 81701e04c3fSmrg if (!data->ignoringDevice && !data->ignoringApp) 81801e04c3fSmrg parseAppAttr (data, attr); 81901e04c3fSmrg break; 82001e04c3fSmrg case OC_OPTION: 82101e04c3fSmrg if (!data->inApp) 82201e04c3fSmrg XML_WARNING1 ("<option> should be inside <application>."); 82301e04c3fSmrg if (data->inOption) 82401e04c3fSmrg XML_WARNING1 ("nested <option> elements."); 82501e04c3fSmrg data->inOption++; 82601e04c3fSmrg if (!data->ignoringDevice && !data->ignoringApp) 82701e04c3fSmrg parseOptConfAttr (data, attr); 82801e04c3fSmrg break; 82901e04c3fSmrg default: 83001e04c3fSmrg XML_WARNING ("unknown element: %s.", name); 83101e04c3fSmrg } 83201e04c3fSmrg} 83301e04c3fSmrg 83401e04c3fSmrg/** \brief Handler for end element events. */ 83501e04c3fSmrgstatic void 83601e04c3fSmrgoptConfEndElem(void *userData, const XML_Char *name) 83701e04c3fSmrg{ 83801e04c3fSmrg struct OptConfData *data = (struct OptConfData *)userData; 83901e04c3fSmrg enum OptConfElem elem = bsearchStr (name, OptConfElems, OC_COUNT); 84001e04c3fSmrg switch (elem) { 84101e04c3fSmrg case OC_DRICONF: 84201e04c3fSmrg data->inDriConf--; 84301e04c3fSmrg break; 84401e04c3fSmrg case OC_DEVICE: 84501e04c3fSmrg if (data->inDevice-- == data->ignoringDevice) 84601e04c3fSmrg data->ignoringDevice = 0; 84701e04c3fSmrg break; 84801e04c3fSmrg case OC_APPLICATION: 84901e04c3fSmrg if (data->inApp-- == data->ignoringApp) 85001e04c3fSmrg data->ignoringApp = 0; 85101e04c3fSmrg break; 85201e04c3fSmrg case OC_OPTION: 85301e04c3fSmrg data->inOption--; 85401e04c3fSmrg break; 85501e04c3fSmrg default: 85601e04c3fSmrg /* unknown element, warning was produced on start tag */; 85701e04c3fSmrg } 85801e04c3fSmrg} 85901e04c3fSmrg 86001e04c3fSmrg/** \brief Initialize an option cache based on info */ 86101e04c3fSmrgstatic void 86201e04c3fSmrginitOptionCache(driOptionCache *cache, const driOptionCache *info) 86301e04c3fSmrg{ 86401e04c3fSmrg unsigned i, size = 1 << info->tableSize; 86501e04c3fSmrg cache->info = info->info; 86601e04c3fSmrg cache->tableSize = info->tableSize; 86701e04c3fSmrg cache->values = malloc((1<<info->tableSize) * sizeof (driOptionValue)); 86801e04c3fSmrg if (cache->values == NULL) { 86901e04c3fSmrg fprintf (stderr, "%s: %d: out of memory.\n", __FILE__, __LINE__); 87001e04c3fSmrg abort(); 87101e04c3fSmrg } 87201e04c3fSmrg memcpy (cache->values, info->values, 87301e04c3fSmrg (1<<info->tableSize) * sizeof (driOptionValue)); 87401e04c3fSmrg for (i = 0; i < size; ++i) { 87501e04c3fSmrg if (cache->info[i].type == DRI_STRING) 87601e04c3fSmrg XSTRDUP(cache->values[i]._string, info->values[i]._string); 87701e04c3fSmrg } 87801e04c3fSmrg} 87901e04c3fSmrg 88001e04c3fSmrgstatic void 88101e04c3fSmrg_parseOneConfigFile(XML_Parser p) 88201e04c3fSmrg{ 88301e04c3fSmrg#define BUF_SIZE 0x1000 88401e04c3fSmrg struct OptConfData *data = (struct OptConfData *)XML_GetUserData (p); 88501e04c3fSmrg int status; 88601e04c3fSmrg int fd; 88701e04c3fSmrg 88801e04c3fSmrg if ((fd = open (data->name, O_RDONLY)) == -1) { 88901e04c3fSmrg __driUtilMessage ("Can't open configuration file %s: %s.", 89001e04c3fSmrg data->name, strerror (errno)); 89101e04c3fSmrg return; 89201e04c3fSmrg } 89301e04c3fSmrg 89401e04c3fSmrg while (1) { 89501e04c3fSmrg int bytesRead; 89601e04c3fSmrg void *buffer = XML_GetBuffer (p, BUF_SIZE); 89701e04c3fSmrg if (!buffer) { 89801e04c3fSmrg __driUtilMessage ("Can't allocate parser buffer."); 89901e04c3fSmrg break; 90001e04c3fSmrg } 90101e04c3fSmrg bytesRead = read (fd, buffer, BUF_SIZE); 90201e04c3fSmrg if (bytesRead == -1) { 90301e04c3fSmrg __driUtilMessage ("Error reading from configuration file %s: %s.", 90401e04c3fSmrg data->name, strerror (errno)); 90501e04c3fSmrg break; 90601e04c3fSmrg } 90701e04c3fSmrg status = XML_ParseBuffer (p, bytesRead, bytesRead == 0); 90801e04c3fSmrg if (!status) { 90901e04c3fSmrg XML_ERROR ("%s.", XML_ErrorString(XML_GetErrorCode(p))); 91001e04c3fSmrg break; 91101e04c3fSmrg } 91201e04c3fSmrg if (bytesRead == 0) 91301e04c3fSmrg break; 91401e04c3fSmrg } 91501e04c3fSmrg 91601e04c3fSmrg close (fd); 91701e04c3fSmrg#undef BUF_SIZE 91801e04c3fSmrg} 91901e04c3fSmrg 92001e04c3fSmrg/** \brief Parse the named configuration file */ 92101e04c3fSmrgstatic void 92201e04c3fSmrgparseOneConfigFile(struct OptConfData *data, const char *filename) 92301e04c3fSmrg{ 92401e04c3fSmrg XML_Parser p; 92501e04c3fSmrg 92601e04c3fSmrg p = XML_ParserCreate (NULL); /* use encoding specified by file */ 92701e04c3fSmrg XML_SetElementHandler (p, optConfStartElem, optConfEndElem); 92801e04c3fSmrg XML_SetUserData (p, data); 92901e04c3fSmrg data->parser = p; 93001e04c3fSmrg data->name = filename; 93101e04c3fSmrg data->ignoringDevice = 0; 93201e04c3fSmrg data->ignoringApp = 0; 93301e04c3fSmrg data->inDriConf = 0; 93401e04c3fSmrg data->inDevice = 0; 93501e04c3fSmrg data->inApp = 0; 93601e04c3fSmrg data->inOption = 0; 93701e04c3fSmrg 93801e04c3fSmrg _parseOneConfigFile (p); 93901e04c3fSmrg XML_ParserFree (p); 94001e04c3fSmrg} 94101e04c3fSmrg 94201e04c3fSmrgstatic int 94301e04c3fSmrgscandir_filter(const struct dirent *ent) 94401e04c3fSmrg{ 94501e04c3fSmrg#ifndef DT_REG /* systems without d_type in dirent results */ 94601e04c3fSmrg struct stat st; 94701e04c3fSmrg 94801e04c3fSmrg if ((lstat(ent->d_name, &st) != 0) || 94901e04c3fSmrg (!S_ISREG(st.st_mode) && !S_ISLNK(st.st_mode))) 95001e04c3fSmrg return 0; 95101e04c3fSmrg#else 95201e04c3fSmrg if (ent->d_type != DT_REG && ent->d_type != DT_LNK) 95301e04c3fSmrg return 0; 95401e04c3fSmrg#endif 95501e04c3fSmrg 95601e04c3fSmrg if (fnmatch("*.conf", ent->d_name, 0)) 95701e04c3fSmrg return 0; 95801e04c3fSmrg 95901e04c3fSmrg return 1; 96001e04c3fSmrg} 96101e04c3fSmrg 96201e04c3fSmrg/** \brief Parse configuration files in a directory */ 96301e04c3fSmrgstatic void 96401e04c3fSmrgparseConfigDir(struct OptConfData *data, const char *dirname) 96501e04c3fSmrg{ 96601e04c3fSmrg int i, count; 96701e04c3fSmrg struct dirent **entries = NULL; 96801e04c3fSmrg 96901e04c3fSmrg count = scandir(dirname, &entries, scandir_filter, alphasort); 97001e04c3fSmrg if (count < 0) 97101e04c3fSmrg return; 97201e04c3fSmrg 97301e04c3fSmrg for (i = 0; i < count; i++) { 97401e04c3fSmrg char filename[PATH_MAX]; 97501e04c3fSmrg 97601e04c3fSmrg snprintf(filename, PATH_MAX, "%s/%s", dirname, entries[i]->d_name); 97701e04c3fSmrg free(entries[i]); 97801e04c3fSmrg 97901e04c3fSmrg parseOneConfigFile(data, filename); 98001e04c3fSmrg } 98101e04c3fSmrg 98201e04c3fSmrg free(entries); 98301e04c3fSmrg} 98401e04c3fSmrg 98501e04c3fSmrg#ifndef SYSCONFDIR 98601e04c3fSmrg#define SYSCONFDIR "/etc" 98701e04c3fSmrg#endif 98801e04c3fSmrg 98901e04c3fSmrg#ifndef DATADIR 99001e04c3fSmrg#define DATADIR "/usr/share" 99101e04c3fSmrg#endif 99201e04c3fSmrg 99301e04c3fSmrgvoid 99401e04c3fSmrgdriParseConfigFiles(driOptionCache *cache, const driOptionCache *info, 99501e04c3fSmrg int screenNum, const char *driverName, 99601e04c3fSmrg const char *kernelDriverName) 99701e04c3fSmrg{ 99801e04c3fSmrg char *home; 99901e04c3fSmrg struct OptConfData userData; 100001e04c3fSmrg 100101e04c3fSmrg initOptionCache (cache, info); 100201e04c3fSmrg 100301e04c3fSmrg userData.cache = cache; 100401e04c3fSmrg userData.screenNum = screenNum; 100501e04c3fSmrg userData.driverName = driverName; 100601e04c3fSmrg userData.kernelDriverName = kernelDriverName; 100701e04c3fSmrg userData.execName = util_get_process_name(); 100801e04c3fSmrg 100901e04c3fSmrg parseConfigDir(&userData, DATADIR "/drirc.d"); 101001e04c3fSmrg parseOneConfigFile(&userData, SYSCONFDIR "/drirc"); 101101e04c3fSmrg 101201e04c3fSmrg if ((home = getenv ("HOME"))) { 101301e04c3fSmrg char filename[PATH_MAX]; 101401e04c3fSmrg 101501e04c3fSmrg snprintf(filename, PATH_MAX, "%s/.drirc", home); 101601e04c3fSmrg parseOneConfigFile(&userData, filename); 101701e04c3fSmrg } 101801e04c3fSmrg} 101901e04c3fSmrg 102001e04c3fSmrgvoid 102101e04c3fSmrgdriDestroyOptionInfo(driOptionCache *info) 102201e04c3fSmrg{ 102301e04c3fSmrg driDestroyOptionCache(info); 102401e04c3fSmrg if (info->info) { 102501e04c3fSmrg uint32_t i, size = 1 << info->tableSize; 102601e04c3fSmrg for (i = 0; i < size; ++i) { 102701e04c3fSmrg if (info->info[i].name) { 102801e04c3fSmrg free(info->info[i].name); 102901e04c3fSmrg free(info->info[i].ranges); 103001e04c3fSmrg } 103101e04c3fSmrg } 103201e04c3fSmrg free(info->info); 103301e04c3fSmrg } 103401e04c3fSmrg} 103501e04c3fSmrg 103601e04c3fSmrgvoid 103701e04c3fSmrgdriDestroyOptionCache(driOptionCache *cache) 103801e04c3fSmrg{ 103901e04c3fSmrg if (cache->info) { 104001e04c3fSmrg unsigned i, size = 1 << cache->tableSize; 104101e04c3fSmrg for (i = 0; i < size; ++i) { 104201e04c3fSmrg if (cache->info[i].type == DRI_STRING) 104301e04c3fSmrg free(cache->values[i]._string); 104401e04c3fSmrg } 104501e04c3fSmrg } 104601e04c3fSmrg free(cache->values); 104701e04c3fSmrg} 104801e04c3fSmrg 104901e04c3fSmrgunsigned char 105001e04c3fSmrgdriCheckOption(const driOptionCache *cache, const char *name, 105101e04c3fSmrg driOptionType type) 105201e04c3fSmrg{ 105301e04c3fSmrg uint32_t i = findOption (cache, name); 105401e04c3fSmrg return cache->info[i].name != NULL && cache->info[i].type == type; 105501e04c3fSmrg} 105601e04c3fSmrg 105701e04c3fSmrgunsigned char 105801e04c3fSmrgdriQueryOptionb(const driOptionCache *cache, const char *name) 105901e04c3fSmrg{ 106001e04c3fSmrg uint32_t i = findOption (cache, name); 106101e04c3fSmrg /* make sure the option is defined and has the correct type */ 106201e04c3fSmrg assert (cache->info[i].name != NULL); 106301e04c3fSmrg assert (cache->info[i].type == DRI_BOOL); 106401e04c3fSmrg return cache->values[i]._bool; 106501e04c3fSmrg} 106601e04c3fSmrg 106701e04c3fSmrgint 106801e04c3fSmrgdriQueryOptioni(const driOptionCache *cache, const char *name) 106901e04c3fSmrg{ 107001e04c3fSmrg uint32_t i = findOption (cache, name); 107101e04c3fSmrg /* make sure the option is defined and has the correct type */ 107201e04c3fSmrg assert (cache->info[i].name != NULL); 107301e04c3fSmrg assert (cache->info[i].type == DRI_INT || cache->info[i].type == DRI_ENUM); 107401e04c3fSmrg return cache->values[i]._int; 107501e04c3fSmrg} 107601e04c3fSmrg 107701e04c3fSmrgfloat 107801e04c3fSmrgdriQueryOptionf(const driOptionCache *cache, const char *name) 107901e04c3fSmrg{ 108001e04c3fSmrg uint32_t i = findOption (cache, name); 108101e04c3fSmrg /* make sure the option is defined and has the correct type */ 108201e04c3fSmrg assert (cache->info[i].name != NULL); 108301e04c3fSmrg assert (cache->info[i].type == DRI_FLOAT); 108401e04c3fSmrg return cache->values[i]._float; 108501e04c3fSmrg} 108601e04c3fSmrg 108701e04c3fSmrgchar * 108801e04c3fSmrgdriQueryOptionstr(const driOptionCache *cache, const char *name) 108901e04c3fSmrg{ 109001e04c3fSmrg uint32_t i = findOption (cache, name); 109101e04c3fSmrg /* make sure the option is defined and has the correct type */ 109201e04c3fSmrg assert (cache->info[i].name != NULL); 109301e04c3fSmrg assert (cache->info[i].type == DRI_STRING); 109401e04c3fSmrg return cache->values[i]._string; 109501e04c3fSmrg} 1096