-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Line discipline is a layer in the terminal subsystem of Unix-like systems, governing the processing of input and output streams. It manages tasks such as line editing, buffering, and character echoing, ensuring a smooth user experience. Implementing line discipline will enhance the structure of our system, resulting in a more maintainable and reliable console component. Close #12
- Loading branch information
1 parent
830a294
commit 4e35b55
Showing
8 changed files
with
167 additions
and
125 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
#pragma once | ||
|
||
#include <config.h> | ||
#include <dev.h> | ||
|
||
typedef struct { | ||
bool crlf; | ||
} ldisc_config_t; | ||
|
||
typedef struct { | ||
dev_t dev; | ||
ldisc_config_t config; | ||
} ldisc_dev_t; | ||
|
||
typedef struct { | ||
ldisc_dev_t src; | ||
ldisc_dev_t sinks[TTY_SINK_CAPACITY]; | ||
u32 nr_sinks; | ||
u32 cursor_pos; | ||
} ldisc_ctrl_blk_t; | ||
|
||
i32 ldisc_register_src(u32 num, ldisc_dev_t dev); | ||
i32 ldisc_register_sink(u32 num, ldisc_dev_t dev); | ||
void ldisc_recv_byte(u32 num, u8 byte); | ||
isize ldisc_write(u32 num, const u8 *buf, usize size); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,9 +1,15 @@ | ||
#pragma once | ||
|
||
#include <dev.h> | ||
#include <types.h> | ||
#include <drivers/ldisc.h> | ||
#include <sem.h> | ||
|
||
i32 tty_register_src(u32 num, dev_t src); | ||
i32 tty_register_sink(u32 num, dev_t sink); | ||
typedef struct { | ||
ldisc_ctrl_blk_t ldisc_ctrl_blk; | ||
u8 *rx_buf; | ||
usize rx_head, rx_tail; | ||
sem_t rx_sem; | ||
} tty_ctrl_blk_t; | ||
|
||
extern | ||
extern tty_ctrl_blk_t *tty_ctrl_blks[DRIVER_DEV_CAPACITY]; | ||
|
||
i32 tty_init_ctrl_blk(tty_ctrl_blk_t *ctrl_blk); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,88 @@ | ||
#include <drivers/ldisc.h> | ||
|
||
#include <drivers/tty.h> | ||
#include <errno.h> | ||
#include <kalloc.h> | ||
|
||
static i32 lazy_init_ctrl_blk(u32 num) { | ||
if (num >= DRIVER_DEV_CAPACITY) | ||
return -EAGAIN; | ||
if (tty_ctrl_blks[num]) | ||
return 0; | ||
|
||
tty_ctrl_blk_t *tty_ctrl_blk = | ||
(tty_ctrl_blk_t *) kmalloc(sizeof(tty_ctrl_blk_t)); | ||
if (!tty_ctrl_blk) | ||
return -ENOMEM; | ||
|
||
ldisc_ctrl_blk_t *ctrl_blk = &tty_ctrl_blk->ldisc_ctrl_blk; | ||
ctrl_blk->nr_sinks = 0; | ||
ctrl_blk->cursor_pos = 0; | ||
|
||
i32 res = tty_init_ctrl_blk(tty_ctrl_blk); | ||
if (res < 0) | ||
return res; | ||
tty_ctrl_blks[num] = tty_ctrl_blk; | ||
return 0; | ||
} | ||
|
||
i32 ldisc_register_src(u32 num, ldisc_dev_t dev) { | ||
i32 res = lazy_init_ctrl_blk(num); | ||
if (res < 0) | ||
return res; | ||
ldisc_ctrl_blk_t *ctrl_blk = &tty_ctrl_blks[num]->ldisc_ctrl_blk; | ||
ctrl_blk->src = dev; | ||
return 0; | ||
} | ||
|
||
i32 ldisc_register_sink(u32 num, ldisc_dev_t dev) { | ||
i32 res = lazy_init_ctrl_blk(num); | ||
if (res < 0) | ||
return res; | ||
ldisc_ctrl_blk_t *ctrl_blk = &tty_ctrl_blks[num]->ldisc_ctrl_blk; | ||
if (ctrl_blk->nr_sinks >= TTY_SINK_CAPACITY) | ||
return -EAGAIN; | ||
ctrl_blk->sinks[ctrl_blk->nr_sinks++] = dev; | ||
return 0; | ||
} | ||
|
||
void ldisc_recv_byte(u32 num, u8 byte) { | ||
tty_ctrl_blk_t *tty_ctrl_blk = tty_ctrl_blks[num]; | ||
ldisc_ctrl_blk_t *ctrl_blk = &tty_ctrl_blk->ldisc_ctrl_blk; | ||
switch (byte) { | ||
case '\r': | ||
tty_ctrl_blk->rx_buf[tty_ctrl_blk->rx_tail++] = '\n'; | ||
tty_ctrl_blk->rx_tail %= TTY_BUF_SIZE; | ||
ctrl_blk->cursor_pos++; | ||
sem_signaln(&tty_ctrl_blk->rx_sem, ctrl_blk->cursor_pos); | ||
ctrl_blk->cursor_pos = 0; | ||
ldisc_write(num, (const u8 *) "\n", 1); | ||
break; | ||
case '\x7F': | ||
if (ctrl_blk->cursor_pos > 0) { | ||
if (tty_ctrl_blk->rx_tail == 0) { | ||
tty_ctrl_blk->rx_tail = TTY_BUF_SIZE; | ||
} | ||
tty_ctrl_blk->rx_tail--; | ||
ctrl_blk->cursor_pos--; | ||
ldisc_write(num, (const u8 *) "\b \b", 3); | ||
} | ||
break; | ||
default: | ||
tty_ctrl_blk->rx_buf[tty_ctrl_blk->rx_tail++] = byte; | ||
tty_ctrl_blk->rx_tail %= TTY_BUF_SIZE; | ||
ctrl_blk->cursor_pos++; | ||
ldisc_write(num, &byte, 1); | ||
break; | ||
} | ||
} | ||
|
||
isize ldisc_write(u32 num, const u8 *buf, usize size) { | ||
ldisc_ctrl_blk_t *ctrl_blk = &tty_ctrl_blks[num]->ldisc_ctrl_blk; | ||
for (u32 i = 0; i < ctrl_blk->nr_sinks; i++) { | ||
isize res = dev_write(ctrl_blk->sinks[i].dev, buf, size); | ||
if (res < 0) | ||
return res; | ||
} | ||
return size; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.