]> Pileus Git - ~andy/ct/blob - lib.c
Update knots
[~andy/ct] / lib.c
1 #define _GNU_SOURCE
2 #include <stdio.h>
3 #include <stdlib.h>
4 #include <printf.h>
5
6 static int printf_markup(FILE *stream,
7                 const struct printf_info *info,
8                 const void *const *args)
9 {
10         int len = 0;
11         const char *str = *(const char **)args[0];
12         for (int i = 0; str[i]; i++)
13                 switch (str[i]) {
14                 case '"':  len += fputs("&#39;",  stream); break;
15                 case '\'': len += fputs("&quot;", stream); break;
16                 case '&':  len += fputs("&amp;",  stream); break;
17                 case '<':  len += fputs("&lt;",   stream); break;
18                 case '>':  len += fputs("&gt;",   stream); break;
19                 default:   len += fputc(str[i],   stream); break;
20                 }
21         return len;
22 }
23
24 static int printf_markup_arginfo(const struct printf_info *info,
25                 size_t n, int *argtypes, int *size)
26 {
27         argtypes[0] = PA_STRING;
28         return 1;
29 }
30
31 void ct_use_escape()
32 {
33         register_printf_specifier('M', printf_markup, printf_markup_arginfo);
34 }
35
36 int main(void)
37 {
38         ct_use_escape();
39         printf("%M\n", "<Hello, World>");
40         printf("%M\n", "<Hello, World>");
41         printf("%M\n", "<Hello, World>");
42         return 0;
43 }