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