Skip to content

Commit

Permalink
kern: Better initialisation of console termios
Browse files Browse the repository at this point in the history
  • Loading branch information
mintsuki committed Mar 1, 2025
1 parent f644f97 commit 285392c
Show file tree
Hide file tree
Showing 2 changed files with 61 additions and 4 deletions.
29 changes: 25 additions & 4 deletions kernel/modules/dev/console/console.v
Original file line number Diff line number Diff line change
Expand Up @@ -310,10 +310,18 @@ fn is_printable(c u8) bool {
fn add_to_buf_char(_c u8, echo bool) {
mut c := _c

if c == `\r` && console_termios.c_iflag & termios.igncr != 0 {
return
}

if c == `\n` && console_termios.c_iflag & termios.icrnl == 0 {
c = `\r`
} else if c == `\r` && console_termios.c_iflag & termios.icrnl != 0 {
c = `\n`
} else if c == `\r` && console_termios.c_iflag & termios.inlcr == 0 {
c = `\n`
} else if c == `\n` && console_termios.c_iflag & termios.inlcr != 0 {
c = `\r`
}

if console_termios.c_lflag & termios.icanon != 0 {
Expand Down Expand Up @@ -684,10 +692,23 @@ pub fn initialise() {
console_res.stat.mode = 0o644 | stat.ifchr

// Initialise termios
console_res.termios.c_lflag = termios.isig | termios.icanon | termios.echo
console_res.termios.c_cc[termios.vintr] = 0x03
console_res.termios.ibaud = 38400
console_res.termios.obaud = 38400
console_res.termios.c_iflag = termios.brkint | termios.icrnl | termios.ixon | termios.imaxbel
console_res.termios.c_oflag = termios.opost | termios.onlcr
console_res.termios.c_cflag = termios.cs8 | termios.cread | termios.b38400
console_res.termios.c_lflag = termios.isig | termios.icanon | termios.iexten | termios.echo | termios.echoe | termios.echok | termios.echoctl | termios.echoke
console_res.termios.c_cc[termios.vintr] = termios.ctrl(`C`)
console_res.termios.c_cc[termios.vquit] = termios.ctrl(`\\`)
console_res.termios.c_cc[termios.verase] = 0x7f //termios.ctrl(`?`)
console_res.termios.c_cc[termios.vkill] = termios.ctrl(`U`)
console_res.termios.c_cc[termios.veof] = termios.ctrl(`D`)
console_res.termios.c_cc[termios.vstart] = termios.ctrl(`Q`)
console_res.termios.c_cc[termios.vstop] = termios.ctrl(`S`)
console_res.termios.c_cc[termios.vsusp] = termios.ctrl(`Z`)
console_res.termios.c_cc[termios.vreprint] = termios.ctrl(`R`)
console_res.termios.c_cc[termios.vwerase] = termios.ctrl(`W`)
console_res.termios.c_cc[termios.vlnext] = termios.ctrl(`V`)
console_res.termios.c_cc[termios.vdiscard] = termios.ctrl(`O`)
console_res.termios.c_cc[termios.vmin] = 1

console_termios = &console_res.termios

Expand Down
36 changes: 36 additions & 0 deletions kernel/modules/termios/termios.v
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,10 @@

module termios

pub const brkint = 0o0002

pub const ignpar = 0o0004

pub const echo = 0o0010

pub const echoe = 0o0020
Expand All @@ -16,8 +20,14 @@ pub const icanon = 0o0002

pub const inlcr = 0o0100

pub const igncr = 0o0200

pub const icrnl = 0o0400

pub const ixon = 0o2000

pub const imaxbel = 0o20000

pub const iexten = 0o100000

pub const isig = 0o0001
Expand All @@ -26,8 +36,20 @@ pub const noflsh = 0o0200

pub const tostop = 0o0400

pub const echoctl = 0o1000

pub const echoprt = 0o2000

pub const echoke = 0o4000

pub const opost = 0o0001

pub const onlcr = 0o0004

pub const cs8 = 0o0060

pub const cread = 0o0200

pub const nccs = 32

pub const veof = 4
Expand All @@ -52,6 +74,16 @@ pub const vsusp = 10

pub const vtime = 5

pub const vdiscard = 13

pub const vreprint = 12

pub const vwerase = 14

pub const vlnext = 15

pub const b38400 = 15

pub struct Termios {
pub mut:
c_iflag u32
Expand All @@ -63,3 +95,7 @@ pub mut:
ibaud u32
obaud u32
}

pub fn ctrl(c u8) u8 {
return c & 0o37
}

0 comments on commit 285392c

Please sign in to comment.