Skip to content

Commit

Permalink
shell tweaks
Browse files Browse the repository at this point in the history
  • Loading branch information
fare committed Oct 27, 2023
1 parent e0738cf commit f208e7a
Show file tree
Hide file tree
Showing 2 changed files with 42 additions and 4 deletions.
38 changes: 38 additions & 0 deletions doc/reference/std/cli/shell.md
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,44 @@ in gerbil-utils.
## Interface

### easy-shell-character?
```scheme
(easy-shell-character? character) => bool
```

Returns true if the `character` if a string may contain the character in any position
without that this fact requiring the string to be quoted in any shell.
This include alphanumeric characters and those in `"@%-_=+:,./"`
(not including the double quotes).

All other ASCII characters may require the string to be quoted.
For good measure we also quote strings containing non-ASCII characters.


(test-case "easy-shell-character?"
(string-for-each (lambda (c) (check (easy-shell-character? c) => #t))
"abcdefghijklmnopqrstuvwxzABCDEFGHIJKLMNOPQRSTUVWXZ012345678")
(string-for-each (lambda (c) (check (easy-shell-character? c) => #f))
"`~#$^&*()[{]}\\|;'\"<>? \r\n\t\v"))
(test-case "needs-shell-escape?, escape-shell-token"
(defrules checks+1 ()
((_ (s e)) (begin
(check (needs-shell-escape? s) => #t)
(check (escape-shell-token s) => (string-append "\"" e "\""))))
((_ s) (begin
(check (needs-shell-escape? s) => #t)
(check (escape-shell-token s) => (string-append "\"" s "\"")))))
(defrule (checks+ x ...)
(begin (checks+1 x) ...))
(checks+ "foo?" "~user" ("$1" "\\$1") "*.*" "!1" ("ab\\cd" "ab\\\\cd")
"{}" "a;b" "&amp;" "|" "a b c")
(defrule (checks- s ...) (begin (check (needs-shell-escape? s) => #f) ...))
(checks- "foo" "%-_=+:,./" "1" "..." "abcd" "x=y:z,t.z/u+v_w"))
(test-case "->envvar"
(defrule (checks (s e) ...)
(begin (check (->envvar s) => e) ...))
(checks ("foo" "FOO")
("bar baz" "BAR_BAZ")))))


### needs-shell-escape?

Expand Down
8 changes: 4 additions & 4 deletions src/std/cli/shell-test.ss
Original file line number Diff line number Diff line change
Expand Up @@ -10,17 +10,17 @@
(def shell-test
(test-suite "test :std/misc/shell"
(test-case "easy-shell-character?"
(string-for-each (lambda (c) (check (easy-shell-character? c) => #t))
(string-for-each (lambda (c) (check (not (easy-shell-character? c)) => #f))
"abcdefghijklmnopqrstuvwxzABCDEFGHIJKLMNOPQRSTUVWXZ012345678@%-_=+:,./")
(string-for-each (lambda (c) (check (easy-shell-character? c) => #f))
"`~#$^&*()[{]}\\|;'\"<>? \r\n\t\v"))
"!`~#$^&*()[{]}\\|;'\"<>? \r\n\t\v"))
(test-case "needs-shell-escape?, escape-shell-token"
(defrules checks+1 ()
((_ (s e)) (begin
(check (needs-shell-escape? s) => #t)
(check (not (needs-shell-escape? s)) => #f)
(check (escape-shell-token s) => (string-append "\"" e "\""))))
((_ s) (begin
(check (needs-shell-escape? s) => #t)
(check (not (needs-shell-escape? s)) => #f)
(check (escape-shell-token s) => (string-append "\"" s "\"")))))
(defrule (checks+ x ...)
(begin (checks+1 x) ...))
Expand Down

0 comments on commit f208e7a

Please sign in to comment.