Merge pull request #2499 from 353fc443/uresult-kill

Add Uresult for false, echo, pwd, true and kill
This commit is contained in:
Terts Diepraam 2021-07-13 17:29:26 +02:00 committed by GitHub
commit f5ba8b1811
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
5 changed files with 84 additions and 42 deletions

View file

@ -6,6 +6,9 @@
// For the full copyright and license information, please view the LICENSE // For the full copyright and license information, please view the LICENSE
// file that was distributed with this source code. // file that was distributed with this source code.
// clippy bug https://github.com/rust-lang/rust-clippy/issues/7422
#![allow(clippy::nonstandard_macro_braces)]
#[macro_use] #[macro_use]
extern crate uucore; extern crate uucore;
@ -13,6 +16,7 @@ use clap::{crate_version, App, Arg};
use std::io::{self, Write}; use std::io::{self, Write};
use std::iter::Peekable; use std::iter::Peekable;
use std::str::Chars; use std::str::Chars;
use uucore::error::{FromIo, UResult};
use uucore::InvalidEncodingHandling; use uucore::InvalidEncodingHandling;
const NAME: &str = "echo"; const NAME: &str = "echo";
@ -113,7 +117,8 @@ fn print_escaped(input: &str, mut output: impl Write) -> io::Result<bool> {
Ok(should_stop) Ok(should_stop)
} }
pub fn uumain(args: impl uucore::Args) -> i32 { #[uucore_procs::gen_uumain]
pub fn uumain(args: impl uucore::Args) -> UResult<()> {
let args = args let args = args
.collect_str(InvalidEncodingHandling::ConvertLossy) .collect_str(InvalidEncodingHandling::ConvertLossy)
.accept_any(); .accept_any();
@ -126,13 +131,7 @@ pub fn uumain(args: impl uucore::Args) -> i32 {
None => vec!["".to_string()], None => vec!["".to_string()],
}; };
match execute(no_newline, escaped, values) { execute(no_newline, escaped, values).map_err_context(|| "could not write to stdout".to_string())
Ok(_) => 0,
Err(f) => {
show_error!("{}", f);
1
}
}
} }
pub fn uu_app() -> App<'static, 'static> { pub fn uu_app() -> App<'static, 'static> {

View file

@ -5,12 +5,20 @@
// * For the full copyright and license information, please view the LICENSE // * For the full copyright and license information, please view the LICENSE
// * file that was distributed with this source code. // * file that was distributed with this source code.
// Clippy bug: https://github.com/rust-lang/rust-clippy/issues/7422
#![allow(clippy::nonstandard_macro_braces)]
#[macro_use]
extern crate uucore;
use clap::App; use clap::App;
use uucore::error::{UError, UResult};
use uucore::executable; use uucore::executable;
pub fn uumain(args: impl uucore::Args) -> i32 { #[uucore_procs::gen_uumain]
pub fn uumain(args: impl uucore::Args) -> UResult<()> {
uu_app().get_matches_from(args); uu_app().get_matches_from(args);
1 Err(UError::from(1))
} }
pub fn uu_app() -> App<'static, 'static> { pub fn uu_app() -> App<'static, 'static> {

View file

@ -7,20 +7,21 @@
// spell-checker:ignore (ToDO) signalname pids // spell-checker:ignore (ToDO) signalname pids
// clippy bug https://github.com/rust-lang/rust-clippy/issues/7422
#![allow(clippy::nonstandard_macro_braces)]
#[macro_use] #[macro_use]
extern crate uucore; extern crate uucore;
use clap::{crate_version, App, Arg}; use clap::{crate_version, App, Arg};
use libc::{c_int, pid_t}; use libc::{c_int, pid_t};
use std::io::Error; use std::io::Error;
use uucore::error::{UResult, USimpleError};
use uucore::signals::ALL_SIGNALS; use uucore::signals::ALL_SIGNALS;
use uucore::InvalidEncodingHandling; use uucore::InvalidEncodingHandling;
static ABOUT: &str = "Send signal to processes or list information about signals."; static ABOUT: &str = "Send signal to processes or list information about signals.";
static EXIT_OK: i32 = 0;
static EXIT_ERR: i32 = 1;
pub mod options { pub mod options {
pub static PIDS_OR_SIGNALS: &str = "pids_of_signals"; pub static PIDS_OR_SIGNALS: &str = "pids_of_signals";
pub static LIST: &str = "list"; pub static LIST: &str = "list";
@ -36,7 +37,8 @@ pub enum Mode {
List, List,
} }
pub fn uumain(args: impl uucore::Args) -> i32 { #[uucore_procs::gen_uumain]
pub fn uumain(args: impl uucore::Args) -> UResult<()> {
let args = args let args = args
.collect_str(InvalidEncodingHandling::Ignore) .collect_str(InvalidEncodingHandling::Ignore)
.accept_any(); .accept_any();
@ -66,13 +68,14 @@ pub fn uumain(args: impl uucore::Args) -> i32 {
(None, Some(s)) => s.to_owned(), (None, Some(s)) => s.to_owned(),
(None, None) => "TERM".to_owned(), (None, None) => "TERM".to_owned(),
}; };
return kill(&sig, &pids_or_signals); kill(&sig, &pids_or_signals)
}
Mode::Table => {
table();
Ok(())
} }
Mode::Table => table(),
Mode::List => list(pids_or_signals.get(0).cloned()), Mode::List => list(pids_or_signals.get(0).cloned()),
} }
EXIT_OK
} }
pub fn uu_app() -> App<'static, 'static> { pub fn uu_app() -> App<'static, 'static> {
@ -139,20 +142,23 @@ fn table() {
println!(); println!();
} }
} }
println!() println!();
} }
fn print_signal(signal_name_or_value: &str) { fn print_signal(signal_name_or_value: &str) -> UResult<()> {
for (value, &signal) in ALL_SIGNALS.iter().enumerate() { for (value, &signal) in ALL_SIGNALS.iter().enumerate() {
if signal == signal_name_or_value || (format!("SIG{}", signal)) == signal_name_or_value { if signal == signal_name_or_value || (format!("SIG{}", signal)) == signal_name_or_value {
println!("{}", value); println!("{}", value);
exit!(EXIT_OK as i32) return Ok(());
} else if signal_name_or_value == value.to_string() { } else if signal_name_or_value == value.to_string() {
println!("{}", signal); println!("{}", signal);
exit!(EXIT_OK as i32) return Ok(());
} }
} }
crash!(EXIT_ERR, "unknown signal name {}", signal_name_or_value) Err(USimpleError::new(
1,
format!("unknown signal name {}", signal_name_or_value),
))
} }
fn print_signals() { fn print_signals() {
@ -170,30 +176,41 @@ fn print_signals() {
} }
} }
fn list(arg: Option<String>) { fn list(arg: Option<String>) -> UResult<()> {
match arg { match arg {
Some(ref x) => print_signal(x), Some(ref x) => print_signal(x),
None => print_signals(), None => {
}; print_signals();
Ok(())
}
}
} }
fn kill(signalname: &str, pids: &[String]) -> i32 { fn kill(signalname: &str, pids: &[String]) -> UResult<()> {
let mut status = 0;
let optional_signal_value = uucore::signals::signal_by_name_or_value(signalname); let optional_signal_value = uucore::signals::signal_by_name_or_value(signalname);
let signal_value = match optional_signal_value { let signal_value = match optional_signal_value {
Some(x) => x, Some(x) => x,
None => crash!(EXIT_ERR, "unknown signal name {}", signalname), None => {
return Err(USimpleError::new(
1,
format!("unknown signal name {}", signalname),
));
}
}; };
for pid in pids { for pid in pids {
match pid.parse::<usize>() { match pid.parse::<usize>() {
Ok(x) => { Ok(x) => {
if unsafe { libc::kill(x as pid_t, signal_value as c_int) } != 0 { if unsafe { libc::kill(x as pid_t, signal_value as c_int) } != 0 {
show_error!("{}", Error::last_os_error()); show!(USimpleError::new(1, format!("{}", Error::last_os_error())));
status = 1;
} }
} }
Err(e) => crash!(EXIT_ERR, "failed to parse argument {}: {}", pid, e), Err(e) => {
return Err(USimpleError::new(
1,
format!("failed to parse argument {}: {}", pid, e),
));
}
}; };
} }
status Ok(())
} }

