feat(main_loop): Wrote the base of the main entrypoint. Implemented a helper which converts the args options to iob_context
This commit is contained in:
parent
83b7374583
commit
f71d0a2183
3 changed files with 89 additions and 5 deletions
|
|
@ -2,6 +2,8 @@
|
||||||
|
|
||||||
#include <stdio.h>
|
#include <stdio.h>
|
||||||
|
|
||||||
|
#include "utils/args/args.h"
|
||||||
|
|
||||||
static struct iob_context context;
|
static struct iob_context context;
|
||||||
static FILE *input;
|
static FILE *input;
|
||||||
|
|
||||||
|
|
@ -33,3 +35,28 @@ int iob_init(struct iob_context *ctx)
|
||||||
return -1;
|
return -1;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
int iob_config_from_args(struct args_options *args, struct iob_context *ctx)
|
||||||
|
{
|
||||||
|
switch (args->type)
|
||||||
|
{
|
||||||
|
case INPUT_STDIN:
|
||||||
|
ctx->mode = IOB_MODE_STDIN;
|
||||||
|
ctx->args = NULL;
|
||||||
|
break;
|
||||||
|
|
||||||
|
case INPUT_FILE:
|
||||||
|
ctx->mode = IOB_MODE_SCRIPT;
|
||||||
|
ctx->args = (char *)args->input_source;
|
||||||
|
break;
|
||||||
|
|
||||||
|
case INPUT_CMD:
|
||||||
|
ctx->mode = IOB_MODE_CMD;
|
||||||
|
ctx->args = NULL;
|
||||||
|
break;
|
||||||
|
|
||||||
|
default:
|
||||||
|
return -1;
|
||||||
|
}
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
|
@ -23,6 +23,14 @@ struct iob_context
|
||||||
char *args;
|
char *args;
|
||||||
};
|
};
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @brief Converts struct arg_options to iob_context
|
||||||
|
*
|
||||||
|
* @param args The arguments options struct
|
||||||
|
* @param ctx The IO Backend context struct to populate
|
||||||
|
* @return int 0 on success, negative value on error
|
||||||
|
*/
|
||||||
|
int iob_config_from_args(struct args_options *args, struct iob_context *ctx);
|
||||||
/*
|
/*
|
||||||
* @brief Initializes the IO Backend module
|
* @brief Initializes the IO Backend module
|
||||||
*
|
*
|
||||||
|
|
|
||||||
59
src/main.c
59
src/main.c
|
|
@ -1,19 +1,68 @@
|
||||||
// all includes
|
// All the includes
|
||||||
|
|
||||||
#include <stdio.h>
|
#include <stdio.h>
|
||||||
|
#include <stdlib.h>
|
||||||
|
|
||||||
|
#include "execution/execution.h"
|
||||||
|
#include "io_backend/io_backend.h"
|
||||||
|
#include "parser/parser.h"
|
||||||
#include "utils/args/args.h"
|
#include "utils/args/args.h"
|
||||||
|
|
||||||
|
enum return_codes
|
||||||
|
{
|
||||||
|
SUCCESS = 0,
|
||||||
|
ERR_IO_BACKEND = 2,
|
||||||
|
ERR_MALLOC = 3
|
||||||
|
};
|
||||||
|
|
||||||
int main(int argc, char **argv)
|
int main(int argc, char **argv)
|
||||||
{
|
{
|
||||||
|
// Create the options struct (with argument handler)
|
||||||
struct args_options options;
|
struct args_options options;
|
||||||
int r = args_handler(argc, argv, &options);
|
int r = args_handler(argc, argv, &options);
|
||||||
if (r != 0)
|
if (r != 0)
|
||||||
{
|
{
|
||||||
print_usage(stderr, argv[0]);
|
print_usage(stderr, argv[0]);
|
||||||
return 2;
|
return ERR_IO_BACKEND;
|
||||||
|
}
|
||||||
|
// args_print(&options);
|
||||||
|
|
||||||
|
// Create the IO-Backend context struct
|
||||||
|
struct iob_context *io_context = malloc(sizeof(struct iob_context));
|
||||||
|
if (io_context == NULL)
|
||||||
|
{
|
||||||
|
fprintf(stderr,
|
||||||
|
"Error: Memory allocation failed for IO Backend context\n");
|
||||||
|
return ERR_MALLOC;
|
||||||
}
|
}
|
||||||
|
|
||||||
args_print(&options);
|
// Convert args_options to iob_context
|
||||||
return 0;
|
r = iob_config_from_args(&options, io_context);
|
||||||
|
if (r != 0)
|
||||||
|
{
|
||||||
|
fprintf(stderr,
|
||||||
|
"Error: Failed to configure IO Backend from arguments\n");
|
||||||
|
free(io_context);
|
||||||
|
return ERR_IO_BACKEND;
|
||||||
|
}
|
||||||
|
|
||||||
|
// Init IO Backend (with the context struct)
|
||||||
|
r = iob_init(io_context);
|
||||||
|
if (r != 0)
|
||||||
|
{
|
||||||
|
fprintf(stderr,
|
||||||
|
"Error: IO Backend initialization failed with code %d\n", r);
|
||||||
|
free(io_context);
|
||||||
|
return ERR_IO_BACKEND;
|
||||||
|
}
|
||||||
|
|
||||||
|
free(io_context);
|
||||||
|
|
||||||
|
// Call the parser to get the AST
|
||||||
|
struct ast *command_ast = get_ast(); // We'll pass the options later
|
||||||
|
|
||||||
|
// Call the executor with the AST
|
||||||
|
r = execution(command_ast);
|
||||||
|
|
||||||
|
// Return the execution return code (last command executed)
|
||||||
|
return r;
|
||||||
}
|
}
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue