summaryrefslogtreecommitdiff
path: root/gui/fmt-pango.c
blob: 6305f82e80da21e033d4af5266df0e078ba6a4f8 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
/*
 * gui/fmt-pango.c - Format strings for Pango markup
 *
 * Written 2016 by Werner Almesberger
 * Copyright 2016 by Werner Almesberger
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2 of the License, or
 * (at your option) any later version.
 */


#include <stdarg.h>
#include <stdlib.h>
#include <stdio.h>
#include <ctype.h>
#include <alloca.h>

#include "misc/util.h"
#include "misc/diag.h"
#include "gui/fmt-pango.h"


unsigned vsfmt_pango(char *buf, const char *fmt, va_list ap)
{
	char *res;
	const char *p, *q, *s, *t;
	char *u;
	char *tmp_fmt;
	char *tmp, *tmp2;
	int len;
	unsigned extra;

	res = buf;
	for (p = fmt; *p; p++) {
		if (*p != '%') {
			if (buf)
				*res = *p;
			res++;
			continue;
		}
		for (q = p + 1; isdigit(*q) || *q == '.' || *q == '-'; q++);
		tmp_fmt = alloca(q - p + 1 + 1);
		memcpy(tmp_fmt, p, q - p + 1);
		tmp_fmt[q - p + 1] = 0;
		switch (*q) {
		case 's':
			s = va_arg(ap, const char *);
			len = alloc_printf(&tmp, tmp_fmt, s);

			extra = 0;
			for (t = tmp; *t; t++)
				switch (*t) {
				case '<':
				case '>':
					extra += 3;
					break;
				case '&':
					extra += 4;
					break;
				default:
					break;
				}

			if (extra) {
				tmp2 = u = alloca(len + extra + 1);
				for (t = tmp; *t; t++) {
					switch (*t) {
					case '<':
						strcpy(u, "&lt;");
						u += 4;
						break;
					case '>':
						strcpy(u, "&gt;");
						u += 4;
						break;
					case '&':
						strcpy(u, "&amp;");
						u += 5;
						break;
					default:
						*u++ = *t;
						break;
					}
				}
				*u = 0;
				tmp = tmp2;
			}

			if (buf)
				memcpy(res, tmp, len + extra);
			res += len + extra;
			break;
		case 'c':
			/* @@@ we don't filter markup meta-characters */
		case 'd':
		case 'x':
			len = alloc_printf(&tmp, tmp_fmt, va_arg(ap, int));
			if (buf)
				memcpy(res, tmp, len);
			res += len;
			break;
		case 'u':
			len = alloc_printf(&tmp, tmp_fmt, va_arg(ap, unsigned));
			if (buf)
				memcpy(res, tmp, len);
			res += len;
			break;
		case '%':
			if (buf)
				*res = '%';
			res++;
			break;
		default:
			fatal("unrecognized format '%%%c'\n", *q);
		}
		p = q;
	}
	if (buf)
		*res = 0;
	return res - buf;
}


char *vfmt_pango(const char *fmt, va_list ap)
{
	va_list aq;
	unsigned len;
	char *buf;

	va_copy(aq, ap);
	len = vsfmt_pango(NULL, fmt, ap);
	buf = alloc_size(len + 1);
	vsfmt_pango(buf, fmt, aq);
	va_end(aq);
	return buf;
}


char *fmt_pango(const char *fmt, ...)
{
	va_list ap;
	char *buf;

	va_start(ap, fmt);
	buf = vfmt_pango(fmt, ap);
	va_end(ap);
	return buf;
}