Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
76 changes: 76 additions & 0 deletions src/dmlog.c
Original file line number Diff line number Diff line change
Expand Up @@ -1065,4 +1065,80 @@ DMOD_INPUT_API_DECLARATION( Dmod, 1.0, int ,_Printf, ( const char* Format, ...
return written;
}

/**
* @brief Built-in getc function for DMLoG.
*
* Reads a single character from the dmlog input buffer.
* If no input is available, requests input from the host and waits.
*
* @return int Character read from input, or EOF if no default context.
*/
DMOD_INPUT_API_DECLARATION( Dmod, 1.0, int ,_Getc, ( void ) )
{
dmlog_ctx_t ctx = dmlog_get_default();
if(ctx == NULL)
{
return EOF;
}

// Try to read first (in case there's already buffered data)
char c = dmlog_input_getc(ctx);
if(c != '\0')
{
return (unsigned char)c;
}

// No data available - request input and wait
dmlog_input_request(ctx, DMLOG_INPUT_REQUEST_FLAG_DEFAULT);

// Wait for input to be available in the ring buffer
while(!dmlog_input_available(ctx))
{
// Busy wait for input
}

c = dmlog_input_getc(ctx);
return (c == '\0') ? EOF : (unsigned char)c;
}

/**
* @brief Built-in gets function for DMLoG.
*
* Reads a line of input from the dmlog input buffer.
* If no input is available, requests input from the host and waits.
*
* @param Buffer Pointer to buffer where the string will be stored.
* @param Size Maximum number of characters to read (including null terminator).
* @return char* Pointer to buffer on success, NULL on error.
*/
DMOD_INPUT_API_DECLARATION( Dmod, 1.0, char* ,_Gets, ( char* Buffer, int Size ) )
{
dmlog_ctx_t ctx = dmlog_get_default();
if(ctx == NULL || Buffer == NULL || Size <= 0)
{
return NULL;
}

// Try to read first (in case there's already buffered data)
if(dmlog_input_gets(ctx, Buffer, (size_t)Size))
{
return Buffer;
}

// No data available - request input and wait
dmlog_input_request(ctx, DMLOG_INPUT_REQUEST_FLAG_LINE_MODE);

// Wait for input to be available in the ring buffer
while(!dmlog_input_available(ctx))
{
// Busy wait for input
}

if(dmlog_input_gets(ctx, Buffer, (size_t)Size))
{
return Buffer;
}
return NULL;
}

#endif // DMLOG_DONT_IMPLEMENT_DMOD_API
19 changes: 19 additions & 0 deletions tests/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,23 @@ target_include_directories(test_input
${CMAKE_SOURCE_DIR}/include
)

# =====================================================================
# Test: DMOD Input API Test
# =====================================================================
add_executable(test_dmod_input_api test_dmod_input_api.c dmod_test_stubs.c)
target_link_libraries(test_dmod_input_api
PRIVATE
dmlog
dmod_system
dmod_common
dmod_fastlz
dmod_inc
)
target_include_directories(test_dmod_input_api
PRIVATE
${CMAKE_SOURCE_DIR}/include
)

# =====================================================================
# Test: Interactive Test Application
# =====================================================================
Expand All @@ -100,6 +117,7 @@ add_test(NAME dmlog_unit COMMAND test_dmlog_unit)
add_test(NAME simple_test COMMAND test_simple)
add_test(NAME benchmark COMMAND test_benchmark)
add_test(NAME input_test COMMAND test_input)
add_test(NAME dmod_input_api_test COMMAND test_dmod_input_api)

# =====================================================================
# Coverage Support (optional)
Expand All @@ -115,6 +133,7 @@ if(ENABLE_COVERAGE)
target_link_libraries(test_simple PRIVATE gcov)
target_link_libraries(test_benchmark PRIVATE gcov)
target_link_libraries(test_input PRIVATE gcov)
target_link_libraries(test_dmod_input_api PRIVATE gcov)
target_link_libraries(test_app_interactive PRIVATE gcov)

# Add custom target for generating coverage report
Expand Down
Loading