LCOV - code coverage report
Current view: top level - src - custom_commands.c (source / functions) Coverage Total Hit
Test: report.info Lines: 96.6 % 89 86
Test Date: 2025-02-11 09:58:02 Functions: 100.0 % 4 4

            Line data    Source code
       1              : #include "custom_commands.h"
       2              : 
       3              : static const char *custom_commands_list[] = {
       4              :     "ls_custom",
       5              :     "cd",
       6              :     "pwd",
       7              :     "echo",
       8              :     "exit",
       9              :     "alias",
      10              :     "unalias",
      11              :     NULL};
      12              : 
      13              : static const char *custom_commands_main_process_list[] = {
      14              :     "cd",
      15              :     "alias",
      16              :     "unalias",
      17              :     "help",
      18              :     NULL};
      19              : 
      20          130 : bool is_custom_command(char *command)
      21              : {
      22          654 :     for (int i = 0; custom_commands_list[i] != NULL; i++)
      23              :     {
      24          618 :         if (strcmp(command, custom_commands_list[i]) == 0)
      25           94 :             return true;
      26              :     }
      27              : 
      28           36 :     return false;
      29              : }
      30              : 
      31           89 : bool is_custom_command_main_process(char *command)
      32              : {
      33          281 :     for (int i = 0; custom_commands_main_process_list[i] != NULL; i++)
      34              :     {
      35          242 :         if (strcmp(command, custom_commands_main_process_list[i]) == 0)
      36           50 :             return true;
      37              :     }
      38              : 
      39           39 :     return false;
      40              : }
      41              : 
      42           48 : void execute_custom_command(char *command, char **args)
      43              : {
      44           48 :     if (!is_custom_command(command))
      45              :     {
      46            1 :         errno = EINVAL;
      47            1 :         print_error("[ERROR] Command is not a custom command");
      48            1 :         exit(EXIT_FAILURE);
      49              :         return;
      50              :     }
      51              : 
      52           47 :     int argc = 0;
      53              : 
      54          142 :     while (args[argc] != NULL)
      55           95 :         argc++;
      56              : 
      57           47 :     int status = EXIT_SUCCESS;
      58              : 
      59           47 :     if (strcmp(command, "ls_custom") == 0)
      60              :     {
      61            4 :         char *param = ".";
      62            4 :         if (argc > 2)
      63              :         {
      64            1 :             errno = EINVAL;
      65            1 :             print_error("[ERROR] Too many arguments for ls_custom");
      66            1 :             exit(EXIT_FAILURE);
      67              :             return;
      68              :         }
      69              : 
      70            3 :         if (argc == 2)
      71            2 :             param = args[1];
      72              : 
      73            3 :         status = ls(param);
      74              :     }
      75           43 :     else if (strcmp(command, "pwd") == 0)
      76              :     {
      77              :         char cwd[MAX_PATH_LENGTH];
      78            1 :         if (getcwd(cwd, sizeof(cwd)) == NULL)
      79              :         {
      80            0 :             print_error("[ERROR] getcwd");
      81            0 :             exit(EXIT_FAILURE);
      82              :         }
      83              : 
      84            1 :         print_generic(STDOUT_FILENO, "%s\n", cwd);
      85              :     }
      86           42 :     else if (strcmp(command, "echo") == 0)
      87              :     {
      88           36 :         for (int i = 1; i < argc; i++)
      89              :         {
      90           19 :             print_generic(STDOUT_FILENO, "%s", args[i]);
      91           19 :             if (i < argc - 1)
      92            2 :                 print_generic(STDOUT_FILENO, " ");
      93              :         }
      94           17 :         print_generic(STDOUT_FILENO, "\n");
      95              :     }
      96           25 :     else if (strcmp(command, "exit") == 0)
      97              :     {
      98            1 :         int exit_status = EXIT_FAILURE;
      99              : 
     100            1 :         if (argc == 2)
     101              :         {
     102            1 :             if(strcmp(args[1], "0") == 0)
     103              :             {
     104            1 :                 exit_status = EXIT_SUCCESS;
     105              :             }
     106              :         }
     107              : 
     108            1 :         exit(exit_status);
     109              :     }
     110              : 
     111           45 :     exit(status);
     112              : }
     113              : 
     114           27 : bool execute_custom_command_main_process(char *command, char **args)
     115              : {
     116           27 :     if (!is_custom_command_main_process(command))
     117              :     {
     118            1 :         errno = EINVAL;
     119            1 :         print_error("[ERROR] Command is not a custom command (main process)");
     120            1 :         return false;
     121              :     }
     122              : 
     123           26 :     int argc = 0;
     124              : 
     125           77 :     while (args[argc] != NULL)
     126           51 :         argc++;
     127              : 
     128           26 :     if (strcmp(command, "cd") == 0)
     129           11 :         return cd(argc, args);
     130           15 :     else if (strcmp(command, "alias") == 0)
     131              :     {
     132           11 :         if(argc == 1)
     133              :         {
     134            4 :             for(int i = 0; i < get_alias_count(); i++) {
     135              :                 char name[MAX_INPUT_LENGTH];
     136            3 :                 get_alias_name_by_index(i, name);
     137            3 :                 char *command = get_alias_command(name);
     138            3 :                 print_generic(STDOUT_FILENO, "alias %s='%s'\n", name, command);
     139            3 :                 free_if_needed(command);
     140              :             }
     141            1 :             return true;
     142              :         }
     143              : 
     144           10 :         if(argc > 2)
     145              :         {
     146            1 :             errno = EINVAL;
     147            1 :             print_error("[ERROR] Invalid number of arguments for alias");
     148            1 :             return false;
     149              :         }
     150              : 
     151              :         char name[MAX_INPUT_LENGTH];
     152              :         char value[MAX_INPUT_LENGTH];
     153            9 :         int ret = get_var_values(args[1], name, value);
     154            9 :         if (ret < 0)
     155            0 :             return false;
     156              : 
     157            9 :         int status = set_alias(name, value);
     158            9 :         if(status == -1)
     159              :         {
     160            1 :             errno = EINVAL;
     161            1 :             print_error("[ERROR] Cannot set alias");
     162            1 :             return false;
     163              :         }
     164              : 
     165            8 :         return true;
     166              :     }
     167            4 :     else if (strcmp(command, "unalias") == 0)
     168              :     {
     169            3 :         if(argc != 2)
     170              :         {
     171            1 :             errno = EINVAL;
     172            1 :             print_error("[ERROR] Invalid number of arguments for unalias");
     173            1 :             return false;
     174              :         }
     175              : 
     176            2 :         int status = unset_alias(args[1]);
     177            2 :         if(status == -1)
     178              :         {
     179            1 :             errno = EINVAL;
     180            1 :             print_error("[ERROR] Alias not found");
     181            1 :             return false;
     182              :         }
     183              : 
     184            1 :         return true;
     185              :     }
     186              : 
     187            1 :     return true;
     188              : }
        

Generated by: LCOV version 2.0-1