2 b2b17923 2022-12-17 op * Copyright (c) 2022 Omar Polo <op@openbsd.org>
4 2c02675e 2022-12-14 op * Permission to use, copy, modify, and distribute this software for any
5 2c02675e 2022-12-14 op * purpose with or without fee is hereby granted, provided that the above
6 2c02675e 2022-12-14 op * copyright notice and this permission notice appear in all copies.
8 2c02675e 2022-12-14 op * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
9 2c02675e 2022-12-14 op * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
10 2c02675e 2022-12-14 op * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
11 2c02675e 2022-12-14 op * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
12 2c02675e 2022-12-14 op * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
13 2c02675e 2022-12-14 op * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
14 2c02675e 2022-12-14 op * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
17 2c02675e 2022-12-14 op #include <ctype.h>
18 62eab86e 2023-09-13 op #include <stdarg.h>
19 2c02675e 2022-12-14 op #include <stdio.h>
20 2c02675e 2022-12-14 op #include <stdlib.h>
21 62eab86e 2023-09-13 op #include <string.h>
23 2c02675e 2022-12-14 op #include "tmpl.h"
26 62eab86e 2023-09-13 op tp_write(struct template *tp, const char *str, size_t len)
30 62eab86e 2023-09-13 op while (len > 0) {
31 62eab86e 2023-09-13 op avail = tp->tp_cap - tp->tp_len;
32 62eab86e 2023-09-13 op if (avail == 0) {
33 62eab86e 2023-09-13 op if (template_flush(tp) == -1)
35 62eab86e 2023-09-13 op avail = tp->tp_cap;
38 62eab86e 2023-09-13 op if (len < avail)
41 62eab86e 2023-09-13 op memcpy(tp->tp_buf + tp->tp_len, str, avail);
42 62eab86e 2023-09-13 op tp->tp_len += avail;
51 62eab86e 2023-09-13 op tp_writes(struct template *tp, const char *str)
53 62eab86e 2023-09-13 op return (tp_write(tp, str, strlen(str)));
57 62eab86e 2023-09-13 op tp_writef(struct template *tp, const char *fmt, ...)
63 62eab86e 2023-09-13 op va_start(ap, fmt);
64 62eab86e 2023-09-13 op r = vasprintf(&str, fmt, ap);
68 62eab86e 2023-09-13 op r = tp_write(tp, str, r);
74 2c02675e 2022-12-14 op tp_urlescape(struct template *tp, const char *str)
79 2c02675e 2022-12-14 op if (str == NULL)
82 2c02675e 2022-12-14 op for (; *str; ++str) {
83 2c02675e 2022-12-14 op if (iscntrl((unsigned char)*str) ||
84 2c02675e 2022-12-14 op isspace((unsigned char)*str) ||
85 2c02675e 2022-12-14 op *str == '\'' || *str == '"' || *str == '\\') {
86 2c02675e 2022-12-14 op r = snprintf(tmp, sizeof(tmp), "%%%2X", *str);
87 2c02675e 2022-12-14 op if (r < 0 || (size_t)r >= sizeof(tmp))
89 62eab86e 2023-09-13 op if (tp_write(tp, tmp, r) == -1)
92 62eab86e 2023-09-13 op if (tp_write(tp, str, 1) == -1)
100 ac67fee4 2023-12-08 op static inline int
101 ac67fee4 2023-12-08 op htmlescape(struct template *tp, char c)
105 ac67fee4 2023-12-08 op return tp_write(tp, "<", 4);
107 ac67fee4 2023-12-08 op return tp_write(tp, ">", 4);
109 ac67fee4 2023-12-08 op return tp_write(tp, "&", 5);
111 ac67fee4 2023-12-08 op return tp_write(tp, """, 6);
113 ac67fee4 2023-12-08 op return tp_write(tp, "'", 6);
115 ac67fee4 2023-12-08 op return tp_write(tp, &c, 1);
120 2c02675e 2022-12-14 op tp_htmlescape(struct template *tp, const char *str)
122 2c02675e 2022-12-14 op if (str == NULL)
125 2c02675e 2022-12-14 op for (; *str; ++str) {
126 ac67fee4 2023-12-08 op if (htmlescape(tp, *str) == -1)
134 ac67fee4 2023-12-08 op tp_write_htmlescape(struct template *tp, const char *str, size_t len)
138 ac67fee4 2023-12-08 op for (i = 0; i < len; ++i) {
139 ac67fee4 2023-12-08 op if (htmlescape(tp, str[i]) == -1)
146 2c02675e 2022-12-14 op struct template *
147 62eab86e 2023-09-13 op template(void *arg, tmpl_write writefn, char *buf, size_t siz)
149 2c02675e 2022-12-14 op struct template *tp;
151 2c02675e 2022-12-14 op if ((tp = calloc(1, sizeof(*tp))) == NULL)
152 2c02675e 2022-12-14 op return (NULL);
154 2c02675e 2022-12-14 op tp->tp_arg = arg;
155 62eab86e 2023-09-13 op tp->tp_write = writefn;
156 62eab86e 2023-09-13 op tp->tp_buf = buf;
157 62eab86e 2023-09-13 op tp->tp_cap = siz;
163 62eab86e 2023-09-13 op template_flush(struct template *tp)
165 62eab86e 2023-09-13 op if (tp->tp_len == 0)
168 62eab86e 2023-09-13 op if (tp->tp_write(tp->tp_arg, tp->tp_buf, tp->tp_len) == -1)
170 62eab86e 2023-09-13 op tp->tp_len = 0;
175 2c02675e 2022-12-14 op template_free(struct template *tp)
177 2c02675e 2022-12-14 op free(tp->tp_tmp);