Add simple shell

This commit is contained in:
Jeremy Soller
2016-02-24 16:04:30 -07:00
parent c231ddb219
commit d116ce57da
+31 -11
View File
@@ -1,6 +1,7 @@
extern crate redoxfs;
use std::env;
use std::io::{self, Write};
use redoxfs::{Disk, FileSystem};
@@ -8,13 +9,23 @@ use file_disk::FileDisk;
pub mod file_disk;
fn main() {
let mut args = env::args();
if let Some(path) = args.nth(1) {
match FileDisk::new(&path) {
Ok(disk) => match FileSystem::new(Box::new(disk)) {
Ok(filesystem) => {
let path = args.next().unwrap_or(String::new());
fn shell<E>(filesystem: FileSystem<E>){
let mut stdout = io::stdout();
let stdin = io::stdin();
loop {
stdout.write(b"redoxfs# ").unwrap();
stdout.flush().unwrap();
let mut line = String::new();
stdin.read_line(&mut line).unwrap();
let mut args = line.trim().split(' ');
if let Some(command) = args.next() {
match command {
"" => (),
"ls" => {
let path = args.next().unwrap_or("/");
for (node_block, node) in filesystem.nodes.iter() {
let mut name = "/".to_string();
for &b in node.name.iter() {
@@ -24,14 +35,23 @@ fn main() {
unsafe { name.as_mut_vec().push(b); }
}
}
if name == path {
println!("{}: {}", node_block, name);
break;
} else if name.starts_with(&path) {
if name.starts_with(&path) {
println!("{}: {}", node_block, name);
}
}
},
_ => println!("unknown command: {}", command)
}
}
}
}
fn main() {
let mut args = env::args();
if let Some(path) = args.nth(1) {
match FileDisk::new(&path) {
Ok(disk) => match FileSystem::new(Box::new(disk)) {
Ok(filesystem) => shell(filesystem),
Err(err) => {
println!("redoxfs: failed to open filesystem: {}", err);
}