From 13771cc5366474e8a72a3451a4ebba94fdfa1dcb Mon Sep 17 00:00:00 2001 From: Christoffer Lerno Date: Wed, 29 Jan 2025 13:44:04 +0100 Subject: [PATCH] Truncate output from execute and print to stdout on error. --- src/build/libraries.c | 4 ++-- src/compiler/compiler.c | 32 +++++++++++++++++--------------- src/compiler/compiler_internal.h | 2 +- src/compiler/sema_passes.c | 4 ++-- src/utils/file_utils.c | 30 ++++++++++++++++++++++-------- src/utils/find_msvc.c | 2 +- src/utils/lib.h | 4 ++-- 7 files changed, 47 insertions(+), 31 deletions(-) diff --git a/src/build/libraries.c b/src/build/libraries.c index 865d87e84..4c5559bdc 100644 --- a/src/build/libraries.c +++ b/src/build/libraries.c @@ -324,12 +324,12 @@ void resolve_libraries(BuildTarget *build_target) FOREACH(const char *, exec, library->execs) { printf("] Execute '%s' for library '%s':", exec, library->provides); - puts(execute_cmd(exec, false, NULL)); + puts(execute_cmd(exec, false, NULL, 2048)); } FOREACH(const char *, exec, target->execs) { printf("] Execute '%s' for library '%s':", exec, library->provides); - puts(execute_cmd(exec, false, NULL)); + puts(execute_cmd(exec, false, NULL, 2048)); } } } diff --git a/src/compiler/compiler.c b/src/compiler/compiler.c index 5cb4e7e88..e78232c0c 100644 --- a/src/compiler/compiler.c +++ b/src/compiler/compiler.c @@ -1128,30 +1128,20 @@ void execute_scripts(void) if (call.len < 3 || call.ptr[call.len - 3] != '.' || call.ptr[call.len - 2] != 'c' || call.ptr[call.len - 1] != '3') { - char *res = execute_cmd(exec, false, NULL); + char *res = execute_cmd(exec, false, NULL, 0); if (compiler.build.silent) continue; script = source_file_text_load(exec, res); goto PRINT_SCRIPT; } scratch_buffer_clear(); scratch_buffer_append_len(call.ptr, call.len); - script = compile_and_invoke(scratch_buffer_copy(), execs.len ? execs.ptr : "", NULL); + script = compile_and_invoke(scratch_buffer_copy(), execs.len ? execs.ptr : "", NULL, 2048); PRINT_SCRIPT:; size_t out_len = script->content_len; const char *out = script->contents; if (!compiler.build.silent && script->content_len > 0) { - if (out_len > 2048) - { - puts("Truncated script output --------------------------------->"); - out_len = 2048; - } - else - { - puts("Script output ------------------------------------------->"); - } printf("%.*s\n", (int)out_len, out); - puts("---------------------------------------------------------<"); } } dir_change(old_path); @@ -1522,7 +1512,7 @@ void scratch_buffer_append_native_safe_path(const char *data, int len) #endif } -File *compile_and_invoke(const char *file, const char *args, const char *stdin_data) +File *compile_and_invoke(const char *file, const char *args, const char *stdin_data, size_t limit) { char *name; if (!file_namesplit(compiler_exe_name, &name, NULL)) @@ -1547,8 +1537,14 @@ File *compile_and_invoke(const char *file, const char *args, const char *stdin_d scratch_buffer_printf(" -o %s", output); char *out; if (PLATFORM_WINDOWS) scratch_buffer_append_char('"'); - if (!execute_cmd_failable(scratch_buffer_to_string(), &out, NULL)) + if (!execute_cmd_failable(scratch_buffer_to_string(), &out, NULL, limit)) { + if (strlen(out)) + { + eprintf("+-- Script compilation output ---------+\n"); + eprintf("%s\n", out); + eprintf("+--------------------------------------+\n"); + } error_exit("Failed to compile script '%s'.", file); } DEBUG_LOG("EXEC OUT: %s", out); @@ -1559,8 +1555,14 @@ File *compile_and_invoke(const char *file, const char *args, const char *stdin_d scratch_buffer_append(output); scratch_buffer_append(" "); scratch_buffer_append(args); - if (!execute_cmd_failable(scratch_buffer_to_string(), &out, stdin_data)) + if (!execute_cmd_failable(scratch_buffer_to_string(), &out, stdin_data, limit)) { + if (strlen(out)) + { + eprintf("+-- Script output ---------------------+\n"); + eprintf("%s\n", out); + eprintf("+--------------------------------------+\n"); + } error_exit("Error invoking script '%s' with arguments %s.", file, args); } file_delete_file(output); diff --git a/src/compiler/compiler_internal.h b/src/compiler/compiler_internal.h index 3b508549a..c5c8519ce 100644 --- a/src/compiler/compiler_internal.h +++ b/src/compiler/compiler_internal.h @@ -2374,7 +2374,7 @@ File *source_file_load(const char *filename, bool *already_loaded, const char ** File *source_file_generate(const char *filename); File *source_file_text_load(const char *filename, char *content); -File *compile_and_invoke(const char *file, const char *args, const char *stdin_data); +File *compile_and_invoke(const char *file, const char *args, const char *stdin_data, size_t limit); void compiler_parse(void); void emit_json(void); diff --git a/src/compiler/sema_passes.c b/src/compiler/sema_passes.c index ddca42c67..af8af47c8 100644 --- a/src/compiler/sema_passes.c +++ b/src/compiler/sema_passes.c @@ -292,11 +292,11 @@ static Decl **sema_run_exec(CompilationUnit *unit, Decl *decl) } if (c3_script) { - file = compile_and_invoke(file_str, scratch_buffer_copy(), stdin_string); + file = compile_and_invoke(file_str, scratch_buffer_copy(), stdin_string, 0); } else { - char *output = execute_cmd(scratch_buffer_to_string(), false, stdin_string); + char *output = execute_cmd(scratch_buffer_to_string(), false, stdin_string, 0); file = source_file_text_load(scratch_buffer_to_string(), output); } if (old_path) diff --git a/src/utils/file_utils.c b/src/utils/file_utils.c index 950731851..6cf42cf6b 100644 --- a/src/utils/file_utils.c +++ b/src/utils/file_utils.c @@ -542,7 +542,7 @@ void file_copy_file(const char *src_path, const char *dst_path, bool overwrite) CopyFileW(win_utf8to16(src_path), win_utf8to16(dst_path), !overwrite); #else const char *cmd = "cp %s %s %s"; - execute_cmd(str_printf(cmd, !overwrite ? "--update=none" : "--update=all", src_path, dst_path), true, NULL); + execute_cmd(str_printf(cmd, !overwrite ? "--update=none" : "--update=all", src_path, dst_path), true, NULL, 2048); #endif } @@ -564,7 +564,7 @@ void file_delete_all_files_in_dir_with_suffix(const char *path, const char *suff #else const char *cmd = "rm -f %s/*%s"; #endif - execute_cmd(str_printf(cmd, path, suffix), true, NULL); + execute_cmd(str_printf(cmd, path, suffix), true, NULL, 2048); } #if (_MSC_VER) @@ -704,19 +704,25 @@ const char **target_expand_source_names(const char *base_dir, const char** dirs, #define BUFSIZE 1024 -char *execute_cmd(const char *cmd, bool ignore_failure, const char *stdin_string) +char *execute_cmd(const char *cmd, bool ignore_failure, const char *stdin_string, size_t limit) { char *result = NULL; - bool success = execute_cmd_failable(cmd, &result, stdin_string); + bool success = execute_cmd_failable(cmd, &result, stdin_string, limit); if (!success) { if (ignore_failure) return ""; + if (strlen(result)) + { + eprintf("+-- Command output --------------------+\n"); + eprintf("%s\n", result); + eprintf("+--------------------------------------+\n"); + } error_exit("Failed to execute '%s'.", cmd); } return result; } -bool execute_cmd_failable(const char *cmd, char **result, const char *stdin_string) +bool execute_cmd_failable(const char *cmd, char **result, const char *stdin_string, size_t limit) { DEBUG_LOG("Executing: %s", cmd); char buffer[BUFSIZE]; @@ -743,8 +749,18 @@ bool execute_cmd_failable(const char *cmd, char **result, const char *stdin_stri #else if (!(process = popen(cmd, "r"))) return false; #endif + unsigned len = 0; while (fgets(buffer, BUFSIZE - 1, process)) { + if (limit) + { + if (len > limit) + { + output = str_cat(output, " ... [TRUNCATED]"); + break; + } + len += strlen(buffer); + } output = str_cat(output, buffer); } #if PLATFORM_WINDOWS @@ -756,8 +772,6 @@ bool execute_cmd_failable(const char *cmd, char **result, const char *stdin_stri { file_delete_file("__c3temp.bin"); } - if (err) return false; - while (output[0] != 0) { switch (output[0]) @@ -774,7 +788,7 @@ bool execute_cmd_failable(const char *cmd, char **result, const char *stdin_stri break; } *result = str_trim(output); - return true; + return !err; } #if PLATFORM_WINDOWS diff --git a/src/utils/find_msvc.c b/src/utils/find_msvc.c index a061c98e0..95803b4f6 100644 --- a/src/utils/find_msvc.c +++ b/src/utils/find_msvc.c @@ -38,7 +38,7 @@ static char *find_visual_studio(void) char *install_path = NULL; // Call vswhere.exe - if (!execute_cmd_failable(scratch_buffer_to_string(), &install_path, NULL)) + if (!execute_cmd_failable(scratch_buffer_to_string(), &install_path, NULL, 0)) { error_exit("Failed to find vswhere.exe to detect MSVC."); } diff --git a/src/utils/lib.h b/src/utils/lib.h index 66ee7c74e..f3ad62711 100644 --- a/src/utils/lib.h +++ b/src/utils/lib.h @@ -92,9 +92,9 @@ const char *file_append_path_temp(const char *path, const char *name); const char **target_expand_source_names(const char *base_dir, const char** dirs, const char **suffix_list, const char ***object_list_ref, int suffix_count, bool error_on_mismatch); -char * execute_cmd(const char *cmd, bool ignore_failure, const char *stdin_string); +char * execute_cmd(const char *cmd, bool ignore_failure, const char *stdin_string, size_t limit); -bool execute_cmd_failable(const char *cmd, char **result, const char *stdin_string); +bool execute_cmd_failable(const char *cmd, char **result, const char *stdin_string, size_t limit); void *cmalloc(size_t size); void *ccalloc(size_t size, size_t elements); void memory_init(size_t max_mem);