Compare commits

..

4 Commits

Author SHA1 Message Date
IonutParau
4a5562549c quick fix 2025-07-16 00:38:26 +02:00
IonutParau
712cc6752e made null error bufs valid 2025-07-16 00:32:13 +02:00
IonutParau
8e59ab6070 work on some fs stuff 2025-07-15 21:45:34 +02:00
IonutParau
332c3d5aca stuff 2025-07-15 21:45:09 +02:00
5 changed files with 126 additions and 3 deletions

View File

@ -33,6 +33,7 @@
- `microphone` component, allows reading audio from nearby sources - `microphone` component, allows reading audio from nearby sources
- `tape_drive` component, compatible with Computronics, except maybe with proper seek times and support for multiple tapes - `tape_drive` component, compatible with Computronics, except maybe with proper seek times and support for multiple tapes
- `cd_reader` and `cd_writer` components, to work with CDs - `cd_reader` and `cd_writer` components, to work with CDs
- `serial` component, for serial communications with other devices (USB?)
# Internal changes # Internal changes

View File

@ -17,6 +17,8 @@ fn addEngineSources(b: *std.Build, opts: LibBuildOpts) *std.Build.Module {
.unwind_tables = if(opts.optimize == .Debug) null else .none, .unwind_tables = if(opts.optimize == .Debug) null else .none,
}); });
const strict = opts.optimize != .Debug;
dataMod.addCSourceFiles(.{ dataMod.addCSourceFiles(.{
.files = &[_][]const u8{ .files = &[_][]const u8{
"src/lock.c", "src/lock.c",
@ -40,8 +42,8 @@ fn addEngineSources(b: *std.Build, opts: LibBuildOpts) *std.Build.Module {
.flags = &.{ .flags = &.{
if(opts.baremetal) "-DNN_BAREMETAL" else "", if(opts.baremetal) "-DNN_BAREMETAL" else "",
if(opts.bit32) "-DNN_BIT32" else "", if(opts.bit32) "-DNN_BIT32" else "",
"-Wall", if(strict) "-Wall" else "",
"-Werror", if(strict) "-Werror" else "",
"-std=gnu23", "-std=gnu23",
"-Wno-keyword-macro", // cuz bools "-Wno-keyword-macro", // cuz bools
}, },

View File

@ -30,7 +30,7 @@ typedef enum nn_vfmode {
typedef struct nn_vfhandle { typedef struct nn_vfhandle {
nn_vfnode *node; nn_vfnode *node;
nn_size_t position; nn_intptr_t position;
nn_vfmode mode; nn_vfmode mode;
} nn_vfhandle; } nn_vfhandle;

View File

@ -292,6 +292,20 @@ void *nn_memdup(nn_Alloc *alloc, const void *buf, nn_size_t len);
void nn_deallocStr(nn_Alloc *alloc, char *s); void nn_deallocStr(nn_Alloc *alloc, char *s);
nn_address nn_randomUUID(nn_Context *ctx); nn_address nn_randomUUID(nn_Context *ctx);
nn_bool_t nn_path_hasSlash(const char *path);
nn_size_t nn_path_firstSlash(const char *path);
nn_size_t nn_path_lastSlash(const char *path);
// returns whether it is the last name
nn_bool_t nn_path_firstName(const char path[NN_MAX_PATH], char firstDirectory[NN_MAX_PATH], char subpath[NN_MAX_PATH]);
// returns whether it is the only name
nn_bool_t nn_path_lastName(const char path[NN_MAX_PATH], char name[NN_MAX_PATH], char parent[NN_MAX_PATH]);
// returns whether the path is valid
nn_bool_t nn_path_isValid(const char *path);
// writes to canonical the standard form of the path
// returns whether the path is so horribly bad it cannot be converted in canonical form.
nn_bool_t nn_path_canonical(const char path[NN_MAX_PATH], char canonical[NN_MAX_PATH]);
nn_guard *nn_newGuard(nn_Context *context); nn_guard *nn_newGuard(nn_Context *context);
void nn_lock(nn_Context *context, nn_guard *guard); void nn_lock(nn_Context *context, nn_guard *guard);
nn_bool_t nn_tryLock(nn_Context *context, nn_guard *guard); nn_bool_t nn_tryLock(nn_Context *context, nn_guard *guard);

View File

@ -238,11 +238,15 @@ int nn_mapColor(int color, int *palette, int paletteSize) {
} }
void nn_memset(void *buf, unsigned char byte, nn_size_t len) { void nn_memset(void *buf, unsigned char byte, nn_size_t len) {
if(buf == NULL) return;
unsigned char *bytes = buf; unsigned char *bytes = buf;
for(nn_size_t i = 0; i < len; i++) bytes[i] = byte; for(nn_size_t i = 0; i < len; i++) bytes[i] = byte;
} }
void nn_memcpy(void *dest, const void *src, nn_size_t len) { void nn_memcpy(void *dest, const void *src, nn_size_t len) {
if(dest == NULL) return;
if(src == NULL) return;
char *destBytes = dest; char *destBytes = dest;
const char *srcBytes = src; const char *srcBytes = src;
for(nn_size_t i = 0; i < len; i++) { for(nn_size_t i = 0; i < len; i++) {
@ -251,6 +255,7 @@ void nn_memcpy(void *dest, const void *src, nn_size_t len) {
} }
char *nn_strcpy(char *dest, const char *src) { char *nn_strcpy(char *dest, const char *src) {
if(dest == NULL) return dest;
nn_size_t i = 0; nn_size_t i = 0;
while(src[i]) { while(src[i]) {
dest[i] = src[i]; dest[i] = src[i];
@ -280,6 +285,7 @@ int nn_strcmp(const char *a, const char *b) {
} }
const char *nn_strchr(const char *str, int ch) { const char *nn_strchr(const char *str, int ch) {
if(str == NULL) return NULL;
nn_size_t i = 0; nn_size_t i = 0;
while(NN_TRUE) { while(NN_TRUE) {
if(str[i] == ch) return str + i; if(str[i] == ch) return str + i;
@ -289,16 +295,19 @@ const char *nn_strchr(const char *str, int ch) {
} }
nn_size_t nn_strlen(const char *a) { nn_size_t nn_strlen(const char *a) {
if(a == NULL) return 0;
nn_size_t l = 0; nn_size_t l = 0;
while(a[l]) l++; while(a[l]) l++;
return l; return l;
} }
nn_bool_t nn_error_isEmpty(nn_errorbuf_t buf) { nn_bool_t nn_error_isEmpty(nn_errorbuf_t buf) {
if(buf == NULL) return true;
return buf[0] == 0; return buf[0] == 0;
} }
void nn_error_write(nn_errorbuf_t buf, const char *s) { void nn_error_write(nn_errorbuf_t buf, const char *s) {
if(buf == NULL) return;
for(nn_size_t i = 0; i < NN_MAX_ERROR_BUFFER; i++) { for(nn_size_t i = 0; i < NN_MAX_ERROR_BUFFER; i++) {
buf[i] = s[i]; buf[i] = s[i];
if(s[i] == 0) break; if(s[i] == 0) break;
@ -308,5 +317,102 @@ void nn_error_write(nn_errorbuf_t buf, const char *s) {
} }
void nn_error_clear(nn_errorbuf_t buf) { void nn_error_clear(nn_errorbuf_t buf) {
if(buf == NULL) return;
buf[0] = 0; buf[0] = 0;
} }
nn_bool_t nn_path_hasSlash(const char *path) {
while(*path) {
if(*path == '/') return true;
path++;
}
return false;
}
nn_size_t nn_path_firstSlash(const char *path) {
for(nn_size_t i = 0; path[i]; i++) {
if(path[i] == '/') return i;
}
return 0; // should never happen
}
nn_size_t nn_path_lastSlash(const char *path) {
nn_size_t slash = 0;
for(nn_size_t i = 0; path[i]; i++) {
if(path[i] == '/') slash = i;
}
return slash;
}
// returns whether it is the last name
nn_bool_t nn_path_firstName(const char *path, char firstDirectory[NN_MAX_PATH], char subpath[NN_MAX_PATH]) {
if(!nn_path_hasSlash(path)) {
nn_strcpy(firstDirectory, path);
nn_strcpy(subpath, "");
return false; // end
}
nn_size_t slash = nn_path_firstSlash(path);
nn_memcpy(firstDirectory, path, slash);
firstDirectory[slash] = 0;
nn_strcpy(subpath, path + slash + 1);
return true;
}
// returns whether it is the only name
nn_bool_t nn_path_lastName(const char *path, char name[NN_MAX_PATH], char parent[NN_MAX_PATH]) {
if(!nn_path_hasSlash(path)) {
nn_strcpy(name, path);
nn_strcpy(parent, "");
return false; // end
}
nn_size_t slash = nn_path_lastSlash(path);
nn_strcpy(name, path + slash + 1);
nn_memcpy(parent, path, slash);
parent[slash] = 0;
return true;
}
const char *nn_path_illegal = "\"\\:*?<>|";
nn_bool_t nn_path_isValid(const char *path) {
// if we don't check for these, we will be FUCKED
for(nn_size_t i = 0; nn_path_illegal[i] != '\0'; i++) {
if(nn_strchr(path, nn_path_illegal[i]) != NULL) return false;
}
return nn_strlen(path) < NN_MAX_PATH; // less then because we depend on the terminator
}
nn_bool_t nn_path_canonical(const char path[NN_MAX_PATH], char canonical[NN_MAX_PATH]) {
// attempts to convert a random barely legal path
// if this shit is ever bugged and a sandbox escape is done
// by tricking it into sneaking some .. in there
// !!!! WE WILL BE FUCKED, THE SERVER WILL BE HACKED, AND WE WILL DIE !!!!
if(!nn_path_isValid(path)) {
// HELL NO
return false;
}
// tmp shit because lazy, maybe the optimizer be with us
char junk[NN_MAX_PATH];
// 0'd out because it fills it up with terminators, simplifying the rest of the code
// in theory this is suboptimal, however, I'm lazy
nn_memset(canonical, 0, NN_MAX_PATH);
size_t ptr = 0;
// TODO: burn it with fire and get banned from programming
for(nn_size_t i = 0; path[i]; i++) {
// all of this is very slow
while(path[i] == '/') continue; // just do not ask
}
return true;
}