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

          Line data    Source code
       1             : #include "alias.h"
       2             : 
       3             : Alias *aliases;
       4             : int num_aliases;
       5             : 
       6           2 : void init_aliases()
       7             : {
       8           2 :     FILE *file = fopen(ALIASES_FILE, "r");
       9           2 :     if (file == NULL)
      10             :     {
      11             :         // Create the alias file if it doesn't exist
      12           0 :         file = fopen(ALIASES_FILE, "w");
      13           0 :         if (file != NULL)
      14             :         {
      15           0 :             fclose(file);
      16             :         }
      17             :         else
      18             :         {
      19           0 :             perror("Error creating aliases file.");
      20             :         }
      21             :     }
      22             : 
      23           2 :     aliases = malloc(sizeof(Alias) * MAX_ALIASES);
      24             : 
      25             :     char line[MAX_INPUT_LENGTH];
      26             : 
      27           2 :     num_aliases = 0;
      28             : 
      29           6 :     while (fgets(line, sizeof(line), file) != NULL && num_aliases < MAX_ALIASES)
      30             :     {
      31             :         // Extract alias and command from line
      32           4 :         char *alias = strtok(line, "=");
      33           4 :         char *command = strtok(NULL, "=");
      34             : 
      35             :         // Remove leading and trailing whitespace from alias and command
      36           4 :         alias = trim(alias);
      37           4 :         command = trim(command);
      38           4 :         handle_quotes(command);
      39             : 
      40           4 :         if (alias != NULL && command != NULL)
      41             :         {
      42           4 :             aliases[num_aliases].alias = malloc(sizeof(char) * (strlen(alias) + 1));
      43           4 :             if (aliases[num_aliases].alias == NULL)
      44             :             {
      45           0 :                 perror("malloc");
      46           0 :                 exit(EXIT_FAILURE);
      47             :             }
      48             : 
      49           4 :             aliases[num_aliases].command = malloc(sizeof(char) * (strlen(command) + 1));
      50           4 :             if (aliases[num_aliases].command == NULL)
      51             :             {
      52           0 :                 perror("malloc");
      53           0 :                 exit(EXIT_FAILURE);
      54             :             }
      55             : 
      56           4 :             strcpy(aliases[num_aliases].alias, alias);
      57           4 :             strcpy(aliases[num_aliases].command, command);
      58             : 
      59           4 :             num_aliases++;
      60             :         }
      61             :     }
      62             : 
      63           2 :     fclose(file);
      64           2 : }
      65             : 
      66          31 : int is_alias(const char *name, char *command)
      67             : {
      68          89 :     for (int i = 0; i < num_aliases; ++i)
      69             :     {
      70          60 :         if (aliases[i].alias != NULL && strcmp(aliases[i].alias, name) == 0)
      71             :         {
      72           2 :             strcpy(command, aliases[i].command);
      73           2 :             return IS_ALIAS_COMMAND;
      74             :         }
      75             :     }
      76          29 :     return IS_NOT_ALIAS_COMMAND;
      77             : }
      78             : 
      79           2 : void free_aliases()
      80             : {
      81           6 :     for (int i = 0; i < num_aliases; ++i)
      82             :     {
      83           4 :         free(aliases[i].alias);
      84           4 :         free(aliases[i].command);
      85             :     }
      86           2 :     free(aliases);
      87           2 : }
      88             : 
      89          31 : void parse_aliases(char *input)
      90             : {
      91          31 :     char *result = malloc(sizeof(char) * MAX_INPUT_LENGTH);
      92          31 :     if (result == NULL)
      93             :     {
      94           0 :         perror("malloc");
      95           0 :         exit(EXIT_FAILURE);
      96             :     }
      97          31 :     result[0] = '\0';
      98          31 :     int i = 0;
      99             : 
     100          31 :     char *token = strtok(input, CMD_DELIMITERS);
     101          89 :     while (token != NULL)
     102             :     {
     103             :         char command[MAX_INPUT_LENGTH];
     104          58 :         int isAlias = i == 0 ? is_alias(token, command) : IS_NOT_ALIAS_COMMAND;
     105          58 :         if (isAlias == IS_ALIAS_COMMAND)
     106             :         {
     107           2 :             strcat(result, command);
     108             :         }
     109             :         else
     110             :         {
     111          56 :             strcat(result, token);
     112             :         }
     113             : 
     114          58 :         token = strtok(NULL, CMD_DELIMITERS);
     115          58 :         if (token != NULL)
     116             :         {
     117          27 :             strcat(result, " ");
     118             :         }
     119          58 :         i++;
     120             :     }
     121             : 
     122          31 :     strcpy(input, result);
     123          31 :     free(result);
     124          31 : }

Generated by: LCOV version 1.14