diff --git a/core/rifle.c b/core/rifle.c index 7c94997..76ac466 100644 --- a/core/rifle.c +++ b/core/rifle.c @@ -1,6 +1,7 @@ #include #include #include +#include "rifle/state.h" /* Compiler version */ #define RIFLE_VERSION "0.0.1" @@ -28,9 +29,23 @@ version(void) ); } +static int +compile(const char *in_path) +{ + struct rifle_state state; + + if (rifle_state_init(&state, in_path) < 0) { + return -1; + } + + rifle_state_destroy(&state); + return 0; +} + int main(int argc, char **argv) { + size_t files_compiled = 0; int opt; /* Begin parsing command line options */ @@ -45,5 +60,17 @@ main(int argc, char **argv) } } + while (optind < argc) { + if (compile(argv[optind++]) < 0) { + break; + } + + ++files_compiled; + } + + if (files_compiled == 0) { + printf("fatal: no input files\n"); + return -1; + } return 0; } diff --git a/core/state.c b/core/state.c new file mode 100644 index 0000000..bb9af74 --- /dev/null +++ b/core/state.c @@ -0,0 +1,29 @@ +#include +#include +#include "rifle/state.h" + +int +rifle_state_init(struct rifle_state *state, const char *in_path) +{ + if (state == NULL || in_path == NULL) { + return -1; + } + + state->in_fd = open(in_path, O_RDONLY); + if (state->in_fd < 0) { + return -1; + } + + return 0; +} + +void +rifle_state_destroy(struct rifle_state *state) +{ + if (state == NULL) { + return; + } + + close(state->in_fd); + state->in_fd = -1; +} diff --git a/inc/rifle/state.h b/inc/rifle/state.h new file mode 100644 index 0000000..7330528 --- /dev/null +++ b/inc/rifle/state.h @@ -0,0 +1,35 @@ +#ifndef RIFLE_STATE_H +#define RIFLE_STATE_H 1 + +#include +#include + +/* + * Represents the compiler state + * + * @in_fd: Input file descriptor + * @line_num: Current line number + */ +struct rifle_state { + int in_fd; + size_t line_num; +}; + +/* + * Initialize the compiler state + * + * @state: State to initialize + * @in_path: Input path + * + * Returns zero on success + */ +int rifle_state_init(struct rifle_state *state, const char *in_path); + +/* + * Destroy the compiler state + * + * @state: Compiler state to destroy + */ +void rifle_state_destroy(struct rifle_state *state); + +#endif /* !RIFLE_STATE_H */