author | Lars Hjemli <hjemli@gmail.com> | 2008-02-23 19:13:57 (UTC) |
---|---|---|
committer | Lars Hjemli <hjemli@gmail.com> | 2008-02-23 19:14:01 (UTC) |
commit | e0572c39f78b4f88c706a49a60e211302b8e015c (patch) (unidiff) | |
tree | 4432d0400549c7ea521eea1dffddfbb2f96de0c6 | |
parent | e5ed227ef0da561e2bde8646ec816842392377ee (diff) | |
parent | eacde43d7184452e1fdc90b982b531f1f5239923 (diff) | |
download | cgit-e0572c39f78b4f88c706a49a60e211302b8e015c.zip cgit-e0572c39f78b4f88c706a49a60e211302b8e015c.tar.gz cgit-e0572c39f78b4f88c706a49a60e211302b8e015c.tar.bz2 |
Merge branch 'stable'
* stable:
Fix segfault
Signed-off-by: Lars Hjemli <hjemli@gmail.com>
-rw-r--r-- | html.c | 20 |
1 files changed, 5 insertions, 15 deletions
@@ -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 | ||
11 | char *fmt(const char *format, ...) | 11 | char *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 | ||
29 | void html(const char *txt) | 29 | void html(const char *txt) |
30 | { | 30 | { |
31 | write(htmlfd, txt, strlen(txt)); | 31 | write(htmlfd, txt, strlen(txt)); |
32 | } | 32 | } |
33 | 33 | ||
34 | void htmlf(const char *format, ...) | 34 | void 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 | ||
45 | void html_txt(char *txt) | 45 | void 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(">"); | 53 | html(">"); |
56 | else if (c=='<') | 54 | else if (c=='<') |
57 | html("<"); | 55 | html("<"); |
58 | else if (c=='&') | 56 | else if (c=='&') |
59 | html("&"); | 57 | html("&"); |
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 | ||
68 | void html_ntxt(int len, char *txt) | 66 | void 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(">"); | 74 | html(">"); |
79 | else if (c=='<') | 75 | else if (c=='<') |
80 | html("<"); | 76 | html("<"); |
81 | else if (c=='&') | 77 | else if (c=='&') |
82 | html("&"); | 78 | html("&"); |
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 | ||
97 | void html_attr(char *txt) | 89 | void 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(">"); | 97 | html(">"); |
108 | else if (c=='<') | 98 | else if (c=='<') |
109 | html("<"); | 99 | html("<"); |
110 | else if (c=='\'') | 100 | else if (c=='\'') |
111 | html(""e;"); | 101 | html(""e;"); |
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 | ||
120 | void html_hidden(char *name, char *value) | 110 | void 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 | ||
129 | void html_option(char *value, char *text, char *selected_value) | 119 | void 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 | ||
141 | void html_link_open(char *url, char *title, char *class) | 131 | void 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 | ||
156 | void html_link_close(void) | 146 | void html_link_close(void) |
157 | { | 147 | { |
158 | html("</a>"); | 148 | html("</a>"); |
159 | } | 149 | } |
160 | 150 | ||
161 | void html_fileperm(unsigned short mode) | 151 | void 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 | ||
167 | void html_filemode(unsigned short mode) | 157 | void 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 | ||
182 | int html_include(const char *filename) | 172 | int 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 | } |