ascii-chat
Binary v0.16.50-198-g78f51665f
Library lib/v0.3.2-2240-g78f51665f
Video chat in your terminal
Loading...
Searching...
No Matches
parser.c File Reference

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

Go to the source code of this file.

Macros

#define MANPAGE_INITIAL_PARSE_CAPACITY   16

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 implementation.

Parses man page templates to extract sections and detect their types (AUTO-generated, MANUAL, or MERGE sections).

Definition in file parser.c.

Macro Definition Documentation

◆ MANPAGE_INITIAL_PARSE_CAPACITY

#define MANPAGE_INITIAL_PARSE_CAPACITY   16

Definition at line 11 of file parser.c.

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

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

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

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

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.

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

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