1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
use std::collections::HashMap;
use crate::{CmdResult, FunResult};
use std::io::{Error, ErrorKind};
use crate::sym_table::resolve_name;
use crate::parser::{parse_cmds, parse_argv, parse_pipes};
use crate::process::Process;
#[macro_export]
macro_rules! run_fun {
($($cur:tt)*) => {
$crate::cmd_fun::run_fun_with_sym_table(
&$crate::source_text!(run_fun),
&$crate::parse_sym_table!($($cur)*),
&file!(),
line!())
};
}
#[macro_export]
macro_rules! run_cmd {
($($cur:tt)*) => {
$crate::cmd_fun::run_cmd_with_sym_table(
&$crate::source_text!(run_cmd),
&$crate::parse_sym_table!($($cur)*),
&file!(),
line!())
};
}
#[doc(hidden)]
pub fn run_fun_with_sym_table(
fun: &str,
sym_table: &HashMap<String, String>,
file: &str,
line: u32,
) -> FunResult {
run_fun(&resolve_name(&fun, &sym_table, &file, line))
}
#[doc(hidden)]
pub fn run_cmd_with_sym_table(
cmd: &str,
sym_table: &HashMap<String, String>,
file: &str,
line: u32,
) -> CmdResult {
run_cmd(&resolve_name(&cmd, &sym_table, &file, line))
}
#[doc(hidden)]
pub fn run_fun(cmds: &str) -> FunResult {
run_pipe_fun(cmds)
}
#[doc(hidden)]
pub fn run_cmd(cmds: &str) -> CmdResult {
let cmd_args = parse_cmds(cmds);
let cmd_argv = parse_argv(cmd_args);
let mut cd_opt: Option<String> = None;
for cmd in cmd_argv {
if let Err(e) = run_pipe_cmd(&cmd, &mut cd_opt) {
return Err(e);
}
}
Ok(())
}
fn run_pipe_cmd(full_command: &str, cd_opt: &mut Option<String>) -> CmdResult {
let pipe_args = parse_pipes(full_command.trim());
let pipe_argv = parse_argv(pipe_args);
let mut pipe_iter = pipe_argv[0].split_whitespace();
let cmd = pipe_iter.next().unwrap();
if cmd == "cd" || cmd == "lcd" {
let dir = pipe_iter.next().unwrap().trim();
if pipe_iter.next() != None {
let err = Error::new(
ErrorKind::Other,
format!("{} format wrong: {}", cmd, full_command),
);
return Err(err);
} else {
if cmd == "cd" {
return std::env::set_current_dir(dir);
} else {
*cd_opt = Some(dir.into());
return Ok(());
}
}
} else if cmd == "pwd" {
let pwd = std::env::current_dir()?;
println!("{}", pwd.display());
return Ok(());
}
let mut last_proc = Process::new(pipe_argv[0].clone());
if let Some(dir) = cd_opt {
last_proc.current_dir(dir.clone());
}
for pipe_cmd in pipe_argv.iter().skip(1) {
last_proc.pipe(pipe_cmd.clone());
}
last_proc.wait::<CmdResult>()
}
fn run_pipe_fun(full_command: &str) -> FunResult {
let pipe_args = parse_pipes(full_command.trim());
let pipe_argv = parse_argv(pipe_args);
let mut pipe_iter = pipe_argv[0].split_whitespace();
let cmd = pipe_iter.next().unwrap();
if cmd == "pwd" {
let pwd = std::env::current_dir()?;
return Ok(format!("{}", pwd.display()));
}
let mut last_proc = Process::new(pipe_argv[0].clone());
for pipe_cmd in pipe_argv.iter().skip(1) {
last_proc.pipe(pipe_cmd.clone());
}
last_proc.wait::<FunResult>()
}