LCOV - code coverage report
Current view: top level - src - helper.c (source / functions) Coverage Total Hit
Test: report.info Lines: 91.5 % 59 54
Test Date: 2025-02-11 09:58:02 Functions: 100.0 % 8 8

            Line data    Source code
       1              : #include "helper.h"
       2              : 
       3              : /**
       4              :  * @brief Internal binary options declaration (must end with {0,0,0,0})
       5              :  * @param help
       6              :  * @param verbose
       7              :  * @param command
       8              :  * @return struct option
       9              :  */
      10              : static struct option binary_opts[] = {
      11              :     {"help", no_argument, 0, 'h'},
      12              :     {"verbose", no_argument, 0, 'v'},
      13              :     {"command", required_argument, 0, 'c'},
      14              :     {0, 0, 0, 0}};
      15              : 
      16              : static char *command_requested = NULL;
      17              : 
      18            3 : char *get_requested_command()
      19              : {
      20            3 :     return command_requested;
      21              : }
      22              : 
      23            5 : char *dup_optarg_str()
      24              : {
      25            5 :     char *str = NULL;
      26            5 :     if (optarg != NULL)
      27              :     {
      28            3 :         str = strndup(optarg, MAX_INPUT_LENGTH);
      29            3 :         if (str == NULL)
      30              :         {
      31            0 :             print_error("[ERROR] strndup() failed");
      32              :         }
      33              :     }
      34            5 :     return str;
      35              : }
      36              : 
      37            3 : void show_help(char **argv)
      38              : {
      39            3 :     print_generic(STDOUT_FILENO, "USAGE: %s %s\n\n%s\n", argv[0], USAGE_SYNTAX, USAGE_PARAMS);
      40            3 : }
      41              : 
      42            2 : void show_parameters(bool verbose_mode, bool command_mode)
      43              : {
      44            2 :     print("** PARAMS **\n%-8s: %d\n%-8s: %d\n",
      45              :           "verbose", verbose_mode,
      46              :           "command", command_mode);
      47            2 : }
      48              : 
      49            1 : void check_requirements(int argc, char **argv)
      50              : {
      51            1 :     return;
      52              : }
      53              : 
      54            5 : void parse_options(int argc, char **argv, bool *command_mode, bool *no_execute)
      55              : {
      56            5 :     *no_execute = false;
      57            5 :     *command_mode = false;
      58              : 
      59            5 :     int opt = -1;
      60            5 :     int opt_idx = -1;
      61              : 
      62           10 :     while ((opt = getopt_long(argc, argv, BINARY_OPTION_STR, binary_opts, &opt_idx)) != -1)
      63              :     {
      64            5 :         switch (opt)
      65              :         {
      66            1 :         case 'c':
      67              :             // Command param
      68            1 :             if (optarg)
      69              :             {
      70            1 :                 command_requested = dup_optarg_str();
      71              : 
      72            1 :                 if (command_requested == NULL)
      73              :                 {
      74            0 :                     errno = EINVAL;
      75            0 :                     print_error("[ERROR] Command is NULL");
      76              :                 }
      77              : 
      78            1 :                 if (!*no_execute)
      79              :                 {
      80            1 :                     *command_mode = true;
      81              :                 }
      82              :             }
      83            1 :             break;
      84            1 :         case 'v':
      85            1 :             set_verbose_mode(true);
      86            1 :             break;
      87            1 :         case 'h':
      88            1 :             show_help(argv);
      89            1 :             *no_execute = true;
      90            1 :             *command_mode = false;
      91            1 :             break;
      92            2 :         default:
      93            2 :             *no_execute = true;
      94            2 :             break;
      95              :         }
      96              :     }
      97            5 : }
      98              : 
      99            1 : void print_prompt()
     100              : {
     101              :     char cwd[MAX_INPUT_LENGTH];
     102            1 :     char *username = getenv("USER");
     103              : 
     104            1 :     if (getcwd(cwd, sizeof(cwd)) != NULL)
     105              :     {
     106            1 :         print_generic(STDOUT_FILENO, GREEN_COLOR "%s@%s" RESET_COLOR ":" BLUE_COLOR "%s\n" RESET_COLOR "$ ", username, SHELL_NAME, cwd);
     107              :     }
     108              :     else
     109              :     {
     110            0 :         print_error("getcwd() error");
     111            0 :         exit(EXIT_FAILURE);
     112              :     }
     113            1 : }
     114              : 
     115            1 : void predator_shake(void)
     116              : {
     117            1 :     print_generic(STDOUT_FILENO, "\n");
     118              :     static const char *art_lines[] = {
     119              :         "    .................      . .:.:-.# ...........................................",
     120              :         "      ...............  @*%#+:-:=#@@* .........................................  ",
     121              :         "  .    ...............  @@@#+@@@%**@@  .....................................    ",
     122              :         ".@@%=    ............. .@%=*#@@@@@@#@  ............................           @#",
     123              :         " =*%@@@@    .......... @@%@@%@+=*-%@@@  ........................     -@@@@@@@###",
     124              :         " ##%#+:#@@    ........ *@@%%@@**@+=*@@. ....................    +@#@@%###%%%%@%*",
     125              :         " ++-::+@++@@:  ....... . @%++@@@@@**%@@  .................  .-##%############%%*",
     126              :         " +=-=*@..:-@@   .......  :@@+**#%%+*%@@. ...............   :###%##%########%#%%*",
     127              :         ".++=+*=-**@@@@-  .......  .@%*%%@%*%@@@. ............   :%###@##%####%##%####%%*",
     128              :         " +++*###=-+.=-*   .......  @@@-%%+*#@@%   ......      .%##%@%%@%########%#####%*",
     129              :         " =+#+=+=:=*@@-%@ .  .....  .@. @#*%@%@#    ...    @@@@@@########%@#%########%#%*",
     130              :         " #@#+*+=+@@%..:@@@    ....  @ #@**###@@@@    . =-@@#****=:+###%##@##%##########*",
     131              :         " %%###-#@%=  #@@**@@#    .  .@%#***@#@*#@@@    .@%**+:.=-=-*##%@%@##%%%%##%####*",
     132              :         " @%+=++@*  .@@*:.=:=+@@   .@@@@@%#*##@+**@@@-  @@#+-:+####*+%##%%@%#%%%%%%%%%%%*",
     133              :         ".%**++@=..=@#.:.===- .@@ . @%#*######@**#=+@@@@%#%@%%%%##%@@@%###@@#%%%%%%%%%%%*",
     134              :         ".%@*+#*- *@#-::===-..@@@@@@@%#*#+*##%@**#*#@+#*==-#@@@@@@@+..%#%@@@##%%%%%%%%%%*",
     135              :         ".%%@##  @@#=:====:..@@@%*@@+***#%%%#@%##%**##*+@@%*:.-++*+++=*#%@@@##%%%#%#%%%%*",
     136              :         "-@#-:+@@@#:--#+-. @@%%%@#=%%*+*@@%#@@###*#%***+*#@-*@@*#=:+*#@@@@@@@%%%%%%%%%%%*",
     137              :         "%@@@@@@+=+#@*-. @@@@%%##@@@*==*@@#@@@#%%%##*###%=-.-=%%*+*%@@@@@   .@%%%%%%%%%%*",
     138              :         ":@@@@@@@@#*:.=@@@@%%%%%##%#%%#@@%@@@%#%%%%%%@@#=::+%%%%+@@@@@-     .@%%%%%%%%%%*",
     139              :         "+@@@@@@@@-@@@@@@@@@@%%%%###%#@@@@@@@@@@@%%@@@*=+#%@@@@@@@@@    ...  @%@%%%%%%%%*",
     140              :         "+@@%%@%@@ .@@@@@@@@@@@@@@@@@#%@@@@@  @@%%%%*+#@@@@@@@@%      .....  @@@@%%%%%%%*",
     141              :         "-:..++=%     -##**#####*****#**+=+%   *+++++**-             ......  =#**********",
     142              :     };
     143              : 
     144            1 :     size_t num_lines = sizeof(art_lines) / sizeof(art_lines[0]);
     145           24 :     for (size_t i = 0; i < num_lines; i++)
     146           23 :         print_generic(STDOUT_FILENO, "%s\n", art_lines[i]);
     147              : 
     148            1 :     print_generic(STDOUT_FILENO, "\n");
     149            1 : }
        

Generated by: LCOV version 2.0-1