View file

@ -5,6 +5,9 @@
// * For the full copyright and license information, please view the LICENSE // * For the full copyright and license information, please view the LICENSE
// * file that was distributed with this source code. // * file that was distributed with this source code.
// clippy bug https://github.com/rust-lang/rust-clippy/issues/7422
#![allow(clippy::nonstandard_macro_braces)]
#[macro_use] #[macro_use]
extern crate uucore; extern crate uucore;
@ -13,6 +16,8 @@ use std::env;
use std::io; use std::io;
use std::path::{Path, PathBuf}; use std::path::{Path, PathBuf};
use uucore::error::{FromIo, UResult, USimpleError};
static ABOUT: &str = "Display the full filename of the current working directory."; static ABOUT: &str = "Display the full filename of the current working directory.";
static OPT_LOGICAL: &str = "logical"; static OPT_LOGICAL: &str = "logical";
static OPT_PHYSICAL: &str = "physical"; static OPT_PHYSICAL: &str = "physical";
@ -36,7 +41,8 @@ fn get_usage() -> String {
format!("{0} [OPTION]... FILE...", executable!()) format!("{0} [OPTION]... FILE...", executable!())
} }
pub fn uumain(args: impl uucore::Args) -> i32 { #[uucore_procs::gen_uumain]
pub fn uumain(args: impl uucore::Args) -> UResult<()> {
let usage = get_usage(); let usage = get_usage();
let matches = uu_app().usage(&usage[..]).get_matches_from(args); let matches = uu_app().usage(&usage[..]).get_matches_from(args);
@ -46,16 +52,20 @@ pub fn uumain(args: impl uucore::Args) -> i32 {
if matches.is_present(OPT_LOGICAL) { if matches.is_present(OPT_LOGICAL) {
println!("{}", logical_path.display()); println!("{}", logical_path.display());
} else { } else {
match absolute_path(&logical_path) { let physical_path = absolute_path(&logical_path)
Ok(physical_path) => println!("{}", physical_path.display()), .map_err_context(|| "failed to get absolute path".to_string())?;
Err(e) => crash!(1, "failed to get absolute path {}", e), println!("{}", physical_path.display());
};
} }
} }
Err(e) => crash!(1, "failed to get current directory {}", e), Err(e) => {
return Err(USimpleError::new(
1,
format!("failed to get current directory {}", e),
))
}
}; };
0 Ok(())
} }
pub fn uu_app() -> App<'static, 'static> { pub fn uu_app() -> App<'static, 'static> {

View file

@ -5,12 +5,20 @@
// * For the full copyright and license information, please view the LICENSE // * For the full copyright and license information, please view the LICENSE
// * file that was distributed with this source code. // * file that was distributed with this source code.
// Clippy bug: https://github.com/rust-lang/rust-clippy/issues/7422
#![allow(clippy::nonstandard_macro_braces)]
#[macro_use]
extern crate uucore;
use clap::App; use clap::App;
use uucore::error::UResult;
use uucore::executable; use uucore::executable;
pub fn uumain(args: impl uucore::Args) -> i32 { #[uucore_procs::gen_uumain]
pub fn uumain(args: impl uucore::Args) -> UResult<()> {
uu_app().get_matches_from(args); uu_app().get_matches_from(args);
0 Ok(())
} }
pub fn uu_app() -> App<'static, 'static> { pub fn uu_app() -> App<'static, 'static> {