int consolewrite(struct inode *ip, char *buf, int n) { int i; iunlock(ip); acquire(&cons.lock); for (i = 0; i < n; i++) consputc(buf[i] & 0xff); release(&cons.lock); ilock(ip); return n; }
void consoleintr(int (*getc)(void)) { int c; acquire(&input.lock); while((c = getc()) >= 0){ switch(c){ case C('P'): // Process listing. procdump(); break; case C('U'): // Kill line. while(input.e != input.w && input.buf[(input.e-1) % INPUT_BUF] != '\n'){ input.e--; consputc(BACKSPACE); } break; case C('H'): case '\x7f': // Backspace if(input.e != input.w){ input.e--; consputc(BACKSPACE); } break; default: if(c != 0 && input.e-input.r < INPUT_BUF){ c = (c == '\r') ? '\n' : c; input.buf[input.e++ % INPUT_BUF] = c; consputc(c); //buffer flush if(c == '\n' || c == C('D') || input.e == input.r+INPUT_BUF){ input.w = input.e; wakeup(&input.r); } } break; } } release(&input.lock); }
int consolewrite(inode *ip, char *buf, int n) { int i; ip->unlock(); for(i = 0; i < n; i++) consputc(buf[i] & 0xff); ip->lock(); return n; }
static void printint(int xx, int base, int sign) { static char digits[] = "0123456789abcdef"; char buf[16]; int i; uint x; if (sign && (sign = xx < 0)) x = -xx; else x = xx; i = 0; do { buf[i++] = digits[x % base]; } while ((x /= base) != 0); if (sign) buf[i++] = '-'; while (--i >= 0) consputc(buf[i]); }
static void printint(int xx, int base, int sgn) { static char digits[] = "0123456789abcdef"; char buf[16]; int i = 0, neg = 0; uint x; if(sgn && xx < 0){ neg = 1; x = -xx; } else x = xx; do{ buf[i++] = digits[x % base]; }while((x /= base) != 0); if(neg) buf[i++] = '-'; while(--i >= 0) consputc(buf[i]); }
void consoleintr(int (*getc)(void)) { int c; acquire(&input.lock); while((c = getc()) >= 0){ switch(c){ case C('P'): // Process listing. procdump(); break; case C('U'): // Kill line. while(input.e != input.w && input.buf[(input.e-1) % INPUT_BUF] != '\n'){ input.e--; consputc(BACKSPACE); } break; case C('H'): case '\x7f': // Backspace if(input.f != input.w){ input.e--; input.f--; consputc(BACKSPACE); } break; case KEY_LF: // Key Left { if(input.f != input.w) { input.f--; cgaputc(KEY_LF); } break; } case KEY_RT: // Key Right { int IsCanRight = input.e - input.f; if(IsCanRight > 0) { input.f++; cgaputc(KEY_RT); } break; } case KEY_UP: // Key Up { int NextHistoryPos = (HistoryPos + MAX_HISTORY_LENGTH - 1) % MAX_HISTORY_LENGTH; if(NextHistoryPos < MaxHistoryPos) { HistoryPos = NextHistoryPos; SelectFromHistory(); } break; } case KEY_DN: // Key Down { if(HistoryPos < MaxHistoryPos) { HistoryPos = (HistoryPos + 1) % MAX_HISTORY_LENGTH; SelectFromHistory(); } break; } default: if(c != 0 && input.e-input.r < INPUT_BUF) { c = (c == '\r') ? '\n' : c; if(c == '\n' || c == C('D') || input.e == input.r+INPUT_BUF) { // Insert Command To History if(input.e > input.w) // if command exist { int j; for(j = 0; input.w+j<input.e; ++j) { History[HistoryWrite][j] = input.buf[(input.w +j) % INPUT_BUF]; } History[HistoryWrite][j] = '\0'; HistoryWrite = (HistoryWrite + 1) % MAX_HISTORY_LENGTH; MaxHistoryPos = (MaxHistoryPos == MAX_HISTORY_LENGTH) ? MAX_HISTORY_LENGTH : (MaxHistoryPos + 1); HistoryPos = HistoryWrite; } input.buf[input.e++ % INPUT_BUF] = c; input.f = input.e; consputc(c); input.w = input.e; wakeup(&input.r); } else { input.buf[input.f++ % INPUT_BUF] = c; if(input.f > input.e) { input.e = input.f; } consputc(c); } } break; } // cprintf("buf %s, read %d, write %d, edit %d\n final %d\n",input.buf,input.r,input.w,input.e, input.f); } release(&input.lock); }
// Print to the console. only understands %d, %x, %p, %s. void cprintf(const char *fmt, ...) { int i, c, locking; uint *argp; char *s; locking = cons.locking; if (locking) { acquire(&cons.lock); } if (fmt == 0) { panic("null fmt"); } argp = (uint *)(void *)(&fmt + 1); for (i = 0; (c = fmt[i] & 0xff) != 0; i++) { if (c != '%') { consputc(c); continue; } c = fmt[++i] & 0xff; if (c == 0) { break; } switch (c) { case 'd': printint(*argp++, 10, 1); break; case 'x': case 'p': printint(*argp++, 16, 0); break; case 's': if ((s = (char *)*argp++) == 0) { s = "(null)"; } for (; *s; s++) { consputc(*s); } break; case '%': consputc('%'); break; default: // Print unknown % sequence to draw attention. consputc('%'); consputc(c); break; } } if (locking) { release(&cons.lock); } }
void consoleintr(int (*getc)(void)) { int c; int i; acquire(&input.lock); while((c = getc()) >= 0){ switch(c){ case C('P'): // Process listing. procdump(); break; case C('U'): // Kill line. while(input.e != input.w && input.buf[(input.e-1) % INPUT_BUF] != '\n'){ input.e--; consputc(BACKSPACE); } break; case C('H'): case '\x7f': // Backspace if(input.e != input.w){ input.e--; consputc(BACKSPACE); //added roy -> shift all the chars back when backspace used within the buffer if(left_count>0){ for( i=0 ; i<=left_count; i++ ){ input.buf[input.e+i % INPUT_BUF]= input.buf[input.e+i+1 % INPUT_BUF]; } } } break; //added roy case RIGHT: if(left_count>0){ move_cursor(1); left_count--; } break; //added roy case LEFT: if(input.e != input.w){ left_count++; move_cursor(-1); } break; //changed roy-> updated buffer allows to insert/remove chars within the buffer, (memory) default: if(c != 0 && input.e-input.r < INPUT_BUF){ c = (c == '\r') ? '\n' : c; consputc(c); if(c == '\n' || c == C('D') || input.e == input.r+INPUT_BUF){ input.e= input.e+left_count; input.buf[input.e++ % INPUT_BUF] = c; input.w = input.e; left_count=0; wakeup(&input.r); }else{ if(left_count>0){ for( i=0 ; i<=left_count; i++ ){ input.buf[input.e+(left_count-i)+1 % INPUT_BUF]= input.buf[input.e+(left_count-i) % INPUT_BUF]; } } input.buf[input.e++ % INPUT_BUF] = c; } } break; } } release(&input.lock); }
void consoleintr(int (*getc)(void)) { int c, doprocdump = 0; // int historyInd = 1; //when arr not full we should put here the last command written, most cases, when full davka should be 15 acquire(&cons.lock); int tmp; while ((c = getc()) >= 0) { switch (c) { case C('P'): // Process listing. doprocdump = 1; // procdump() locks cons.lock indirectly; invoke later break; case C('U'): // Kill line. kill_line(); break; case C('H'): case '\x7f': // Backspace if (input.e != input.w) { //regular caret input.e--; input.f--; if (input.e < input.f) { // middle caret midflag = input.f - input.e; int i = input.e; while (i < input.f) { input.buf[i % INPUT_BUF] = input.buf[(i + 1) % INPUT_BUF]; i++; } } consputc(BACKSPACE); midflag = 0; } break; case KEY_LEFT: // Left arrow if (input.e != input.w) { input.e--; consputc(KEY_LEFT); } break; case KEY_RIGHT: // Right arrow if (input.f > input.e) { input.e++; consputc(KEY_RIGHT); } break; case KEY_UP: // Key Up if (historyInd != 0) kill_line(); if (historyList.size > 0) { char buffer[INPUT_BUF]; tmp = (historyInd - 1) % historyList.size; if (tmp > -1) { historyInd = tmp; if (history(buffer, historyInd) == 0) { setHistory(buffer); } else panic("history"); } } break; case KEY_DOWN: // Key Down kill_line(); if (historyInd < historyList.size - 1) { char buffer[INPUT_BUF]; historyInd = (historyInd + 1) % MAX_HISTORY; if (historyInd < historyList.size) { if (history(buffer, historyInd) == 0) { setHistory(buffer); } else panic("history"); } else kill_line(); } else if (historyInd == historyList.size - 1) kill_line(); break; default: if (c != 0 && input.f - input.r < INPUT_BUF) { c = (c == '\r') ? '\n' : c; if (c == '\n') { // if any command is currently written, first record it in the history books if (input.f >= input.w) { updateHistory(); } } //*** regular caret *** if (input.e >= input.f) { input.buf[input.e++ % INPUT_BUF] = c; consputc(c); } //*** middle caret *** else { if (c == '\n') { input.buf[input.f % INPUT_BUF] = c; input.e = input.f + 1; consputc(c); } else { int index = input.f; while (index > input.e) { // first shift by one each char in buffer from caret to the end input.buf[index % INPUT_BUF] = input.buf[(index - 1) % INPUT_BUF]; index--; } input.buf[input.e % INPUT_BUF] = c; // Write new char in buffer int i = input.e; index = input.f + 1; while (i < index) // Print those chars from buffer to console consputc(input.buf[i++ % INPUT_BUF]); i = input.e; index = input.f; while (i < index) { // move caret back to it's place after the new character consputc(KEY_LEFT); i++; } input.e++; } } input.f++; if (c == '\n' || c == C('D') || input.f == input.r + INPUT_BUF) { input.w = input.f; input.e = input.f; wakeup(&input.r); } } break; } } release(&cons.lock); if (doprocdump) { procdump(); // now call procdump() wo. cons.lock held } }
void consoleintr(int (*getc)(void)) { int i,j; int c; acquire(&input.lock); while((c = getc()) >= 0){ switch(c){ case C('P'): // Process listing. procdump(); break; case C('U'): // Kill line. while(input.e != input.w && input.buf[(input.e-1) % INPUT_BUF] != '\n'){ input.e--; input.rm--; consputc(BACKSPACE); } break; //------------------- PATCH ------------------// case C('H'): case '\x7f': // Backspace if(input.e != input.w){ consputc(BACKSPACE); input.e--; input.rm--; if(input.e < input.rm){ //if cursor is not at end of sentance for (i=input.e; i<=input.rm; i++){ input.buf[i % INPUT_BUF] = input.buf[i+1 %INPUT_BUF]; } for(i = input.e; i <=input.rm; i++) consputc(input.buf[i %INPUT_BUF]); for(i = input.e; i <=input.rm; ++i) consputc(LEFTARROW); } } break; case UPARROW: if(numOentries >= MAX_HISTORY_LENGTH){ if(input.w < input.rm){ for(i=input.e ; i<=input.rm; i++){ consputc(RIGHTARROW); } for(i=input.rm ; i>=input.w; i--){ consputc(BACKSPACE); } input.e = input.w; input.rm = input.e; } hstryPos--; if (hstryPos < 0){ // if hstrypos is under zero hstryPos = MAX_HISTORY_LENGTH -1; } for(i = input.w,j=0; j<(strlen(history[hstryPos % MAX_HISTORY_LENGTH])); i++,j++){ input.buf[i] = history[hstryPos % MAX_HISTORY_LENGTH][j]; } j = strlen(history[hstryPos % MAX_HISTORY_LENGTH]); input.e += j; input.rm = input.e; for(i = input.w; i< input.e; i++){ consputc(input.buf[i]); } } else{ if((hstryPos % MAX_HISTORY_LENGTH) > 0){ if(input.w < input.rm){ for(i=input.e ; i<=input.rm; i++){ consputc(RIGHTARROW); } for(i=input.rm ; i>=input.w; i--){ consputc(BACKSPACE); } input.e = input.w; input.rm = input.e; } hstryPos--; for(i = input.w,j=0; j<(strlen(history[hstryPos % MAX_HISTORY_LENGTH])); i++,j++){ input.buf[i] = history[hstryPos % MAX_HISTORY_LENGTH][j]; } j = strlen(history[hstryPos % MAX_HISTORY_LENGTH]); input.e += j; input.rm = input.e; for(i = input.w; i< input.e; i++){ consputc(input.buf[i]); } } } break; case DOWNARROW: if(numOentries >= MAX_HISTORY_LENGTH){ if(input.w < input.rm){ for(i=input.rm ; i>input.w; i--){ consputc(BACKSPACE); } input.e = input.w; input.rm = input.e; } hstryPos++; if (hstryPos == MAX_HISTORY_LENGTH){ hstryPos = 0; } for(i = input.w,j=0; j<(strlen(history[hstryPos % MAX_HISTORY_LENGTH])); i++,j++){ input.buf[i] = history[hstryPos % MAX_HISTORY_LENGTH][j]; } j = strlen(history[hstryPos % MAX_HISTORY_LENGTH]); input.e += j; input.rm = input.e; for(i = input.w; i< input.e; i++){ consputc(input.buf[i]); } } else{ if((hstryPos % MAX_HISTORY_LENGTH) < (hstryNext % MAX_HISTORY_LENGTH)){ if(input.w < input.rm){ for(i=input.rm ; i>input.w; i--){ consputc(BACKSPACE); } input.e = input.w; input.rm = input.e; } hstryPos++; for(i = input.w,j=0; j<(strlen(history[hstryPos % MAX_HISTORY_LENGTH])); i++,j++){ input.buf[i] = history[hstryPos % MAX_HISTORY_LENGTH][j]; } j = strlen(history[hstryPos % MAX_HISTORY_LENGTH]); input.e += j; input.rm = input.e; for(i = input.w; i< input.e; i++){ consputc(input.buf[i]); } } } break; case RIGHTARROW: if(input.e < input.rm){ input.e++; consputc(RIGHTARROW); } break; case LEFTARROW: if(input.e != input.w){ input.e--; consputc(LEFTARROW); } break; //------------------- PATCH ------------------// default: if(c != 0 && input.e-input.r < INPUT_BUF){ c = (c == '\r') ? '\n' : c; if((c == '\n')&&(input.e<input.rm)){ // case of enter in mid of sentance input.e = input.rm; input.buf[input.e++ % INPUT_BUF] = c; consputc(c); //cprintf("buf enter after left:%s\n",input.buf); }else{ for (i=input.rm; i>input.e; i--){ //put letter in middle of sentance input.buf[i % INPUT_BUF] = input.buf[i-1 %INPUT_BUF]; } input.buf[input.e++ % INPUT_BUF] = c; consputc(c); for(i = input.e; i <= input.rm; ++i) consputc(input.buf[i]); input.rm++; for(i = input.e; i < input.rm; ++i) consputc(LEFTARROW); } if(c == '\n' || c == C('D') || input.e == input.r+INPUT_BUF){ //cprintf("buf:%s\n",input.buf); input.w = input.e; input.rm = input.e; // j =0; char command[INPUT_BUF]; memset(&command[0], 0, sizeof(command)); for (i=input.r; i < input.rm-1; i++) { command[j] = input.buf[i %INPUT_BUF]; j++; //cprintf("buf:%s\n",input.buf); } // cprintf("command is:%s",command); //command[j-1] = '\n'; if (command[0] != '\0'){ // cprintf("command:'%s'\n",command); strncpy(history[hstryNext % MAX_HISTORY_LENGTH],command,strlen(command)); hstryNext = ((hstryNext+1) % MAX_HISTORY_LENGTH); hstryPos = hstryNext; numOentries++; } //cprintf("next:%d\n",hstryNext); // for (i=0; i < hstryNext; i++) { // cprintf("history:%s\n",history[i]); // } wakeup(&input.r); } } break; } } release(&input.lock); }