summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorAndreas Fankhauser hiddenalpha.ch2022-12-09 17:21:10 +0100
committerAndreas Fankhauser hiddenalpha.ch2022-12-09 17:21:10 +0100
commitae2a605b4a438f0fb7c36878ef46651a5253fe5f (patch)
tree3b5d7ac242c293db2ed2ebe937101c87da06054e
parent85abf8cae9007429350067c94d4ae4e5e2eca907 (diff)
downloadUnspecifiedGarbage-ae2a605b4a438f0fb7c36878ef46651a5253fe5f.zip
UnspecifiedGarbage-ae2a605b4a438f0fb7c36878ef46651a5253fe5f.tar.gz
Add ShellUtils.java. Add link
-rw-r--r--doc/note/links/links.txt1
-rw-r--r--src/main/java/ch/hiddenalpha/unspecifiedgarbage/shell/ShellUtils.java41
2 files changed, 42 insertions, 0 deletions
diff --git a/doc/note/links/links.txt b/doc/note/links/links.txt
index a3b680d..cdc8016 100644
--- a/doc/note/links/links.txt
+++ b/doc/note/links/links.txt
@@ -151,6 +151,7 @@ Links (Aka arguments)
- [static final java uppercase](https://gitit.post.ch/projects/ISA/repos/preflux/pull-requests/82/overview?commentId=39126)
- [invalid java class name](https://gitit.post.ch/projects/ISA/repos/preflux/pull-requests/82/overview?commentId=39125)
- [Formatters produce crap](https://gitit.post.ch/projects/ISA/repos/minetti/pull-requests/14/overview)
+- [Formatters produce crap](https://gitit.post.ch/projects/ISA/repos/veet/pull-requests/2/overview?commentId=233638)
- "https://gitit.post.ch/projects/ISA/repos/zarniwoop/pull-requests/20/overview?commentId=85912"
- "https://gitit.post.ch/projects/ISA/repos/zarniwoop/pull-requests/21/overview?commentId=87250"
- "https://gitit.post.ch/projects/ISA/repos/beeble/pull-requests/126/overview?commentId=70762"
diff --git a/src/main/java/ch/hiddenalpha/unspecifiedgarbage/shell/ShellUtils.java b/src/main/java/ch/hiddenalpha/unspecifiedgarbage/shell/ShellUtils.java
new file mode 100644
index 0000000..5e2aff2
--- /dev/null
+++ b/src/main/java/ch/hiddenalpha/unspecifiedgarbage/shell/ShellUtils.java
@@ -0,0 +1,41 @@
+package ch.hiddenalpha.unspecifiedgarbage.shell;
+
+
+public class ShellUtils {
+
+ /**
+ * Escapes the string so we can use the result in a
+ * <a href="https://pubs.opengroup.org/onlinepubs/009695399/utilities/xcu_chap02.html#tag_02_02_03">double-quoted shell string</a>.
+ * But keeps <a href="https://pubs.opengroup.org/onlinepubs/009695399/utilities/xcu_chap02.html#tag_02_06_02">parameter expansion</a> alive.
+ * Example usage:
+ * String path = "pâth \\with ${varToResolve} but a|so €vil chars like spaces, p|pes or * asterisk";
+ * cmd = "ls '"+ escapeForSnglQuotEverything(path) +"'";
+ */
+ public static String escapeForDblQuotButParams( String s ){
+ s = s.replace("\\", "\\\\");
+ s = s.replace("\"", "\\\"");
+ s = s.replace("`", "\\`");
+ // do NOT escape '$' as we want to keep parameter expansion.
+ return s;
+ }
+
+ /**
+ * Escapes the string so we can use result in a
+ * <a href="https://pubs.opengroup.org/onlinepubs/009695399/utilities/xcu_chap02.html#tag_02_02_02">single-quoted shell string</a>.
+ * Example usage:
+ * String path = "pâth \\with ${MustNotResolveThisVar} and a|so €vil chars like spaces, p|pes or * asterisk";
+ * cmd = "ls '"+ escapeForSnglQuotEverything(path) +"'";
+ */
+ public static String escapeForSingleQuotEverything( String s ){
+ // Cited from "Single-Quotes" in "https://pubs.opengroup.org/onlinepubs/009695399/utilities/xcu_chap02.html":
+ // Enclosing characters in single-quotes shall preserve the literal
+ // value of each character within the single-quotes. A single-quote
+ // cannot occur within single-quotes.
+ // Cited from "Double-Quotes" in "https://pubs.opengroup.org/onlinepubs/009695399/utilities/xcu_chap02.html":
+ // Enclosing characters in double-quotes ( "" ) shall preserve the
+ // literal value of all characters within the double-quotes, with the
+ // exception of the characters dollar sign, backquote, and backslash
+ return s.replace("'", "'\"'\"'");
+ }
+
+}