📄 rules.c
字号:
/* Copyright (C) 2002-2003 Gerd Rausch, BlauLogic (http://blaulogic.com) All rights reserved. Redistribution and use in source and binary forms, with or without modification, are permitted provided that the following conditions are met: 1. Redistributions of source code must retain the above copyright notice, this list of conditions and the following disclaimer. 2. Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the following disclaimer in the documentation and/or other materials provided with the distribution. 3. Except as contained in this notice, neither the name of BlauLogic nor the name(s) of the author(s) may be used to endorse or promote products derived from this software without specific prior written permission. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHOR(S) OR BLAULOGIC BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.*/#include <inttypes.h>#include <string.h>#include <pgmspace.h>#include <eeprom.h>#include <io.h>#include "rules.h"#if E2END>=3static inline uint8_t digit_count(char *number){ uint8_t count; char *cp; count=0; for(cp=number; *cp && *cp!=','; cp++) { if((*cp>='0' && *cp<='9') || *cp=='*' || *cp=='#') count++; } return count;}static inline uint8_t number_matches(char *number, uint16_t prefix_addr, uint8_t prefix_len){ uint8_t i; for(i=0; number[i] && i<prefix_len; i++) { if(number[i]!=(char)eeprom_rb(prefix_addr+i)) return 0; } return i==prefix_len;}static void replace_number(char *buf, uint16_t buf_size, uint16_t repl_addr, uint8_t repl_len, char *number){ uint16_t number_len, i; number_len=strlen(number); if(number_len+repl_len>=buf_size) { buf[0]=0; return; } memmove(buf+repl_len, number, number_len+1); for(i=0; i<repl_len; i++) buf[i]=(char)eeprom_rb(repl_addr+i);}void rules_apply(char *number, uint16_t size){ uint16_t addr, next_addr; Rule rule; uint8_t state; state=0; for(addr=2; addr+sizeof(Rule)<=E2END+1 && eeprom_rw(addr)!=0xFFFF; addr=next_addr) { eeprom_read_block(&rule, addr, sizeof(Rule)); next_addr=addr+sizeof(Rule)+rule.prefix_len+rule.repl_len; if(next_addr>E2END+1) break; if(rule.digit_count!=0xFF && digit_count(number)!=rule.digit_count) continue; if((state & rule.state_filter_mask)!=rule.state_filter_value || !number_matches(number, addr+sizeof(Rule), rule.prefix_len)) continue; replace_number(number, size, addr+sizeof(Rule)+rule.prefix_len, rule.repl_len, number+rule.prefix_len); state&=rule.state_mod_mask; state^=rule.state_mod_toggle; }}void rules_parse(Rules_Parse_Context *context_p, uint8_t *data, uint16_t size){ uint16_t addr, i;#ifndef IRDIAL_SLOW_EEPROM uint16_t j;#endif i=0; while(i<size) { switch(context_p->state) { case RULES_PARSE_STATE_INITIAL: if(data[i]=='\r' || data[i]=='\n') { context_p->state=RULES_PARSE_STATE_WHITESPACE; context_p->access_code=0xFFFF; context_p->eeprom_addr=2; memset(&context_p->rule, 0, sizeof(Rule)); context_p->rule.digit_count=0xFF; } i++; break; case RULES_PARSE_STATE_WHITESPACE: switch(data[i]) { case '#': context_p->state=RULES_PARSE_STATE_ACCESS_CODE; context_p->access_code=0; break; case '=': context_p->state=RULES_PARSE_STATE_DIGIT_COUNT; context_p->rule.digit_count=0; break; case '-': context_p->state=RULES_PARSE_STATE_FILTER; context_p->mask_bit_on=0; context_p->mask_bit_shift=0; break; case '+': context_p->state=RULES_PARSE_STATE_FILTER; context_p->mask_bit_on=1; context_p->mask_bit_shift=0; break; case '/': if(context_p->access_code==eeprom_rw(0)) context_p->state=RULES_PARSE_STATE_PREFIX; else context_p->state=RULES_PARSE_STATE_DONE; break; case ' ': case '\t': case '\r': case '\n': break; default: context_p->state=RULES_PARSE_STATE_DONE; } i++; break; case RULES_PARSE_STATE_ACCESS_CODE: if(data[i]>='0' && data[i]<='9') { context_p->access_code=context_p->access_code*10+data[i++]-'0'; } else { if(eeprom_rw(0)==0xFFFF) { eeprom_wb(0, context_p->access_code); eeprom_wb(1, context_p->access_code>>8); } context_p->state=RULES_PARSE_STATE_WHITESPACE; } break; case RULES_PARSE_STATE_DIGIT_COUNT: if(data[i]>='0' && data[i]<='9') { context_p->rule.digit_count=context_p->rule.digit_count*10+data[i++]-'0'; } else context_p->state=RULES_PARSE_STATE_WHITESPACE; break; case RULES_PARSE_STATE_FILTER: if(data[i]>='0' && data[i]<='9') { context_p->mask_bit_shift=context_p->mask_bit_shift*10+data[i++]-'0'; } else { context_p->rule.state_filter_mask|=1<<context_p->mask_bit_shift; context_p->rule.state_filter_value|=context_p->mask_bit_on<<context_p->mask_bit_shift; context_p->state=RULES_PARSE_STATE_WHITESPACE; } break; case RULES_PARSE_STATE_PREFIX: if(data[i]!='/') { addr=context_p->eeprom_addr+sizeof(Rule)+context_p->rule.prefix_len; if(addr<=E2END) {#ifdef IRDIAL_SLOW_EEPROM context_p->tmp_eeprom[addr]=data[i];#else eeprom_wb(addr, data[i]);#endif } context_p->rule.prefix_len++; } else context_p->state=RULES_PARSE_STATE_REPL; i++; break; case RULES_PARSE_STATE_REPL: if(data[i]!='/') { addr=context_p->eeprom_addr+sizeof(Rule)+context_p->rule.prefix_len+context_p->rule.repl_len; if(addr<=E2END) {#ifdef IRDIAL_SLOW_EEPROM context_p->tmp_eeprom[addr]=data[i];#else eeprom_wb(addr, data[i]);#endif } context_p->rule.repl_len++; } else context_p->state=RULES_PARSE_STATE_MOD_TEST; i++; break; case RULES_PARSE_STATE_MOD_TEST: switch(data[i]) { case '-': context_p->state=RULES_PARSE_STATE_MOD; context_p->mask_bit_on=0; context_p->mask_bit_shift=0; i++; break; case '+': context_p->state=RULES_PARSE_STATE_MOD; context_p->mask_bit_on=1; context_p->mask_bit_shift=0; i++; break; default: context_p->rule.state_mod_mask^=0xFF; addr=context_p->eeprom_addr+sizeof(Rule)+context_p->rule.prefix_len+context_p->rule.repl_len; if(addr<=E2END-1) {#ifdef IRDIAL_SLOW_EEPROM memcpy(context_p->tmp_eeprom+context_p->eeprom_addr, &context_p->rule, sizeof(Rule));#else for(j=0; j<sizeof(Rule); j++) eeprom_wb(context_p->eeprom_addr+j, ((uint8_t *)&context_p->rule)[j]);#endif context_p->eeprom_addr=addr; context_p->state=RULES_PARSE_STATE_WHITESPACE; memset(&context_p->rule, 0, sizeof(Rule)); context_p->rule.digit_count=0xFF; } else context_p->state=RULES_PARSE_STATE_DONE; } break; case RULES_PARSE_STATE_MOD: if(data[i]>='0' && data[i]<='9') { context_p->mask_bit_shift=context_p->mask_bit_shift*10+data[i++]-'0'; } else { context_p->rule.state_mod_mask|=1<<context_p->mask_bit_shift; context_p->rule.state_mod_toggle|=context_p->mask_bit_on<<context_p->mask_bit_shift; context_p->state=RULES_PARSE_STATE_MOD_TEST; } break; default: return; } }}void rules_parse_finish(Rules_Parse_Context *context_p, uint8_t success){#ifdef IRDIAL_SLOW_EEPROM uint16_t addr;#endif if(context_p->access_code!=eeprom_rw(0)) return; if(!success) context_p->eeprom_addr=2;#ifdef IRDIAL_SLOW_EEPROM for(addr=2; addr<context_p->eeprom_addr; addr++) eeprom_wb(addr, context_p->tmp_eeprom[addr]);#endif /* write sentinel */ eeprom_wb(context_p->eeprom_addr, 0xFF); eeprom_wb(context_p->eeprom_addr+1, 0xFF);}#else /* E2END<3 */void rules_apply(char *number, uint16_t size){}void rules_parse(Rules_Parse_Context *context_p, uint8_t *data, uint16_t size){}void rules_parse_finish(Rules_Parse_Context *context_p, uint8_t success){}#endif /* E2END<3 */
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -