Skip to content
Snippets Groups Projects
Commit 7e25636b authored by Sag0Sag0's avatar Sag0Sag0 Committed by Michael Aaron Murphy
Browse files

Add builtin isatty (#667)

* Add builtin isatty

* Attempt fix for redox specific error

* Add documentation on platform specific stuff
parent d7b7ffe6
No related branches found
No related tags found
No related merge requests found
......@@ -78,6 +78,16 @@ OPTIONS
returns 0 if the two arguments are not equal.
"#;
pub(crate) const MAN_ISATTY: &'static str = r#"
isatty - Checks if argument is a file descriptor
SYNOPSIS
isatty [FD]
DESCRIPTION
Returns 0 exit status if the supplied file descriptor is a tty.
"#;
pub(crate) const MAN_DIRS: &'static str = r#"NAME
dirs - prints the directory stack
......
......@@ -64,7 +64,7 @@ macro_rules! map {
/// If you are implementing a builtin add it to the table below, create a well named manpage in
/// man_pages and check for help flags by adding to the start of your builtin the following
/// if check_help(args, MAN_CD) {
/// if check_help(args, MAN_BUILTIN_NAME) {
/// return SUCCESS
/// }
......@@ -93,6 +93,7 @@ pub const BUILTINS: &'static BuiltinMap = &map!(
"history" => builtin_history : "Display a log of all commands previously executed",
"ion-docs" => ion_docs : "Opens the Ion manual",
"is" => builtin_is : "Simple alternative to == and !=",
"isatty" => builtin_isatty : "Returns 0 exit status if the supplied FD is a tty",
"jobs" => builtin_jobs : "Displays all jobs that are attached to the background",
"matches" => builtin_matches : "Checks if a string matches a given regex",
"not" => builtin_not : "Reverses the exit status value of the given command.",
......@@ -637,3 +638,50 @@ fn builtin_which(args: &[&str], shell: &mut Shell) -> i32 {
}
result
}
/// There are two different `builtin_isatty()` functions because
/// `sys::isatty()` when built for redox expects a usize while otherwise expecting a i32.
/// This could probably be done more elegantly.
#[cfg(target_os = "redox")]
fn builtin_isatty(args: &[&str], _: &mut Shell) -> i32 {
if check_help(args, MAN_ISATTY) {
return SUCCESS
}
let stderr = io::stderr();
let mut stderr = stderr.lock();
if args.len() > 1 {
match args[1].parse::<usize>() {
Ok(r) => if sys::isatty(r) {
return SUCCESS
},
Err(_) => { let _ = stderr.write_all("ion: isatty given bad number".as_bytes()); }
}
} else {
return SUCCESS
}
FAILURE
}
#[cfg(not(target_os = "redox"))]
fn builtin_isatty(args: &[&str], _: &mut Shell) -> i32 {
if check_help(args, MAN_ISATTY) {
return SUCCESS
}
let stderr = io::stderr();
let mut stderr = stderr.lock();
if args.len() > 1 {
match args[1].parse::<i32>() {
Ok(r) => if sys::isatty(r) {
return SUCCESS
},
Err(_) => { let _ = stderr.write_all("ion: isatty given bad number".as_bytes()); }
}
} else {
return SUCCESS
}
FAILURE
}
\ No newline at end of file
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment