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
|
use crate::compose::Environment;
use crate::poem::{read::Readable, recite::Reciteable, Poem};
use std::fs;
use std::os::unix::process::ExitStatusExt;
use std::process::{ExitStatus, Output};
/// source
///
/// The builtin `source` command. Used to change the shell's global environment
/// state via a `.sh` or `.dwvsh` file.
///
/// # Shell Examples
/// ```sh
/// source ~/.dwvshrc
/// ```
pub fn incant(
clause: &Option<Vec<String>>,
uout: bool,
uerr: bool,
env: &mut Environment,
) -> Output {
let mut status = 0;
let mut out: Vec<u8> = Vec::new();
let mut err: Vec<u8> = Vec::new();
let files = match clause {
Some(clause) => clause,
None => {
status = 1;
if uerr {
err.append(&mut "source: not enough arguments\n".as_bytes().to_vec());
} else {
eprintln!("source: not enough arguments");
}
return Output {
status: ExitStatus::from_raw(status),
stdout: out,
stderr: err,
};
}
};
for file in files {
let poetry = match fs::read_to_string(&file) {
Ok(poetry) => poetry,
Err(e) => {
status = 127;
if uerr {
err.append(
&mut format!(
"source: could not load {}: {}\n",
file,
e.to_string().to_lowercase()
)
.as_bytes()
.to_vec(),
);
} else {
eprintln!(
"source: could not load {}: {}",
file,
e.to_string().to_lowercase()
);
}
return Output {
status: ExitStatus::from_raw(status),
stdout: out,
stderr: err,
};
}
};
let poem = match Poem::read(poetry, env) {
Ok(poem) => poem,
Err(e) => {
if uerr {
err.append(
&mut format!("dwvsh: {}", e.to_string().to_lowercase())
.as_bytes()
.to_vec(),
);
} else {
eprintln!("dwvsh: {}", e.to_string().to_lowercase());
}
continue;
}
};
status = match poem.recite(env) {
Ok(mut sout) => {
if uout {
out.append(&mut sout);
} else {
if !sout.is_empty() {
println!("{}", String::from_utf8_lossy(&sout));
}
}
0
}
Err(e) => {
if uerr {
err.append(
&mut format!("dwvsh: {}", e.to_string().to_lowercase())
.as_bytes()
.to_vec(),
);
} else {
eprintln!("dwvsh: {}", e.to_string().to_lowercase());
}
1
}
};
}
Output {
status: ExitStatus::from_raw(status),
stdout: out,
stderr: err,
}
}
|