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