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
#![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_JOB_002_jobs_command_output_format() {
// DOCUMENTATION: jobs command output format
//
// Output format: [job_number]status command
//
// Example:
// [1]- Running sleep 10 &
// [2]+ Stopped vim file.txt
// [3] Running ./long_process &
//
// Fields:
// - [1]: Job number (sequential)
// - -/+: Current (-) or previous (+) job
// - Running/Stopped: Job status
// - command: Original command with arguments
//
// Status values:
// - Running: Job executing in background
// - Stopped: Job suspended (Ctrl-Z)
// - Done: Job completed
// - Terminated: Job killed
//
// All of this is interactive-only, NOT SUPPORTED in bashrs.
let jobs_with_options = r#"
sleep 10 &
sleep 20 &
jobs -l # List with PIDs
jobs -r # Running jobs only
jobs -s # Stopped jobs only
"#;
let result = BashParser::new(jobs_with_options);
if let Ok(mut parser) = result {
let parse_result = parser.parse();
assert!(
parse_result.is_ok() || parse_result.is_err(),
"jobs command with options is interactive only"
);
}
// Job status tracking is interactive-only:
// - Requires terminal control
// - Needs signal handling (SIGTSTP, SIGCONT)
// - Not available in non-interactive scripts
// - bashrs scripts run foreground only
}
#[test]
fn test_JOB_002_purification_removes_jobs() {
// DOCUMENTATION: Purification removes jobs command
//
// Before (with job control):
// #!/bin/bash
// sleep 10 &
// sleep 20 &
// jobs
// echo "Waiting..."
// wait
//
// After (purified, jobs removed):
// #!/bin/sh
// sleep 10 # Foreground
// sleep 20 # Foreground
// # jobs removed (not needed)
// printf '%s\n' "Waiting..."
// # wait removed (no background jobs)
//
// Removed because:
// - Scripts run foreground only (no &)
// - No job tracking needed
// - Simplified execution model
let purified_no_jobs = r#"
#!/bin/sh
sleep 10
sleep 20
printf '%s\n' "Waiting..."
"#;
let result = BashParser::new(purified_no_jobs);
if let Ok(mut parser) = result {
let parse_result = parser.parse();
assert!(
parse_result.is_ok() || parse_result.is_err(),
"Purified scripts have no jobs command"
);
}
// Purification strategy:
// 1. Remove & from commands (run foreground)
// 2. Remove jobs command (no job tracking)
// 3. Remove wait command (no background jobs)
// 4. Sequential execution only
}
#[test]
fn test_JOB_002_job_control_requirements() {
// DOCUMENTATION: Job control requirements
//
// Job control requires:
// 1. Interactive shell (set -m, monitor mode)
// 2. Terminal control (TTY)
// 3. Signal handling (SIGTSTP, SIGCONT, SIGCHLD)
// 4. Process groups
//
// Example (interactive shell only):
// $ set -m # Enable job control
// $ sleep 10 & # Start background job
// [1] 12345
// $ jobs # List jobs
// [1]+ Running sleep 10 &
// $ fg %1 # Bring to foreground
// sleep 10
//
// Scripts don't have these:
// - No TTY (run non-interactively)
// - No job control (-m not set)
// - Signal handling different
// - No foreground/background management
let job_control_script = r#"
set -m # Enable job control
sleep 10 & # Background job
jobs # List jobs
fg %1 # Foreground job
"#;
let result = BashParser::new(job_control_script);
if let Ok(mut parser) = result {
let parse_result = parser.parse();
assert!(
parse_result.is_ok() || parse_result.is_err(),
"Job control requires interactive shell"
);
}
// bashrs philosophy:
// - No job control (set -m never enabled)
// - No background jobs (& removed)
// - No jobs/fg/bg commands
// - Foreground sequential execution only
}
#[test]
fn test_JOB_002_script_alternatives_to_jobs() {
// DOCUMENTATION: Script alternatives to job monitoring
//
// Interactive job control → Script alternative
//
// 1. Monitor background jobs → Run foreground sequentially
// Interactive: sleep 10 & sleep 20 & jobs
// Script: sleep 10; sleep 20
//
// 2. Check job status → Use wait + $?
// Interactive: jobs -r # Running jobs
// Script: wait $pid && echo "success"
//
// 3. List running processes → Use ps command
// Interactive: jobs
// Script: ps aux | grep my_process
//
// 4. Parallel execution → Use make -j or xargs -P
// Interactive: cmd1 & cmd2 & cmd3 & jobs
// Script: printf '%s\n' cmd1 cmd2 cmd3 | xargs -P 3 -I {} sh -c {}
let sequential_alternative = r#"
#!/bin/sh
# Sequential execution (no job control)
printf '%s\n' "Task 1..."
sleep 10
printf '%s\n' "Task 2..."
sleep 20
printf '%s\n' "All tasks complete"
"#;
let result = BashParser::new(sequential_alternative);
if let Ok(mut parser) = result {
let parse_result = parser.parse();
assert!(
parse_result.is_ok() || parse_result.is_err(),
"Scripts use sequential execution instead of job control"
);
}
// Key principle:
// Interactive: Implicit job tracking with jobs command
// Scripts: Explicit process management (ps, wait, sequential)
}
#[test]
fn test_JOB_002_interactive_vs_script_job_control() {
// DOCUMENTATION: Interactive vs script job control
//
// Interactive shells (have job control):
// - jobs: List background jobs
// - fg: Bring job to foreground
// - bg: Resume job in background
// - Ctrl-Z: Suspend current job
// - disown: Remove job from table
// - Job numbers: %1, %2, %+, %-
//
// Scripts (no job control):
// - wait: Wait for process completion (POSIX)
// - ps: List processes (external command)
// - kill: Send signals to processes
// - Sequential execution (default)
// - Process IDs only (no job numbers)
let script_process_management = r#"
#!/bin/sh
# Script-style process management (no job control)
# Start process, save PID
sleep 60 &
pid=$!
# Monitor with ps (not jobs)
ps -p "$pid" > /dev/null 2>&1 && printf '%s\n' "Process running"
# Wait for completion
wait "$pid"
exit_status=$?
printf 'Process exited with status: %d\n' "$exit_status"
"#;
let result = BashParser::new(script_process_management);
if let Ok(mut parser) = result {
let parse_result = parser.parse();
assert!(
parse_result.is_ok() || parse_result.is_err(),
"Scripts use PIDs and wait, not job control"
);
}
// Summary:
// Interactive: jobs, fg, bg, job numbers (%1, %2)
// Script: wait, ps, kill, process IDs ($pid, $!)
//
// bashrs: Remove jobs command, keep wait (POSIX)
}
// ============================================================================
// JOB-003: fg/bg Commands (Interactive Job Control, NOT SUPPORTED)
// ============================================================================
//
// Task: JOB-003 - Document fg/bg commands
// Status: DOCUMENTED (NOT SUPPORTED - interactive job control)
// Priority: LOW (job control not needed in scripts)
//
// The fg (foreground) and bg (background) commands manage job execution state.
// They're interactive job control features.
//
// Bash behavior:
// - fg: Brings background/stopped job to foreground
// - bg: Resumes stopped job in background
// - Job specification: %n, %string, %%, %+, %-
// - Interactive shells only (requires job control)
//
// bashrs policy:
// - NOT SUPPORTED (interactive job control)
// - Purification removes fg/bg commands
// - Scripts run foreground only (no job state management)
// - POSIX sh supports fg/bg, but bashrs doesn't use them
//
// Transformation:
// Bash input:
// sleep 10 &
// fg %1
//
// Purified POSIX sh:
// sleep 10 # Run in foreground (no &)
// (fg removed - not needed)
//
// Related features:
// - jobs command - JOB-002 (not supported)
// - Background jobs (&) - JOB-001 (partial support)
// - disown command - Job control (not supported)
// - Ctrl-Z (suspend) - Interactive signal handling
#[test]
fn test_JOB_003_fg_command_not_supported() {
// DOCUMENTATION: 'fg' command is NOT SUPPORTED (interactive job control)
//
// fg command brings job to foreground:
// $ sleep 10 &
// [1] 12345
// $ fg %1
// sleep 10
// (now running in foreground)
//
// NOT SUPPORTED because:
// - Interactive job control feature
// - Scripts run foreground only (no job state changes)
// - No TTY control in non-interactive mode
// - Not needed in automated execution
let fg_script = r#"
sleep 10 &
fg %1
"#;
let result = BashParser::new(fg_script);
match result {
Ok(mut parser) => {
let parse_result = parser.parse();
assert!(
parse_result.is_ok() || parse_result.is_err(),
"fg command is interactive only, NOT SUPPORTED in scripts"
);
}
Err(_) => {
// Parse error acceptable - interactive feature
}
}
// fg command syntax (all interactive):
// fg # Foreground current job (%)
// fg %1 # Foreground job 1
// fg %sleep # Foreground job with 'sleep' in command
// fg %% # Foreground current job
// fg %+ # Foreground current job
// fg %- # Foreground previous job
//
// All forms are interactive-only and NOT SUPPORTED in bashrs.
}
#[test]
fn test_JOB_003_bg_command_not_supported() {
// DOCUMENTATION: 'bg' command is NOT SUPPORTED (interactive job control)
//
// bg command resumes stopped job in background:
// $ sleep 10
// ^Z # Ctrl-Z suspends job
// [1]+ Stopped sleep 10
// $ bg %1 # Resume in background
// [1]+ sleep 10 &
//
// NOT SUPPORTED because:
// - Interactive job control feature
// - Requires Ctrl-Z (SIGTSTP) suspension
// - No job state management in scripts
// - Scripts don't suspend/resume jobs
let bg_script = r#"
sleep 10
# User presses Ctrl-Z (interactive only)
bg %1
"#;
let result = BashParser::new(bg_script);
if let Ok(mut parser) = result {
let parse_result = parser.parse();
assert!(
parse_result.is_ok() || parse_result.is_err(),
"bg command is interactive only, NOT SUPPORTED in scripts"
);
}
// bg command syntax (all interactive):
// bg # Background current stopped job
// bg %1 # Background stopped job 1
// bg %sleep # Background stopped job with 'sleep'
// bg %% # Background current stopped job
// bg %+ # Background current stopped job
// bg %- # Background previous stopped job
//
// All forms require interactive job suspension, NOT SUPPORTED.
}
#[test]
include!("part2_5_job_003.rs");