ascii-chat 0.8.38
Real-time terminal-based video chat with ASCII art conversion
Loading...
Searching...
No Matches
abstraction.c File Reference

🏗️ Common platform abstraction stubs (OS-specific code in posix/ and windows/ subdirectories) More...

Go to the source code of this file.

Functions

size_t platform_write_all (int fd, const void *buf, size_t count)
 Write all data to file descriptor with automatic retry on transient errors.
 
bool terminal_should_use_control_sequences (int fd)
 Check if terminal control sequences should be used for the given fd.
 

Detailed Description

🏗️ Common platform abstraction stubs (OS-specific code in posix/ and windows/ subdirectories)

Definition in file abstraction.c.

Function Documentation

◆ platform_write_all()

size_t platform_write_all ( int  fd,
const void *  buf,
size_t  count 
)

Write all data to file descriptor with automatic retry on transient errors.

Handles incomplete writes and transient errors (EAGAIN, EWOULDBLOCK) by retrying. This ensures complete writes even under high load or when piping to tools like tee.

Parameters
fdFile descriptor to write to
bufBuffer containing data to write
countNumber of bytes to write
Returns
Number of bytes written (should equal count if all data written), or -1 if failed

Definition at line 39 of file abstraction.c.

39 {
40 if (!buf || count == 0) {
41 return 0;
42 }
43
44 size_t written_total = 0;
45 int attempts = 0;
46 const int MAX_ATTEMPTS = 1000;
47
48 while (written_total < count && attempts < MAX_ATTEMPTS) {
49 ssize_t result = platform_write(fd, (const char *)buf + written_total, count - written_total);
50
51 if (result > 0) {
52 written_total += (size_t)result;
53 attempts = 0; // Reset attempt counter on successful write
54 } else if (result < 0) {
55 // Handle EAGAIN (non-blocking would-block) with sleep instead of tight loop
56 if (errno == EAGAIN || errno == EWOULDBLOCK) {
57 // Sleep 100us before retrying to avoid busy-waiting and spinning CPU
59 } else {
60 // Other write errors - log and retry
61 log_warn("platform_write_all: write() error on fd=%d (wrote %zu/%zu so far, errno=%d)", fd, written_total,
62 count, errno);
63 }
64 attempts++;
65 } else {
66 // result == 0: no bytes written, retry
67 attempts++;
68 }
69 }
70
71 if (attempts >= MAX_ATTEMPTS && written_total < count) {
72 log_warn("platform_write_all: Hit retry limit on fd=%d: wrote %zu of %zu bytes", fd, written_total, count);
73 }
74
75 return written_total;
76}
void platform_sleep_us(unsigned int us)
ssize_t platform_write(int fd, const void *buf, size_t count)

References platform_sleep_us(), and platform_write().

Referenced by ascii_write(), config_create_default(), interactive_grep_render_input_line(), log_console_impl(), log_json_async_safe(), session_client_like_run(), session_display_render_frame(), session_display_write_raw(), and terminal_screen_render().

◆ terminal_should_use_control_sequences()

bool terminal_should_use_control_sequences ( int  fd)

Check if terminal control sequences should be used for the given fd.

Parameters
fdFile descriptor to check
Returns
true if terminal control sequences should be used, false otherwise

This function determines whether to send terminal POSITIONING and CONTROL sequences (cursor home, clear screen, hide cursor, etc.) which should only be sent to TTY and never to pipes/redirected output.

Note: This does NOT control ANSI COLOR CODES, which are controlled by –color-mode option and may be sent to pipes if explicitly requested.

Definition at line 90 of file abstraction.c.

90 {
91 if (fd < 0) {
92 return false;
93 }
94 if (GET_OPTION(snapshot_mode)) {
95 return false;
96 }
97 const char *testing_env = SAFE_GETENV("TESTING");
98 if (testing_env != NULL) {
99 return false;
100 }
101 return platform_isatty(fd) != 0;
102}
int platform_isatty(int fd)
Definition util.c:61

References platform_isatty().

Referenced by ascii_write(), ascii_write_destroy(), and ascii_write_init().