| 1 | module command_capture_callback |
| 2 | use shell_types |
| 3 | use grammar_parser, only: parse_command_line |
| 4 | use ast_executor, only: execute_ast |
| 5 | use command_tree, only: command_node_t |
| 6 | implicit none |
| 7 | |
| 8 | contains |
| 9 | |
| 10 | ! Callback function for command_capture module |
| 11 | subroutine execute_for_capture(shell, command, exit_status) |
| 12 | type(shell_state_t), intent(inout) :: shell |
| 13 | character(len=*), intent(in) :: command |
| 14 | integer, intent(out) :: exit_status |
| 15 | |
| 16 | type(command_node_t), pointer :: ast_root |
| 17 | |
| 18 | ! Parse using new parser and execute via AST |
| 19 | ast_root => parse_command_line(command) |
| 20 | if (associated(ast_root)) then |
| 21 | exit_status = execute_ast(ast_root, shell) |
| 22 | ! If errexit triggered (shell%running = .false.), use the failing exit status |
| 23 | if (.not. shell%running) then |
| 24 | exit_status = shell%last_exit_status |
| 25 | end if |
| 26 | ! TODO: Add AST cleanup when deallocate_command_tree is available |
| 27 | else |
| 28 | exit_status = 127 |
| 29 | end if |
| 30 | end subroutine execute_for_capture |
| 31 | |
| 32 | ! Initialize command capture callback |
| 33 | subroutine init_command_capture() |
| 34 | use command_capture, only: set_execute_callback |
| 35 | call set_execute_callback(execute_for_capture) |
| 36 | end subroutine init_command_capture |
| 37 | |
| 38 | end module command_capture_callback |