Exemple #1
0
static void LoadConfigFromFile(FILE *f)
{
    char curSection[128] = "";
    char *buffer = NULL;
    size_t maxlen = 0;
    ConfigEntry *ent;

    while(readline(f, &buffer, &maxlen))
    {
        char *line, *comment;
        char key[256] = "";
        char value[256] = "";

        comment = strchr(buffer, '#');
        if(comment) *(comment++) = 0;

        line = rstrip(lstrip(buffer));
        if(!line[0])
            continue;

        if(line[0] == '[')
        {
            char *section = line+1;
            char *endsection;

            endsection = strchr(section, ']');
            if(!endsection || section == endsection || endsection[1] != 0)
            {
                 ERR("config parse error: bad line \"%s\"\n", line);
                 continue;
            }
            *endsection = 0;

            if(strcasecmp(section, "general") == 0)
                curSection[0] = 0;
            else
            {
                strncpy(curSection, section, sizeof(curSection)-1);
                curSection[sizeof(curSection)-1] = 0;
            }

            continue;
        }

        if(sscanf(line, "%255[^=] = \"%255[^\"]\"", key, value) == 2 ||
           sscanf(line, "%255[^=] = '%255[^\']'", key, value) == 2 ||
           sscanf(line, "%255[^=] = %255[^\n]", key, value) == 2)
        {
            /* sscanf doesn't handle '' or "" as empty values, so clip it
             * manually. */
            if(strcmp(value, "\"\"") == 0 || strcmp(value, "''") == 0)
                value[0] = 0;
        }
        else if(sscanf(line, "%255[^=] %255[=]", key, value) == 2)
        {
            /* Special case for 'key =' */
            value[0] = 0;
        }
        else
        {
            ERR("config parse error: malformed option line: \"%s\"\n\n", line);
            continue;
        }
        rstrip(key);

        if(curSection[0] != 0)
        {
            size_t len = strlen(curSection);
            memmove(&key[len+1], key, sizeof(key)-1-len);
            key[len] = '/';
            memcpy(key, curSection, len);
        }

        /* Check if we already have this option set */
        ent = cfgBlock.entries;
        while((unsigned int)(ent-cfgBlock.entries) < cfgBlock.entryCount)
        {
            if(strcasecmp(ent->key, key) == 0)
                break;
            ent++;
        }

        if((unsigned int)(ent-cfgBlock.entries) >= cfgBlock.entryCount)
        {
            /* Allocate a new option entry */
            ent = realloc(cfgBlock.entries, (cfgBlock.entryCount+1)*sizeof(ConfigEntry));
            if(!ent)
            {
                 ERR("config parse error: error reallocating config entries\n");
                 continue;
            }
            cfgBlock.entries = ent;
            ent = cfgBlock.entries + cfgBlock.entryCount;
            cfgBlock.entryCount++;

            ent->key = strdup(key);
            ent->value = NULL;
        }

        free(ent->value);
        ent->value = expdup(value);

        TRACE("found '%s' = '%s'\n", ent->key, ent->value);
    }

    free(buffer);
}
Exemple #2
0
static void LoadConfigFromFile(FILE *f)
{
    char curSection[128] = "";
    char *buffer = NULL;
    size_t maxlen = 0;
    ConfigEntry *ent;

    while(readline(f, &buffer, &maxlen))
    {
        char *line, *comment;
        char key[256] = "";
        char value[256] = "";

        line = rstrip(lstrip(buffer));
        if(!line[0]) continue;

        if(line[0] == '[')
        {
            char *section = line+1;
            char *endsection;

            endsection = strchr(section, ']');
            if(!endsection || section == endsection)
            {
                ERR("config parse error: bad line \"%s\"\n", line);
                continue;
            }
            if(endsection[1] != 0)
            {
                char *end = endsection+1;
                while(isspace(*end))
                    ++end;
                if(*end != 0 && *end != '#')
                {
                    ERR("config parse error: bad line \"%s\"\n", line);
                    continue;
                }
            }
            *endsection = 0;

            if(strcasecmp(section, "general") == 0)
                curSection[0] = 0;
            else
            {
                size_t len, p = 0;
                do {
                    char *nextp = strchr(section, '%');
                    if(!nextp)
                    {
                        strncpy(curSection+p, section, sizeof(curSection)-1-p);
                        break;
                    }

                    len = nextp - section;
                    if(len > sizeof(curSection)-1-p)
                        len = sizeof(curSection)-1-p;
                    strncpy(curSection+p, section, len);
                    p += len;
                    section = nextp;

                    if(((section[1] >= '0' && section[1] <= '9') ||
                        (section[1] >= 'a' && section[1] <= 'f') ||
                        (section[1] >= 'A' && section[1] <= 'F')) &&
                       ((section[2] >= '0' && section[2] <= '9') ||
                        (section[2] >= 'a' && section[2] <= 'f') ||
                        (section[2] >= 'A' && section[2] <= 'F')))
                    {
                        unsigned char b = 0;
                        if(section[1] >= '0' && section[1] <= '9')
                            b = (section[1]-'0') << 4;
                        else if(section[1] >= 'a' && section[1] <= 'f')
                            b = (section[1]-'a'+0xa) << 4;
                        else if(section[1] >= 'A' && section[1] <= 'F')
                            b = (section[1]-'A'+0x0a) << 4;
                        if(section[2] >= '0' && section[2] <= '9')
                            b |= (section[2]-'0');
                        else if(section[2] >= 'a' && section[2] <= 'f')
                            b |= (section[2]-'a'+0xa);
                        else if(section[2] >= 'A' && section[2] <= 'F')
                            b |= (section[2]-'A'+0x0a);
                        if(p < sizeof(curSection)-1)
                            curSection[p++] = b;
                        section += 3;
                    }
                    else if(section[1] == '%')
                    {
                        if(p < sizeof(curSection)-1)
                            curSection[p++] = '%';
                        section += 2;
                    }
                    else
                    {
                        if(p < sizeof(curSection)-1)
                            curSection[p++] = '%';
                        section += 1;
                    }
                    if(p < sizeof(curSection)-1)
                        curSection[p] = 0;
                } while(p < sizeof(curSection)-1 && *section != 0);
                curSection[sizeof(curSection)-1] = 0;
            }

            continue;
        }

        comment = strchr(line, '#');
        if(comment) *(comment++) = 0;
        if(!line[0]) continue;

        if(sscanf(line, "%255[^=] = \"%255[^\"]\"", key, value) == 2 ||
           sscanf(line, "%255[^=] = '%255[^\']'", key, value) == 2 ||
           sscanf(line, "%255[^=] = %255[^\n]", key, value) == 2)
        {
            /* sscanf doesn't handle '' or "" as empty values, so clip it
             * manually. */
            if(strcmp(value, "\"\"") == 0 || strcmp(value, "''") == 0)
                value[0] = 0;
        }
        else if(sscanf(line, "%255[^=] %255[=]", key, value) == 2)
        {
            /* Special case for 'key =' */
            value[0] = 0;
        }
        else
        {
            ERR("config parse error: malformed option line: \"%s\"\n\n", line);
            continue;
        }
        rstrip(key);

        if(curSection[0] != 0)
        {
            size_t len = strlen(curSection);
            memmove(&key[len+1], key, sizeof(key)-1-len);
            key[len] = '/';
            memcpy(key, curSection, len);
        }

        /* Check if we already have this option set */
        ent = cfgBlock.entries;
        while((unsigned int)(ent-cfgBlock.entries) < cfgBlock.entryCount)
        {
            if(strcasecmp(ent->key, key) == 0)
                break;
            ent++;
        }

        if((unsigned int)(ent-cfgBlock.entries) >= cfgBlock.entryCount)
        {
            /* Allocate a new option entry */
            ent = realloc(cfgBlock.entries, (cfgBlock.entryCount+1)*sizeof(ConfigEntry));
            if(!ent)
            {
                 ERR("config parse error: error reallocating config entries\n");
                 continue;
            }
            cfgBlock.entries = ent;
            ent = cfgBlock.entries + cfgBlock.entryCount;
            cfgBlock.entryCount++;

            ent->key = strdup(key);
            ent->value = NULL;
        }

        free(ent->value);
        ent->value = expdup(value);

        TRACE("found '%s' = '%s'\n", ent->key, ent->value);
    }

    free(buffer);
}