Blame


1 bff58270 2022-09-19 op /*
2 bff58270 2022-09-19 op * Copyright (c) 2022 Omar Polo <op@omarpolo.com>
3 bff58270 2022-09-19 op *
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.
7 bff58270 2022-09-19 op *
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.
15 bff58270 2022-09-19 op */
16 bff58270 2022-09-19 op
17 bff58270 2022-09-19 op #include <ctype.h>
18 bff58270 2022-09-19 op #include <stdio.h>
19 bff58270 2022-09-19 op #include <stdlib.h>
20 bff58270 2022-09-19 op
21 bff58270 2022-09-19 op #include "tmpl.h"
22 bff58270 2022-09-19 op
23 bff58270 2022-09-19 op int
24 bff58270 2022-09-19 op tp_urlescape(struct template *tp, const char *str)
25 bff58270 2022-09-19 op {
26 bff58270 2022-09-19 op int r;
27 bff58270 2022-09-19 op char tmp[4];
28 bff58270 2022-09-19 op
29 bff58270 2022-09-19 op for (; *str; ++str) {
30 bff58270 2022-09-19 op if (iscntrl((unsigned char)*str) ||
31 bff58270 2022-09-19 op isspace((unsigned char)*str) ||
32 bff58270 2022-09-19 op *str == '\'' || *str == '"' || *str == '\\') {
33 bff58270 2022-09-19 op r = snprintf(tmp, sizeof(tmp), "%%%2X", *str);
34 95448b62 2022-09-29 op if (r < 0 || (size_t)r >= sizeof(tmp))
35 95448b62 2022-09-29 op return (0);
36 bff58270 2022-09-19 op if (tp->tp_puts(tp, tmp) == -1)
37 bff58270 2022-09-19 op return (-1);
38 bff58270 2022-09-19 op } else {
39 bff58270 2022-09-19 op if (tp->tp_putc(tp, *str) == -1)
40 bff58270 2022-09-19 op return (-1);
41 bff58270 2022-09-19 op }
42 bff58270 2022-09-19 op }
43 bff58270 2022-09-19 op
44 bff58270 2022-09-19 op return (0);
45 bff58270 2022-09-19 op }
46 bff58270 2022-09-19 op
47 bff58270 2022-09-19 op int
48 bff58270 2022-09-19 op tp_htmlescape(struct template *tp, const char *str)
49 bff58270 2022-09-19 op {
50 bff58270 2022-09-19 op int r;
51 bff58270 2022-09-19 op
52 bff58270 2022-09-19 op for (; *str; ++str) {
53 bff58270 2022-09-19 op switch (*str) {
54 bff58270 2022-09-19 op case '<':
55 bff58270 2022-09-19 op r = tp->tp_puts(tp, "&lt;");
56 bff58270 2022-09-19 op break;
57 bff58270 2022-09-19 op case '>':
58 bff58270 2022-09-19 op r = tp->tp_puts(tp, "&gt;");
59 bff58270 2022-09-19 op break;
60 bff58270 2022-09-19 op case '&':
61 bff58270 2022-09-19 op r = tp->tp_puts(tp, "&amp;");
62 bff58270 2022-09-19 op break;
63 bff58270 2022-09-19 op case '"':
64 bff58270 2022-09-19 op r = tp->tp_puts(tp, "&quot;");
65 bff58270 2022-09-19 op break;
66 bff58270 2022-09-19 op case '\'':
67 bff58270 2022-09-19 op r = tp->tp_puts(tp, "&apos;");
68 bff58270 2022-09-19 op break;
69 bff58270 2022-09-19 op default:
70 bff58270 2022-09-19 op r = tp->tp_putc(tp, *str);
71 bff58270 2022-09-19 op break;
72 bff58270 2022-09-19 op }
73 bff58270 2022-09-19 op
74 95448b62 2022-09-29 op if (r == -1)
75 bff58270 2022-09-19 op return (-1);
76 bff58270 2022-09-19 op }
77 bff58270 2022-09-19 op
78 bff58270 2022-09-19 op return (0);
79 bff58270 2022-09-19 op }
80 bff58270 2022-09-19 op
81 bff58270 2022-09-19 op struct template *
82 bff58270 2022-09-19 op template(void *arg, tmpl_puts putsfn, tmpl_putc putcfn)
83 bff58270 2022-09-19 op {
84 bff58270 2022-09-19 op struct template *tp;
85 bff58270 2022-09-19 op
86 bff58270 2022-09-19 op if ((tp = calloc(1, sizeof(*tp))) == NULL)
87 bff58270 2022-09-19 op return (NULL);
88 bff58270 2022-09-19 op
89 bff58270 2022-09-19 op tp->tp_arg = arg;
90 bff58270 2022-09-19 op tp->tp_escape = tp_htmlescape;
91 bff58270 2022-09-19 op tp->tp_puts = putsfn;
92 bff58270 2022-09-19 op tp->tp_putc = putcfn;
93 bff58270 2022-09-19 op
94 bff58270 2022-09-19 op return (tp);
95 bff58270 2022-09-19 op }
96 bff58270 2022-09-19 op
97 bff58270 2022-09-19 op void
98 9b929b3a 2022-09-29 op template_free(struct template *tp)
99 9b929b3a 2022-09-29 op {
100 9b929b3a 2022-09-29 op free(tp->tp_tmp);
101 9b929b3a 2022-09-29 op free(tp);
102 9b929b3a 2022-09-29 op }