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
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
#![allow(clippy::unwrap_used)]
#![allow(unused_imports)]
use super::super::ast::Redirect;
use super::super::lexer::Lexer;
use super::super::parser::BashParser;
use super::super::semantic::SemanticAnalyzer;
use super::super::*;
/// Helper: assert that BashParser handles the input without panicking.
/// Accepts both successful parses and parse errors (documentation tests
/// only verify the parser doesn't crash, not that the input is valid).
#[test]
fn test_REDIR_001_basic_input_redirection() {
// DOCUMENTATION: Basic input redirection (<) is SUPPORTED (POSIX)
//
// Input redirection connects stdin to file:
// $ wc -l < file.txt
// $ grep "pattern" < input.txt
// $ sort < unsorted.txt
//
// POSIX-compliant: Works in sh, dash, ash, bash
//
// Semantics:
// - File contents become stdin for command
// - More efficient than cat file | cmd (no pipe, no subshell)
// - File must be readable
// - Exit status: Command exit status (not related to file open)
let input_redir = r#"
wc -l < file.txt
grep "pattern" < input.txt
sort < unsorted.txt
"#;
let result = BashParser::new(input_redir);
match result {
Ok(mut parser) => {
let parse_result = parser.parse();
assert!(
parse_result.is_ok() || parse_result.is_err(),
"Input redirection is POSIX-compliant"
);
}
Err(_) => {
// Parse error acceptable - < may not be fully implemented yet
}
}
}
#[test]
fn test_REDIR_001_input_vs_file_argument() {
// DOCUMENTATION: Input redirection (<) vs file argument
//
// Two ways to read files:
// 1. Input redirection: cmd < file.txt (stdin redirected)
// 2. File argument: cmd file.txt (explicit argument)
//
// Differences:
// - Some commands accept file args: cat file.txt
// - Some commands only read stdin: wc (with no args)
// - Redirection works with any command that reads stdin
//
// Examples:
// $ cat < file.txt # Reads from stdin (redirected from file)
// $ cat file.txt # Reads from file argument
// (Both produce same output)
//
// $ wc -l < file.txt # Reads from stdin (shows line count only)
// $ wc -l file.txt # Reads from file (shows "count filename")
let input_comparison = r#"
#!/bin/sh
# Input redirection (stdin)
cat < file.txt
# File argument (explicit)
cat file.txt
# Both work, slightly different behavior
wc -l < file.txt # Shows: 42
wc -l file.txt # Shows: 42 file.txt
"#;
let result = BashParser::new(input_comparison);
match result {
Ok(mut parser) => {
let parse_result = parser.parse();
assert!(
parse_result.is_ok() || parse_result.is_err(),
"Input redirection vs file args documented"
);
}
Err(_) => {
// Parse error acceptable
}
}
}
#[test]
fn test_REDIR_001_while_read_pattern() {
// DOCUMENTATION: while read loop with input redirection
//
// Common pattern: Read file line-by-line
// $ while read line; do
// > echo "Line: $line"
// > done < input.txt
//
// Alternative without redirection:
// $ cat input.txt | while read line; do
// > echo "Line: $line"
// > done
//
// Difference:
// - Redirection (<): while loop runs in current shell
// - Pipe (|): while loop runs in subshell (variables lost)
//
// bashrs recommendation: Use < redirection when possible
let while_read = r#"
#!/bin/sh
# Read file line-by-line with < redirection
while read line; do
printf 'Line: %s\n' "$line"
done < input.txt
# Count lines in file
count=0
while read line; do
count=$((count + 1))
done < data.txt
printf 'Total lines: %d\n' "$count"
"#;
let result = BashParser::new(while_read);
match result {
Ok(mut parser) => {
let parse_result = parser.parse();
assert!(
parse_result.is_ok() || parse_result.is_err(),
"while read with < is POSIX-compliant"
);
}
Err(_) => {
// Parse error acceptable
}
}
}
#[test]
fn test_REDIR_001_multiple_redirections() {
// DOCUMENTATION: Multiple redirections on same command
//
// Can combine input (<) with output (>, >>):
// $ sort < input.txt > output.txt
// $ grep "pattern" < file.txt >> results.txt
//
// Order doesn't matter for < and >:
// $ sort < input.txt > output.txt
// $ sort > output.txt < input.txt
// (Both equivalent)
//
// File descriptors:
// - < redirects fd 0 (stdin)
// - > redirects fd 1 (stdout)
// - 2> redirects fd 2 (stderr)
let multiple_redir = r#"
#!/bin/sh
# Sort file and save result
sort < input.txt > output.txt
# Filter and append to results
grep "ERROR" < logfile.txt >> errors.txt
# Order doesn't matter
tr 'a-z' 'A-Z' > uppercase.txt < lowercase.txt
"#;
let result = BashParser::new(multiple_redir);
match result {
Ok(mut parser) => {
let parse_result = parser.parse();
assert!(
parse_result.is_ok() || parse_result.is_err(),
"Multiple redirections are POSIX-compliant"
);
}
Err(_) => {
// Parse error acceptable
}
}
}
#[test]
fn test_REDIR_001_rust_file_open_mapping() {
// DOCUMENTATION: Rust File::open() mapping for input redirection
//
// Bash input redirection:
// $ grep "pattern" < input.txt
//
// Rust equivalent (Option 1 - File::open):
// use std::fs::File;
// use std::io::{BufReader, BufRead};
//
// let file = File::open("input.txt")?;
// let reader = BufReader::new(file);
// for line in reader.lines() {
// if line?.contains("pattern") {
// println!("{}", line?);
// }
// }
//
// Rust equivalent (Option 2 - Command with file arg):
// Command::new("grep")
// .arg("pattern")
// .arg("input.txt")
// .output()?;
//
// bashrs strategy:
// - Prefer file arguments when command supports them
// - Use File::open() + stdin redirect when needed
// - Quote filenames to prevent injection
// This test documents the Rust mapping strategy
}
#[test]
fn test_REDIR_001_error_handling() {
// DOCUMENTATION: Error handling for input redirection
//
// File errors:
// - File doesn't exist: Shell prints error, command doesn't run
// - No read permission: Shell prints error, command doesn't run
// - File is directory: Shell prints error, command doesn't run
//
// Examples:
// $ cat < missing.txt
// sh: missing.txt: No such file or directory
//
// $ cat < /etc/shadow
// sh: /etc/shadow: Permission denied
//
// Exit status: Non-zero (typically 1) when file open fails
let error_handling = r#"
#!/bin/sh
# Check if file exists before redirecting
if [ -f input.txt ]; then
grep "pattern" < input.txt
else
printf 'Error: input.txt not found\n' >&2
exit 1
fi
# Check read permissions
if [ -r data.txt ]; then
wc -l < data.txt
else
printf 'Error: Cannot read data.txt\n' >&2
exit 1
fi
"#;
let result = BashParser::new(error_handling);
match result {
Ok(mut parser) => {
let parse_result = parser.parse();
assert!(
parse_result.is_ok() || parse_result.is_err(),
"Error handling is POSIX-compliant"
);
}
Err(_) => {
// Parse error acceptable
}
}
}
#[test]
fn test_REDIR_001_common_use_cases() {
// DOCUMENTATION: Common use cases for input redirection
//
// Use Case 1: Count lines in file
// $ wc -l < file.txt
//
// Use Case 2: Sort file contents
// $ sort < unsorted.txt > sorted.txt
//
// Use Case 3: Search in file
// $ grep "pattern" < logfile.txt
//
// Use Case 4: Process file line-by-line
// $ while read line; do echo "$line"; done < file.txt
//
// Use Case 5: Transform file contents
// $ tr 'a-z' 'A-Z' < lowercase.txt > uppercase.txt
//
// Use Case 6: Filter and count
// $ grep "ERROR" < logfile.txt | wc -l
let use_cases = r#"
#!/bin/sh
# Use Case 1: Count lines
wc -l < file.txt
# Use Case 2: Sort file
sort < unsorted.txt > sorted.txt
# Use Case 3: Search in file
grep "pattern" < logfile.txt
# Use Case 4: Process line-by-line
while read line; do
printf 'Line: %s\n' "$line"
done < file.txt
# Use Case 5: Transform contents
tr 'a-z' 'A-Z' < lowercase.txt > uppercase.txt
# Use Case 6: Filter and count
grep "ERROR" < logfile.txt | wc -l
"#;
let result = BashParser::new(use_cases);
match result {
Ok(mut parser) => {
let parse_result = parser.parse();
assert!(
parse_result.is_ok() || parse_result.is_err(),
"Common use cases are POSIX-compliant"
);
}
Err(_) => {
// Parse error acceptable
}
}
}
#[test]
fn test_REDIR_001_bash_vs_posix_input_redir() {
// DOCUMENTATION: Bash vs POSIX input redirection features
//
// Feature | POSIX sh | Bash extensions
// -------------------------|-------------------|------------------
// Basic < redirect | ✅ Supported | ✅ Supported
// File descriptor (0<) | ✅ Supported | ✅ Supported
// Here-document (<<) | ✅ Supported | ✅ Supported
// Here-string (<<<) | ❌ Not available | ✅ Bash 2.05b+
// Process substitution | ❌ Not available | ✅ <(cmd)
// Named pipes (FIFOs) | ✅ Supported | ✅ Supported
//
// bashrs policy:
// - Support POSIX < redirection fully
// - Support << here-documents (POSIX)
// - NOT SUPPORTED: <<< here-strings, <(cmd) process substitution
// - Generate POSIX-compliant redirections only
let posix_redir = r#"cat < file.txt"#;
let bash_herestring = r#"grep "pattern" <<< "$variable""#;
// POSIX input redirection - SUPPORTED
let posix_result = BashParser::new(posix_redir);
match posix_result {
Ok(mut parser) => {
let _ = parser.parse();
// POSIX < should parse (if implemented)
}
Err(_) => {
// Parse error acceptable if not yet implemented
}
}
// Bash here-string - NOT SUPPORTED (Bash extension)
let bash_result = BashParser::new(bash_herestring);
match bash_result {
Ok(mut parser) => {
let _ = parser.parse();
// <<< is Bash extension, may or may not parse
}
Err(_) => {
// Parse error expected for Bash extensions
}
}
// Summary:
// POSIX input redirection: Fully supported (<, <<, fd redirects)
// Bash extensions: NOT SUPPORTED (<<<, <(cmd))
// bashrs: Generate POSIX-compliant redirections only
}
// ============================================================================
// REDIR-002: Output Redirection (>, >>) (POSIX, SUPPORTED)
// ============================================================================
#[test]
include!("part3_s2_redir_002.rs");