#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; }