=== modified file 'termkey.c' --- termkey.c 2008-02-10 19:15:29 +0000 +++ termkey.c 2008-02-10 19:03:36 +0000 @@ -16,9 +16,8 @@ int fd; int flags; unsigned char *buffer; - size_t buffstart; // First offset in buffer - size_t buffcount; // NUMBER of entires valid in buffer - size_t buffsize; // Total malloc'ed size + size_t buffvalid; + size_t buffsize; char is_closed; @@ -71,8 +70,7 @@ return NULL; } - tk->buffstart = 0; - tk->buffcount = 0; + tk->buffvalid = 0; tk->buffsize = buffsize; tk->is_closed = 0; @@ -173,20 +171,8 @@ static inline void eatbytes(termkey_t *tk, size_t count) { - tk->buffstart += count; - tk->buffcount -= count; - - if(tk->buffcount <= 0) { - tk->buffstart = 0; - tk->buffcount = 0; - } - - size_t halfsize = tk->buffsize / 2; - - if(tk->buffstart > halfsize) { - memcpy(tk->buffer, tk->buffer + halfsize, halfsize); - tk->buffstart -= halfsize; - } + memmove(tk->buffer, tk->buffer + count, tk->buffvalid - count); + tk->buffvalid -= count; } #define UTF8_INVALID 0xFFFD @@ -282,22 +268,20 @@ fill_utf8(key); } -#define CHARAT(i) (tk->buffer[tk->buffstart + (i)]) - static termkey_result getkey_csi(termkey_t *tk, size_t introlen, termkey_key *key) { size_t csi_end = introlen; - while(csi_end < tk->buffcount) { - if(CHARAT(csi_end) >= 0x40 && CHARAT(csi_end) < 0x80) + while(csi_end < tk->buffvalid) { + if(tk->buffer[csi_end] >= 0x40 && tk->buffer[csi_end] < 0x80) break; csi_end++; } - if(csi_end >= tk->buffcount) + if(csi_end >= tk->buffvalid) return TERMKEY_RES_NONE; - unsigned char cmd = CHARAT(csi_end); + unsigned char cmd = tk->buffer[csi_end]; int arg[16]; char present = 0; int args = 0; @@ -306,7 +290,7 @@ // Now attempt to parse out up number;number;... separated values while(p < csi_end) { - unsigned char c = CHARAT(p); + unsigned char c = tk->buffer[p]; if(c >= '0' && c <= '9') { if(!present) { @@ -372,10 +356,10 @@ static termkey_result getkey_ss3(termkey_t *tk, size_t introlen, termkey_key *key) { - if(introlen + 1 < tk->buffcount) + if(introlen + 1 < tk->buffvalid) return TERMKEY_RES_NONE; - unsigned char cmd = CHARAT(introlen); + unsigned char cmd = tk->buffer[introlen]; eatbytes(tk, introlen + 1); @@ -411,34 +395,34 @@ termkey_result termkey_getkey(termkey_t *tk, termkey_key *key) { - if(tk->buffcount == 0) + if(tk->buffvalid == 0) return tk->is_closed ? TERMKEY_RES_EOF : TERMKEY_RES_NONE; // Now we're sure at least 1 byte is valid - unsigned char b0 = CHARAT(0); + unsigned char b0 = tk->buffer[0]; if(b0 == 0x1b) { - if(tk->buffcount == 1) { + if(tk->buffvalid == 1) { do_codepoint(tk, b0, key); eatbytes(tk, 1); return TERMKEY_RES_KEY; } - unsigned char b1 = CHARAT(1); + unsigned char b1 = tk->buffer[1]; if(b1 == '[') return getkey_csi(tk, 2, key); if(b1 == 'O') return getkey_ss3(tk, 2, key); - tk->buffstart++; + tk->buffer++; termkey_result metakey_result = termkey_getkey(tk, key); switch(metakey_result) { case TERMKEY_RES_KEY: key->modifiers |= TERMKEY_KEYMOD_ALT; - tk->buffstart--; + tk->buffer--; eatbytes(tk, 1); break; @@ -501,11 +485,11 @@ return TERMKEY_RES_KEY; } - if(tk->buffcount < nbytes) + if(tk->buffvalid < nbytes) return TERMKEY_RES_NONE; for(int b = 1; b < nbytes; b++) { - unsigned char cb = CHARAT(b); + unsigned char cb = tk->buffer[b]; if(cb < 0x80 || cb >= 0xc0) { do_codepoint(tk, UTF8_INVALID, key); eatbytes(tk, b - 1); @@ -559,14 +543,14 @@ void termkey_pushinput(termkey_t *tk, unsigned char *input, size_t inputlen) { - if(tk->buffstart + tk->buffcount + inputlen > tk->buffsize) { + if(tk->buffvalid + inputlen > tk->buffsize) { fprintf(stderr, "TODO! Extend input buffer!\n"); exit(0); } // Not strcpy just in case of NUL bytes - memcpy(tk->buffer + tk->buffstart + tk->buffcount, input, inputlen); - tk->buffcount += inputlen; + memcpy(tk->buffer + tk->buffvalid, input, inputlen); + tk->buffvalid += inputlen; } void termkey_advisereadable(termkey_t *tk)