author | Lars Hjemli <hjemli@gmail.com> | 2008-02-23 21:45:33 (UTC) |
---|---|---|
committer | Lars Hjemli <hjemli@gmail.com> | 2008-03-18 07:13:10 (UTC) |
commit | b1f9b9c1459cb9a30ebf80721aff6ef788d1f891 (patch) (unidiff) | |
tree | 05796a741faef90c12aadd3a5c92b702ec870c48 /html.c | |
parent | b88fb016d0209f7041ac7d3b4d2c077318407a4d (diff) | |
download | cgit-b1f9b9c1459cb9a30ebf80721aff6ef788d1f891.zip cgit-b1f9b9c1459cb9a30ebf80721aff6ef788d1f891.tar.gz cgit-b1f9b9c1459cb9a30ebf80721aff6ef788d1f891.tar.bz2 |
Introduce html.h
All html-functions can be quite easily separated from the rest of cgit, so
lets do it; the only issue was html_filemode which uses some git-defined
macros so the function is moved into ui-shared.c::cgit_print_filemode().
Signed-off-by: Lars Hjemli <hjemli@gmail.com>
-rw-r--r-- | html.c | 31 |
1 files changed, 12 insertions, 19 deletions
@@ -1,194 +1,187 @@ | |||
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 | |||
15 | int htmlfd = STDOUT_FILENO; | ||
10 | 16 | ||
11 | char *fmt(const char *format, ...) | 17 | char *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 | ||
29 | void html(const char *txt) | 37 | void html(const char *txt) |
30 | { | 38 | { |
31 | write(htmlfd, txt, strlen(txt)); | 39 | write(htmlfd, txt, strlen(txt)); |
32 | } | 40 | } |
33 | 41 | ||
34 | void htmlf(const char *format, ...) | 42 | void 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 | ||
45 | void html_txt(char *txt) | 53 | void 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(">"); | 63 | html(">"); |
56 | else if (c=='<') | 64 | else if (c=='<') |
57 | html("<"); | 65 | html("<"); |
58 | else if (c=='&') | 66 | else if (c=='&') |
59 | html("&"); | 67 | html("&"); |
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 | ||
68 | void html_ntxt(int len, char *txt) | 76 | void 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=='&') { |
74 | *t = '\0'; | 82 | *t = '\0'; |
75 | html(txt); | 83 | html(txt); |
76 | *t = c; | 84 | *t = c; |
77 | if (c=='>') | 85 | if (c=='>') |
78 | html(">"); | 86 | html(">"); |
79 | else if (c=='<') | 87 | else if (c=='<') |
80 | html("<"); | 88 | html("<"); |
81 | else if (c=='&') | 89 | else if (c=='&') |
82 | html("&"); | 90 | html("&"); |
83 | txt = t+1; | 91 | txt = t+1; |
84 | } | 92 | } |
85 | t++; | 93 | t++; |
86 | } | 94 | } |
87 | if (t!=txt) { | 95 | if (t!=txt) { |
88 | char c = *t; | 96 | char c = *t; |
89 | *t = '\0'; | 97 | *t = '\0'; |
90 | html(txt); | 98 | html(txt); |
91 | *t = c; | 99 | *t = c; |
92 | } | 100 | } |
93 | if (len<0) | 101 | if (len<0) |
94 | html("..."); | 102 | html("..."); |
95 | } | 103 | } |
96 | 104 | ||
97 | void html_attr(char *txt) | 105 | void html_attr(char *txt) |
98 | { | 106 | { |
99 | char *t = txt; | 107 | char *t = txt; |
100 | while(t && *t){ | 108 | while(t && *t){ |
101 | int c = *t; | 109 | int c = *t; |
102 | if (c=='<' || c=='>' || c=='\'') { | 110 | if (c=='<' || c=='>' || c=='\'') { |
103 | *t = '\0'; | 111 | *t = '\0'; |
104 | html(txt); | 112 | html(txt); |
105 | *t = c; | 113 | *t = c; |
106 | if (c=='>') | 114 | if (c=='>') |
107 | html(">"); | 115 | html(">"); |
108 | else if (c=='<') | 116 | else if (c=='<') |
109 | html("<"); | 117 | html("<"); |
110 | else if (c=='\'') | 118 | else if (c=='\'') |
111 | html(""e;"); | 119 | html(""e;"); |
112 | txt = t+1; | 120 | txt = t+1; |
113 | } | 121 | } |
114 | t++; | 122 | t++; |
115 | } | 123 | } |
116 | if (t!=txt) | 124 | if (t!=txt) |
117 | html(txt); | 125 | html(txt); |
118 | } | 126 | } |
119 | 127 | ||
120 | void html_hidden(char *name, char *value) | 128 | void 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 | ||
129 | void html_option(char *value, char *text, char *selected_value) | 137 | void 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 | ||
141 | void html_link_open(char *url, char *title, char *class) | 149 | void 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 | ||
156 | void html_link_close(void) | 164 | void html_link_close(void) |
157 | { | 165 | { |
158 | html("</a>"); | 166 | html("</a>"); |
159 | } | 167 | } |
160 | 168 | ||
161 | void html_fileperm(unsigned short mode) | 169 | void 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 | ||
167 | void 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 | |||
182 | int html_include(const char *filename) | 175 | int 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 | } |