-
Notifications
You must be signed in to change notification settings - Fork 0
/
gash.rs
241 lines (216 loc) · 6.7 KB
/
gash.rs
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
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
extern mod extra;
use std::{io, run, os, path, task, int, str, libc};
//use extra::{deque};
static NORMAL:int = 0;
static OUTPUT_REDIRECTION:int = 1;
static OUTPUT_REDIRECTION_APPEND:int = 2;
static INPUT_REDIRECTION:int = 3;
static PIPELINE:int = 4;
fn main() {
static CMD_PROMPT: &'static str = "gash > ";
let history: @mut extra::deque::Deque<~[~str]> = @mut extra::deque::Deque::new();
loop {
print(os::getcwd().to_str() + ": " + CMD_PROMPT);
let line = io::stdin().read_line();
debug!(fmt!("line: %?", line));
let argv: ~[~str] = line.split_iter(' ').filter(|&x| x != "")
.transform(|x| x.to_owned()).collect();
debug!(fmt!("argv %?", argv));
if argv.len() > 0 {
run_program(argv.to_owned(), line.ends_with("&"), history);
history.add_front(argv);
}
}
}
fn run_program(args: ~[~str], run_in_background: bool, history: @mut extra::deque::Deque<~[~str]>) {
let mut argv = args;
let mut current_argv = ~[];
let mut argv_modes = ~[];
let mut pipe = false;
let mut pipe_program = ~"";
//Determine role of each parameter
if argv.len() > 0 {
let program = argv.remove(0);
unsafe { while argv.len() > 0 {
let sig = argv.unsafe_get(0);
match sig {
~"|" => {
//println("Found a pipe");
argv.remove(0);
pipe=true;
break;
}
~">" => {
//println("Found redirect output");
argv.remove(0);
argv_modes.push(OUTPUT_REDIRECTION);
if argv.len() > 0 { current_argv.push(argv.remove(0)); }
else{println("Syntax Error"); return;}
}
~"<" => {
//println("Found redirect input");
argv.remove(0);
argv_modes.push(INPUT_REDIRECTION);
if argv.len() > 0 { current_argv.push(argv.remove(0));}
else{println("Syntax Error"); return;}
}
_ => {
current_argv.push(argv.remove(0));
argv_modes.push(NORMAL);
}
}
} }
//println(fmt!("argv: %? \n current_argv: %? \n modes: %?", argv, current_argv, argv_modes));
assert!(current_argv.len() == argv_modes.len());
//Get Read and Write files
let mut i= 0;
let mut writefile = ~"";
let mut readfile = ~"";
while i < argv_modes.len() {
if argv_modes[i] == OUTPUT_REDIRECTION {
writefile = current_argv.remove(i);
argv_modes.remove(i);
}
else if argv_modes[i] == INPUT_REDIRECTION {
readfile = current_argv.remove(i);
argv_modes.remove(i);
}
else { i+=1; }
}
match program {
~"exit" => { return; }
~"cd" => {
if !current_argv.is_empty() {
pre_cd(current_argv.remove(0));
}
else {
cd(~os::getcwd())
}
}
~"add" => {
if !current_argv.len() >= 2 {
let result = int::from_str(current_argv.remove(0)).get()
+ int::from_str(current_argv.remove(0)).get();
println(fmt!("%i", result));
}
}
~"sub" => {
if !current_argv.len() >= 2 {
let result = int::from_str(current_argv.remove(0)).get()
- int::from_str(current_argv.remove(0)).get();
println(fmt!("%i",result));
}
}
~"mul" => {
if !current_argv.len() >= 2 {
let result = int::from_str(current_argv.remove(0)).get()
* int::from_str(current_argv.remove(0)).get();
println(fmt!("%i",result));
}
}
~"div" => {
if !current_argv.len() >= 2 {
let result = int::from_str(current_argv.remove(0)).get()
- int::from_str(current_argv.remove(0)).get();
println(fmt!("%i",result));
}
}
~"search" => {
let matcher = current_argv.remove(0);
let mut found = false;
for history.rev_iter().advance|s| {
for s.iter().advance |word| {
if word.contains(matcher) {
for s.iter().advance |pword| {print(*pword + " ") }
found = true;
println("");
}
}
}
if !found {
println("No Match Found");
}
}
~"history" => {
let mut i = 1;
for history.rev_iter().advance |s| {
print(fmt!("%i ",i));
for s.iter().advance |word| {
print(*word + " ")
}
println("");
i+=1;
}
}
_ => {
let temp = current_argv;
let r = readfile;
let w = writefile;
let mut process_options = ~run::ProcessOptions::new();
//Default stdin, stdout, stderr
process_options.in_fd = Some(0);
process_options.out_fd = Some(1);
process_options.err_fd = Some(2);
unsafe {
if w != ~"" {
let fp = std::libc::fopen(
w.as_c_str(|x| x),
"w".as_c_str(|x| x));
process_options.out_fd = Some(libc::fileno(fp));
}
if r != ~"" {
if os::path_exists(~path::Path(r)) {
let fp = std::libc::fopen(
r.as_c_str(|x| x),
"r".as_c_str(|x| x));
process_options.in_fd = Some(libc::fileno(fp));
}
}
}
//println(fmt!("%?", process_options));
if run_in_background {
task::spawn_sched(task::SingleThreaded, | | {
let mut process_options = ~run::ProcessOptions::new();
process_options.in_fd = Some(0);
process_options.out_fd = Some(1);
process_options.err_fd = Some(2);
unsafe {
if w != ~"" {
let fp = std::libc::fopen(
w.as_c_str(|x| x),
"w".as_c_str(|x| x));
process_options.out_fd = Some(libc::fileno(fp));
}
if r != ~"" {
if os::path_exists(~path::Path(r)) {
let fp = std::libc::fopen(
r.as_c_str(|x| x),
"r".as_c_str(|x| x));
process_options.in_fd = Some(libc::fileno(fp));
}
}
}
run::Process::new(program, temp, *process_options);
});
}
else {
run::Process::new(program, temp, *process_options);
}
}
}
if pipe {
run_program(argv, run_in_background, history);
}
}
}
fn pre_cd(s: ~str){
if s.starts_with("~"){ cd(~path::Path(s.slice(1, s.len()))); }
else{ cd(~path::Path(s)); }
}
fn cd(p: &Path) {
let exists:bool = os::path_exists(p);
let is_dir:bool = os::path_is_dir(p);
if exists && is_dir{ os::change_dir(p); }
else if exists{ println(fmt!("gash: cd: %s: Not a directory", p.to_str())); }
else { println(fmt!("gash: cd: %s: No such file or directory", p.to_str())); }
}