ascii-chat
Binary v0.15.83-53-gb711d8720
Library lib/v0.3.2-701-gb711d8720
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 405 of file parser.c.

406 {
407 if (!sections || !section_name) {
408 return NULL;
409 }
410
411 for (size_t i = 0; i < count; i++) {
412 if (sections[i].section_name && strcmp(sections[i].section_name, section_name) == 0) {
413 return &sections[i];
414 }
415 }
416
417 return NULL;
418}

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 388 of file parser.c.

388 {
389 if (!sections) {
390 return;
391 }
392
393 for (size_t i = 0; i < count; i++) {
394 if (sections[i].section_name) {
395 SAFE_FREE(sections[i].section_name);
396 }
397 if (sections[i].content) {
398 SAFE_FREE(sections[i].content);
399 }
400 }
401
402 SAFE_FREE(sections);
403}
#define SAFE_FREE(ptr)
Definition common.h:377

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 345 of file parser.c.

345 {
346 if (!f || !out_sections || !out_count) {
347 return SET_ERRNO(ERROR_INVALID_PARAM, "Invalid parameters");
348 }
349
350 return parse_sections_internal(f, out_sections, out_count);
351}
#define SET_ERRNO(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 353 of file parser.c.

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

References ERROR_CONFIG, ERROR_INVALID_PARAM, SET_ERRNO, and SET_ERRNO_SYS.