2019-12-31 12:10:03 +00:00
|
|
|
use crate::{print, user, kernel};
|
2020-02-07 17:36:27 +00:00
|
|
|
use alloc::format;
|
2020-01-25 09:48:06 +00:00
|
|
|
use alloc::vec;
|
2020-01-20 09:44:59 +00:00
|
|
|
use alloc::vec::Vec;
|
|
|
|
use alloc::string::String;
|
2019-12-28 17:08:11 +00:00
|
|
|
|
2020-01-01 08:07:09 +00:00
|
|
|
#[repr(u8)]
|
|
|
|
pub enum ExitCode {
|
|
|
|
CommandSuccessful = 0,
|
|
|
|
CommandUnknown = 1,
|
|
|
|
CommandError = 2,
|
|
|
|
ShellExit = 255,
|
|
|
|
}
|
|
|
|
|
2019-12-31 12:10:03 +00:00
|
|
|
pub struct Shell {
|
2020-01-20 09:44:59 +00:00
|
|
|
cmd: String,
|
|
|
|
prompt: String,
|
|
|
|
history: Vec<String>,
|
2019-12-31 12:10:03 +00:00
|
|
|
history_index: usize,
|
2020-01-25 09:48:06 +00:00
|
|
|
autocomplete: Vec<String>,
|
|
|
|
autocomplete_index: usize,
|
2019-12-28 17:08:11 +00:00
|
|
|
}
|
|
|
|
|
2019-12-31 12:10:03 +00:00
|
|
|
impl Shell {
|
|
|
|
pub fn new() -> Self {
|
|
|
|
Shell {
|
|
|
|
cmd: String::new(),
|
2020-01-07 09:48:09 +00:00
|
|
|
prompt: String::from("> "),
|
2020-01-20 09:44:59 +00:00
|
|
|
history: Vec::new(),
|
2019-12-31 12:10:03 +00:00
|
|
|
history_index: 0,
|
2020-01-25 09:48:06 +00:00
|
|
|
autocomplete: Vec::new(),
|
|
|
|
autocomplete_index: 0,
|
2019-12-31 12:10:03 +00:00
|
|
|
}
|
|
|
|
}
|
2019-12-29 13:43:36 +00:00
|
|
|
|
2020-01-01 08:07:09 +00:00
|
|
|
pub fn run(&mut self) -> user::shell::ExitCode {
|
2020-02-07 17:36:27 +00:00
|
|
|
self.load_history();
|
2019-12-31 12:10:03 +00:00
|
|
|
self.print_prompt();
|
|
|
|
loop {
|
2020-01-14 22:54:46 +00:00
|
|
|
let (x, y) = kernel::vga::cursor_position();
|
2019-12-31 20:20:59 +00:00
|
|
|
let c = kernel::console::get_char();
|
|
|
|
match c {
|
2019-12-31 17:16:52 +00:00
|
|
|
'\0' => {
|
2019-12-31 12:10:03 +00:00
|
|
|
continue;
|
2019-12-29 09:34:08 +00:00
|
|
|
}
|
2020-01-07 09:48:09 +00:00
|
|
|
'\x03' => { // Ctrl C
|
2020-01-07 09:48:30 +00:00
|
|
|
if self.cmd.len() > 0 {
|
|
|
|
self.cmd.clear();
|
|
|
|
print!("\n");
|
|
|
|
self.print_prompt();
|
|
|
|
} else {
|
|
|
|
return ExitCode::CommandSuccessful;
|
|
|
|
}
|
2020-01-07 09:48:09 +00:00
|
|
|
},
|
|
|
|
'\n' => { // Newline
|
2020-02-07 17:36:27 +00:00
|
|
|
self.update_history();
|
2020-01-25 09:48:06 +00:00
|
|
|
self.update_autocomplete();
|
2019-12-31 12:10:03 +00:00
|
|
|
print!("\n");
|
2019-12-31 22:14:17 +00:00
|
|
|
if self.cmd.len() > 0 {
|
|
|
|
// Add or move command to history at the end
|
|
|
|
let cmd = self.cmd.clone();
|
2020-01-20 09:44:59 +00:00
|
|
|
if let Some(pos) = self.history.iter().position(|s| *s == *cmd) {
|
|
|
|
self.history.remove(pos);
|
2019-12-31 22:14:17 +00:00
|
|
|
}
|
2020-01-20 09:44:59 +00:00
|
|
|
self.history.push(cmd);
|
|
|
|
self.history_index = self.history.len();
|
2019-12-31 12:10:03 +00:00
|
|
|
|
|
|
|
let line = self.cmd.clone();
|
2020-01-01 08:07:09 +00:00
|
|
|
match self.exec(&line) {
|
2020-02-07 17:36:27 +00:00
|
|
|
ExitCode::CommandSuccessful => {
|
|
|
|
self.save_history();
|
|
|
|
},
|
|
|
|
ExitCode::ShellExit => {
|
|
|
|
return ExitCode::CommandSuccessful
|
|
|
|
},
|
|
|
|
_ => {
|
|
|
|
print!("?\n")
|
|
|
|
},
|
2019-12-31 12:10:03 +00:00
|
|
|
}
|
|
|
|
self.cmd.clear();
|
|
|
|
}
|
|
|
|
self.print_prompt();
|
|
|
|
},
|
2020-01-25 09:48:06 +00:00
|
|
|
'\t' => { // Tab
|
2020-02-07 17:36:27 +00:00
|
|
|
self.update_history();
|
2020-01-25 09:48:06 +00:00
|
|
|
self.print_autocomplete();
|
|
|
|
},
|
2019-12-31 17:16:52 +00:00
|
|
|
'↑' => { // Arrow up
|
2020-01-25 09:48:06 +00:00
|
|
|
self.update_autocomplete();
|
2019-12-31 12:10:03 +00:00
|
|
|
if self.history.len() > 0 {
|
|
|
|
if self.history_index > 0 {
|
|
|
|
self.history_index -= 1;
|
|
|
|
}
|
2020-02-07 17:36:27 +00:00
|
|
|
let cmd = &self.history[self.history_index];
|
|
|
|
kernel::vga::clear_row();
|
|
|
|
print!("{}{}", self.prompt, cmd);
|
2019-12-31 12:10:03 +00:00
|
|
|
}
|
|
|
|
},
|
2019-12-31 17:16:52 +00:00
|
|
|
'↓' => { // Arrow down
|
2020-01-25 09:48:06 +00:00
|
|
|
self.update_autocomplete();
|
2020-02-07 17:36:27 +00:00
|
|
|
if self.history_index < self.history.len() {
|
|
|
|
self.history_index += 1;
|
|
|
|
let cmd = if self.history_index < self.history.len() {
|
|
|
|
&self.history[self.history_index]
|
|
|
|
} else {
|
|
|
|
&self.cmd
|
|
|
|
};
|
|
|
|
kernel::vga::clear_row();
|
|
|
|
print!("{}{}", self.prompt, cmd);
|
2019-12-31 12:10:03 +00:00
|
|
|
}
|
|
|
|
},
|
2020-01-07 09:48:09 +00:00
|
|
|
'←' => { // Arrow left
|
2020-02-07 17:36:27 +00:00
|
|
|
self.update_history();
|
2020-01-25 09:48:06 +00:00
|
|
|
self.update_autocomplete();
|
2020-01-07 09:48:09 +00:00
|
|
|
if x > self.prompt.len() {
|
|
|
|
kernel::vga::set_cursor_position(x - 1, y);
|
|
|
|
kernel::vga::set_writer_position(x - 1, y);
|
|
|
|
}
|
|
|
|
},
|
|
|
|
'→' => { // Arrow right
|
2020-02-07 17:36:27 +00:00
|
|
|
self.update_history();
|
2020-01-25 09:48:06 +00:00
|
|
|
self.update_autocomplete();
|
2020-01-07 09:48:09 +00:00
|
|
|
if x < self.prompt.len() + self.cmd.len() {
|
|
|
|
kernel::vga::set_cursor_position(x + 1, y);
|
|
|
|
kernel::vga::set_writer_position(x + 1, y);
|
|
|
|
}
|
|
|
|
},
|
|
|
|
'\x08' => { // Backspace
|
2020-02-07 17:36:27 +00:00
|
|
|
self.update_history();
|
2020-01-25 09:48:06 +00:00
|
|
|
self.update_autocomplete();
|
2020-01-07 09:48:09 +00:00
|
|
|
let cmd = self.cmd.clone();
|
2020-02-02 17:53:55 +00:00
|
|
|
if cmd.len() > 0 && x > self.prompt.len() {
|
2020-01-07 09:48:09 +00:00
|
|
|
let (before_cursor, mut after_cursor) = cmd.split_at(x - 1 - self.prompt.len());
|
|
|
|
if after_cursor.len() > 0 {
|
|
|
|
after_cursor = &after_cursor[1..];
|
|
|
|
}
|
|
|
|
self.cmd.clear();
|
2020-01-20 09:44:59 +00:00
|
|
|
self.cmd.push_str(before_cursor);
|
|
|
|
self.cmd.push_str(after_cursor);
|
2020-01-07 09:48:09 +00:00
|
|
|
kernel::vga::clear_row();
|
|
|
|
print!("{}{}", self.prompt, self.cmd);
|
|
|
|
kernel::vga::set_cursor_position(x - 1, y);
|
|
|
|
kernel::vga::set_writer_position(x - 1, y);
|
|
|
|
}
|
|
|
|
},
|
2019-12-31 17:16:52 +00:00
|
|
|
c => {
|
2020-02-07 17:36:27 +00:00
|
|
|
self.update_history();
|
2020-01-25 09:48:06 +00:00
|
|
|
self.update_autocomplete();
|
2020-01-23 20:30:53 +00:00
|
|
|
if c.is_ascii() && kernel::vga::is_printable(c as u8) {
|
2020-01-07 09:48:09 +00:00
|
|
|
let cmd = self.cmd.clone();
|
|
|
|
let (before_cursor, after_cursor) = cmd.split_at(x - self.prompt.len());
|
|
|
|
self.cmd.clear();
|
2020-01-20 09:44:59 +00:00
|
|
|
self.cmd.push_str(before_cursor);
|
|
|
|
self.cmd.push(c);
|
|
|
|
self.cmd.push_str(after_cursor);
|
2020-01-07 09:48:09 +00:00
|
|
|
kernel::vga::clear_row();
|
|
|
|
print!("{}{}", self.prompt, self.cmd);
|
|
|
|
kernel::vga::set_cursor_position(x + 1, y);
|
|
|
|
kernel::vga::set_writer_position(x + 1, y);
|
2019-12-31 12:10:03 +00:00
|
|
|
}
|
|
|
|
},
|
2019-12-29 09:34:08 +00:00
|
|
|
}
|
2019-12-31 12:10:03 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-02-07 17:36:27 +00:00
|
|
|
// Called when a key other than up or down is pressed while in history
|
|
|
|
// mode. The history index point to a command that will be selected and
|
|
|
|
// the index will be reset to the length of the history vector to signify
|
|
|
|
// that the editor is no longer in history mode.
|
|
|
|
pub fn update_history(&mut self) {
|
|
|
|
if self.history_index != self.history.len() {
|
|
|
|
self.cmd = self.history[self.history_index].clone();
|
|
|
|
self.history_index = self.history.len();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn load_history(&mut self) {
|
|
|
|
let username = "admin"; // TODO: The login command should write the username somewhere
|
|
|
|
let pathname = format!("/usr/{}/.shell_history", username);
|
|
|
|
|
|
|
|
if let Some(file) = kernel::fs::File::open(&pathname) {
|
|
|
|
let contents = file.read_to_string();
|
|
|
|
for line in contents.split('\n') {
|
|
|
|
let cmd = line.trim();
|
|
|
|
if cmd.len() > 0 {
|
|
|
|
self.history.push(cmd.into());
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
self.history_index = self.history.len();
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn save_history(&mut self) {
|
|
|
|
let username = "admin"; // TODO: The login command should write the username somewhere
|
|
|
|
let pathname = format!("/usr/{}/.shell_history", username);
|
|
|
|
|
|
|
|
let mut contents = String::new();
|
|
|
|
for cmd in &self.history {
|
|
|
|
contents.push_str(&format!("{}\n", cmd));
|
|
|
|
}
|
|
|
|
|
|
|
|
let mut file = match kernel::fs::File::open(&pathname) {
|
|
|
|
Some(file) => file,
|
|
|
|
None => kernel::fs::File::create(&pathname).unwrap(),
|
|
|
|
};
|
|
|
|
|
|
|
|
file.write(&contents.as_bytes()).unwrap();
|
|
|
|
}
|
|
|
|
|
2020-01-25 09:48:06 +00:00
|
|
|
pub fn print_autocomplete(&mut self) {
|
|
|
|
let mut args = self.parse(&self.cmd);
|
|
|
|
let i = args.len() - 1;
|
|
|
|
if self.autocomplete_index == 0 {
|
|
|
|
if args.len() == 1 {
|
|
|
|
// Autocomplete command
|
|
|
|
let autocomplete_commands = vec![ // TODO: scan /bin
|
|
|
|
"copy", "delete", "edit", "help", "move", "print", "quit", "read", "write", "sleep", "clear"
|
|
|
|
];
|
|
|
|
self.autocomplete = vec![args[i].into()];
|
|
|
|
for cmd in autocomplete_commands {
|
|
|
|
if cmd.starts_with(args[i]) {
|
|
|
|
self.autocomplete.push(cmd.into());
|
|
|
|
}
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
// Autocomplete path
|
|
|
|
let dirname = kernel::fs::dirname(args[i]);
|
|
|
|
let filename = kernel::fs::filename(args[i]);
|
2020-02-02 17:53:55 +00:00
|
|
|
self.autocomplete = vec![args[i].into()];
|
2020-01-25 09:48:06 +00:00
|
|
|
if let Some(dir) = kernel::fs::Dir::open(dirname) {
|
|
|
|
for entry in dir.read() {
|
|
|
|
if entry.name().starts_with(filename) {
|
|
|
|
let mut path = String::new();
|
|
|
|
path.push_str(dirname);
|
|
|
|
if path != "/" {
|
|
|
|
path.push('/');
|
|
|
|
}
|
|
|
|
path.push_str(&entry.name());
|
|
|
|
self.autocomplete.push(path);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
self.autocomplete_index = (self.autocomplete_index + 1) % self.autocomplete.len();
|
|
|
|
args[i] = &self.autocomplete[self.autocomplete_index];
|
|
|
|
|
|
|
|
let cmd = args.join(" ");
|
|
|
|
kernel::vga::clear_row();
|
|
|
|
print!("{}{}", self.prompt, cmd);
|
|
|
|
}
|
|
|
|
|
2020-02-07 17:36:27 +00:00
|
|
|
// Called when a key other than tab is pressed while in autocomplete mode.
|
|
|
|
// The autocomplete index point to an argument that will be added to the
|
|
|
|
// command and the index will be reset to signify that the editor is no
|
|
|
|
// longer in autocomplete mode.
|
2020-01-25 09:48:06 +00:00
|
|
|
pub fn update_autocomplete(&mut self) {
|
|
|
|
if self.autocomplete_index != 0 {
|
|
|
|
let mut args = self.parse(&self.cmd);
|
|
|
|
let i = args.len() - 1;
|
|
|
|
args[i] = &self.autocomplete[self.autocomplete_index];
|
|
|
|
self.cmd = args.join(" ");
|
|
|
|
self.autocomplete_index = 0;
|
|
|
|
self.autocomplete = vec!["".into()];
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-01-20 09:44:59 +00:00
|
|
|
pub fn parse<'a>(&self, cmd: &'a str) -> Vec<&'a str> {
|
|
|
|
//let args: Vec<&str> = cmd.split_whitespace().collect();
|
|
|
|
let mut args: Vec<&str> = Vec::new();
|
2020-01-06 21:26:48 +00:00
|
|
|
let mut i = 0;
|
|
|
|
let mut n = cmd.len();
|
|
|
|
let mut is_quote = false;
|
|
|
|
|
|
|
|
for (j, c) in cmd.char_indices() {
|
2020-01-12 08:19:16 +00:00
|
|
|
if c == '#' && !is_quote {
|
|
|
|
n = j; // Discard comments
|
|
|
|
break;
|
|
|
|
} else if c == ' ' && !is_quote {
|
2020-01-06 21:26:48 +00:00
|
|
|
if i != j {
|
2020-01-20 09:44:59 +00:00
|
|
|
args.push(&cmd[i..j]);
|
2020-01-06 21:26:48 +00:00
|
|
|
}
|
|
|
|
i = j + 1;
|
|
|
|
} else if c == '"' {
|
|
|
|
is_quote = !is_quote;
|
|
|
|
if !is_quote {
|
2020-01-20 09:44:59 +00:00
|
|
|
args.push(&cmd[i..j]);
|
2020-01-06 21:26:48 +00:00
|
|
|
}
|
|
|
|
i = j + 1;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if i < n {
|
|
|
|
if is_quote {
|
|
|
|
n -= 1;
|
|
|
|
}
|
2020-01-20 09:44:59 +00:00
|
|
|
args.push(&cmd[i..n]);
|
2020-01-06 21:26:48 +00:00
|
|
|
}
|
|
|
|
|
2020-01-25 09:48:06 +00:00
|
|
|
if n == 0 || cmd.chars().last().unwrap() == ' ' {
|
|
|
|
args.push("");
|
|
|
|
}
|
|
|
|
|
2020-01-06 21:26:48 +00:00
|
|
|
args
|
|
|
|
}
|
|
|
|
|
2020-01-01 08:07:09 +00:00
|
|
|
pub fn exec(&self, cmd: &str) -> ExitCode {
|
2020-01-06 21:26:48 +00:00
|
|
|
let args = self.parse(cmd);
|
|
|
|
|
2020-01-01 08:07:09 +00:00
|
|
|
match args[0] {
|
2020-01-25 09:48:06 +00:00
|
|
|
"" => ExitCode::CommandSuccessful,
|
2020-01-17 18:55:09 +00:00
|
|
|
"a" | "alias" => ExitCode::CommandUnknown,
|
|
|
|
"b" => ExitCode::CommandUnknown,
|
|
|
|
"c" | "copy" => user::copy::main(&args),
|
|
|
|
"d" | "del" | "delete" => user::delete::main(&args),
|
|
|
|
"e" | "edit" => user::editor::main(&args),
|
|
|
|
"f" | "find" => ExitCode::CommandUnknown,
|
|
|
|
"g" | "go" => ExitCode::CommandUnknown,
|
2020-01-17 19:10:27 +00:00
|
|
|
"h" | "help" => user::help::main(&args),
|
2020-01-17 18:55:09 +00:00
|
|
|
"i" => ExitCode::CommandUnknown,
|
|
|
|
"j" | "jump" => ExitCode::CommandUnknown,
|
|
|
|
"k" | "kill" => ExitCode::CommandUnknown,
|
|
|
|
"l" | "list" => user::list::main(&args),
|
|
|
|
"m" | "move" => user::r#move::main(&args),
|
|
|
|
"n" => ExitCode::CommandUnknown,
|
|
|
|
"o" => ExitCode::CommandUnknown,
|
|
|
|
"p" | "print" => user::print::main(&args),
|
|
|
|
"q" | "quit" | "exit" => ExitCode::ShellExit,
|
|
|
|
"r" | "read" => user::read::main(&args),
|
|
|
|
"s" => ExitCode::CommandUnknown,
|
|
|
|
"t" => ExitCode::CommandUnknown,
|
|
|
|
"u" => ExitCode::CommandUnknown,
|
|
|
|
"v" => ExitCode::CommandUnknown,
|
|
|
|
"w" | "write" => user::write::main(&args),
|
|
|
|
"x" => ExitCode::CommandUnknown,
|
|
|
|
"y" => ExitCode::CommandUnknown,
|
|
|
|
"z" => ExitCode::CommandUnknown,
|
|
|
|
"shell" => user::shell::main(&args),
|
|
|
|
"sleep" => user::sleep::main(&args),
|
|
|
|
"clear" => user::clear::main(&args),
|
|
|
|
"login" => user::login::main(&args),
|
|
|
|
"base64" => user::base64::main(&args),
|
|
|
|
"halt" => user::halt::main(&args),
|
2020-02-02 17:55:20 +00:00
|
|
|
"hex" => user::hex::main(&args), // TODO: Rename to `dump`
|
|
|
|
"net" => user::net::main(&args),
|
|
|
|
"route" => user::route::main(&args),
|
|
|
|
"dhcp" => user::dhcp::main(&args),
|
|
|
|
"http" => user::http::main(&args),
|
2020-02-06 19:26:08 +00:00
|
|
|
"tcp" => user::tcp::main(&args),
|
2020-02-02 17:55:20 +00:00
|
|
|
"host" => user::host::main(&args),
|
|
|
|
"ip" => user::ip::main(&args),
|
2020-02-03 08:45:53 +00:00
|
|
|
"geotime" => user::geotime::main(&args),
|
2020-02-03 08:33:30 +00:00
|
|
|
"colors" => user::colors::main(&args),
|
2020-01-17 18:55:09 +00:00
|
|
|
_ => ExitCode::CommandUnknown,
|
2020-01-01 08:07:09 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-12-31 12:10:03 +00:00
|
|
|
fn print_prompt(&self) {
|
2020-01-07 09:48:09 +00:00
|
|
|
print!("\n{}", self.prompt);
|
2019-12-28 17:08:11 +00:00
|
|
|
}
|
|
|
|
}
|
2020-01-01 08:07:24 +00:00
|
|
|
|
|
|
|
pub fn main(args: &[&str]) -> ExitCode {
|
|
|
|
let mut shell = Shell::new();
|
|
|
|
match args.len() {
|
|
|
|
1 => {
|
|
|
|
return shell.run();
|
|
|
|
},
|
|
|
|
2 => {
|
|
|
|
let pathname = args[1];
|
|
|
|
if let Some(file) = kernel::fs::File::open(pathname) {
|
2020-01-17 18:52:48 +00:00
|
|
|
for line in file.read_to_string().split("\n") {
|
2020-01-01 08:55:33 +00:00
|
|
|
if line.len() > 0 {
|
|
|
|
shell.exec(line);
|
|
|
|
}
|
2020-01-01 08:07:24 +00:00
|
|
|
}
|
|
|
|
ExitCode::CommandSuccessful
|
|
|
|
} else {
|
|
|
|
print!("File not found '{}'\n", pathname);
|
|
|
|
ExitCode::CommandError
|
|
|
|
}
|
|
|
|
},
|
|
|
|
_ => {
|
|
|
|
ExitCode::CommandError
|
|
|
|
},
|
|
|
|
}
|
|
|
|
}
|