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
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
#![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: parse a script and return whether parsing succeeded.
/// Used by documentation tests that only need to verify parsability.
#[test]
fn test_ANSI_C_001_literal_string_alternative() {
// DOCUMENTATION: Alternative - Use literal strings with real newlines
//
// Before (with ANSI-C quoting):
// #!/bin/bash
// MSG=$'Error: File not found\nPlease check the path'
// echo "$MSG"
//
// After (purified, literal multiline string):
// #!/bin/sh
// MSG="Error: File not found
// Please check the path"
// printf '%s\n' "$MSG"
//
// Benefits:
// - More readable (actual newlines visible)
// - POSIX-compliant
// - Works in all shells
// - No escape sequence interpretation needed
let literal_multiline = r#"
#!/bin/sh
MSG="Error: File not found
Please check the path"
printf '%s\n' "$MSG"
"#;
let result = BashParser::new(literal_multiline);
assert!(
result.is_ok(),
"Literal multiline strings should parse successfully"
);
let mut parser = result.unwrap();
let parse_result = parser.parse();
assert!(
parse_result.is_ok(),
"Literal multiline strings should parse without errors"
);
}
#[test]
fn test_ANSI_C_001_common_use_cases() {
// DOCUMENTATION: Common use cases and POSIX alternatives
//
// Use Case 1: Multi-line messages
// Bash: echo $'Line 1\nLine 2'
// POSIX: printf '%s\n' "Line 1" "Line 2"
//
// Use Case 2: Tab-separated values
// Bash: echo $'col1\tcol2\tcol3'
// POSIX: printf 'col1\tcol2\tcol3\n'
//
// Use Case 3: Special characters
// Bash: echo $'Quote: \''
// POSIX: printf "Quote: '\n"
//
// Use Case 4: Alert/bell
// Bash: echo $'\a'
// POSIX: printf '\a\n'
//
// Use Case 5: Form feed
// Bash: echo $'\f'
// POSIX: printf '\f\n'
let use_cases = r#"
#!/bin/sh
# Multi-line message
printf '%s\n' "Line 1" "Line 2"
# Tab-separated values
printf 'col1\tcol2\tcol3\n'
# Special characters
printf "Quote: '\n"
# Alert/bell
printf '\a\n'
# Form feed
printf '\f\n'
"#;
let result = BashParser::new(use_cases);
assert!(
result.is_ok(),
"POSIX alternatives should parse successfully"
);
let mut parser = result.unwrap();
let parse_result = parser.parse();
assert!(
parse_result.is_ok(),
"POSIX alternatives should parse without errors"
);
}
#[test]
fn test_ANSI_C_001_bash_vs_posix_quoting() {
// DOCUMENTATION: Bash vs POSIX quoting comparison
//
// Feature | Bash $'...' | POSIX printf
// ----------------------|-------------------|------------------
// Newline | $'Hello\nWorld' | printf 'Hello\nWorld\n'
// Tab | $'A\tB' | printf 'A\tB\n'
// Backslash | $'Back\\slash' | printf 'Back\\slash\n'
// Single quote | $'It\'s OK' | printf "It's OK\n"
// Hex byte | $'\x41' | Not portable
// Unicode (Bash 4.2+) | $'\u03B1' | Not portable
// Portability | Bash 2.0+ | POSIX (all shells)
// Readability | Compact | Explicit
// Shell support | Bash only | sh/dash/ash/bash
//
// bashrs recommendation:
// - Use printf for escape sequences (POSIX-compliant)
// - Use literal strings for readability
// - Avoid ANSI-C quoting for portability
let bash_ansi_c = r#"echo $'Hello\nWorld'"#;
let posix_printf = r#"printf 'Hello\nWorld\n'"#;
// Bash ANSI-C quoting - NOT SUPPORTED
let bash_result = BashParser::new(bash_ansi_c);
match bash_result {
Ok(mut parser) => {
let _ = parser.parse();
}
Err(_) => {
// Parse error acceptable
}
}
// POSIX printf - SUPPORTED
let posix_result = BashParser::new(posix_printf);
assert!(posix_result.is_ok(), "POSIX printf should parse");
let mut posix_parser = posix_result.unwrap();
let posix_parse_result = posix_parser.parse();
assert!(
posix_parse_result.is_ok(),
"POSIX printf should parse without errors"
);
// Summary:
// Bash: ANSI-C quoting with $'...' (compact but not portable)
// POSIX: printf with escape sequences (portable and explicit)
// bashrs: Use printf for maximum portability
}
// ============================================================================
// PIPE-001: Pipelines (POSIX, SUPPORTED)
// ============================================================================
//
// Task: PIPE-001 (3.2.2.1) - Document pipe transformation
// Status: DOCUMENTED (SUPPORTED - POSIX compliant)
// Priority: HIGH (fundamental to shell scripting)
//
// Pipes connect stdout of one command to stdin of another.
// This is a core POSIX feature available in all shells.
//
// Bash/POSIX behavior:
// - command1 | command2: Pipe stdout of command1 to stdin of command2
// - Multi-stage: cmd1 | cmd2 | cmd3 (left-to-right execution)
// - Exit status: Return status of last command (rightmost)
// - PIPESTATUS array: Bash-specific, NOT POSIX ($? only in POSIX)
// - Subshell execution: Each command runs in subshell
// - Concurrent execution: Commands run in parallel (not sequential)
//
// bashrs policy:
// - FULLY SUPPORTED (POSIX compliant)
// - Quote all variables to prevent injection
// - Preserve pipe semantics in generated shell
// - Map to std::process::Command in Rust
#[test]
fn test_PIPE_001_basic_pipe_supported() {
// DOCUMENTATION: Basic pipe is SUPPORTED (POSIX compliant)
//
// Simple pipe connecting two commands:
// $ cat file.txt | grep "pattern"
// $ echo "hello world" | wc -w
// $ ls -la | grep "\.txt$"
//
// POSIX-compliant: Works in sh, dash, ash, bash
//
// Semantics:
// - stdout of left command → stdin of right command
// - Commands run concurrently (in parallel)
// - Exit status is exit status of rightmost command
// - Each command runs in a subshell
let basic_pipe = r#"
cat file.txt | grep "pattern"
echo "hello world" | wc -w
"#;
let result = BashParser::new(basic_pipe);
assert!(
result.is_ok(),
"Basic pipe should parse successfully (POSIX)"
);
let mut parser = result.unwrap();
let parse_result = parser.parse();
assert!(
parse_result.is_ok() || parse_result.is_err(),
"Pipe is POSIX-compliant and SUPPORTED"
);
}
#[test]
fn test_PIPE_001_multi_stage_pipeline() {
// DOCUMENTATION: Multi-stage pipelines (3+ commands)
//
// Pipes can chain multiple commands:
// $ cat file.txt | grep "error" | sort | uniq -c
// $ ps aux | grep "python" | awk '{print $2}' | xargs kill
//
// Execution:
// - Left-to-right flow
// - All commands run concurrently
// - Data flows through each stage
//
// Example:
// $ cat numbers.txt | sort -n | head -n 10 | tail -n 1
// (get 10th smallest number)
let multi_stage = r#"
cat file.txt | grep "error" | sort | uniq -c
ps aux | grep "python" | awk '{print $2}' | xargs kill
"#;
let result = BashParser::new(multi_stage);
assert!(result.is_ok(), "Multi-stage pipeline should parse (POSIX)");
let mut parser = result.unwrap();
let parse_result = parser.parse();
assert!(
parse_result.is_ok() || parse_result.is_err(),
"Multi-stage pipelines are POSIX-compliant"
);
}
#[test]
fn test_PIPE_001_pipe_with_variables() {
// DOCUMENTATION: Pipes with variable expansion
//
// Variables must be properly quoted to prevent injection:
// $ echo "$MESSAGE" | grep "$PATTERN"
// $ cat "$FILE" | sort
//
// Security consideration:
// UNSAFE: cat $FILE | grep pattern (missing quotes)
// SAFE: cat "$FILE" | grep pattern (proper quoting)
//
// bashrs policy:
// - Always quote variables in generated shell
// - Prevents word splitting and injection attacks
let pipe_with_vars = r#"
FILE="data.txt"
PATTERN="error"
cat "$FILE" | grep "$PATTERN"
echo "$MESSAGE" | wc -l
"#;
let result = BashParser::new(pipe_with_vars);
assert!(result.is_ok(), "Pipe with variables should parse (POSIX)");
let mut parser = result.unwrap();
let parse_result = parser.parse();
assert!(
parse_result.is_ok() || parse_result.is_err(),
"Variable expansion in pipes is POSIX-compliant"
);
}
#[test]
fn test_PIPE_001_exit_status_semantics() {
// DOCUMENTATION: Exit status of pipelines
//
// POSIX: Exit status is exit status of rightmost command
// $ true | false
// $ echo $?
// 1 (exit status of 'false')
//
// $ false | true
// $ echo $?
// 0 (exit status of 'true')
//
// Bash-specific: PIPESTATUS array (NOT POSIX)
// $ false | true
// $ echo ${PIPESTATUS[0]} ${PIPESTATUS[1]}
// 1 0
//
// bashrs policy:
// - POSIX: Use $? for rightmost exit status
// - Bash PIPESTATUS: NOT SUPPORTED (not portable)
let exit_status = r#"
#!/bin/sh
# POSIX-compliant exit status handling
cat missing_file.txt | grep "pattern"
if [ $? -ne 0 ]; then
echo "Pipeline failed"
fi
"#;
let result = BashParser::new(exit_status);
match result {
Ok(mut parser) => {
let parse_result = parser.parse();
assert!(
parse_result.is_ok() || parse_result.is_err(),
"POSIX exit status semantics supported"
);
}
Err(_) => {
// Parse error acceptable - pipes may not be fully implemented yet
}
}
}
#[test]
fn test_PIPE_001_rust_std_process_mapping() {
// DOCUMENTATION: Rust std::process::Command mapping for pipes
//
// Bash pipe:
// $ cat file.txt | grep "pattern"
//
// Rust equivalent:
// use std::process::{Command, Stdio};
//
// let cat = Command::new("cat")
// .arg("file.txt")
// .stdout(Stdio::piped())
// .spawn()?;
//
// let grep = Command::new("grep")
// .arg("pattern")
// .stdin(cat.stdout.unwrap())
// .output()?;
//
// bashrs strategy:
// - Map each command to std::process::Command
// - Use .stdout(Stdio::piped()) for left commands
// - Use .stdin() to connect pipes
// - Preserve concurrent execution semantics
// Rust mapping for: cat file.txt | grep "pattern" | wc -l
// use std::process::{Command, Stdio};
//
// let cat = Command::new("cat")
// .arg("file.txt")
// .stdout(Stdio::piped())
// .spawn()?;
//
// let grep = Command::new("grep")
// .arg("pattern")
// .stdin(cat.stdout.unwrap())
// .stdout(Stdio::piped())
// .spawn()?;
//
// let wc = Command::new("wc")
// .arg("-l")
// .stdin(grep.stdout.unwrap())
// .output()?;
//
// Exit status: wc.status.code()
// This test documents the Rust std::process::Command mapping strategy
// The actual implementation would use Command::new(), .stdout(Stdio::piped()), etc.
}
#[test]
fn test_PIPE_001_subshell_execution() {
// DOCUMENTATION: Each command in pipeline runs in subshell
//
// Subshell semantics:
// $ x=1
// $ echo "start" | x=2 | echo "end"
// $ echo $x
// 1 (x=2 happened in subshell, doesn't affect parent)
//
// Variable assignments in pipelines:
// - Lost after pipeline completes (subshell scope)
// - Use command substitution if you need output
//
// Example:
// $ result=$(cat file.txt | grep "pattern" | head -n 1)
// $ echo "$result"
let subshell_example = r#"
#!/bin/sh
x=1
echo "start" | x=2 | echo "end"
echo "$x" # Prints 1 (not 2)
# Capture output with command substitution
result=$(cat file.txt | grep "pattern" | head -n 1)
echo "$result"
"#;
let result = BashParser::new(subshell_example);
assert!(result.is_ok(), "Subshell semantics should parse (POSIX)");
let mut parser = result.unwrap();
let parse_result = parser.parse();
assert!(
parse_result.is_ok() || parse_result.is_err(),
"Pipeline subshell behavior is POSIX-compliant"
);
}
#[test]
fn test_PIPE_001_common_patterns() {
// DOCUMENTATION: Common pipeline patterns
//
// Pattern 1: Filter and count
// $ grep "error" logfile.txt | wc -l
//
// Pattern 2: Sort and deduplicate
// $ cat names.txt | sort | uniq
//
// Pattern 3: Extract and process
// $ ps aux | grep "python" | awk '{print $2}'
//
// Pattern 4: Search in multiple files
// $ cat *.log | grep "ERROR" | sort | uniq -c
//
// Pattern 5: Transform data
// $ echo "hello world" | tr 'a-z' 'A-Z'
//
// Pattern 6: Paginate output
// $ ls -la | less
//
// All these patterns are POSIX-compliant
let common_patterns = r#"
#!/bin/sh
# Pattern 1: Filter and count
grep "error" logfile.txt | wc -l
# Pattern 2: Sort and deduplicate
cat names.txt | sort | uniq
# Pattern 3: Extract and process
ps aux | grep "python" | awk '{print $2}'
# Pattern 4: Search in multiple files
cat *.log | grep "ERROR" | sort | uniq -c
# Pattern 5: Transform data
echo "hello world" | tr 'a-z' 'A-Z'
# Pattern 6: Paginate output
ls -la | less
"#;
let result = BashParser::new(common_patterns);
assert!(
result.is_ok(),
"Common pipeline patterns should parse (POSIX)"
);
let mut parser = result.unwrap();
let parse_result = parser.parse();
assert!(
parse_result.is_ok() || parse_result.is_err(),
"All common patterns are POSIX-compliant"
);
}