Added auto-recompilation (sometimes it works)

This commit is contained in:
Casey 2023-11-16 14:36:47 +03:00
parent 2bc3f75535
commit 869553f402
Signed by: hkc
GPG Key ID: F0F6CFE11CDB0960
2 changed files with 24 additions and 9 deletions

5
cbt.c
View File

@ -3,11 +3,10 @@
#include "cbt.h"
int main(int argc, char **argv) {
cbt_verbosity = CBT_LOG_ALL;
cbt_verbosity = CBT_LOG_DEBUG;
CBT_INIT(argc, argv);
struct cbt_proc proc = cbt_proc_new(CBT_PROC_NORMAL, "sh", "-c", "while sleep 0.1; do echo 'nya'; done");
struct cbt_proc proc = cbt_proc_new(CBT_PROC_NORMAL, "sh", "-c", "echo 'owo'");
cbt_proc_wait(proc);
cbt_cleanup();

28
cbt.h
View File

@ -115,11 +115,12 @@ int cbt_proc_wait(struct cbt_proc proc);
#include <signal.h>
#include <errno.h>
#define CBT_FAIL(COND) if (COND) { cbt_log(CBT_LOG_FATAL, "condition %s failed: %s", #COND, strerror(errno)); abort(); };
#define CBT_FAIL(COND) if (COND) { cbt_log(CBT_LOG_FATAL, "condition %s failed, %s", #COND, strerror(errno)); abort(); };
enum cbt_loglevel cbt_verbosity = 0;
char *cbt_cc = NULL;
long cbt_start_time = 0;
bool cbt_running = false;
struct _cbt__autoproc_fdset {
struct pollfd *items;
@ -134,9 +135,10 @@ struct _cbt__autoproc_set {
size_t size, cap;
} cbt__autoproc_set = { 0 };
struct cbt_procgroup cbt__default_procgroup = { .items = 0, .size = 0, .cap = 4 };
pthread_t _cbt__autoproc_thread;
pthread_mutex_t _cbt__autoproc_mut = PTHREAD_MUTEX_INITIALIZER;
bool cbt_running = false;
const char *cbt_log__colors[CBT_LOG_ALL + 1] = {
"\x1b[1;31m",
@ -167,13 +169,25 @@ void cbt__init(int argc, char **argv, const char *source_file) {
cbt_log(CBT_LOG_DEBUG, "Args: %s", cbt_escape_args(argv));
cbt_log(CBT_LOG_DEBUG, "%s", cbt_fmt("format test: %s", "ok"));
if (cbt_needs_recompilation(source_file, argv[0])) {
cbt_log(CBT_LOG_WARNING, "Needs recompilation, but not implemented yet");
}
cbt__default_procgroup.items = calloc(cbt__default_procgroup.cap, sizeof(struct cbt_proc));
cbt_log(CBT_LOG_DEBUG, "Starting line processor thread");
// TODO: error checking
pthread_create(&_cbt__autoproc_thread, NULL, cbt__line_processor, NULL);
if (cbt_needs_recompilation(source_file, argv[0])) {
cbt_log(CBT_LOG_INFO, "Recompiling...");
struct cbt_proc cc = cbt_proc_new(CBT_PROC_NORMAL, cbt_cc, source_file, "-o", argv[0], "-Wall", "-Wextra", NULL);
int status = cbt_proc_wait(cc);
cbt_log(CBT_LOG_INFO, "CC returned %d", status);
if (status == 0) {
cbt_cleanup();
cbt_log(CBT_LOG_INFO, "Restarting CBT", cbt_escape_args(argv));
execv(argv[0], argv);
exit(0);
}
}
}
void cbt_cleanup(void) {
@ -183,6 +197,7 @@ void cbt_cleanup(void) {
cbt_log(CBT_LOG_INFO, "cbt shutting down");
pthread_join(_cbt__autoproc_thread, NULL);
pthread_mutex_unlock(&_cbt__autoproc_mut);
cbt_log(CBT_LOG_INFO, "shutdown complete");
}
const char *cbt_fmt(const char *fmt, ...) {
@ -276,7 +291,6 @@ bool cbt_needs_recompilation(const char *input, const char *output) {
void cbt__add_fd(int fd, bool is_err) {
cbt_log(CBT_LOG_DEBUG, "cbt__add_fd() adding %d", fd);
pthread_kill(_cbt__autoproc_thread, SIGUSR1);
pthread_mutex_lock(&_cbt__autoproc_mut);
cbt_log(CBT_LOG_DEBUG, "cbt__add_fd() locked mutex for fd=%d", fd);
@ -310,6 +324,7 @@ struct cbt_proc cbt_proc_newv(enum cbt_proc_mode mode, va_list args) {
if (arg == NULL) break;
cbt_da_add(args_da, strdup(arg));
}
cbt_da_add(args_da, NULL);
cbt_log(CBT_LOG_DEBUG, "Spawning process with mode %d and args %s", mode, cbt_escape_args(args_da.items));
@ -365,6 +380,7 @@ int cbt_proc_wait(struct cbt_proc proc) {
do {
pid_t w = waitpid(proc.pid, &wstatus, WUNTRACED | WCONTINUED);
cbt_log(CBT_LOG_DEBUG, "waitpid(%d) -> %d, status = %d", proc.pid, w, wstatus);
if (w == -1) {
cbt_log(CBT_LOG_WARNING, "waitpid(%d) -> %d (%s)", proc.pid, w, strerror(errno));
return -2000;