Move common output-functions into ui-shared.c

While at it, replace the cgit_[lib_]error constants with a proper function

Signed-off-by: Lars Hjemli <hjemli@gmail.com>
This commit is contained in:
Lars Hjemli 2006-12-11 16:38:30 +01:00
parent 51ada4fda2
commit 5a106eb09b
4 changed files with 99 additions and 82 deletions

View File

@ -5,7 +5,7 @@ INSTALL_CSS = /var/www/htdocs/cgit.css
CACHE_ROOT = /var/cache/cgit CACHE_ROOT = /var/cache/cgit
EXTLIBS = ../git/libgit.a ../git/xdiff/lib.a -lz -lcrypto EXTLIBS = ../git/libgit.a ../git/xdiff/lib.a -lz -lcrypto
OBJECTS = parsing.o html.o cache.o OBJECTS = parsing.o html.o cache.o ui-shared.o
CFLAGS += -Wall CFLAGS += -Wall

94
cgit.c
View File

@ -10,16 +10,6 @@
const char cgit_version[] = CGIT_VERSION; const char cgit_version[] = CGIT_VERSION;
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";
const char cgit_error[] =
"<div class='error'>%s</div>";
const char cgit_lib_error[] =
"<div class='error'>%s: %s</div>";
int htmlfd = 0; int htmlfd = 0;
char *cgit_root = "/usr/src/git"; char *cgit_root = "/usr/src/git";
@ -144,63 +134,7 @@ static int cgit_print_branch_cb(const char *refname, const unsigned char *sha1,
return 0; return 0;
} }
/* Sun, 06 Nov 1994 08:49:37 GMT */ static void cgit_print_repolist(struct cacheitem *item)
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");
htmlf("<meta name='generator' content='cgit v%s'/>\n", cgit_version);
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()
{
html("</body>\n</html>\n");
}
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; DIR *d;
struct dirent *de; struct dirent *de;
@ -208,12 +142,12 @@ static void cgit_print_repolist()
char *name; char *name;
chdir(cgit_root); chdir(cgit_root);
cgit_print_docstart(cgit_root_title); cgit_print_docstart(cgit_root_title, item);
cgit_print_pageheader(cgit_root_title); cgit_print_pageheader(cgit_root_title);
if (!(d = opendir("."))) { if (!(d = opendir("."))) {
htmlf(cgit_lib_error, "Unable to scan repository directory", cgit_print_error(fmt("Unable to scan repository directory: %s",
strerror(errno)); strerror(errno)));
cgit_print_docend(); cgit_print_docend();
return; return;
} }
@ -379,18 +313,18 @@ static void cgit_print_object(char *hex)
unsigned long size; unsigned long size;
if (get_sha1_hex(hex, sha1)){ if (get_sha1_hex(hex, sha1)){
htmlf(cgit_error, "Bad hex value"); cgit_print_error(fmt("Bad hex value: %s", hex));
return; return;
} }
if (sha1_object_info(sha1, type, NULL)){ if (sha1_object_info(sha1, type, NULL)){
htmlf(cgit_error, "Bad object name"); cgit_print_error("Bad object name");
return; return;
} }
buf = read_sha1_file(sha1, type, &size); buf = read_sha1_file(sha1, type, &size);
if (!buf) { if (!buf) {
htmlf(cgit_error, "Error reading object"); cgit_print_error("Error reading object");
return; return;
} }
@ -402,21 +336,21 @@ static void cgit_print_object(char *hex)
html("</pre>"); html("</pre>");
} }
static void cgit_print_repo_page() static void cgit_print_repo_page(struct cacheitem *item)
{ {
if (chdir(fmt("%s/%s", cgit_root, cgit_query_repo)) || if (chdir(fmt("%s/%s", cgit_root, cgit_query_repo)) ||
cgit_read_config("info/cgit", cgit_repo_config_cb)) { cgit_read_config("info/cgit", cgit_repo_config_cb)) {
char *title = fmt("%s - %s", cgit_root_title, "Bad request"); char *title = fmt("%s - %s", cgit_root_title, "Bad request");
cgit_print_docstart(title); cgit_print_docstart(title, item);
cgit_print_pageheader(title); cgit_print_pageheader(title);
htmlf(cgit_lib_error, "Unable to scan repository", cgit_print_error(fmt("Unable to scan repository: %s",
strerror(errno)); strerror(errno)));
cgit_print_docend(); cgit_print_docend();
return; return;
} }
setenv("GIT_DIR", fmt("%s/%s", cgit_root, cgit_query_repo), 1); setenv("GIT_DIR", fmt("%s/%s", cgit_root, cgit_query_repo), 1);
char *title = fmt("%s - %s", cgit_repo_name, cgit_repo_desc); char *title = fmt("%s - %s", cgit_repo_name, cgit_repo_desc);
cgit_print_docstart(title); cgit_print_docstart(title, item);
cgit_print_pageheader(title); cgit_print_pageheader(title);
if (!cgit_query_page) if (!cgit_query_page)
cgit_print_repo_summary(); cgit_print_repo_summary();
@ -433,9 +367,9 @@ static void cgit_fill_cache(struct cacheitem *item)
htmlfd = item->fd; htmlfd = item->fd;
item->st.st_mtime = time(NULL); item->st.st_mtime = time(NULL);
if (cgit_query_repo) if (cgit_query_repo)
cgit_print_repo_page(); cgit_print_repo_page(item);
else else
cgit_print_repolist(); cgit_print_repolist(item);
} }
static void cgit_refresh_cache(struct cacheitem *item) static void cgit_refresh_cache(struct cacheitem *item)

