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