Commit 060623b
authored
Add printf, read, break, continue, command, function and type builtins (#280)
* Harden shell: eliminate panics, implement all file test operators, fix path resolution
- Replace panic!() in parser (for loop wordlist) and types (arithmetic) with
proper error handling/fallback behavior
- Replace unreachable!() in parser (and_or lists, boolean operators), execute
(assignment operators), and main (script/command dispatch) with descriptive errors
- Implement all 14 previously unimplemented file test operators: -b (block special),
-c (char special), -g (setgid), -k (sticky bit), -p (named pipe), -s (size > 0),
-t (terminal fd), -u (setuid), -G (owned by egid), -N (modified since read),
-O (owned by euid), -S (socket), -R (nameref), and None (implicit -n)
- Add -e (file exists) and -t (terminal fd) to grammar and parser
- Fix file test path resolution to use shell CWD instead of process CWD
- Fix critical unwrap() calls in main.rs (current_dir), execute.rs (parse result),
uname.rs, touch.rs (DST-ambiguous datetimes), set.rs, and mod.rs (clear command)
- Add comprehensive test suite for file test operators (3 new test functions)
* Add missing bash builtins: break, continue, :, printf, read, command, type
Implement 7 new shell builtins for improved bash compatibility:
- break/continue: Loop control flow via sentinel exit codes (-100/-101)
that propagate through ExecuteResult::Exit and are caught by while/for loops
- : (colon): No-op command that always returns 0
- printf: Format string support with %s, %d, %o, %x, %f, %c, %b specifiers,
width/precision/flags, and standard escape sequences
- read: Read input with -r (raw) and -p (prompt) flags, field splitting,
default REPLY variable
- command: Run commands bypassing aliases, -v flag for path lookup
- type: Describe how a command name resolves (alias, builtin, or PATH)
All builtins include comprehensive tests.
* Add bash builtins: eval, source, shift, local, return, trap
Implement 6 more shell builtins for improved bash compatibility:
- eval: Parse and execute string arguments as shell commands
- source/.: Fix to use context pipes instead of real stdout/stderr,
enabling proper pipe capture in tests and subshells. Read file,
parse, and execute in current shell context with env change propagation
- shift: Shift positional parameters ($1, $2, ...) left by N
- local: Declare shell-scoped variables (foundation for future
function support)
- return: Exit from sourced scripts with specified exit code via
ExecuteResult::Exit propagation
- trap: Basic signal handling with -l (list signals), -p (print traps),
and handler registration acceptance for compatibility
All builtins include comprehensive tests. Total: 46 tests passing.
https://claude.ai/code/session_01GTXgqqxHbE7ACysQDZ9cLU
* Implement shell function definitions, positional parameters, and brace groups
- Add function_definition parsing (foo() { ... }) with FunctionDefinition AST node
- Store functions in ShellState and execute via execute_shell_function
- Support positional parameters ($1-$N, $#, $@, $*) with save/restore across calls
- Fix grammar: UNQUOTED_ESCAPE_CHAR now excludes SPECIAL_PARAM so $1/$2/etc parse correctly
- Fix RETURN_EXIT_CODE sentinel handling at top level and in function scope
- Sync last_command_exit_code with shell var "?" for proper $? expansion
- Add brace group compound command support
- Add comprehensive tests for functions, positional params, return values, nested calls
* fix lint / fmt
* Add e2e test-data files for functions and builtins
- functions.sh: tests for function definitions, positional parameters
($1-$N, $#, $@), nested calls, variable scoping, and function overriding
- builtins.sh: tests for eval, shift, local, and brace groups1 parent 7a197ad commit 060623b
File tree
25 files changed
+2399
-94
lines changed- crates
- deno_task_shell
- src
- shell
- commands
- shell/src
- commands
- tests
- src
- test-data
25 files changed
+2399
-94
lines changedSome generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
30 | 30 | | |
31 | 31 | | |
32 | 32 | | |
| 33 | + | |
| 34 | + | |
| 35 | + | |
33 | 36 | | |
34 | 37 | | |
35 | 38 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
77 | 77 | | |
78 | 78 | | |
79 | 79 | | |
80 | | - | |
81 | | - | |
82 | | - | |
| 80 | + | |
| 81 | + | |
| 82 | + | |
83 | 83 | | |
84 | 84 | | |
85 | 85 | | |
| |||
192 | 192 | | |
193 | 193 | | |
194 | 194 | | |
195 | | - | |
196 | | - | |
| 195 | + | |
| 196 | + | |
197 | 197 | | |
198 | 198 | | |
199 | 199 | | |
| |||
347 | 347 | | |
348 | 348 | | |
349 | 349 | | |
350 | | - | |
351 | | - | |
| 350 | + | |
| 351 | + | |
352 | 352 | | |
353 | 353 | | |
354 | 354 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
170 | 170 | | |
171 | 171 | | |
172 | 172 | | |
| 173 | + | |
| 174 | + | |
173 | 175 | | |
174 | 176 | | |
175 | 177 | | |
| |||
238 | 240 | | |
239 | 241 | | |
240 | 242 | | |
| 243 | + | |
| 244 | + | |
| 245 | + | |
| 246 | + | |
| 247 | + | |
| 248 | + | |
| 249 | + | |
| 250 | + | |
| 251 | + | |
241 | 252 | | |
242 | 253 | | |
243 | 254 | | |
| |||
661 | 672 | | |
662 | 673 | | |
663 | 674 | | |
664 | | - | |
| 675 | + | |
665 | 676 | | |
666 | 677 | | |
667 | 678 | | |
668 | | - | |
| 679 | + | |
| 680 | + | |
| 681 | + | |
| 682 | + | |
| 683 | + | |
| 684 | + | |
669 | 685 | | |
670 | 686 | | |
671 | 687 | | |
| |||
779 | 795 | | |
780 | 796 | | |
781 | 797 | | |
782 | | - | |
| 798 | + | |
| 799 | + | |
| 800 | + | |
783 | 801 | | |
784 | 802 | | |
785 | 803 | | |
786 | | - | |
| 804 | + | |
| 805 | + | |
| 806 | + | |
787 | 807 | | |
788 | 808 | | |
789 | 809 | | |
| |||
796 | 816 | | |
797 | 817 | | |
798 | 818 | | |
799 | | - | |
| 819 | + | |
| 820 | + | |
| 821 | + | |
| 822 | + | |
| 823 | + | |
| 824 | + | |
800 | 825 | | |
801 | 826 | | |
802 | | - | |
| 827 | + | |
| 828 | + | |
| 829 | + | |
803 | 830 | | |
804 | 831 | | |
805 | 832 | | |
| |||
910 | 937 | | |
911 | 938 | | |
912 | 939 | | |
913 | | - | |
914 | | - | |
915 | | - | |
| 940 | + | |
916 | 941 | | |
917 | 942 | | |
918 | 943 | | |
| |||
1010 | 1035 | | |
1011 | 1036 | | |
1012 | 1037 | | |
1013 | | - | |
| 1038 | + | |
1014 | 1039 | | |
1015 | 1040 | | |
1016 | 1041 | | |
| |||
1048 | 1073 | | |
1049 | 1074 | | |
1050 | 1075 | | |
1051 | | - | |
1052 | | - | |
1053 | | - | |
| 1076 | + | |
1054 | 1077 | | |
1055 | 1078 | | |
1056 | 1079 | | |
| |||
1116 | 1139 | | |
1117 | 1140 | | |
1118 | 1141 | | |
| 1142 | + | |
| 1143 | + | |
| 1144 | + | |
| 1145 | + | |
| 1146 | + | |
| 1147 | + | |
| 1148 | + | |
| 1149 | + | |
| 1150 | + | |
| 1151 | + | |
| 1152 | + | |
| 1153 | + | |
| 1154 | + | |
| 1155 | + | |
| 1156 | + | |
| 1157 | + | |
| 1158 | + | |
| 1159 | + | |
| 1160 | + | |
| 1161 | + | |
| 1162 | + | |
| 1163 | + | |
| 1164 | + | |
| 1165 | + | |
| 1166 | + | |
| 1167 | + | |
| 1168 | + | |
| 1169 | + | |
| 1170 | + | |
| 1171 | + | |
| 1172 | + | |
| 1173 | + | |
| 1174 | + | |
| 1175 | + | |
| 1176 | + | |
| 1177 | + | |
| 1178 | + | |
| 1179 | + | |
| 1180 | + | |
| 1181 | + | |
| 1182 | + | |
| 1183 | + | |
| 1184 | + | |
| 1185 | + | |
| 1186 | + | |
| 1187 | + | |
| 1188 | + | |
| 1189 | + | |
| 1190 | + | |
| 1191 | + | |
| 1192 | + | |
| 1193 | + | |
| 1194 | + | |
| 1195 | + | |
| 1196 | + | |
| 1197 | + | |
| 1198 | + | |
| 1199 | + | |
| 1200 | + | |
1119 | 1201 | | |
1120 | 1202 | | |
1121 | 1203 | | |
| |||
1265 | 1347 | | |
1266 | 1348 | | |
1267 | 1349 | | |
1268 | | - | |
| 1350 | + | |
1269 | 1351 | | |
1270 | 1352 | | |
1271 | 1353 | | |
| |||
1276 | 1358 | | |
1277 | 1359 | | |
1278 | 1360 | | |
| 1361 | + | |
1279 | 1362 | | |
1280 | 1363 | | |
1281 | 1364 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
205 | 205 | | |
206 | 206 | | |
207 | 207 | | |
| 208 | + | |
208 | 209 | | |
209 | 210 | | |
210 | 211 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
| 1 | + | |
| 2 | + | |
| 3 | + | |
| 4 | + | |
| 5 | + | |
| 6 | + | |
| 7 | + | |
| 8 | + | |
| 9 | + | |
| 10 | + | |
| 11 | + | |
| 12 | + | |
| 13 | + | |
| 14 | + | |
| 15 | + | |
| 16 | + | |
| 17 | + | |
| 18 | + | |
| 19 | + | |
| 20 | + | |
| 21 | + | |
| 22 | + | |
| 23 | + | |
| 24 | + | |
| 25 | + | |
| 26 | + | |
| 27 | + | |
| 28 | + | |
| 29 | + | |
| 30 | + | |
| 31 | + | |
| 32 | + | |
| 33 | + | |
| 34 | + | |
| 35 | + | |
| 36 | + | |
| 37 | + | |
| 38 | + | |
| 39 | + | |
| 40 | + | |
| 41 | + | |
| 42 | + | |
| 43 | + | |
| 44 | + | |
| 45 | + | |
| 46 | + | |
| 47 | + | |
| 48 | + | |
| 49 | + | |
| 50 | + | |
| 51 | + | |
| 52 | + | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
| 1 | + | |
| 2 | + | |
| 3 | + | |
| 4 | + | |
| 5 | + | |
| 6 | + | |
| 7 | + | |
| 8 | + | |
| 9 | + | |
| 10 | + | |
| 11 | + | |
| 12 | + | |
| 13 | + | |
| 14 | + | |
| 15 | + | |
| 16 | + | |
| 17 | + | |
| 18 | + | |
| 19 | + | |
| 20 | + | |
| 21 | + | |
| 22 | + | |
| 23 | + | |
| 24 | + | |
| 25 | + | |
| 26 | + | |
| 27 | + | |
| 28 | + | |
| 29 | + | |
| 30 | + | |
| 31 | + | |
| 32 | + | |
| 33 | + | |
| 34 | + | |
| 35 | + | |
| 36 | + | |
| 37 | + | |
| 38 | + | |
| 39 | + | |
| 40 | + | |
| 41 | + | |
| 42 | + | |
| 43 | + | |
| 44 | + | |
| 45 | + | |
| 46 | + | |
| 47 | + | |
| 48 | + | |
| 49 | + | |
| 50 | + | |
| 51 | + | |
| 52 | + | |
| 53 | + | |
| 54 | + | |
| 55 | + | |
| 56 | + | |
| 57 | + | |
| 58 | + | |
| 59 | + | |
| 60 | + | |
| 61 | + | |
| 62 | + | |
| 63 | + | |
| 64 | + | |
| 65 | + | |
| 66 | + | |
| 67 | + | |
| 68 | + | |
| 69 | + | |
| 70 | + | |
| 71 | + | |
| 72 | + | |
| 73 | + | |
| 74 | + | |
| 75 | + | |
| 76 | + | |
| 77 | + | |
| 78 | + | |
| 79 | + | |
| 80 | + | |
0 commit comments