Debian Codegraph @

src/lib.rs

raw symbols SCIP
1 // Copyright 2015 The Rust Project Developers. See the COPYRIGHT
2 // file at the top-level directory of this distribution and at
3 // http://rust-lang.org/COPYRIGHT.
4 //
5 // Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
6 // http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
7 // <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
8 // option. This file may not be copied, modified, or distributed
9 // except according to those terms.
10 //! Escape characters that may have special meaning in a shell.
11 #![doc(html_root_url="https://docs.rs/shell-escape/0.1")]
12
13 use stdrust-analyzer cargo std https://github.com/rust-lang/rust/library/std crate/::borrowrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc borrow/::Cowrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc borrow/Cow#;
14 use stdrust-analyzer cargo std https://github.com/rust-lang/rust/library/std crate/::envrust-analyzer cargo std https://github.com/rust-lang/rust/library/std env/;
15
16 /// Escape characters that may have special meaning in a shell.
17 pub fn escaperust-analyzer cargo shell-escape 0.1.5 escape().(s: Cowrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc borrow/Cow#<str>) -> Cowrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc borrow/Cow#<str> {
18 if cfgrust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/builtin/cfg!!(unix) || envrust-analyzer cargo std https://github.com/rust-lang/rust/library/std env/::varrust-analyzer cargo std https://github.com/rust-lang/rust/library/std env/var().("MSYSTEM").is_okrust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/impl#[`Result<T, E>`]is_ok().() {
19 unixrust-analyzer cargo shell-escape 0.1.5 unix/::escaperust-analyzer cargo shell-escape 0.1.5 unix/escape().(s)
20 } else {
21 windowsrust-analyzer cargo shell-escape 0.1.5 windows/::escaperust-analyzer cargo shell-escape 0.1.5 windows/escape().(s)
22 }
23 }
24
25 /// Windows-specific escaping.
26 pub mod windowsrust-analyzer cargo shell-escape 0.1.5 windows/ {
27 use stdrust-analyzer cargo std https://github.com/rust-lang/rust/library/std crate/::borrowrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc borrow/::Cowrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc borrow/Cow#;
28 use stdrust-analyzer cargo std https://github.com/rust-lang/rust/library/std crate/::iterrust-analyzer cargo core https://github.com/rust-lang/rust/library/core iter/::repeatrust-analyzer cargo core https://github.com/rust-lang/rust/library/core iter/sources/repeat/repeat().;
29
30 /// Escape for the windows cmd.exe shell.
31 ///
32 /// See [here][msdn] for more information.
33 ///
34 /// [msdn]: http://blogs.msdn.com/b/twistylittlepassagesallalike/archive/2011/04/23/everyone-quotes-arguments-the-wrong-way.aspx
35 pub fn escaperust-analyzer cargo shell-escape 0.1.5 windows/escape().(s: Cowrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc borrow/Cow#<str>) -> Cowrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc borrow/Cow#<str> {
36 let mut needs_escape = s.is_emptyrust-analyzer cargo core https://github.com/rust-lang/rust/library/core str/impl#[str]is_empty().();
37 for ch in s.charsrust-analyzer cargo core https://github.com/rust-lang/rust/library/core str/impl#[str]chars().() {
38 match ch {
39 '"' | '\t' | '\n' | ' ' => needs_escape = true,
40 _ => {}
41 }
42 }
43 if !rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/bit/impl#[bool][Not]not().needs_escape {
44 return s
45 }
46 let mut es = Stringrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc string/String#::with_capacityrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc string/impl#[String]with_capacity().(s.lenrust-analyzer cargo core https://github.com/rust-lang/rust/library/core str/impl#[str]len().());
47 es.pushrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc string/impl#[String]push().('"');
48 let mut chars = s.charsrust-analyzer cargo core https://github.com/rust-lang/rust/library/core str/impl#[str]chars().().peekablerust-analyzer cargo core https://github.com/rust-lang/rust/library/core iter/traits/iterator/Iterator#peekable().();
49 loop {
50 let mut nslashes = 0;
51 while let Somerust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/Option#Some#(&'\\') = chars.peekrust-analyzer cargo core https://github.com/rust-lang/rust/library/core iter/adapters/peekable/impl#[`Peekable<I>`]peek().() {
52 chars.nextrust-analyzer cargo core https://github.com/rust-lang/rust/library/core iter/adapters/peekable/impl#[`Peekable<I>`][Iterator]next().();
53 nslashes rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/arith/impl#[usize][`AddAssign<Self>`]add_assign().+=rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/arith/impl#[usize][`AddAssign<Self>`]add_assign(). rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/arith/impl#[usize][`AddAssign<Self>`]add_assign().1;
54 }
55
56 match chars.nextrust-analyzer cargo core https://github.com/rust-lang/rust/library/core iter/adapters/peekable/impl#[`Peekable<I>`][Iterator]next().() {
57 Somerust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/Option#Some#('"') => {
58 es.extendrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc string/impl#[String][`Extend<char>`]extend().(repeatrust-analyzer cargo core https://github.com/rust-lang/rust/library/core iter/sources/repeat/repeat().('\\').takerust-analyzer cargo core https://github.com/rust-lang/rust/library/core iter/traits/iterator/Iterator#take().(nslashes rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/arith/impl#[usize][`Mul<Self>`]mul().*rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/arith/impl#[usize][`Mul<Self>`]mul(). rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/arith/impl#[usize][`Mul<Self>`]mul().2 rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/arith/impl#[usize][`Add<Self>`]add().+rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/arith/impl#[usize][`Add<Self>`]add(). rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/arith/impl#[usize][`Add<Self>`]add().1));
59 es.pushrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc string/impl#[String]push().('"');
60 }
61 Somerust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/Option#Some#(c) => {
62 es.extendrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc string/impl#[String][`Extend<char>`]extend().(repeatrust-analyzer cargo core https://github.com/rust-lang/rust/library/core iter/sources/repeat/repeat().('\\').takerust-analyzer cargo core https://github.com/rust-lang/rust/library/core iter/traits/iterator/Iterator#take().(nslashes));
63 es.pushrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc string/impl#[String]push().(c);
64 }
65 Nonerust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/Option#None# => {
66 es.extendrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc string/impl#[String][`Extend<char>`]extend().(repeatrust-analyzer cargo core https://github.com/rust-lang/rust/library/core iter/sources/repeat/repeat().('\\').takerust-analyzer cargo core https://github.com/rust-lang/rust/library/core iter/traits/iterator/Iterator#take().(nslashes rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/arith/impl#[usize][`Mul<Self>`]mul().*rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/arith/impl#[usize][`Mul<Self>`]mul(). rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/arith/impl#[usize][`Mul<Self>`]mul().2));
67 break;
68 }
69 }
70
71 }
72 es.pushrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc string/impl#[String]push().('"');
73 es.intorust-analyzer cargo core https://github.com/rust-lang/rust/library/core convert/impl#[T][`Into<U>`]into().()
74 }
75
76 #[testrust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/builtin/test:]
77 fn test_escaperust-analyzer cargo shell-escape 0.1.5 windows/test_escape().() {
78 assert_eqrust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/assert_eq!!(escaperust-analyzer cargo shell-escape 0.1.5 windows/escape().("--aaa=bbb-ccc".intorust-analyzer cargo core https://github.com/rust-lang/rust/library/core convert/impl#[T][`Into<U>`]into().()), "--aaa=bbb-ccc");
79 assert_eqrust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/assert_eq!!(escaperust-analyzer cargo shell-escape 0.1.5 windows/escape().("linker=gcc -L/foo -Wl,bar".intorust-analyzer cargo core https://github.com/rust-lang/rust/library/core convert/impl#[T][`Into<U>`]into().()),
80 r#""linker=gcc -L/foo -Wl,bar""#);
81 assert_eqrust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/assert_eq!!(escaperust-analyzer cargo shell-escape 0.1.5 windows/escape().(r#"--features="default""#.intorust-analyzer cargo core https://github.com/rust-lang/rust/library/core convert/impl#[T][`Into<U>`]into().()),
82 r#""--features=\"default\"""#);
83 assert_eqrust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/assert_eq!!(escaperust-analyzer cargo shell-escape 0.1.5 windows/escape().(r#"\path\to\my documents\"#.intorust-analyzer cargo core https://github.com/rust-lang/rust/library/core convert/impl#[T][`Into<U>`]into().()),
84 r#""\path\to\my documents\\""#);
85 assert_eqrust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/assert_eq!!(escaperust-analyzer cargo shell-escape 0.1.5 windows/escape().("".intorust-analyzer cargo core https://github.com/rust-lang/rust/library/core convert/impl#[T][`Into<U>`]into().()), r#""""#);
86 }
87 }
88
89 /// Unix-specific escaping.
90 pub mod unixrust-analyzer cargo shell-escape 0.1.5 unix/ {
91 use stdrust-analyzer cargo std https://github.com/rust-lang/rust/library/std crate/::borrowrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc borrow/::Cowrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc borrow/Cow#;
92
93 fn non_whitelistedrust-analyzer cargo shell-escape 0.1.5 unix/non_whitelisted().(ch: char) -> bool {
94 match ch {
95 'a'...'z' | 'A'...'Z' | '0'...'9' | '-' | '_' | '=' | '/' | ',' | '.' | '+' => false,
96 _ => true,
97 }
98 }
99
100 /// Escape characters that may have special meaning in a shell, including spaces.
101 pub fn escaperust-analyzer cargo shell-escape 0.1.5 unix/escape().(s: Cowrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc borrow/Cow#<str>) -> Cowrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc borrow/Cow#<str> {
102 if !rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/bit/impl#[bool][Not]not().s.is_emptyrust-analyzer cargo core https://github.com/rust-lang/rust/library/core str/impl#[str]is_empty().() && !rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/bit/impl#[bool][Not]not().s.containsrust-analyzer cargo core https://github.com/rust-lang/rust/library/core str/impl#[str]contains().(non_whitelistedrust-analyzer cargo shell-escape 0.1.5 unix/non_whitelisted().) {
103 return s;
104 }
105
106 let mut es = Stringrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc string/String#::with_capacityrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc string/impl#[String]with_capacity().(s.lenrust-analyzer cargo core https://github.com/rust-lang/rust/library/core str/impl#[str]len().() rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/arith/impl#[usize][`Add<Self>`]add().+rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/arith/impl#[usize][`Add<Self>`]add(). rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/arith/impl#[usize][`Add<Self>`]add().2);
107 es.pushrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc string/impl#[String]push().('\'');
108 for ch in s.charsrust-analyzer cargo core https://github.com/rust-lang/rust/library/core str/impl#[str]chars().() {
109 match ch {
110 '\'' | '!' => {
111 es.push_strrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc string/impl#[String]push_str().("'\\");
112 es.pushrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc string/impl#[String]push().(ch);
113 es.pushrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc string/impl#[String]push().('\'');
114 }
115 _ => es.pushrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc string/impl#[String]push().(ch),
116 }
117 }
118 es.pushrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc string/impl#[String]push().('\'');
119 es.intorust-analyzer cargo core https://github.com/rust-lang/rust/library/core convert/impl#[T][`Into<U>`]into().()
120 }
121
122 #[testrust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/builtin/test:]
123 fn test_escaperust-analyzer cargo shell-escape 0.1.5 unix/test_escape().() {
124 assert_eqrust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/assert_eq!!(
125 escaperust-analyzer cargo shell-escape 0.1.5 unix/escape().("abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789-_=/,.+".intorust-analyzer cargo core https://github.com/rust-lang/rust/library/core convert/impl#[T][`Into<U>`]into().()),
126 "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789-_=/,.+"
127 );
128 assert_eqrust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/assert_eq!!(escaperust-analyzer cargo shell-escape 0.1.5 unix/escape().("--aaa=bbb-ccc".intorust-analyzer cargo core https://github.com/rust-lang/rust/library/core convert/impl#[T][`Into<U>`]into().()), "--aaa=bbb-ccc");
129 assert_eqrust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/assert_eq!!(escaperust-analyzer cargo shell-escape 0.1.5 unix/escape().("linker=gcc -L/foo -Wl,bar".intorust-analyzer cargo core https://github.com/rust-lang/rust/library/core convert/impl#[T][`Into<U>`]into().()), r#"'linker=gcc -L/foo -Wl,bar'"#);
130 assert_eqrust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/assert_eq!!(escaperust-analyzer cargo shell-escape 0.1.5 unix/escape().(r#"--features="default""#.intorust-analyzer cargo core https://github.com/rust-lang/rust/library/core convert/impl#[T][`Into<U>`]into().()), r#"'--features="default"'"#);
131 assert_eqrust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/assert_eq!!(escaperust-analyzer cargo shell-escape 0.1.5 unix/escape().(r#"'!\$`\\\n "#.intorust-analyzer cargo core https://github.com/rust-lang/rust/library/core convert/impl#[T][`Into<U>`]into().()), r#"''\'''\!'\$`\\\n '"#);
132 assert_eqrust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/assert_eq!!(escaperust-analyzer cargo shell-escape 0.1.5 unix/escape().("".intorust-analyzer cargo core https://github.com/rust-lang/rust/library/core convert/impl#[T][`Into<U>`]into().()), r#"''"#);
133 }
134 }
135