diff --git a/header/argp_commons.h b/header/argp_commons.h new file mode 100644 index 0000000..3215ac7 --- /dev/null +++ b/header/argp_commons.h @@ -0,0 +1,14 @@ +#ifndef _ARGP_COMMONS_H_ +#define _ARGP_COMMONS_H_ + +#define ARGUMENT_DOC "INPUT_FILE" +#define LONG_DOC "Argp test" +#define DEFAULT_OUT_FILE "a.eipaimg" + +// Gets declared in the main function dynamically +const char *argp_program_version = + VER_MAJOR "." VER_MINOR "." VER_PATCH " || " TAG; + +const char *argp_program_bug_address = "eipabugs@outlook.com"; + +#endif \ No newline at end of file diff --git a/header/common.h b/header/common.h index d4a9099..cc17837 100644 --- a/header/common.h +++ b/header/common.h @@ -4,14 +4,10 @@ #define MAX_MEMORY 1000000 #define MAX_TOKEN_SIZE 10 -#define ALPHA 0 -#define BETA 1 -#define STABLE 2 - -#define VER_MAJOR 0 -#define VER_MINOR 1 -#define VER_PATCH 0 -#define TAG ALPHA +#define VER_MAJOR "0" +#define VER_MINOR "1" +#define VER_PATCH "0" +#define TAG "alpha" #define ASCII_TAB 9 #define ASCII_SPACE 32 @@ -19,4 +15,4 @@ #define PREFGETC(file) *file->_IO_read_ptr -#endif \ No newline at end of file +#endif diff --git a/src/main.c b/src/main.c index 3964d11..c079373 100644 --- a/src/main.c +++ b/src/main.c @@ -1,19 +1,82 @@ #include #include #include +#include #include "../header/common.h" #include "../header/lexer.h" #include "../header/target_code_generator.h" #include "../header/image_saver.h" +#include "../header/argp_commons.h" -int main(int argc, char const *argv[]) +//Here, the argumets get stored by the parser, so acess them from here +struct cmd_arguments { - printf("-----------------\nEIPA Assembler\nVersion: %d.%d.%d\n-----------------\n", VER_MAJOR, VER_MINOR, VER_PATCH); + char *input_file; + char *output_file; +}; - char assembly_path[] = "test/test.eipa"; - char image_path[] = "a.eipaimg"; - FILE *assembly_file = fopen(assembly_path, "r"); +error_t parser_function(int key, char *arg, struct argp_state *state) +{ + struct cmd_arguments *save_arguments = state->input; + + switch (key) + { + case 'o': + save_arguments->output_file = arg; + break; + case ARGP_KEY_ARG: + // If we got more than one non-option argument + if (state->arg_num >= 1) + { + argp_usage(state); + } + save_arguments->input_file = arg; + break; + case ARGP_KEY_END: + //If we didn't receive 1 Argument yet + if (state->arg_num < 1) + { + argp_usage(state); + } + break; + default: + return ARGP_ERR_UNKNOWN; + } + return 0; +} + +// This array stores structs containing information about the cli options +struct argp_option argp_options[] = +{ + { + .name = "output", + .key = 'o', + .arg = "FILE", + .flags = 0, + .doc = "Output file", + .group = 0 + }, + {0} +}; + +struct argp argument_parser = +{ + .options = argp_options, + .parser = parser_function, + .args_doc = ARGUMENT_DOC, + .doc = LONG_DOC +}; + +int main(int argc, char **argv) +{ + struct cmd_arguments arguments; + arguments.output_file = DEFAULT_OUT_FILE; + arguments.input_file = ""; + argp_parse(&argument_parser, argc, argv, 0, 0, &arguments); + + + FILE *assembly_file = fopen(arguments.input_file, "r"); if (assembly_file == NULL) return 0; char(*tokens)[MAX_TOKEN_SIZE] = malloc(sizeof(*tokens) * MAX_MEMORY); @@ -25,7 +88,7 @@ int main(int argc, char const *argv[]) __uint32_t *target_code = malloc(MAX_MEMORY * sizeof(target_code)); gen_target_code(tokens, target_code); print_target_code(target_code); - save_img(image_path, target_code); + save_img(arguments.output_file, target_code); return 0; } \ No newline at end of file