/* Function to handle system calls for reading from the terminal * PARAMETERS: fd - not used in this function * buf - a pointer to a buffer in which to write the data * nbytes - not used by this function * RETURNS: the number of bytes successfully read into the buffer * INFO: This function reads bytes from the line buffer up until the * newline character (\n). If no newline character is present * than the entire buffer is read. This function also clears * the line buffer. It is intended to be called mainly * by the shell. */ int32_t terminal_read(int32_t fd, void * buf, int32_t nbytes) { cli(); clear_line_buffer(current_process->terminal); current_process->terminal->term_read = 1; sti(); while(current_process->terminal->term_read == 1) { } current_process->terminal->terminal_read_ret = current_process->terminal->line_buf_index; memcpy(buf,(void*)current_process->terminal->line_buf,BUFFER_SIZE); clear_line_buffer(current_process->terminal); return current_process->terminal->terminal_read_ret; }
/* Function to handle system calls for writing to the terminal * PARAMETERS: fd - not used in this function buf - a pointer to a buffer which contains the data to write nbytes - the number of bytes to write from the buffer * RETURNS: the number of bytes successfully written, or -1 on failure * INFO: This function sets the write_lock to 1 at the beginning of * exec ution, and clears it before returning. This will * allow user programs to block keyboard echoes while trying * to output information to the terminal. */ int32_t terminal_write(int32_t fd, const void * buf, int32_t nbytes) { int32_t i; uint8_t* _buf = (uint8_t*)buf; //Check for valid pointer and valid n_bytes if(buf == NULL || nbytes < 0) return -1; else { current_process->terminal->write_lock = 1; for(i=0; i<nbytes; i++) { if(current_process->terminal == curr_terminal) { screen_add_char(_buf[i],current_process->terminal); } storage_add_char(_buf[i],current_process->terminal); } clear_line_buffer(current_process->terminal); current_process->terminal->write_lock = 0; return i; } }
// Clear line of length len void clear_line_buffer(int len){ clear_line_buffer(len,len); }
int main() { std::vector<std::string> history; std::string line; char c; unsigned int hist_idx; unsigned int cursor; // For every line while (true) { printf("> "); line.clear(); hist_idx = history.size(); cursor = 0; // For every char while (true) { c = getch(); // Handle backspace key if (c == 127 && cursor > 0) { clear_line_buffer(line.size(),cursor); line.erase(--cursor,1); printf("%s",line.c_str()); back_up_buffer(line.size()-cursor); continue; } // Handle arrow keys if (c == 27 && getch() == 91) { switch (getch()) { //Down Arrow case 65: if (hist_idx > 0){ clear_line_buffer(line.size(),cursor); line = history.at(--hist_idx); printf("%s",line.c_str()); cursor = line.size(); } break; // Up Arrow case 66: if (hist_idx+1 < history.size() ){ clear_line_buffer(line.size(),cursor); line = history.at(++hist_idx); printf("%s",line.c_str()); cursor = line.size(); } break; // Right Arrow case 67: if (cursor < line.size()) putchar(line.at(cursor++)); break; // Left Arrow case 68: if (cursor > 0) { putchar('\b'); cursor--; } break; } continue; } // Handle Enter key if (c == 10){ putchar(10); history.push_back(line); line.clear(); break; } // Everything else clear_line_buffer(line.size(),cursor); line.insert(cursor++, 1, c); printf("%s",line.c_str()); back_up_buffer(line.size()-cursor); } } }