2026-01-12 18:42:02 +00:00
|
|
|
#include "./args.h"
|
|
|
|
|
|
|
|
|
|
#include <stdbool.h>
|
|
|
|
|
#include <stdio.h>
|
|
|
|
|
#include <string.h>
|
|
|
|
|
|
2026-01-23 17:05:56 +00:00
|
|
|
#include "../string_utils/string_utils.h"
|
|
|
|
|
#include "../vars/vars.h"
|
|
|
|
|
|
|
|
|
|
int args_handler(int argc, char **argv, struct args_options *options,
|
|
|
|
|
struct hash_map *vars)
|
2026-01-12 18:42:02 +00:00
|
|
|
{
|
|
|
|
|
options->type = INPUT_UNDEFINED;
|
|
|
|
|
options->input_source = NULL;
|
|
|
|
|
options->pretty_print = false;
|
|
|
|
|
options->verbose = false;
|
|
|
|
|
|
2026-01-23 17:05:56 +00:00
|
|
|
int arg_index = 1;
|
|
|
|
|
char index_str[11];
|
|
|
|
|
|
|
|
|
|
set_var_copy(vars, "0", argv[0]);
|
|
|
|
|
|
2026-01-12 18:42:02 +00:00
|
|
|
for (int i = 1; i < argc; i++)
|
|
|
|
|
{
|
|
|
|
|
if (strcmp(argv[i], "--pretty-print") == 0)
|
|
|
|
|
{
|
|
|
|
|
options->pretty_print = true;
|
|
|
|
|
}
|
|
|
|
|
else if (strcmp(argv[i], "--verbose") == 0)
|
|
|
|
|
{
|
|
|
|
|
options->verbose = true;
|
|
|
|
|
}
|
2026-01-12 19:30:11 +00:00
|
|
|
else if (strcmp(argv[i], "-c") == 0)
|
2026-01-12 18:42:02 +00:00
|
|
|
{
|
|
|
|
|
if (options->type != INPUT_UNDEFINED)
|
2026-01-12 19:30:11 +00:00
|
|
|
{
|
|
|
|
|
fprintf(stderr, "Multiple input sources specified: %s\n",
|
|
|
|
|
argv[i + 1]);
|
|
|
|
|
return 1;
|
|
|
|
|
}
|
|
|
|
|
else if (i + 1 >= argc)
|
|
|
|
|
{
|
|
|
|
|
fprintf(stderr, "No command provided after -c\n");
|
|
|
|
|
return 1;
|
|
|
|
|
}
|
2026-01-12 18:42:02 +00:00
|
|
|
|
|
|
|
|
options->type = INPUT_CMD;
|
|
|
|
|
options->input_source = argv[i + 1];
|
|
|
|
|
i++;
|
|
|
|
|
}
|
|
|
|
|
else if (argv[i][0] == '-')
|
|
|
|
|
{
|
2026-01-12 19:30:11 +00:00
|
|
|
fprintf(stderr, "Unknown option: %s\n", argv[i]);
|
|
|
|
|
return 1;
|
2026-01-12 18:42:02 +00:00
|
|
|
}
|
2026-01-23 17:05:56 +00:00
|
|
|
else if (options->type == INPUT_UNDEFINED)
|
2026-01-12 18:42:02 +00:00
|
|
|
{
|
|
|
|
|
options->type = INPUT_FILE;
|
|
|
|
|
options->input_source = argv[i];
|
|
|
|
|
}
|
2026-01-23 17:05:56 +00:00
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
// All remaining arguments are treated as additional arguments
|
|
|
|
|
int_to_str(arg_index++, index_str);
|
|
|
|
|
set_var_copy(vars, index_str, argv[i]);
|
|
|
|
|
continue;
|
|
|
|
|
}
|
2026-01-12 18:42:02 +00:00
|
|
|
}
|
|
|
|
|
|
2026-01-24 14:51:27 +00:00
|
|
|
int_to_str(arg_index - 1, index_str);
|
|
|
|
|
set_var_copy(vars, "#", index_str);
|
|
|
|
|
|
2026-01-12 18:42:02 +00:00
|
|
|
if (options->type == INPUT_UNDEFINED)
|
|
|
|
|
options->type = INPUT_STDIN;
|
|
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void args_print(struct args_options *options)
|
|
|
|
|
{
|
|
|
|
|
printf("Input type: %s\n",
|
|
|
|
|
options->type == INPUT_CMD ? "COMMAND"
|
|
|
|
|
: options->type == INPUT_FILE ? "FILE"
|
|
|
|
|
: options->type == INPUT_STDIN ? "STDIN"
|
|
|
|
|
: "UNDEFINED");
|
|
|
|
|
printf("Input source: %s\n",
|
|
|
|
|
options->input_source ? options->input_source : "NULL");
|
|
|
|
|
printf("Pretty print: %s\n", options->pretty_print ? "true" : "false");
|
|
|
|
|
printf("Verbose: %s\n", options->verbose ? "true" : "false");
|
|
|
|
|
}
|
2026-01-12 19:30:11 +00:00
|
|
|
|
|
|
|
|
void print_usage(FILE *std, const char *program_name)
|
|
|
|
|
{
|
|
|
|
|
fprintf(std, "Usage: %s [OPTIONS] [SCRIPT] [ARGUMENTS...]\n", program_name);
|
|
|
|
|
fprintf(std, "Options:\n");
|
|
|
|
|
fprintf(std, " -c [SCRIPT] Execute the given command string.\n");
|
|
|
|
|
fprintf(std, " --pretty-print Enable pretty printing of outputs.\n");
|
|
|
|
|
fprintf(std, " --verbose Enable verbose mode.\n");
|
|
|
|
|
fprintf(std,
|
|
|
|
|
"If no SCRIPT is provided, input is read from standard input.\n");
|
|
|
|
|
}
|