add cmd_run_async()

This commit is contained in:
kamkow1
2025-06-14 22:47:54 +02:00
parent 61e59e6550
commit 2f9ff2e757

35
gebs.h
View File

@ -338,6 +338,10 @@ typedef Gebs_NString_List Gebs_Cmd;
int gebs_cmd_run_sync_alloc(Gebs_Allocator *alloc, Gebs_Cmd *cmd); int gebs_cmd_run_sync_alloc(Gebs_Allocator *alloc, Gebs_Cmd *cmd);
int gebs_cmd_fd_run_sync_alloc(Gebs_Allocator *alloc, int fd, Gebs_Cmd *cmd); int gebs_cmd_fd_run_sync_alloc(Gebs_Allocator *alloc, int fd, Gebs_Cmd *cmd);
#define gebs_cmd_run_async(cmd) gebs_cmd_run_async_alloc(&gebs_default_allocator, (cmd))
pid_t gebs_cmd_run_async_alloc(Gebs_Allocator *alloc, Gebs_Cmd *cmd);
#define GEBS_CMD(...) \ #define GEBS_CMD(...) \
({ \ ({ \
char *__args[] = { __VA_ARGS__ }; \ char *__args[] = { __VA_ARGS__ }; \
@ -644,6 +648,35 @@ int gebs_cmd_fd_run_sync_collect_alloc(Gebs_Allocator *alloc, int fd, Gebs_Cmd *
} }
} }
pid_t gebs_cmd_run_async_alloc(Gebs_Allocator *alloc, Gebs_Cmd *cmd)
{
Gebs_String_Builder sb = {0};
defer { gebs_sb_free_alloc(alloc, &sb); }
gebs_nsl_join_alloc(alloc, cmd, &sb, " ");
gebs_sb_finish_alloc(alloc, &sb);
GEBS_LOGI("cmd `%s`\n", sb.items);
// Clone the list
Gebs_Cmd new_cmd = {0};
defer { gebs_cmd_free_alloc(alloc, &new_cmd); }
for (size_t i = 0; i < cmd->count; i++) {
gebs_cmd_append_alloc(alloc, &new_cmd, cmd->items[i]);
}
gebs_cmd_append_alloc((alloc), &new_cmd, nil);
pid_t pid = vfork();
switch (pid) {
case -1:
GEBS_LOGE("Could not vfork\n");
return -1;
case 0:
execvp(new_cmd.items[0], (char * const *)new_cmd.items);
exit(EXIT_FAILURE);
default:
return pid;
}
}
// ---------------------------------------------------------------------------- // ----------------------------------------------------------------------------
// Allocators // Allocators
// ---------------------------------------------------------------------------- // ----------------------------------------------------------------------------
@ -958,6 +991,8 @@ void gebs_rebuild_self1_alloc(Gebs_Allocator *alloc, int argc, char ** argv,
#define cmd_fd_run_sync_alloc gebs_cmd_fd_run_sync_alloc #define cmd_fd_run_sync_alloc gebs_cmd_fd_run_sync_alloc
#define cmd_fd_run_alloc gebs_cmd_fd_run_alloc #define cmd_fd_run_alloc gebs_cmd_fd_run_alloc
#define cmd_fd_run gebs_cmd_fd_run #define cmd_fd_run gebs_cmd_fd_run
#define cmd_run_async_alloc gebs_cmd_run_async_alloc
#define cmd_run_async gebs_cmd_run_async
#define needs_rebuild gebs_needs_rebuild #define needs_rebuild gebs_needs_rebuild
#define needs_rebuild_many gebs_needs_rebuild_many #define needs_rebuild_many gebs_needs_rebuild_many