Truncate output from execute and print to stdout on error.

This commit is contained in:
Christoffer Lerno
2025-01-29 13:44:04 +01:00
committed by Christoffer Lerno
parent ac3b2f0fea
commit 13771cc536
7 changed files with 47 additions and 31 deletions

View File

@@ -324,12 +324,12 @@ void resolve_libraries(BuildTarget *build_target)
FOREACH(const char *, exec, library->execs) FOREACH(const char *, exec, library->execs)
{ {
printf("] Execute '%s' for library '%s':", exec, library->provides); 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) FOREACH(const char *, exec, target->execs)
{ {
printf("] Execute '%s' for library '%s':", exec, library->provides); printf("] Execute '%s' for library '%s':", exec, library->provides);
puts(execute_cmd(exec, false, NULL)); puts(execute_cmd(exec, false, NULL, 2048));
} }
} }
} }

View File

@@ -1128,30 +1128,20 @@ void execute_scripts(void)
if (call.len < 3 || call.ptr[call.len - 3] != '.' || call.ptr[call.len - 2] != 'c' || if (call.len < 3 || call.ptr[call.len - 3] != '.' || call.ptr[call.len - 2] != 'c' ||
call.ptr[call.len - 1] != '3') 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; if (compiler.build.silent) continue;
script = source_file_text_load(exec, res); script = source_file_text_load(exec, res);
goto PRINT_SCRIPT; goto PRINT_SCRIPT;
} }
scratch_buffer_clear(); scratch_buffer_clear();
scratch_buffer_append_len(call.ptr, call.len); 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:; PRINT_SCRIPT:;
size_t out_len = script->content_len; size_t out_len = script->content_len;
const char *out = script->contents; const char *out = script->contents;
if (!compiler.build.silent && script->content_len > 0) 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); printf("%.*s\n", (int)out_len, out);
puts("---------------------------------------------------------<");
} }
} }
dir_change(old_path); dir_change(old_path);
@@ -1522,7 +1512,7 @@ void scratch_buffer_append_native_safe_path(const char *data, int len)
#endif #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; char *name;
if (!file_namesplit(compiler_exe_name, &name, NULL)) 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); scratch_buffer_printf(" -o %s", output);
char *out; char *out;
if (PLATFORM_WINDOWS) scratch_buffer_append_char('"'); 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); error_exit("Failed to compile script '%s'.", file);
} }
DEBUG_LOG("EXEC OUT: %s", out); 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(output);
scratch_buffer_append(" "); scratch_buffer_append(" ");
scratch_buffer_append(args); 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); error_exit("Error invoking script '%s' with arguments %s.", file, args);
} }
file_delete_file(output); file_delete_file(output);

View File

@@ -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_generate(const char *filename);
File *source_file_text_load(const char *filename, char *content); 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 compiler_parse(void);
void emit_json(void); void emit_json(void);

View File

@@ -292,11 +292,11 @@ static Decl **sema_run_exec(CompilationUnit *unit, Decl *decl)
} }
if (c3_script) 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 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); file = source_file_text_load(scratch_buffer_to_string(), output);
} }
if (old_path) if (old_path)

View File

@@ -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); CopyFileW(win_utf8to16(src_path), win_utf8to16(dst_path), !overwrite);
#else #else
const char *cmd = "cp %s %s %s"; 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 #endif
} }
@@ -564,7 +564,7 @@ void file_delete_all_files_in_dir_with_suffix(const char *path, const char *suff
#else #else
const char *cmd = "rm -f %s/*%s"; const char *cmd = "rm -f %s/*%s";
#endif #endif
execute_cmd(str_printf(cmd, path, suffix), true, NULL); execute_cmd(str_printf(cmd, path, suffix), true, NULL, 2048);
} }
#if (_MSC_VER) #if (_MSC_VER)
@@ -704,19 +704,25 @@ const char **target_expand_source_names(const char *base_dir, const char** dirs,
#define BUFSIZE 1024 #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; 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 (!success)
{ {
if (ignore_failure) return ""; if (ignore_failure) return "";
if (strlen(result))
{
eprintf("+-- Command output --------------------+\n");
eprintf("%s\n", result);
eprintf("+--------------------------------------+\n");
}
error_exit("Failed to execute '%s'.", cmd); error_exit("Failed to execute '%s'.", cmd);
} }
return result; 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); DEBUG_LOG("Executing: %s", cmd);
char buffer[BUFSIZE]; char buffer[BUFSIZE];
@@ -743,8 +749,18 @@ bool execute_cmd_failable(const char *cmd, char **result, const char *stdin_stri
#else #else
if (!(process = popen(cmd, "r"))) return false; if (!(process = popen(cmd, "r"))) return false;
#endif #endif
unsigned len = 0;
while (fgets(buffer, BUFSIZE - 1, process)) 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); output = str_cat(output, buffer);
} }
#if PLATFORM_WINDOWS #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"); file_delete_file("__c3temp.bin");
} }
if (err) return false;
while (output[0] != 0) while (output[0] != 0)
{ {
switch (output[0]) switch (output[0])
@@ -774,7 +788,7 @@ bool execute_cmd_failable(const char *cmd, char **result, const char *stdin_stri
break; break;
} }
*result = str_trim(output); *result = str_trim(output);
return true; return !err;
} }
#if PLATFORM_WINDOWS #if PLATFORM_WINDOWS

View File

@@ -38,7 +38,7 @@ static char *find_visual_studio(void)
char *install_path = NULL; char *install_path = NULL;
// Call vswhere.exe // 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."); error_exit("Failed to find vswhere.exe to detect MSVC.");
} }

View File

@@ -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); 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 *cmalloc(size_t size);
void *ccalloc(size_t size, size_t elements); void *ccalloc(size_t size, size_t elements);
void memory_init(size_t max_mem); void memory_init(size_t max_mem);