Skip to content

Commit

Permalink
Merge pull request chjj#72 from yshui/clean-opt2
Browse files Browse the repository at this point in the history
Option/config parsing clean up
  • Loading branch information
yshui authored Dec 21, 2018
2 parents ee2be09 + c8a9f32 commit 4dce20f
Show file tree
Hide file tree
Showing 12 changed files with 732 additions and 611 deletions.
58 changes: 35 additions & 23 deletions src/c2.c
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,8 @@
#include "utils.h"
#include "log.h"

#pragma GCC diagnostic error "-Wunused-parameter"

#define C2_MAX_LEVELS 10

typedef struct _c2_b c2_b_t;
Expand Down Expand Up @@ -301,22 +303,19 @@ c2h_b_opcmp(c2_b_op_t op1, c2_b_op_t op2) {
}

static int
c2_parse_grp(session_t *ps, const char *pattern, int offset, c2_ptr_t *presult, int level);
c2_parse_grp(const char *pattern, int offset, c2_ptr_t *presult, int level);

static int
c2_parse_target(session_t *ps, const char *pattern, int offset, c2_ptr_t *presult);
c2_parse_target(const char *pattern, int offset, c2_ptr_t *presult);

static int
c2_parse_op(const char *pattern, int offset, c2_ptr_t *presult);

static int
c2_parse_pattern(session_t *ps, const char *pattern, int offset, c2_ptr_t *presult);
c2_parse_pattern(const char *pattern, int offset, c2_ptr_t *presult);

static int
c2_parse_legacy(session_t *ps, const char *pattern, int offset, c2_ptr_t *presult);

static bool
c2_l_postprocess(session_t *ps, c2_l_t *pleaf);
c2_parse_legacy(const char *pattern, int offset, c2_ptr_t *presult);

