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