Convert printf_* in c2.c

Signed-off-by: Yuxuan Shui <yshuiv7@gmail.com>
This commit is contained in:
Yuxuan Shui 2018-12-20 17:16:20 +00:00
parent 832601354a
commit 9b5db1f0aa
No known key found for this signature in database
GPG Key ID: 37C999F617EA1A47
1 changed files with 47 additions and 59 deletions

106
src/c2.c
View File

@ -338,18 +338,8 @@ c2h_dump_str_tgt(const c2_l_t *pleaf);
static const char * static const char *
c2h_dump_str_type(const c2_l_t *pleaf); c2h_dump_str_type(const c2_l_t *pleaf);
static void static void attr_unused
c2_dump_raw(c2_ptr_t p); c2_dump(c2_ptr_t p);
/**
* Wrapper of c2_dump_raw().
*/
static inline void attr_unused
c2_dump(c2_ptr_t p) {
c2_dump_raw(p);
printf("\n");
fflush(stdout);
}
static Atom static Atom
c2_get_atom_type(const c2_l_t *pleaf); c2_get_atom_type(const c2_l_t *pleaf);
@ -393,7 +383,7 @@ c2_parse(session_t *ps, c2_lptr_t **pcondlst, const char *pattern,
} }
#ifdef DEBUG_C2 #ifdef DEBUG_C2
printf_dbgf("(\"%s\"): ", pattern); log_trace("(\"%s\"): ", pattern);
c2_dump(plptr->ptr); c2_dump(plptr->ptr);
#endif #endif
@ -401,13 +391,12 @@ c2_parse(session_t *ps, c2_lptr_t **pcondlst, const char *pattern,
} }
} }
#undef c2_error
#define c2_error(format, ...) do { \ #define c2_error(format, ...) do { \
printf_err("Pattern \"%s\" pos %d: " format, pattern, offset, \ log_error("Pattern \"%s\" pos %d: " format, pattern, offset, ##__VA_ARGS__); \
## __VA_ARGS__); \ goto fail; \
return -1; \
} while(0) } while(0)
// TODO Not a very good macro
#define C2H_SKIP_SPACES() { while (isspace(pattern[offset])) ++offset; } #define C2H_SKIP_SPACES() { while (isspace(pattern[offset])) ++offset; }
/** /**
@ -427,13 +416,6 @@ c2_parse_grp(session_t *ps, const char *pattern, int offset, c2_ptr_t *presult,
// Expected end character // Expected end character
const char endchar = (offset ? ')': '\0'); const char endchar = (offset ? ')': '\0');
#undef c2_error
#define c2_error(format, ...) do { \
printf_err("Pattern \"%s\" pos %d: " format, pattern, offset, \
## __VA_ARGS__); \
goto c2_parse_grp_fail; \
} while(0)
// We use a system that a maximum of 2 elements are kept. When we find // We use a system that a maximum of 2 elements are kept. When we find
// the third element, we combine the elements according to operator // the third element, we combine the elements according to operator
// precedence. This design limits operators to have at most two-levels // precedence. This design limits operators to have at most two-levels
@ -524,23 +506,23 @@ c2_parse_grp(session_t *ps, const char *pattern, int offset, c2_ptr_t *presult,
// It's a subgroup if it starts with '(' // It's a subgroup if it starts with '('
if ('(' == pattern[offset]) { if ('(' == pattern[offset]) {
if ((offset = c2_parse_grp(ps, pattern, offset + 1, pele, level + 1)) < 0) if ((offset = c2_parse_grp(ps, pattern, offset + 1, pele, level + 1)) < 0)
goto c2_parse_grp_fail; goto fail;
} }
// Otherwise it's a leaf // Otherwise it's a leaf
else { else {
if ((offset = c2_parse_target(ps, pattern, offset, pele)) < 0) if ((offset = c2_parse_target(ps, pattern, offset, pele)) < 0)
goto c2_parse_grp_fail; goto fail;
assert(!pele->isbranch && !c2_ptr_isempty(*pele)); assert(!pele->isbranch && !c2_ptr_isempty(*pele));
if ((offset = c2_parse_op(pattern, offset, pele)) < 0) if ((offset = c2_parse_op(pattern, offset, pele)) < 0)
goto c2_parse_grp_fail; goto fail;
if ((offset = c2_parse_pattern(ps, pattern, offset, pele)) < 0) if ((offset = c2_parse_pattern(ps, pattern, offset, pele)) < 0)
goto c2_parse_grp_fail; goto fail;
if (!c2_l_postprocess(ps, pele->l)) if (!c2_l_postprocess(ps, pele->l))
goto c2_parse_grp_fail; goto fail;
} }
// Decrement offset -- we will increment it in loop update // Decrement offset -- we will increment it in loop update
--offset; --offset;
@ -586,20 +568,13 @@ c2_parse_grp(session_t *ps, const char *pattern, int offset, c2_ptr_t *presult,
return offset; return offset;
c2_parse_grp_fail: fail:
c2_freep(&eles[0]); c2_freep(&eles[0]);
c2_freep(&eles[1]); c2_freep(&eles[1]);
return -1; return -1;
} }
#undef c2_error
#define c2_error(format, ...) do { \
printf_err("Pattern \"%s\" pos %d: " format, pattern, offset, \
## __VA_ARGS__); \
return -1; \
} while(0)
/** /**
* Parse the target part of a rule. * Parse the target part of a rule.
*/ */
@ -734,11 +709,11 @@ c2_parse_target(session_t *ps, const char *pattern, int offset, c2_ptr_t *presul
if (type) { if (type) {
if (pleaf->predef) { if (pleaf->predef) {
printf_errf("(): Warning: Type specified for a default target will be ignored."); log_warn("Type specified for a default target will be ignored.");
} }
else { else {
if (pleaf->type && type != pleaf->type) if (pleaf->type && type != pleaf->type)
printf_errf("(): Warning: Default type overridden on target."); log_warn("Default type overridden on target.");
pleaf->type = type; pleaf->type = type;
} }
} }
@ -764,13 +739,12 @@ c2_parse_target(session_t *ps, const char *pattern, int offset, c2_ptr_t *presul
// Write format // Write format
if (hasformat) { if (hasformat) {
if (pleaf->predef) if (pleaf->predef)
printf_errf("(): Warning: Format \"%d\" specified on a default target will be ignored.", format); log_warn("Format \"%d\" specified on a default target will be ignored.", format);
else if (C2_L_TSTRING == pleaf->type) else if (C2_L_TSTRING == pleaf->type)
printf_errf("(): Warning: Format \"%d\" specified on a string target will be ignored.", format); log_warn("Format \"%d\" specified on a string target will be ignored.", format);
else { else {
if (pleaf->format && pleaf->format != format) if (pleaf->format && pleaf->format != format)
printf_err("Warning: Default format %d overridden on target.", log_warn("Default format %d overridden on target.", pleaf->format);
pleaf->format);
pleaf->format = format; pleaf->format = format;
} }
} }
@ -787,6 +761,9 @@ c2_parse_target(session_t *ps, const char *pattern, int offset, c2_ptr_t *presul
c2_error("Invalid format."); c2_error("Invalid format.");
return offset; return offset;
fail:
return -1;
} }
/** /**
@ -852,6 +829,9 @@ c2_parse_op(const char *pattern, int offset, c2_ptr_t *presult) {
c2_error("Exists/greater-than/less-than operators cannot have a qualifier."); c2_error("Exists/greater-than/less-than operators cannot have a qualifier.");
return offset; return offset;
fail:
return -1;
} }
/** /**
@ -988,6 +968,9 @@ c2_parse_pattern(session_t *ps, const char *pattern, int offset, c2_ptr_t *presu
c2_error("String pattern cannot have an arithmetic operator."); c2_error("String pattern cannot have an arithmetic operator.");
return offset; return offset;
fail:
return -1;
} }
/** /**
@ -1054,12 +1037,12 @@ c2_parse_legacy(session_t *ps, const char *pattern, int offset, c2_ptr_t *presul
return -1; return -1;
return offset; return offset;
fail:
return -1;
} }
#undef c2_error #undef c2_error
#define c2_error(format, ...) { \
printf_err(format, ## __VA_ARGS__); \
return false; }
/** /**
* Do postprocessing on a condition leaf. * Do postprocessing on a condition leaf.
@ -1075,8 +1058,10 @@ c2_l_postprocess(session_t *ps, c2_l_t *pleaf) {
// Get target atom if it's not a predefined one // Get target atom if it's not a predefined one
if (!pleaf->predef) { if (!pleaf->predef) {
pleaf->tgtatom = get_atom(ps, pleaf->tgt); pleaf->tgtatom = get_atom(ps, pleaf->tgt);
if (!pleaf->tgtatom) if (!pleaf->tgtatom) {
c2_error("Failed to get atom for target \"%s\".", pleaf->tgt); log_error("Failed to get atom for target \"%s\".", pleaf->tgt);
return false;
}
} }
// Insert target Atom into atom track list // Insert target Atom into atom track list
@ -1115,7 +1100,7 @@ c2_l_postprocess(session_t *ps, c2_l_t *pleaf) {
if (!pleaf->predef) { if (!pleaf->predef) {
for (const char *pc = pleaf->tgt; *pc; ++pc) { for (const char *pc = pleaf->tgt; *pc; ++pc) {
if (islower(*pc)) { if (islower(*pc)) {
printf_errf("(): Warning: Lowercase character in target name \"%s\".", pleaf->tgt); log_warn("Lowercase character in target name \"%s\".", pleaf->tgt);
break; break;
} }
} }
@ -1135,9 +1120,11 @@ c2_l_postprocess(session_t *ps, c2_l_t *pleaf) {
// Compile PCRE expression // Compile PCRE expression
pleaf->regex_pcre = pcre_compile(pleaf->ptnstr, options, pleaf->regex_pcre = pcre_compile(pleaf->ptnstr, options,
&error, &erroffset, NULL); &error, &erroffset, NULL);
if (!pleaf->regex_pcre) if (!pleaf->regex_pcre) {
c2_error("Pattern \"%s\": PCRE regular expression parsing failed on " log_error("Pattern \"%s\": PCRE regular expression parsing failed on "
"offset %d: %s", pleaf->ptnstr, erroffset, error); "offset %d: %s", pleaf->ptnstr, erroffset, error);
return false;
}
#ifdef CONFIG_REGEX_PCRE_JIT #ifdef CONFIG_REGEX_PCRE_JIT
pleaf->regex_pcre_extra = pcre_study(pleaf->regex_pcre, pleaf->regex_pcre_extra = pcre_study(pleaf->regex_pcre,
PCRE_STUDY_JIT_COMPILE, &error); PCRE_STUDY_JIT_COMPILE, &error);
@ -1151,7 +1138,8 @@ c2_l_postprocess(session_t *ps, c2_l_t *pleaf) {
// free(pleaf->tgt); // free(pleaf->tgt);
// pleaf->tgt = NULL; // pleaf->tgt = NULL;
#else #else
c2_error("PCRE regular expression support not compiled in."); log_error("PCRE regular expression support not compiled in.");
return false;
#endif #endif
} }
@ -1237,7 +1225,7 @@ c2h_dump_str_type(const c2_l_t *pleaf) {
* Dump a condition tree. * Dump a condition tree.
*/ */
static void static void
c2_dump_raw(c2_ptr_t p) { c2_dump(c2_ptr_t p) {
// For a branch // For a branch
if (p.isbranch) { if (p.isbranch) {
const c2_b_t * const pbranch = p.b; const c2_b_t * const pbranch = p.b;
@ -1249,7 +1237,7 @@ c2_dump_raw(c2_ptr_t p) {
putchar('!'); putchar('!');
printf("("); printf("(");
c2_dump_raw(pbranch->opr1); c2_dump(pbranch->opr1);
switch (pbranch->op) { switch (pbranch->op) {
case C2_B_OAND: printf(" && "); break; case C2_B_OAND: printf(" && "); break;
@ -1258,7 +1246,7 @@ c2_dump_raw(c2_ptr_t p) {
default: assert(0); break; default: assert(0); break;
} }
c2_dump_raw(pbranch->opr2); c2_dump(pbranch->opr2);
printf(")"); printf(")");
} }
// For a leaf // For a leaf
@ -1592,7 +1580,7 @@ c2_match_once(session_t *ps, win *w, const c2_ptr_t cond) {
} }
#ifdef DEBUG_WINMATCH #ifdef DEBUG_WINMATCH
printf_dbgf("(%#010lx): branch: result = %d, pattern = ", w->id, result); log_trace("(%#010lx): branch: result = %d, pattern = ", w->id, result);
c2_dump(cond); c2_dump(cond);
#endif #endif
} }
@ -1612,9 +1600,9 @@ c2_match_once(session_t *ps, win *w, const c2_ptr_t cond) {
} }
#ifdef DEBUG_WINMATCH #ifdef DEBUG_WINMATCH
printf_dbgf("(%#010lx): leaf: result = %d, error = %d, " log_trace("(%#010lx): leaf: result = %d, error = %d, "
"client = %#010lx, pattern = ", "client = %#010lx, pattern = ",
w->id, result, error, w->client_win); w->id, result, error, w->client_win);
c2_dump(cond); c2_dump(cond);
#endif #endif
} }