Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

add TAB completion function for user_shell #94

Open
wants to merge 4 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 4 additions & 3 deletions .github/workflows/doc-and-test.yml
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ on: [push]

env:
CARGO_TERM_COLOR: always
rust_toolchain: nightly-2022-08-05

jobs:
build-doc:
Expand All @@ -13,7 +14,7 @@ jobs:
- uses: actions-rs/toolchain@v1
with:
profile: minimal
toolchain: nightly-2022-07-20
toolchain: ${{ env.rust_toolchain }}
components: rust-src, llvm-tools-preview
target: riscv64gc-unknown-none-elf
- name: Build doc
Expand All @@ -32,7 +33,7 @@ jobs:
- uses: actions-rs/toolchain@v1
with:
profile: minimal
toolchain: nightly-2022-07-20
toolchain: ${{ env.rust_toolchain }}
components: rust-src, llvm-tools-preview
target: riscv64gc-unknown-none-elf
- uses: actions-rs/install@v0.1
Expand Down Expand Up @@ -66,4 +67,4 @@ jobs:
timeout-minutes: 10

- name: Build for k210
run: cd os && make build BOARD=k210
run: cd os && make build BOARD=k210
13 changes: 12 additions & 1 deletion os/src/fs/inode.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ use super::File;
use crate::drivers::BLOCK_DEVICE;
use crate::mm::UserBuffer;
use crate::sync::UPIntrFreeCell;
use alloc::sync::Arc;
use alloc::{sync::Arc, string::ToString};
use alloc::vec::Vec;
use bitflags::*;
use easy_fs::{EasyFileSystem, Inode};
Expand Down Expand Up @@ -105,6 +105,17 @@ pub fn open_file(name: &str, flags: OpenFlags) -> Option<Arc<OSInode>> {
}
}

pub fn open_dir_context() -> Vec<u8> {
let mut list = "".to_string();
let mut v: Vec<u8> = Vec::new();
for fileObj in ROOT_INODE.ls(){
list += &fileObj.to_string();
list += &";".to_string();
}
v.extend_from_slice(&list.as_bytes());
v
}

