Browse Source

修复读取stdin时,无法正常读取的问题。 (#264)

login 1 year ago
parent
commit
bfafc10279

+ 13 - 1
kernel/src/driver/tty/mod.rs

@@ -1,3 +1,5 @@
+use core::intrinsics::unlikely;
+
 use alloc::string::String;
 
 use thingbuf::mpsc::{
@@ -168,13 +170,23 @@ impl TtyCore {
                     _ => return Err(TtyError::Unknown(format!("{err:?}"))),
                 }
             } else {
-                buf[cnt] = *val.unwrap();
+                let x = *val.unwrap();
+                buf[cnt] = x;
                 cnt += 1;
+
+                if unlikely(self.stdin_should_return(x)) {
+                    return Ok(cnt);
+                }
             }
         }
         return Ok(cnt);
     }
 
+    fn stdin_should_return(&self, c: u8) -> bool {
+        // 如果是换行符或者是ctrl+d,那么就应该返回
+        return c == b'\n' || c == 4;
+    }
+
     /// @brief 向stdin缓冲区内写入数据
     ///
     /// @param buf 输入缓冲区

+ 2 - 1
kernel/src/driver/tty/tty_device.rs

@@ -12,7 +12,7 @@ use crate::{
     include::bindings::bindings::{textui_putchar, BLACK, WHITE},
     kerror,
     libs::rwlock::RwLock,
-    syscall::SystemError,
+    syscall::SystemError, kdebug, arch::asm::current::current_pcb,
 };
 
 use super::{TtyCore, TtyError, TtyFileFlag, TtyFilePrivateData};
@@ -263,6 +263,7 @@ impl IndexNode for TtyDevice {
         }
         return Ok(());
     }
+
 }
 
 impl TtyDevicePrivateData {

+ 7 - 2
kernel/src/filesystem/vfs/file.rs

@@ -173,9 +173,14 @@ impl File {
     ///
     /// @param origin 调整的起始位置
     pub fn lseek(&mut self, origin: SeekFrom) -> Result<usize, SystemError> {
-        if self.inode.metadata().unwrap().file_type == FileType::Pipe {
-            return Err(SystemError::ESPIPE);
+        let file_type = self.inode.metadata().unwrap().file_type;
+        match file_type {
+            FileType::Pipe | FileType::CharDevice => {
+                return Err(SystemError::ESPIPE);
+            }
+            _ => {}
         }
+        
         let pos: i64;
         match origin {
             SeekFrom::SeekSet(offset) => {