1 /* html.c: helper functions for html output
3 * Copyright (C) 2006 Lars Hjemli
5 * Licensed under GNU General Public License v2
6 * (see COPYING for full license text)
15 int htmlfd = STDOUT_FILENO;
17 char *fmt(const char *format, ...)
19 static char buf[8][1024];
27 va_start(args, format);
28 len = vsnprintf(buf[bufidx], sizeof(buf[bufidx]), format, args);
30 if (len>sizeof(buf[bufidx])) {
31 fprintf(stderr, "[html.c] string truncated: %s\n", format);
37 void html(const char *txt)
39 write(htmlfd, txt, strlen(txt));
42 void htmlf(const char *format, ...)
44 static char buf[65536];
47 va_start(args, format);
48 vsnprintf(buf, sizeof(buf), format, args);
53 void html_txt(char *txt)
58 if (c=='<' || c=='>' || c=='&') {
76 void html_ntxt(int len, char *txt)
79 while(t && *t && len--){
81 if (c=='<' || c=='>' || c=='&') {
105 void html_attr(char *txt)
110 if (c=='<' || c=='>' || c=='\'') {
128 void html_hidden(char *name, char *value)
130 html("<input type='hidden' name='");
137 void html_option(char *value, char *text, char *selected_value)
139 html("<option value='");
142 if (selected_value && !strcmp(selected_value, value))
143 html(" selected='selected'");
149 void html_link_open(char *url, char *title, char *class)
164 void html_link_close(void)
169 void html_fileperm(unsigned short mode)
171 htmlf("%c%c%c", (mode & 4 ? 'r' : '-'),
172 (mode & 2 ? 'w' : '-'), (mode & 1 ? 'x' : '-'));
175 int html_include(const char *filename)
181 if (!(f = fopen(filename, "r")))
183 while((len = fread(buf, 1, 4096, f)) > 0)
184 write(htmlfd, buf, len);
191 if (c >= 'a' && c <= 'f')
193 else if (c >= 'A' && c <= 'F')
195 else if (c >= '0' && c <= '9')
201 char *convert_query_hexchar(char *txt)
204 if (strlen(txt) < 3) {
208 d1 = hextoint(*(txt+1));
209 d2 = hextoint(*(txt+2));
215 strcpy(txt+1, txt+3);
220 int http_parse_querystring(char *txt, void (*fn)(const char *name, const char *value))
222 char *t, *value = NULL, c;
227 t = txt = strdup(txt);
229 printf("Out of memory\n");
232 while((c=*t) != '\0') {
239 t = convert_query_hexchar(t);