forked from redox-os/ion
/
variables.rs
289 lines (258 loc) · 8.51 KB
/
variables.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
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
// TODO: Move into grammar
use std::io::{self, Write};
use shell::status::*;
use shell::variables::Variables;
use types::*;
fn print_list(list: &VariableContext) {
let stdout = io::stdout();
let stdout = &mut stdout.lock();
for (key, value) in list {
let _ = stdout
.write(key.as_bytes())
.and_then(|_| stdout.write_all(b" = "))
.and_then(|_| stdout.write_all(value.as_bytes()))
.and_then(|_| stdout.write_all(b"\n"));
}
}
enum Binding {
InvalidKey(Identifier),
ListEntries,
KeyOnly(Identifier),
KeyValue(Identifier, Value),
Math(Identifier, Operator, f32),
MathInvalid(Value),
}
enum Operator {
Plus,
Minus,
Divide,
Multiply,
}
/// Parses alias as a `(key, value)` tuple.
fn parse_alias(args: &str) -> Binding {
// Write all the arguments into a single `String`
let mut char_iter = args.chars();
// Find the key and advance the iterator until the equals operator is found.
let mut key = "".to_owned();
let mut found_key = false;
let mut operator = None;
// Scans through characters until the key is found, then continues to scan until
// the equals operator is found.
while let Some(character) = char_iter.next() {
match character {
' ' if key.is_empty() => (),
' ' => found_key = true,
'+' => {
if char_iter.next() == Some('=') {
operator = Some(Operator::Plus);
found_key = true;
}
break;
}
'-' => {
if char_iter.next() == Some('=') {
operator = Some(Operator::Minus);
found_key = true;
}
break;
}
'*' => {
if char_iter.next() == Some('=') {
operator = Some(Operator::Multiply);
found_key = true;
}
break;
}
'/' => {
if char_iter.next() == Some('=') {
operator = Some(Operator::Divide);
found_key = true;
}
break;
}
'=' => {
found_key = true;
break;
}
_ if !found_key => key.push(character),
_ => (),
}
}
let key: Identifier = key.into();
if !found_key && key.is_empty() {
Binding::ListEntries
} else {
let value: Value = char_iter.skip_while(|&x| x == ' ').collect();
if value.is_empty() {
Binding::KeyOnly(key)
} else if !Variables::is_valid_variable_name(&key) {
Binding::InvalidKey(key)
} else {
match operator {
Some(operator) => match value.parse::<f32>() {
Ok(value) => Binding::Math(key, operator, value),
Err(_) => Binding::MathInvalid(value),
},
None => Binding::KeyValue(key, value),
}
}
}
}
/// The `alias` command will define an alias for another command, and thus may be used as a
/// command itself.
pub(crate) fn alias(vars: &mut Variables, args: &str) -> i32 {
match parse_alias(args) {
Binding::InvalidKey(key) => {
eprintln!("ion: alias name, '{}', is invalid", key);
return FAILURE;
}
Binding::KeyValue(key, value) => {
vars.aliases.insert(key, value);
}
Binding::ListEntries => print_list(&vars.aliases),
Binding::KeyOnly(key) => {
eprintln!("ion: please provide value for alias '{}'", key);
return FAILURE;
}
_ => {
eprintln!("ion: invalid alias syntax");
return FAILURE;
}
}
SUCCESS
}
/// Dropping an alias will erase it from the shell.
pub(crate) fn drop_alias<I: IntoIterator>(vars: &mut Variables, args: I) -> i32
where
I::Item: AsRef<str>,
{
let args = args.into_iter().collect::<Vec<I::Item>>();
if args.len() <= 1 {
eprintln!("ion: you must specify an alias name");
return FAILURE;
}
for alias in args.iter().skip(1) {
if vars.aliases.remove(alias.as_ref()).is_none() {
eprintln!("ion: undefined alias: {}", alias.as_ref());
return FAILURE;
}
}
SUCCESS
}
/// Dropping an array will erase it from the shell.
pub(crate) fn drop_array<I: IntoIterator>(vars: &mut Variables, args: I) -> i32
where
I::Item: AsRef<str>,
{
let args = args.into_iter().collect::<Vec<I::Item>>();
if args.len() <= 2 {
eprintln!("ion: you must specify an array name");
return FAILURE;
}
if args[1].as_ref() != "-a" {
eprintln!("ion: drop_array must be used with -a option");
return FAILURE;
}
for array in args.iter().skip(2) {
if vars.unset_array(array.as_ref()).is_none() {
eprintln!("ion: undefined array: {}", array.as_ref());
return FAILURE;
}
}
SUCCESS
}
/// Dropping a variable will erase it from the shell.
pub(crate) fn drop_variable<I: IntoIterator>(vars: &mut Variables, args: I) -> i32
where
I::Item: AsRef<str>,
{
let args = args.into_iter().collect::<Vec<I::Item>>();
if args.len() <= 1 {
eprintln!("ion: you must specify a variable name");
return FAILURE;
}
for variable in args.iter().skip(1) {
if vars.unset_var(variable.as_ref()).is_none() {
eprintln!("ion: undefined variable: {}", variable.as_ref());
return FAILURE;
}
}
SUCCESS
}
#[cfg(test)]
mod test {
use super::*;
use parser::{expand_string, Expander};
use shell::status::{FAILURE, SUCCESS};
struct VariableExpander(pub Variables);
impl Expander for VariableExpander {
fn variable(&self, var: &str, _: bool) -> Option<Value> {
self.0.get_var(var)
}
}
// TODO: Rewrite tests now that let is part of the grammar.
// #[test]
// fn let_and_expand_a_variable() {
// let mut variables = Variables::default();
// let dir_stack = new_dir_stack();
// let_(&mut variables, vec!["let", "FOO", "=", "BAR"]);
// let expanded = expand_string("$FOO", &variables, &dir_stack,
// false).join(""); assert_eq!("BAR", &expanded);
// }
//
// #[test]
// fn let_fails_if_no_value() {
// let mut variables = Variables::default();
// let return_status = let_(&mut variables, vec!["let", "FOO"]);
// assert_eq!(FAILURE, return_status);
// }
//
// #[test]
// fn let_checks_variable_name() {
// let mut variables = Variables::default();
// let return_status = let_(&mut variables, vec!["let", ",;!:", "=",
// "FOO"]); assert_eq!(FAILURE, return_status);
// }
#[test]
fn drop_deletes_variable() {
let mut variables = Variables::default();
variables.set_var("FOO", "BAR");
let return_status = drop_variable(&mut variables, vec!["drop", "FOO"]);
assert_eq!(SUCCESS, return_status);
let expanded = expand_string("$FOO", &VariableExpander(variables), false).join("");
assert_eq!("", expanded);
}
#[test]
fn drop_fails_with_no_arguments() {
let mut variables = Variables::default();
let return_status = drop_variable(&mut variables, vec!["drop"]);
assert_eq!(FAILURE, return_status);
}
#[test]
fn drop_fails_with_undefined_variable() {
let mut variables = Variables::default();
let return_status = drop_variable(&mut variables, vec!["drop", "FOO"]);
assert_eq!(FAILURE, return_status);
}
#[test]
fn drop_deletes_array() {
let mut variables = Variables::default();
variables.set_array("FOO", array!["BAR"]);
let return_status = drop_array(&mut variables, vec!["drop", "-a", "FOO"]);
assert_eq!(SUCCESS, return_status);
let expanded = expand_string("@FOO", &VariableExpander(variables), false).join("");
assert_eq!("", expanded);
}
#[test]
fn drop_array_fails_with_no_arguments() {
let mut variables = Variables::default();
let return_status = drop_array(&mut variables, vec!["drop", "-a"]);
assert_eq!(FAILURE, return_status);
}
#[test]
fn drop_array_fails_with_undefined_array() {
let mut variables = Variables::default();
let return_status = drop_array(&mut variables, vec!["drop", "FOO"]);
assert_eq!(FAILURE, return_status);
}
}