LCOV - code coverage report
Current view: top level - src - command.c (source / functions) Hit Total Coverage
Test: report.info Lines: 56 61 91.8 %
Date: 2024-01-02 12:43:31 Functions: 4 4 100.0 %

          Line data    Source code
       1             : #include "command.h"
       2             : 
       3          31 : Command evaluate_command(const char *input)
       4             : {
       5             :     Command cmd;
       6          31 :     init_command(&cmd);
       7             : 
       8             :     // Copy the input for manipulation
       9          31 :     char *input_copy = strdup(input);
      10             : 
      11             :     // Handle aliases
      12          31 :     parse_aliases(input_copy);
      13             :    
      14             :     // Tokenize by spaces, tabs, and newline characters
      15          31 :     char *token = strtok(input_copy, CMD_DELIMITERS);
      16             : 
      17             :     // 1. The first part is the command (from the first letter to the one of the first delimiters)
      18          31 :     if (token == NULL)
      19             :     {
      20           0 :         perror("strtok");
      21           0 :         free(input_copy);
      22           0 :         return cmd;
      23             :     }
      24             : 
      25          31 :     cmd.command = strdup(token);
      26             : 
      27             :     // 2. After getting the command name, collect all arguments (each token = 1 argument depending on the delimiters)
      28          60 :     while ((token = strtok(NULL, CMD_DELIMITERS)) != NULL)
      29             :     {
      30          29 :         handle_quotes(token);
      31          29 :         cmd.arguments = realloc(cmd.arguments, (cmd.num_arguments + 1) * sizeof(char *));
      32          29 :         cmd.arguments[cmd.num_arguments] = strdup(token);
      33          29 :         cmd.num_arguments++;
      34             :     }
      35             : 
      36             :     // Add NULL at the end of the arguments array for execvp
      37          31 :     cmd.arguments = realloc(cmd.arguments, (cmd.num_arguments + 1) * sizeof(char *));
      38          31 :     cmd.arguments[cmd.num_arguments] = NULL;
      39             : 
      40             :     // Get the complete command
      41          31 :     get_complete_command_array(&cmd);
      42          31 :     cmd.input_string = strdup(input);
      43             : 
      44          31 :     free(token);
      45          31 :     free(input_copy);
      46          31 :     return cmd;
      47             : }
      48             : 
      49          31 : void get_complete_command_array(Command *cmd)
      50             : {
      51          31 :     char **new_arguments = malloc((cmd->num_arguments + 2) * sizeof(char *));
      52          31 :     if (new_arguments == NULL)
      53             :     {
      54           0 :         perror("malloc");
      55           0 :         exit(EXIT_FAILURE);
      56             :     }
      57          31 :     new_arguments[0] = strdup(cmd->command);
      58          60 :     for (int i = 0; i < cmd->num_arguments; ++i)
      59             :     {
      60          29 :         new_arguments[i + 1] = strdup(cmd->arguments[i]);
      61             :     }
      62          31 :     new_arguments[cmd->num_arguments + 1] = NULL;
      63             :     
      64          31 :     cmd->complete_command = new_arguments;
      65          31 : }
      66             : 
      67          31 : void init_command(Command *cmd)
      68             : {
      69          31 :     cmd->command = NULL;
      70          31 :     cmd->arguments = NULL;
      71          31 :     cmd->complete_command = NULL;
      72          31 :     cmd->input_string = NULL;
      73          31 :     cmd->redirection.input_file = NULL;
      74          31 :     cmd->redirection.output_file = NULL;
      75          31 :     cmd->redirection.append_input = 0;
      76          31 :     cmd->redirection.append_output = 0;
      77          31 :     cmd->num_arguments = 0;
      78          31 : }
      79             : 
      80          31 : void free_command(Command *cmd)
      81             : {
      82          31 :     free(cmd->command);
      83          31 :     cmd->command = NULL;
      84             : 
      85          60 :     for (int i = 0; i < cmd->num_arguments; ++i)
      86             :     {
      87          29 :         free(cmd->arguments[i]);
      88          29 :         cmd->arguments[i] = NULL;
      89             :     }
      90          31 :     free(cmd->arguments);
      91          31 :     cmd->arguments = NULL;
      92             : 
      93          31 :     free(cmd->redirection.input_file);
      94          31 :     cmd->redirection.input_file = NULL;
      95             : 
      96          31 :     free(cmd->redirection.output_file);
      97          31 :     cmd->redirection.output_file = NULL;
      98             : 
      99          31 :     free(cmd->complete_command);
     100          31 :     cmd->complete_command = NULL;
     101             : 
     102          31 :     free(cmd->input_string);
     103          31 :     cmd->input_string = NULL;
     104          31 : }

Generated by: LCOV version 1.14