16e297d90SMemoryShore #![allow(non_snake_case)] 26f50094aSMemoryShore extern crate libc; 36f50094aSMemoryShore 46f50094aSMemoryShore #[macro_use] 56f50094aSMemoryShore extern crate lazy_static; 66f50094aSMemoryShore 781c61261SMemoryShore #[macro_use] 881c61261SMemoryShore extern crate num_derive; 981c61261SMemoryShore 10*ca2bc756S裕依2439 mod shell; 11*ca2bc756S裕依2439 12*ca2bc756S裕依2439 use num_enum::TryFromPrimitive; 13*ca2bc756S裕依2439 use shell::Shell; 146f50094aSMemoryShore use std::{ 156f50094aSMemoryShore collections::HashMap, 166f50094aSMemoryShore fs::File, 176f50094aSMemoryShore io::{Read, Write}, 186f50094aSMemoryShore path::Path, 196f50094aSMemoryShore string::String, 206f50094aSMemoryShore vec::Vec, 216f50094aSMemoryShore }; 226f50094aSMemoryShore 236f50094aSMemoryShore pub const ROOT_PATH: &str = "/"; 24*ca2bc756S裕依2439 pub const ENV_FILE_PATH: &str = "/etc/profile"; 256f50094aSMemoryShore 2681c61261SMemoryShore #[repr(u8)] 2781c61261SMemoryShore #[derive(Debug, FromPrimitive, TryFromPrimitive, ToPrimitive, PartialEq, Eq, Clone)] 2881c61261SMemoryShore #[allow(dead_code)] 2981c61261SMemoryShore pub enum SpecialKeycode { 3081c61261SMemoryShore LF = b'\n', 3181c61261SMemoryShore CR = b'\r', 3281c61261SMemoryShore Delete = b'\x7f', 3381c61261SMemoryShore BackSpace = b'\x08', 3481c61261SMemoryShore Tab = b'\t', 356f50094aSMemoryShore 3681c61261SMemoryShore FunctionKey = 0xE0, 3781c61261SMemoryShore PauseBreak = 0xE1, 3881c61261SMemoryShore 3981c61261SMemoryShore Up = 0x48, 4081c61261SMemoryShore Down = 0x50, 4181c61261SMemoryShore Left = 0x4B, 4281c61261SMemoryShore Right = 0x4D, 4381c61261SMemoryShore 4481c61261SMemoryShore Home = 0x47, 4581c61261SMemoryShore End = 0x4F, 4681c61261SMemoryShore } 4781c61261SMemoryShore 4881c61261SMemoryShore impl Into<u8> for SpecialKeycode { 4981c61261SMemoryShore fn into(self) -> u8 { 5081c61261SMemoryShore self as u8 5181c61261SMemoryShore } 526f50094aSMemoryShore } 536f50094aSMemoryShore 546f50094aSMemoryShore struct Env(std::collections::HashMap<String, String>); 556f50094aSMemoryShore 566f50094aSMemoryShore lazy_static! { 576f50094aSMemoryShore static ref ENV: std::sync::Mutex<Env> = std::sync::Mutex::new(Env(HashMap::new())); 586f50094aSMemoryShore } 596f50094aSMemoryShore 606f50094aSMemoryShore impl Env { 61*ca2bc756S裕依2439 /// 初始化环境变量文件 62*ca2bc756S裕依2439 fn init_envfile() { 63*ca2bc756S裕依2439 let mut file = File::create(ENV_FILE_PATH).unwrap(); 646f50094aSMemoryShore file.write_all("PATH=/bin:/usr/bin:/usr/local/bin\n".as_bytes()) 656f50094aSMemoryShore .unwrap(); 666f50094aSMemoryShore file.write_all("PWD=/\n".as_bytes()).unwrap(); 676f50094aSMemoryShore } 686f50094aSMemoryShore 69*ca2bc756S裕依2439 /// 读取环境变量文件 70*ca2bc756S裕依2439 /// 如果文件不存在则创建 716f50094aSMemoryShore fn read_env() { 7281c61261SMemoryShore let mut env = ENV.lock().unwrap(); 73*ca2bc756S裕依2439 if !Path::new(ENV_FILE_PATH).exists() { 74*ca2bc756S裕依2439 Env::init_envfile(); 75*ca2bc756S裕依2439 } 76*ca2bc756S裕依2439 let mut file = File::open(ENV_FILE_PATH).unwrap(); 776f50094aSMemoryShore let mut buf: Vec<u8> = Vec::new(); 786f50094aSMemoryShore file.read_to_end(&mut buf).unwrap(); 796f50094aSMemoryShore for (name, value) in String::from_utf8(buf) 806f50094aSMemoryShore .unwrap() 816f50094aSMemoryShore .split('\n') 826f50094aSMemoryShore .filter_map(|str| { 836f50094aSMemoryShore let v = str.split('=').collect::<Vec<&str>>(); 846f50094aSMemoryShore if v.len() == 2 && !v.contains(&"") { 856f50094aSMemoryShore Some((*v.get(0).unwrap(), *v.get(1).unwrap())) 866f50094aSMemoryShore } else { 876f50094aSMemoryShore None 886f50094aSMemoryShore } 896f50094aSMemoryShore }) 906f50094aSMemoryShore .collect::<Vec<(&str, &str)>>() 916f50094aSMemoryShore { 9281c61261SMemoryShore env.0.insert(String::from(name), String::from(value)); 936f50094aSMemoryShore } 946f50094aSMemoryShore } 956f50094aSMemoryShore 966f50094aSMemoryShore fn get(key: &String) -> Option<String> { 976f50094aSMemoryShore let env = &mut ENV.lock().unwrap().0; 986f50094aSMemoryShore env.get(key).map(|value| value.clone()) 996f50094aSMemoryShore } 1006f50094aSMemoryShore 1016f50094aSMemoryShore fn insert(key: String, value: String) { 1026f50094aSMemoryShore ENV.lock().unwrap().0.insert(key, value); 1036f50094aSMemoryShore } 1046f50094aSMemoryShore 1056f50094aSMemoryShore fn path() -> Vec<String> { 1066f50094aSMemoryShore let env = &ENV.lock().unwrap().0; 1076f50094aSMemoryShore let paths = env.get("PATH").unwrap(); 1086f50094aSMemoryShore paths 1096f50094aSMemoryShore .split(':') 1106f50094aSMemoryShore .filter_map(|str| { 1116f50094aSMemoryShore let path = String::from(str); 1126f50094aSMemoryShore if Path::new(&path).is_dir() { 1136f50094aSMemoryShore Some(path) 1146f50094aSMemoryShore } else { 1156f50094aSMemoryShore None 1166f50094aSMemoryShore } 1176f50094aSMemoryShore }) 1186f50094aSMemoryShore .collect::<Vec<String>>() 1196f50094aSMemoryShore } 1206f50094aSMemoryShore } 1216f50094aSMemoryShore 1226f50094aSMemoryShore fn main() { 1236f50094aSMemoryShore Env::read_env(); 124*ca2bc756S裕依2439 let mut shell = Shell::new(); 1256f50094aSMemoryShore shell.exec(); 1266f50094aSMemoryShore return; 1276f50094aSMemoryShore } 128