-rw-r--r-- | .gitignore | 1 | ||||
-rw-r--r-- | Makefile | 6 | ||||
-rw-r--r-- | README | 54 | ||||
-rw-r--r-- | cache.c | 86 | ||||
-rw-r--r-- | cgit.c | 117 | ||||
-rw-r--r-- | cgit.h | 47 | ||||
-rw-r--r-- | config.c | 4 | ||||
-rw-r--r-- | git.h | 60 | ||||
-rw-r--r-- | html.c | 6 |
9 files changed, 353 insertions, 28 deletions
@@ -1,3 +1,4 @@ # Files I don't care to see in git-status/commit cgit *.o +*~ @@ -1,20 +1,22 @@ INSTALL_BIN = /var/www/htdocs/cgit.cgi INSTALL_CSS = /var/www/htdocs/cgit.css EXTLIBS = ../git/libgit.a ../git/xdiff/lib.a -lz -lcrypto -OBJECTS = cgit.o config.o html.o +OBJECTS = cgit.o config.o html.o cache.o + +CFLAGS += -Wall all: cgit install: all install cgit $(INSTALL_BIN) install cgit.css $(INSTALL_CSS) clean: rm -f cgit *.o cgit: $(OBJECTS) - $(CC) -o cgit $(OBJECTS) $(EXTLIBS) + $(CC) $(CFLAGS) -o cgit $(OBJECTS) $(EXTLIBS) $(OBJECTS): cgit.h git.h @@ -0,0 +1,54 @@ +Cache algorithm +=============== + +Cgit normally returns cached pages when invoked. If there is no cache file, or +the cache file has expired, it is regenerated. Finally, the cache file is +printed on stdout. + +When it is decided that a cache file needs to be regenerated, an attempt is +made to create a corresponding lockfile. If this fails, the process gives up +and uses the expired cache file instead. + +When there is no cache file for a request, an attempt is made to create a +corresponding lockfile. If this fails, the process calls sched_yield(2) before +restarting the request handling. + +In pseudocode: + + name = generate_cache_name(request); +top: + if (!exists(name)) { + if (lock_cache(name)) { + generate_cache(request, name); + unlock_cache(name); + } else { + sched_yield(); + goto top; + } + } else if (expired(name)) { + if (lock_cache(name)) { + generate_cache(request, name); + unlock_cache(name); + } + } + print_file(name); + + +The following options can be set in /etc/cgitrc to control cache behaviour: + cache-root: root directory for cache files + cache-root-ttl: TTL for the repo listing page + cache-repo-ttl: TTL for any repos summary page + cache-dynamic-ttl: TTL for pages with symbolic references (not SHA1) + cache-static-ttl: TTL for pages with sha1 references + +TTL is specified in minutes, -1 meaning "infinite caching". + + +Naming of cache files +--------------------- +Repository listing: <cachedir>/index.html +Repository summary: <cachedir>/<repo>/index.html +Repository subpage: <cachedir>/<repo>/<page>/<querystring>.html + +The corresponding lock files have a ".lock" suffix. + @@ -0,0 +1,86 @@ +#include "cgit.h" + +const int NOLOCK = -1; + +int cache_lookup(struct cacheitem *item) +{ + if (!cgit_query_repo) { + item->name = xstrdup(fmt("%s/index.html", cgit_cache_root)); + item->ttl = cgit_cache_root_ttl; + } else if (!cgit_query_page) { + item->name = xstrdup(fmt("%s/%s/index.html", cgit_cache_root, + cgit_query_repo)); + item->ttl = cgit_cache_repo_ttl; + } else { + item->name = xstrdup(fmt("%s/%s/%s/%s.html", cgit_cache_root, + cgit_query_repo, cgit_query_page, + cgit_querystring)); + if (cgit_query_has_symref) + item->ttl = cgit_cache_dynamic_ttl; + else if (cgit_query_has_sha1) + item->ttl = cgit_cache_static_ttl; + else + item->ttl = cgit_cache_repo_ttl; + } + if (stat(item->name, &item->st)) { + item->st.st_mtime = 0; + return 0; + } + return 1; +} + +int cache_create_dirs() +{ + char *path; + + if (!cgit_query_repo) + return 0; + + path = fmt("%s/%s", cgit_cache_root, cgit_query_repo); + if (mkdir(path, S_IRWXU) && errno!=EEXIST) + return 0; + + if (cgit_query_page) { + path = fmt("%s/%s/%s", cgit_cache_root, cgit_query_repo, + cgit_query_page); + if (mkdir(path, S_IRWXU) && errno!=EEXIST) + return 0; + } + return 1; +} + +int cache_lock(struct cacheitem *item) +{ + int ret; + char *lockfile = fmt("%s.lock", item->name); + + top: + item->fd = open(lockfile, O_WRONLY | O_CREAT | O_EXCL, S_IRUSR|S_IWUSR); + if (item->fd == NOLOCK && errno == ENOENT && cache_create_dirs()) + goto top; + if (item->fd == NOLOCK && errno == EEXIST) { + struct stat st; + time_t t; + if (stat(lockfile, &st)) + return ret; + t = time(NULL); + if (t-st.st_mtime > cgit_cache_max_create_time && + !unlink(lockfile)) + goto top; + return 0; + } + return (item->fd > 0); +} + +int cache_unlock(struct cacheitem *item) +{ + close(item->fd); + return (rename(fmt("%s.lock", item->name), item->name) == 0); +} + +int cache_expired(struct cacheitem *item) +{ + if (item->ttl < 0) + return 0; + return item->st.st_mtime + item->ttl * 60 < time(NULL); +} @@ -1,51 +1,69 @@ #include "cgit.h" static const char cgit_doctype[] = "<!DOCTYPE html PUBLIC \"-//W3C//DTD XHTML 1.0 Transitional//EN\"\n" " \"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd\">\n"; static const char cgit_error[] = "<div class='error'>%s</div>"; static const char cgit_lib_error[] = "<div class='error'>%s: %s</div>"; +int htmlfd = 0; -char *cgit_root = "/var/git"; +char *cgit_root = "/usr/src/git"; char *cgit_root_title = "Git repository browser"; char *cgit_css = "/cgit.css"; char *cgit_logo = "/git-logo.png"; char *cgit_logo_link = "http://www.kernel.org/pub/software/scm/git/docs/"; char *cgit_virtual_root = NULL; +char *cgit_cache_root = "/var/cache/cgit"; + +int cgit_cache_root_ttl = 5; +int cgit_cache_repo_ttl = 5; +int cgit_cache_dynamic_ttl = 5; +int cgit_cache_static_ttl = -1; +int cgit_cache_max_create_time = 5; + char *cgit_repo_name = NULL; char *cgit_repo_desc = NULL; char *cgit_repo_owner = NULL; +int cgit_query_has_symref = 0; +int cgit_query_has_sha1 = 0; + +char *cgit_querystring = NULL; char *cgit_query_repo = NULL; char *cgit_query_page = NULL; char *cgit_query_head = NULL; +char *cgit_query_sha1 = NULL; + +struct cacheitem cacheitem; int cgit_parse_query(char *txt, configfn fn) { - char *t = txt, *value = NULL, c; + char *t, *value = NULL, c; if (!txt) return 0; + t = txt = xstrdup(txt); + while((c=*t) != '\0') { if (c=='=') { *t = '\0'; value = t+1; } else if (c=='&') { *t = '\0'; (*fn)(txt, value); txt = t+1; value = NULL; } t++; } if (t!=txt) (*fn)(txt, value); return 0; } @@ -69,34 +87,39 @@ void cgit_global_config_cb(const char *name, const char *value) void cgit_repo_config_cb(const char *name, const char *value) { if (!strcmp(name, "name")) cgit_repo_name = xstrdup(value); else if (!strcmp(name, "desc")) cgit_repo_desc = xstrdup(value); else if (!strcmp(name, "owner")) cgit_repo_owner = xstrdup(value); } void cgit_querystring_cb(const char *name, const char *value) { if (!strcmp(name,"r")) cgit_query_repo = xstrdup(value); else if (!strcmp(name, "p")) cgit_query_page = xstrdup(value); - else if (!strcmp(name, "h")) + else if (!strcmp(name, "h")) { cgit_query_head = xstrdup(value); + cgit_query_has_symref = 1; + } else if (!strcmp(name, "id")) { + cgit_query_sha1 = xstrdup(value); + cgit_query_has_sha1 = 1; + } } char *cgit_repourl(const char *reponame) { if (cgit_virtual_root) { return fmt("%s/%s/", cgit_virtual_root, reponame); } else { return fmt("?r=%s", reponame); } } char *cgit_pageurl(const char *reponame, const char *pagename, const char *query) { if (cgit_virtual_root) { return fmt("%s/%s/%s/?%s", cgit_virtual_root, reponame, @@ -123,35 +146,58 @@ static int cgit_print_branch_cb(const char *refname, const unsigned char *sha1, html_link_close(); html("</td><td>"); pretty_print_commit(CMIT_FMT_ONELINE, commit, ~0, buf, sizeof(buf), 0, NULL, NULL, 0); html_txt(buf); html("</td></tr>\n"); } else { html("<tr><td>"); html_txt(buf); html("</td><td>"); htmlf("*** bad ref %s", sha1_to_hex(sha1)); html("</td></tr>\n"); } return 0; } +/* Sun, 06 Nov 1994 08:49:37 GMT */ +static char *http_date(time_t t) +{ + static char day[][4] = {"Sun", "Mon", "Tue", "Wed", "Thu", "Fri", "Sat"}; + static char month[][4] = {"Jan", "Feb", "Mar", "Apr", "May", "Jun", + "Jul", "Aug", "Sep", "Oct", "Now", "Dec"}; + struct tm *tm = gmtime(&t); + return fmt("%s, %02d %s %04d %02d:%02d:%02d GMT", day[tm->tm_wday], + tm->tm_mday, month[tm->tm_mon], 1900+tm->tm_year, + tm->tm_hour, tm->tm_min, tm->tm_sec); +} + +static int ttl_seconds(int ttl) +{ + if (ttl<0) + return 60 * 60 * 24 * 365; + else + return ttl * 60; +} + static void cgit_print_docstart(char *title) { html("Content-Type: text/html; charset=utf-8\n"); + htmlf("Last-Modified: %s\n", http_date(cacheitem.st.st_mtime)); + htmlf("Expires: %s\n", http_date(cacheitem.st.st_mtime + + ttl_seconds(cacheitem.ttl))); html("\n"); html(cgit_doctype); html("<html>\n"); html("<head>\n"); html("<title>"); html_txt(title); html("</title>\n"); html("<link rel='stylesheet' type='text/css' href='"); html_attr(cgit_css); html("'/>\n"); html("</head>\n"); html("<body>\n"); } static void cgit_print_docend() { @@ -162,55 +208,56 @@ static void cgit_print_pageheader(char *title) { html("<div id='header'>"); htmlf("<a href='%s'>", cgit_logo_link); htmlf("<img id='logo' src='%s'/>\n", cgit_logo); htmlf("</a>"); html_txt(title); html("</div>"); } static void cgit_print_repolist() { DIR *d; struct dirent *de; struct stat st; char *name; + chdir(cgit_root); cgit_print_docstart(cgit_root_title); cgit_print_pageheader(cgit_root_title); if (!(d = opendir("."))) { htmlf(cgit_lib_error, "Unable to scan repository directory", strerror(errno)); cgit_print_docend(); return; } html("<h2>Repositories</h2>\n"); html("<table class='list'>"); html("<tr><th>Name</th><th>Description</th><th>Owner</th></tr>\n"); while ((de = readdir(d)) != NULL) { if (de->d_name[0] == '.') continue; if (stat(de->d_name, &st) < 0) continue; if (!S_ISDIR(st.st_mode)) continue; cgit_repo_name = cgit_repo_desc = cgit_repo_owner = NULL; - name = fmt("%s/.git/info/cgit", de->d_name); + name = fmt("%s/info/cgit", de->d_name); if (cgit_read_config(name, cgit_repo_config_cb)) continue; html("<tr><td>"); html_link_open(cgit_repourl(de->d_name), NULL, NULL); html_txt(cgit_repo_name); html_link_close(); html("</td><td>"); html_txt(cgit_repo_desc); html("</td><td>"); html_txt(cgit_repo_owner); html("</td></tr>\n"); } closedir(d); html("</table>"); cgit_print_docend(); @@ -278,33 +325,33 @@ static void cgit_print_commit_shortlog(struct commit *commit) sec = atoi(t); time = gmtime(&sec); } while((len = get_one_line(h)) > 0) h += len+2; h++; len = get_one_line(h); subject = h; html("<tr><td>"); strftime(buf, sizeof(buf), "%Y-%m-%d %H:%M:%S", time); html_txt(buf); html("</td><td>"); - char *qry = fmt("h=%s", sha1_to_hex(commit->object.sha1)); + char *qry = fmt("id=%s", sha1_to_hex(commit->object.sha1)); char *url = cgit_pageurl(cgit_query_repo, "view", qry); html_link_open(url, NULL, NULL); html_txt(subject); html_link_close(); html("</td><td>"); html_txt(author); html("</td></tr>\n"); } static void cgit_print_log(const char *tip, int ofs, int cnt) { struct rev_info rev; struct commit *commit; const char *argv[2] = {NULL, tip}; int n = 0; @@ -358,53 +405,93 @@ static void cgit_print_object(char *hex) buf = read_sha1_file(sha1, type, &size); if (!buf) { htmlf(cgit_error, "Error reading object"); return; } buf[size] = '\0'; html("<h2>Object view</h2>"); htmlf("sha1=%s<br/>type=%s<br/>size=%i<br/>", hex, type, size); html("<pre>"); html_txt(buf); html("</pre>"); } static void cgit_print_repo_page() { - if (chdir(cgit_query_repo) || - cgit_read_config(".git/info/cgit", cgit_repo_config_cb)) { + if (chdir(fmt("%s/%s", cgit_root, cgit_query_repo)) || + cgit_read_config("info/cgit", cgit_repo_config_cb)) { char *title = fmt("%s - %s", cgit_root_title, "Bad request"); cgit_print_docstart(title); cgit_print_pageheader(title); htmlf(cgit_lib_error, "Unable to scan repository", strerror(errno)); cgit_print_docend(); return; } - + setenv("GIT_DIR", fmt("%s/%s", cgit_root, cgit_query_repo), 1); char *title = fmt("%s - %s", cgit_repo_name, cgit_repo_desc); cgit_print_docstart(title); cgit_print_pageheader(title); if (!cgit_query_page) cgit_print_repo_summary(); else if (!strcmp(cgit_query_page, "log")) { cgit_print_log(cgit_query_head, 0, 100); } else if (!strcmp(cgit_query_page, "view")) { - cgit_print_object(cgit_query_head); + cgit_print_object(cgit_query_sha1); } cgit_print_docend(); } -int main(int argc, const char **argv) +static void cgit_fill_cache(struct cacheitem *item) { - if (cgit_read_config("/etc/cgitrc", cgit_global_config_cb)) - die("Error reading config: %d %s", errno, strerror(errno)); - - chdir(cgit_root); - cgit_parse_query(getenv("QUERY_STRING"), cgit_querystring_cb); + htmlfd = item->fd; + item->st.st_mtime = time(NULL); if (cgit_query_repo) cgit_print_repo_page(); else cgit_print_repolist(); +} + +static void cgit_refresh_cache(struct cacheitem *item) +{ + top: + if (!cache_lookup(item)) { + if (cache_lock(item)) { + cgit_fill_cache(item); + cache_unlock(item); + } else { + sched_yield(); + goto top; + } + } else if (cache_expired(item)) { + if (cache_lock(item)) { + cgit_fill_cache(item); + cache_unlock(item); + } + } +} + +static void cgit_print_cache(struct cacheitem *item) +{ + static char buf[4096]; + ssize_t i; + + int fd = open(item->name, O_RDONLY); + if (fd<0) + die("Unable to open cached file %s", item->name); + + while((i=read(fd, buf, sizeof(buf))) > 0) + write(STDOUT_FILENO, buf, i); + + close(fd); +} + +int main(int argc, const char **argv) +{ + cgit_read_config("/etc/cgitrc", cgit_global_config_cb); + cgit_querystring = xstrdup(getenv("QUERY_STRING")); + cgit_parse_query(cgit_querystring, cgit_querystring_cb); + cgit_refresh_cache(&cacheitem); + cgit_print_cache(&cacheitem); return 0; } @@ -1,21 +1,64 @@ #ifndef CGIT_H #define CGIT_H #include "git.h" #include <openssl/sha.h> +#include <ctype.h> +#include <sched.h> + +typedef void (*configfn)(const char *name, const char *value); + +struct cacheitem { + char *name; + struct stat st; + int ttl; + int fd; +}; + +extern char *cgit_root; +extern char *cgit_root_title; +extern char *cgit_css; +extern char *cgit_logo; +extern char *cgit_logo_link; +extern char *cgit_virtual_root; +extern char *cgit_cache_root; + +extern int cgit_cache_root_ttl; +extern int cgit_cache_repo_ttl; +extern int cgit_cache_dynamic_ttl; +extern int cgit_cache_static_ttl; +extern int cgit_cache_max_create_time; + +extern char *cgit_repo_name; +extern char *cgit_repo_desc; +extern char *cgit_repo_owner; + +extern int cgit_query_has_symref; +extern int cgit_query_has_sha1; + +extern char *cgit_querystring; +extern char *cgit_query_repo; +extern char *cgit_query_page; +extern char *cgit_query_head; +extern char *cgit_query_sha1; + +extern int htmlfd; extern char *fmt(const char *format,...); extern void html(const char *txt); extern void htmlf(const char *format,...); extern void html_txt(char *txt); extern void html_attr(char *txt); - extern void html_link_open(char *url, char *title, char *class); extern void html_link_close(void); -typedef void (*configfn)(const char *name, const char *value); extern int cgit_read_config(const char *filename, configfn fn); +extern int cache_lookup(struct cacheitem *item); +extern int cache_lock(struct cacheitem *item); +extern int cache_unlock(struct cacheitem *item); +extern int cache_expired(struct cacheitem *item); + #endif /* CGIT_H */ @@ -19,55 +19,55 @@ void skip_line(FILE *f) while((c=next_char(f)) && c!='\n' && c!=EOF) ; } int read_config_line(FILE *f, char *line, const char **value, int bufsize) { int i = 0, isname = 0; *value = NULL; while(i<bufsize-1) { int c = next_char(f); if (!isname && (c=='#' || c==';')) { skip_line(f); continue; } - if (!isname && isblank(c)) + if (!isname && isspace(c)) continue; if (c=='=' && !*value) { line[i] = 0; *value = &line[i+1]; } else if (c=='\n' && !isname) { i = 0; continue; } else if (c=='\n' || c==EOF) { line[i] = 0; break; } else { line[i]=c; } isname = 1; i++; } line[i+1] = 0; return i; } int cgit_read_config(const char *filename, configfn fn) { int ret = 0, len; char line[256]; const char *value; FILE *f = fopen(filename, "r"); if (!f) return -1; - while(len = read_config_line(f, line, &value, sizeof(line))) + while((len = read_config_line(f, line, &value, sizeof(line))) > 0) (*fn)(line, value); fclose(f); return ret; } @@ -20,32 +20,52 @@ #include <stdio.h> #include <sys/stat.h> #include <fcntl.h> #include <stddef.h> #include <stdlib.h> #include <stdarg.h> #include <string.h> #include <errno.h> #include <limits.h> #include <sys/param.h> #include <netinet/in.h> #include <sys/types.h> #include <dirent.h> #include <time.h> +/* On most systems <limits.h> would have given us this, but + * not on some systems (e.g. GNU/Hurd). + */ +#ifndef PATH_MAX +#define PATH_MAX 4096 +#endif + +#ifdef __GNUC__ +#define NORETURN __attribute__((__noreturn__)) +#else +#define NORETURN +#ifndef __attribute__ +#define __attribute__(x) +#endif +#endif + + +extern void die(const char *err, ...) NORETURN __attribute__((format (printf, 1, 2))); + + static inline char* xstrdup(const char *str) { char *ret = strdup(str); if (!ret) die("Out of memory, strdup failed"); return ret; } static inline void *xmalloc(size_t size) { void *ret = malloc(size); if (!ret && !size) ret = malloc(1); if (!ret) die("Out of memory, malloc failed"); #ifdef XMALLOC_POISON @@ -95,35 +115,39 @@ static inline ssize_t xwrite(int fd, const void *buf, size_t len) return nr; } } /* * from git:cache.h */ /* Convert to/from hex/sha1 representation */ #define MINIMUM_ABBREV 4 #define DEFAULT_ABBREV 7 +extern int sha1_object_info(const unsigned char *, char *, unsigned long *); extern void * read_sha1_file(const unsigned char *sha1, char *type, unsigned long *size); +extern int get_sha1(const char *str, unsigned char *sha1); +extern int get_sha1_hex(const char *hex, unsigned char *sha1); +extern char *sha1_to_hex(const unsigned char *sha1); /* static buffer result! */ /* * from git:object.h */ struct object_list { struct object *item; struct object_list *next; }; struct object_refs { unsigned count; struct object *base; struct object *ref[FLEX_ARRAY]; /* more */ @@ -170,53 +194,64 @@ struct tree { struct commit_list { struct commit *item; struct commit_list *next; }; struct commit { struct object object; void *util; unsigned long date; struct commit_list *parents; struct tree *tree; char *buffer; }; +struct commit *lookup_commit(const unsigned char *sha1); +struct commit *lookup_commit_reference(const unsigned char *sha1); +struct commit *lookup_commit_reference_gently(const unsigned char *sha1, + int quiet); + +int parse_commit_buffer(struct commit *item, void *buffer, unsigned long size); +int parse_commit(struct commit *item); + +struct commit_list * commit_list_insert(struct commit *item, struct commit_list **list_p); +struct commit_list * insert_by_date(struct commit *item, struct commit_list **list); + +void free_commit_list(struct commit_list *list); + +void sort_by_date(struct commit_list **list); + /* Commit formats */ enum cmit_fmt { CMIT_FMT_RAW, CMIT_FMT_MEDIUM, CMIT_FMT_DEFAULT = CMIT_FMT_MEDIUM, CMIT_FMT_SHORT, CMIT_FMT_FULL, CMIT_FMT_FULLER, CMIT_FMT_ONELINE, CMIT_FMT_EMAIL, CMIT_FMT_UNSPECIFIED, }; +extern unsigned long pretty_print_commit(enum cmit_fmt fmt, const struct commit *, unsigned long len, char *buf, unsigned long space, int abbrev, const char *subject, const char *after_subject, int relative_date); -struct commit *lookup_commit(const unsigned char *sha1); -struct commit *lookup_commit_reference(const unsigned char *sha1); -struct commit *lookup_commit_reference_gently(const unsigned char *sha1, - int quiet); - typedef void (*topo_sort_set_fn_t)(struct commit*, void *data); typedef void* (*topo_sort_get_fn_t)(struct commit*); /* * from git:diff.h */ struct rev_info; struct diff_options; struct diff_queue_struct; typedef void (*change_fn_t)(struct diff_options *options, @@ -293,32 +328,42 @@ struct diff_options { }; enum color_diff { DIFF_RESET = 0, DIFF_PLAIN = 1, DIFF_METAINFO = 2, DIFF_FRAGINFO = 3, DIFF_FILE_OLD = 4, DIFF_FILE_NEW = 5, DIFF_COMMIT = 6, DIFF_WHITESPACE = 7, }; +/* + * from git:refs.g + */ + +typedef int each_ref_fn(const char *refname, const unsigned char *sha1, int flags, void *cb_data); +extern int head_ref(each_ref_fn, void *); +extern int for_each_ref(each_ref_fn, void *); +extern int for_each_tag_ref(each_ref_fn, void *); +extern int for_each_branch_ref(each_ref_fn, void *); +extern int for_each_remote_ref(each_ref_fn, void *); /* * from git:revision.h */ struct rev_info; struct log_info; typedef void (prune_fn_t)(struct rev_info *revs, struct commit *commit); struct rev_info { /* Starting list */ struct commit_list *commits; struct object_array pending; @@ -378,22 +423,27 @@ struct rev_info { struct grep_opt *grep_filter; /* special limits */ int max_count; unsigned long max_age; unsigned long min_age; /* diff info for patches and for paths limiting */ struct diff_options diffopt; struct diff_options pruning; topo_sort_set_fn_t topo_setter; topo_sort_get_fn_t topo_getter; }; +extern void init_revisions(struct rev_info *revs, const char *prefix); +extern int setup_revisions(int argc, const char **argv, struct rev_info *revs, const char *def); +extern int handle_revision_arg(const char *arg, struct rev_info *revs,int flags,int cant_be_filename); + +extern void prepare_revision_walk(struct rev_info *revs); extern struct commit *get_revision(struct rev_info *revs); #endif /* GIT_H */ @@ -7,42 +7,44 @@ char *fmt(const char *format, ...) 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])) die("[html.c] string truncated: %s", format); return buf[bufidx]; } void html(const char *txt) { - fputs(txt, stdout); + write(htmlfd, txt, strlen(txt)); } void htmlf(const char *format, ...) { + static char buf[65536]; va_list args; va_start(args, format); - vprintf(format, args); + vsnprintf(buf, sizeof(buf), format, args); va_end(args); + html(buf); } void html_txt(char *txt) { char *t = txt; while(*t){ int c = *t; if (c=='<' || c=='>' || c=='&') { *t = '\0'; html(txt); *t = c; if (c=='>') html(">"); else if (c=='<') html("<"); else if (c=='&') |