diff --git a/build.sh b/build.sh index 45e9723..2783d96 100755 --- a/build.sh +++ b/build.sh @@ -1 +1 @@ -gcc -o ibuild src/main.c src/memory.c src/utils.c src/string.c src/discovery.c src/io.c -fsanitize=address -g -DDEBUG +gcc -o ibuild src/main.c src/memory.c src/utils.c src/string.c src/discovery.c src/io.c src/command.c -fsanitize=address -g -DDEBUG -Wall -Wextra -Wpedantic -Wshadow diff --git a/src/command.c b/src/command.c new file mode 100644 index 0000000..08825ee --- /dev/null +++ b/src/command.c @@ -0,0 +1,60 @@ +#include "command.h" + +#include "memory.h" +#include "discovery.h" +#include "io.h" +#include "utils.h" +#include "config.h" + +#include +#include +#include + +// TODO: This file is UNSAFE. This file is purely here for MVP purposes, and to self-host this program itself. + +Command command_create(Arena* a, Discovery* d, Configuration* c) +{ + assert(d != NULL); + assert(a != NULL); + assert(c != NULL); + + Command cmd; + cmd.a = a; + cmd.app = d->cc; + + size_t args_len = d->c_count + 2; + const char** args_buf = (const char**)arena_alloc(a, sizeof(const char*) * (args_len)); + args_buf[0] = d->cc.buf; + + for(size_t i = 0; i < d->c_count; i++) + { + args_buf[i+1] = d->c_files[i]->path.buf; + LOG_DEBUG("File to build: %s", d->c_files[i]->path); + } + + args_buf[args_len - 1] = NULL; + cmd.args = args_buf; + return cmd; +} + +int command_run(Command* cmd) +{ + assert(cmd != NULL); + if(cmd->args == NULL) DIE("No arguments were supplied to the command"); + + pid_t pid = fork(); + + if(pid == -1) DIE("fork failed, could not create child process."); + + if(pid == 0) + { + if(execvp(cmd->app.buf, (char*const*)cmd->args) == -1) + DIE("Failed to launch command: %s.", cmd->app.buf); + } + + int wstatus = 0; + if(waitpid(pid, &wstatus, 0) == -1) DIE("wait failed."); + LOG_DEBUG("Command calling app %s exited with status code %d on child process PID %d.", cmd->app.buf, wstatus, pid); + return wstatus; +} + diff --git a/src/command.h b/src/command.h new file mode 100644 index 0000000..1d13273 --- /dev/null +++ b/src/command.h @@ -0,0 +1,24 @@ +#ifndef COMMAND_H +#define COMMAND_H + +#include "string.h" + +typedef struct Arena Arena; +typedef struct Discovery Discovery; +typedef struct Configuration Configuration; + +struct Command +{ + Arena* a; + StringView app; + const char** args; +}; + +typedef struct Argument Argument; +typedef struct Command Command; + +Command command_create(Arena* a, Discovery* d, Configuration* c); + +int command_run(Command* cmd); + +#endif diff --git a/src/config.c b/src/config.c new file mode 100644 index 0000000..968f9d4 --- /dev/null +++ b/src/config.c @@ -0,0 +1,2 @@ +#include "config.h" + diff --git a/src/config.h b/src/config.h new file mode 100644 index 0000000..6481538 --- /dev/null +++ b/src/config.h @@ -0,0 +1,15 @@ +#ifndef CONFIG_H +#define CONFIG_H + +#include "string.h" + +struct Configuration +{ + StringView compiler_path; + StringView src_dir; + StringView* src_files; +}; + +typedef struct Configuration Configuration; + +#endif diff --git a/src/main.c b/src/main.c index d487031..59e80ab 100644 --- a/src/main.c +++ b/src/main.c @@ -7,20 +7,27 @@ #include "utils.h" #include "string.h" #include "discovery.h" +#include "command.h" +#include "config.h" -int main(void) { - Backend b = {NULL, 0}; +int main(void) +{ + Backend b = BACKEND_CONST; backend_initialize(&b); Arena a_sp = arena_create(&b, MEM_ARENA_MAX_CAP); StringPool sp = string_pool_create(&a_sp); - Arena a_sptemp = arena_create(&b, MEM_ARENA_MAX_CAP); - StringPool sp_temp = string_pool_create(&a_sptemp); - Arena a_d = arena_create(&b, 1024*1024*5); // 5MB for Discovery Discovery d = discovery_create(&a_d, &sp); discovery_discover(&d); + + Arena a_cmd = arena_create(&b, MEM_ARENA_MAX_CAP); + + Configuration sample_config; + + Command cmd = command_create(&a_cmd, &d, &sample_config); + command_run(&cmd); return 0; }