summaryrefslogtreecommitdiffabout
path: root/html.c
Unidiff
Diffstat (limited to 'html.c') (more/less context) (ignore whitespace changes)
-rw-r--r--html.c31
1 files changed, 12 insertions, 19 deletions
diff --git a/html.c b/html.c
index eb163d9..0962e71 100644
--- a/html.c
+++ b/html.c
@@ -1,73 +1,81 @@
1/* html.c: helper functions for html output 1/* html.c: helper functions for html output
2 * 2 *
3 * Copyright (C) 2006 Lars Hjemli 3 * Copyright (C) 2006 Lars Hjemli
4 * 4 *
5 * Licensed under GNU General Public License v2 5 * Licensed under GNU General Public License v2
6 * (see COPYING for full license text) 6 * (see COPYING for full license text)
7 */ 7 */
8 8
9#include "cgit.h" 9#include <unistd.h>
10#include <stdio.h>
11#include <stdlib.h>
12#include <stdarg.h>
13#include <string.h>
14
15int htmlfd = STDOUT_FILENO;
10 16
11char *fmt(const char *format, ...) 17char *fmt(const char *format, ...)
12{ 18{
13 static char buf[8][1024]; 19 static char buf[8][1024];
14 static int bufidx; 20 static int bufidx;
15 int len; 21 int len;
16 va_list args; 22 va_list args;
17 23
18 bufidx++; 24 bufidx++;
19 bufidx &= 7; 25 bufidx &= 7;
20 26
21 va_start(args, format); 27 va_start(args, format);
22 len = vsnprintf(buf[bufidx], sizeof(buf[bufidx]), format, args); 28 len = vsnprintf(buf[bufidx], sizeof(buf[bufidx]), format, args);
23 va_end(args); 29 va_end(args);
24 if (len>sizeof(buf[bufidx])) 30 if (len>sizeof(buf[bufidx])) {
25 die("[html.c] string truncated: %s", format); 31 fprintf(stderr, "[html.c] string truncated: %s\n", format);
32 exit(1);
33 }
26 return buf[bufidx]; 34 return buf[bufidx];
27} 35}
28 36
29void html(const char *txt) 37void html(const char *txt)
30{ 38{
31 write(htmlfd, txt, strlen(txt)); 39 write(htmlfd, txt, strlen(txt));
32} 40}
33 41
34void htmlf(const char *format, ...) 42void htmlf(const char *format, ...)
35{ 43{
36 static char buf[65536]; 44 static char buf[65536];
37 va_list args; 45 va_list args;
38 46
39 va_start(args, format); 47 va_start(args, format);
40 vsnprintf(buf, sizeof(buf), format, args); 48 vsnprintf(buf, sizeof(buf), format, args);
41 va_end(args); 49 va_end(args);
42 html(buf); 50 html(buf);
43} 51}
44 52
45void html_txt(char *txt) 53void html_txt(char *txt)
46{ 54{
47 char *t = txt; 55 char *t = txt;
48 while(t && *t){ 56 while(t && *t){
49 int c = *t; 57 int c = *t;
50 if (c=='<' || c=='>' || c=='&') { 58 if (c=='<' || c=='>' || c=='&') {
51 *t = '\0'; 59 *t = '\0';
52 html(txt); 60 html(txt);
53 *t = c; 61 *t = c;
54 if (c=='>') 62 if (c=='>')
55 html("&gt;"); 63 html("&gt;");
56 else if (c=='<') 64 else if (c=='<')
57 html("&lt;"); 65 html("&lt;");
58 else if (c=='&') 66 else if (c=='&')
59 html("&amp;"); 67 html("&amp;");
60 txt = t+1; 68 txt = t+1;
61 } 69 }
62 t++; 70 t++;
63 } 71 }
64 if (t!=txt) 72 if (t!=txt)
65 html(txt); 73 html(txt);
66} 74}
67 75
68void html_ntxt(int len, char *txt) 76void html_ntxt(int len, char *txt)
69{ 77{
70 char *t = txt; 78 char *t = txt;
71 while(t && *t && len--){ 79 while(t && *t && len--){
72 int c = *t; 80 int c = *t;
73 if (c=='<' || c=='>' || c=='&') { 81 if (c=='<' || c=='>' || c=='&') {
@@ -115,80 +123,65 @@ void html_attr(char *txt)
115 } 123 }
116 if (t!=txt) 124 if (t!=txt)
117 html(txt); 125 html(txt);
118} 126}
119 127
120void html_hidden(char *name, char *value) 128void html_hidden(char *name, char *value)
121{ 129{
122 html("<input type='hidden' name='"); 130 html("<input type='hidden' name='");
123 html_attr(name); 131 html_attr(name);
124 html("' value='"); 132 html("' value='");
125 html_attr(value); 133 html_attr(value);
126 html("'/>"); 134 html("'/>");
127} 135}
128 136
129void html_option(char *value, char *text, char *selected_value) 137void html_option(char *value, char *text, char *selected_value)
130{ 138{
131 html("<option value='"); 139 html("<option value='");
132 html_attr(value); 140 html_attr(value);
133 html("'"); 141 html("'");
134 if (selected_value && !strcmp(selected_value, value)) 142 if (selected_value && !strcmp(selected_value, value))
135 html(" selected='selected'"); 143 html(" selected='selected'");
136 html(">"); 144 html(">");
137 html_txt(text); 145 html_txt(text);
138 html("</option>\n"); 146 html("</option>\n");
139} 147}
140 148
141void html_link_open(char *url, char *title, char *class) 149void html_link_open(char *url, char *title, char *class)
142{ 150{
143 html("<a href='"); 151 html("<a href='");
144 html_attr(url); 152 html_attr(url);
145 if (title) { 153 if (title) {
146 html("' title='"); 154 html("' title='");
147 html_attr(title); 155 html_attr(title);
148 } 156 }
149 if (class) { 157 if (class) {
150 html("' class='"); 158 html("' class='");
151 html_attr(class); 159 html_attr(class);
152 } 160 }
153 html("'>"); 161 html("'>");
154} 162}
155 163
156void html_link_close(void) 164void html_link_close(void)
157{ 165{
158 html("</a>"); 166 html("</a>");
159} 167}
160 168
161void html_fileperm(unsigned short mode) 169void html_fileperm(unsigned short mode)
162{ 170{
163 htmlf("%c%c%c", (mode & 4 ? 'r' : '-'), 171 htmlf("%c%c%c", (mode & 4 ? 'r' : '-'),
164 (mode & 2 ? 'w' : '-'), (mode & 1 ? 'x' : '-')); 172 (mode & 2 ? 'w' : '-'), (mode & 1 ? 'x' : '-'));
165} 173}
166 174
167void html_filemode(unsigned short mode)
168{
169 if (S_ISDIR(mode))
170 html("d");
171 else if (S_ISLNK(mode))
172 html("l");
173 else if (S_ISGITLINK(mode))
174 html("m");
175 else
176 html("-");
177 html_fileperm(mode >> 6);
178 html_fileperm(mode >> 3);
179 html_fileperm(mode);
180}
181
182int html_include(const char *filename) 175int html_include(const char *filename)
183{ 176{
184 FILE *f; 177 FILE *f;
185 char buf[4096]; 178 char buf[4096];
186 size_t len; 179 size_t len;
187 180
188 if (!(f = fopen(filename, "r"))) 181 if (!(f = fopen(filename, "r")))
189 return -1; 182 return -1;
190 while((len = fread(buf, 1, 4096, f)) > 0) 183 while((len = fread(buf, 1, 4096, f)) > 0)
191 write(htmlfd, buf, len); 184 write(htmlfd, buf, len);
192 fclose(f); 185 fclose(f);
193 return 0; 186 return 0;
194} 187}