Skip to content

Commit 652f262

Browse files
committed
Added run_system_command API
1 parent f896046 commit 652f262

4 files changed

Lines changed: 69 additions & 3 deletions

File tree

code/cbuild_api.cpp

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
#include "anyfin/startup.hpp"
44
#include "anyfin/console.hpp"
55
#include "anyfin/platform.hpp"
6+
#include "anyfin/commands.hpp"
67

78
#include "cbuild.hpp"
89
#include "cbuild_api.hpp"
@@ -526,3 +527,22 @@ CBUILD_EXPERIMENTAL_API const char * find_executable (Project *project, const ch
526527
auto [system_error, path] = find_executable(project->arena, String(name, get_string_length(name)));
527528
return (system_error || path.is_none()) ? nullptr : path.value.value;
528529
}
530+
531+
CBUILD_EXPERIMENTAL_API int run_system_command (Project *project, const char *command_str, char *buffer, unsigned int buffer_size, unsigned int *written_size) CBUILD_NO_EXCEPT {
532+
auto command = String(command_str, get_string_length(command_str));
533+
534+
auto [system_error, status] = run_system_command(project->arena, command);
535+
if (system_error) panic("Command '%' execution failed due to a system error: %\n", command, system_error.value);
536+
537+
if (status.output.length == 0 || !buffer || !buffer_size) {
538+
if (written_size) *written_size = 0;
539+
return status.status_code;
540+
}
541+
542+
auto bytes_to_copy = (status.output.length < buffer_size) ? status.output.length : buffer_size;
543+
copy_memory(buffer, status.output.value, bytes_to_copy);
544+
545+
if (written_size) *written_size = bytes_to_copy;
546+
547+
return status.status_code;
548+
}

code/templates/cbuild_api_experimental_template.hpp

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,8 @@ CBUILD_EXPERIMENTAL_API void add_system_include_search_path (Target *target, con
4949

5050
CBUILD_EXPERIMENTAL_API const char * find_executable (Project *project, const char *name) CBUILD_NO_EXCEPT;
5151

52+
CBUILD_EXPERIMENTAL_API int run_system_command (Project *project, const char *command_name, char *buffer, unsigned int buffer_size, unsigned int *written_size) CBUILD_NO_EXCEPT;
53+
5254
#ifdef __cplusplus
5355
}
5456
#endif

0 commit comments

Comments
 (0)