all repos — cgit @ 20a33548b9a87a6eb23162ee5d137daa46d78613

a hyperfast web frontend for git written in c

html.c (view raw)

  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 <unistd.h>
 10#include <stdio.h>
 11#include <stdlib.h>
 12#include <stdarg.h>
 13#include <string.h>
 14
 15int htmlfd = STDOUT_FILENO;
 16
 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);
 30	if (len>sizeof(buf[bufidx])) {
 31		fprintf(stderr, "[html.c] string truncated: %s\n", format);
 32		exit(1);
 33	}
 34	return buf[bufidx];
 35}
 36
 37void html(const char *txt)
 38{
 39	write(htmlfd, txt, strlen(txt));
 40}
 41
 42void htmlf(const char *format, ...)
 43{
 44	static char buf[65536];
 45	va_list args;
 46
 47	va_start(args, format);
 48	vsnprintf(buf, sizeof(buf), format, args);
 49	va_end(args);
 50	html(buf);
 51}
 52
 53void html_txt(char *txt)
 54{
 55	char *t = txt;
 56	while(t && *t){
 57		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
 76void html_ntxt(int len, char *txt)
 77{
 78	char *t = txt;
 79	while(t && *t && len--){
 80		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}
104
105void html_attr(char *txt)
106{
107	char *t = txt;
108	while(t && *t){
109		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
128void 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
137void 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))
143		html(" selected='selected'");
144	html(">");
145	html_txt(text);
146	html("</option>\n");
147}
148
149void 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}
168
169void html_fileperm(unsigned short mode)
170{
171	htmlf("%c%c%c", (mode & 4 ? 'r' : '-'),
172	      (mode & 2 ? 'w' : '-'), (mode & 1 ? 'x' : '-'));
173}
174
175int 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}