blob: 0962e71a32b66a68f1653594de37506425e5eca9 [file] [log] [blame]
Lars Hjemli7640d902006-12-10 22:41:14 +01001/* 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
Lars Hjemlib1f9b9c2008-02-23 22:45:33 +01009#include <unistd.h>
10#include <stdio.h>
11#include <stdlib.h>
12#include <stdarg.h>
13#include <string.h>
14
15int htmlfd = STDOUT_FILENO;
Lars Hjemli0d169ad2006-12-09 15:18:17 +010016
17char *fmt(const char *format, ...)
18{
19 static char buf[8][1024];
20 static int bufidx;
21 int len;
22 va_list args;
23
24 bufidx++;
25 bufidx &= 7;
26
27 va_start(args, format);
28 len = vsnprintf(buf[bufidx], sizeof(buf[bufidx]), format, args);
29 va_end(args);
Lars Hjemlib1f9b9c2008-02-23 22:45:33 +010030 if (len>sizeof(buf[bufidx])) {
31 fprintf(stderr, "[html.c] string truncated: %s\n", format);
32 exit(1);
33 }
Lars Hjemli0d169ad2006-12-09 15:18:17 +010034 return buf[bufidx];
35}
36
37void html(const char *txt)
38{
Lars Hjemli25105d72006-12-10 22:31:36 +010039 write(htmlfd, txt, strlen(txt));
Lars Hjemli0d169ad2006-12-09 15:18:17 +010040}
41
42void htmlf(const char *format, ...)
43{
Lars Hjemli25105d72006-12-10 22:31:36 +010044 static char buf[65536];
Lars Hjemli0d169ad2006-12-09 15:18:17 +010045 va_list args;
46
47 va_start(args, format);
Lars Hjemli25105d72006-12-10 22:31:36 +010048 vsnprintf(buf, sizeof(buf), format, args);
Lars Hjemli0d169ad2006-12-09 15:18:17 +010049 va_end(args);
Lars Hjemli25105d72006-12-10 22:31:36 +010050 html(buf);
Lars Hjemli0d169ad2006-12-09 15:18:17 +010051}
52
53void html_txt(char *txt)
54{
55 char *t = txt;
Lars Hjemli27cd3b22006-12-28 01:54:43 +010056 while(t && *t){
Lars Hjemli0d169ad2006-12-09 15:18:17 +010057 int c = *t;
58 if (c=='<' || c=='>' || c=='&') {
59 *t = '\0';
60 html(txt);
61 *t = c;
62 if (c=='>')
63 html("&gt;");
64 else if (c=='<')
65 html("&lt;");
66 else if (c=='&')
67 html("&amp;");
68 txt = t+1;
69 }
70 t++;
71 }
72 if (t!=txt)
73 html(txt);
74}
75
Lars Hjemli9d8d9b62006-12-22 00:58:18 +010076void html_ntxt(int len, char *txt)
77{
78 char *t = txt;
Lars Hjemli27cd3b22006-12-28 01:54:43 +010079 while(t && *t && len--){
Lars Hjemli9d8d9b62006-12-22 00:58:18 +010080 int c = *t;
81 if (c=='<' || c=='>' || c=='&') {
82 *t = '\0';
83 html(txt);
84 *t = c;
85 if (c=='>')
86 html("&gt;");
87 else if (c=='<')
88 html("&lt;");
89 else if (c=='&')
90 html("&amp;");
91 txt = t+1;
92 }
93 t++;
94 }
95 if (t!=txt) {
96 char c = *t;
97 *t = '\0';
98 html(txt);
99 *t = c;
100 }
101 if (len<0)
102 html("...");
103}
Lars Hjemli0d169ad2006-12-09 15:18:17 +0100104
105void html_attr(char *txt)
106{
107 char *t = txt;
Lars Hjemli27cd3b22006-12-28 01:54:43 +0100108 while(t && *t){
Lars Hjemli0d169ad2006-12-09 15:18:17 +0100109 int c = *t;
110 if (c=='<' || c=='>' || c=='\'') {
111 *t = '\0';
112 html(txt);
113 *t = c;
114 if (c=='>')
115 html("&gt;");
116 else if (c=='<')
117 html("&lt;");
118 else if (c=='\'')
119 html("&quote;");
120 txt = t+1;
121 }
122 t++;
123 }
124 if (t!=txt)
125 html(txt);
126}
127
Lars Hjemlie39d7382006-12-28 02:01:49 +0100128void html_hidden(char *name, char *value)
129{
130 html("<input type='hidden' name='");
131 html_attr(name);
132 html("' value='");
133 html_attr(value);
134 html("'/>");
135}
136
Lars Hjemli6ec5f362007-10-28 12:08:45 +0100137void html_option(char *value, char *text, char *selected_value)
138{
139 html("<option value='");
140 html_attr(value);
141 html("'");
142 if (selected_value && !strcmp(selected_value, value))
Lars Hjemli29154832007-11-11 13:04:28 +0100143 html(" selected='selected'");
Lars Hjemli6ec5f362007-10-28 12:08:45 +0100144 html(">");
145 html_txt(text);
146 html("</option>\n");
147}
148
Lars Hjemli0d169ad2006-12-09 15:18:17 +0100149void html_link_open(char *url, char *title, char *class)
150{
151 html("<a href='");
152 html_attr(url);
153 if (title) {
154 html("' title='");
155 html_attr(title);
156 }
157 if (class) {
158 html("' class='");
159 html_attr(class);
160 }
161 html("'>");
162}
163
164void html_link_close(void)
165{
166 html("</a>");
167}
Lars Hjemli6cb326c2006-12-17 23:07:28 +0100168
169void html_fileperm(unsigned short mode)
170{
Lars Hjemlib1f9b9c2008-02-23 22:45:33 +0100171 htmlf("%c%c%c", (mode & 4 ? 'r' : '-'),
Lars Hjemli6cb326c2006-12-17 23:07:28 +0100172 (mode & 2 ? 'w' : '-'), (mode & 1 ? 'x' : '-'));
173}
174
Lars Hjemli5e751282007-05-18 23:56:10 +0200175int html_include(const char *filename)
176{
177 FILE *f;
178 char buf[4096];
179 size_t len;
180
181 if (!(f = fopen(filename, "r")))
182 return -1;
183 while((len = fread(buf, 1, 4096, f)) > 0)
184 write(htmlfd, buf, len);
185 fclose(f);
186 return 0;
187}