2 52afe358 2022-12-17 op * Copyright (c) 2022 Omar Polo <op@openbsd.org>
4 bff58270 2022-09-19 op * Permission to use, copy, modify, and distribute this software for any
5 bff58270 2022-09-19 op * purpose with or without fee is hereby granted, provided that the above
6 bff58270 2022-09-19 op * copyright notice and this permission notice appear in all copies.
8 bff58270 2022-09-19 op * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
9 bff58270 2022-09-19 op * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
10 bff58270 2022-09-19 op * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
11 bff58270 2022-09-19 op * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
12 bff58270 2022-09-19 op * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
13 bff58270 2022-09-19 op * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
14 bff58270 2022-09-19 op * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
17 bff58270 2022-09-19 op #include <ctype.h>
18 18906bcc 2023-09-13 op #include <stdarg.h>
19 bff58270 2022-09-19 op #include <stdio.h>
20 bff58270 2022-09-19 op #include <stdlib.h>
21 18906bcc 2023-09-13 op #include <string.h>
23 bff58270 2022-09-19 op #include "tmpl.h"
26 18906bcc 2023-09-13 op tp_write(struct template *tp, const char *str, size_t len)
30 18906bcc 2023-09-13 op while (len > 0) {
31 18906bcc 2023-09-13 op avail = tp->tp_cap - tp->tp_len;
32 18906bcc 2023-09-13 op if (avail == 0) {
33 18906bcc 2023-09-13 op if (template_flush(tp) == -1)
35 18906bcc 2023-09-13 op avail = tp->tp_cap;
38 18906bcc 2023-09-13 op if (len < avail)
41 18906bcc 2023-09-13 op memcpy(tp->tp_buf + tp->tp_len, str, avail);
42 18906bcc 2023-09-13 op tp->tp_len += avail;
51 18906bcc 2023-09-13 op tp_writes(struct template *tp, const char *str)
53 18906bcc 2023-09-13 op return (tp_write(tp, str, strlen(str)));
57 18906bcc 2023-09-13 op tp_writef(struct template *tp, const char *fmt, ...)
63 18906bcc 2023-09-13 op va_start(ap, fmt);
64 18906bcc 2023-09-13 op r = vasprintf(&str, fmt, ap);
68 18906bcc 2023-09-13 op r = tp_write(tp, str, r);
74 bff58270 2022-09-19 op tp_urlescape(struct template *tp, const char *str)
79 20c8292e 2022-12-04 op if (str == NULL)
82 bff58270 2022-09-19 op for (; *str; ++str) {
83 bff58270 2022-09-19 op if (iscntrl((unsigned char)*str) ||
84 bff58270 2022-09-19 op isspace((unsigned char)*str) ||
85 bff58270 2022-09-19 op *str == '\'' || *str == '"' || *str == '\\') {
86 bff58270 2022-09-19 op r = snprintf(tmp, sizeof(tmp), "%%%2X", *str);
87 95448b62 2022-09-29 op if (r < 0 || (size_t)r >= sizeof(tmp))
89 18906bcc 2023-09-13 op if (tp_write(tp, tmp, r) == -1)
92 18906bcc 2023-09-13 op if (tp_write(tp, str, 1) == -1)
101 bff58270 2022-09-19 op tp_htmlescape(struct template *tp, const char *str)
105 20c8292e 2022-12-04 op if (str == NULL)
108 bff58270 2022-09-19 op for (; *str; ++str) {
109 bff58270 2022-09-19 op switch (*str) {
111 18906bcc 2023-09-13 op r = tp_write(tp, "<", 4);
114 18906bcc 2023-09-13 op r = tp_write(tp, ">", 4);
117 18906bcc 2023-09-13 op r = tp_write(tp, "&", 5);
120 18906bcc 2023-09-13 op r = tp_write(tp, """, 6);
123 18906bcc 2023-09-13 op r = tp_write(tp, "'", 6);
126 18906bcc 2023-09-13 op r = tp_write(tp, str, 1);
137 bff58270 2022-09-19 op struct template *
138 18906bcc 2023-09-13 op template(void *arg, tmpl_write writefn, char *buf, size_t siz)
140 bff58270 2022-09-19 op struct template *tp;
142 bff58270 2022-09-19 op if ((tp = calloc(1, sizeof(*tp))) == NULL)
143 bff58270 2022-09-19 op return (NULL);
145 bff58270 2022-09-19 op tp->tp_arg = arg;
146 18906bcc 2023-09-13 op tp->tp_write = writefn;
147 18906bcc 2023-09-13 op tp->tp_buf = buf;
148 18906bcc 2023-09-13 op tp->tp_cap = siz;
154 18906bcc 2023-09-13 op template_flush(struct template *tp)
156 18906bcc 2023-09-13 op if (tp->tp_len == 0)
159 18906bcc 2023-09-13 op if (tp->tp_write(tp->tp_arg, tp->tp_buf, tp->tp_len) == -1)
161 18906bcc 2023-09-13 op tp->tp_len = 0;
166 9b929b3a 2022-09-29 op template_free(struct template *tp)
168 9b929b3a 2022-09-29 op free(tp->tp_tmp);