ascii-chat
Binary v0.15.83-53-gb711d8720
Library lib/v0.3.2-701-gb711d8720
Video chat in your terminal
Loading...
Searching...
No Matches
formatter.h File Reference

Groff/troff formatting utilities for man page generation. More...

Go to the source code of this file.

Functions

const char * manpage_fmt_escape_groff (const char *str)
 Escape special characters for groff output.
void manpage_fmt_write_section (FILE *f, const char *section_name)
 Write a section header directive.
void manpage_fmt_write_blank_line (FILE *f)
 Write a blank line (for spacing between sections).
void manpage_fmt_write_bold (FILE *f, const char *text)
 Write text in bold format.
void manpage_fmt_write_italic (FILE *f, const char *text)
 Write text in italic format.
void manpage_fmt_write_tagged_paragraph (FILE *f)
 Write a tagged paragraph header.
void manpage_fmt_write_text (FILE *f, const char *text)
 Write plain text line (without directive).
void manpage_fmt_write_title (FILE *f, const char *program_name, const char *mode_name, const char *brief_description)
 Write groff title/header (.TH directive).

Detailed Description

Groff/troff formatting utilities for man page generation.

This module provides utilities for generating properly formatted groff/troff output for man pages. Handles:

  • Section headers (.SH directive)
  • Text formatting (bold .B, italic .I, constant .C)
  • Paragraph and item formatting (.TP for tagged paragraphs)
  • Special character escaping
  • Section markers (AUTO/MANUAL/MERGE)

All functions write directly to FILE* for efficient streaming output.

Definition in file formatter.h.

Function Documentation

◆ manpage_fmt_escape_groff()

const char * manpage_fmt_escape_groff ( const char * str)

Escape special characters for groff output.

Escapes characters that have special meaning in groff format. Currently returns the string as-is since most man page content doesn't contain problematic characters. Can be extended for more robust escaping if needed.

Parameters
[in]strString to escape (can be NULL)
Returns
Escaped string (never NULL, returns "" for NULL input)
Note
This is a simple implementation. For production use with arbitrary content, consider a more robust escaping strategy.

Definition at line 25 of file formatter.c.

25 {
26 // For simplicity, we'll just return the string as-is
27 // In a more robust implementation, we'd escape special characters
28 // But for man page content, most strings don't have problematic characters
29 return str ? str : "";
30}

Referenced by manpage_fmt_write_title().

◆ manpage_fmt_write_blank_line()

void manpage_fmt_write_blank_line ( FILE * f)

Write a blank line (for spacing between sections).

Parameters
[in]fOutput file handle (cannot be NULL)

Definition at line 39 of file formatter.c.

39 {
40 if (!f) {
41 return;
42 }
43 fprintf(f, "\n");
44}

Referenced by options_config_generate_manpage_template().

◆ manpage_fmt_write_bold()

void manpage_fmt_write_bold ( FILE * f,
const char * text )

Write text in bold format.

Writes text with bold formatting directive. Example: manpage_fmt_write_bold(f, "ascii-chat") writes ".B ascii-chat\n"

Parameters
[in]fOutput file handle (cannot be NULL)
[in]textText to write in bold (cannot be NULL)

Definition at line 46 of file formatter.c.

46 {
47 if (!f || !text) {
48 return;
49 }
50 fprintf(f, ".B %s\n", text);
51}

◆ manpage_fmt_write_italic()

void manpage_fmt_write_italic ( FILE * f,
const char * text )

Write text in italic format.

Writes text with italic formatting directive. Example: manpage_fmt_write_italic(f, "options") writes ".I options\n"

Parameters
[in]fOutput file handle (cannot be NULL)
[in]textText to write in italic (cannot be NULL)

Definition at line 53 of file formatter.c.

53 {
54 if (!f || !text) {
55 return;
56 }
57 fprintf(f, ".I %s\n", text);
58}

◆ manpage_fmt_write_section()

void manpage_fmt_write_section ( FILE * f,
const char * section_name )

Write a section header directive.

Writes ".SH SECTION_NAME" directive to output. Example: manpage_fmt_write_section(f, "OPTIONS") writes ".SH OPTIONS\n"

