Skip to content

Commit

Permalink
Truncate output from execute and print to stdout on error.
Browse files Browse the repository at this point in the history
  • Loading branch information
lerno committed Jan 29, 2025
1 parent ac3b2f0 commit 13771cc
Show file tree
Hide file tree
Showing 7 changed files with 47 additions and 31 deletions.
4 changes: 2 additions & 2 deletions src/build/libraries.c
Original file line number Diff line number Diff line change
Expand Up @@ -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));
}
}
}
32 changes: 17 additions & 15 deletions src/compiler/compiler.c
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down Expand Up @@ -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))
Expand All @@ -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);
Expand All @@ -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);
Expand Down
2 changes: 1 addition & 1 deletion src/compiler/compiler_internal.h
Original file line number Diff line number Diff line change
Expand Up @@ -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);

Expand Down
4 changes: 2 additions & 2 deletions src/compiler/sema_passes.c
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
30 changes: 22 additions & 8 deletions src/utils/file_utils.c
Original file line number Diff line number Diff line change
Expand Up @@ -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
}

Expand All @@ -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)
Expand Down Expand Up @@ -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];
Expand All @@ -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
Expand All @@ -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])
Expand All @@ -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
Expand Down
2 changes: 1 addition & 1 deletion src/utils/find_msvc.c
Original file line number Diff line number Diff line change
Expand Up @@ -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.");
}
Expand Down
4 changes: 2 additions & 2 deletions src/utils/lib.h
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down

0 comments on commit 13771cc

Please sign in to comment.