/* html.c: helper functions for html output * * Copyright (C) 2006 Lars Hjemli * * Licensed under GNU General Public License v2 * (see COPYING for full license text) */ #include #include #include #include #include #include int htmlfd = STDOUT_FILENO; char *fmt(const char *format, ...) { static char buf[8][1024]; static int bufidx; int len; va_list args; bufidx++; bufidx &= 7; va_start(args, format); len = vsnprintf(buf[bufidx], sizeof(buf[bufidx]), format, args); va_end(args); if (len>sizeof(buf[bufidx])) { fprintf(stderr, "[html.c] string truncated: %s\n", format); exit(1); } return buf[bufidx]; } void html_raw(const char *data, size_t size) { write(htmlfd, data, size); } void html(const char *txt) { write(htmlfd, txt, strlen(txt)); } void htmlf(const char *format, ...) { static char buf[65536]; va_list args; va_start(args, format); vsnprintf(buf, sizeof(buf), format, args); va_end(args); html(buf); } void html_status(int code, const char *msg, int more_headers) { htmlf("Status: %d %s\n", code, msg); if (!more_headers) html("\n"); } void html_txt(char *txt) { char *t = txt; while(t && *t){ int c = *t; if (c=='<' || c=='>' || c=='&') { write(htmlfd, txt, t - txt); if (c=='>') html(">"); else if (c=='<') html("<"); else if (c=='&') html("&"); txt = t+1; } t++; } if (t!=txt) html(txt); } void html_ntxt(int len, char *txt) { char *t = txt; while(t && *t && len--){ int c = *t; if (c=='<' || c=='>' || c=='&') { write(htmlfd, txt, t - txt); if (c=='>') html(">"); else if (c=='<') html("<"); else if (c=='&') html("&"); txt = t+1; } t++; } if (t!=txt) write(htmlfd, txt, t - txt); if (len<0) html("..."); } void html_attr(char *txt) { char *t = txt; while(t && *t){ int c = *t; if (c=='<' || c=='>' || c=='\'' || c=='\"') { write(htmlfd, txt, t - txt); if (c=='>') html(">"); else if (c=='<') html("<"); else if (c=='\'') html("'"); else if (c=='"') html("""); txt = t+1; } t++; } if (t!=txt) html(txt); } void html_url_path(char *txt) { char *t = txt; while(t && *t){ int c = *t; if (c=='"' || c=='#' || c=='\'' || c=='?') { write(htmlfd, txt, t - txt); write(htmlfd, fmt("%%%2x", c), 3); txt = t+1; } t++; } if (t!=txt) html(txt); } void html_url_arg(char *txt) { char *t = txt; while(t && *t){ int c = *t; if (c=='"' || c=='#' || c=='%' || c=='&' || c=='\'' || c=='+' || c=='?') { write(htmlfd, txt, t - txt); write(htmlfd, fmt("%%%2x", c), 3); txt = t+1; } t++; } if (t!=txt) html(txt); } void html_hidden(char *name, char *value) { html(""); } void html_option(char *value, char *text, char *selected_value) { html("\n"); } void html_link_open(char *url, char *title, char *class) { html(""); } void html_link_close(void) { html(""); } void html_fileperm(unsigned short mode) { htmlf("%c%c%c", (mode & 4 ? 'r' : '-'), (mode & 2 ? 'w' : '-'), (mode & 1 ? 'x' : '-')); } int html_include(const char *filename) { FILE *f; char buf[4096]; size_t len; if (!(f = fopen(filename, "r"))) { fprintf(stderr, "[cgit] Failed to include file %s: %s (%d).\n", filename, strerror(errno), errno); returJune McEnroe <june@causal.agency>2020-03-02 18:55:35 -0500 commit34daf56d67522d13375b1dca80d313955b85e7c2 (patch) tree5be656896c7fda0f15820411128ed83b85b1f0e7 /Makefile parentReplace .mk files with configure script (diff)downloadli