mirror of
https://github.com/RGBCube/uutils-coreutils
synced 2025-07-30 12:37:49 +00:00
timeout: Fixed ownership issues
Fixed some minor ownership issues in converting from the options to the arguments to the timeout COMMAND. Additionally, fixed a rustfmt issue in other files (fold/stdbuf.rs)
This commit is contained in:
parent
ea0ead6a2e
commit
431a6ee1b5
3 changed files with 51 additions and 48 deletions
|
@ -45,7 +45,7 @@ pub fn uumain(args: impl uucore::Args) -> i32 {
|
||||||
.short("b")
|
.short("b")
|
||||||
.help(
|
.help(
|
||||||
"count using bytes rather than columns (meaning control characters \
|
"count using bytes rather than columns (meaning control characters \
|
||||||
such as newline are not treated specially)",
|
such as newline are not treated specially)",
|
||||||
)
|
)
|
||||||
.takes_value(false),
|
.takes_value(false),
|
||||||
)
|
)
|
||||||
|
|
|
@ -80,7 +80,8 @@ fn print_version() {
|
||||||
fn print_usage(opts: &Options) {
|
fn print_usage(opts: &Options) {
|
||||||
let brief = "Run COMMAND, with modified buffering operations for its standard streams\n \
|
let brief = "Run COMMAND, with modified buffering operations for its standard streams\n \
|
||||||
Mandatory arguments to long options are mandatory for short options too.";
|
Mandatory arguments to long options are mandatory for short options too.";
|
||||||
let explanation = "If MODE is 'L' the corresponding stream will be line buffered.\n \
|
let explanation =
|
||||||
|
"If MODE is 'L' the corresponding stream will be line buffered.\n \
|
||||||
This option is invalid with standard input.\n\n \
|
This option is invalid with standard input.\n\n \
|
||||||
If MODE is '0' the corresponding stream will be unbuffered.\n\n \
|
If MODE is '0' the corresponding stream will be unbuffered.\n\n \
|
||||||
Otherwise MODE is a number which may be followed by one of the following:\n\n \
|
Otherwise MODE is a number which may be followed by one of the following:\n\n \
|
||||||
|
|
|
@ -12,16 +12,19 @@ extern crate uucore;
|
||||||
|
|
||||||
extern crate clap;
|
extern crate clap;
|
||||||
|
|
||||||
use clap::{App, Arg, ArgMatches, AppSettings};
|
use clap::{App, AppSettings, Arg};
|
||||||
use std::io::ErrorKind;
|
use std::io::ErrorKind;
|
||||||
use std::process::{Command, Stdio};
|
use std::process::{Command, Stdio};
|
||||||
use std::time::Duration;
|
use std::time::Duration;
|
||||||
use uucore::process::ChildExt;
|
use uucore::process::ChildExt;
|
||||||
use uucore::signals::{Signal, signal_by_name_or_value};
|
use uucore::signals::signal_by_name_or_value;
|
||||||
|
|
||||||
|
|
||||||
static NAME: &str = "timeout";
|
|
||||||
static VERSION: &str = env!("CARGO_PKG_VERSION");
|
static VERSION: &str = env!("CARGO_PKG_VERSION");
|
||||||
|
static ABOUT: &str = "Start COMMAND, and kill it if still running after DURATION.";
|
||||||
|
|
||||||
|
fn get_usage() -> String {
|
||||||
|
format!("{0} [OPTION]... [FILE]...", executable!())
|
||||||
|
}
|
||||||
|
|
||||||
const ERR_EXIT_STATUS: i32 = 125;
|
const ERR_EXIT_STATUS: i32 = 125;
|
||||||
|
|
||||||
|
@ -40,70 +43,68 @@ pub mod options {
|
||||||
|
|
||||||
struct Config {
|
struct Config {
|
||||||
foreground: bool,
|
foreground: bool,
|
||||||
kill_after: Option<Duration>,
|
kill_after: Duration,
|
||||||
signal: Option<Signal>,
|
signal: usize,
|
||||||
version: bool,
|
|
||||||
duration: Duration,
|
duration: Duration,
|
||||||
preserve_status: bool
|
preserve_status: bool,
|
||||||
|
|
||||||
command: String,
|
command: String,
|
||||||
command_args: &[String]
|
command_args: Vec<String>,
|
||||||
}
|
}
|
||||||
|
|
||||||
impl Config {
|
impl Config {
|
||||||
fn from(options: Clap::ArgMatches) -> Config {
|
fn from(options: clap::ArgMatches) -> Config {
|
||||||
let timeout_signal = match options.value_of(options::SIGNAL) {
|
let signal = match options.value_of(options::SIGNAL) {
|
||||||
Some(signal_) =>
|
Some(signal_) => {
|
||||||
{
|
|
||||||
let signal_result = signal_by_name_or_value(&signal_);
|
let signal_result = signal_by_name_or_value(&signal_);
|
||||||
match signal_result{
|
match signal_result {
|
||||||
None => {
|
None => {
|
||||||
show_error!("invalid signal '{}'", signal_);
|
unreachable!("invalid signal '{}'", signal_);
|
||||||
return ERR_EXIT_STATUS;
|
}
|
||||||
},
|
Some(signal_value) => signal_value,
|
||||||
_ => Some(signal_result)
|
|
||||||
}
|
}
|
||||||
},
|
}
|
||||||
_ => None
|
_ => uucore::signals::signal_by_name_or_value("TERM").unwrap(),
|
||||||
};
|
};
|
||||||
|
|
||||||
let kill_after: Option<Duration> =
|
let kill_after: Duration = match options.value_of(options::KILL_AFTER) {
|
||||||
match options.value_of(options::KILL_AFTER) {
|
Some(time) => uucore::parse_time::from_str(&time).unwrap(),
|
||||||
Some(time) => Some(uucore::parse_time::from_str(&time)),
|
None => Duration::new(0, 0),
|
||||||
None => None
|
};
|
||||||
};
|
|
||||||
|
|
||||||
let duration: Duration = uucore::parse_time::from_str(
|
let duration: Duration =
|
||||||
options.value_of(options::DURATION)
|
uucore::parse_time::from_str(options.value_of(options::DURATION).unwrap()).unwrap();
|
||||||
);
|
|
||||||
|
|
||||||
let preserve_status: bool = options.is_present(options::PRESERVE_STATUS);
|
let preserve_status: bool = options.is_present(options::PRESERVE_STATUS);
|
||||||
|
let foreground = options.is_present(options::FOREGROUND);
|
||||||
|
|
||||||
let command: String = options.value_of(options::COMMAND).to_str();
|
let command: String = options.value_of(options::COMMAND).unwrap().to_string();
|
||||||
let command_args: &[String] = options.values_of(options::ARGS)
|
let command_args: Vec<String> = options
|
||||||
.map(|x| x.as_str());
|
.values_of(options::ARGS)
|
||||||
|
.unwrap()
|
||||||
|
.map(|x| x.to_owned())
|
||||||
|
.collect();
|
||||||
|
|
||||||
Config {
|
Config {
|
||||||
foreground: options.is_present(options::FOREGROUND),
|
foreground,
|
||||||
kill_after,
|
kill_after,
|
||||||
signal: timeout_signal,
|
signal,
|
||||||
duration,
|
duration,
|
||||||
preserve_status,
|
preserve_status,
|
||||||
command,
|
command,
|
||||||
command_args
|
command_args,
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn uumain(args: impl uucore::Args) -> i32 {
|
pub fn uumain(args: impl uucore::Args) -> i32 {
|
||||||
let args = args.collect_str();
|
let args = args.collect_str();
|
||||||
|
let usage = get_usage();
|
||||||
|
|
||||||
let program = args[0].clone();
|
let app = App::new("timeout")
|
||||||
|
|
||||||
let mut opts = getopts::Options::new();
|
|
||||||
|
|
||||||
let mut app = App::new("timeout")
|
|
||||||
.version(VERSION)
|
.version(VERSION)
|
||||||
|
.usage(&usage[..])
|
||||||
|
.about(ABOUT)
|
||||||
.arg(
|
.arg(
|
||||||
Arg::with_name(options::FOREGROUND)
|
Arg::with_name(options::FOREGROUND)
|
||||||
.long(options::FOREGROUND)
|
.long(options::FOREGROUND)
|
||||||
|
@ -143,13 +144,14 @@ pub fn uumain(args: impl uucore::Args) -> i32 {
|
||||||
let matches = app.get_matches_from(args);
|
let matches = app.get_matches_from(args);
|
||||||
|
|
||||||
let config = Config::from(matches);
|
let config = Config::from(matches);
|
||||||
timeout(config.command,
|
timeout(
|
||||||
config.command_args,
|
&config.command,
|
||||||
config.duration,
|
&config.command_args,
|
||||||
config.signal,
|
config.duration,
|
||||||
config.kill_after,
|
config.signal,
|
||||||
config.foreground,
|
config.kill_after,
|
||||||
config.preserve_status
|
config.foreground,
|
||||||
|
config.preserve_status,
|
||||||
)
|
)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue