Add Command line Arguments

This commit is contained in:
XOR 2023-02-04 18:53:08 +01:00
parent 3ad00c45dc
commit 2368a92f58
3 changed files with 88 additions and 15 deletions

14
header/argp_commons.h Normal file
View file

@ -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

View file

@ -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

View file

@ -1,19 +1,82 @@
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <argp.h>
#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;
}