Parameters
[in]fOutput file handle (cannot be NULL)
[in]section_nameSection name to write (cannot be NULL)

Definition at line 32 of file formatter.c.

32 {
33 if (!f || !section_name) {
34 return;
35 }
36 fprintf(f, ".SH %s\n", section_name);
37}

Referenced by options_config_generate_manpage_template().

◆ manpage_fmt_write_tagged_paragraph()

void manpage_fmt_write_tagged_paragraph ( FILE * f)

Write a tagged paragraph header.

Writes ".TP" directive to start a tagged paragraph (for option descriptions). Should be followed by manpage_fmt_write_bold() for the tag and then regular text for the description.

Parameters
[in]fOutput file handle (cannot be NULL)

Definition at line 60 of file formatter.c.

60 {
61 if (!f) {
62 return;
63 }
64 fprintf(f, ".TP\n");
65}

◆ manpage_fmt_write_text()

void manpage_fmt_write_text ( FILE * f,
const char * text )

Write plain text line (without directive).

Writes text directly without any formatting directive. Useful for description text and content lines.

Parameters
[in]fOutput file handle (cannot be NULL)
[in]textText to write (can be NULL, in which case only newline written)

Definition at line 67 of file formatter.c.

67 {
68 if (!f) {
69 return;
70 }
71
72 if (text) {
73 fprintf(f, "%s\n", text);
74 } else {
75 fprintf(f, "\n");
76 }
77}

◆ manpage_fmt_write_title()

void manpage_fmt_write_title ( FILE * f,
const char * program_name,
const char * mode_name,
const char * brief_description )

Write groff title/header (.TH directive).

Writes the full title header for a man page with current date. Format: .TH NAME SECTION DATE SOURCE MANUAL

Parameters
[in]fOutput file handle (cannot be NULL)
[in]program_nameProgram name (e.g., "ascii-chat") (cannot be NULL)
[in]mode_nameMode name or NULL (e.g., "server", "client", or NULL for binary-level)
[in]brief_descriptionOne-line description (cannot be NULL)

Definition at line 79 of file formatter.c.

79 {
80 if (!f || !program_name || !brief_description) {
81 return;
82 }
83
84 time_t now = time(NULL);
85 struct tm tm_buf;
86 platform_localtime(&now, &tm_buf);
88 strftime(date_str, sizeof(date_str), "%B %Y", &tm_buf);
89
90 // Build full program name (e.g., "ascii-chat-server" or just "ascii-chat")
92 if (mode_name) {
93 safe_snprintf(full_name, sizeof(full_name), "%s-%s", program_name, mode_name);
94 } else {
95 safe_snprintf(full_name, sizeof(full_name), "%s", program_name);
96 }
97
98 // .TH NAME SECTION DATE SOURCE MANUAL
99 // Section 1 = user commands, 5 = file formats
100 fprintf(f, ".TH %s 1 \"%s\" \"%s\" \"User Commands\"\n", full_name, date_str, program_name);
101 fprintf(f, ".SH NAME\n");
102 fprintf(f, ".B %s\n", full_name);
103 fprintf(f, "\\- %s\n", manpage_fmt_escape_groff(brief_description));
104 fprintf(f, "\n");
105}
const char * manpage_fmt_escape_groff(const char *str)
Escape special characters for groff output.
Definition formatter.c:25
#define OPTIONS_MANPAGE_FORMATTER_DATE_BUFFER_SIZE
Definition formatter.c:11
#define OPTIONS_MANPAGE_FORMATTER_FULL_NAME_BUFFER_SIZE
Definition formatter.c:12
int safe_snprintf(char *buffer, size_t buffer_size, const char *format,...)
Platform-safe snprintf wrapper.
asciichat_error_t platform_localtime(const time_t *timer, struct tm *result)
Platform-safe localtime wrapper.

References manpage_fmt_escape_groff(), OPTIONS_MANPAGE_FORMATTER_DATE_BUFFER_SIZE, OPTIONS_MANPAGE_FORMATTER_FULL_NAME_BUFFER_SIZE, platform_localtime(), and safe_snprintf().

Referenced by options_config_generate_manpage_template().