2022-01-15 16:47:59 -05:00
|
|
|
#include <stdlib.h>
|
|
|
|
|
|
|
|
#include "argo.h"
|
|
|
|
#include "global.h"
|
|
|
|
#include "debug.h"
|
|
|
|
|
2022-02-12 19:40:27 -05:00
|
|
|
int parse_arg(char *arg);
|
|
|
|
int str2int(char *arg);
|
|
|
|
|
2022-01-15 16:47:59 -05:00
|
|
|
/**
|
|
|
|
* @brief Validates command line arguments passed to the program.
|
|
|
|
* @details This function will validate all the arguments passed to the
|
|
|
|
* program, returning 0 if validation succeeds and -1 if validation fails.
|
|
|
|
* Upon successful return, the various options that were specified will be
|
|
|
|
* encoded in the global variable 'global_options', where it will be
|
|
|
|
* accessible elsewhere in the program. For details of the required
|
|
|
|
* encoding, see the assignment handout.
|
|
|
|
*
|
|
|
|
* @param argc The number of arguments passed to the program from the CLI.
|
|
|
|
* @param argv The argument strings passed to the program from the CLI.
|
|
|
|
* @return 0 if validation succeeds and -1 if validation fails.
|
|
|
|
* @modifies global variable "global_options" to contain an encoded representation
|
|
|
|
* of the selected program options.
|
|
|
|
*/
|
|
|
|
|
2022-02-12 16:47:38 -05:00
|
|
|
int validargs(int argc, char **argv)
|
|
|
|
{
|
2022-02-12 16:53:07 -05:00
|
|
|
global_options = 0;
|
2022-02-12 19:40:27 -05:00
|
|
|
int isValid = 0;
|
2022-02-12 16:47:38 -05:00
|
|
|
char **ptr = argv;
|
2022-02-12 19:40:27 -05:00
|
|
|
switch (parse_arg(*(++ptr)))
|
2022-02-12 16:47:38 -05:00
|
|
|
{
|
2022-02-12 19:40:27 -05:00
|
|
|
case 8:
|
|
|
|
global_options = HELP_OPTION;
|
|
|
|
isValid = 1;
|
|
|
|
break;
|
|
|
|
case 4:
|
|
|
|
if (argc == 2)
|
2022-02-12 16:47:38 -05:00
|
|
|
{
|
2022-02-12 19:40:27 -05:00
|
|
|
global_options = VALIDATE_OPTION;
|
|
|
|
isValid = 1;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
if (parse_arg(*(++ptr)) == -1)
|
2022-02-12 16:47:38 -05:00
|
|
|
{
|
2022-02-12 19:40:27 -05:00
|
|
|
global_options = VALIDATE_OPTION;
|
|
|
|
isValid = 1;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
case 2:
|
|
|
|
if (argc == 2)
|
|
|
|
{
|
|
|
|
global_options = CANONICALIZE_OPTION;
|
|
|
|
isValid = 1;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
switch (parse_arg(*(++ptr)))
|
|
|
|
{
|
|
|
|
case -1:
|
|
|
|
global_options = CANONICALIZE_OPTION;
|
|
|
|
isValid = 1;
|
2022-02-12 16:47:38 -05:00
|
|
|
break;
|
2022-02-12 19:40:27 -05:00
|
|
|
case 1:
|
|
|
|
global_options = CANONICALIZE_OPTION + PRETTY_PRINT_OPTION + str2int(*(++ptr));
|
|
|
|
isValid = 1;
|
2022-02-12 16:47:38 -05:00
|
|
|
break;
|
|
|
|
default:
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
2022-02-12 19:40:27 -05:00
|
|
|
break;
|
|
|
|
default:
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
if (isValid)
|
|
|
|
{
|
|
|
|
return 0;
|
2022-02-12 16:47:38 -05:00
|
|
|
}
|
|
|
|
return -1;
|
2022-02-12 19:40:27 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
int parse_arg(char *arg)
|
|
|
|
{
|
|
|
|
int ans = -1;
|
|
|
|
if (*arg == '-')
|
|
|
|
{
|
|
|
|
char arg1 = *(++arg), arg2 = *(++arg);
|
|
|
|
if (arg2 == '\0')
|
|
|
|
{
|
|
|
|
if (arg1 == 'h')
|
|
|
|
{
|
|
|
|
ans = 8;
|
|
|
|
}
|
|
|
|
else if (arg1 == 'v')
|
|
|
|
{
|
|
|
|
ans = 4;
|
|
|
|
}
|
|
|
|
else if (arg1 == 'c')
|
|
|
|
{
|
|
|
|
ans = 2;
|
|
|
|
}
|
|
|
|
else if (arg1 == 'p')
|
|
|
|
{
|
|
|
|
ans = 1;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return ans;
|
|
|
|
}
|
|
|
|
|
|
|
|
int str2int(char *arg)
|
|
|
|
{
|
|
|
|
int ans = 0;
|
|
|
|
while (*arg != '\0')
|
|
|
|
{
|
|
|
|
if (*arg <= '9' && *arg >= '0')
|
|
|
|
{
|
|
|
|
ans = ans * 10 + (*arg - '0');
|
|
|
|
arg++;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
ans = 4;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return ans;
|
2022-02-12 16:47:38 -05:00
|
|
|
}
|