Commit c8a535a6 authored by AdminXVII's avatar AdminXVII
Browse files

cargo +nightly fmt

parent 1eb4f279
...@@ -38,12 +38,17 @@ fn get_git_rev() -> io::Result<String> { ...@@ -38,12 +38,17 @@ fn get_git_rev() -> io::Result<String> {
if version_file.exists() { if version_file.exists() {
fs::read_to_string(&version_file) fs::read_to_string(&version_file)
} else { } else {
Command::new("git").arg("rev-parse").arg("master").output() Command::new("git")
.arg("rev-parse")
.arg("master")
.output()
.and_then(|out| { .and_then(|out| {
String::from_utf8(out.stdout).map_err(|_| io::Error::new( String::from_utf8(out.stdout).map_err(|_| {
io::ErrorKind::InvalidData, io::Error::new(
format!("git rev-parse master output was not UTF-8") io::ErrorKind::InvalidData,
)) format!("git rev-parse master output was not UTF-8"),
)
})
}) })
.or_else(|_| git_rev_from_file()) .or_else(|_| git_rev_from_file())
} }
......
...@@ -76,19 +76,22 @@ impl<'a> Iterator for MultipleBraceExpand<'a> { ...@@ -76,19 +76,22 @@ impl<'a> Iterator for MultipleBraceExpand<'a> {
fn next(&mut self) -> Option<Self::Item> { fn next(&mut self) -> Option<Self::Item> {
if self.permutator.next_with_buffer(&mut self.buffer) { if self.permutator.next_with_buffer(&mut self.buffer) {
let mut strings = self.buffer.iter(); let mut strings = self.buffer.iter();
let small_vec: SmallVec<[u8; 64]> = self.tokens.iter().fold( let small_vec: SmallVec<[u8; 64]> =
SmallVec::with_capacity(64), self.tokens
|mut small_vec, token| match *token { .iter()
BraceToken::Normal(ref text) => { .fold(
escape_string(&mut small_vec, text); SmallVec::with_capacity(64),
small_vec |mut small_vec, token| match *token {
} BraceToken::Normal(ref text) => {
BraceToken::Expander => { escape_string(&mut small_vec, text);
escape_string(&mut small_vec, strings.next().unwrap()); small_vec
small_vec }
} BraceToken::Expander => {
}, escape_string(&mut small_vec, strings.next().unwrap());
); small_vec
}
},
);
Some(unsafe { small::String::from_utf8_unchecked(small_vec.to_vec()) }) Some(unsafe { small::String::from_utf8_unchecked(small_vec.to_vec()) })
} else { } else {
None None
...@@ -114,19 +117,22 @@ where ...@@ -114,19 +117,22 @@ where
fn next(&mut self) -> Option<Self::Item> { fn next(&mut self) -> Option<Self::Item> {
match self.loop_count { match self.loop_count {
0 => { 0 => {
let small_vec: SmallVec<[u8; 64]> = self.tokens.iter().fold( let small_vec: SmallVec<[u8; 64]> =
SmallVec::with_capacity(64), self.tokens
|mut small_vec, token| match *token { .iter()
BraceToken::Normal(ref text) => { .fold(
escape_string(&mut small_vec, text); SmallVec::with_capacity(64),
small_vec |mut small_vec, token| match *token {
} BraceToken::Normal(ref text) => {
BraceToken::Expander => { escape_string(&mut small_vec, text);
escape_string(&mut small_vec, self.elements.next().unwrap()); small_vec
small_vec }
} BraceToken::Expander => {
}, escape_string(&mut small_vec, self.elements.next().unwrap());
); small_vec
}
},
);
self.loop_count = 1; self.loop_count = 1;
Some(unsafe { small::String::from_utf8_unchecked(small_vec.to_vec()) }) Some(unsafe { small::String::from_utf8_unchecked(small_vec.to_vec()) })
} }
...@@ -194,7 +200,8 @@ mod tests { ...@@ -194,7 +200,8 @@ mod tests {
elements: elements.iter().map(|element| *element), elements: elements.iter().map(|element| *element),
tokens, tokens,
loop_count: 0, loop_count: 0,
}.collect::<Vec<small::String>>(), }
.collect::<Vec<small::String>>(),
vec![ vec![
small::String::from("A=one"), small::String::from("A=one"),
small::String::from("A=two"), small::String::from("A=two"),
......
...@@ -8,16 +8,20 @@ macro_rules! string_function { ...@@ -8,16 +8,20 @@ macro_rules! string_function {
eprintln!("ion: {}: two arguments must be supplied", args[0]); eprintln!("ion: {}: two arguments must be supplied", args[0]);
return 2; return 2;
} }
3 => if args[1].$method(args[2].as_str()) { 3 => {
0 if args[1].$method(args[2].as_str()) {
} else { 0
1 } else {
}, 1
_ => if args[2..].iter().any(|arg| args[1].$method(arg.as_str())) { }
0 }
} else { _ => {
1 if args[2..].iter().any(|arg| args[1].$method(arg.as_str())) {
}, 0
} else {
1
}
}
} }
} }
}; };
......
...@@ -19,29 +19,31 @@ pub fn echo(args: &[small::String]) -> Result<(), io::Error> { ...@@ -19,29 +19,31 @@ pub fn echo(args: &[small::String]) -> Result<(), io::Error> {
"--escape" => flags |= Flags::ESCAPE, "--escape" => flags |= Flags::ESCAPE,
"--no-newline" => flags |= Flags::NO_NEWLINE, "--no-newline" => flags |= Flags::NO_NEWLINE,
"--no-spaces" => flags |= Flags::NO_SPACES, "--no-spaces" => flags |= Flags::NO_SPACES,
_ => if arg.starts_with('-') { _ => {
let mut is_opts = true; if arg.starts_with('-') {
let opts = &arg[1..]; let mut is_opts = true;
let mut short_flags = Flags::empty(); let opts = &arg[1..];
for argopt in opts.chars() { let mut short_flags = Flags::empty();
match argopt { for argopt in opts.chars() {
'e' => short_flags |= Flags::ESCAPE, match argopt {
'n' => short_flags |= Flags::NO_NEWLINE, 'e' => short_flags |= Flags::ESCAPE,
's' => short_flags |= Flags::NO_SPACES, 'n' => short_flags |= Flags::NO_NEWLINE,
_ => { 's' => short_flags |= Flags::NO_SPACES,
is_opts = false; _ => {
break; is_opts = false;
break;
}
} }
} }
} if is_opts {
if is_opts { flags |= short_flags;
flags |= short_flags; } else {
data.push(arg);
}
} else { } else {
data.push(arg); data.push(arg);
} }
} else { }
data.push(arg);
},
} }
} }
......
...@@ -287,7 +287,9 @@ fn file_has_read_permission(filepath: &str) -> bool { ...@@ -287,7 +287,9 @@ fn file_has_read_permission(filepath: &str) -> bool {
const GUEST: u32 = 0b100; const GUEST: u32 = 0b100;
// Collect the mode of permissions for the file // Collect the mode of permissions for the file
fs::metadata(filepath).map(|metadata| metadata.permissions().mode()).ok() fs::metadata(filepath)
.map(|metadata| metadata.permissions().mode())
.ok()
// If the mode is equal to any of the above, return `SUCCESS` // If the mode is equal to any of the above, return `SUCCESS`
.map_or(false, |mode| mode & (USER + GROUP + GUEST) != 0) .map_or(false, |mode| mode & (USER + GROUP + GUEST) != 0)
} }
...@@ -302,7 +304,9 @@ fn file_has_write_permission(filepath: &str) -> bool { ...@@ -302,7 +304,9 @@ fn file_has_write_permission(filepath: &str) -> bool {
const GUEST: u32 = 0b10; const GUEST: u32 = 0b10;
// Collect the mode of permissions for the file // Collect the mode of permissions for the file
fs::metadata(filepath).map(|metadata| metadata.permissions().mode()).ok() fs::metadata(filepath)
.map(|metadata| metadata.permissions().mode())
.ok()
// If the mode is equal to any of the above, return `SUCCESS` // If the mode is equal to any of the above, return `SUCCESS`
.map_or(false, |mode| mode & (USER + GROUP + GUEST) != 0) .map_or(false, |mode| mode & (USER + GROUP + GUEST) != 0)
} }
...@@ -319,7 +323,9 @@ fn file_has_execute_permission(filepath: &str) -> bool { ...@@ -319,7 +323,9 @@ fn file_has_execute_permission(filepath: &str) -> bool {
const GUEST: u32 = 0b1; const GUEST: u32 = 0b1;
// Collect the mode of permissions for the file // Collect the mode of permissions for the file
fs::metadata(filepath).map(|metadata| metadata.permissions().mode()).ok() fs::metadata(filepath)
.map(|metadata| metadata.permissions().mode())
.ok()
// If the mode is equal to any of the above, return `SUCCESS` // If the mode is equal to any of the above, return `SUCCESS`
.map_or(false, |mode| mode & (USER + GROUP + GUEST) != 0) .map_or(false, |mode| mode & (USER + GROUP + GUEST) != 0)
} }
......
...@@ -128,7 +128,9 @@ impl<'a> Iterator for ArgumentSplitter<'a> { ...@@ -128,7 +128,9 @@ impl<'a> Iterator for ArgumentSplitter<'a> {
if !self if !self
.bitflags .bitflags
.intersects(ArgumentFlags::DOUBLE | ArgumentFlags::METHOD) .intersects(ArgumentFlags::DOUBLE | ArgumentFlags::METHOD)
&& level == 0 && alevel == 0 && blevel == 0 && level == 0
&& alevel == 0
&& blevel == 0
{ {
break; break;
} }
......
...@@ -93,7 +93,7 @@ impl<'a> KeyIterator<'a> { ...@@ -93,7 +93,7 @@ impl<'a> KeyIterator<'a> {
None => { None => {
break Err(TypeError::Invalid( break Err(TypeError::Invalid(
self.data[index_ident_end + 1..self.read].into(), self.data[index_ident_end + 1..self.read].into(),
)) ));
} }
}, },
}; };
...@@ -142,7 +142,7 @@ impl<'a> Iterator for KeyIterator<'a> { ...@@ -142,7 +142,7 @@ impl<'a> Iterator for KeyIterator<'a> {
return Some(Ok(Key { return Some(Ok(Key {
name: &self.data[start..self.read].trim(), name: &self.data[start..self.read].trim(),
kind: Primitive::Any, kind: Primitive::Any,
})) }));
} }
b':' => { b':' => {
let end = self.read - 1; let end = self.read - 1;
......
...@@ -13,11 +13,13 @@ impl Index { ...@@ -13,11 +13,13 @@ impl Index {
pub fn resolve(&self, vector_length: usize) -> Option<usize> { pub fn resolve(&self, vector_length: usize) -> Option<usize> {
match *self { match *self {
Index::Forward(n) => Some(n), Index::Forward(n) => Some(n),
Index::Backward(n) => if n >= vector_length { Index::Backward(n) => {
None if n >= vector_length {
} else { None
Some(vector_length - (n + 1)) } else {
}, Some(vector_length - (n + 1))
}
}
} }
} }
...@@ -25,7 +27,7 @@ impl Index { ...@@ -25,7 +27,7 @@ impl Index {
/// - A positive value `n` represents `Forward(n)` /// - A positive value `n` represents `Forward(n)`
/// - A negative value `-n` reprents `Backwards(n - 1)` such that: /// - A negative value `-n` reprents `Backwards(n - 1)` such that:
/// ```ignore,rust /// ```ignore,rust
/// assert_eq!(Index::new(-1), Index::Backward(0)) /// assert_eq!(Index::new(-1), Index::Backward(0))
/// ``` /// ```
pub fn new(input: isize) -> Index { pub fn new(input: isize) -> Index {
if input < 0 { if input < 0 {
......
...@@ -196,8 +196,7 @@ pub fn parse_range(input: &str) -> Option<Box<Iterator<Item = small::String>>> { ...@@ -196,8 +196,7 @@ pub fn parse_range(input: &str) -> Option<Box<Iterator<Item = small::String>>> {
macro_rules! finish { macro_rules! finish {
($inclusive:expr, $read:expr) => { ($inclusive:expr, $read:expr) => {
let end_str = &input[$read..]; let end_str = &input[$read..];
if let Some((start, end, nb_digits)) = strings_to_isizes(first, end_str) if let Some((start, end, nb_digits)) = strings_to_isizes(first, end_str) {
{
return numeric_range( return numeric_range(
start, start,
end, end,
...@@ -211,8 +210,7 @@ pub fn parse_range(input: &str) -> Option<Box<Iterator<Item = small::String>>> { ...@@ -211,8 +210,7 @@ pub fn parse_range(input: &str) -> Option<Box<Iterator<Item = small::String>>> {
}; };
($inclusive:expr, $read:expr, $step:expr) => { ($inclusive:expr, $read:expr, $step:expr) => {
let end_str = &input[$read..]; let end_str = &input[$read..];
if let Some((start, end, nb_digits)) = strings_to_isizes(first, end_str) if let Some((start, end, nb_digits)) = strings_to_isizes(first, end_str) {
{
return numeric_range(start, end, $step, $inclusive, nb_digits); return numeric_range(start, end, $step, $inclusive, nb_digits);
} else { } else {
finish_char!($inclusive, end_str, $step); finish_char!($inclusive, end_str, $step);
...@@ -301,8 +299,7 @@ pub fn parse_index_range(input: &str) -> Option<Range> { ...@@ -301,8 +299,7 @@ pub fn parse_index_range(input: &str) -> Option<Range> {
break; break;
} }
let inclusive = dots == 3 let inclusive = dots == 3 || (dots == 2 && last_byte == b'=');
|| (dots == 2 && last_byte == b'=');
let end = &input[id + if inclusive { 3 } else { 2 }..]; let end = &input[id + if inclusive { 3 } else { 2 }..];
...@@ -310,19 +307,26 @@ pub fn parse_index_range(input: &str) -> Option<Range> { ...@@ -310,19 +307,26 @@ pub fn parse_index_range(input: &str) -> Option<Range> {
return if end.is_empty() { return if end.is_empty() {
None None
} else { } else {
end.parse::<isize>().map(|end| Range::to(Index::new(end))).ok() end.parse::<isize>()
.map(|end| Range::to(Index::new(end)))
.ok()
}; };
} else if end.is_empty() { } else if end.is_empty() {
return first.parse::<isize>() return first
.map(|start| Range::from(Index::new(start))).ok(); .parse::<isize>()
.map(|start| Range::from(Index::new(start)))
.ok();
} }
return first.parse::<isize>() return first
.parse::<isize>()
.and_then(|start| end.parse::<isize>().map(|end| (start, end))) .and_then(|start| end.parse::<isize>().map(|end| (start, end)))
.map(|(start, end)| if inclusive { .map(|(start, end)| {
Range::inclusive(Index::new(start), Index::new(end)) if inclusive {
} else { Range::inclusive(Index::new(start), Index::new(end))
Range::exclusive(Index::new(start), Index::new(end)) } else {
Range::exclusive(Index::new(start), Index::new(end))
}
}) })
.ok(); .ok();
} }
......
...@@ -45,11 +45,13 @@ where ...@@ -45,11 +45,13 @@ where
.and_then(|idx| self.nth(idx)) .and_then(|idx| self.nth(idx))
.into_iter() .into_iter()
.collect(), .collect(),
Select::Range(range) => if let Some((start, length)) = range.bounds(size) { Select::Range(range) => {
self.skip(start).take(length).collect() if let Some((start, length)) = range.bounds(size) {
} else { self.skip(start).take(length).collect()
empty().collect() } else {
}, empty().collect()
}
}
Select::Key(_) => empty().collect(), Select::Key(_) => empty().collect(),
} }
} }
......
...@@ -7,7 +7,11 @@ use std::{ ...@@ -7,7 +7,11 @@ use std::{
fs::File, fs::File,
io::{self, prelude::*, BufReader, SeekFrom}, io::{self, prelude::*, BufReader, SeekFrom},
mem, mem,
os::unix::{ffi::OsStrExt, fs::MetadataExt, io::{AsRawFd, RawFd}}, os::unix::{
ffi::OsStrExt,
fs::MetadataExt,
io::{AsRawFd, RawFd},
},
path::PathBuf, path::PathBuf,
process::exit, process::exit,
slice, slice,
...@@ -175,7 +179,7 @@ pub fn execve<S: AsRef<str>>(prog: &str, args: &[S], clear_env: bool) -> io::Err ...@@ -175,7 +179,7 @@ pub fn execve<S: AsRef<str>>(prog: &str, args: &[S], clear_env: bool) -> io::Err
if let Some(prog) = prog { if let Some(prog) = prog {
let mut file = match File::open(&prog) { let mut file = match File::open(&prog) {
Ok(file) => file, Ok(file) => file,
Err(err) => return err Err(err) => return err,
}; };
// Construct a valid set of arguments to pass to execve. Ensure that // Construct a valid set of arguments to pass to execve. Ensure that
...@@ -195,7 +199,7 @@ pub fn execve<S: AsRef<str>>(prog: &str, args: &[S], clear_env: bool) -> io::Err ...@@ -195,7 +199,7 @@ pub fn execve<S: AsRef<str>>(prog: &str, args: &[S], clear_env: bool) -> io::Err
match reader.read(&mut shebang[read..]) { match reader.read(&mut shebang[read..]) {
Ok(0) => break, Ok(0) => break,
Ok(n) => read += n, Ok(n) => read += n,
Err(err) => return err Err(err) => return err,
} }
} }
...@@ -205,21 +209,21 @@ pub fn execve<S: AsRef<str>>(prog: &str, args: &[S], clear_env: bool) -> io::Err ...@@ -205,21 +209,21 @@ pub fn execve<S: AsRef<str>>(prog: &str, args: &[S], clear_env: bool) -> io::Err
// we need to make sure ourselves the file is executable // we need to make sure ourselves the file is executable
let metadata = match file.metadata() { let metadata = match file.metadata() {
Ok(meta) => meta, Ok(meta) => meta,
Err(err) => return err Err(err) => return err,
}; };
let uid = match syscall::getuid() { let uid = match syscall::getuid() {
Ok(uid) => uid, Ok(uid) => uid,
Err(err) => return io::Error::from_raw_os_error(err.errno) Err(err) => return io::Error::from_raw_os_error(err.errno),
}; };
let gid = match syscall::getgid() { let gid = match syscall::getgid() {
Ok(gid) => gid, Ok(gid) => gid,
Err(err) => return io::Error::from_raw_os_error(err.errno) Err(err) => return io::Error::from_raw_os_error(err.errno),
}; };
let mode = if uid == metadata.uid() as usize { let mode = if uid == metadata.uid() as usize {
(metadata.mode() >> 3*2) & 0o7 (metadata.mode() >> 3 * 2) & 0o7
} else if gid == metadata.gid() as usize { } else if gid == metadata.gid() as usize {
(metadata.mode() >> 3*1) & 0o7 (metadata.mode() >> 3 * 1) & 0o7
} else { } else {
metadata.mode() & 0o7 metadata.mode() & 0o7
}; };
...@@ -240,13 +244,13 @@ pub fn execve<S: AsRef<str>>(prog: &str, args: &[S], clear_env: bool) -> io::Err ...@@ -240,13 +244,13 @@ pub fn execve<S: AsRef<str>>(prog: &str, args: &[S], clear_env: bool) -> io::Err
// (But remember to make sure the vector lives long // (But remember to make sure the vector lives long
// enough for the arguments!!) // enough for the arguments!!)
Some(interpreter) Some(interpreter)
}, }
Err(err) => return err Err(err) => return err,
} }
} else { } else {
match reader.seek(SeekFrom::Start(0)) { match reader.seek(SeekFrom::Start(0)) {
Ok(_) => (), Ok(_) => (),
Err(err) => return err Err(err) => return err,
} }
None None
} }
...@@ -255,13 +259,16 @@ pub fn execve<S: AsRef<str>>(prog: &str, args: &[S], clear_env: bool) -> io::Err ...@@ -255,13 +259,16 @@ pub fn execve<S: AsRef<str>>(prog: &str, args: &[S], clear_env: bool) -> io::Err
let path: &OsStr = OsStrExt::from_bytes(&interpreter); let path: &OsStr = OsStrExt::from_bytes(&interpreter);
file = match File::open(path) { file = match File::open(path) {
Ok(file) => file, Ok(file) => file,
Err(err) => return err Err(err) => return err,
}; };
cvt_args.push([interpreter.as_ptr() as usize, interpreter.len()]); cvt_args.push([interpreter.as_ptr() as usize, interpreter.len()]);
} }
// Push the program name // Push the program name
cvt_args.push([prog.as_os_str().as_bytes().as_ptr() as usize, prog.as_os_str().len()]); cvt_args.push([
prog.as_os_str().as_bytes().as_ptr() as usize,
prog.as_os_str().len(),
]);
// Push all arguments // Push all arguments
for arg in args { for arg in args {
......
extern crate libc;
#[cfg(target_os = "dragonfly")] #[cfg(target_os = "dragonfly")]
extern crate errno_dragonfly; extern crate errno_dragonfly;
extern crate libc;
pub mod signals; pub mod signals;
...@@ -40,26 +40,14 @@ pub const STDIN_FILENO: i32 = libc::STDIN_FILENO; ...@@ -40,26 +40,14 @@ pub const STDIN_FILENO: i32 = libc::STDIN_FILENO;
#[cfg(target_os = "linux")] #[cfg(target_os = "linux")]