int main(int argc, char *argv[]) { int c; size_t i; Buffer *buf; errno = 0; setpname(argv[0]); if (argc < 2) die(2, "usage: %s file", getpname()); if ((c = open(argv[1], O_RDONLY)) == -1) die(1, "failed to open file %s:", argv[1]); if ((buf = makebuf(512)) == NULL) die(1, "allocating buffer failed:"); if (initbuf(buf, c, O_RDONLY) == EOF) die(1, "initializing buffer failed:"); i = 0; while ((c = bgetchar(buf)) != EOF) { if (i == 511 || i == 512 || i == 513) { if (bungetchar(buf) == EOF) { die(1, "bungetchar failed at char %lu", i); break; } if ((c = bgetchar(buf)) == EOF) break; } putc(c, stdout); i++; } if (errno != 0) die(1, "bgetchar failed at char %lu:", i); fprintf(stderr, "reached end of input file %s\n", argv[1]); fprintf(stderr, "%lu bytes read\n", i); bclose(buf); exit(0); }
char KB_Getch( void ) { /* unsigned char ch; char shifted; if (keyfifoplc == keyfifoend) return 0; ch = keyfifo[keyfifoplc]; keyfifoplc = ((keyfifoplc+2)&(KEYFIFOSIZ-1)); shifted = ((keystatus[0x2a]!=0)||(keystatus[0x36]!=0)); if (ch >= 0x47 && ch <= 0x52) shifted = numpad; return sctoasc[shifted][ch]; */ return (char)bgetchar(); }
// // OSD_HandleKey() -- Handles keyboard input when capturing input. // Returns 0 if the key was handled internally, or the scancode if it should // be passed on to the game. // int OSD_HandleKey(int sc, int press) { char ch; int i,j; symbol_t *tabc = NULL; static symbol_t *lastmatch = NULL; if (!osdinited) return sc; if (sc == osdkey) { if (press) { OSD_ShowDisplay(osdvisible ^ 1); bflushchars(); } return 0;//sc; } else if (!osdvisible) { return sc; } if (!press) { if (sc == 42 || sc == 54) // shift osdeditshift = 0; if (sc == 29 || sc == 157) // control osdeditcontrol = 0; return 0;//sc; } keytime = gettime(); if (sc != 15) lastmatch = NULL; // tab while ( (ch = bgetchar()) ) { if (ch == 1) { // control a. jump to beginning of line } else if (ch == 2) { // control b, move one character left } else if (ch == 5) { // control e, jump to end of line } else if (ch == 6) { // control f, move one character right } else if (ch == 8 || ch == 127) { // control h, backspace if (!osdeditcursor || !osdeditlen) return 0; if (!osdovertype) { if (osdeditcursor < osdeditlen) Bmemmove(osdeditbuf+osdeditcursor-1, osdeditbuf+osdeditcursor, osdeditlen-osdeditcursor); osdeditlen--; } osdeditcursor--; if (osdeditcursor<osdeditwinstart) osdeditwinstart--,osdeditwinend--; } else if (ch == 9) { // tab if (!lastmatch) { for (i=osdeditcursor;i>0;i--) if (osdeditbuf[i-1] == ' ') break; for (j=0;osdeditbuf[i] != ' ' && i < osdeditlen;j++,i++) osdedittmp[j] = osdeditbuf[i]; osdedittmp[j] = 0; if (j > 0) tabc = findsymbol(osdedittmp, NULL); } else { tabc = findsymbol(osdedittmp, lastmatch->next); if (!tabc && lastmatch) tabc = findsymbol(osdedittmp, NULL); // wrap } if (tabc) { for (i=osdeditcursor;i>0;i--) if (osdeditbuf[i-1] == ' ') break; osdeditlen = i; for (j=0;tabc->name[j] && osdeditlen <= EDITLENGTH;i++,j++,osdeditlen++) osdeditbuf[i] = tabc->name[j]; osdeditcursor = osdeditlen; osdeditwinend = osdeditcursor; osdeditwinstart = osdeditwinend-editlinewidth; if (osdeditwinstart<0) { osdeditwinstart=0; osdeditwinend = editlinewidth; } lastmatch = tabc; } } else if (ch == 11) { // control k, delete all to end of line } else if (ch == 12) { // control l, clear screen } else if (ch == 13) { // control m, enter if (osdeditlen>0) { osdeditbuf[osdeditlen] = 0; Bmemmove(osdhistorybuf[1], osdhistorybuf[0], (HISTORYDEPTH-1)*(EDITLENGTH+1)); Bmemmove(osdhistorybuf[0], osdeditbuf, EDITLENGTH+1); if (osdhistorysize < HISTORYDEPTH) osdhistorysize++; if (osdexeccount == HISTORYDEPTH) OSD_Printf("Command Buffer Warning: Failed queueing command " "for execution. Buffer full.\n"); else osdexeccount++; osdhistorypos=-1; } osdeditlen=0; osdeditcursor=0; osdeditwinstart=0; osdeditwinend=editlinewidth; } else if (ch == 16) { // control p, previous (ie. up arrow) } else if (ch == 20) { // control t, swap previous two chars } else if (ch == 21) { // control u, delete all to beginning if (osdeditcursor>0 && osdeditlen) { if (osdeditcursor<osdeditlen) Bmemmove(osdeditbuf, osdeditbuf+osdeditcursor, osdeditlen-osdeditcursor); osdeditlen-=osdeditcursor; osdeditcursor = 0; osdeditwinstart = 0; osdeditwinend = editlinewidth; } } else if (ch == 23) { // control w, delete one word back if (osdeditcursor>0 && osdeditlen>0) { i=osdeditcursor; while (i>0 && osdeditbuf[i-1]==32) i--; while (i>0 && osdeditbuf[i-1]!=32) i--; if (osdeditcursor<osdeditlen) Bmemmove(osdeditbuf+i, osdeditbuf+osdeditcursor, osdeditlen-osdeditcursor); osdeditlen -= (osdeditcursor-i); osdeditcursor = i; if (osdeditcursor < osdeditwinstart) { osdeditwinstart=osdeditcursor; osdeditwinend=osdeditwinstart+editlinewidth; } } } else if (ch >= 32) { // text char if (!osdovertype && osdeditlen == EDITLENGTH) // buffer full, can't insert another char return 0; if (!osdovertype) { if (osdeditcursor < osdeditlen) Bmemmove(osdeditbuf+osdeditcursor+1, osdeditbuf+osdeditcursor, osdeditlen-osdeditcursor); osdeditlen++; } else { if (osdeditcursor == osdeditlen) osdeditlen++; } osdeditbuf[osdeditcursor] = ch; osdeditcursor++; if (osdeditcursor>osdeditwinend) osdeditwinstart++,osdeditwinend++; } } if (sc == 15) { // tab } else if (sc == 1) { // escape OSD_ShowDisplay(0); } else if (sc == 201) { // page up if (osdhead < osdlines-1) osdhead++; } else if (sc == 209) { // page down if (osdhead > 0) osdhead--; } else if (sc == 199) { // home if (osdeditcontrol) { osdhead = osdlines-1; } else { osdeditcursor = 0; osdeditwinstart = osdeditcursor; osdeditwinend = osdeditwinstart+editlinewidth; } } else if (sc == 207) { // end if (osdeditcontrol) { osdhead = 0; } else { osdeditcursor = osdeditlen; osdeditwinend = osdeditcursor; osdeditwinstart = osdeditwinend-editlinewidth; if (osdeditwinstart<0) { osdeditwinstart=0; osdeditwinend = editlinewidth; } } } else if (sc == 210) { // insert osdovertype ^= 1; } else if (sc == 203) { // left if (osdeditcursor>0) { if (osdeditcontrol) { while (osdeditcursor>0) { if (osdeditbuf[osdeditcursor-1] != 32) break; osdeditcursor--; } while (osdeditcursor>0) { if (osdeditbuf[osdeditcursor-1] == 32) break; osdeditcursor--; } } else osdeditcursor--; } if (osdeditcursor<osdeditwinstart) osdeditwinend-=(osdeditwinstart-osdeditcursor), osdeditwinstart-=(osdeditwinstart-osdeditcursor); } else if (sc == 205) { // right if (osdeditcursor<osdeditlen) { if (osdeditcontrol) { while (osdeditcursor<osdeditlen) { if (osdeditbuf[osdeditcursor] == 32) break; osdeditcursor++; } while (osdeditcursor<osdeditlen) { if (osdeditbuf[osdeditcursor] != 32) break; osdeditcursor++; } } else osdeditcursor++; } if (osdeditcursor>=osdeditwinend) osdeditwinstart+=(osdeditcursor-osdeditwinend), osdeditwinend+=(osdeditcursor-osdeditwinend); } else if (sc == 200) { // up if (osdhistorypos < osdhistorysize-1) { osdhistorypos++; memcpy(osdeditbuf, osdhistorybuf[osdhistorypos], EDITLENGTH+1); osdeditlen = osdeditcursor = 0; while (osdeditbuf[osdeditcursor]) osdeditlen++, osdeditcursor++; if (osdeditcursor<osdeditwinstart) { osdeditwinend = osdeditcursor; osdeditwinstart = osdeditwinend-editlinewidth; if (osdeditwinstart<0) osdeditwinend-=osdeditwinstart, osdeditwinstart=0; } else if (osdeditcursor>=osdeditwinend) osdeditwinstart+=(osdeditcursor-osdeditwinend), osdeditwinend+=(osdeditcursor-osdeditwinend); } } else if (sc == 208) { // down if (osdhistorypos >= 0) { if (osdhistorypos == 0) { osdeditlen=0; osdeditcursor=0; osdeditwinstart=0; osdeditwinend=editlinewidth; osdhistorypos = -1; } else { osdhistorypos--; memcpy(osdeditbuf, osdhistorybuf[osdhistorypos], EDITLENGTH+1); osdeditlen = osdeditcursor = 0; while (osdeditbuf[osdeditcursor]) osdeditlen++, osdeditcursor++; if (osdeditcursor<osdeditwinstart) { osdeditwinend = osdeditcursor; osdeditwinstart = osdeditwinend-editlinewidth; if (osdeditwinstart<0) osdeditwinend-=osdeditwinstart, osdeditwinstart=0; } else if (osdeditcursor>=osdeditwinend) osdeditwinstart+=(osdeditcursor-osdeditwinend), osdeditwinend+=(osdeditcursor-osdeditwinend); } } } else if (sc == 42 || sc == 54) { // shift osdeditshift = 1; } else if (sc == 29 || sc == 157) { // control osdeditcontrol = 1; } else if (sc == 58) { // capslock osdeditcaps ^= 1; } else if (sc == 28 || sc == 156) { // enter } else if (sc == 14) { // backspace } else if (sc == 211) { // delete if (osdeditcursor == osdeditlen || !osdeditlen) return 0; if (osdeditcursor <= osdeditlen-1) Bmemmove(osdeditbuf+osdeditcursor, osdeditbuf+osdeditcursor+1, osdeditlen-osdeditcursor-1); osdeditlen--; } return 0; }