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=='&') {
59 write(htmlfd, txt, t - txt);
74 void html_ntxt(int len, char *txt)
77 while(t && *t && len--){
79 if (c=='<' || c=='>' || c=='&') {
80 write(htmlfd, txt, t - txt);
92 write(htmlfd, txt, t - txt);
97 void html_attr(char *txt)
102 if (c=='<' || c=='>' || c=='\'') {
103 write(htmlfd, txt, t - txt);
118 void html_hidden(char *name, char *value)
120 html("<input type='hidden' name='");
127 void html_option(char *value, char *text, char *selected_value)
129 html("<option value='");
132 if (selected_value && !strcmp(selected_value, value))
133 html(" selected='selected'");
139 void html_link_open(char *url, char *title, char *class)
154 void html_link_close(void)
159 void html_fileperm(unsigned short mode)
161 htmlf("%c%c%c", (mode & 4 ? 'r' : '-'),
162 (mode & 2 ? 'w' : '-'), (mode & 1 ? 'x' : '-'));
165 int html_include(const char *filename)
171 if (!(f = fopen(filename, "r")))
173 while((len = fread(buf, 1, 4096, f)) > 0)
174 write(htmlfd, buf, len);
181 if (c >= 'a' && c <= 'f')
183 else if (c >= 'A' && c <= 'F')
185 else if (c >= '0' && c <= '9')
191 char *convert_query_hexchar(char *txt)
194 if (strlen(txt) < 3) {
198 d1 = hextoint(*(txt+1));
199 d2 = hextoint(*(txt+2));
205 strcpy(txt+1, txt+3);
210 int http_parse_querystring(char *txt, void (*fn)(const char *name, const char *value))
212 char *t, *value = NULL, c;
217 t = txt = strdup(txt);
219 printf("Out of memory\n");
222 while((c=*t) != '\0') {
229 t = convert_query_hexchar(t);