ascii-chat 0.11.33
Video chat in your terminal
Loading...
Searching...
No Matches
parser.h File Reference

Man page template parsing and section extraction. More...

Go to the source code of this file.

Functions

asciichat_error_t manpage_parser_parse_file (FILE *f, parsed_section_t **out_sections, size_t *out_count)
 Parse man page sections from a FILE handle.
 
asciichat_error_t manpage_parser_parse_memory (const char *content, size_t content_len, parsed_section_t **out_sections, size_t *out_count)
 Parse man page sections from memory buffer.
 
void manpage_parser_free_sections (parsed_section_t *sections, size_t count)
 Free parsed sections array.
 
const parsed_section_t * manpage_parser_find_section (const parsed_section_t *sections, size_t count, const char *section_name)
 Find section by name.
 

Detailed Description

Man page template parsing and section extraction.

This module handles parsing man page templates to extract sections and metadata about their types (AUTO-generated, MANUAL, or MERGE sections).

Supports both FILE* and memory buffer parsing with automatic detection of platform capabilities.

Definition in file parser.h.

Function Documentation

◆ manpage_parser_find_section()

const parsed_section_t * manpage_parser_find_section ( const parsed_section_t *  sections,
size_t  count,
const char *  section_name 
)

Find section by name.

Searches the parsed sections array for a section with matching name. Comparison is case-sensitive.

Parameters
[in]sectionsArray of parsed sections
[in]countNumber of sections
[in]section_nameSection name to search for (case-sensitive)
Returns
Pointer to matching section, or NULL if not found

Definition at line 400 of file parser.c.

401 {
402 if (!sections || !section_name) {
403 return NULL;
404 }
405
406 for (size_t i = 0; i < count; i++) {
407 if (sections[i].section_name && strcmp(sections[i].section_name, section_name) == 0) {
408 return &sections[i];
409 }
410 }
411
412 return NULL;
413}

Referenced by find_section().

◆ manpage_parser_free_sections()

void manpage_parser_free_sections ( parsed_section_t *  sections,
size_t  count 
)

Free parsed sections array.

Frees all allocated memory for sections array including section names and content. Safe to call with NULL pointers.

Parameters
[in]sectionsArray of parsed sections (can be NULL)
[in]countNumber of sections in array

Definition at line 383 of file parser.c.

383 {
384 if (!sections) {
385 return;
386 }
387
388 for (size_t i = 0; i < count; i++) {
389 if (sections[i].section_name) {
390 SAFE_FREE(sections[i].section_name);
391 }
392 if (sections[i].content) {
393 SAFE_FREE(sections[i].content);
394 }
395 }
396
397 SAFE_FREE(sections);
398}
#define SAFE_FREE(ptr)
Definition common.h:376

References SAFE_FREE.

Referenced by free_parsed_sections().

◆ manpage_parser_parse_file()

asciichat_error_t manpage_parser_parse_file ( FILE *  f,
parsed_section_t **  out_sections,
size_t *  out_count 
)

Parse man page sections from a FILE handle.

Parses a man page template from an open FILE*, detecting:

  • Section headers (.SH directive)
  • Section markers (AUTO-START/END, MANUAL-START/END, MERGE-START/END)
  • Section content and boundaries

Memory is allocated for each section. Caller must use manpage_parser_free_sections() to clean up.

Parameters
[in]fOpen file handle to parse (cannot be NULL)
[out]out_sectionsPointer to array of parsed_section_t (output)
[out]out_countNumber of sections parsed (output)
Returns
ASCIICHAT_OK on success
ERROR_INVALID_PARAM if parameters are invalid
ERROR_CONFIG if parsing fails or memory allocation fails
Note
Caller must call manpage_parser_free_sections(out_sections, out_count)
The file position is not reset; caller may need to fseek if needed

Definition at line 339 of file parser.c.

339 {
340 if (!f || !out_sections || !out_count) {
341 return SET_ERRNO(ERROR_INVALID_PARAM, "Invalid parameters for manpage_parser_parse_file");
342 }
343
344 return parse_sections_internal(f, out_sections, out_count);
345}
#define SET_ERRNO(code, context_msg,...)
Set error code with custom context message and log it, returning the error code.
@ ERROR_INVALID_PARAM

References ERROR_INVALID_PARAM, and SET_ERRNO.

Referenced by parse_manpage_sections().

◆ manpage_parser_parse_memory()

asciichat_error_t manpage_parser_parse_memory ( const char *  content,
size_t  content_len,
parsed_section_t **  out_sections,
size_t *  out_count 
)

Parse man page sections from memory buffer.

Parses man page content from a memory buffer, detecting sections and markers like parse_file() but from pre-loaded content.

Uses platform-specific optimizations:

  • Unix/macOS: fmemopen() for zero-copy parsing (if available)
  • Windows/fallback: Creates temporary file

Memory is allocated for each section. Caller must use manpage_parser_free_sections() to clean up.

Parameters
[in]contentPointer to memory buffer containing man page (cannot be NULL)
[in]content_lenLength of buffer in bytes (must be > 0)
[out]out_sectionsPointer to array of parsed_section_t (output)
[out]out_countNumber of sections parsed (output)
Returns
ASCIICHAT_OK on success
ERROR_INVALID_PARAM if parameters are invalid
ERROR_CONFIG if parsing fails or memory allocation fails
Note
Caller must call manpage_parser_free_sections(out_sections, out_count)
Available in all builds (Debug, Dev, Release)

Definition at line 347 of file parser.c.

348 {
349 if (!content || content_len == 0 || !out_sections || !out_count) {
350 return SET_ERRNO(ERROR_INVALID_PARAM, "Invalid parameters for manpage_parser_parse_memory");
351 }
352
353 // Create temporary file from memory
354 FILE *tmp = platform_tmpfile();
355 if (!tmp) {
356 return SET_ERRNO_SYS(ERROR_CONFIG, "Failed to create temporary file for memory parsing");
357 }
358
359 // Write content to temporary file
360 // Write content_len + 1 to include the null terminator.
361 // The embedded string is content_len + 1 bytes (content_len chars + null terminator).
362 // platform_getline() needs the null terminator to properly handle EOF.
363 size_t bytes_to_write = content_len + 1;
364 size_t written = fwrite(content, 1, bytes_to_write, tmp);
365 if (written != bytes_to_write) {
366 fclose(tmp);
367 return SET_ERRNO_SYS(ERROR_CONFIG, "Failed to write complete content to temporary file");
368 }
369
370 // Rewind to beginning for reading
371 if (fseek(tmp, 0, SEEK_SET) != 0) {
372 fclose(tmp);
373 return SET_ERRNO_SYS(ERROR_CONFIG, "Failed to rewind temporary file");
374 }
375
376 // Parse using common function
377 asciichat_error_t err = parse_sections_internal(tmp, out_sections, out_count);
378 fclose(tmp);
379
380 return err;
381}
#define SET_ERRNO_SYS(code, context_msg,...)
Set error code with custom message and system error context, returning the error code.
asciichat_error_t
Error and exit codes - unified status values (0-255)
Definition error_codes.h:49
@ ERROR_CONFIG
Definition error_codes.h:57
FILE * platform_tmpfile(void)
Create a temporary file (tmpfile replacement)

References ERROR_CONFIG, ERROR_INVALID_PARAM, platform_tmpfile(), SET_ERRNO, and SET_ERRNO_SYS.