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_status(int code, int more_headers)
56 htmlf("Status: %d\n", code);
61 void html_txt(char *txt)
66 if (c=='<' || c=='>' || c=='&') {
67 write(htmlfd, txt, t - txt);
82 void html_ntxt(int len, char *txt)
85 while(t && *t && len--){
87 if (c=='<' || c=='>' || c=='&') {
88 write(htmlfd, txt, t - txt);
100 write(htmlfd, txt, t - txt);
105 void html_attr(char *txt)
110 if (c=='<' || c=='>' || c=='\'') {
111 write(htmlfd, txt, t - txt);
126 void html_hidden(char *name, char *value)
128 html("<input type='hidden' name='");
135 void html_option(char *value, char *text, char *selected_value)
137 html("<option value='");
140 if (selected_value && !strcmp(selected_value, value))
141 html(" selected='selected'");
147 void html_link_open(char *url, char *title, char *class)
162 void html_link_close(void)
167 void html_fileperm(unsigned short mode)
169 htmlf("%c%c%c", (mode & 4 ? 'r' : '-'),
170 (mode & 2 ? 'w' : '-'), (mode & 1 ? 'x' : '-'));
173 int html_include(const char *filename)
179 if (!(f = fopen(filename, "r"))) {
180 fprintf(stderr, "[cgit] Failed to include file %s: %s (%d).\n",
181 filename, strerror(errno), errno);
184 while((len = fread(buf, 1, 4096, f)) > 0)
185 write(htmlfd, buf, len);
192 if (c >= 'a' && c <= 'f')
194 else if (c >= 'A' && c <= 'F')
196 else if (c >= '0' && c <= '9')
202 char *convert_query_hexchar(char *txt)
205 if (strlen(txt) < 3) {
209 d1 = hextoint(*(txt+1));
210 d2 = hextoint(*(txt+2));
216 strcpy(txt+1, txt+3);
221 int http_parse_querystring(char *txt, void (*fn)(const char *name, const char *value))
223 char *t, *value = NULL, c;
228 t = txt = strdup(txt);
230 printf("Out of memory\n");
233 while((c=*t) != '\0') {
240 t = convert_query_hexchar(t);