2010-07-09 11:04:20 +00:00
|
|
|
/*
|
|
|
|
* uhub - A tiny ADC p2p connection hub
|
|
|
|
* Copyright (C) 2007-2010, Jan Vidar Krey
|
|
|
|
*
|
|
|
|
* This program is free software; you can redistribute it and/or modify
|
|
|
|
* it under the terms of the GNU General Public License as published by
|
|
|
|
* the Free Software Foundation; either version 3 of the License, or
|
|
|
|
* (at your option) any later version.
|
|
|
|
*
|
|
|
|
* This program is distributed in the hope that it will be useful,
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
* GNU General Public License for more details.
|
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU General Public License
|
|
|
|
* along with this program. If not, see <http://www.gnu.org/licenses/>.
|
|
|
|
*
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include "uhub.h"
|
|
|
|
|
|
|
|
#define ADD_CHAR(X) do { *out = X; out++; token_size++; } while(0)
|
2010-07-18 17:43:11 +00:00
|
|
|
#define RESET_TOKEN do { ADD_CHAR('\0'); out = buffer; if (cfg_token_add(tokens, out)) token_count++; token_size = 0; buffer[0] = '\0'; } while (0)
|
2010-07-09 11:04:20 +00:00
|
|
|
|
2010-07-18 17:43:11 +00:00
|
|
|
struct cfg_tokens
|
2010-07-09 11:04:20 +00:00
|
|
|
{
|
2010-07-18 17:43:11 +00:00
|
|
|
struct linked_list* list;
|
|
|
|
};
|
2010-07-09 11:04:20 +00:00
|
|
|
|
2010-07-18 17:43:11 +00:00
|
|
|
struct cfg_tokens* cfg_tokenize(const char* line)
|
2010-07-09 11:04:20 +00:00
|
|
|
{
|
2011-01-02 01:39:25 +00:00
|
|
|
struct cfg_tokens* tokens = (struct cfg_tokens*) hub_malloc_zero(sizeof(struct cfg_tokens));
|
|
|
|
char* buffer = (char*) hub_malloc_zero(strlen(line));
|
2010-07-09 11:04:20 +00:00
|
|
|
char* out = buffer;
|
|
|
|
const char* p = line;
|
|
|
|
int backslash = 0;
|
|
|
|
char quote = 0;
|
|
|
|
size_t token_count = 0;
|
|
|
|
size_t token_size = 0;
|
2011-01-02 01:39:25 +00:00
|
|
|
tokens->list = list_create();
|
2010-07-09 11:04:20 +00:00
|
|
|
|
|
|
|
for (; *p; p++)
|
|
|
|
{
|
|
|
|
switch (*p)
|
|
|
|
{
|
|
|
|
case '\\':
|
|
|
|
if (backslash)
|
|
|
|
{
|
|
|
|
ADD_CHAR('\\');
|
|
|
|
backslash = 0;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
backslash = 1;
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
|
|
|
|
case '#':
|
|
|
|
if (backslash)
|
|
|
|
{
|
|
|
|
ADD_CHAR('#');
|
|
|
|
backslash = 0;
|
|
|
|
}
|
|
|
|
else if (quote)
|
|
|
|
{
|
|
|
|
ADD_CHAR('#');
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
RESET_TOKEN;
|
|
|
|
return tokens;
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
|
|
|
|
case '\"':
|
|
|
|
if (backslash)
|
|
|
|
{
|
|
|
|
ADD_CHAR('\"');
|
|
|
|
backslash = 0;
|
|
|
|
}
|
|
|
|
else if (quote)
|
|
|
|
{
|
|
|
|
quote = 0;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
quote = 1;
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
|
|
|
|
case '\r':
|
|
|
|
/* Pretend it does not exist! */
|
|
|
|
break;
|
|
|
|
|
|
|
|
case ' ':
|
|
|
|
case '\t':
|
|
|
|
if (quote)
|
|
|
|
{
|
|
|
|
ADD_CHAR(*p);
|
|
|
|
}
|
|
|
|
else if (backslash)
|
|
|
|
{
|
|
|
|
ADD_CHAR(*p);
|
|
|
|
backslash = 0;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
RESET_TOKEN;
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
|
|
|
|
default:
|
|
|
|
ADD_CHAR(*p);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
RESET_TOKEN;
|
|
|
|
return tokens;
|
|
|
|
}
|
|
|
|
|
2010-07-18 17:43:11 +00:00
|
|
|
void cfg_tokens_free(struct cfg_tokens* tokens)
|
2010-07-09 11:04:20 +00:00
|
|
|
{
|
2011-10-17 07:50:14 +00:00
|
|
|
if (tokens)
|
|
|
|
{
|
|
|
|
list_clear(tokens->list, hub_free);
|
|
|
|
list_destroy(tokens->list);
|
|
|
|
hub_free(tokens);
|
|
|
|
}
|
2010-07-09 11:04:20 +00:00
|
|
|
}
|
|
|
|
|
2010-07-18 17:43:11 +00:00
|
|
|
int cfg_token_add(struct cfg_tokens* tokens, char* new_token)
|
2010-07-09 11:04:20 +00:00
|
|
|
{
|
2010-07-18 17:43:11 +00:00
|
|
|
if (*new_token)
|
|
|
|
{
|
|
|
|
list_append(tokens->list, hub_strdup(new_token));
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
return 0;
|
|
|
|
}
|
2010-07-09 11:04:20 +00:00
|
|
|
|
2010-07-18 17:43:11 +00:00
|
|
|
size_t cfg_token_count(struct cfg_tokens* tokens)
|
|
|
|
{
|
|
|
|
return list_size(tokens->list);
|
|
|
|
}
|
2010-07-09 11:04:20 +00:00
|
|
|
|
2010-07-18 17:43:11 +00:00
|
|
|
char* cfg_token_get(struct cfg_tokens* tokens, size_t offset)
|
|
|
|
{
|
|
|
|
return list_get_index(tokens->list, offset);
|
2010-07-09 11:04:20 +00:00
|
|
|
}
|
|
|
|
|
2010-07-18 17:43:11 +00:00
|
|
|
char* cfg_token_get_first(struct cfg_tokens* tokens)
|
2010-07-09 11:04:20 +00:00
|
|
|
{
|
2010-07-18 17:43:11 +00:00
|
|
|
return list_get_first(tokens->list);
|
2010-07-09 11:04:20 +00:00
|
|
|
}
|
|
|
|
|
2010-07-18 17:43:11 +00:00
|
|
|
char* cfg_token_get_next(struct cfg_tokens* tokens)
|
2010-07-09 11:04:20 +00:00
|
|
|
{
|
2010-07-18 17:43:11 +00:00
|
|
|
return list_get_next(tokens->list);
|
2010-07-09 11:04:20 +00:00
|
|
|
}
|
2010-10-22 08:55:40 +00:00
|
|
|
|
|
|
|
struct cfg_settings
|
|
|
|
{
|
|
|
|
char* key;
|
|
|
|
char* value;
|
|
|
|
};
|
|
|
|
|
|
|
|
struct cfg_settings* cfg_settings_split(const char* line)
|
|
|
|
{
|
|
|
|
struct cfg_settings* s = NULL;
|
|
|
|
struct cfg_tokens* tok = NULL;
|
|
|
|
char* pos = NULL;
|
|
|
|
|
|
|
|
if ( !line
|
|
|
|
|| !*line
|
|
|
|
|| ((pos = (char*) strchr(line, '=')) == NULL)
|
|
|
|
|| ((s = hub_malloc_zero(sizeof(struct cfg_settings))) == NULL)
|
|
|
|
|| ((tok = cfg_tokenize(line)) == NULL)
|
2010-11-18 21:43:48 +00:00
|
|
|
|| (cfg_token_count(tok) < 1)
|
2010-10-22 08:55:40 +00:00
|
|
|
|| (cfg_token_count(tok) > 3)
|
|
|
|
|| (cfg_token_count(tok) == 3 && strcmp(cfg_token_get(tok, 1), "="))
|
|
|
|
)
|
|
|
|
{
|
|
|
|
cfg_tokens_free(tok);
|
|
|
|
cfg_settings_free(s);
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
2010-11-18 21:43:48 +00:00
|
|
|
if (cfg_token_count(tok) == 1)
|
2010-10-22 08:55:40 +00:00
|
|
|
{
|
|
|
|
char* key = cfg_token_get_first(tok);
|
|
|
|
pos = strchr(key, '=');
|
2010-11-18 21:43:48 +00:00
|
|
|
if (!pos)
|
|
|
|
{
|
|
|
|
cfg_tokens_free(tok);
|
|
|
|
cfg_settings_free(s);
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
2010-10-22 08:55:40 +00:00
|
|
|
pos[0] = 0;
|
|
|
|
key = strip_white_space(key);
|
|
|
|
|
|
|
|
if (!*key)
|
|
|
|
{
|
|
|
|
cfg_tokens_free(tok);
|
|
|
|
cfg_settings_free(s);
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
s->key = strdup(key);
|
2010-11-18 21:43:48 +00:00
|
|
|
s->value = strdup(strip_white_space(pos+1));
|
|
|
|
}
|
|
|
|
else if (cfg_token_count(tok) == 2)
|
|
|
|
{
|
|
|
|
char* key = cfg_token_get_first(tok);
|
|
|
|
char* val = cfg_token_get_next(tok);
|
|
|
|
|
|
|
|
if ((pos = strchr(key, '=')))
|
|
|
|
{
|
|
|
|
pos[0] = 0;
|
|
|
|
key = strip_white_space(key);
|
|
|
|
}
|
|
|
|
else if ((pos = strchr(val, '=')))
|
|
|
|
{
|
|
|
|
val = strip_white_space(pos+1);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
cfg_tokens_free(tok);
|
|
|
|
cfg_settings_free(s);
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!*key)
|
|
|
|
{
|
|
|
|
cfg_tokens_free(tok);
|
|
|
|
cfg_settings_free(s);
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
s->key = strdup(key);
|
|
|
|
s->value = strdup(val);
|
2010-10-22 08:55:40 +00:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
s->key = strdup(strip_white_space(cfg_token_get(tok, 0)));
|
|
|
|
s->value = strdup(strip_white_space(cfg_token_get(tok, 2)));
|
|
|
|
}
|
|
|
|
cfg_tokens_free(tok);
|
|
|
|
return s;
|
|
|
|
}
|
|
|
|
|
|
|
|
const char* cfg_settings_get_key(struct cfg_settings* s)
|
|
|
|
{
|
|
|
|
return s->key;
|
|
|
|
}
|
|
|
|
|
|
|
|
const char* cfg_settings_get_value(struct cfg_settings* s)
|
|
|
|
{
|
|
|
|
return s->value;
|
|
|
|
}
|
|
|
|
|
|
|
|
void cfg_settings_free(struct cfg_settings* s)
|
|
|
|
{
|
|
|
|
if (s)
|
|
|
|
{
|
|
|
|
hub_free(s->key);
|
|
|
|
hub_free(s->value);
|
|
|
|
hub_free(s);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|