static void
c2_free(c2_ptr_t p);
Expand Down Expand Up @@ -351,7 +350,7 @@ c2_match_once(session_t *ps, win *w, const c2_ptr_t cond);
* Parse a condition string.
*/
c2_lptr_t *
c2_parse(session_t *ps, c2_lptr_t **pcondlst, const char *pattern,
c2_parse(c2_lptr_t **pcondlst, const char *pattern,
void *data) {
if (!pattern)
return NULL;
Expand All @@ -361,9 +360,9 @@ c2_parse(session_t *ps, c2_lptr_t **pcondlst, const char *pattern,
int offset = -1;

if (strlen(pattern) >= 2 && ':' == pattern[1])
offset = c2_parse_legacy(ps, pattern, 0, &result);
offset = c2_parse_legacy(pattern, 0, &result);
else
offset = c2_parse_grp(ps, pattern, 0, &result, 0);
offset = c2_parse_grp(pattern, 0, &result, 0);

if (offset < 0) {
c2_freep(&result);
Expand Down Expand Up @@ -405,7 +404,7 @@ c2_parse(session_t *ps, c2_lptr_t **pcondlst, const char *pattern,
* @return offset of next character in string
*/
static int
c2_parse_grp(session_t *ps, const char *pattern, int offset, c2_ptr_t *presult, int level) {
c2_parse_grp(const char *pattern, int offset, c2_ptr_t *presult, int level) {
// Check for recursion levels
if (level > C2_MAX_LEVELS)
c2_error("Exceeded maximum recursion levels.");
Expand Down Expand Up @@ -505,23 +504,20 @@ c2_parse_grp(session_t *ps, const char *pattern, int offset, c2_ptr_t *presult,

// It's a subgroup if it starts with '('
if ('(' == pattern[offset]) {
if ((offset = c2_parse_grp(ps, pattern, offset + 1, pele, level + 1)) < 0)
if ((offset = c2_parse_grp(pattern, offset + 1, pele, level + 1)) < 0)
goto fail;
}
// Otherwise it's a leaf
else {
if ((offset = c2_parse_target(ps, pattern, offset, pele)) < 0)
if ((offset = c2_parse_target(pattern, offset, pele)) < 0)
goto fail;

assert(!pele->isbranch && !c2_ptr_isempty(*pele));

if ((offset = c2_parse_op(pattern, offset, pele)) < 0)
goto fail;

if ((offset = c2_parse_pattern(ps, pattern, offset, pele)) < 0)
goto fail;

if (!c2_l_postprocess(ps, pele->l))
if ((offset = c2_parse_pattern(pattern, offset, pele)) < 0)
goto fail;
}
// Decrement offset -- we will increment it in loop update
Expand Down Expand Up @@ -579,7 +575,7 @@ c2_parse_grp(session_t *ps, const char *pattern, int offset, c2_ptr_t *presult,
* Parse the target part of a rule.
*/
static int
c2_parse_target(session_t *ps, const char *pattern, int offset, c2_ptr_t *presult) {
c2_parse_target(const char *pattern, int offset, c2_ptr_t *presult) {
// Initialize leaf
presult->isbranch = false;
presult->l = cmalloc(c2_l_t);
Expand Down Expand Up @@ -838,7 +834,7 @@ c2_parse_op(const char *pattern, int offset, c2_ptr_t *presult) {
* Parse the pattern part of a leaf.
*/
static int
c2_parse_pattern(session_t *ps, const char *pattern, int offset, c2_ptr_t *presult) {
c2_parse_pattern(const char *pattern, int offset, c2_ptr_t *presult) {
c2_l_t * const pleaf = presult->l;

// Exists operator cannot have pattern
Expand Down Expand Up @@ -977,7 +973,7 @@ c2_parse_pattern(session_t *ps, const char *pattern, int offset, c2_ptr_t *presu
* Parse a condition with legacy syntax.
*/
static int
c2_parse_legacy(session_t *ps, const char *pattern, int offset, c2_ptr_t *presult) {
c2_parse_legacy(const char *pattern, int offset, c2_ptr_t *presult) {
unsigned plen = strlen(pattern + offset);

if (plen < 4 || ':' != pattern[offset + 1]
Expand Down Expand Up @@ -1033,9 +1029,6 @@ c2_parse_legacy(session_t *ps, const char *pattern, int offset, c2_ptr_t *presul
// Copy the pattern
pleaf->ptnstr = strdup(pattern + offset);

if (!c2_l_postprocess(ps, pleaf))
return -1;

return offset;

fail:
Expand Down Expand Up @@ -1145,6 +1138,25 @@ c2_l_postprocess(session_t *ps, c2_l_t *pleaf) {

return true;
}

static bool c2_tree_postprocess(session_t *ps, c2_ptr_t node) {
if (!node.isbranch) {
return c2_l_postprocess(ps, node.l);
}
if (!c2_tree_postprocess(ps, node.b->opr1))
return false;
return c2_tree_postprocess(ps, node.b->opr2);
}

bool c2_list_postprocess(session_t *ps, c2_lptr_t *list) {
c2_lptr_t *head = list;
while (head) {
if (!c2_tree_postprocess(ps, head->ptr))
return false;
head = head->next;
}
return true;
}
/**
* Free a condition tree.
*/
Expand Down
14 changes: 6 additions & 8 deletions src/c2.h
Original file line number Diff line number Diff line change
Expand Up @@ -17,13 +17,11 @@ typedef struct _c2_lptr c2_lptr_t;
typedef struct session session_t;
typedef struct win win;

c2_lptr_t *
c2_parse(session_t *ps, c2_lptr_t **pcondlst, const char *pattern,
void *data);
c2_lptr_t *c2_parse(c2_lptr_t **pcondlst, const char *pattern, void *data);

c2_lptr_t *
c2_free_lptr(c2_lptr_t *lp);
c2_lptr_t *c2_free_lptr(c2_lptr_t *lp);

bool
c2_match(session_t *ps, win *w, const c2_lptr_t *condlst,
const c2_lptr_t **cache, void **pdata);
bool c2_match(session_t *ps, win *w, const c2_lptr_t *condlst, const c2_lptr_t **cache,
void **pdata);

bool c2_list_postprocess(session_t *ps, c2_lptr_t *list);
Loading

0 comments on commit 4dce20f

Please sign in to comment.