Line data Source code
1 : /**
2 : * @file execute_command.c
3 : * @brief Implementation of command execution functionality with output capture
4 : * @details This file contains implementation of execute_command function that allows
5 : * executing shell commands with controlled output capture
6 : */
7 :
8 : #include "testitall.h"
9 :
10 : /**
11 : * @brief Executes a shell command and optionally copies captured stdout into result.
12 : *
13 : * @param command The shell command to execute (must not be NULL)
14 : * @param result Managed memory buffer to receive stdout (can be NULL to ignore stdout)
15 : * @param expected_return_code Expected exit code from the command execution
16 : * @param buffer_policy Bitmask controlling stdout/stderr handling.
17 : * Use STDOUT_SUPPRESS/STDERR_SUPPRESS to drop buffers.
18 : *
19 : * @return SUCCESS when the command runs, exits with the expected code, and parameters are valid;
20 : * FAILURE otherwise.
21 : *
22 : * @details The function clears the shared STDOUT buffer, delegates execution to external_call(),
23 : * copies any captured stdout into the provided buffer, and then frees the shared buffer.
24 : */
25 104 : Return execute_command(
26 : const char *command,
27 : memory *result,
28 : const int expected_return_code,
29 : unsigned int buffer_policy)
30 : {
31 : /// The status that will be passed to return() before exiting.
32 : /// By default, the function worked without errors.
33 104 : Return status = SUCCESS;
34 :
35 : /* Validate input parameters */
36 104 : if(!command)
37 : {
38 0 : return(FAILURE); // Invalid arguments
39 : }
40 :
41 : /* Clean the STDOUT buffer to prepare for new command output */
42 104 : call(del(STDOUT));
43 :
44 : /* Execute the command with specified parameters */
45 104 : run(external_call(command,expected_return_code,buffer_policy));
46 :
47 : /* Copy captured output to result buffer */
48 104 : if(NULL != result)
49 : {
50 94 : if(STDOUT->length > 0U)
51 : {
52 84 : call(copy(result,STDOUT));
53 : }
54 : }
55 :
56 : /* Free temporary STDOUT buffer after copying */
57 104 : call(del(STDOUT));
58 :
59 104 : return(status);
60 : }
|