]>
git.cameronkatri.com Git - cgit.git/blob - html.c
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_raw(const char *data
, size_t size
)
40 write(htmlfd
, data
, size
);
43 void html(const char *txt
)
45 write(htmlfd
, txt
, strlen(txt
));
48 void htmlf(const char *format
, ...)
50 static char buf
[65536];
53 va_start(args
, format
);
54 vsnprintf(buf
, sizeof(buf
), format
, args
);
59 void html_status(int code
, const char *msg
, int more_headers
)
61 htmlf("Status: %d %s\n", code
, msg
);
66 void html_txt(char *txt
)
71 if (c
=='<' || c
=='>' || c
=='&') {
72 write(htmlfd
, txt
, t
- txt
);
87 void html_ntxt(int len
, char *txt
)
90 while(t
&& *t
&& len
--){
92 if (c
=='<' || c
=='>' || c
=='&') {
93 write(htmlfd
, txt
, t
- txt
);
105 write(htmlfd
, txt
, t
- txt
);
110 void html_attr(char *txt
)
115 if (c
=='<' || c
=='>' || c
=='\'') {
116 write(htmlfd
, txt
, t
- txt
);
131 void html_url_path(char *txt
)
136 if (c
=='"' || c
=='#' || c
=='\'' || c
=='?') {
137 write(htmlfd
, txt
, t
- txt
);
138 write(htmlfd
, fmt("%%%2x", c
), 3);
147 void html_url_arg(char *txt
)
152 if (c
=='"' || c
=='#' || c
=='%' || c
=='&' || c
=='\'' || c
=='+' || c
=='?') {
153 write(htmlfd
, txt
, t
- txt
);
154 write(htmlfd
, fmt("%%%2x", c
), 3);
163 void html_hidden(char *name
, char *value
)
165 html("<input type='hidden' name='");
172 void html_option(char *value
, char *text
, char *selected_value
)
174 html("<option value='");
177 if (selected_value
&& !strcmp(selected_value
, value
))
178 html(" selected='selected'");
184 void html_link_open(char *url
, char *title
, char *class)
199 void html_link_close(void)
204 void html_fileperm(unsigned short mode
)
206 htmlf("%c%c%c", (mode
& 4 ? 'r' : '-'),
207 (mode
& 2 ? 'w' : '-'), (mode
& 1 ? 'x' : '-'));
210 int html_include(const char *filename
)
216 if (!(f
= fopen(filename
, "r"))) {
217 fprintf(stderr
, "[cgit] Failed to include file %s: %s (%d).\n",
218 filename
, strerror(errno
), errno
);
221 while((len
= fread(buf
, 1, 4096, f
)) > 0)
222 write(htmlfd
, buf
, len
);
229 if (c
>= 'a' && c
<= 'f')
231 else if (c
>= 'A' && c
<= 'F')
233 else if (c
>= '0' && c
<= '9')
239 char *convert_query_hexchar(char *txt
)
242 if (strlen(txt
) < 3) {
246 d1
= hextoint(*(txt
+1));
247 d2
= hextoint(*(txt
+2));
253 strcpy(txt
+1, txt
+3);
258 int http_parse_querystring(char *txt
, void (*fn
)(const char *name
, const char *value
))
260 char *t
, *value
= NULL
, c
;
265 t
= txt
= strdup(txt
);
267 printf("Out of memory\n");
270 while((c
=*t
) != '\0') {
277 t
= convert_query_hexchar(t
);