You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
lighttpd2/src/main/config_parser.rl

1911 lines
52 KiB

#include <lighttpd/base.h>
#include <lighttpd/config_parser.h>
15 years ago
#ifdef HAVE_LUA_H
# include <lighttpd/config_lua.h>
#endif
#include <stdarg.h>
#define KV_LISTING_MAX 100
typedef enum {
TK_ERROR,
TK_EOF,
TK_AND,
TK_ASSIGN,
TK_ASSOCICATE,
TK_CAST_STRING,
TK_CAST_INT,
TK_COMMA,
TK_CURLY_CLOSE,
TK_CURLY_OPEN,
TK_DEFAULT,
TK_DIVIDE,
TK_ELSE,
TK_EQUAL,
TK_FALSE,
TK_GLOBAL,
TK_GREATER,
TK_GREATER_EQUAL,
TK_IF,
TK_INCLUDE,
TK_INCLUDE_LUA,
TK_INCLUDE_SHELL,
TK_INTEGER,
TK_LESS,
TK_LESS_EQUAL,
TK_LOCAL,
TK_MATCH,
TK_MINUS,
TK_MULTIPLY,
TK_NAME,
TK_NONE,
TK_NOT,
TK_NOT_EQUAL,
TK_NOT_MATCH,
TK_NOT_PREFIX,
TK_NOT_SUBNET,
TK_NOT_SUFFIX,
TK_OR,
TK_PARA_CLOSE,
TK_PARA_OPEN,
TK_PLUS,
TK_PREFIX,
TK_SEMICOLON,
TK_SETUP,
TK_SQUARE_CLOSE,
TK_SQUARE_OPEN,
TK_STRING,
TK_SUBNET,
TK_SUFFIX,
TK_TRUE
} liConfigToken;
typedef struct liConfigScope liConfigScope;
struct liConfigScope {
liConfigScope *parent;
GHashTable *variables;
};
typedef struct liConfigTokenizerContext liConfigTokenizerContext;
struct liConfigTokenizerContext {
liServer *srv;
liWorker *wrk;
gboolean master_config; /* whether includes/changing global vars are allowed */
/* ragel vars */
int cs;
const char *p, *pe, *eof;
/* remember token start position for error messages */
const char *token_start;
gsize token_line;
const gchar *token_line_start;
/* mark start of strings and similar */
const gchar *mark;
/* number stuff */
gboolean negative;
gint64 suffix_factor;
/* information about currently parsed file */
const gchar *filename;
const gchar *content; /* pointer to the data */
gsize len;
gsize line; /* holds current line */
const gchar *line_start;
/* result */
GString *token_string;
gint64 token_number;
/* TK_ERROR => have to parse it. parsing returns the token, but you can put it back here */
/* only put the last token back, as on the next parse call token_string and token_number get reset */
liConfigToken next_token;
/* variable storage */
liConfigScope *current_scope;
};
typedef struct liConditionTree liConditionTree;
struct liConditionTree {
gboolean negated;
15 years ago
liCondition *condition;
15 years ago
liConfigToken op;
liConditionTree *left, *right;
};
static liConfigToken tokenizer_error(liConfigTokenizerContext *ctx, GError **error, const char *fmt, ...) G_GNUC_PRINTF(3, 4);
GQuark li_config_error_quark(void) {
return g_quark_from_string("li-config-error-quark");
}
15 years ago
%%{
## ragel stuff
machine config_tokenizer;
15 years ago
variable p ctx->p;
variable pe ctx->pe;
variable eof ctx->eof;
15 years ago
access ctx->;
15 years ago
action mark {
ctx->mark = fpc;
15 years ago
}
action name {
g_string_append_len(ctx->token_string, ctx->mark, fpc - ctx->mark);
return TK_NAME;
}
action char {
g_string_append_c(ctx->token_string, fc);
15 years ago
}
action echar {
switch (fc) {
case 't':
g_string_append_c(ctx->token_string, '\t');
break;
case 'r':
g_string_append_c(ctx->token_string, '\r');
break;
case 'n':
g_string_append_c(ctx->token_string, '\n');
break;
case '"':
case '\'':
case '\\':
g_string_append_c(ctx->token_string, fc);
break;
default:
g_string_append_c(ctx->token_string, '\\');
g_string_append_c(ctx->token_string, fc);
break;
}
15 years ago
}
action xchar {
char xstr[3] = " ";
xstr[0] = fpc[-1]; xstr[1] = fpc[0];
g_string_append_c(ctx->token_string, strtol(xstr, NULL, 16));
15 years ago
}
action string {
++fpc;
return TK_STRING;
}
action e_char {
g_string_append_c(ctx->token_string, fc);
}
action e_echar {
switch (fc) {
case 't':
g_string_append_c(ctx->token_string, '\t');
break;
case 'r':
g_string_append_c(ctx->token_string, '\r');
break;
case 'n':
g_string_append_c(ctx->token_string, '\n');
break;
default:
g_string_append_c(ctx->token_string, fc);
break;
}
}
action e_xchar {
char xstr[3] = " ";
xstr[0] = fpc[-1]; xstr[1] = fpc[0];
g_string_append_c(ctx->token_string, strtol(xstr, NULL, 16));
}
action e_string {
++fpc;
return TK_STRING;
15 years ago
}
action int_start {
ctx->negative = FALSE;
ctx->token_number = 0;
ctx->suffix_factor = 1;
}
action int_negative {
ctx->negative = TRUE;
}
action dec_digit {
gint64 digit = fc - '0';
if (ctx->negative) {
if (ctx->token_number < G_MININT64 / 10 + digit) {
return tokenizer_error(ctx, error, "integer overflow");
}
ctx->token_number = 10*ctx->token_number - digit;
} else {
if (ctx->token_number > G_MAXINT64 / 10 - digit) {
return tokenizer_error(ctx, error, "integer overflow");
}
ctx->token_number = 10*ctx->token_number + digit;
}
}
action oct_digit {
gint64 digit = fc - '0';
if (ctx->negative) {
if (ctx->token_number < G_MININT64 / 8 + digit) {
return tokenizer_error(ctx, error, "integer overflow");
}
ctx->token_number = 8*ctx->token_number - digit;
} else {
if (ctx->token_number > G_MAXINT64 / 8 - digit) {
return tokenizer_error(ctx, error, "integer overflow");
}
ctx->token_number = 8*ctx->token_number + digit;
}
}
action hex_digit {
gint64 digit;
if (fc >= '0' && fc <= '9') digit = fc - '0';
else if (fc >= 'a' && fc <= 'f') digit = fc - 'a' + 10;
else /*if (fc >= 'A' && fc <= 'F')*/ digit = fc - 'A' + 10;
if (ctx->negative) {
if (ctx->token_number < G_MININT64 / 16 + digit) {
return tokenizer_error(ctx, error, "integer overflow");
}
ctx->token_number = 16*ctx->token_number - digit;
} else {
if (ctx->token_number > G_MAXINT64 / 16 - digit) {
return tokenizer_error(ctx, error, "integer overflow");
}
ctx->token_number = 16*ctx->token_number + digit;
}
}
action integer_suffix {
if (ctx->negative) {
if (ctx->token_number < G_MININT64 / ctx->suffix_factor) {
return tokenizer_error(ctx, error, "integer overflow in suffix");
}
} else if (ctx->token_number < 0) {
if (ctx->token_number > G_MAXINT64 / ctx->suffix_factor) {
return tokenizer_error(ctx, error, "integer overflow in suffix");
}
}
ctx->token_number *= ctx->suffix_factor;
}
action integer {
return TK_INTEGER;
}
noise_char = [\t \r\n#];
operator_char = [+\-*/=<>!^$~;,(){}[\]] | '"' | "'";
operator_separator_char = [;,(){}[\]];
keywords = ( 'and' | 'default' | 'cast' | 'else' | 'false' | 'global' | 'if' | 'include' | 'include_lua' | 'include_shell' | 'local' | 'none' | 'not' | 'or' | 'setup' | 'true' );
line_sane = ( '\n' ) >{ ctx->line++; ctx->line_start = fpc + 1; };
line_weird = ( '\r' ) >{ ctx->line++; ctx->line_start = fpc + 1; };
line_insane = ( '\r' ( '\n' >{ ctx->line--; } ) );
line = ( line_sane | line_weird | line_insane );
ws = ( '\t' | ' ' );
comment = ( '#' (any - line)* line );
noise = ( ws | line | comment )**;
integer_suffix_bytes =
( 'byte' %{ ctx->suffix_factor = G_GINT64_CONSTANT(1); }
| 'kbyte' %{ ctx->suffix_factor = G_GINT64_CONSTANT(1024); }
| 'mbyte' %{ ctx->suffix_factor = G_GINT64_CONSTANT(1024)*1024; }
| 'gbyte' %{ ctx->suffix_factor = G_GINT64_CONSTANT(1024)*1024*1024; }
| 'tbyte' %{ ctx->suffix_factor = G_GINT64_CONSTANT(1024)*1024*1024*1024; }
| 'pbyte' %{ ctx->suffix_factor = G_GINT64_CONSTANT(1024)*1024*1024*1024*1024; }
);
integer_suffix_bits =
( 'bit' %{ ctx->token_number /= 8; ctx->suffix_factor = G_GINT64_CONSTANT(1); }
| 'kbit' %{ ctx->suffix_factor = G_GINT64_CONSTANT(1024) / 8; }
| 'mbit' %{ ctx->suffix_factor = G_GINT64_CONSTANT(1024)*1024 / 8; }
| 'gbit' %{ ctx->suffix_factor = G_GINT64_CONSTANT(1024)*1024*1024 / 8; }
| 'tbit' %{ ctx->suffix_factor = G_GINT64_CONSTANT(1024)*1024*1024*1024 / 8; }
| 'pbit' %{ ctx->suffix_factor = G_GINT64_CONSTANT(1024)*1024*1024*1024*1024 / 8; }
);
integer_suffix_seconds =
( 'sec' %{ ctx->suffix_factor = G_GINT64_CONSTANT(1); }
| 'min' %{ ctx->suffix_factor = G_GINT64_CONSTANT(60); }
| 'hours' %{ ctx->suffix_factor = G_GINT64_CONSTANT(3600); }
| 'days' %{ ctx->suffix_factor = G_GINT64_CONSTANT(24)*3600; }
);
integer_suffix = ( integer_suffix_bytes | integer_suffix_bits | integer_suffix_seconds ) %integer_suffix;
integer_base = ('-'@int_negative)?
( ([1-9] [0-9]*)$dec_digit
| '0'
| '0' ([0-9]+)$oct_digit
| '0x' (xdigit+)$hex_digit
)>int_start;
# noise_char only matches after integer_suffix, as noise matches all noice chars greedy
integer = (integer_base noise integer_suffix?)%integer (noise_char | operator_char);
string =
( ( '"' ( (any - [\\"])@char | ('\\' [^x])@echar | ('\\x' xdigit{2})@xchar )* '"' ) @string
| ( "'" ( (any - [\\'])@char | ('\\' [^x])@echar | ('\\x' xdigit{2})@xchar )* "'" ) @string
| ( 'e"' ( (any - [\\"])@e_char | ('\\' [trn\\"'])@e_echar | ('\\x' xdigit{2})@e_xchar )* '"' ) @e_string
| ( "e'" ( (any - [\\'])@e_char | ('\\' [trn\\"'])@e_echar | ('\\x' xdigit{2})@e_xchar )* "'" ) @e_string
);
keyword =
( 'and' %{ return TK_AND; }
| 'default' %{ return TK_DEFAULT; }
| 'else' %{ return TK_ELSE; }
| 'false' %{ return TK_FALSE; }
| 'global' %{ return TK_GLOBAL; }
| 'if' %{ return TK_IF; }
| 'include' %{ return TK_INCLUDE; }
| 'include_lua' %{ return TK_INCLUDE_LUA; }
| 'include_shell' %{ return TK_INCLUDE_SHELL; }
| 'local' %{ return TK_LOCAL; }
| 'none' %{ return TK_NONE; }
| 'not' %{ return TK_NOT; }
| 'or' %{ return TK_OR; }
| 'setup' %{ return TK_SETUP; }
| 'true' %{ return TK_TRUE; }
) %(identifier, 1) (noise_char | operator_char)?;
name = ( (alpha | '_') (alnum | [._])* - keywords) >mark %name (noise_char | operator_char)?;
cast =
( 'cast' noise '(' noise 'int' noise ')' @{ ++fpc; return TK_CAST_INT; }
| 'cast' noise '(' noise 'string' noise ')' @{ ++fpc; return TK_CAST_STRING; }
);
single_operator =
( ';' @{ ++fpc; return TK_SEMICOLON; }
| ',' @{ ++fpc; return TK_COMMA; }
| '(' @{ ++fpc; return TK_PARA_OPEN; }
| ')' @{ ++fpc; return TK_PARA_CLOSE; }
| '[' @{ ++fpc; return TK_SQUARE_OPEN; }
| ']' @{ ++fpc; return TK_SQUARE_CLOSE; }
| '{' @{ ++fpc; return TK_CURLY_OPEN; }
| '}' @{ ++fpc; return TK_CURLY_CLOSE; }
);
operator =
# minus not accepted here in front of a digit
'-' %{ return TK_MINUS; } (noise_char | operator_separator_char | alpha | '_' | '"' | "'")? |
( '+' %{ return TK_PLUS; }
| '*' %{ return TK_MULTIPLY; }
| '/' %{ return TK_DIVIDE; }
| '!' %{ return TK_NOT; }
| '==' %{ return TK_EQUAL; }
| '!=' %{ return TK_NOT_EQUAL; }
| '=^' %{ return TK_PREFIX; }
| '!^' %{ return TK_NOT_PREFIX; }
| '=$' %{ return TK_SUFFIX; }
| '!$' %{ return TK_NOT_SUFFIX; }
| '<' %{ return TK_LESS; }
| '<=' %{ return TK_LESS_EQUAL; }
| '>' %{ return TK_GREATER; }
| '>=' %{ return TK_GREATER_EQUAL; }
| '=~' %{ return TK_MATCH; }
| '!~' %{ return TK_NOT_MATCH; }
| '=/' %{ return TK_SUBNET; }
| '!/' %{ return TK_NOT_SUBNET; }
| '=' %{ return TK_ASSIGN; }
| '=>' %{ return TK_ASSOCICATE; }
) (noise_char | operator_separator_char | alnum | '_' | '"' | "'")?;
endoffile = '' %{ fpc = NULL; return TK_EOF; };
token :=
noise
( endoffile | keyword | name | cast | single_operator | operator | integer | string );
}%%
%% write data noerror nofinal;
static void set_config_error(liConfigTokenizerContext *ctx, GError **error, const char *fmt, va_list ap) {
GString *msg = g_string_sized_new(127);
g_string_vprintf(msg, fmt, ap);
g_set_error(error, LI_CONFIG_ERROR, 0, "error in %s:%" G_GSIZE_FORMAT ":%" G_GSIZE_FORMAT ": %s", ctx->filename, ctx->token_line, 1 + ctx->token_start - ctx->token_line_start, msg->str);
g_string_free(msg, TRUE);
}
static liConfigToken tokenizer_error(liConfigTokenizerContext *ctx, GError **error, const char *fmt, ...) {
va_list ap;
va_start(ap, fmt);
set_config_error(ctx, error, fmt, ap);
va_end(ap);
return TK_ERROR;
}
static gboolean parse_error(liConfigTokenizerContext *ctx, GError **error, const char *fmt, ...) {
va_list ap;
15 years ago
va_start(ap, fmt);
set_config_error(ctx, error, fmt, ap);
va_end(ap);
return FALSE;
}
static void tokenizer_init(liServer *srv, liWorker *wrk, liConfigTokenizerContext *ctx, const gchar *filename, const gchar *content, gsize len) {
memset(ctx, 0, sizeof(*ctx));
ctx->srv = srv;
ctx->wrk = wrk;
ctx->p = content;
ctx->pe = ctx->eof = content + len;
15 years ago
ctx->filename = filename;
ctx->content = content;
ctx->len = len;
ctx->line = 1;
ctx->line_start = ctx->content;
15 years ago
ctx->token_string = g_string_sized_new(31);
}
15 years ago
static gboolean tokenizer_init_file(liServer *srv, liWorker *wrk, liConfigTokenizerContext *ctx, const gchar *filename, GError **error) {
memset(ctx, 0, sizeof(*ctx));
ctx->srv = srv;
ctx->wrk = wrk;
if (!g_file_get_contents(filename, (gchar**) &ctx->content, &ctx->len, error)) return FALSE;
ctx->p = ctx->content;
ctx->pe = ctx->eof = ctx->content + ctx->len;
15 years ago
ctx->filename = filename;
ctx->line = 1;
ctx->line_start = ctx->content;
15 years ago
ctx->token_string = g_string_sized_new(31);
15 years ago
return TRUE;
}
15 years ago
static void tokenizer_clear(liConfigTokenizerContext *ctx) {
g_string_free(ctx->token_string, TRUE);
}
15 years ago
static liConfigToken tokenizer_next(liConfigTokenizerContext *ctx, GError **error) {
g_string_truncate(ctx->token_string, 0);
ctx->token_number = 0;
if (NULL == ctx->p) return tokenizer_error(ctx, error, "already reached end of file");
15 years ago
ctx->token_start = ctx->p;
ctx->token_line = ctx->line;
ctx->token_line_start = ctx->line_start;
(void) config_tokenizer_en_token;
%% write init;
15 years ago
%% write exec;
return tokenizer_error(ctx, error, "couldn't parse token");
}
#define NEXT(token) do { \
if (TK_ERROR != ctx->next_token) { token = ctx->next_token; ctx->next_token = TK_ERROR; } \
else if (TK_ERROR == (token = tokenizer_next(ctx, error))) goto error; \
} while (0)
#define REMEMBER(token) do { \
LI_FORCE_ASSERT(TK_ERROR == ctx->next_token); /* mustn't contain a token */ \
ctx->next_token = token; \
} while (0)
static void scope_enter(liConfigTokenizerContext *ctx) {
liConfigScope *scope = g_slice_new0(liConfigScope);
scope->parent = ctx->current_scope;
scope->variables = li_value_new_hashtable();
ctx->current_scope = scope;
}
static void scope_leave(liConfigTokenizerContext *ctx) {
liConfigScope *scope = ctx->current_scope;
LI_FORCE_ASSERT(NULL != scope);
ctx->current_scope = scope->parent;
g_hash_table_destroy(scope->variables);
scope->variables = NULL;
scope->parent = NULL;
g_slice_free(liConfigScope, scope);
}
/* copy name, takeover value */
static gboolean scope_setvar(liConfigTokenizerContext *ctx, GString *name, liValue *value, GError **error) {
liConfigScope *scope;
if (g_str_has_prefix(name->str, "sys.")) {
li_value_free(value);
return parse_error(ctx, error, "sys.* variables are read only");
}
scope = ctx->current_scope;
/* search whether name already exists in a scope. otherwise make it local */
while (NULL != scope) {
if (NULL != g_hash_table_lookup(scope->variables, name)) {
g_hash_table_insert(scope->variables, g_string_new_len(GSTR_LEN(name)), value);
return TRUE;
}
scope = scope->parent;
}
if (ctx->master_config) {
/* modify global vars in master config */
if (NULL != ctx->srv->config_global_vars && NULL != g_hash_table_lookup(ctx->srv->config_global_vars, name)) {
g_hash_table_insert(ctx->srv->config_global_vars, g_string_new_len(GSTR_LEN(name)), value);
return TRUE;
}
}
15 years ago
if (NULL != ctx->current_scope) {
g_hash_table_insert(ctx->current_scope->variables, g_string_new_len(GSTR_LEN(name)), value);
return TRUE;
}
/* no scope... do nothing */
li_value_free(value);
return parse_error(ctx, error, "can't write variable without scope");
}
/* copy name, takeover value */
static gboolean scope_local_setvar(liConfigTokenizerContext *ctx, GString *name, liValue *value, GError **error) {
if (g_str_has_prefix(name->str, "sys.")) {
li_value_free(value);
return parse_error(ctx, error, "sys.* variables are read only");
}
if (NULL != ctx->current_scope) {
g_hash_table_insert(ctx->current_scope->variables, g_string_new_len(GSTR_LEN(name)), value);
return TRUE;
}
/* no scope... do nothing */
li_value_free(value);
return parse_error(ctx, error, "can't write variable without scope");
}
/* copy name, takeover value */
static gboolean scope_global_setvar(liConfigTokenizerContext *ctx, GString *name, liValue *value, GError **error) {
if (g_str_has_prefix(name->str, "sys.")) {
li_value_free(value);
return parse_error(ctx, error, "sys.* variables are read only");
}
if (!ctx->master_config) {
li_value_free(value);
return parse_error(ctx, error, "modifying global variables not allowed in this context");
}
if (NULL == ctx->srv->config_global_vars) {
li_value_free(value);
return parse_error(ctx, error, "no global variable scope");
}
g_hash_table_insert(ctx->srv->config_global_vars, g_string_new_len(GSTR_LEN(name)), value);
return TRUE;
}
/* read-only pointer, no reference, return NULL if not found - no error */
/* only to read "action" variables */
static liValue* scope_peekvar(liConfigTokenizerContext *ctx, GString *name) {
liConfigScope *scope = ctx->current_scope;
liValue *value;
/* can't peek sys. vars */
if (g_str_has_prefix(name->str, "sys.")) return NULL;
15 years ago
while (NULL != scope) {
if (NULL != (value = g_hash_table_lookup(scope->variables, name))) {
return value;
}
scope = scope->parent;
}
15 years ago
if (NULL != ctx->srv->config_global_vars && NULL != (value = g_hash_table_lookup(ctx->srv->config_global_vars, name))) {
return value;
}
return NULL;
}
static liValue* scope_getvar(liConfigTokenizerContext *ctx, GString *name, GError **error) {
liValue *value;
if (g_str_has_prefix(name->str, "sys.")) {
if (g_str_equal(name->str, "sys.pid")) {
return li_value_new_number(getpid());
} else if (g_str_equal(name->str, "sys.cwd")) {
gchar cwd[1024];
if (NULL != getcwd(cwd, sizeof(cwd)-1)) {
return li_value_new_string(g_string_new(cwd));
} else {
parse_error(ctx, error, "failed to get CWD: %s", g_strerror(errno));
return NULL;
}
} else if (g_str_equal(name->str, "sys.version")) {
return li_value_new_string(g_string_new(PACKAGE_VERSION));
} else if (g_str_has_prefix(name->str, "sys.env.")) {
/* look up string in environment, push value onto stack */
gchar *env = getenv(name->str + sizeof("sys.env.") - 1);
if (env == NULL) {
parse_error(ctx, error, "undefined environment variable: %s", name->str + sizeof("sys.env.") - 1);
return NULL;
}
return li_value_new_string(g_string_new(env));
}
parse_error(ctx, error, "unknown sys.* variable: %s", name->str);
return NULL;
}
value = scope_peekvar(ctx, name);
if (NULL != value) return li_value_copy(value);
parse_error(ctx, error, "undefined variable '%s'", name->str);
return NULL;
}
static gboolean p_include(liAction *list, liConfigTokenizerContext *ctx, GError **error);
static gboolean p_include_lua(liAction *list, liConfigTokenizerContext *ctx, GError **error);
static gboolean p_include_shell(liAction *list, liConfigTokenizerContext *ctx, GError **error);
static gboolean p_setup(GString *name, liConfigTokenizerContext *ctx, GError **error);
static gboolean p_setup_block(liConfigTokenizerContext *ctx, GError **error);
static gboolean p_action(liAction *list, GString *name, liConfigTokenizerContext *ctx, GError **error);
static gboolean p_actions(gboolean block, liAction *list, liConfigTokenizerContext *ctx, GError **error);
static gboolean p_value_list(gint *key_value_nesting, liValue **result, gboolean key_value_list, liValue *pre_value, liConfigToken end, liConfigTokenizerContext *ctx, GError **error);
static gboolean p_value_group(gint *key_value_nesting, liValue **value, liConfigTokenizerContext *ctx, GError **error);
static gboolean p_value(gint *key_value_nesting, liValue **value, liConfigToken preOp, liConfigTokenizerContext *ctx, GError **error);
static gboolean p_vardef(GString *name, int normalLocalGlobal, liConfigTokenizerContext *ctx, GError **error);
static gboolean p_parameter_values(liValue **result, liConfigTokenizerContext *ctx, GError **error);
static liAction* cond_walk(liServer *srv, liConditionTree *tree, liAction *positive, liAction *negative);
static gboolean p_condition_value(liConditionTree **cond, liConfigTokenizerContext *ctx, GError **error);
static gboolean p_condition_expr(liConditionTree **tree, liConfigToken preOp, liConfigTokenizerContext *ctx, GError **error);
static gboolean p_condition(liAction *list, liConfigTokenizerContext *ctx, GError **error);
/* whether token can be start of a value */
static gboolean is_value_start_token(liConfigToken token) {
switch (token) {
case TK_CAST_STRING:
case TK_CAST_INT:
case TK_CURLY_OPEN:
case TK_DEFAULT:
case TK_FALSE:
case TK_INTEGER:
case TK_MINUS:
case TK_NAME:
case TK_NONE:
case TK_NOT:
case TK_PARA_OPEN:
case TK_SQUARE_OPEN:
case TK_STRING:
case TK_TRUE:
return TRUE;
default:
return FALSE;
}
}
static int _op_precedence(liConfigToken op) {
switch (op) {
case TK_OR:
return 1;
case TK_AND:
return 2;
case TK_PLUS:
case TK_MINUS:
return 3;
case TK_MULTIPLY:
case TK_DIVIDE:
return 4;
default:
return 0;
}
}
/* returns TRUE iff a <op1> b <op2> c == (a <op1> b) <op2> c
* returns TRUE if <op2> isn't an operator
* returns FALSE if <op1> isn't an operator but <op2> is
*/
static gboolean operator_precedence(liConfigToken op1, liConfigToken op2) {
return _op_precedence(op1) >= _op_precedence(op2);
}
/* overflow checks, assuming two-complement representation, i.e. G_MININT64 == (-G_MAXINT64)+ - 1, -G_MININT64 "==" G_MININT64 */
static gboolean overflow_op_plus_int(gint64 a, gint64 b) {
if (a < 0) {
if (b >= 0) return FALSE;
return (a < G_MININT64 - b);
} else {
if (b <= 0) return FALSE;
return (a > G_MAXINT64 - b);
}
}
15 years ago
static gboolean overflow_op_minus_int(gint64 a, gint64 b) {
if (a < 0) {
if (b <= 0) return FALSE;
return (a < G_MININT64 + b);
} else {
if (b >= 0) return FALSE;
return (a > G_MAXINT64 + b);
}
}
15 years ago
static gboolean overflow_op_multiply_int(gint64 a, gint64 b) {
if (0 == a || 0 == b) return FALSE;
if (a < 0) {
if (b > 0) return (a < G_MININT64 / b);
if (G_MININT64 == a || G_MININT64 == b) return TRUE;
/* b < 0 */ return ((-a) > G_MAXINT64 / (-b));
} else { /* a > 0 */
if (b < 0) return (b < G_MININT64 / a);
/* b > 0 */ return (a > G_MAXINT64 / b);
}
}
static gboolean overflow_op_divide_int(gint64 a, gint64 b) {
/* only MIN INT / -1 == MAX INT + 1 overflows */
return (-1 == b && G_MININT64 == a);
}
/* always frees v1 and v2 */
static gboolean op_execute(liValue **vresult, liConfigToken op, liValue *v1, liValue *v2, liConfigTokenizerContext *ctx, GError **error) {
if (li_value_type(v1) == li_value_type(v2)) {
switch (li_value_type(v1)) {
case LI_VALUE_NUMBER:
switch (op) {
case TK_AND:
case TK_OR:
parse_error(ctx, error, "boolean operations not allowed on numbers");
goto error;
case TK_PLUS:
if (overflow_op_plus_int(v1->data.number, v2->data.number)) {
parse_error(ctx, error, "overflow in addition");
goto error;
}
*vresult = li_value_new_number(v1->data.number + v2->data.number);
break;
case TK_MINUS:
if (overflow_op_minus_int(v1->data.number, v2->data.number)) {
parse_error(ctx, error, "overflow in subtraction");
goto error;
}
*vresult = li_value_new_number(v1->data.number - v2->data.number);
break;
case TK_MULTIPLY:
if (overflow_op_multiply_int(v1->data.number, v2->data.number)) {
parse_error(ctx, error, "overflow in product");
goto error;
}