| 1 | #### Turn off external binaries with shvar PATH='' {}
|
| 2 | shopt --set parse_brace parse_proc
|
| 3 |
|
| 4 | source --builtin ysh/shvar.ysh
|
| 5 |
|
| 6 | echo hi > file
|
| 7 |
|
| 8 | # Note: this CACHES the lookup, so shvar has to clear cache when modifying it
|
| 9 | cp -v file /tmp >&2
|
| 10 | echo status=$?
|
| 11 |
|
| 12 | # TODO: implement this, and call it whenever shvar mutates PATH?
|
| 13 | # what about when PATH is mutated? No leave it out for now.
|
| 14 |
|
| 15 | # hash -r # clear the cache, no longer necessary
|
| 16 |
|
| 17 | shvar PATH='' {
|
| 18 | cp -v file /tmp
|
| 19 | echo status=$?
|
| 20 |
|
| 21 | # this also doesn't work
|
| 22 | command cp -v file /tmp
|
| 23 | echo status=$?
|
| 24 | }
|
| 25 |
|
| 26 | # Now it's available again
|
| 27 | cp -v file /tmp >&2
|
| 28 | echo status=$?
|
| 29 |
|
| 30 | ## STDOUT:
|
| 31 | status=0
|
| 32 | status=127
|
| 33 | status=127
|
| 34 | status=0
|
| 35 | ## END
|
| 36 |
|
| 37 | #### More shvar PATH=''
|
| 38 | shopt --set parse_brace command_sub_errexit parse_proc
|
| 39 |
|
| 40 | source --builtin ysh/shvar.ysh
|
| 41 |
|
| 42 | shvar PATH='' {
|
| 43 | ( cp -v file /tmp >&2 )
|
| 44 | echo status=$?
|
| 45 |
|
| 46 | forkwait {
|
| 47 | cp -v file /tmp >&2
|
| 48 | }
|
| 49 | echo status=$?
|
| 50 |
|
| 51 | try {
|
| 52 | true $(cp -v file /tmp >&2)
|
| 53 | }
|
| 54 | echo _status $_status
|
| 55 | }
|
| 56 |
|
| 57 | ## STDOUT:
|
| 58 | status=127
|
| 59 | status=127
|
| 60 | _status 127
|
| 61 | ## END
|
| 62 |
|
| 63 |
|
| 64 | #### builtins and externals not available in hay eval
|
| 65 | shopt --set parse_brace
|
| 66 | shopt --unset errexit
|
| 67 |
|
| 68 | hay define Package
|
| 69 |
|
| 70 | try {
|
| 71 | hay eval :result {
|
| 72 | Package foo {
|
| 73 | /bin/ls
|
| 74 | }
|
| 75 | }
|
| 76 | }
|
| 77 | echo "status $_status"
|
| 78 |
|
| 79 | try {
|
| 80 | hay eval :result {
|
| 81 | cd /tmp
|
| 82 | }
|
| 83 | }
|
| 84 | echo "status $_status"
|
| 85 |
|
| 86 | ## STDOUT:
|
| 87 | status 127
|
| 88 | status 127
|
| 89 | ## END
|
| 90 |
|
| 91 | #### procs in hay eval
|
| 92 | shopt --set parse_brace parse_at parse_proc
|
| 93 |
|
| 94 | hay define Package
|
| 95 |
|
| 96 | proc outside {
|
| 97 | echo outside
|
| 98 | Package OUT
|
| 99 | }
|
| 100 |
|
| 101 | hay eval :result {
|
| 102 | outside
|
| 103 |
|
| 104 | proc inside {
|
| 105 | echo inside
|
| 106 | }
|
| 107 |
|
| 108 | inside
|
| 109 | }
|
| 110 |
|
| 111 | const args = result['children'][0]['args']
|
| 112 | write --sep ' ' -- $[len(result['children'])] @args
|
| 113 |
|
| 114 | ## STDOUT:
|
| 115 | outside
|
| 116 | inside
|
| 117 | 1 OUT
|
| 118 | ## END
|
| 119 |
|
| 120 |
|
| 121 | #### variables mutated within hay eval don't persist
|
| 122 | shopt --set parse_brace
|
| 123 |
|
| 124 | hay define Package
|
| 125 |
|
| 126 | setvar x = 42
|
| 127 |
|
| 128 | hay eval :result {
|
| 129 | Package foo
|
| 130 |
|
| 131 | setvar x = 1
|
| 132 | }
|
| 133 |
|
| 134 | echo "x = $x"
|
| 135 |
|
| 136 | ## STDOUT:
|
| 137 | x = 42
|
| 138 | ## END
|
| 139 |
|
| 140 |
|
| 141 |
|
| 142 | #### hay at top level allows arbitrary commands
|
| 143 | shopt --set parse_brace
|
| 144 |
|
| 145 | hay define Package
|
| 146 |
|
| 147 | Package $(seq 2) {
|
| 148 | seq 3 4
|
| 149 | }
|
| 150 |
|
| 151 | json write (_hay()) | jq '.children[0].args' > actual.txt
|
| 152 |
|
| 153 | diff -u - actual.txt <<EOF
|
| 154 | [
|
| 155 | "1",
|
| 156 | "2"
|
| 157 | ]
|
| 158 | EOF
|
| 159 |
|
| 160 | hay eval :result {
|
| 161 | echo inside
|
| 162 | Package $(seq 2) {
|
| 163 | seq 3 4
|
| 164 | }
|
| 165 | }
|
| 166 |
|
| 167 | ## status: 127
|
| 168 | ## STDOUT:
|
| 169 | 3
|
| 170 | 4
|
| 171 | inside
|
| 172 | ## END
|
| 173 |
|