2015-05-12 11:03:49 -07:00
|
|
|
//! Extensions to `std::process` for Windows.
|
|
|
|
|
|
2015-06-09 16:41:14 -07:00
|
|
|
#![stable(feature = "process_extensions", since = "1.2.0")]
|
2015-05-12 11:03:49 -07:00
|
|
|
|
2021-05-30 17:01:02 +01:00
|
|
|
use crate::ffi::OsStr;
|
2021-08-19 12:24:25 -07:00
|
|
|
use crate::os::windows::io::{
|
|
|
|
|
AsHandle, AsRawHandle, BorrowedHandle, FromRawHandle, IntoRawHandle, OwnedHandle, RawHandle,
|
|
|
|
|
};
|
2019-02-11 04:23:21 +09:00
|
|
|
use crate::process;
|
2021-02-10 21:30:30 +00:00
|
|
|
use crate::sealed::Sealed;
|
2019-02-11 04:23:21 +09:00
|
|
|
use crate::sys;
|
2019-11-27 10:28:39 -08:00
|
|
|
use crate::sys_common::{AsInner, AsInnerMut, FromInner, IntoInner};
|
2015-05-12 11:03:49 -07:00
|
|
|
|
2015-06-09 16:41:14 -07:00
|
|
|
#[stable(feature = "process_extensions", since = "1.2.0")]
|
2015-05-12 11:03:49 -07:00
|
|
|
impl FromRawHandle for process::Stdio {
|
|
|
|
|
unsafe fn from_raw_handle(handle: RawHandle) -> process::Stdio {
|
2021-06-30 21:44:30 -07:00
|
|
|
let handle = sys::handle::Handle::from_raw_handle(handle as *mut _);
|
2016-02-04 11:10:37 -08:00
|
|
|
let io = sys::process::Stdio::Handle(handle);
|
|
|
|
|
process::Stdio::from_inner(io)
|
2015-05-12 11:03:49 -07:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2021-08-17 16:28:00 -07:00
|
|
|
#[unstable(feature = "io_safety", issue = "87074")]
|
|
|
|
|
impl From<OwnedHandle> for process::Stdio {
|
|
|
|
|
fn from(handle: OwnedHandle) -> process::Stdio {
|
2021-08-19 12:24:25 -07:00
|
|
|
let handle = sys::handle::Handle::from_inner(handle);
|
2021-08-17 16:28:00 -07:00
|
|
|
let io = sys::process::Stdio::Handle(handle);
|
|
|
|
|
process::Stdio::from_inner(io)
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2015-06-09 16:41:14 -07:00
|
|
|
#[stable(feature = "process_extensions", since = "1.2.0")]
|
2015-05-12 11:03:49 -07:00
|
|
|
impl AsRawHandle for process::Child {
|
2021-04-25 07:39:09 +01:00
|
|
|
#[inline]
|
2015-05-12 11:03:49 -07:00
|
|
|
fn as_raw_handle(&self) -> RawHandle {
|
2021-06-30 21:44:30 -07:00
|
|
|
self.as_inner().handle().as_raw_handle() as *mut _
|
2015-05-12 11:03:49 -07:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2021-08-17 16:28:00 -07:00
|
|
|
#[unstable(feature = "io_safety", issue = "87074")]
|
|
|
|
|
impl AsHandle for process::Child {
|
|
|
|
|
#[inline]
|
|
|
|
|
fn as_handle(&self) -> BorrowedHandle<'_> {
|
|
|
|
|
self.as_inner().handle().as_handle()
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2016-09-28 11:28:42 +01:00
|
|
|
#[stable(feature = "into_raw_os", since = "1.4.0")]
|
2015-07-15 23:31:24 -07:00
|
|
|
impl IntoRawHandle for process::Child {
|
|
|
|
|
fn into_raw_handle(self) -> RawHandle {
|
2021-06-30 21:44:30 -07:00
|
|
|
self.into_inner().into_handle().into_raw_handle() as *mut _
|
2015-07-15 23:31:24 -07:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2021-08-17 16:28:00 -07:00
|
|
|
#[unstable(feature = "io_safety", issue = "87074")]
|
2021-08-19 12:24:25 -07:00
|
|
|
impl From<process::Child> for OwnedHandle {
|
|
|
|
|
fn from(child: process::Child) -> OwnedHandle {
|
|
|
|
|
child.into_inner().into_handle().into_inner()
|
2021-08-17 16:28:00 -07:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2015-06-09 16:41:14 -07:00
|
|
|
#[stable(feature = "process_extensions", since = "1.2.0")]
|
2015-05-12 11:03:49 -07:00
|
|
|
impl AsRawHandle for process::ChildStdin {
|
2021-04-25 07:39:09 +01:00
|
|
|
#[inline]
|
2015-05-12 11:03:49 -07:00
|
|
|
fn as_raw_handle(&self) -> RawHandle {
|
2021-06-30 21:44:30 -07:00
|
|
|
self.as_inner().handle().as_raw_handle() as *mut _
|
2015-05-12 11:03:49 -07:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2015-06-09 16:41:14 -07:00
|
|
|
#[stable(feature = "process_extensions", since = "1.2.0")]
|
2015-05-12 11:03:49 -07:00
|
|
|
impl AsRawHandle for process::ChildStdout {
|
2021-04-25 07:39:09 +01:00
|
|
|
#[inline]
|
2015-05-12 11:03:49 -07:00
|
|
|
fn as_raw_handle(&self) -> RawHandle {
|
2021-06-30 21:44:30 -07:00
|
|
|
self.as_inner().handle().as_raw_handle() as *mut _
|
2015-05-12 11:03:49 -07:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2015-06-09 16:41:14 -07:00
|
|
|
#[stable(feature = "process_extensions", since = "1.2.0")]
|
2015-05-12 11:03:49 -07:00
|
|
|
impl AsRawHandle for process::ChildStderr {
|
2021-04-25 07:39:09 +01:00
|
|
|
#[inline]
|
2015-05-12 11:03:49 -07:00
|
|
|
fn as_raw_handle(&self) -> RawHandle {
|
2021-06-30 21:44:30 -07:00
|
|
|
self.as_inner().handle().as_raw_handle() as *mut _
|
2015-05-12 11:03:49 -07:00
|
|
|
}
|
|
|
|
|
}
|
2015-07-15 23:31:24 -07:00
|
|
|
|
2016-09-28 11:28:42 +01:00
|
|
|
#[stable(feature = "into_raw_os", since = "1.4.0")]
|
2015-07-15 23:31:24 -07:00
|
|
|
impl IntoRawHandle for process::ChildStdin {
|
|
|
|
|
fn into_raw_handle(self) -> RawHandle {
|
2021-06-30 21:44:30 -07:00
|
|
|
self.into_inner().into_handle().into_raw_handle() as *mut _
|
2015-07-15 23:31:24 -07:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2016-09-28 11:28:42 +01:00
|
|
|
#[stable(feature = "into_raw_os", since = "1.4.0")]
|
2015-07-15 23:31:24 -07:00
|
|
|
impl IntoRawHandle for process::ChildStdout {
|
|
|
|
|
fn into_raw_handle(self) -> RawHandle {
|
2021-06-30 21:44:30 -07:00
|
|
|
self.into_inner().into_handle().into_raw_handle() as *mut _
|
2015-07-15 23:31:24 -07:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2016-09-28 11:28:42 +01:00
|
|
|
#[stable(feature = "into_raw_os", since = "1.4.0")]
|
2015-07-15 23:31:24 -07:00
|
|
|
impl IntoRawHandle for process::ChildStderr {
|
|
|
|
|
fn into_raw_handle(self) -> RawHandle {
|
2021-06-30 21:44:30 -07:00
|
|
|
self.into_inner().into_handle().into_raw_handle() as *mut _
|
2015-07-15 23:31:24 -07:00
|
|
|
}
|
|
|
|
|
}
|
2016-04-26 15:23:46 -07:00
|
|
|
|
2018-04-09 17:44:28 -07:00
|
|
|
/// Windows-specific extensions to [`process::ExitStatus`].
|
2021-01-04 17:45:23 +00:00
|
|
|
///
|
2021-01-10 18:11:00 -08:00
|
|
|
/// This trait is sealed: it cannot be implemented outside the standard library.
|
2021-01-04 17:45:23 +00:00
|
|
|
/// This is so that future additional methods are not breaking changes.
|
2016-08-11 14:08:24 -07:00
|
|
|
#[stable(feature = "exit_status_from", since = "1.12.0")]
|
2021-02-10 21:30:30 +00:00
|
|
|
pub trait ExitStatusExt: Sealed {
|
2016-04-26 15:23:46 -07:00
|
|
|
/// Creates a new `ExitStatus` from the raw underlying `u32` return value of
|
|
|
|
|
/// a process.
|
2016-08-11 14:08:24 -07:00
|
|
|
#[stable(feature = "exit_status_from", since = "1.12.0")]
|
2016-04-26 15:23:46 -07:00
|
|
|
fn from_raw(raw: u32) -> Self;
|
|
|
|
|
}
|
|
|
|
|
|
2016-09-28 11:28:42 +01:00
|
|
|
#[stable(feature = "exit_status_from", since = "1.12.0")]
|
2016-04-26 15:23:46 -07:00
|
|
|
impl ExitStatusExt for process::ExitStatus {
|
|
|
|
|
fn from_raw(raw: u32) -> Self {
|
|
|
|
|
process::ExitStatus::from_inner(From::from(raw))
|
|
|
|
|
}
|
|
|
|
|
}
|
2016-11-30 19:44:07 -05:00
|
|
|
|
2018-04-09 17:44:28 -07:00
|
|
|
/// Windows-specific extensions to the [`process::Command`] builder.
|
2021-02-10 21:30:30 +00:00
|
|
|
///
|
|
|
|
|
/// This trait is sealed: it cannot be implemented outside the standard library.
|
|
|
|
|
/// This is so that future additional methods are not breaking changes.
|
2017-01-25 15:37:20 -08:00
|
|
|
#[stable(feature = "windows_process_extensions", since = "1.16.0")]
|
2021-02-10 21:30:30 +00:00
|
|
|
pub trait CommandExt: Sealed {
|
2016-11-30 19:44:07 -05:00
|
|
|
/// Sets the [process creation flags][1] to be passed to `CreateProcess`.
|
|
|
|
|
///
|
|
|
|
|
/// These will always be ORed with `CREATE_UNICODE_ENVIRONMENT`.
|
2017-04-06 12:57:40 +01:00
|
|
|
///
|
2019-12-25 15:35:54 +00:00
|
|
|
/// [1]: https://docs.microsoft.com/en-us/windows/win32/procthread/process-creation-flags
|
2017-01-25 15:37:20 -08:00
|
|
|
#[stable(feature = "windows_process_extensions", since = "1.16.0")]
|
2016-11-30 21:31:47 -05:00
|
|
|
fn creation_flags(&mut self, flags: u32) -> &mut process::Command;
|
2020-10-08 22:26:31 +00:00
|
|
|
|
|
|
|
|
/// Forces all arguments to be wrapped in quote (`"`) characters.
|
|
|
|
|
///
|
|
|
|
|
/// This is useful for passing arguments to [MSYS2/Cygwin][1] based
|
|
|
|
|
/// executables: these programs will expand unquoted arguments containing
|
|
|
|
|
/// wildcard characters (`?` and `*`) by searching for any file paths
|
|
|
|
|
/// matching the wildcard pattern.
|
|
|
|
|
///
|
|
|
|
|
/// Adding quotes has no effect when passing arguments to programs
|
|
|
|
|
/// that use [msvcrt][2]. This includes programs built with both
|
|
|
|
|
/// MinGW and MSVC.
|
|
|
|
|
///
|
|
|
|
|
/// [1]: <https://github.com/msys2/MSYS2-packages/issues/2176>
|
|
|
|
|
/// [2]: <https://msdn.microsoft.com/en-us/library/17w5ykft.aspx>
|
|
|
|
|
#[unstable(feature = "windows_process_extensions_force_quotes", issue = "82227")]
|
|
|
|
|
fn force_quotes(&mut self, enabled: bool) -> &mut process::Command;
|
2021-05-30 17:01:02 +01:00
|
|
|
|
|
|
|
|
/// Append literal text to the command line without any quoting or escaping.
|
|
|
|
|
///
|
|
|
|
|
/// This is useful for passing arguments to `cmd.exe /c`, which doesn't follow
|
|
|
|
|
/// `CommandLineToArgvW` escaping rules.
|
|
|
|
|
#[unstable(feature = "windows_process_extensions_raw_arg", issue = "29494")]
|
2021-07-05 00:05:46 +01:00
|
|
|
fn raw_arg<S: AsRef<OsStr>>(&mut self, text_to_append_as_is: S) -> &mut process::Command;
|
2016-11-30 19:44:07 -05:00
|
|
|
}
|
|
|
|
|
|
2017-01-25 15:37:20 -08:00
|
|
|
#[stable(feature = "windows_process_extensions", since = "1.16.0")]
|
2016-11-30 19:44:07 -05:00
|
|
|
impl CommandExt for process::Command {
|
2016-11-30 21:31:47 -05:00
|
|
|
fn creation_flags(&mut self, flags: u32) -> &mut process::Command {
|
|
|
|
|
self.as_inner_mut().creation_flags(flags);
|
2016-11-30 19:44:07 -05:00
|
|
|
self
|
|
|
|
|
}
|
2020-10-08 22:26:31 +00:00
|
|
|
|
|
|
|
|
fn force_quotes(&mut self, enabled: bool) -> &mut process::Command {
|
|
|
|
|
self.as_inner_mut().force_quotes(enabled);
|
|
|
|
|
self
|
|
|
|
|
}
|
2021-05-30 17:01:02 +01:00
|
|
|
|
2021-07-05 00:05:46 +01:00
|
|
|
fn raw_arg<S: AsRef<OsStr>>(&mut self, raw_text: S) -> &mut process::Command {
|
|
|
|
|
self.as_inner_mut().raw_arg(raw_text.as_ref());
|
2021-05-30 17:01:02 +01:00
|
|
|
self
|
|
|
|
|
}
|
2016-11-30 19:44:07 -05:00
|
|
|
}
|