From 36281eb1eba37d106ac4575c0197e6467bf1073a Mon Sep 17 00:00:00 2001 From: Jianfeng Jiang Date: Wed, 26 Jul 2023 16:27:24 +0800 Subject: [PATCH] Allow setting callback for push char --- .../jinux-std/src/device/tty/line_discipline.rs | 17 +++++++++-------- services/libs/jinux-std/src/device/tty/mod.rs | 2 +- 2 files changed, 10 insertions(+), 9 deletions(-) diff --git a/services/libs/jinux-std/src/device/tty/line_discipline.rs b/services/libs/jinux-std/src/device/tty/line_discipline.rs index 9efde2330..772a0a89f 100644 --- a/services/libs/jinux-std/src/device/tty/line_discipline.rs +++ b/services/libs/jinux-std/src/device/tty/line_discipline.rs @@ -5,6 +5,7 @@ use crate::{ prelude::*, process::{process_table, signal::signals::kernel::KernelSignal, Pgid}, }; +use alloc::format; use ringbuf::{ring_buffer::RbBase, Rb, StaticRb}; use super::termio::{KernelTermios, CC_C_CHAR}; @@ -75,7 +76,7 @@ impl LineDiscipline { } /// push char to line discipline. This function should be called in input interrupt handler. - pub fn push_char(&self, mut item: u8) { + pub fn push_char(&self, mut item: u8, echo_callback: fn(&str)) { let termios = self.termios.lock_irq_disabled(); if termios.contains_icrnl() { if item == b'\r' { @@ -128,7 +129,7 @@ impl LineDiscipline { } if termios.contain_echo() { - self.output_char(item, &termios); + self.output_char(item, &termios, echo_callback); } if self.is_readable() { @@ -142,22 +143,22 @@ impl LineDiscipline { } // TODO: respect output flags - fn output_char(&self, item: u8, termios: &KernelTermios) { + fn output_char(&self, item: u8, termios: &KernelTermios, echo_callback: fn(&str)) { match item { - b'\n' => print!("\n"), - b'\r' => print!("\r\n"), + b'\n' => echo_callback("\n"), + b'\r' => echo_callback("\r\n"), item if item == *termios.get_special_char(CC_C_CHAR::VERASE) => { // write a space to overwrite current character let backspace: &str = core::str::from_utf8(&[b'\x08', b' ', b'\x08']).unwrap(); - print!("{}", backspace); + echo_callback(backspace); } item if 0x20 <= item && item < 0x7f => print!("{}", char::from(item)), item if 0 < item && item < 0x20 && termios.contains_echo_ctl() => { // The unprintable chars between 1-31 are mapped to ctrl characters between 65-95. // e.g., 0x3 is mapped to 0x43, which is C. So, we will print ^C when 0x3 is met. if 0 < item && item < 0x20 { - let ctrl_char = char::from(item + 0x40); - print!("^{ctrl_char}"); + let ctrl_char = format!("^{}", char::from(item + 0x40)); + echo_callback(&ctrl_char); } } item => {} diff --git a/services/libs/jinux-std/src/device/tty/mod.rs b/services/libs/jinux-std/src/device/tty/mod.rs index 6db7bf530..5d5aff5ee 100644 --- a/services/libs/jinux-std/src/device/tty/mod.rs +++ b/services/libs/jinux-std/src/device/tty/mod.rs @@ -50,7 +50,7 @@ impl Tty { } pub fn receive_char(&self, item: u8) { - self.ldisc.push_char(item); + self.ldisc.push_char(item, |content| print!("{}", content)); } }