Skip to content

Commit e5541bf

Browse files
fix error & update 1.0.4
1 parent e4fd486 commit e5541bf

File tree

12 files changed

+138
-41
lines changed

12 files changed

+138
-41
lines changed

Cargo.lock

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

command/Cargo.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
[package]
22
name = "command"
3-
version = "1.0.3"
3+
version = "1.0.4"
44
edition = "2021"
55
authors = ["Zhangzijie <[email protected]>"]
66
repository = "https://github.com/zhangzijie-pro/Tiks.git"

command/bin/main.rs

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,6 @@
66
// There is some error or other suggestions contact me : [email protected]
77
// Cargo run
88

9-
109
use command::env::init_env;
1110
use command::run::init_shell;
1211
use command::start::start_logo::start_logo;

command/src/commands/arg.rs

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -125,7 +125,6 @@ pub fn execute_command(command: &str, option: &str, arg: &Vec<String>, session_c
125125
_=>{
126126
if session_context.user_state.root.check_permission() {
127127
session_context.user_state.exit_root();
128-
println!("Switched to root mode: {}", session_context.user_state.root.check_permission());
129128
} else {
130129
std::process::exit(0);
131130
}
@@ -145,7 +144,7 @@ pub fn execute_command(command: &str, option: &str, arg: &Vec<String>, session_c
145144
}
146145
_=>Ok((0,"help:\n -i|-install: install package \n -u|-update: update this version".to_string()))
147146
},
148-
//
147+
"export" => set(&arg[0], &arg[1]),
149148
"whoami" => whoami(session_context),
150149
"pd" => match option{ // match arg empty
151150
"-f"|"-fix" => match arg.is_empty(){
@@ -173,7 +172,7 @@ pub fn execute_other_command(command: &str, option: &str, arg: &[String]) -> Res
173172
match command {
174173
"help" => Ok((0,help())),
175174
"pwd" => pwd(),
176-
"time" => get_time(),
175+
"time" => Ok(get_time()),
177176
"history" => history(),
178177
// test C define here
179178
"hello_c" => {

command/src/commands/command.rs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -465,11 +465,11 @@ pub fn sudo(session_context: &mut SessionContext)->io::Result<(usize,String)>{
465465

466466

467467
// get time
468-
pub fn get_time() -> io::Result<(usize,String)>{
468+
pub fn get_time() -> (usize,String){
469469
let now = chrono::Local::now();
470470
let time = now.format("%Y-%m-%d %H:%M:%S").to_string();
471471

472-
Ok((STATUE_CODE,time))
472+
(STATUE_CODE,time)
473473
}
474474

475475
// grep
@@ -538,7 +538,7 @@ pub fn pipe(command:Vec<String>) -> io::Result<(usize,String)>{
538538
}
539539

540540
// &&
541-
pub fn and(command:Vec<String>,session_context: &mut SessionContext) -> Vec<String>{
541+
pub fn and(command:Vec<String>,session_context: &mut SessionContext) -> Vec<(usize,String)>{
542542
let mut output:Vec<_> = Vec::new();
543543
let commands = command.split(|x| x=="&&");
544544
for c in commands{
@@ -550,7 +550,7 @@ pub fn and(command:Vec<String>,session_context: &mut SessionContext) -> Vec<Stri
550550
}
551551

552552
// &
553-
pub fn priority_run(command:Vec<String>,session_context: &mut SessionContext) -> Vec<String>{
553+
pub fn priority_run(command:Vec<String>,session_context: &mut SessionContext) -> Vec<(usize,String)>{
554554
let mut output:Vec<_> = Vec::new();
555555
let commands = command.split(|x| x=="&");
556556
let mut save_command = Vec::new();

command/src/lib.rs

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,4 +7,11 @@ pub mod signal;
77
mod priority;
88
pub mod env;
99
mod test;
10-
pub mod start;
10+
pub mod start;
11+
mod tauri_func;
12+
13+
pub use crate::root::SESSION;
14+
pub use crate::start::*;
15+
pub use crate::tauri_func::run_command;
16+
pub use crate::commands::command::{HISTROY,history_push,pwd,whoami,get_time};
17+
pub use crate::set::set::home_dir;

command/src/root.rs

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
11
// build root
2-
32
pub struct UserState{
43
pub root: UserRole
54
}

command/src/run.rs

Lines changed: 24 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -28,8 +28,10 @@ pub fn handle_command(args: Vec<String>) -> (Commands,usize,usize,CommandPriorit
2828
}
2929

3030

31-
pub fn run(args: Vec<String>,session_context: &mut SessionContext) -> String{
32-
let (commands,pid,tid,priority) = handle_command(args);
31+
pub fn run<T>(input: T,session_context: &mut SessionContext) -> (usize,String)
32+
where Vec<String>: From<T>
33+
{
34+
let (commands,pid,tid,priority) = handle_command(input.into());
3335
let semaphore = semaphore_new();
3436
let mut tcb = ThreadControlBlock::new();
3537
let mut pcb = ProcessManager::new();
@@ -49,17 +51,17 @@ pub fn run(args: Vec<String>,session_context: &mut SessionContext) -> String{
4951
"ps" => {
5052
pcb.kill(pid);
5153
tcb.stop_thread(priority_tid);
52-
ps()
54+
(tid,ps())
5355
}
5456
"kill" => {
5557
tcb.stop_thread(priority_tid);
5658
pcb.kill(pid);
57-
pcb.kill(arg[0].parse::<usize>().unwrap())
59+
(tid,pcb.kill(arg[0].parse::<usize>().unwrap()))
5860
},
5961
"sleep" => {
6062
pcb.kill(pid);
6163
tcb.stop_thread(priority_tid);
62-
sleep(&mut tcb, commands.arg[0].parse::<usize>().unwrap())
64+
(tid,sleep(&mut tcb, commands.arg[0].parse::<usize>().unwrap()))
6365
}
6466
_ =>{
6567
// start process and thread
@@ -70,41 +72,36 @@ pub fn run(args: Vec<String>,session_context: &mut SessionContext) -> String{
7072
let status = res.0.clone();
7173
let result = res.1.clone();
7274
if status==0{
73-
//println!("[{}] Done\n{}",tid,result);
7475
pcb.kill(pid);
7576
tcb.stop_thread(priority_tid);
76-
result
77+
(tid,result)
7778
}else{
78-
error_log(res.1.clone());
79-
res.1
79+
error_log(res.1.clone());
80+
(tid,result)
8081
}
81-
}else{String::new()}
82+
}else{(tid,String::from("Error: Not found"))}
8283
} else if !session_context.user_state.root.check_permission() && !session_context.root.allowed_commands.contains(&commands.command) {
8384
// Execute normal commands
8485
// Handle commands normally when user is not in root mode
8586
if let Ok(res) = command_match(commands, session_context) {
8687
let status = res.0.clone();
8788
let result = res.1.clone();
8889
if status==0{
89-
//println!("[{}] Done\n{}",tid,result);
9090
pcb.kill(pid);
9191
tcb.stop_thread(priority_tid);
92-
result
92+
(tid,result)
9393
}else{
9494
error_log(res.1.clone());
95-
//println!("{}",res.1);
96-
result
95+
(tid,result)
9796
}
9897
}else {
99-
String::new()
98+
(tid,String::from("Error: Not found"))
10099
}
101100
}else{
102-
eprintln!("Permission not support");
103-
String::new()
101+
(tid,String::from("Error: Permission not support"))
104102
}
105103
}
106104
}
107-
108105
}
109106

110107

@@ -147,20 +144,21 @@ pub fn init_shell(session_context: &mut SessionContext){
147144
let args: Box<Vec<String>> = Box::new(line.split_whitespace().map(|s| s.to_string()).collect());
148145
if args.contains(&"&&".to_string()) {
149146
let res = and(*args, session_context);
150-
for r in res{
151-
println!("Done[1]: {r}")
147+
for (t,r) in res{
148+
println!("Done[{t}]: \n{r}")
152149
}
153150
} else if args.contains(&"|".to_string()) {
154-
let s = pipe(*args).unwrap();
155-
println!("{}", s.1);
151+
let res = pipe(*args).unwrap();
152+
println!("{}",res.1)
156153
} else if args.contains(&"&".to_string()) {
157154
let res = priority_run(*args, session_context);
158-
for r in res{
159-
println!("Done[1]: {r}")
155+
for (t,r) in res{
156+
println!("Done[{t}]: \n{r}")
157+
160158
}
161159
} else {
162-
let res = run(*args, session_context);
163-
println!("{res}")
160+
let (t,res) = run(*args, session_context);
161+
println!("Done[{t}]: \n{res}")
164162
}
165163

166164
}

command/src/set/set.rs

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -105,7 +105,7 @@ fn get_home_err() -> String{
105105
}
106106

107107
pub fn error_log(err: String){
108-
let time = get_time().unwrap().1;
108+
let time = get_time().1;
109109
let home_path = get_home_err();
110110
let path = Path::new(&home_path);
111111

@@ -134,4 +134,9 @@ pub fn error_log(err: String){
134134
return;
135135
}
136136
// error.log -> $HOME/.Tiks/error.log -> [time]: [Error]
137+
}
138+
139+
pub fn home_dir() -> String{
140+
let binding= dirs::home_dir().unwrap();
141+
binding.as_os_str().to_str().map(|x| x.to_string()).unwrap()
137142
}

command/src/tauri_func/mod.rs

Lines changed: 89 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,89 @@
1+
use crate::root::SessionContext;
2+
use crate::commands::arg::{split, Commands,command_match};
3+
use crate::process::{add_task::*, ps, sleep};
4+
use crate::process::process::ProcessManager;
5+
use crate::process::thread::ThreadControlBlock;
6+
use crate::run::handle_command;
7+
use crate::set::set::error_log;
8+
use crate::signal::semaphore_new;
9+
10+
#[allow(dead_code)]
11+
pub fn string_to_command(string: String) -> Commands{
12+
Commands::from_string(string)
13+
}
14+
15+
pub fn run_command<T>(input: T,session_context: &mut SessionContext) -> (usize,String)
16+
where Vec<String>: From<T>
17+
{
18+
let (commands,pid,tid,priority) = handle_command(input.into());
19+
let semaphore = semaphore_new();
20+
let mut tcb = ThreadControlBlock::new();
21+
let mut pcb = ProcessManager::new();
22+
23+
24+
let (command,_option,arg) = split(commands.clone());
25+
26+
add_command_to_thread(tid, command.clone(), priority, &mut tcb);
27+
add_thread_to_process(pid, command.clone(), tcb.clone(), semaphore, &mut pcb);
28+
29+
let priority_tid = tcb.get_highest_priority_thread().unwrap();
30+
tcb.start_thread(priority_tid);
31+
pcb.start_process(pid);
32+
33+
34+
match command.as_str(){
35+
"ps" => {
36+
pcb.kill(pid);
37+
tcb.stop_thread(priority_tid);
38+
(tid,ps())
39+
}
40+
"kill" => {
41+
tcb.stop_thread(priority_tid);
42+
pcb.kill(pid);
43+
(tid,pcb.kill(arg[0].parse::<usize>().unwrap()))
44+
},
45+
"sleep" => {
46+
pcb.kill(pid);
47+
tcb.stop_thread(priority_tid);
48+
(tid,sleep(&mut tcb, commands.arg[0].parse::<usize>().unwrap()))
49+
}
50+
_ =>{
51+
// start process and thread
52+
if session_context.user_state.root.check_permission(){
53+
// Execute root commands
54+
// Handle commands differently when user is in root mode
55+
if let Ok(res) = command_match(commands, session_context){
56+
let status = res.0.clone();
57+
let result = res.1.clone();
58+
if status==0{
59+
pcb.kill(pid);
60+
tcb.stop_thread(priority_tid);
61+
(tid,result)
62+
}else{
63+
error_log(res.1.clone());
64+
(tid,result)
65+
}
66+
}else{(tid,String::from("Error: Not found"))}
67+
} else if !session_context.user_state.root.check_permission() && !session_context.root.allowed_commands.contains(&commands.command) {
68+
// Execute normal commands
69+
// Handle commands normally when user is not in root mode
70+
if let Ok(res) = command_match(commands, session_context) {
71+
let status = res.0.clone();
72+
let result = res.1.clone();
73+
if status==0{
74+
pcb.kill(pid);
75+
tcb.stop_thread(priority_tid);
76+
(tid,result)
77+
}else{
78+
error_log(res.1.clone());
79+
(tid,result)
80+
}
81+
}else {
82+
(tid,String::from("Error: Not found"))
83+
}
84+
}else{
85+
(tid,String::from("Error: Permission not support"))
86+
}
87+
}
88+
}
89+
}

0 commit comments

Comments
 (0)