diff options
Diffstat (limited to 'kernel/drivers/tty/ldisc.c')
-rw-r--r-- | kernel/drivers/tty/ldisc.c | 165 |
1 files changed, 157 insertions, 8 deletions
diff --git a/kernel/drivers/tty/ldisc.c b/kernel/drivers/tty/ldisc.c index eaf0440..dd67706 100644 --- a/kernel/drivers/tty/ldisc.c +++ b/kernel/drivers/tty/ldisc.c @@ -16,7 +16,19 @@ */ void ldisc_init(ldisc_t *ldisc) { - NOT_YET_IMPLEMENTED("DRIVERS: ldisc_init"); + // NOT_YET_IMPLEMENTED("DRIVERS: ldisc_init"); + + // reset static buffer + memset(ldisc->ldisc_buffer, 0, LDISC_BUFFER_SIZE); + + // init the queue + sched_queue_init(&ldisc->ldisc_read_queue); + + // fill in other fields + ldisc->ldisc_head = 0; + ldisc->ldisc_tail = 0; + ldisc->ldisc_cooked = 0; + ldisc->ldisc_full = 0; // 0 not full } /** @@ -33,8 +45,31 @@ void ldisc_init(ldisc_t *ldisc) */ long ldisc_wait_read(ldisc_t *ldisc) { - NOT_YET_IMPLEMENTED("DRIVERS: ldisc_wait_read"); - return -1; + // NOT_YET_IMPLEMENTED("DRIVERS: ldisc_wait_read"); + + // if it's full, return 0 + if (ldisc->ldisc_full) + { + return 0; + } + + // while there are no need chars to be read, sleep + // TODO: check if this is the right condition + while ( + (ldisc->ldisc_head == ldisc->ldisc_tail) + || + (ldisc->ldisc_head == ldisc->ldisc_cooked) + ) + { + long ret = sched_cancellable_sleep_on(&ldisc->ldisc_read_queue); + if (ret != 0) + { + return ret; + } + } + + // if we are here, it means we have new chars to read + return 0; } /** @@ -54,8 +89,51 @@ long ldisc_wait_read(ldisc_t *ldisc) */ size_t ldisc_read(ldisc_t *ldisc, char *buf, size_t count) { - NOT_YET_IMPLEMENTED("DRIVERS: ldisc_read"); - return 0; + // NOT_YET_IMPLEMENTED("DRIVERS: ldisc_read"); + + // read from ldisc buffer to buf + size_t i = 0; + while (i < count) + { + // if we have new chars to read + if (ldisc->ldisc_head != ldisc->ldisc_tail) + { + // if we encounter a new line symbol + if (ldisc->ldisc_buffer[ldisc->ldisc_head] == '\n') + { + buf[i] = ldisc->ldisc_buffer[ldisc->ldisc_head]; + ldisc->ldisc_head = (ldisc->ldisc_head + 1) % LDISC_BUFFER_SIZE; + i++; + break; + } + // if we encounter an EOT + else if (ldisc->ldisc_buffer[ldisc->ldisc_head] == EOT) + { + break; + } + // if we encounter an ETX + else if (ldisc->ldisc_buffer[ldisc->ldisc_head] == ETX) + { + // clear uncooked portion of the line + ldisc->ldisc_head = ldisc->ldisc_cooked; + break; + } + // if we encounter a normal char + else + { + buf[i] = ldisc->ldisc_buffer[ldisc->ldisc_head]; + ldisc->ldisc_head = (ldisc->ldisc_head + 1) % LDISC_BUFFER_SIZE; + i++; + } + } + else + { + break; + } + } + + // return the number of bytes read + return i; } /** @@ -103,7 +181,66 @@ size_t ldisc_read(ldisc_t *ldisc, char *buf, size_t count) */ void ldisc_key_pressed(ldisc_t *ldisc, char c) { - NOT_YET_IMPLEMENTED("DRIVERS: ldisc_key_pressed"); + // NOT_YET_IMPLEMENTED("DRIVERS: ldisc_key_pressed"); + + // if the buffer is full, ignore the incoming char + if (ldisc->ldisc_full) + { + return; + } + + switch (c) + { + case '\b': + // if there is a character to remove + if (ldisc->ldisc_head != ldisc->ldisc_tail) + { + // remove the last char + ldisc->ldisc_head = (ldisc->ldisc_head - 1 + LDISC_BUFFER_SIZE) % LDISC_BUFFER_SIZE; + // emit a `\b` to the vterminal + vterminal_write(ldisc_to_tty(ldisc), "\b", 1); + } + break; + + case '\n': + // emit a `\n` to the vterminal + vterminal_write(ldisc_to_tty(ldisc), "\n", 1); + + // add the new char to the buffer + ldisc->ldisc_buffer[ldisc->ldisc_tail] = c; + ldisc->ldisc_tail = (ldisc->ldisc_tail + 1) % LDISC_BUFFER_SIZE; + // set the buffer to full + ldisc->ldisc_full = 1; + + // wake up the thread that is sleeping on the wait queue of the line discipline + sched_wakeup_on(&ldisc->ldisc_read_queue, 0); + break; + + case EOT: + // add the new char to the buffer + ldisc->ldisc_buffer[ldisc->ldisc_tail] = c; + ldisc->ldisc_tail = (ldisc->ldisc_tail + 1) % LDISC_BUFFER_SIZE; + // set the buffer to full + ldisc->ldisc_full = 1; + + // cook the buffer + ldisc->ldisc_cooked = ldisc->ldisc_tail; + + // wake up the thread that is sleeping on the wait queue of the line discipline + sched_wakeup_on(&ldisc->ldisc_read_queue, 0); + break; + + case ETX: + // clear uncooked portion of the line + ldisc->ldisc_head = ldisc->ldisc_cooked; + break; + + default: + // if none applies, fallback to vterminal_key_pressed + vterminal_key_pressed(ldisc_to_tty(ldisc)); + + break; + } } /** @@ -115,6 +252,18 @@ void ldisc_key_pressed(ldisc_t *ldisc, char c) */ size_t ldisc_get_current_line_raw(ldisc_t *ldisc, char *s) { - NOT_YET_IMPLEMENTED("DRIVERS: ldisc_get_current_line_raw"); - return 0; + // NOT_YET_IMPLEMENTED("DRIVERS: ldisc_get_current_line_raw"); + + // copy the raw part of the line discipline buffer into the buffer provided + size_t i = 0; + size_t j = ldisc->ldisc_cooked; + while (j != ldisc->ldisc_head) + { + s[i] = ldisc->ldisc_buffer[j]; + j = (j + 1) % LDISC_BUFFER_SIZE; + i++; + } + + // return the number of bytes copied + return i; } |