diff options
| author | 2013-04-07 22:40:50 (JST) | |
|---|---|---|
| committer | 2013-04-08 23:10:11 (JST) | |
| commit | fd00d2f9d6088223f57006949dc6ce7c36316a79 (patch) | |
| tree | 46092821a261964a35b36f0e22b924fdd144bf75 | |
| parent | 57d09bf448990b3a67436e928807e854e491756f (diff) | |
| download | cgit-fd00d2f9d6088223f57006949dc6ce7c36316a79.zip cgit-fd00d2f9d6088223f57006949dc6ce7c36316a79.tar.gz | |
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.h | 3 | ||||
| -rw-r--r-- | html.c | 53 | ||||
| -rw-r--r-- | html.h | 11 |
3 files changed, 63 insertions, 4 deletions
| @@ -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))) |
| 328 | extern char *fmt(const char *format,...); | 328 | extern char *fmt(const char *format,...); |
| 329 | 329 | ||
| 330 | __attribute__((format (printf,1,2))) | ||
| 331 | extern char *fmtalloc(const char *format,...); | ||
| 332 | |||
| 330 | extern struct commitinfo *cgit_parse_commit(struct commit *commit); | 333 | extern struct commitinfo *cgit_parse_commit(struct commit *commit); |
| 331 | extern struct taginfo *cgit_parse_tag(struct tag *tag); | 334 | extern struct taginfo *cgit_parse_tag(struct tag *tag); |
| 332 | extern void cgit_parse_url(const char *url); | 335 | extern void cgit_parse_url(const char *url); |
| @@ -63,6 +63,18 @@ char *fmt(const char *format, ...) | |||
| 63 | return buf[bufidx]; | 63 | return buf[bufidx]; |
| 64 | } | 64 | } |
| 65 | 65 | ||
| 66 | char *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 | |||
| 66 | void html_raw(const char *data, size_t size) | 78 | void 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 | ||
| 77 | void htmlf(const char *format, ...) | 89 | void 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 | |||
| 101 | void 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 | |||
| 110 | void 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 | ||
| 88 | void html_status(int code, const char *msg, int more_headers) | 122 | void 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 | ||
| 173 | void 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 | |||
| 139 | void html_attr(const char *txt) | 186 | void html_attr(const char *txt) |
| 140 | { | 187 | { |
| 141 | const char *t = txt; | 188 | const char *t = txt; |
| @@ -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 | ||
| 6 | extern void html_raw(const char *txt, size_t size); | 6 | extern void html_raw(const char *txt, size_t size); |
| 7 | extern void html(const char *txt); | 7 | extern 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))) |
| 10 | extern void htmlf(const char *format,...); | 10 | extern void htmlf(const char *format,...); |
| 11 | 11 | ||
| 12 | __attribute__((format (printf,1,2))) | ||
| 13 | extern void html_txtf(const char *format,...); | ||
| 14 | |||
| 15 | __attribute__((format (printf,1,0))) | ||
| 16 | extern void html_vtxtf(const char *format, va_list ap); | ||
| 17 | |||
| 18 | __attribute__((format (printf,1,2))) | ||
| 19 | extern void html_attrf(const char *format,...); | ||
| 20 | |||
| 12 | extern void html_status(int code, const char *msg, int more_headers); | 21 | extern void html_status(int code, const char *msg, int more_headers); |
| 13 | extern void html_txt(const char *txt); | 22 | extern void html_txt(const char *txt); |
| 14 | extern void html_ntxt(int len, const char *txt); | 23 | extern void html_ntxt(int len, const char *txt); |
