status qra_lexicon_bituyim (istream& in, Format format="") { DOr(testchar(in,'{')); for (Index i=0;;++i) { Bituy curdata; if (testchar(in,'}')==OK) { return OK; } in.ipfx(); DOr ( read (in, curdata ) ); if (OK!=add_bituy (curdata)) { int i=0; } } DOr(testchar(in,'}')); return OK; }
void printcharset (const byte *st) { int i; printf("["); for (i = 0; i <= UCHAR_MAX; i++) { int first = i; while (testchar(st, i) && i <= UCHAR_MAX) i++; if (i - 1 == first) /* unary range? */ printf("(%02x)", first); else if (i - 1 > first) /* non-empty range? */ printf("(%02x-%02x)", first, i - 1); } printf("]"); }
static int get_data(unsigned char *ptr,unsigned int len,unsigned int timeout) { int i=0; volatile int count=1; while(i<len) { if(testchar()>0) ptr[i++]=tgt_getchar(); else { if(!count)break; dbg_printf("count=%d\n",count); count--; } } dbg_printf("i=%d\n",i); return i; }
/* ** Opcode interpreter */ const char *match (lua_State *L, const char *o, const char *s, const char *e, Instruction *op, Capture *capture, int ptop) { Stack stackbase[INITBACK]; Stack *stacklimit = stackbase + INITBACK; Stack *stack = stackbase; /* point to first empty slot in stack */ int capsize = INITCAPSIZE; int captop = 0; /* point to first empty slot in captures */ int ndyncap = 0; /* number of dynamic captures (in Lua stack) */ const Instruction *p = op; /* current instruction */ stack->p = &giveup; stack->s = s; stack->caplevel = 0; stack++; lua_pushlightuserdata(L, stackbase); for (;;) { //#if defined(DEBUG) // printf("s: |%s| stck:%d, dyncaps:%d, caps:%d ", // s, stack - getstackbase(L, ptop), ndyncap, captop); /*printinst(op, p); printcaplist(capture, capture + captop);*/ //#endif assert(stackidx(ptop) + ndyncap == lua_gettop(L) && ndyncap <= captop); switch ((Opcode)p->i.code) { case IEnd: { assert(stack == getstackbase(L, ptop) + 1); capture[captop].kind = Cclose; capture[captop].s = NULL; return s; } case IGiveup: { assert(stack == getstackbase(L, ptop)); return NULL; } case IRet: { assert(stack > getstackbase(L, ptop) && (stack - 1)->s == NULL); p = (--stack)->p; continue; } case IAny: { if (s < e) { p++; s++; } else goto fail; continue; } case ITestAny: { if (s < e) p += 2; else p += getoffset(p); continue; } case IChar: { if ((byte)*s == p->i.aux && s < e) { p++; s++; } else goto fail; continue; } case ITestChar: { if ((byte)*s == p->i.aux && s < e) p += 2; else p += getoffset(p); continue; } case ISet: { int c = (byte)*s; if (testchar((p+1)->buff, c) && s < e) { p += CHARSETINSTSIZE; s++; } else goto fail; continue; } case ITestSet: { int c = (byte)*s; if (testchar((p + 2)->buff, c) && s < e) p += 1 + CHARSETINSTSIZE; else p += getoffset(p); continue; } case IBehind: { int n = p->i.aux; if (n > s - o) goto fail; s -= n; p++; continue; } case ISpan: { for (; s < e; s++) { int c = (byte)*s; if (!testchar((p+1)->buff, c)) break; } p += CHARSETINSTSIZE; continue; } case IJmp: { p += getoffset(p); continue; } case IChoice: { if (stack == stacklimit) stack = doublestack(L, &stacklimit, ptop); stack->p = p + getoffset(p); stack->s = s; stack->caplevel = captop; stack++; p += 2; continue; } case ICall: { if (stack == stacklimit) stack = doublestack(L, &stacklimit, ptop); stack->s = NULL; stack->p = p + 2; /* save return address */ stack++; p += getoffset(p); continue; } case ICommit: { assert(stack > getstackbase(L, ptop) && (stack - 1)->s != NULL); stack--; p += getoffset(p); continue; } case IPartialCommit: { assert(stack > getstackbase(L, ptop) && (stack - 1)->s != NULL); (stack - 1)->s = s; (stack - 1)->caplevel = captop; p += getoffset(p); continue; } case IBackCommit: { assert(stack > getstackbase(L, ptop) && (stack - 1)->s != NULL); s = (--stack)->s; captop = stack->caplevel; p += getoffset(p); continue; } case IFailTwice: assert(stack > getstackbase(L, ptop)); stack--; /* go through */ case IFail: fail: { /* pattern failed: try to backtrack */ do { /* remove pending calls */ assert(stack > getstackbase(L, ptop)); s = (--stack)->s; } while (s == NULL); if (ndyncap > 0) /* is there matchtime captures? */ ndyncap -= removedyncap(L, capture, stack->caplevel, captop); captop = stack->caplevel; p = stack->p; continue; } case ICloseRunTime: { CapState cs; int rem, res, n; int fr = lua_gettop(L) + 1; /* stack index of first result */ cs.s = o; cs.L = L; cs.ocap = capture; cs.ptop = ptop; n = runtimecap(&cs, capture + captop, s, &rem); /* call function */ captop -= n; /* remove nested captures */ fr -= rem; /* 'rem' items were popped from Lua stack */ res = resdyncaptures(L, fr, s - o, e - o); /* get result */ if (res == -1) /* fail? */ goto fail; s = o + res; /* else update current position */ n = lua_gettop(L) - fr + 1; /* number of new captures */ ndyncap += n - rem; /* update number of dynamic captures */ if (n > 0) { /* any new capture? */ if ((captop += n + 2) >= capsize) { capture = doublecap(L, capture, captop, ptop); capsize = 2 * captop; } /* add new captures to 'capture' list */ adddyncaptures(s, capture + captop - n - 2, n, fr); } p++; continue; } case ICloseCapture: { const char *s1 = s; assert(captop > 0); /* if possible, turn capture into a full capture */ if (capture[captop - 1].siz == 0 && s1 - capture[captop - 1].s < UCHAR_MAX) { capture[captop - 1].siz = s1 - capture[captop - 1].s + 1; p++; continue; } else { capture[captop].siz = 1; /* mark entry as closed */ capture[captop].s = s; goto pushcapture; } } case IOpenCapture: capture[captop].siz = 0; /* mark entry as open */ capture[captop].s = s; goto pushcapture; case IFullCapture: capture[captop].siz = getoff(p) + 1; /* save capture size */ capture[captop].s = s - getoff(p); /* goto pushcapture; */ pushcapture: { capture[captop].idx = p->i.key; capture[captop].kind = getkind(p); if (++captop >= capsize) { capture = doublecap(L, capture, captop, ptop); capsize = 2 * captop; } p++; continue; } default: assert(0); return NULL; } } }
static int xmodem_transfer(char *base) { unsigned char c; unsigned int i; unsigned int crc; unsigned int filesize=0; unsigned char BlockCount=1; //数据块累计(仅8位,无须考虑溢出) //向PC机发送开始提示信息 STATUS=ST_WAIT_START; //并且数据='d'或'D',进入XMODEM c=0; while(1) { tgt_putchar(XMODEM_WAIT_CHAR); if(testchar()>0)break; } while(STATUS!=ST_OK) //循环接收,直到全部发完 { i=get_data(&strXMODEM.SOH,BLOCKSIZE+5,1000); //限时1秒,接收133字节数据 if(i) { //分析数据包的第一个数据 SOH/EOT/CAN switch(strXMODEM.SOH) { case XMODEM_SOH: //收到开始符SOH if (i>=(BLOCKSIZE+5)) { STATUS=ST_BLOCK_OK; } else { STATUS=ST_BLOCK_FAIL; //如果数据不足,要求重发当前数据块 tgt_putchar(XMODEM_NAK); } break; case XMODEM_EOT: //收到结束符EOT tgt_putchar(XMODEM_ACK); //通知PC机全部收到 STATUS=ST_OK; printf("transfer succeed!\n"); break; case XMODEM_CAN: //收到取消符CAN tgt_putchar(XMODEM_ACK); //回应PC机 STATUS=ST_OK; printf("Warning:use cancelled!\n"); break; default: //起始字节错误 tgt_putchar(XMODEM_NAK); //要求重发当前数据块 STATUS=ST_BLOCK_FAIL; break; } } else { dbg_printf("time out!\n"); break; } if (STATUS==ST_BLOCK_OK) //接收133字节OK,且起始字节正确 { dbg_printf("BlockCount=%d,strXMODEM.BlockNo=%d\n",BlockCount,strXMODEM.BlockNo); if (BlockCount != strXMODEM.BlockNo)//核对数据块编号正确 { tgt_putchar(XMODEM_NAK); //数据块编号错误,要求重发当前数据块 continue; } if (BlockCount !=(unsigned char)(~strXMODEM.nBlockNo)) { tgt_putchar(XMODEM_NAK); //数据块编号反码错误,要求重发当前数据块 continue; } crc=strXMODEM.CRC16hi<<8; crc+=strXMODEM.CRC16lo; //AVR的16位整数是低位在先,XMODEM的CRC16是高位在先 if(calcrc(&strXMODEM.Xdata[0],BLOCKSIZE)!=crc) { tgt_putchar(XMODEM_NAK); //CRC错误,要求重发当前数据块 dbg_printf("crc error\n"); continue; } //正确接收128个字节数据,刚好是M16的一页 memcpy(base+filesize,strXMODEM.Xdata,128); filesize+=128; tgt_putchar(XMODEM_ACK); //回应已正确收到一个数据块 BlockCount++; //数据块累计加1 } } //退出Bootloader程序,从0x0000处执行应用程序 printf("xmodem finished\n"); return filesize; }