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
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
/// Contains all functionality pertaining to parsing, tokenizing, and generating input arguments.
pub mod errors;
mod jobs;
mod man;
mod quote;
use std::env;
use std::ffi::OsStr;
use std::fs;
use std::io::{self, BufRead, BufReader};
use std::path::Path;
use std::process::exit;
use arrayvec::ArrayVec;
use permutate::Permutator;
use num_cpus;
use super::disk_buffer::{self, DiskBufferTrait};
use super::input_iterator::InputIterator;
use super::tokenizer::Token;
use self::errors::ParseErr;
// Re-export key items from internal modules.
pub use self::errors::{FileErr, InputIteratorErr};
#[derive(PartialEq)]
enum Mode { Arguments, Command, Inputs, Files }
pub const INPUTS_ARE_COMMANDS: u8 = 1;
pub const PIPE_IS_ENABLED: u8 = 2;
pub const SHELL_ENABLED: u8 = 4;
pub const QUIET_MODE: u8 = 8;
pub const VERBOSE_MODE: u8 = 16;
pub const DASH_EXISTS: u8 = 32;
/// Defines what quoting mode to use when expanding the command.
enum Quoting { None, Basic, Shell }
/// `Args` is a collection of critical options and arguments that were collected at
/// startup of the application.
pub struct Args<'a> {
pub flags: u8,
pub ncores: usize,
pub arguments: ArrayVec<[Token<'a>; 128]>,
pub ninputs: usize,
}
impl<'a> Args<'a> {
pub fn new() -> Args<'a> {
Args {
ncores: num_cpus::get(),
flags: SHELL_ENABLED,
arguments: ArrayVec::new(),
ninputs: 0,
}
}
pub fn parse(&mut self, comm: &mut String, unprocessed_path: &Path) -> Result<InputIterator, ParseErr> {
let mut quote = Quoting::None;
// Create a write buffer that automatically writes data to the disk when the buffer is full.
let mut disk_buffer = disk_buffer::DiskBuffer::new(unprocessed_path).write()
.map_err(|why| ParseErr::File(FileErr::Open(unprocessed_path.to_owned(), why)))?;
// Temporary stores for input arguments.
let mut raw_args = env::args().skip(1).peekable();
let mut lists: Vec<Vec<String>> = Vec::new();
let mut current_inputs: Vec<String> = Vec::with_capacity(1024);
let mut number_of_arguments = 0;
if env::args().len() > 1 {
// The purpose of this is to set the initial parsing mode.
let mut mode = match raw_args.peek().unwrap().as_ref() {
":::" => Mode::Inputs,
"::::" => Mode::Files,
_ => Mode::Arguments
};
// If there are no arguments to be parsed, then the inputs are commands.
if mode == Mode::Inputs || mode == Mode::Files {
self.flags |= INPUTS_ARE_COMMANDS;
} else {
self.flags &= 255 ^ INPUTS_ARE_COMMANDS;
}
// Parse each and every input argument supplied to the program.
while let Some(argument) = raw_args.next() {
let argument = argument.as_str();
match mode {
Mode::Arguments => {
let mut char_iter = argument.chars().peekable();
// If the first character is a '-' then it will be processed as an argument.
// We can guarantee that there will always be at least one character.
if char_iter.next().unwrap() == '-' {
// If the second character exists, everything's OK.
if let Some(character) = char_iter.next() {
// This scope of code allows users to utilize the GNU style
// command line arguments, to allow for laziness.
if character == 'j' {
// The short-hand job argument needs to be handled specially.
self.ncores = if char_iter.peek().is_some() {
// Each character that follows after `j` will be considered an
// input value.
jobs::parse(&argument[2..])?
} else {
// If there was no character after `j`, the following argument
// must be the job value.
let val = &raw_args.next().ok_or(ParseErr::JobsNoValue)?;
jobs::parse(val)?
}
} else if character != '-' {
// NOTE: Short mode versions of arguments
for character in argument[1..].chars() {
match character {
'h' => {
println!("{}", man::MAN_PAGE);
exit(0);
},
'n' => self.flags &= 255 ^ SHELL_ENABLED,
'p' => self.flags |= PIPE_IS_ENABLED,
'q' => quote = Quoting::Basic,
's' => self.flags |= QUIET_MODE,
'v' => self.flags |= VERBOSE_MODE,
_ => {
return Err(ParseErr::InvalidArgument(argument.to_owned()))
}
}
}
} else {
// NOTE: Long mode versions of arguments
match &argument[2..] {
"help" => {
println!("{}", man::MAN_PAGE);
exit(0);
},
"jobs" => {
let val = &raw_args.next().ok_or(ParseErr::JobsNoValue)?;
self.ncores = jobs::parse(val)?
},
"no-shell" => self.flags &= 255 ^ SHELL_ENABLED,
"num-cpu-cores" => {
println!("{}", num_cpus::get());
exit(0);
},
"pipe" => self.flags |= PIPE_IS_ENABLED,
"quiet" | "silent" => self.flags |= QUIET_MODE,
"quote" => quote = Quoting::Basic,
"shellquote" => quote = Quoting::Shell,
"verbose" => self.flags |= VERBOSE_MODE,
"version" => {
println!("parallel 0.6.2\n\nCrate Dependencies:");
println!(" libc 0.2.15");
println!(" num_cpus 1.0.0");
println!(" permutate 0.1.3");
exit(0);
}
_ => {
return Err(ParseErr::InvalidArgument(argument.to_owned()));
}
}
}
} else {
// `-` will never be a valid argument
return Err(ParseErr::InvalidArgument("-".to_owned()));
}
} else {
match argument {
":::" => {
mode = Mode::Inputs;
self.flags |= INPUTS_ARE_COMMANDS;
},
"::::" => {
mode = Mode::Files;
self.flags |= INPUTS_ARE_COMMANDS;
}
_ => {
// The command has been supplied, and argument parsing is over.
comm.push_str(argument);
mode = Mode::Command;
}
}
}
},
Mode::Command => match argument {
// Arguments after `:::` are input values.
":::" | ":::+" => mode = Mode::Inputs,
// Arguments after `::::` are files with inputs.
"::::" | "::::+" => mode = Mode::Files,
// All other arguments are command arguments.
_ => {
comm.push(' ');
comm.push_str(argument);
}
},
_ => match argument {
// `:::` denotes that the next set of inputs will be added to a new list.
":::" => {
mode = Mode::Inputs;
if !current_inputs.is_empty() {
lists.push(current_inputs.clone());
current_inputs.clear();
}
},
// `:::+` denotes that the next set of inputs will be added to the current list.
":::+" => mode = Mode::Inputs,
// `::::` denotes that the next set of inputs will be added to a new list.
"::::" => {
mode = Mode::Files;
if !current_inputs.is_empty() {
lists.push(current_inputs.clone());
current_inputs.clear();
}
},
// `:::+` denotes that the next set of inputs will be added to the current list.
"::::+" => mode = Mode::Files,
// All other arguments will be added to the current list.
_ => match mode {
Mode::Inputs => current_inputs.push(argument.to_owned()),
Mode::Files => file_parse(&mut current_inputs, argument)?,
_ => unreachable!()
}
}
}
}
if !current_inputs.is_empty() {
lists.push(current_inputs.clone());
}
if lists.len() > 1 {
// Convert the Vec<Vec<String>> into a Vec<Vec<&str>>
let tmp: Vec<Vec<&str>> = lists.iter()
.map(|list| list.iter().map(AsRef::as_ref).collect::<Vec<&str>>())
.collect();
// Convert the Vec<Vec<&str>> into a Vec<&[&str]>
let list_array: Vec<&[&str]> = tmp.iter().map(AsRef::as_ref).collect();
// Create a `Permutator` with the &[&[&str]] as the input.
let permutator = Permutator::new(&list_array[..]);
for permutation in permutator {
let mut iter = permutation.iter();
disk_buffer.write(iter.next().unwrap().as_bytes())
.map_err(|why| ParseErr::File(FileErr::Write(disk_buffer.path.clone(), why)))?;
for element in iter {
disk_buffer.write_byte(b' ')
.map_err(|why| ParseErr::File(FileErr::Write(disk_buffer.path.clone(), why)))?;
disk_buffer.write(element.as_bytes())
.map_err(|why| ParseErr::File(FileErr::Write(disk_buffer.path.clone(), why)))?;
}
disk_buffer.write_byte(b'\n')
.map_err(|why| ParseErr::File(FileErr::Write(disk_buffer.path.clone(), why)))?;
number_of_arguments += 1;
}
} else {
for input in current_inputs {
disk_buffer.write(input.as_bytes()).map_err(|why|
ParseErr::File(FileErr::Write(disk_buffer.path.clone(), why)))?;
disk_buffer.write_byte(b'\n').map_err(|why|
ParseErr::File(FileErr::Write(disk_buffer.path.clone(), why)))?;
number_of_arguments += 1;
}
}
}
// If no inputs are provided, read from stdin instead.
if disk_buffer.is_empty() {
let stdin = io::stdin();
for line in stdin.lock().lines() {
if let Ok(line) = line {
disk_buffer.write(line.as_bytes()).map_err(|why|
ParseErr::File(FileErr::Write(disk_buffer.path.clone(), why)))?;
disk_buffer.write_byte(b'\n').map_err(|why|
ParseErr::File(FileErr::Write(disk_buffer.path.clone(), why)))?;
number_of_arguments += 1;
}
}
}
if number_of_arguments == 0 { return Err(ParseErr::NoArguments); }
// Flush the contents of the buffer to the disk before tokenizing the command argument.
disk_buffer.flush().map_err(|why| ParseErr::File(FileErr::Write(disk_buffer.path.clone(), why)))?;
// Expand the command if quoting is enabled
match quote {
Quoting::None => (),
Quoting::Basic => quote::basic(comm),
Quoting::Shell => quote::shell(comm),
}
if dash_exists() {
self.flags |= DASH_EXISTS;
}
if !shell_required(&self.arguments) {
self.flags &= 255 ^ SHELL_ENABLED;
if self.flags & SHELL_ENABLED == 1 {
println!("Shell enabled");
} else {
println!("Shell disabled");
}
}
// Return an `InputIterator` of the arguments contained within the unprocessed file.
let inputs = InputIterator::new(unprocessed_path, number_of_arguments).map_err(ParseErr::File)?;
Ok(inputs)
}
}
fn shell_required(arguments: &[Token]) -> bool {
for token in arguments {
if let &Token::Argument(ref arg) = token {
if arg.contains(';') || arg.contains('&') {
return true
}
}
}
false
}
fn dash_exists() -> bool {
if let Ok(path) = env::var("PATH") {
for path in path.split(':') {
if let Ok(directory) = fs::read_dir(path) {
for entry in directory {
if let Ok(entry) = entry {
let path = entry.path();
if path.is_file() && path.file_name() == Some(OsStr::new("dash")) { return true; }
}
}
}
}
}
false
}
/// Attempts to open an input argument and adds each line to the `inputs` list.
fn file_parse<P: AsRef<Path>>(inputs: &mut Vec<String>, path: P) -> Result<(), ParseErr> {
let path = path.as_ref();
let file = fs::File::open(path).map_err(|err| ParseErr::File(FileErr::Open(path.to_owned(), err)))?;
for line in BufReader::new(file).lines() {
if let Ok(line) = line { inputs.push(line); }
}
Ok(())
}