impl File for OSInode {
fn readable(&self) -> bool {
self.readable
Expand Down
2 changes: 1 addition & 1 deletion os/src/fs/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,6 @@ pub trait File: Send + Sync {
fn write(&self, buf: UserBuffer) -> usize;
}

pub use inode::{list_apps, open_file, OSInode, OpenFlags, ROOT_INODE};
pub use inode::{list_apps, open_file, open_dir_context, OSInode, OpenFlags, ROOT_INODE};
pub use pipe::{make_pipe, Pipe};
pub use stdio::{Stdin, Stdout};
27 changes: 26 additions & 1 deletion os/src/syscall/fs.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use crate::fs::{make_pipe, open_file, OpenFlags};
use crate::fs::{make_pipe, open_dir_context, open_file, OpenFlags};
use crate::mm::{translated_byte_buffer, translated_refmut, translated_str, UserBuffer};
use crate::task::{current_process, current_user_token};
use alloc::sync::Arc;
Expand Down Expand Up @@ -43,6 +43,31 @@ pub fn sys_read(fd: usize, buf: *const u8, len: usize) -> isize {
}
}

pub fn sys_fs_ls(fd: usize, buf: *const u8) -> isize {
//FIXME 以后实现dir之后就要换成从dir读取
let token = current_user_token();
let mut context = open_dir_context();
let mut buffers = translated_byte_buffer(token, buf, context.len());
let mut start = 0;
let mut total_read_size = 0;
for slice in buffers.iter_mut() {
let mut block_read_size = slice.len();
let dst = &mut slice[0..block_read_size];
if start + block_read_size <= context.len(){
let src = &context[start..start + block_read_size];
dst.copy_from_slice(src);
start = block_read_size + start + 1;
total_read_size += block_read_size;
}else{
let src = &context[start..];
dst.copy_from_slice(src);
total_read_size += src.len();
break;
}
}
total_read_size as isize
}

pub fn sys_open(path: *const u8, flags: u32) -> isize {
let process = current_process();
let token = current_user_token();
Expand Down
3 changes: 3 additions & 0 deletions os/src/syscall/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ const SYSCALL_CLOSE: usize = 57;
const SYSCALL_PIPE: usize = 59;
const SYSCALL_READ: usize = 63;
const SYSCALL_WRITE: usize = 64;
const SYSCALL_FS_LS: usize = 65;
const SYSCALL_EXIT: usize = 93;
const SYSCALL_SLEEP: usize = 101;
const SYSCALL_YIELD: usize = 124;
Expand Down Expand Up @@ -51,6 +52,7 @@ pub fn syscall(syscall_id: usize, args: [usize; 3]) -> isize {
SYSCALL_PIPE => sys_pipe(args[0] as *mut usize),
SYSCALL_READ => sys_read(args[0], args[1] as *const u8, args[2]),
SYSCALL_WRITE => sys_write(args[0], args[1] as *const u8, args[2]),
SYSCALL_FS_LS => sys_fs_ls(args[0], args[1] as *const u8),
SYSCALL_EXIT => sys_exit(args[0] as i32),
SYSCALL_SLEEP => sys_sleep(args[0]),
SYSCALL_YIELD => sys_yield(),
Expand Down Expand Up @@ -86,6 +88,7 @@ pub fn syscall(syscall_id: usize, args: [usize; 3]) -> isize {
SYSCALL_PIPE => sys_pipe(args[0] as *mut usize),
SYSCALL_READ => sys_read(args[0], args[1] as *const u8, args[2]),
SYSCALL_WRITE => sys_write(args[0], args[1] as *const u8, args[2]),
SYSCALL_FS_LS => sys_fs_ls(args[0], args[1] as *const u8),
SYSCALL_EXIT => sys_exit(args[0] as i32),
SYSCALL_SLEEP => sys_sleep(args[0]),
SYSCALL_YIELD => sys_yield(),
Expand Down
3 changes: 2 additions & 1 deletion rust-toolchain.toml
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
[toolchain]
profile = "minimal"
channel = "nightly-2022-07-20"
# use the nightly version of the last stable toolchain, see <https://forge.rust-lang.org/>
channel = "nightly-2022-08-05"
components = ["rust-src", "llvm-tools-preview", "rustfmt", "clippy"]
30 changes: 27 additions & 3 deletions user/src/bin/user_shell.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,12 +11,13 @@ const LF: u8 = 0x0au8;
const CR: u8 = 0x0du8;
const DL: u8 = 0x7fu8;
const BS: u8 = 0x08u8;
const TAB: u8 = 0x0009;
const LINE_START: &str = ">> ";

use alloc::string::String;
use alloc::string::{String};
use alloc::vec::Vec;
use user_lib::console::getchar;
use user_lib::{close, dup, exec, fork, open, pipe, waitpid, OpenFlags};
use user_lib::console::{getchar};
use user_lib::{close, dup, exec, fork, open, pipe, waitpid,read_fs_ls, OpenFlags};

#[derive(Debug)]
struct ProcessArguments {
Expand Down Expand Up @@ -205,6 +206,29 @@ pub fn main() -> i32 {
line.pop();
}
}
TAB =>{
let mut matched = false;
if !line.is_empty() {
let mut buffer = [0u8; 514];
read_fs_ls(0, &mut buffer);
let ls = String::from_utf8_lossy(&buffer);
let lss = ls.split(";");
for ele in lss {
if(ele.starts_with(&line)){
let word = ele.replace(&line, "");
print!("{}",word);
line.push_str(&word);
matched = true;
break;
// line.push(ele);
}
}
}
if !matched {
print!("{}", c as char);
line.push(c as char);
}
}
_ => {
print!("{}", c as char);
line.push(c as char);
Expand Down
3 changes: 3 additions & 0 deletions user/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -84,6 +84,9 @@ pub fn pipe(pipe_fd: &mut [usize]) -> isize {
pub fn read(fd: usize, buf: &mut [u8]) -> isize {
sys_read(fd, buf)
}
pub fn read_fs_ls(fd: usize, buf: &mut [u8]) -> isize {
sys_fs_ls(fd, buf)
}
pub fn write(fd: usize, buf: &[u8]) -> isize {
sys_write(fd, buf)
}
Expand Down
5 changes: 5 additions & 0 deletions user/src/syscall.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ const SYSCALL_CLOSE: usize = 57;
const SYSCALL_PIPE: usize = 59;
const SYSCALL_READ: usize = 63;
const SYSCALL_WRITE: usize = 64;
const SYSCALL_FS_LS: usize = 65;
const SYSCALL_EXIT: usize = 93;
const SYSCALL_SLEEP: usize = 101;
const SYSCALL_YIELD: usize = 124;
Expand Down Expand Up @@ -63,6 +64,10 @@ pub fn sys_read(fd: usize, buffer: &mut [u8]) -> isize {
)
}

pub fn sys_fs_ls(fd:usize,buffer: &mut [u8]) ->isize {
syscall(SYSCALL_FS_LS, [fd, buffer.as_mut_ptr() as usize, 0])
}

pub fn sys_write(fd: usize, buffer: &[u8]) -> isize {
syscall(SYSCALL_WRITE, [fd, buffer.as_ptr() as usize, buffer.len()])
}
Expand Down