[srvx-commits] CVS: services/src main.c,1.115,1.116 rules.c,1.3,1.4
Brought to you by:
entrope
From: Zoot <zo...@us...> - 2001-10-13 23:51:36
|
Update of /cvsroot/srvx/services/src In directory usw-pr-cvs1:/tmp/cvs-serv21566/src Modified Files: main.c rules.c Log Message: Fix rule parsing and add a NOT operator. Index: main.c =================================================================== RCS file: /cvsroot/srvx/services/src/main.c,v retrieving revision 1.115 retrieving revision 1.116 diff -C2 -r1.115 -r1.116 *** main.c 2001/10/12 04:03:51 1.115 --- main.c 2001/10/13 23:51:33 1.116 *************** *** 53,56 **** --- 53,57 ---- #include "hash.h" #include "log.h" + #include "rules.h" #include "messages.h" #include "modules.h" *************** *** 463,466 **** --- 464,468 ---- sockcheck_init(); /* could be modularized */ hash_init(); + rules_init(); service_init(); conf_rlimits(); Index: rules.c =================================================================== RCS file: /cvsroot/srvx/services/src/rules.c,v retrieving revision 1.3 retrieving revision 1.4 diff -C2 -r1.3 -r1.4 *** rules.c 2001/10/02 16:04:36 1.3 --- rules.c 2001/10/13 23:51:33 1.4 *************** *** 66,87 **** maxlen = pos - *string; dp = dest = malloc(maxlen); ! for (pos=*string; *pos && isalnum(*pos); pos++) *dp++ = *pos++; *dp = 0; return dest; } static struct expression * ! expr_parse_int(const unsigned char **string) { struct expression *expr; unsigned int nn, argno, num_args; struct parse_expression **args, *arg; ! if (**string++ != '(') return NULL; expr = NULL; argno = 0; num_args = 4; args = malloc(num_args*sizeof(*args)); ! while (**string && (**string != ')')) { if ((argno+1) == num_args) { num_args <<= 1; --- 66,90 ---- maxlen = pos - *string; dp = dest = malloc(maxlen); ! for (pos=*string; *pos && isalnum(*pos); pos++) *dp++ = *pos; *dp = 0; + *string = pos; return dest; } static struct expression * ! expr_parse_int(const unsigned char **input) { struct expression *expr; unsigned int nn, argno, num_args; struct parse_expression **args, *arg; + const unsigned char *string; ! string = *input; ! if (*string++ != '(') return NULL; expr = NULL; argno = 0; num_args = 4; args = malloc(num_args*sizeof(*args)); ! while (*string && (*string != ')')) { if ((argno+1) == num_args) { num_args <<= 1; *************** *** 89,113 **** } arg = args[argno++] = calloc(1, sizeof(**args)); ! if (**string == '(') { arg->type = PEXPR_EXPR; ! arg->u.expr = expr_parse_int(string); ! if (!arg->u.expr) while (**string) (*string)++; ! } else if (**string == '"') { arg->type = PEXPR_QSTRING; ! arg->u.qstring = expr_parse_string(string); ! if (!arg->u.qstring) while (**string) (*string)++; ! } else if (isalnum(**string)) { arg->type = PEXPR_TOKEN; ! arg->u.token = expr_parse_token(string); ! if (!arg->u.token) while (**string) (*string)++; } else { /* parse error, skip to end of input */ ! while (**string) (*string)++; } ! if (!**string) goto out; ! if (expr_skip_whitespace(string)) goto out; ! argno++; } ! if (!**string) goto out; /* make sure first argument (start of list) is a token */ if (args[0]->type != PEXPR_TOKEN) goto out; --- 92,115 ---- } arg = args[argno++] = calloc(1, sizeof(**args)); ! if (*string == '(') { arg->type = PEXPR_EXPR; ! arg->u.expr = expr_parse_int(&string); ! if (!arg->u.expr) while (*string) (string)++; ! } else if (*string == '"') { arg->type = PEXPR_QSTRING; ! arg->u.qstring = expr_parse_string(&string); ! if (!arg->u.qstring) while (*string) (string)++; ! } else if (isalnum(*string)) { arg->type = PEXPR_TOKEN; ! arg->u.token = expr_parse_token(&string); ! if (!arg->u.token) while (*string) (string)++; } else { /* parse error, skip to end of input */ ! while (*string) (string)++; } ! if (!*string) goto out; ! if (expr_skip_whitespace(&string)) goto out; } ! if (!*string) goto out; /* make sure first argument (start of list) is a token */ if (args[0]->type != PEXPR_TOKEN) goto out; *************** *** 116,123 **** --- 118,127 ---- if (!(expr->oper = dict_find(expr_opers, args[0]->u.token, NULL))) { free(expr); + expr = NULL; goto out; } if (expr->oper->compile(expr, argno, args)) { free(expr); + expr = NULL; goto out; } *************** *** 131,134 **** --- 135,144 ---- } free(args); + + if(*string == ')') { + string++; + } + + *input = string; return expr; } *************** *** 161,165 **** } ! static EXPR_COMPILE_FUNCTION(oper_or_compile) { unsigned int nn; --- 171,177 ---- } ! /* Compiles an operator that contains only subexpressions; ! used by the or, and operators. */ ! static EXPR_COMPILE_FUNCTION(oper_subexpr_compile) { unsigned int nn; *************** *** 178,187 **** } ! static EXPR_FREE_FUNCTION(oper_or_free) { ! unsigned int nn; ! for (nn=0; nn<expr->argc; nn++) { ! expression_free(expr->args[nn]); ! } } --- 190,196 ---- } ! static EXPR_COMPILE_FUNCTION(oper_not_compile) { ! return (argc != 2) ? 1 : oper_subexpr_compile(expr, argc, pexpr); } *************** *** 207,212 **** } ! static EXPR_COMPILE_FUNCTION(oper_true_compile) { (void)argc; (void)pexpr; expr->argc = 0; --- 216,237 ---- } ! static EXPR_EVAL_FUNCTION(oper_not_eval) { + return !expression_evaluate(expr->args[0], context); + } + + /* The counterpart to oper_subexpr_compile frees all + subexpressions; used by the or, and, not operators. */ + static EXPR_FREE_FUNCTION(oper_subexpr_free) + { + unsigned int nn; + for (nn=0; nn<expr->argc; nn++) { + expression_free(expr->args[nn]); + } + } + + /* Compiles an operator that returns a constant. */ + static EXPR_COMPILE_FUNCTION(oper_constant_compile) + { (void)argc; (void)pexpr; expr->argc = 0; *************** *** 215,223 **** } - static EXPR_FREE_FUNCTION(oper_true_free) - { - (void)expr; - } - static EXPR_EVAL_FUNCTION(oper_true_eval) { --- 240,243 ---- *************** *** 232,235 **** --- 252,261 ---- } + /* The counterpart to oper_constant_compile. */ + static EXPR_FREE_FUNCTION(oper_constant_free) + { + (void)expr; + } + void rules_init(void) *************** *** 241,268 **** op = calloc(1, sizeof(*op)); op->name = "or"; ! op->compile = oper_or_compile; op->eval = oper_or_eval; ! op->free_ = oper_or_free; register_expression_operator(op); /* Register AND operator */ op = calloc(1, sizeof(*op)); op->name = "and"; ! op->compile = oper_or_compile; /* same as "or" */ op->eval = oper_and_eval; ! op->free_ = oper_or_free; /* same as "or" */ register_expression_operator(op); /* Register TRUE operator */ op = calloc(1, sizeof(*op)); op->name = "true"; ! op->compile = oper_true_compile; op->eval = oper_true_eval; ! op->free_ = oper_true_free; register_expression_operator(op); /* Register FALSE operator */ op = calloc(1, sizeof(*op)); op->name = "false"; ! op->compile = oper_true_compile; /* same as "true" */ op->eval = oper_false_eval; ! op->free_ = oper_true_free; /* same as "true" */ register_expression_operator(op); } --- 267,301 ---- op = calloc(1, sizeof(*op)); op->name = "or"; ! op->compile = oper_subexpr_compile; op->eval = oper_or_eval; ! op->free_ = oper_subexpr_free; register_expression_operator(op); /* Register AND operator */ op = calloc(1, sizeof(*op)); op->name = "and"; ! op->compile = oper_subexpr_compile; /* same as "or" */ op->eval = oper_and_eval; ! op->free_ = oper_subexpr_free; /* same as "or" */ ! register_expression_operator(op); ! /* Register NOT operator */ ! op = calloc(1, sizeof(*op)); ! op->name = "not"; ! op->compile = oper_not_compile; ! op->eval = oper_not_eval; ! op->free_ = oper_subexpr_free; /* same as "or" */ register_expression_operator(op); /* Register TRUE operator */ op = calloc(1, sizeof(*op)); op->name = "true"; ! op->compile = oper_constant_compile; op->eval = oper_true_eval; ! op->free_ = oper_constant_free; register_expression_operator(op); /* Register FALSE operator */ op = calloc(1, sizeof(*op)); op->name = "false"; ! op->compile = oper_constant_compile; /* same as "true" */ op->eval = oper_false_eval; ! op->free_ = oper_constant_free; /* same as "true" */ register_expression_operator(op); } |