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> {
if version_file.exists() {
fs::read_to_string(&version_file)
} else {
Command::new("git").arg("rev-parse").arg("master").output()
Command::new("git")
.arg("rev-parse")
.arg("master")
.output()
.and_then(|out| {
String::from_utf8(out.stdout).map_err(|_| io::Error::new(
String::from_utf8(out.stdout).map_err(|_| {
io::Error::new(
io::ErrorKind::InvalidData,
format!("git rev-parse master output was not UTF-8")
))
format!("git rev-parse master output was not UTF-8"),
)
})
})
.or_else(|_| git_rev_from_file())
}
......
......@@ -76,7 +76,10 @@ impl<'a> Iterator for MultipleBraceExpand<'a> {
fn next(&mut self) -> Option<Self::Item> {
if self.permutator.next_with_buffer(&mut self.buffer) {
let mut strings = self.buffer.iter();
let small_vec: SmallVec<[u8; 64]> = self.tokens.iter().fold(
let small_vec: SmallVec<[u8; 64]> =
self.tokens
.iter()
.fold(
SmallVec::with_capacity(64),
|mut small_vec, token| match *token {
BraceToken::Normal(ref text) => {
......@@ -114,7 +117,10 @@ where
fn next(&mut self) -> Option<Self::Item> {
match self.loop_count {
0 => {
let small_vec: SmallVec<[u8; 64]> = self.tokens.iter().fold(
let small_vec: SmallVec<[u8; 64]> =
self.tokens
.iter()
.fold(
SmallVec::with_capacity(64),
|mut small_vec, token| match *token {
BraceToken::Normal(ref text) => {
......@@ -194,7 +200,8 @@ mod tests {
elements: elements.iter().map(|element| *element),
tokens,
loop_count: 0,
}.collect::<Vec<small::String>>(),
}
.collect::<Vec<small::String>>(),
vec![
small::String::from("A=one"),
small::String::from("A=two"),
......
......@@ -8,16 +8,20 @@ macro_rules! string_function {
eprintln!("ion: {}: two arguments must be supplied", args[0]);
return 2;
}
3 => if args[1].$method(args[2].as_str()) {
3 => {
if args[1].$method(args[2].as_str()) {
0
} else {
1
},
_ => if args[2..].iter().any(|arg| args[1].$method(arg.as_str())) {
}
}
_ => {
if args[2..].iter().any(|arg| args[1].$method(arg.as_str())) {
0
} else {
1
},
}
}
}
}
};
......
......@@ -19,7 +19,8 @@ pub fn echo(args: &[small::String]) -> Result<(), io::Error> {
"--escape" => flags |= Flags::ESCAPE,
"--no-newline" => flags |= Flags::NO_NEWLINE,
"--no-spaces" => flags |= Flags::NO_SPACES,
_ => if arg.starts_with('-') {
_ => {
if arg.starts_with('-') {
let mut is_opts = true;
let opts = &arg[1..];
let mut short_flags = Flags::empty();
......@@ -41,7 +42,8 @@ pub fn echo(args: &[small::String]) -> Result<(), io::Error> {
}
} else {
data.push(arg);
},
}
}
}
}
......
......@@ -287,7 +287,9 @@ fn file_has_read_permission(filepath: &str) -> bool {
const GUEST: u32 = 0b100;
// 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`
.map_or(false, |mode| mode & (USER + GROUP + GUEST) != 0)
}
......@@ -302,7 +304,9 @@ fn file_has_write_permission(filepath: &str) -> bool {
const GUEST: u32 = 0b10;
// 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`
.map_or(false, |mode| mode & (USER + GROUP + GUEST) != 0)
}
......@@ -319,7 +323,9 @@ fn file_has_execute_permission(filepath: &str) -> bool {
const GUEST: u32 = 0b1;
// 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`
.map_or(false, |mode| mode & (USER + GROUP + GUEST) != 0)
}
......
......@@ -128,7 +128,9 @@ impl<'a> Iterator for ArgumentSplitter<'a> {
if !self
.bitflags
.intersects(ArgumentFlags::DOUBLE | ArgumentFlags::METHOD)
&& level == 0 && alevel == 0 && blevel == 0
&& level == 0
&& alevel == 0
&& blevel == 0
{
break;
}
......
......@@ -93,7 +93,7 @@ impl<'a> KeyIterator<'a> {
None => {
break Err(TypeError::Invalid(
self.data[index_ident_end + 1..self.read].into(),
))
));
}
},
};
......@@ -142,7 +142,7 @@ impl<'a> Iterator for KeyIterator<'a> {
return Some(Ok(Key {
name: &self.data[start..self.read].trim(),
kind: Primitive::Any,
}))
}));
}
b':' => {
let end = self.read - 1;
......
......@@ -13,11 +13,13 @@ impl Index {
pub fn resolve(&self, vector_length: usize) -> Option<usize> {
match *self {
Index::Forward(n) => Some(n),
Index::Backward(n) => if n >= vector_length {
Index::Backward(n) => {
if n >= vector_length {
None
} else {
Some(vector_length - (n + 1))
},
}
}
}
}
......
......@@ -196,8 +196,7 @@ pub fn parse_range(input: &str) -> Option<Box<Iterator<Item = small::String>>> {
macro_rules! finish {
($inclusive:expr, $read:expr) => {
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,
......@@ -211,8 +210,7 @@ pub fn parse_range(input: &str) -> Option<Box<Iterator<Item = small::String>>> {
};
($inclusive:expr, $read:expr, $step:expr) => {
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);
} else {
finish_char!($inclusive, end_str, $step);
......@@ -301,8 +299,7 @@ pub fn parse_index_range(input: &str) -> Option<Range> {
break;
}
let inclusive = dots == 3
|| (dots == 2 && last_byte == b'=');
let inclusive = dots == 3 || (dots == 2 && last_byte == b'=');
let end = &input[id + if inclusive { 3 } else { 2 }..];
......@@ -310,19 +307,26 @@ pub fn parse_index_range(input: &str) -> Option<Range> {
return if end.is_empty() {
None
} 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() {
return first.parse::<isize>()
.map(|start| Range::from(Index::new(start))).ok();
return first
.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)))
.map(|(start, end)| if inclusive {
.map(|(start, end)| {
if inclusive {
Range::inclusive(Index::new(start), Index::new(end))
} else {
Range::exclusive(Index::new(start), Index::new(end))
}
})
.ok();
}
......
......@@ -45,11 +45,13 @@ where
.and_then(|idx| self.nth(idx))
.into_iter()
.collect(),
Select::Range(range) => if let Some((start, length)) = range.bounds(size) {
Select::Range(range) => {
if let Some((start, length)) = range.bounds(size) {
self.skip(start).take(length).collect()
} else {
empty().collect()
},
}
}
Select::Key(_) => empty().collect(),
}
}
......
......@@ -7,7 +7,11 @@ use std::{
fs::File,
io::{self, prelude::*, BufReader, SeekFrom},
mem,
os::unix::{ffi::OsStrExt, fs::MetadataExt, io::{AsRawFd, RawFd}},
os::unix::{
ffi::OsStrExt,
fs::MetadataExt,
io::{AsRawFd, RawFd},
},
path::PathBuf,
process::exit,
slice,
......@@ -175,7 +179,7 @@ pub fn execve<S: AsRef<str>>(prog: &str, args: &[S], clear_env: bool) -> io::Err
if let Some(prog) = prog {
let mut file = match File::open(&prog) {
Ok(file) => file,
Err(err) => return err
Err(err) => return err,
};
// 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
match reader.read(&mut shebang[read..]) {
Ok(0) => break,
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
// we need to make sure ourselves the file is executable
let metadata = match file.metadata() {
Ok(meta) => meta,
Err(err) => return err
Err(err) => return err,
};
let uid = match syscall::getuid() {
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() {
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 {
(metadata.mode() >> 3*2) & 0o7
(metadata.mode() >> 3 * 2) & 0o7
} else if gid == metadata.gid() as usize {
(metadata.mode() >> 3*1) & 0o7
(metadata.mode() >> 3 * 1) & 0o7
} else {
metadata.mode() & 0o7
};
......@@ -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
// enough for the arguments!!)
Some(interpreter)
},
Err(err) => return err
}
Err(err) => return err,
}
} else {
match reader.seek(SeekFrom::Start(0)) {
Ok(_) => (),
Err(err) => return err
Err(err) => return err,
}
None
}
......@@ -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);
file = match File::open(path) {
Ok(file) => file,
Err(err) => return err
Err(err) => return err,
};
cvt_args.push([interpreter.as_ptr() as usize, interpreter.len()]);
}
// 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
for arg in args {
......
extern crate libc;
#[cfg(target_os = "dragonfly")]
extern crate errno_dragonfly;
extern crate libc;
pub mod signals;
......@@ -40,26 +40,14 @@ pub const STDIN_FILENO: i32 = libc::STDIN_FILENO;
#[cfg(target_os = "linux")]
fn errno() -> i32 { unsafe { *libc::__errno_location() } }
#[cfg(
any(
target_os = "openbsd",
target_os = "bitrig",
target_os = "android"
)
)]
#[cfg(any(target_os = "openbsd", target_os = "bitrig", target_os = "android"))]
fn errno() -> i32 { unsafe { *libc::__errno() } }
#[cfg(
any(
target_os = "macos",
target_os = "ios",
target_os = "freebsd"
)
)]
#[cfg(any(target_os = "macos", target_os = "ios", target_os = "freebsd"))]
fn errno() -> i32 { unsafe { *libc::__error() } }
#[cfg(target_os = "dragonfly")]
fn errno() -> i32 { unsafe { *errno_dragonfly::errno_location()} }
fn errno() -> i32 { unsafe { *errno_dragonfly::errno_location() } }
pub fn strerror(errno: i32) -> &'static str {
unsafe {
......@@ -411,23 +399,9 @@ pub mod env {
.or_else(|| unsafe { fallback() })
.map(PathBuf::from);
#[cfg(
any(
target_os = "android",
target_os = "ios",
target_os = "emscripten"
)
)]
#[cfg(any(target_os = "android", target_os = "ios", target_os = "emscripten"))]
unsafe fn fallback() -> Option<OsString> { None }
#[cfg(
not(
any(
target_os = "android",
target_os = "ios",
target_os = "emscripten"
)
)
)]
#[cfg(not(any(target_os = "android", target_os = "ios", target_os = "emscripten")))]
unsafe fn fallback() -> Option<OsString> {
let amt = match libc::sysconf(libc::_SC_GETPW_R_SIZE_MAX) {
n if n < 0 => 512 as usize,
......
......@@ -19,16 +19,16 @@ pub(crate) fn which(args: &[small::String], shell: &mut Shell) -> Result<i32, ()
let mut result = SUCCESS;
for command in &args[1..] {
match get_command_info(command, shell) {
Ok(c_type) => {
match c_type.as_ref() {
"alias" => if let Some(alias) = shell.variables.get::<types::Alias>(&**command) {
Ok(c_type) => match c_type.as_ref() {
"alias" => {
if let Some(alias) = shell.variables.get::<types::Alias>(&**command) {
println!("{}: alias to {}", command, &*alias);
},
}
}
"function" => println!("{}: function", command),
"builtin" => println!("{}: built-in shell command", command),
_path => println!("{}", _path),
}
}
},
Err(_) => result = FAILURE,
}
}
......@@ -47,9 +47,11 @@ pub(crate) fn find_type(args: &[small::String], shell: &mut Shell) -> Result<i32
match get_command_info(command, shell) {
Ok(c_type) => {
match c_type.as_ref() {
"alias" => if let Some(alias) = shell.variables.get::<types::Alias>(&**command) {
"alias" => {
if let Some(alias) = shell.variables.get::<types::Alias>(&**command) {
println!("{} is aliased to `{}`", command, &*alias);
},
}
}
// TODO Make it print the function.
"function" => println!("{} is a function", command),
"builtin" => println!("{} is a shell builtin", command),
......
......@@ -109,7 +109,9 @@ fn file_has_execute_permission(filepath: &str) -> bool {
const GUEST: u32 = 0b1;
// 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`
.map_or(false, |mode| mode & (USER + GROUP + GUEST) != 0)
}
......@@ -193,7 +195,9 @@ fn test_evaluate_arguments() {
// TODO: see test_binary_is_in_path()
// no argument means we treat it as a string
assert_eq!(evaluate_arguments(&["-b".into()], &shell), Ok(true));
let oldpath = shell.get::<types::Str>("PATH").unwrap_or_else(|| "/usr/bin".into());
let oldpath = shell
.get::<types::Str>("PATH")
.unwrap_or_else(|| "/usr/bin".into());
shell.set("PATH", "testing/");
assert_eq!(
......
......@@ -4,14 +4,18 @@ use types;
pub(crate) fn is(args: &[small::String], shell: &mut Shell) -> Result<(), String> {
match args.len() {
4 => if args[1] != "not" {
4 => {
if args[1] != "not" {
return Err(format!("Expected 'not' instead found '{}'\n", args[1]).to_string());
} else if eval_arg(&*args[2], shell) == eval_arg(&*args[3], shell) {
return Err("".to_string());
},
3 => if eval_arg(&*args[1], shell) != eval_arg(&*args[2], shell) {
}
}
3 => {
if eval_arg(&*args[1], shell) != eval_arg(&*args[2], shell) {
return Err("".to_string());
},
}
}
_ => return Err("is needs 3 or 4 arguments\n".to_string()),
}
......
......@@ -590,17 +590,21 @@ fn builtin_isatty(args: &[small::String], _: &mut Shell) -> i32 {
// sys::isatty expects a usize if compiled for redox but otherwise a i32.
#[cfg(target_os = "redox")]
match args[1].parse::<usize>() {
Ok(r) => if sys::isatty(r) {
Ok(r) => {
if sys::isatty(r) {
return SUCCESS;
},
}
}
Err(_) => eprintln!("ion: isatty given bad number"),
}
#[cfg(not(target_os = "redox"))]
match args[1].parse::<i32>() {
Ok(r) => if sys::isatty(r) {
Ok(r) => {
if sys::isatty(r) {
return SUCCESS;
},
}
}
Err(_) => eprintln!("ion: isatty given bad number"),
}
} else {
......
......@@ -31,12 +31,16 @@ pub(crate) fn set(args: &[small::String], shell: &mut Shell) -> i32 {
match flag {
b'e' => shell.flags |= ERR_EXIT,
b'o' => match args_iter.next().map(|s| s as &str) {
Some("vi") => if let Some(context) = shell.context.as_mut() {
Some("vi") => {
if let Some(context) = shell.context.as_mut() {
context.key_bindings = KeyBindings::Vi;
},
Some("emacs") => if let Some(context) = shell.context.as_mut() {
}
}
Some("emacs") => {
if let Some(context) = shell.context.as_mut() {
context.key_bindings = KeyBindings::Emacs;
},
}
}
Some("huponexit") => shell.flags |= HUPONEXIT,
Some(_) => {
eprintln!("ion: set: invalid option");
......@@ -85,9 +89,11 @@ pub(crate) fn set(args: &[small::String], shell: &mut Shell) -> i32 {
UnsetIfNone => {
shell.variables.set("args", arguments);
}
RetainIfNone => if arguments.len() != 1 {
RetainIfNone => {
if arguments.len() != 1 {
shell.variables.set("args", arguments);
},
}
}
}
}
}
......
......@@ -5,7 +5,8 @@ use std::{fs::File, io::Read};
/// Evaluates the given file and returns 'SUCCESS' if it succeeds.
pub(crate) fn source(shell: &mut Shell, arguments: &[small::String]) -> Result<(), String> {
match arguments.get(1) {
Some(argument) => if let Ok(mut file) = File::open(argument.as_str()) {
Some(argument) => {
if let Ok(mut file) = File::open(argument.as_str()) {
let capacity = file.metadata().map(|x| x.len()).unwrap_or(1) as usize;
let mut command_list = String::with_capacity(capacity);
file.read_to_string(&mut command_list)
......@@ -18,7 +19,8 @@ pub(crate) fn source(shell: &mut Shell, arguments: &[small::String]) -> Result<(
})
} else {
Err(format!("ion: failed to open {}\n", argument))
},
}
}
None => {
shell.evaluate_init_file();
Ok(())
......
......@@ -35,7 +35,8 @@ pub(crate) fn status(args: &[small::String], shell: &mut Shell) -> Result<(), St
"--is-login" => flags |= Flags::LOGIN_SHELL,
"--is-interactive" => flags |= Flags::INTERACTIVE,
"--current-filename" => flags |= Flags::FILENAME,
_ => if arg.starts_with('-') {
_ => {
if arg.starts_with('-') {
match arg.chars().nth(1).unwrap() {
'h' => flags |= Flags::HELP,
'l' => flags |= Flags::LOGIN_SHELL,
......@@ -43,7 +44,8 @@ pub(crate) fn status(args: &[small::String], shell: &mut Shell) -> Result<(), St
'f' => flags |= Flags::FILENAME,
_ => (),
}
},
}
}
}