blob: bddb04d1adcb8052b7590a07838b8b4c1f6a8247 [file] [log] [blame]
Lars Hjemli7640d902006-12-10 22:41:14 +01001/* html.c: helper functions for html output
2 *
3 * Copyright (C) 2006 Lars Hjemli
4 *
5 * Licensed under GNU General Public License v2
6 * (see COPYING for full license text)
7 */
8
Lars Hjemlib1f9b9c2008-02-23 22:45:33 +01009#include <unistd.h>
10#include <stdio.h>
11#include <stdlib.h>
12#include <stdarg.h>
13#include <string.h>
Harley Laue112b2082008-04-29 17:59:53 +020014#include <errno.h>
Lars Hjemlib1f9b9c2008-02-23 22:45:33 +010015
16int htmlfd = STDOUT_FILENO;
Lars Hjemli0d169ad2006-12-09 15:18:17 +010017
18char *fmt(const char *format, ...)
19{
20 static char buf[8][1024];
21 static int bufidx;
22 int len;
23 va_list args;
24
25 bufidx++;
26 bufidx &= 7;
27
28 va_start(args, format);
29 len = vsnprintf(buf[bufidx], sizeof(buf[bufidx]), format, args);
30 va_end(args);
Lars Hjemlib1f9b9c2008-02-23 22:45:33 +010031 if (len>sizeof(buf[bufidx])) {
32 fprintf(stderr, "[html.c] string truncated: %s\n", format);
33 exit(1);
34 }
Lars Hjemli0d169ad2006-12-09 15:18:17 +010035 return buf[bufidx];
36}
37
38void html(const char *txt)
39{
Lars Hjemli25105d72006-12-10 22:31:36 +010040 write(htmlfd, txt, strlen(txt));
Lars Hjemli0d169ad2006-12-09 15:18:17 +010041}
42
43void htmlf(const char *format, ...)
44{
Lars Hjemli25105d72006-12-10 22:31:36 +010045 static char buf[65536];
Lars Hjemli0d169ad2006-12-09 15:18:17 +010046 va_list args;
47
48 va_start(args, format);
Lars Hjemli25105d72006-12-10 22:31:36 +010049 vsnprintf(buf, sizeof(buf), format, args);
Lars Hjemli0d169ad2006-12-09 15:18:17 +010050 va_end(args);
Lars Hjemli25105d72006-12-10 22:31:36 +010051 html(buf);
Lars Hjemli0d169ad2006-12-09 15:18:17 +010052}
53
54void html_txt(char *txt)
55{
56 char *t = txt;
Lars Hjemli27cd3b22006-12-28 01:54:43 +010057 while(t && *t){
Lars Hjemli0d169ad2006-12-09 15:18:17 +010058 int c = *t;
59 if (c=='<' || c=='>' || c=='&') {
Hiroki Hattorieacde432008-02-24 02:57:34 +090060 write(htmlfd, txt, t - txt);
Lars Hjemli0d169ad2006-12-09 15:18:17 +010061 if (c=='>')
62 html("&gt;");
63 else if (c=='<')
64 html("&lt;");
65 else if (c=='&')
66 html("&amp;");
67 txt = t+1;
68 }
69 t++;
70 }
71 if (t!=txt)
72 html(txt);
73}
74
Lars Hjemli9d8d9b62006-12-22 00:58:18 +010075void html_ntxt(int len, char *txt)
76{
77 char *t = txt;
Lars Hjemli27cd3b22006-12-28 01:54:43 +010078 while(t && *t && len--){
Lars Hjemli9d8d9b62006-12-22 00:58:18 +010079 int c = *t;
80 if (c=='<' || c=='>' || c=='&') {
Hiroki Hattorieacde432008-02-24 02:57:34 +090081 write(htmlfd, txt, t - txt);
Lars Hjemli9d8d9b62006-12-22 00:58:18 +010082 if (c=='>')
83 html("&gt;");
84 else if (c=='<')
85 html("&lt;");
86 else if (c=='&')
87 html("&amp;");
88 txt = t+1;
89 }
90 t++;
91 }
Hiroki Hattorieacde432008-02-24 02:57:34 +090092 if (t!=txt)
93 write(htmlfd, txt, t - txt);
Lars Hjemli9d8d9b62006-12-22 00:58:18 +010094 if (len<0)
95 html("...");
96}
Lars Hjemli0d169ad2006-12-09 15:18:17 +010097
98void html_attr(char *txt)
99{
100 char *t = txt;
Lars Hjemli27cd3b22006-12-28 01:54:43 +0100101 while(t && *t){
Lars Hjemli0d169ad2006-12-09 15:18:17 +0100102 int c = *t;
103 if (c=='<' || c=='>' || c=='\'') {
Hiroki Hattorieacde432008-02-24 02:57:34 +0900104 write(htmlfd, txt, t - txt);
Lars Hjemli0d169ad2006-12-09 15:18:17 +0100105 if (c=='>')
106 html("&gt;");
107 else if (c=='<')
108 html("&lt;");
109 else if (c=='\'')
110 html("&quote;");
111 txt = t+1;
112 }
113 t++;
114 }
115 if (t!=txt)
116 html(txt);
117}
118
Lars Hjemlie39d7382006-12-28 02:01:49 +0100119void html_hidden(char *name, char *value)
120{
121 html("<input type='hidden' name='");
122 html_attr(name);
123 html("' value='");
124 html_attr(value);
125 html("'/>");
126}
127
Lars Hjemli6ec5f362007-10-28 12:08:45 +0100128void html_option(char *value, char *text, char *selected_value)
129{
130 html("<option value='");
131 html_attr(value);
132 html("'");
133 if (selected_value && !strcmp(selected_value, value))
Lars Hjemli29154832007-11-11 13:04:28 +0100134 html(" selected='selected'");
Lars Hjemli6ec5f362007-10-28 12:08:45 +0100135 html(">");
136 html_txt(text);
137 html("</option>\n");
138}
139
Lars Hjemli0d169ad2006-12-09 15:18:17 +0100140void html_link_open(char *url, char *title, char *class)
141{
142 html("<a href='");
143 html_attr(url);
144 if (title) {
145 html("' title='");
146 html_attr(title);
147 }
148 if (class) {
149 html("' class='");
150 html_attr(class);
151 }
152 html("'>");
153}
154
155void html_link_close(void)
156{
157 html("</a>");
158}
Lars Hjemli6cb326c2006-12-17 23:07:28 +0100159
160void html_fileperm(unsigned short mode)
161{
Lars Hjemlib1f9b9c2008-02-23 22:45:33 +0100162 htmlf("%c%c%c", (mode & 4 ? 'r' : '-'),
Lars Hjemli6cb326c2006-12-17 23:07:28 +0100163 (mode & 2 ? 'w' : '-'), (mode & 1 ? 'x' : '-'));
164}
165
Lars Hjemli5e751282007-05-18 23:56:10 +0200166int html_include(const char *filename)
167{
168 FILE *f;
169 char buf[4096];
170 size_t len;
171
Harley Laue112b2082008-04-29 17:59:53 +0200172 if (!(f = fopen(filename, "r"))) {
173 fprintf(stderr, "[cgit] Failed to include file %s: %s (%d).\n",
174 filename, strerror(errno), errno);
Lars Hjemli5e751282007-05-18 23:56:10 +0200175 return -1;
Harley Laue112b2082008-04-29 17:59:53 +0200176 }
Lars Hjemli5e751282007-05-18 23:56:10 +0200177 while((len = fread(buf, 1, 4096, f)) > 0)
178 write(htmlfd, buf, len);
179 fclose(f);
180 return 0;
181}
Lars Hjemlie87e8962008-04-08 21:11:36 +0200182
183int hextoint(char c)
184{
185 if (c >= 'a' && c <= 'f')
186 return 10 + c - 'a';
187 else if (c >= 'A' && c <= 'F')
188 return 10 + c - 'A';
189 else if (c >= '0' && c <= '9')
190 return c - '0';
191 else
192 return -1;
193}
194
195char *convert_query_hexchar(char *txt)
196{
197 int d1, d2;
198 if (strlen(txt) < 3) {
199 *txt = '\0';
200 return txt-1;
201 }
202 d1 = hextoint(*(txt+1));
203 d2 = hextoint(*(txt+2));
204 if (d1<0 || d2<0) {
205 strcpy(txt, txt+3);
206 return txt-1;
207 } else {
208 *txt = d1 * 16 + d2;
209 strcpy(txt+1, txt+3);
210 return txt;
211 }
212}
213
214int http_parse_querystring(char *txt, void (*fn)(const char *name, const char *value))
215{
216 char *t, *value = NULL, c;
217
218 if (!txt)
219 return 0;
220
221 t = txt = strdup(txt);
222 if (t == NULL) {
223 printf("Out of memory\n");
224 exit(1);
225 }
226 while((c=*t) != '\0') {
227 if (c=='=') {
228 *t = '\0';
229 value = t+1;
230 } else if (c=='+') {
231 *t = ' ';
232 } else if (c=='%') {
233 t = convert_query_hexchar(t);
234 } else if (c=='&') {
235 *t = '\0';
236 (*fn)(txt, value);
237 txt = t+1;
238 value = NULL;
239 }
240 t++;
241 }
242 if (t!=txt)
243 (*fn)(txt, value);
244 return 0;
245}