### Cross-Platform Shell Escaping with `shell_escape::escape` Source: https://context7.com/sfackler/shell-escape/llms.txt Use this function as the main entry point for escaping. It automatically selects the correct escaping strategy based on the current platform. On Unix-like systems, it uses single-quote escaping, and on Windows, it uses cmd.exe double-quote escaping. ```rust use shell_escape::escape; use std::borrow::Cow; fn main() { // Simple string that doesn't need escaping - returns unchanged (zero-copy) let safe_arg = escape(Cow::Borrowed("--output=file.txt")); println!("{}", safe_arg); // Output: --output=file.txt // String with spaces - gets properly quoted let path_with_spaces = escape(Cow::Borrowed("/path/to/my documents/file.txt")); println!("{}", path_with_spaces); // Unix: '/path/to/my documents/file.txt' // Dangerous characters are safely escaped let user_input = escape(Cow::Borrowed("hello; rm -rf /")); println!("{}", user_input); // Unix: 'hello; rm -rf /' // Building a safe shell command let filename = "user's file.txt"; let escaped = escape(Cow::Borrowed(filename)); let command = format!("cat {}", escaped); println!("{}", command); // Output: cat 'user'\'s file.txt' } ``` -------------------------------- ### Cross-Platform Shell Escaping Source: https://context7.com/sfackler/shell-escape/llms.txt The main entry point that automatically selects the appropriate escaping strategy based on the current platform. On Unix systems, it uses Unix-style single-quote escaping. On Windows, it uses cmd.exe double-quote escaping. ```APIDOC ## shell_escape::escape - Cross-Platform Shell Escaping ### Description Automatically selects the appropriate escaping strategy based on the current platform. On Unix systems (or Windows with MSYSTEM environment variable set), it uses Unix-style single-quote escaping. On Windows, it uses cmd.exe double-quote escaping. ### Method Rust function call ### Endpoint N/A (Rust library function) ### Parameters #### Path Parameters N/A #### Query Parameters N/A #### Request Body N/A ### Request Example ```rust use shell_escape::escape; use std::borrow::Cow; fn main() { // Simple string that doesn't need escaping - returns unchanged (zero-copy) let safe_arg = escape(Cow::Borrowed("--output=file.txt")); println!("{}", safe_arg); // Output: --output=file.txt // String with spaces - gets properly quoted let path_with_spaces = escape(Cow::Borrowed("/path/to/my documents/file.txt")); println!("{}", path_with_spaces); // Unix: '/path/to/my documents/file.txt' // Dangerous characters are safely escaped let user_input = escape(Cow::Borrowed("hello; rm -rf /")); println!("{}", user_input); // Unix: 'hello; rm -rf /' // Building a safe shell command let filename = "user's file.txt"; let escaped = escape(Cow::Borrowed(filename)); let command = format!("cat {}", escaped); println!("{}", command); // Output: cat 'user'\'s file.txt' } ``` ### Response #### Success Response (200) Returns a `Cow` containing the escaped string. #### Response Example ```json { "example": "'user\'s file.txt'" } ``` ``` -------------------------------- ### Windows cmd.exe Escaping with `shell_escape::windows::escape` Source: https://context7.com/sfackler/shell-escape/llms.txt This function provides platform-specific escaping for the Windows cmd.exe shell. It uses double-quote wrapping and correctly handles backslashes and double quotes according to Windows command-line argument parsing rules. ```rust use shell_escape::windows::escape; use std::borrow::Cow; fn main() { // Safe characters pass through unchanged let safe = escape(Cow::Borrowed("--aaa=bbb-ccc")); assert_eq!(safe, "--aaa=bbb-ccc"); // Strings with spaces get double-quoted let with_spaces = escape(Cow::Borrowed("linker=gcc -L/foo -Wl,bar")); assert_eq!(with_spaces, r""linker=gcc -L/foo -Wl,bar""); // Embedded double quotes are escaped with backslash let with_quotes = escape(Cow::Borrowed(r"--features=\"default\"")); assert_eq!(with_quotes, r""--features=\"default\"""); // Trailing backslashes before closing quote are doubled let trailing_slash = escape(Cow::Borrowed(r"\path\to\my documents\")); assert_eq!(trailing_slash, r""\path\to\my documents\\""); // Empty strings become empty double quotes let empty = escape(Cow::Borrowed("")); assert_eq!(empty, r""""); // Example: Building a safe Windows command let file_path = r"C:\Users\John\My Documents\file.txt"; let escaped_path = escape(Cow::Borrowed(file_path)); let cmd = format!("type {}", escaped_path); println!("{}", cmd); // type "C:\Users\John\My Documents\file.txt" } ``` -------------------------------- ### Windows cmd.exe Escaping Source: https://context7.com/sfackler/shell-escape/llms.txt Platform-specific escaping for Windows cmd.exe shell. Uses double-quote wrapping and properly handles backslashes and double quotes according to the Windows command-line argument parsing rules. ```APIDOC ## shell_escape::windows::escape - Windows cmd.exe Escaping ### Description Platform-specific escaping for Windows cmd.exe shell. Uses double-quote wrapping and properly handles backslashes and double quotes according to the Windows command-line argument parsing rules. ### Method Rust function call ### Endpoint N/A (Rust library function) ### Parameters #### Path Parameters N/A #### Query Parameters N/A #### Request Body N/A ### Request Example ```rust use shell_escape::windows::escape; use std::borrow::Cow; fn main() { // Safe characters pass through unchanged let safe = escape(Cow::Borrowed("--aaa=bbb-ccc")); assert_eq!(safe, "--aaa=bbb-ccc"); // Strings with spaces get double-quoted let with_spaces = escape(Cow::Borrowed("linker=gcc -L/foo -Wl,bar")); assert_eq!(with_spaces, r""linker=gcc -L/foo -Wl,bar"""); // Embedded double quotes are escaped with backslash let with_quotes = escape(Cow::Borrowed(r"--features=\"default\"" )); assert_eq!(with_quotes, r""--features=\"default\""""); // Trailing backslashes before closing quote are doubled let trailing_slash = escape(Cow::Borrowed(r"\path\to\my documents\" )); assert_eq!(trailing_slash, r""\path\to\my documents\\"""); // Empty strings become empty double quotes let empty = escape(Cow::Borrowed("")); assert_eq!(empty, r""""); // Example: Building a safe Windows command let file_path = r"C:\Users\John\My Documents\file.txt"; let escaped_path = escape(Cow::Borrowed(file_path)); let cmd = format!("type {}", escaped_path); println!("{}", cmd); // type "C:\Users\John\My Documents\file.txt" } ``` ### Response #### Success Response (200) Returns a `Cow` containing the Windows-escaped string. #### Response Example ```json { "example": ""C:\\Users\\John\\My Documents\\file.txt"" } ``` ``` -------------------------------- ### Unix Shell Escaping Source: https://context7.com/sfackler/shell-escape/llms.txt Platform-specific escaping for Unix shells (bash, sh, zsh). Uses single-quote wrapping and handles special characters like single quotes and exclamation marks by breaking out of the quoted string and inserting escaped literals. ```APIDOC ## shell_escape::unix::escape - Unix Shell Escaping ### Description Platform-specific escaping for Unix shells (bash, sh, zsh). Uses single-quote wrapping and handles special characters like single quotes and exclamation marks by breaking out of the quoted string and inserting escaped literals. ### Method Rust function call ### Endpoint N/A (Rust library function) ### Parameters #### Path Parameters N/A #### Query Parameters N/A #### Request Body N/A ### Request Example ```rust use shell_escape::unix::escape; use std::borrow::Cow; fn main() { // Safe characters pass through unchanged (efficient zero-copy) let alphanumeric = escape(Cow::Borrowed("abcABC123-_=/,.+")); assert_eq!(alphanumeric, "abcABC123-_=/,.+"); // Strings with spaces get single-quoted let with_spaces = escape(Cow::Borrowed("linker=gcc -L/foo -Wl,bar")); assert_eq!(with_spaces, "'linker=gcc -L/foo -Wl,bar'"); // Double quotes are preserved inside single quotes let with_quotes = escape(Cow::Borrowed(r"--features=\"default\"" )); assert_eq!(with_quotes, r"'--features=\"default\"'"); // Single quotes and exclamation marks get special handling let complex = escape(Cow::Borrowed("it's a test!")); println!("{}", complex); // Output: 'it'\'s a test'!' // Empty strings become empty single quotes let empty = escape(Cow::Borrowed("")); assert_eq!(empty, "''"); // Example: Building a safe grep command let search_pattern = "user's query*"; let escaped_pattern = escape(Cow::Borrowed(search_pattern)); let cmd = format!("grep {} /var/log/app.log", escaped_pattern); println!("{}", cmd); // grep 'user'\'s query*' /var/log/app.log } ``` ### Response #### Success Response (200) Returns a `Cow` containing the Unix-escaped string. #### Response Example ```json { "example": "'it\'s a test!'" } ``` ``` -------------------------------- ### Unix Shell Escaping with `shell_escape::unix::escape` Source: https://context7.com/sfackler/shell-escape/llms.txt This function provides platform-specific escaping for Unix shells like bash and sh. It uses single-quote wrapping and handles special characters such as single quotes and exclamation marks by breaking out of the quoted string and inserting escaped literals. ```rust use shell_escape::unix::escape; use std::borrow::Cow; fn main() { // Safe characters pass through unchanged (efficient zero-copy) let alphanumeric = escape(Cow::Borrowed("abcABC123-_=/,.+")); assert_eq!(alphanumeric, "abcABC123-_=/,.+"); // Strings with spaces get single-quoted let with_spaces = escape(Cow::Borrowed("linker=gcc -L/foo -Wl,bar")); assert_eq!(with_spaces, "'linker=gcc -L/foo -Wl,bar'"); // Double quotes are preserved inside single quotes let with_quotes = escape(Cow::Borrowed(r"--features=\"default\"")); assert_eq!(with_quotes, r"'--features=\"default\"'"); // Single quotes and exclamation marks get special handling let complex = escape(Cow::Borrowed("it's a test!")); println!("{}", complex); // Output: 'it'\''s a test'!' // Empty strings become empty single quotes let empty = escape(Cow::Borrowed("")); assert_eq!(empty, "''"); // Example: Building a safe grep command let search_pattern = "user's query*"; let escaped_pattern = escape(Cow::Borrowed(search_pattern)); let cmd = format!("grep {} /var/log/app.log", escaped_pattern); println!("{}", cmd); // grep 'user'\'s query*' /var/log/app.log } ``` === COMPLETE CONTENT === This response contains all available snippets from this library. No additional content exists. Do not make further requests.