linux lib compatibility

This commit is contained in:
2026-06-03 09:20:00 +02:00
parent cec92cacd1
commit 564292b03e
16 changed files with 75 additions and 166 deletions
-16
View File
@@ -1,16 +0,0 @@
//
// Created by Giorgio on 28/05/2026.
//
#ifndef BELUGA_UTILS_H
#define BELUGA_UTILS_H
#include <sys/_types/_size_t.h>
extern int beluga_alloc_counter;
void * bAlloc(size_t size);
void * bRealloc(void * ptr, size_t size);
void * bFree(void * ptr);
#endif //BELUGA_UTILS_H
+7 -8
View File
@@ -11,8 +11,6 @@
#include <libgen.h>
#include "include/utils.h"
#include "include/buffer.h"
#include "include/split_screen.h"
#include <string.h>
@@ -26,13 +24,15 @@
#include "include/completion.h"
#include <signal.h>
#include "include/utils.h"
struct editorConfig E;
int main(int argc, char *argv[]) {
char * splash_screen = bAlloc(sizeof(char) * 512);
char * splash_screen = strdup(getenv("HOME"));
int home_path_len = (int) strlen(splash_screen);
char * splash_screen_relative_path = strdup("/.beluga/assets/beluga.txt");
int splash_screen_relative_path_len = (int) strlen(splash_screen_relative_path);
signal(SIGPIPE, SIG_IGN); // don't die on broken pipe, just get EPIPE from write()
@@ -41,8 +41,9 @@ int main(int argc, char *argv[]) {
EditorPane *active = splitScreenGetActivePane();
struct buffer_t *buf;
strcat(splash_screen, getenv("HOME"));
splash_screen = realloc(splash_screen, sizeof(char) * (home_path_len + splash_screen_relative_path_len + 1));
strcat(splash_screen, "/.beluga/assets/beluga.txt");
free(splash_screen_relative_path);
appDebug("splash : %s\n", splash_screen);
active->buffer_id = bufferCreate(splash_screen, READ_ONLY);
@@ -59,12 +60,10 @@ int main(int argc, char *argv[]) {
}
bFree(splash_screen);
free(splash_screen);
editorSetStatusMessage("HELP: Ctrl-x Ctrl-s = save | Ctrl-x Ctrl-c = quit");
appDebug("allocation : %d\n", beluga_alloc_counter);
while (1) {
editorRefreshScreen();
editorProcessKeypress();
+1 -2
View File
@@ -27,8 +27,7 @@ src_files = files(
'src/utf8.c',
'src/completion.c',
'src/lsp_ui.c',
'src/cJSON.c',
'src/utils.c'
'src/cJSON.c'
)
# Executable
+2 -3
View File
@@ -1,8 +1,7 @@
#include "../include/append_buffer.h"
#include "include/utils.h"
void abAppend(struct abuf *ab, const char *s, int len) {
char *new = bRealloc(ab->b, ab->len + len);
char *new = realloc(ab->b, ab->len + len);
if (new == NULL) {
return;
@@ -12,4 +11,4 @@ void abAppend(struct abuf *ab, const char *s, int len) {
ab->len += len;
}
void abFree(const struct abuf *ab) { bFree(ab->b); }
void abFree(const struct abuf *ab) { free(ab->b); }
+10 -11
View File
@@ -17,7 +17,6 @@
#include "include/completion.h"
#include "include/input.h"
#include "include/utils.h"
/**
@@ -82,7 +81,7 @@ int bufferCreate(const char* path, enum bufferStatus_e state)
struct buffer_t* new_buf = &E.buffers[E.number_of_buffer];
new_buf->buffer_id = E.number_of_buffer;
new_buf->filename = strdup(filename);
new_buf->fullname = bAlloc(1024 * sizeof(char));
new_buf->fullname = malloc(1024 * sizeof(char));
realpath(path, new_buf->fullname);
new_buf->path = dirname(new_buf->fullname);
new_buf->type = FILE_BUFF;
@@ -176,7 +175,7 @@ int bufferClose(int buffer_id)
}
// Free buffer resources
bFree(buf->filename);
free(buf->filename);
buf->filename = NULL;
buf->buffer_id = -1;
@@ -312,7 +311,7 @@ void bufferFind(struct buffer_t* buf)
break;
}
}
bFree(query);
free(query);
}
void bufferFindReverse(struct buffer_t* buf)
@@ -335,14 +334,14 @@ void bufferFindReverse(struct buffer_t* buf)
break;
}
}
bFree(query);
free(query);
}
void bufferInsertRow(struct buffer_t *buffer, int at, char *s, size_t len) {
if (at < 0 || at > buffer->numrows)
return;
row_t *tmp = bRealloc(buffer->row, sizeof(row_t) * (buffer->numrows + 1));
row_t *tmp = realloc(buffer->row, sizeof(row_t) * (buffer->numrows + 1));
if (!tmp)
return;
buffer->row = tmp;
@@ -355,7 +354,7 @@ void bufferInsertRow(struct buffer_t *buffer, int at, char *s, size_t len) {
buffer->row[at].size = (int) len;
buffer->row[at].cap = (int) len + 1;
buffer->row[at].chars = bAlloc(len + 1);
buffer->row[at].chars = malloc(len + 1);
if (!buffer->row[at].chars)
return;
memcpy(buffer->row[at].chars, s, len);
@@ -365,7 +364,7 @@ void bufferInsertRow(struct buffer_t *buffer, int at, char *s, size_t len) {
buffer->dirty++;
}
void bufferFreeRow(row_t *row) { bFree(row->chars); }
void bufferFreeRow(row_t *row) { free(row->chars); }
/**
* \fn editorRowInsertChar(erow *row, int at, int c)
@@ -377,12 +376,12 @@ void bufferRowInsertBytes(struct buffer_t *buffer, row_t *row, int at,
return;
if (row->size + n + 1 > row->cap) {
row->cap = (row->size + n + 1) * 2;
row->chars = bRealloc(row->chars, row->cap);
row->chars = realloc(row->chars, row->cap);
}
memmove(row->chars + at + n, row->chars + at, row->size - at);
memcpy(row->chars + at, src, n);
row->size += n;
row->chars = bRealloc(row->chars, row->size + 2);
row->chars = realloc(row->chars, row->size + 2);
++buffer->dirty;
}
@@ -446,7 +445,7 @@ void bufferDelBytes(void)
int prev_char_count = editorRowCharCount(prev, prev->size);
bufferRowInsertBytes(buf, prev, prev->size, r->chars, r->size);
bFree(r->chars);
free(r->chars);
r->chars = NULL;
memmove(&buf->row[buf->y],
+14 -16
View File
@@ -22,7 +22,6 @@
#include "include/completion.h"
#include "include/init.h"
#include "include/utils.h"
/**
* @brief Finds a prefix configuration by name
@@ -69,13 +68,13 @@ Lisp mapKey(Lisp args, LispError* e, LispContext ctx)
// second argument
const Lisp func = lisp_car(args);
memory_temp = bRealloc(
memory_temp = realloc(
E.key_binds, ++E.number_of_keybinds * sizeof(struct keyBind_t));
E.key_binds = (struct keyBind_t*)memory_temp;
if (!E.key_binds)
editorQuit(args, e, ctx);
E.key_binds[E.number_of_keybinds - 1].key_sequence =
(char*)bAlloc(50 * sizeof(char));
(char*)malloc(50 * sizeof(char));
strncpy(E.key_binds[E.number_of_keybinds - 1].key_sequence, key_sequence, 50);
@@ -137,27 +136,27 @@ Lisp moveCursor(Lisp args, LispError* e, LispContext ctx)
void bFree_structs(void)
{
int i, j;
bFree(E.prefix);
free(E.prefix);
for (i = 0; i < E.number_of_keybinds; ++i)
{
bFree(E.key_binds[i].key_sequence);
free(E.key_binds[i].key_sequence);
}
bFree(E.key_binds);
free(E.key_binds);
// bFree layout
bFree(E.layout.panes);
free(E.layout.panes);
// bFree buffers
for (i = 0; i < E.number_of_buffer; ++i)
{
bFree(E.buffers[i].filename);
free(E.buffers[i].filename);
for (j = 0; j < E.buffers[i].numrows; ++j)
{
bFree(E.buffers[i].row[j].chars);
free(E.buffers[i].row[j].chars);
}
bFree(E.buffers[i].row);
free(E.buffers[i].row);
}
bFree(E.init_file_path);
free(E.init_file_path);
fclose(E.fd_init_file);
}
@@ -186,11 +185,10 @@ Lisp editorQuit(Lisp args, LispError* e, LispContext ctx)
bFree_structs();
write(STDOUT_FILENO, "\x1b[2J", 4);
write(STDOUT_FILENO, CURSOR_TOP_LEFT, 3);
disableRawMode();
lspShutdown(E.lsp_client);
lisp_shutdown(E.ctx);
deInitEditor();
appDebug("Rest alloc %d\n", beluga_alloc_counter);
disableRawMode();
exit(0);
}
@@ -379,7 +377,7 @@ Lisp editorOpenFile(Lisp args, LispError* e, LispContext ctx)
EditorPane* active = splitScreenGetActivePane();
active->buffer_id = bufferCreate(filename, READ_AND_WRITE);
}
bFree(filename);
free(filename);
return lisp_null();
}
@@ -427,7 +425,7 @@ Lisp addPackage(Lisp args, LispError* e, LispContext ctx)
lisp_eval(lisp_read_file(fd_package, &E.ctx_error, E.ctx), &E.ctx_error,
E.ctx);
fclose(fd_package);
bFree(package_dir);
free(package_dir);
return lisp_null();
}
@@ -558,7 +556,7 @@ Lisp editorSetPrefix(Lisp args, LispError* e, LispContext ctx)
*/
Lisp editorPrefix(Lisp args, LispError* e, LispContext ctx)
{
E.prefix = (struct prefix_t*)bRealloc(E.prefix, (++(E.number_of_prefix) + 1) *
E.prefix = (struct prefix_t*)realloc(E.prefix, (++(E.number_of_prefix) + 1) *
sizeof(struct prefix_t));
E.prefix[E.number_of_prefix].prefix_id = E.number_of_prefix;
strncpy(E.prefix[E.number_of_prefix].prefix_name, lisp_string(lisp_car(args)),
+14 -31
View File
@@ -15,7 +15,6 @@
#include "include/lsp_ui.h"
#include "include/split_screen.h"
#include "include/terminal.h"
#include "include/utils.h"
static void lsp_send(int fd, const char* json)
{
@@ -33,22 +32,6 @@ static void lsp_send(int fd, const char* json)
fflush(stderr);
}
static int lsp_uri_to_buffer_id(const char* uri)
{
const char *path = uri;
if (strncmp(uri, "file://", 7) == 0)
path = uri + 7;
// path is now "/absolute/path" — realpath output matches this directly
for (int i = 0; i < E.number_of_buffer; i++) {
if (E.buffers[i].filename == NULL) continue;
appDebug("[URI MATCH] comparing '%s' vs '%s'\n", E.buffers[i].fullname, path);
if (strcmp(E.buffers[i].fullname, path) == 0)
return E.buffers[i].buffer_id;
}
return -1;
}
static char* lsp_recv(int fd)
{
char header[1024];
@@ -74,14 +57,14 @@ static char* lsp_recv(int fd)
if (content_length == 0) return NULL;
char* body = bAlloc(content_length + 1);
char* body = malloc(content_length + 1);
int total = 0;
while (total < content_length)
{
int n = read(fd, body + total, content_length - total);
if (n <= 0)
{
bFree(body);
free(body);
return NULL;
}
total += n;
@@ -251,7 +234,7 @@ static void* lsp_reader(void* arg)
char* msg = lsp_recv(lsp->read_fd);
if (!msg) break; // ← pipe closed or error, exit cleanly
lsp_dispatch(lsp, msg);
bFree(msg);
free(msg);
}
return NULL;
}
@@ -382,7 +365,7 @@ static char* buffer_to_text(struct buffer_t* buf)
int total = 0;
for (int i = 0; i < buf->numrows; i++)
total += buf->row[i].size + 1; // +1 for \n
char* text = bAlloc(total + 1);
char* text = malloc(total + 1);
char* p = text;
for (int i = 0; i < buf->numrows; i++)
{
@@ -428,8 +411,8 @@ void lspDidOpen(LspClient* lsp, struct buffer_t* buf)
char* msg = cJSON_PrintUnformatted(root);
lsp_send(lsp->write_fd, msg);
bFree(msg);
bFree(raw);
free(msg);
free(raw);
cJSON_Delete(root);
buf->b_lsp_open = 1;
}
@@ -466,8 +449,8 @@ void lspDidChange(LspClient* lsp, struct buffer_t* buf)
char* msg = cJSON_PrintUnformatted(root);
lsp_send(lsp->write_fd, msg);
bFree(msg);
bFree(raw);
free(msg);
free(raw);
cJSON_Delete(root);
}
@@ -487,7 +470,7 @@ void lspDidClose(LspClient* lsp, struct buffer_t* buf)
cJSON_AddItemToObject(root, "params", params);
char* msg = cJSON_PrintUnformatted(root);
lsp_send(lsp->write_fd, msg);
bFree(msg);
free(msg);
cJSON_Delete(root);
buf->b_lsp_open = 0;
}
@@ -531,7 +514,7 @@ void lspRequestCompletion(LspClient* lsp, struct buffer_t* buf,
lsp_send(lsp->write_fd, msg);
E.lsp_client->completion_requested = 1;
cJSON_Delete(req);
bFree(msg);
free(msg);
}
void lspRequestDefinition(LspClient* lsp, struct buffer_t* buf, int line, int col)
@@ -544,7 +527,7 @@ void lspRequestDefinition(LspClient* lsp, struct buffer_t* buf, int line, int co
"\"position\":{\"line\":%d,\"character\":%d}}}",
lsp->next_id++, buf->filename, line, col);
lsp_send(lsp->write_fd, msg);
bFree(msg);
free(msg);
}
void lspShutdown(LspClient *lsp)
@@ -567,7 +550,7 @@ void lspShutdown(LspClient *lsp)
cJSON_AddNullToObject (req, "params");
char *msg = cJSON_PrintUnformatted(req);
lsp_send(lsp->write_fd, msg);
bFree(msg);
free(msg);
cJSON_Delete(req);
// 3. Wait briefly for the shutdown response (2s timeout)
@@ -577,7 +560,7 @@ void lspShutdown(LspClient *lsp)
FD_SET(lsp->read_fd, &fds);
if (select(lsp->read_fd + 1, &fds, NULL, NULL, &tv) > 0) {
char *resp = lsp_recv(lsp->read_fd);
bFree(resp);
free(resp);
}
// 4. Send exit notification
@@ -607,5 +590,5 @@ void lspShutdown(LspClient *lsp)
// 10. Reap the clangd process
waitpid(lsp->pid, NULL, 0);
bFree(lsp);
free(lsp);
}
+2 -3
View File
@@ -8,7 +8,6 @@
#include "../include/split_screen.h"
#include "../include/terminal.h"
#include "../include/utf8.h"
#include "include/utils.h"
extern struct editorConfig E;
@@ -86,13 +85,13 @@ char *editorGetClipboard(void) {
size_t cap = 4096;
size_t len = 0;
char *buf = bAlloc(cap);
char *buf = malloc(cap);
int c;
while ((c = fgetc(pipe)) != EOF) {
if (len + 1 >= cap) {
cap *= 2;
buf = bRealloc(buf, cap);
buf = realloc(buf, cap);
}
buf[len++] = (char)c;
}
+5 -7
View File
@@ -13,7 +13,6 @@
#include "../include/data.h"
#include "../include/split_screen.h"
#include "../include/row_op.h"
#include <_string.h>
#include <fcntl.h>
#include <stdio.h>
#include <stdlib.h>
@@ -21,7 +20,6 @@
#include <unistd.h>
#include <errno.h>
#include "../include/utils.h"
/**
* @brief Closes the current file and resets editor state
@@ -37,13 +35,13 @@ void editorCloseFile(void) {
active->x_offset = 0;
active->y_offset = 0;
for (int i = 0; i < buf->numrows; ++i) {
bFree(buf->row[i].chars);
free(buf->row[i].chars);
}
buf->numrows = 0;
bFree(buf->row);
free(buf->row);
buf->row = NULL;
buf->dirty = 0;
bFree(buf->filename);
free(buf->filename);
buf->filename = NULL;
E.status_msg[0] = '\0';
E.status_msg_time = 0;
@@ -86,10 +84,10 @@ void editorOpen(struct buffer_t* buffer) {
}
appDebug("line %s\n", line);
bufferInsertRow(buffer, buffer->numrows, line, line_len);
bFree(line);
free(line);
line = NULL;
}
bFree(line);
free(line);
fclose(fp);
E.dirty = 0;
}
+7 -8
View File
@@ -11,7 +11,6 @@
#define LISP_IMPLEMENTATION
#include "../include/lisp.h"
#include "../include/lisp_lib.h"
#include "include/utils.h"
struct editorConfig;
@@ -133,12 +132,12 @@ void initEditor() {
E.number_of_keybinds = 0;
E.number_of_prefix = 0;
// General prefix is 0 (no prefix)
E.prefix = (struct prefix_t *)bAlloc(sizeof(struct prefix_t));
E.prefix = (struct prefix_t *)malloc(sizeof(struct prefix_t));
E.prefix[0].prefix_id = 0;
strncpy(E.prefix[0].prefix_name, "no-prefix", 64);
E.prefix_state = 0;
E.lsp_client = (LspClient*)bAlloc(sizeof(LspClient));
E.lsp_client = (LspClient*)malloc(sizeof(LspClient));
E.lsp_client->state = LSP_SHUTDOWN;
initConfig();
@@ -161,13 +160,13 @@ void initEditor() {
void deInitEditor()
{
freeScreenLayout(&E.layout);
bFree(E.lsp_client);
bFree(E.status_msg);
bFree(E.init_file_path);
bFree(E.key_binds);
free(E.lsp_client);
free(E.status_msg);
free(E.init_file_path);
free(E.key_binds);
for (int i = 0; i < E.number_of_keybinds; i++)
{
bFree(E.key_binds[i].key_sequence);
free(E.key_binds[i].key_sequence);
}
}
+5 -6
View File
@@ -16,7 +16,6 @@
#include "include/terminal.h"
#include "include/utf8.h"
#include "include/utils.h"
/**
* @file input.c
@@ -91,7 +90,7 @@ const char *fileCompletion(const char *path) {
// Cleanup when no more entries
closedir(dir);
dir = NULL;
bFree(entry);
free(entry);
appDebug("[FILE COMP] no entries\n");
return strdup(path);
}
@@ -113,7 +112,7 @@ const char *fileCompletion(const char *path) {
char *editorPrompt(char *prompt, char *placeHolder, char bPathMode) {
size_t buf_size = 256;
appDebug("[FILE COMP] %s %d\n", placeHolder, strlen(placeHolder));
char *buf = bAlloc(buf_size);
char *buf = malloc(buf_size);
size_t buf_len = 0;
int c = 0;
buf[0] = '\0';
@@ -130,7 +129,7 @@ char *editorPrompt(char *prompt, char *placeHolder, char bPathMode) {
}
} else if (c == ESCAPE) {
editorSetStatusMessage("");
bFree(buf);
free(buf);
return NULL;
} else if (c == '\r') {
if (buf_len != 0) {
@@ -152,14 +151,14 @@ char *editorPrompt(char *prompt, char *placeHolder, char bPathMode) {
memset(buf, 0, 256);
char *buf_complete = (char *)fileCompletion(path);
strcpy(buf, buf_complete);
bFree(buf_complete);
free(buf_complete);
buf_len = strlen(buf);
buf[buf_len] = '\0';
} else if (!iscntrl(c) && c < 256) {
if (buf_len == buf_size - 1) {
buf_size *= 2;
buf = bRealloc(buf, buf_size);
buf = realloc(buf, buf_size);
}
buf[buf_len++] = (char)c;
buf[buf_len] = '\0';
+1 -4
View File
@@ -21,9 +21,6 @@
#include <string.h>
#include <time.h>
#include "include/completion.h"
#include "include/utils.h"
/**
* @brief Renders a single pane with its buffer content
*/
@@ -85,7 +82,7 @@ static void editorDrawPane(struct abuf* ab, EditorPane* pane)
// Print only up to pane width
abAppend(ab, highlighted, byte_len_to_print);
bFree(highlighted);
free(highlighted);
}
else
{
+5 -11
View File
@@ -9,7 +9,6 @@
#include <stdlib.h>
#include <string.h>
#include "include/utils.h"
extern struct editorConfig E;
@@ -21,7 +20,7 @@ void splitScreenInit(void) {
E.layout.num_panes = 1;
E.layout.active_pane = 0;
E.layout.panes = bAlloc(sizeof(EditorPane) * 2);
E.layout.panes = malloc(sizeof(EditorPane) * 2);
// Initialize single fullscreen pane
E.layout.panes[0].buffer_id = -1; // No buffer for now
@@ -51,7 +50,7 @@ int splitScreenVertical(int buffer_id_left, int buffer_id_right) {
}
// bReallocate panes array
E.layout.panes = bRealloc(E.layout.panes, sizeof(EditorPane) * 2);
E.layout.panes = realloc(E.layout.panes, sizeof(EditorPane) * 2);
E.layout.mode = SPLIT_VERTICAL;
E.layout.num_panes = 2;
E.layout.active_pane = 0;
@@ -102,7 +101,7 @@ int splitScreenHorizontal(int buffer_id_top, int buffer_id_bottom) {
}
// bReallocate panes array
E.layout.panes = bRealloc(E.layout.panes, sizeof(EditorPane) * 2);
E.layout.panes = realloc(E.layout.panes, sizeof(EditorPane) * 2);
E.layout.mode = SPLIT_HORIZONTAL;
E.layout.num_panes = 2;
E.layout.active_pane = 0;
@@ -221,15 +220,10 @@ EditorPane *splitScreenGetActivePane(void) {
void freeScreenLayout(ScreenLayout *layout)
{
while (--E.layout.num_panes >= 0)
{
bFree(&E.layout.panes);
}
bFree(&E.layout);
free(layout->panes);
}
void freePane(EditorPane *pane)
{
bFree(pane);
free(pane);
}
+1 -2
View File
@@ -5,7 +5,6 @@
#include <stdlib.h>
#include <string.h>
#include "include/utils.h"
extern struct editorConfig E;
@@ -113,7 +112,7 @@ char *highlight_line(const char *line, int *length) {
// Allocate generously based on line length to avoid overflow.
int line_len = strlen(line);
int buf_size = line_len * 32 + 256;
char *result = bAlloc(buf_size);
char *result = malloc(buf_size);
int result_pos = 0;
int i = 0;
+1 -1
View File
@@ -12,7 +12,7 @@
#include "../include/buffer.h"
#include "../include/split_screen.h"
#include "include/utf8.h"
#include "../include/utf8.h"
void die(const char* s)
{
-37
View File
@@ -1,37 +0,0 @@
//
// Created by Giorgio on 28/05/2026.
//
#include "../include/utils.h"
#include <stdlib.h>
int beluga_alloc_counter = 0;
void * bAlloc(size_t size)
{
void * result = malloc(size);
if (!result)
return NULL;
beluga_alloc_counter++;
return result;
}
void * bRealloc(void * ptr, size_t size)
{
void * result = realloc(ptr, size);
if (!result)
return NULL;
beluga_alloc_counter++;
return result;
}
void * bFree(void * ptr)
{
if (ptr)
{
free(ptr);
beluga_alloc_counter--;
}
return NULL;
}