]>
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
=='\'' || c
=='\"') {
116 write(htmlfd
, txt
, t
- txt
);
133 void html_url_path(char *txt
)
138 if (c
=='"' || c
=='#' || c
=='\'' || c
=='?') {
139 write(htmlfd
, txt
, t
- txt
);
140 write(htmlfd
, fmt("%%%2x", c
), 3);
149 void html_url_arg(char *txt
)
154 if (c
=='"' || c
=='#' || c
=='%' || c
=='&' || c
=='\'' || c
=='+' || c
=='?') {
155 write(htmlfd
, txt
, t
- txt
);
156 write(htmlfd
, fmt("%%%2x", c
), 3);
165 void html_hidden(char *name
, char *value
)
167 html("<input type='hidden' name='");
174 void html_option(char *value
, char *text
, char *selected_value
)
176 html("<option value='");
179 if (selected_value
&& !strcmp(selected_value
, value
))
180 html(" selected='selected'");
186 void html_link_open(char *url
, char *title
, char *class)
201 void html_link_close(void)
206 void html_fileperm(unsigned short mode
)
208 htmlf("%c%c%c", (mode
& 4 ? 'r' : '-'),
209 (mode
& 2 ? 'w' : '-'), (mode
& 1 ? 'x' : '-'));
212 int html_include(const char *filename
)
218 if (!(f
= fopen(filename
, "r"))) {
219 fprintf(stderr
, "[cgit] Failed to include file %s: %s (%d).\n",
220 filename
, strerror(errno
), errno
);
223 while((len
= fread(buf
, 1, 4096, f
)) > 0)
224 write(htmlfd
, buf
, len
);
231 if (c
>= 'a' && c
<= 'f')
233 else if (c
>= 'A' && c
<= 'F')
235 else if (c
>= '0' && c
<= '9')
241 char *convert_query_hexchar(char *txt
)
244 if (strlen(txt
) < 3) {
248 d1
= hextoint(*(txt
+1));
249 d2
= hextoint(*(txt
+2));
255 strcpy(txt
+1, txt
+3);
260 int http_parse_querystring(char *txt
, void (*fn
)(const char *name
, const char *value
))
262 char *t
, *value
= NULL
, c
;
267 t
= txt
= strdup(txt
);
269 printf("Out of memory\n");
272 while((c
=*t
) != '\0') {
279 t
= convert_query_hexchar(t
);