ascii-chat 0.11.33
Video chat in your terminal
Loading...
Searching...
No Matches
embedded.c File Reference

Go to the source code of this file.

Functions

int get_manpage_template (FILE **out_file, const char **out_content, size_t *out_len)
 Get man page template source (embedded or filesystem)
 
int get_manpage_content (FILE **out_file, const char **out_content, size_t *out_len)
 Get man page content source (embedded or filesystem)
 
void release_manpage_resources (FILE *file)
 Release resources obtained from get_manpage_*.
 

Function Documentation

◆ get_manpage_content()

int get_manpage_content ( FILE **  out_file,
const char **  out_content,
size_t *  out_len 
)

Get man page content source (embedded or filesystem)

Same behavior as get_manpage_template() but for the content file. See get_manpage_template() documentation for usage details.

Parameters
out_fileOutput pointer for FILE* (development) or NULL (production)
out_contentOutput pointer for const char* (production) or NULL (dev)
out_lenOutput pointer for size_t (production) or NULL (development)
Returns
0 on success, -1 on error

Definition at line 75 of file embedded.c.

75 {
76 // Content is now consolidated into the template - return empty
77 if (out_content) {
78 *out_content = "";
79 }
80 if (out_len) {
81 *out_len = 0;
82 }
83 if (out_file) {
84 *out_file = NULL;
85 }
86
87 log_debug("Man page content is now consolidated into template (empty)");
88 return 0;
89}
#define log_debug(...)
Log a DEBUG message.
Definition log/log.h:548

References log_debug.

◆ get_manpage_template()

int get_manpage_template ( FILE **  out_file,
const char **  out_content,
size_t *  out_len 
)

Get man page template source (embedded or filesystem)

Automatically selects between embedded and filesystem resources based on build type:

  • Release builds (NDEBUG defined): Returns embedded string
  • Debug builds (NDEBUG undefined): Reads from filesystem

Parameter Usage:

  • In production: out_content and out_len are set, out_file is NULL
  • In development: out_file is set to open FILE*, others are NULL
  • Caller must check which one is non-NULL to determine source
Parameters
out_fileOutput pointer for FILE* (development) or NULL (production)
out_contentOutput pointer for const char* (production) or NULL (dev)
out_lenOutput pointer for size_t (production) or NULL (development)
Returns
0 on success, -1 on error
Note
Caller must call release_manpage_resources(out_file) when done
Do NOT free out_content - it's either static or managed elsewhere

Definition at line 22 of file embedded.c.

22 {
23#ifdef NDEBUG
24 // Release build: Return embedded data
25 if (out_content) {
26 *out_content = embedded_manpage_template;
27 }
28 if (out_len) {
30 }
31 if (out_file) {
32 *out_file = NULL;
33 }
34
35 log_debug("Using embedded man page template (%zu bytes)", embedded_manpage_template_size);
36 return 0;
37#else
38 // Debug build: Read from filesystem
39 if (!out_file) {
40 SET_ERRNO(ERROR_INVALID_PARAM, "out_file cannot be NULL in development mode");
41 return -1;
42 }
43
44 // Construct absolute path using ASCIICHAT_RESOURCE_DIR (set at build time)
45#ifdef ASCIICHAT_RESOURCE_DIR
46 static char path_buffer[PATH_MAX];
47 safe_snprintf(path_buffer, sizeof(path_buffer), "%s/share/man/man1/ascii-chat.1.in", ASCIICHAT_RESOURCE_DIR);
48 const char *path = path_buffer;
49#else
50 // Fallback: try relative path if ASCIICHAT_RESOURCE_DIR not defined
51 const char *path = "share/man/man1/ascii-chat.1.in";
52#endif
53
54 // Use binary mode to ensure fread byte count matches ftell file size.
55 // Text mode on Windows translates \r\n to \n, causing size mismatch.
56 FILE *f = platform_fopen("file_stream", path, "rb");
57 if (!f) {
58 SET_ERRNO_SYS(ERROR_CONFIG, "Failed to open man page template: %s", path);
59 return -1;
60 }
61
62 *out_file = f;
63 if (out_content) {
64 *out_content = NULL;
65 }
66 if (out_len) {
67 *out_len = 0;
68 }
69
70 log_debug("Using filesystem man page template: %s", path);
71 return 0;
72#endif
73}
const size_t embedded_manpage_template_size
Size of embedded_manpage_template (excluding null terminator)
const char embedded_manpage_template[]
Behavior based on build type.
#define SET_ERRNO_SYS(code, context_msg,...)
Set error code with custom message and system error context, returning the error code.
#define SET_ERRNO(code, context_msg,...)
Set error code with custom context message and log it, returning the error code.
@ ERROR_CONFIG
Definition error_codes.h:57
@ ERROR_INVALID_PARAM
int safe_snprintf(char *buffer, size_t buffer_size, const char *format,...)
Safe formatted string printing to buffer.
Definition system.c:148
FILE * platform_fopen(const char *name, const char *filename, const char *mode)
Safe file open stream (fopen replacement)

References embedded_manpage_template, embedded_manpage_template_size, ERROR_CONFIG, ERROR_INVALID_PARAM, log_debug, platform_fopen(), safe_snprintf(), SET_ERRNO, and SET_ERRNO_SYS.

Referenced by manpage_resources_load().

◆ release_manpage_resources()

void release_manpage_resources ( FILE *  file)

Release resources obtained from get_manpage_*.

Properly cleans up resources based on build type:

  • Production: No-op (embedded strings are static)
  • Development: Closes FILE* if non-NULL
Parameters
fileFILE* obtained from get_manpage_template/content(), or NULL
Note
Safe to call with NULL
Only closes file, does NOT free content pointer

Definition at line 91 of file embedded.c.

91 {
92#ifdef NDEBUG
93 // Release build: No-op (embedded data is static, not heap-allocated)
94 (void)file; // Suppress unused parameter warning
95#else
96 // Debug build: Close file handle if present
97 if (file) {
98 fclose(file);
99 }
100#endif
101}