-
-
Notifications
You must be signed in to change notification settings - Fork 1.5k
/
Copy pathparse_inputs.rs
375 lines (334 loc) · 13.9 KB
/
parse_inputs.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
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
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
// This file is part of the uutils coreutils package.
//
// For the full copyright and license information, please view the LICENSE
// file that was distributed with this source code.
use super::options;
use clap::ArgMatches;
/// Abstraction for getopts
pub trait CommandLineOpts {
/// returns all command line parameters which do not belong to an option.
fn inputs(&self) -> Vec<&str>;
/// tests if any of the specified options is present.
fn opts_present(&self, _: &[&str]) -> bool;
}
/// Implementation for `getopts`
impl CommandLineOpts for ArgMatches {
fn inputs(&self) -> Vec<&str> {
self.get_many::<String>(options::FILENAME)
.map(|values| values.map(|s| s.as_str()).collect())
.unwrap_or_default()
}
fn opts_present(&self, opts: &[&str]) -> bool {
opts.iter()
.any(|opt| self.value_source(opt) == Some(clap::parser::ValueSource::CommandLine))
}
}
/// Contains the Input filename(s) with an optional offset.
///
/// `FileNames` is used for one or more file inputs ("-" = stdin)
/// `FileAndOffset` is used for a single file input, with an offset
/// and an optional label. Offset and label are specified in bytes.
/// `FileAndOffset` will be only used if an offset is specified,
/// but it might be 0.
#[derive(PartialEq, Eq, Debug)]
pub enum CommandLineInputs {
FileNames(Vec<String>),
FileAndOffset((String, u64, Option<u64>)),
}
/// Interprets the command line inputs of od.
///
/// Returns either an unspecified number of filenames.
/// Or it will return a single filename, with an offset and optional label.
/// Offset and label are specified in bytes.
/// '-' is used as filename if stdin is meant. This is also returned if
/// there is no input, as stdin is the default input.
pub fn parse_inputs(matches: &dyn CommandLineOpts) -> Result<CommandLineInputs, String> {
let mut input_strings = matches.inputs();
if matches.opts_present(&["traditional"]) {
return parse_inputs_traditional(&input_strings);
}
// test if command line contains: [file] <offset>
// fall-through if no (valid) offset is found
if input_strings.len() == 1 || input_strings.len() == 2 {
// if any of the options -A, -j, -N, -t, -v or -w are present there is no offset
if !matches.opts_present(&[
options::ADDRESS_RADIX,
options::READ_BYTES,
options::SKIP_BYTES,
options::FORMAT,
options::OUTPUT_DUPLICATES,
options::WIDTH,
]) {
// test if the last input can be parsed as an offset.
let offset = parse_offset_operand(input_strings[input_strings.len() - 1]);
if let Ok(n) = offset {
// if there is just 1 input (stdin), an offset must start with '+'
if input_strings.len() == 1 && input_strings[0].starts_with('+') {
return Ok(CommandLineInputs::FileAndOffset(("-".to_string(), n, None)));
}
if input_strings.len() == 2 {
return Ok(CommandLineInputs::FileAndOffset((
input_strings[0].to_string(),
n,
None,
)));
}
}
}
}
if input_strings.is_empty() {
input_strings.push("-");
}
Ok(CommandLineInputs::FileNames(
input_strings.iter().map(|&s| s.to_string()).collect(),
))
}
/// interprets inputs when --traditional is on the command line
///
/// normally returns `CommandLineInputs::FileAndOffset`, but if no offset is found,
/// it returns `CommandLineInputs::FileNames` (also to differentiate from the offset == 0)
pub fn parse_inputs_traditional(input_strings: &[&str]) -> Result<CommandLineInputs, String> {
match input_strings.len() {
0 => Ok(CommandLineInputs::FileNames(vec!["-".to_string()])),
1 => {
let offset0 = parse_offset_operand(input_strings[0]);
Ok(match offset0 {
Ok(n) => CommandLineInputs::FileAndOffset(("-".to_string(), n, None)),
_ => CommandLineInputs::FileNames(
input_strings.iter().map(|&s| s.to_string()).collect(),
),
})
}
2 => {
let offset0 = parse_offset_operand(input_strings[0]);
let offset1 = parse_offset_operand(input_strings[1]);
match (offset0, offset1) {
(Ok(n), Ok(m)) => Ok(CommandLineInputs::FileAndOffset((
"-".to_string(),
n,
Some(m),
))),
(_, Ok(m)) => Ok(CommandLineInputs::FileAndOffset((
input_strings[0].to_string(),
m,
None,
))),
_ => Err(format!("invalid offset: {}", input_strings[1])),
}
}
3 => {
let offset = parse_offset_operand(input_strings[1]);
let label = parse_offset_operand(input_strings[2]);
match (offset, label) {
(Ok(n), Ok(m)) => Ok(CommandLineInputs::FileAndOffset((
input_strings[0].to_string(),
n,
Some(m),
))),
(Err(_), _) => Err(format!("invalid offset: {}", input_strings[1])),
(_, Err(_)) => Err(format!("invalid label: {}", input_strings[2])),
}
}
_ => Err(format!(
"too many inputs after --traditional: {}",
input_strings[3]
)),
}
}
/// parses format used by offset and label on the command line
pub fn parse_offset_operand(s: &str) -> Result<u64, &'static str> {
let mut start = 0;
let mut len = s.len();
let mut radix = 8;
let mut multiply = 1;
if s.starts_with('+') {
start += 1;
}
if s[start..len].starts_with("0x") || s[start..len].starts_with("0X") {
start += 2;
radix = 16;
} else {
if s[start..len].ends_with('b') {
len -= 1;
multiply = 512;
}
if s[start..len].ends_with('.') {
len -= 1;
radix = 10;
}
}
match u64::from_str_radix(&s[start..len], radix) {
Ok(i) => Ok(i * multiply),
Err(_) => Err("parse failed"),
}
}
#[cfg(test)]
mod tests {
use super::*;
use crate::uu_app;
#[test]
fn test_parse_inputs_normal() {
assert_eq!(
CommandLineInputs::FileNames(vec!["-".to_string()]),
parse_inputs(&uu_app().get_matches_from(vec!["od"])).unwrap()
);
assert_eq!(
CommandLineInputs::FileNames(vec!["-".to_string()]),
parse_inputs(&uu_app().get_matches_from(vec!["od", "-"])).unwrap()
);
assert_eq!(
CommandLineInputs::FileNames(vec!["file1".to_string()]),
parse_inputs(&uu_app().get_matches_from(vec!["od", "file1"])).unwrap()
);
assert_eq!(
CommandLineInputs::FileNames(vec!["file1".to_string(), "file2".to_string()]),
parse_inputs(&uu_app().get_matches_from(vec!["od", "file1", "file2"])).unwrap()
);
assert_eq!(
CommandLineInputs::FileNames(vec![
"-".to_string(),
"file1".to_string(),
"file2".to_string(),
]),
parse_inputs(&uu_app().get_matches_from(vec!["od", "-", "file1", "file2"])).unwrap()
);
}
#[test]
#[allow(clippy::cognitive_complexity)]
fn test_parse_inputs_with_offset() {
// offset is found without filename, so stdin will be used.
assert_eq!(
CommandLineInputs::FileAndOffset(("-".to_string(), 8, None)),
parse_inputs(&uu_app().get_matches_from(vec!["od", "+10"])).unwrap()
);
// offset must start with "+" if no input is specified.
assert_eq!(
CommandLineInputs::FileNames(vec!["10".to_string()]),
parse_inputs(&uu_app().get_matches_from(vec!["od", "10"])).unwrap()
);
// offset is not valid, so it is considered a filename.
assert_eq!(
CommandLineInputs::FileNames(vec!["+10a".to_string()]),
parse_inputs(&uu_app().get_matches_from(vec!["od", "+10a"])).unwrap()
);
// if -j is included in the command line, there cannot be an offset.
assert_eq!(
CommandLineInputs::FileNames(vec!["+10".to_string()]),
parse_inputs(&uu_app().get_matches_from(vec!["od", "-j10", "+10"])).unwrap()
);
// if -v is included in the command line, there cannot be an offset.
assert_eq!(
CommandLineInputs::FileNames(vec!["+10".to_string()]),
parse_inputs(&uu_app().get_matches_from(vec!["od", "-o", "-v", "+10"])).unwrap()
);
assert_eq!(
CommandLineInputs::FileAndOffset(("file1".to_string(), 8, None)),
parse_inputs(&uu_app().get_matches_from(vec!["od", "file1", "+10"])).unwrap()
);
// offset does not need to start with "+" if a filename is included.
assert_eq!(
CommandLineInputs::FileAndOffset(("file1".to_string(), 8, None)),
parse_inputs(&uu_app().get_matches_from(vec!["od", "file1", "10"])).unwrap()
);
assert_eq!(
CommandLineInputs::FileNames(vec!["file1".to_string(), "+10a".to_string()]),
parse_inputs(&uu_app().get_matches_from(vec!["od", "file1", "+10a"])).unwrap()
);
assert_eq!(
CommandLineInputs::FileNames(vec!["file1".to_string(), "+10".to_string()]),
parse_inputs(&uu_app().get_matches_from(vec!["od", "-j10", "file1", "+10"])).unwrap()
);
// offset must be last on the command line
assert_eq!(
CommandLineInputs::FileNames(vec!["+10".to_string(), "file1".to_string()]),
parse_inputs(&uu_app().get_matches_from(vec!["od", "+10", "file1"])).unwrap()
);
}
#[test]
fn test_parse_inputs_traditional() {
// it should not return FileAndOffset to signal no offset was entered on the command line.
assert_eq!(
CommandLineInputs::FileNames(vec!["-".to_string()]),
parse_inputs(&uu_app().get_matches_from(vec!["od", "--traditional"])).unwrap()
);
assert_eq!(
CommandLineInputs::FileNames(vec!["file1".to_string()]),
parse_inputs(&uu_app().get_matches_from(vec!["od", "--traditional", "file1"])).unwrap()
);
// offset does not need to start with a +
assert_eq!(
CommandLineInputs::FileAndOffset(("-".to_string(), 8, None)),
parse_inputs(&uu_app().get_matches_from(vec!["od", "--traditional", "10"])).unwrap()
);
// valid offset and valid label
assert_eq!(
CommandLineInputs::FileAndOffset(("-".to_string(), 8, Some(8))),
parse_inputs(&uu_app().get_matches_from(vec!["od", "--traditional", "10", "10"]))
.unwrap()
);
assert_eq!(
CommandLineInputs::FileAndOffset(("file1".to_string(), 8, None)),
parse_inputs(&uu_app().get_matches_from(vec!["od", "--traditional", "file1", "10"]))
.unwrap()
);
// only one file is allowed, it must be the first
parse_inputs(&uu_app().get_matches_from(vec!["od", "--traditional", "10", "file1"]))
.unwrap_err();
assert_eq!(
CommandLineInputs::FileAndOffset(("file1".to_string(), 8, Some(8))),
parse_inputs(&uu_app().get_matches_from(vec![
"od",
"--traditional",
"file1",
"10",
"10"
]))
.unwrap()
);
parse_inputs(&uu_app().get_matches_from(vec!["od", "--traditional", "10", "file1", "10"]))
.unwrap_err();
parse_inputs(&uu_app().get_matches_from(vec!["od", "--traditional", "10", "10", "file1"]))
.unwrap_err();
parse_inputs(&uu_app().get_matches_from(vec![
"od",
"--traditional",
"10",
"10",
"10",
"10",
]))
.unwrap_err();
}
fn parse_offset_operand_str(s: &str) -> Result<u64, &'static str> {
parse_offset_operand(&String::from(s))
}
#[test]
fn test_parse_offset_operand_invalid() {
parse_offset_operand_str("").unwrap_err();
parse_offset_operand_str("a").unwrap_err();
parse_offset_operand_str("+").unwrap_err();
parse_offset_operand_str("+b").unwrap_err();
parse_offset_operand_str("0x1.").unwrap_err();
parse_offset_operand_str("0x1.b").unwrap_err();
parse_offset_operand_str("-").unwrap_err();
parse_offset_operand_str("-1").unwrap_err();
parse_offset_operand_str("1e10").unwrap_err();
}
#[test]
#[allow(clippy::cognitive_complexity)]
fn test_parse_offset_operand() {
assert_eq!(8, parse_offset_operand_str("10").unwrap()); // default octal
assert_eq!(0, parse_offset_operand_str("0").unwrap());
assert_eq!(8, parse_offset_operand_str("+10").unwrap()); // optional leading '+'
assert_eq!(16, parse_offset_operand_str("0x10").unwrap()); // hex
assert_eq!(16, parse_offset_operand_str("0X10").unwrap()); // hex
assert_eq!(16, parse_offset_operand_str("+0X10").unwrap()); // hex
assert_eq!(10, parse_offset_operand_str("10.").unwrap()); // decimal
assert_eq!(10, parse_offset_operand_str("+10.").unwrap()); // decimal
assert_eq!(4096, parse_offset_operand_str("10b").unwrap()); // b suffix = *512
assert_eq!(4096, parse_offset_operand_str("+10b").unwrap()); // b suffix = *512
assert_eq!(5120, parse_offset_operand_str("10.b").unwrap()); // b suffix = *512
assert_eq!(5120, parse_offset_operand_str("+10.b").unwrap()); // b suffix = *512
assert_eq!(267, parse_offset_operand_str("0x10b").unwrap()); // hex
}
}