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)
16 int htmlfd = STDOUT_FILENO;
18 char *fmt(const char *format, ...)
20 static char buf[8][1024];
28 va_start(args, format);
29 len = vsnprintf(buf[bufidx], sizeof(buf[bufidx]), format, args);
31 if (len>sizeof(buf[bufidx])) {
32 fprintf(stderr, "[html.c] string truncated: %s\n", format);
38 void html(const char *txt)
40 write(htmlfd, txt, strlen(txt));
43 void htmlf(const char *format, ...)
45 static char buf[65536];
48 va_start(args, format);
49 vsnprintf(buf, sizeof(buf), format, args);
54 void html_txt(char *txt)
59 if (c=='<' || c=='>' || c=='&') {
60 write(htmlfd, txt, t - txt);
75 void html_ntxt(int len, char *txt)
78 while(t && *t && len--){
80 if (c=='<' || c=='>' || c=='&') {
81 write(htmlfd, txt, t - txt);
93 write(htmlfd, txt, t - txt);
98 void html_attr(char *txt)
103 if (c=='<' || c=='>' || c=='\'') {
104 write(htmlfd, txt, t - txt);
119 void html_hidden(char *name, char *value)
121 html("<input type='hidden' name='");
128 void html_option(char *value, char *text, char *selected_value)
130 html("<option value='");
133 if (selected_value && !strcmp(selected_value, value))
134 html(" selected='selected'");
140 void html_link_open(char *url, char *title, char *class)
155 void html_link_close(void)
160 void html_fileperm(unsigned short mode)
162 htmlf("%c%c%c", (mode & 4 ? 'r' : '-'),
163 (mode & 2 ? 'w' : '-'), (mode & 1 ? 'x' : '-'));
166 int html_include(const char *filename)
172 if (!(f = fopen(filename, "r"))) {
173 fprintf(stderr, "[cgit] Failed to include file %s: %s (%d).\n",
174 filename, strerror(errno), errno);
177 while((len = fread(buf, 1, 4096, f)) > 0)
178 write(htmlfd, buf, len);
185 if (c >= 'a' && c <= 'f')
187 else if (c >= 'A' && c <= 'F')
189 else if (c >= '0' && c <= '9')
195 char *convert_query_hexchar(char *txt)
198 if (strlen(txt) < 3) {
202 d1 = hextoint(*(txt+1));
203 d2 = hextoint(*(txt+2));
209 strcpy(txt+1, txt+3);
214 int http_parse_querystring(char *txt, void (*fn)(const char *name, const char *value))
216 char *t, *value = NULL, c;
221 t = txt = strdup(txt);
223 printf("Out of memory\n");
226 while((c=*t) != '\0') {
233 t = convert_query_hexchar(t);