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
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
use Redirect;
use Lexer;
use BashParser;
use SemanticAnalyzer;
use *;
/// Helper: tokenize input and assert tokens are non-empty.
/// Accepts parse errors gracefully (parser may not support all constructs yet).
// Summary:
// Tilde expansion ~: POSIX, FULLY SUPPORTED
// ~ expands to $HOME (user's home directory)
// ~user expands to user's home directory (looked up in /etc/passwd)
// ~+ and ~- are bash extensions (NOT SUPPORTED, use $PWD and $OLDPWD)
// Tilde must be at start of word to expand
// Tilde doesn't expand when quoted ("~" or '~')
// Tilde expands in variable assignments (DIR=~/projects)
// Tilde expands after : in PATH-like variables (PATH=~/bin:/usr/bin)
// Common uses: cd ~, ls ~/documents, mkdir ~/backup, PATH=~/bin:$PATH
// Best practice: Use ~ for convenience, $HOME for clarity, both are POSIX
// ============================================================================
// BUILTIN-005: cd command (POSIX builtin)
// ============================================================================
// Task: Document cd (change directory) builtin command
// Reference: GNU Bash Manual Section 4.1 (Bourne Shell Builtins)
// POSIX: cd is POSIX-COMPLIANT (SUPPORTED)
//
// Syntax:
// cd [directory]
// cd - # Go to previous directory ($OLDPWD)
// cd # Go to home directory ($HOME)
// cd ~ # Go to home directory (tilde expansion)
// cd ~/path # Go to home/path
//
// POSIX Compliance:
// SUPPORTED: cd /path, cd -, cd (no args), cd ~, cd ~/path
// SUPPORTED: Uses $HOME, $OLDPWD, $PWD environment variables
// SUPPORTED: Returns exit status 0 (success) or 1 (failure)
// SUPPORTED: Updates $PWD and $OLDPWD automatically
//
// Bash Extensions:
// -L (default): Follow symbolic links
// -P: Use physical directory structure (resolve symlinks)
// -e: Exit if cd fails (with -P)
// -@: Present extended attributes as directory (rare)
// CDPATH: Search path for directories (bash/ksh extension)
//
// bashrs Support:
// SUPPORTED: Basic cd /path navigation
// SUPPORTED: cd - (previous directory via $OLDPWD)
// SUPPORTED: cd (no args, go to $HOME)
// SUPPORTED: cd ~ (tilde expansion to $HOME)
// SUPPORTED: cd ~/path (tilde expansion)
// NOT SUPPORTED: -L, -P, -e, -@ flags (bash extensions)
// NOT SUPPORTED: CDPATH search path (bash/ksh extension)
//
// Rust Mapping:
// cd /path → std::env::set_current_dir("/path")
// cd - → std::env::set_current_dir(&env::var("OLDPWD"))
// cd → std::env::set_current_dir(&env::home_dir())
// cd ~ → std::env::set_current_dir(&env::home_dir())
//
// Purified Bash:
// cd /path → cd "/path" (quote path for safety)
// cd "$dir" → cd "$dir" (preserve quoting)
// cd - → cd - (POSIX supported)
// cd → cd (POSIX supported)
// cd ~ → cd ~ (POSIX tilde expansion)
// cd -L /path → cd "/path" (strip bash-specific flags)
// cd -P /path → cd "/path" (strip bash-specific flags)
//
// Environment Variables:
// $PWD: Current working directory (updated by cd)
// $OLDPWD: Previous working directory (updated by cd)
// $HOME: Home directory (used by cd with no args)
// $CDPATH: Search path (bash/ksh extension, not POSIX)
//
// Exit Status:
// 0: Success (directory changed)
// 1: Failure (directory doesn't exist, no permissions, etc.)
//
// Common Use Cases:
// 1. Navigate to directory: cd /tmp
// 2. Go to home directory: cd or cd ~
// 3. Go to previous directory: cd -
// 4. Navigate to subdirectory: cd src/main
// 5. Navigate to parent directory: cd ..
// 6. Navigate with variable: cd "$PROJECT_DIR"
//
// Edge Cases:
// 1. cd with no args → go to $HOME
// 2. cd - with no $OLDPWD → error (variable not set)
// 3. cd to nonexistent directory → returns 1, prints error
// 4. cd with permissions denied → returns 1, prints error
// 5. cd to symlink → follows symlink by default
// 6. cd with spaces → requires quoting: cd "My Documents"
//
// Best Practices:
// 1. Always quote paths with spaces: cd "$dir"
// 2. Check exit status for error handling: cd /tmp || exit 1
// 3. Use cd - to toggle between two directories
// 4. Use absolute paths for determinism
// 5. Avoid CDPATH in portable scripts (not POSIX)
//
// POSIX vs Bash Comparison:
//
// | Feature | POSIX | Bash | bashrs | Notes |
// |----------------------|-------|------|--------|--------------------------------|
// | cd /path | ✓ | ✓ | ✓ | Basic directory navigation |
// | cd - | ✓ | ✓ | ✓ | Previous directory ($OLDPWD) |
// | cd (no args) | ✓ | ✓ | ✓ | Go to $HOME |
// | cd ~ | ✓ | ✓ | ✓ | Tilde expansion to $HOME |
// | cd ~/path | ✓ | ✓ | ✓ | Tilde expansion |
// | cd -L /path | ✗ | ✓ | ✗ | Follow symlinks (bash default) |
// | cd -P /path | ✗ | ✓ | ✗ | Physical directory structure |
// | cd -e /path | ✗ | ✓ | ✗ | Exit on failure (with -P) |
// | cd -@ /path | ✗ | ✓ | ✗ | Extended attributes (rare) |
// | CDPATH search | ✗ | ✓ | ✗ | Directory search path |
// | $PWD update | ✓ | ✓ | ✓ | Updated automatically |
// | $OLDPWD update | ✓ | ✓ | ✓ | Updated automatically |
// | Exit status 0/1 | ✓ | ✓ | ✓ | Success/failure |
//
// ✓ = Supported
// ✗ = Not supported
//
// Summary:
// cd command: POSIX, FULLY SUPPORTED (basic navigation)
// Bash extensions (-L, -P, -e, -@, CDPATH): NOT SUPPORTED
// cd changes current working directory, updates $PWD and $OLDPWD
// cd - goes to previous directory, cd (no args) goes to $HOME
// Always quote paths with spaces for safety
// Check exit status for error handling
// Use absolute paths for determinism in automation scripts
include!;