aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorJohn Keeping2013-04-07 14:40:50 +0100
committerJason A. Donenfeld2013-04-08 16:10:11 +0200
commitfd00d2f9d6088223f57006949dc6ce7c36316a79 (patch)
tree46092821a261964a35b36f0e22b924fdd144bf75
parent57d09bf448990b3a67436e928807e854e491756f (diff)
downloadcgit-fd00d2f9d6088223f57006949dc6ce7c36316a79.tar.gz
cgit-fd00d2f9d6088223f57006949dc6ce7c36316a79.tar.bz2
cgit-fd00d2f9d6088223f57006949dc6ce7c36316a79.zip
html.c: add various strbuf and varadic helpers
This adds the fmtalloc helper, html_txtf, html_vtxtf, and html_attrf. These takes a printf style format string like htmlf but escapes the resulting string. The html_vtxtf variant takes a va_list whereas html_txtf is variadic. Signed-off-by: John Keeping <john@keeping.me.uk>
-rw-r--r--cgit.h3
-rw-r--r--html.c53
-rw-r--r--html.h11
3 files changed, 63 insertions, 4 deletions
diff --git a/cgit.h b/cgit.h
index 7581cc1..7619cbb 100644
--- a/cgit.h
+++ b/cgit.h
@@ -327,6 +327,9 @@ extern void cgit_diff_commit(struct commit *commit, filepair_fn fn,
327__attribute__((format (printf,1,2))) 327__attribute__((format (printf,1,2)))
328extern char *fmt(const char *format,...); 328extern char *fmt(const char *format,...);
329 329
330__attribute__((format (printf,1,2)))
331extern char *fmtalloc(const char *format,...);
332
330extern struct commitinfo *cgit_parse_commit(struct commit *commit); 333extern struct commitinfo *cgit_parse_commit(struct commit *commit);
331extern struct taginfo *cgit_parse_tag(struct tag *tag); 334extern struct taginfo *cgit_parse_tag(struct tag *tag);
332extern void cgit_parse_url(const char *url); 335extern void cgit_parse_url(const char *url);
diff --git a/html.c b/html.c
index 8c45ba6..f7772dc 100644
--- a/html.c
+++ b/html.c
@@ -63,6 +63,18 @@ char *fmt(const char *format, ...)
63 return buf[bufidx]; 63 return buf[bufidx];
64} 64}
65 65
66char *fmtalloc(const char *format, ...)
67{
68 struct strbuf sb = STRBUF_INIT;
69 va_list args;
70
71 va_start(args, format);
72 strbuf_vaddf(&sb, format, args);
73 va_end(args);
74
75 return strbuf_detach(&sb, NULL);
76}
77
66void html_raw(const char *data, size_t size) 78void html_raw(const char *data, size_t size)
67{ 79{
68 if (write(htmlfd, data, size) != size) 80 if (write(htmlfd, data, size) != size)
@@ -76,13 +88,35 @@ void html(const char *txt)
76 88
77void htmlf(const char *format, ...) 89void htmlf(const char *format, ...)
78{ 90{
79 static char buf[65536]; 91 va_list args;
92 struct strbuf buf = STRBUF_INIT;
93
94 va_start(args, format);
95 strbuf_vaddf(&buf, format, args);
96 va_end(args);
97 html(buf.buf);
98 strbuf_release(&buf);
99}
100
101void html_txtf(const char *format, ...)
102{
80 va_list args; 103 va_list args;
81 104
82 va_start(args, format); 105 va_start(args, format);
83 vsnprintf(buf, sizeof(buf), format, args); 106 html_vtxtf(format, args);
84 va_end(args); 107 va_end(args);
85 html(buf); 108}
109
110void html_vtxtf(const char *format, va_list ap)
111{
112 va_list cp;
113 struct strbuf buf = STRBUF_INIT;
114
115 va_copy(cp, ap);
116 strbuf_vaddf(&buf, format, cp);
117 va_end(cp);
118 html_txt(buf.buf);
119 strbuf_release(&buf);
86} 120}
87 121
88void html_status(int code, const char *msg, int more_headers) 122void html_status(int code, const char *msg, int more_headers)
@@ -136,6 +170,19 @@ void html_ntxt(int len, const char *txt)
136 html("..."); 170 html("...");
137} 171}
138 172
173void html_attrf(const char *fmt, ...)
174{
175 va_list ap;
176 struct strbuf sb = STRBUF_INIT;
177
178 va_start(ap, fmt);
179 strbuf_vaddf(&sb, fmt, ap);
180 va_end(ap);
181
182 html_attr(sb.buf);
183 strbuf_release(&sb);
184}
185
139void html_attr(const char *txt) 186void html_attr(const char *txt)
140{ 187{
141 const char *t = txt; 188 const char *t = txt;
diff --git a/html.h b/html.h
index bb36f37..be3b311 100644
--- a/html.h
+++ b/html.h
@@ -1,7 +1,7 @@
1#ifndef HTML_H 1#ifndef HTML_H
2#define HTML_H 2#define HTML_H
3 3
4#include <stddef.h> 4#include "cgit.h"
5 5
6extern void html_raw(const char *txt, size_t size); 6extern void html_raw(const char *txt, size_t size);
7extern void html(const char *txt); 7extern void html(const char *txt);
@@ -9,6 +9,15 @@ extern void html(const char *txt);
9__attribute__((format (printf,1,2))) 9__attribute__((format (printf,1,2)))
10extern void htmlf(const char *format,...); 10extern void htmlf(const char *format,...);
11 11
12__attribute__((format (printf,1,2)))
13extern void html_txtf(const char *format,...);
14
15__attribute__((format (printf,1,0)))
16extern void html_vtxtf(const char *format, va_list ap);
17
18__attribute__((format (printf,1,2)))
19extern void html_attrf(const char *format,...);
20
12extern void html_status(int code, const char *msg, int more_headers); 21extern void html_status(int code, const char *msg, int more_headers);
13extern void html_txt(const char *txt); 22extern void html_txt(const char *txt);
14extern void html_ntxt(int len, const char *txt); 23extern void html_ntxt(int len, const char *txt);