Skip to content

sh: remove unnecessary atty dep #399

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 3 additions & 5 deletions CONTRIBUTING.md
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ There are several ways to contribute to posixutils-rs:
3. Test coverage: Integration tests, positive and negative, are complete, pass 100%
4. Code coverage: Automated code coverage data indicates 100%
5. Translated: All strings are internationalized, including common OS errors for common error cases.
6. Audited: An external party has reviewed and tested for correctness,
6. Audited: An external party has reviewed and tested for correctness,
POSIX compliance, security, races and similar issues.

### Coding considerations
Expand All @@ -29,12 +29,11 @@ There are several ways to contribute to posixutils-rs:
### CLI utility and Rust style guidelines

1. `cargo fmt` is required.
2. Ideal goal: **Each utility should look like a standard Rust CLI program.**
2. Ideal goal: **Each utility should look like a standard Rust CLI program.**
Small, lightweight utility with command line processing,
core algorithm, and zero external crate dependencies.
3. "only std" When an external crate is required, avoid mega-crates. Prefer
std-only, or, tiny crates such as `atty` that perform a single,
lightweight function.
std-only, or, tiny crates that perform a single, lightweight function.
4. Correctness, readability, performance, in that order.
Code should be readable by unfamiliar developers. Avoid dense,
uncommented code.
Expand All @@ -58,4 +57,3 @@ There are several ways to contribute to posixutils-rs:
* Provide any input data can that be used to reproduce the bug.
* Provide any error output from the utility.
* Describe expected results: What did you expect to happen, and did not?

22 changes: 0 additions & 22 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 0 additions & 1 deletion misc/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@ rust-version.workspace = true

[dependencies]
clap.workspace = true
libc.workspace = true
gettext-rs.workspace = true

[dev-dependencies]
Expand Down
17 changes: 10 additions & 7 deletions misc/test.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,14 @@
// - fix and test unary ops
//

use std::os::unix::fs::{FileTypeExt, MetadataExt, PermissionsExt};
use std::path::Path;
use std::{
io::IsTerminal,
os::{
fd::{BorrowedFd, RawFd},
unix::fs::{FileTypeExt, MetadataExt, PermissionsExt},
},
path::Path,
};

use gettextrs::{bind_textdomain_codeset, gettext, setlocale, textdomain, LocaleCategory};

Expand Down Expand Up @@ -132,17 +138,14 @@ fn eval_unary_path(op: &UnaryOp, s: &str) -> bool {
}

fn eval_terminal(s: &str) -> bool {
let fd = match s.parse::<u32>() {
let fd = match s.parse::<RawFd>() {
Ok(f) => f,
Err(_) => {
return false;
}
};

// Normally, posixutils would use the atty crate.
// Passing an arbitrary fd requires unsafe isatty in this case.

unsafe { libc::isatty(fd as i32) == 1 }
unsafe { BorrowedFd::borrow_raw(fd).is_terminal() }
}

fn eval_unary(op_str: &str, s: &str) -> bool {
Expand Down
1 change: 0 additions & 1 deletion sh/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@ edition = "2021"
plib = { path = "../plib" }
gettext-rs.workspace = true
nix = { version = "0.29", features = ["process", "fs", "resource", "signal", "user", "term"] }
atty = "0.2"

[[bin]]
name = "sh"
Expand Down
14 changes: 8 additions & 6 deletions sh/src/builtin/read.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,10 +13,12 @@ use crate::shell::opened_files::{OpenedFile, OpenedFiles, STDIN_FILENO};
use crate::shell::Shell;
use crate::wordexp::expanded_word::ExpandedWord;
use crate::wordexp::split_fields;
use atty::Stream;
use nix::errno::Errno;
use std::os::fd::{AsRawFd, RawFd};
use std::time::Duration;
use std::{io::stdin, time::Duration};
use std::{
io::IsTerminal,
os::fd::{AsRawFd, RawFd},
};
Comment on lines -18 to +21
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This was cargo fmt - I didn't review it. Perhaps consider going with the same decision as the rust standard library does and configuring a rustfmt.toml to group imports by module? This tends to cut down a large number of lines and makes diffs simpler generally.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.


fn bytes_to_string(bytes: Vec<u8>) -> Result<String, BuiltinError> {
String::from_utf8(bytes.to_vec()).map_err(|_| "read: invalid UTF-8".into())
Expand Down Expand Up @@ -140,13 +142,13 @@ fn read_from_stdin(
delimiter: u8,
backslash_escape: bool,
) -> Result<ReadResult, BuiltinError> {
if atty::is(Stream::Stdin) {
if stdin().is_terminal() {
let original_terminal_settings = shell.terminal.reset();
shell.terminal.set_nonblocking();

let result = read_until_from_non_blocking_fd(
shell,
std::io::stdin().as_raw_fd(),
stdin().as_raw_fd(),
delimiter,
backslash_escape,
);
Expand All @@ -155,7 +157,7 @@ fn read_from_stdin(

result
} else {
read_until_from_file(std::io::stdin().as_raw_fd(), delimiter, backslash_escape)
read_until_from_file(stdin().as_raw_fd(), delimiter, backslash_escape)
}
}

Expand Down
5 changes: 2 additions & 3 deletions sh/src/cli/terminal.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,11 +7,10 @@
// SPDX-License-Identifier: MIT
//

use atty::Stream;
use nix::sys::termios;
use nix::sys::termios::{LocalFlags, Termios};
use std::io;
use std::io::Read;
use std::io::{self, stdin, stdout, IsTerminal};
use std::os::fd::AsFd;

#[derive(Clone)]
Expand Down Expand Up @@ -81,5 +80,5 @@ pub fn read_nonblocking_char() -> Option<u8> {
}

pub fn is_attached_to_terminal() -> bool {
atty::is(Stream::Stdin) && atty::is(Stream::Stdout)
stdin().is_terminal() && stdout().is_terminal()
}