mirror of
https://github.com/0x221E/ibuild.git
synced 2026-01-17 18:37:00 +00:00
Feature: Add command execution on child processes feature
Temporary: Add configuration sample for forward compatibility
This commit is contained in:
2
build.sh
2
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
|
||||
|
||||
60
src/command.c
Normal file
60
src/command.c
Normal file
@@ -0,0 +1,60 @@
|
||||
#include "command.h"
|
||||
|
||||
#include "memory.h"
|
||||
#include "discovery.h"
|
||||
#include "io.h"
|
||||
#include "utils.h"
|
||||
#include "config.h"
|
||||
|
||||
#include <assert.h>
|
||||
#include <unistd.h>
|
||||
#include <sys/wait.h>
|
||||
|
||||
// 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;
|
||||
}
|
||||
|
||||
24
src/command.h
Normal file
24
src/command.h
Normal file
@@ -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
|
||||
2
src/config.c
Normal file
2
src/config.c
Normal file
@@ -0,0 +1,2 @@
|
||||
#include "config.h"
|
||||
|
||||
15
src/config.h
Normal file
15
src/config.h
Normal file
@@ -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
|
||||
17
src/main.c
17
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;
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user