9
cgit.h
View File

@ -15,6 +15,8 @@ struct cacheitem {
int fd; int fd;
}; };
extern const char cgit_version[];
extern char *cgit_root; extern char *cgit_root;
extern char *cgit_root_title; extern char *cgit_root_title;
extern char *cgit_css; extern char *cgit_css;
@ -54,7 +56,6 @@ extern void html_attr(char *txt);
extern void html_link_open(char *url, char *title, char *class); extern void html_link_open(char *url, char *title, char *class);
extern void html_link_close(void); extern void html_link_close(void);
extern int cgit_read_config(const char *filename, configfn fn); extern int cgit_read_config(const char *filename, configfn fn);
extern int cgit_parse_query(char *txt, configfn fn); extern int cgit_parse_query(char *txt, configfn fn);
@ -64,4 +65,10 @@ extern int cache_unlock(struct cacheitem *item);
extern int cache_exist(struct cacheitem *item); extern int cache_exist(struct cacheitem *item);
extern int cache_expired(struct cacheitem *item); extern int cache_expired(struct cacheitem *item);
extern void cgit_print_error(char *msg);
extern void cgit_print_docstart(char *title, struct cacheitem *item);
extern void cgit_print_docend();
extern void cgit_print_pageheader(char *title);
#endif /* CGIT_H */ #endif /* CGIT_H */

76
ui-shared.c Normal file
View File

@ -0,0 +1,76 @@
/* ui-shared.c: common web output functions
*
* Copyright (C) 2006 Lars Hjemli
*
* Licensed under GNU General Public License v2
* (see COPYING for full license text)
*/
#include "cgit.h"
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 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;
}
void cgit_print_error(char *msg)
{
html("<div class='error'>");
html_txt(msg);
html("</div>\n");
}
void cgit_print_docstart(char *title, struct cacheitem *item)
{
html("Content-Type: text/html; charset=utf-8\n");
htmlf("Last-Modified: %s\n", http_date(item->st.st_mtime));
htmlf("Expires: %s\n", http_date(item->st.st_mtime +
ttl_seconds(item->ttl)));
html("\n");
html(cgit_doctype);
html("<html>\n");
html("<head>\n");
html("<title>");
html_txt(title);
html("</title>\n");
htmlf("<meta name='generator' content='cgit v%s'/>\n", cgit_version);
html("<link rel='stylesheet' type='text/css' href='");
html_attr(cgit_css);
html("'/>\n");
html("</head>\n");
html("<body>\n");
}
void cgit_print_docend()
{
html("</body>\n</html>\n");
}
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>");
}