static const response* add(const char* l) { struct rule* r; if (*l != 'k' && *l != 'd' && *l != 'z' && *l != 'p' && *l != 'n') return 0; r = alloc_rule(); r->code = *l++; if ((l = parse_pattern(l, ':', &r->sender)) != 0 && *l == ':') if ((l = parse_pattern(l+1, ':', &r->recipient)) != 0 && *l == ':') if ((l = parse_str(l+1, ':', &r->response)) != 0 && *l == ':') if ((l = parse_uint(l+1, ':', &r->databytes)) != 0) if (*l == ':' && (l = parse_str(l+1, ':', &r->relayclient)) != 0 && *l == ':') parse_env(l+1, &r->environment); if (l == 0) return &resp_syntax; append_rule(r); /* Pre-load text files and pre-open CDB files */ if (!try_load(&r->sender)) return &resp_erropenref; if (!try_load(&r->recipient)) return &resp_erropenref; return 0; }
Key2KanaTable::Key2KanaTable (std::string name, ConvRule *table) : m_name (name) { for (unsigned int i = 0; table[i].string; i++) { append_rule (table[i].string ? table[i].string : "", table[i].result ? table[i].result : "", table[i].cont ? table[i].cont : ""); } }
Key2KanaTable::Key2KanaTable (std::string name, NicolaRule *table) : m_name (name) { for (unsigned int i = 0; table[i].key; i++) { append_rule (table[i].key ? table[i].key : "", table[i].single ? table[i].single : "", table[i].left_shift ? table[i].left_shift : "", table[i].right_shift ? table[i].right_shift : ""); } }
/* * This function build the rules: * * -A ntk_mark_chain -o ntk_tunl<m> * -j CONNMARK --set-mark m * * If: * * s= n-number_of_rules_present * then: * if s>0, will be created s rules, * else: * nothing. * * Returns: * 0 * -1 */ int create_mark_rules(int n) { int nchain; int res, i; char rule[MARK_RULE_SZ]; iptc_handle_t t; res = table_init(MANGLE_TABLE, &t); if (res) { error(err_str); err_ret(ERR_NETRUL, -1); } nchain = count_ntk_mark_chain(&t); if (nchain == -1) { error("In create_mark_rules: can not read ntk_mark_chain."); err_ret(ERR_NETRUL, -1); } if (nchain >= n) { debug(DBG_NORMAL, "In create_mark_rules: rules present yet."); return 0; } for (i = nchain; i < n; i++) { mark_rule_init(rule, NTK_TUNL_PREFIX, i); res = append_rule(rule, &t, NTK_MARK_CHAIN); if (res) { error(err_str); err_ret(ERR_NETRUL, -1); } } res = commit_rules(&t); if (res) { error(err_str); err_ret(ERR_NETRUL, -1); } debug(DBG_NORMAL, "Created %d marking rules.", n - nchain); return 0; }
int rewrite_rule_compile( struct rewrite_info *info, struct rewrite_context *context, const char *pattern, const char *result, const char *flagstring ) { int flags = REWRITE_REGEX_EXTENDED | REWRITE_REGEX_ICASE; int mode = REWRITE_RECURSE; int max_passes = info->li_max_passes_per_rule; struct rewrite_rule *rule = NULL; struct rewrite_subst *subst = NULL; struct rewrite_action *action = NULL, *first_action = NULL; const char *p; assert( info != NULL ); assert( context != NULL ); assert( pattern != NULL ); assert( result != NULL ); /* * A null flagstring should be allowed */ /* * Take care of substitution string */ subst = rewrite_subst_compile( info, result ); if ( subst == NULL ) { return REWRITE_ERR; } /* * Take care of flags */ for ( p = flagstring; p[ 0 ] != '\0'; p++ ) { switch( p[ 0 ] ) { /* * REGEX flags */ case REWRITE_FLAG_HONORCASE: /* 'C' */ /* * Honor case (default is case insensitive) */ flags &= ~REWRITE_REGEX_ICASE; break; case REWRITE_FLAG_BASICREGEX: /* 'R' */ /* * Use POSIX Basic Regular Expression syntax * instead of POSIX Extended Regular Expression * syntax (default) */ flags &= ~REWRITE_REGEX_EXTENDED; break; /* * Execution mode flags */ case REWRITE_FLAG_EXECONCE: /* ':' */ /* * Apply rule once only */ mode &= ~REWRITE_RECURSE; mode |= REWRITE_EXEC_ONCE; break; /* * Special action flags */ case REWRITE_FLAG_STOP: /* '@' */ /* * Bail out after applying rule */ action = calloc( sizeof( struct rewrite_action ), 1 ); if ( action == NULL ) { goto fail; } action->la_type = REWRITE_ACTION_STOP; break; case REWRITE_FLAG_UNWILLING: /* '#' */ /* * Matching objs will be marked as gone! */ action = calloc( sizeof( struct rewrite_action ), 1 ); if ( action == NULL ) { goto fail; } mode &= ~REWRITE_RECURSE; mode |= REWRITE_EXEC_ONCE; action->la_type = REWRITE_ACTION_UNWILLING; break; case REWRITE_FLAG_GOTO: /* 'G' */ /* * After applying rule, jump N rules */ case REWRITE_FLAG_USER: { /* 'U' */ /* * After applying rule, return user-defined * error code */ char *next = NULL; int *d; if ( p[ 1 ] != '{' ) { goto fail; } d = malloc( sizeof( int ) ); if ( d == NULL ) { goto fail; } d[ 0 ] = strtol( &p[ 2 ], &next, 0 ); if ( next == &p[ 2 ] || next[0] != '}' ) { free( d ); goto fail; } action = calloc( sizeof( struct rewrite_action ), 1 ); if ( action == NULL ) { free( d ); goto fail; } switch ( p[ 0 ] ) { case REWRITE_FLAG_GOTO: action->la_type = REWRITE_ACTION_GOTO; break; case REWRITE_FLAG_USER: action->la_type = REWRITE_ACTION_USER; break; default: assert(0); } action->la_args = (void *)d; p = next; /* p is incremented by the for ... */ break; } case REWRITE_FLAG_MAX_PASSES: { /* 'U' */ /* * Set the number of max passes per rule */ char *next = NULL; if ( p[ 1 ] != '{' ) { goto fail; } max_passes = strtol( &p[ 2 ], &next, 0 ); if ( next == &p[ 2 ] || next[0] != '}' ) { goto fail; } if ( max_passes < 1 ) { /* FIXME: nonsense ... */ max_passes = 1; } p = next; /* p is incremented by the for ... */ break; } case REWRITE_FLAG_IGNORE_ERR: /* 'I' */ /* * Ignore errors! */ action = calloc( sizeof( struct rewrite_action ), 1 ); if ( action == NULL ) { goto fail; } action->la_type = REWRITE_ACTION_IGNORE_ERR; break; /* * Other flags ... */ default: /* * Unimplemented feature (complain only) */ break; } /* * Stupid way to append to a list ... */ if ( action != NULL ) { append_action( &first_action, action ); action = NULL; } } /* * Finally, rule allocation */ rule = calloc( sizeof( struct rewrite_rule ), 1 ); if ( rule == NULL ) { goto fail; } /* * REGEX compilation (luckily I don't need to take care of this ...) */ if ( regcomp( &rule->lr_regex, ( char * )pattern, flags ) != 0 ) { goto fail; } /* * Just to remember them ... */ rule->lr_pattern = strdup( pattern ); rule->lr_subststring = strdup( result ); rule->lr_flagstring = strdup( flagstring ); if ( rule->lr_pattern == NULL || rule->lr_subststring == NULL || rule->lr_flagstring == NULL ) { goto fail; } /* * Load compiled data into rule */ rule->lr_subst = subst; /* * Set various parameters */ rule->lr_flags = flags; /* don't really need any longer ... */ rule->lr_mode = mode; rule->lr_max_passes = max_passes; rule->lr_action = first_action; /* * Append rule at the end of the rewrite context */ append_rule( context, rule ); return REWRITE_SUCCESS; fail: if ( rule ) { if ( rule->lr_pattern ) free( rule->lr_pattern ); if ( rule->lr_subststring ) free( rule->lr_subststring ); if ( rule->lr_flagstring ) free( rule->lr_flagstring ); free( rule ); } destroy_actions( first_action ); free( subst ); return REWRITE_ERR; }