Files
orbit/src/main/stub.c
Claude efeaa326c8 orbit: OCI/ORAS-backed package manager in MoonBit
Initial, not yet compiled: CLI, semver constraints, dependency planner,
content-addressed store, install/remove/upgrade/rollback/gc, publish.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01DPL5hhg7TTpuaCymoNQWmQ
2026-11-26 17:21:17 -08:00

120 lines
2.8 KiB
C

// Native glue for orbit: process execution, file IO and environment access.
// Every string crosses the boundary as UTF-8 `Bytes` (not NUL-terminated).
// Arguments are never decref'd here: if the compiler passes them owned we leak
// a few bytes in a short-lived CLI, which is harmless; if it passes them
// borrowed, decref'ing would be a use-after-free.
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/wait.h>
#include "moonbit.h"
static int last_status = 0;
static char *to_cstr(moonbit_bytes_t b) {
int32_t n = Moonbit_array_length(b);
char *s = (char *)malloc((size_t)n + 1);
if (!s) abort();
memcpy(s, b, (size_t)n);
s[n] = 0;
return s;
}
static moonbit_bytes_t from_buf(const char *buf, size_t len) {
moonbit_bytes_t out = moonbit_make_bytes((int32_t)len, 0);
if (len) memcpy(out, buf, len);
return out;
}
static int decode_status(int r) {
if (r == -1) return -1;
if (WIFEXITED(r)) return WEXITSTATUS(r);
return 128;
}
int32_t orbit_run(moonbit_bytes_t cmd) {
char *c = to_cstr(cmd);
fflush(stdout);
int r = system(c);
free(c);
return decode_status(r);
}
moonbit_bytes_t orbit_capture(moonbit_bytes_t cmd) {
char *c = to_cstr(cmd);
fflush(stdout);
FILE *p = popen(c, "r");
free(c);
if (!p) {
last_status = -1;
return from_buf("", 0);
}
size_t cap = 4096, len = 0;
char *buf = (char *)malloc(cap);
size_t got;
while ((got = fread(buf + len, 1, cap - len, p)) > 0) {
len += got;
if (len == cap) {
cap *= 2;
buf = (char *)realloc(buf, cap);
if (!buf) abort();
}
}
last_status = decode_status(pclose(p));
moonbit_bytes_t out = from_buf(buf, len);
free(buf);
return out;
}
int32_t orbit_last_status(void) { return last_status; }
moonbit_bytes_t orbit_read_file(moonbit_bytes_t path) {
char *p = to_cstr(path);
FILE *f = fopen(p, "rb");
free(p);
if (!f) {
last_status = -1;
return from_buf("", 0);
}
size_t cap = 4096, len = 0, got;
char *buf = (char *)malloc(cap);
while ((got = fread(buf + len, 1, cap - len, f)) > 0) {
len += got;
if (len == cap) {
cap *= 2;
buf = (char *)realloc(buf, cap);
if (!buf) abort();
}
}
fclose(f);
last_status = 0;
moonbit_bytes_t out = from_buf(buf, len);
free(buf);
return out;
}
int32_t orbit_write_file(moonbit_bytes_t path, moonbit_bytes_t data) {
char *p = to_cstr(path);
FILE *f = fopen(p, "wb");
free(p);
if (!f) return -1;
int32_t n = Moonbit_array_length(data);
size_t w = fwrite(data, 1, (size_t)n, f);
int rc = fclose(f);
return (w == (size_t)n && rc == 0) ? 0 : -1;
}
moonbit_bytes_t orbit_getenv(moonbit_bytes_t name) {
char *n = to_cstr(name);
const char *v = getenv(n);
free(n);
if (!v) return from_buf("", 0);
return from_buf(v, strlen(v));
}
void orbit_exit(int32_t code) {
fflush(stdout);
exit(code);
}