5 kx
5 kx #ifdef HAVE_CONFIG_H
5 kx #include <config.h>
5 kx #endif
5 kx
5 kx #include <stdlib.h>
5 kx #include <stdio.h>
5 kx #include <sys/sysinfo.h>
5 kx #include <sys/types.h>
5 kx #ifdef HAVE_INTTYPES_H
5 kx #include <inttypes.h>
5 kx #else
5 kx #include <stdint.h>
5 kx #endif
5 kx #include <stddef.h> /* offsetof(3) */
5 kx #include <dirent.h>
5 kx #include <sys/stat.h> /* chmod(2) */
5 kx #include <sys/file.h>
5 kx #include <sys/mman.h>
5 kx #include <fcntl.h>
5 kx #include <limits.h>
5 kx #include <string.h> /* strdup(3) */
5 kx #include <libgen.h> /* basename(3) */
5 kx #include <ctype.h> /* tolower(3) */
5 kx #include <errno.h>
5 kx #include <time.h>
5 kx #include <sys/time.h>
5 kx #include <pwd.h>
5 kx #include <grp.h>
5 kx #include <stdarg.h>
5 kx #include <locale.h>
5 kx #include <unistd.h>
5 kx
5 kx #define _GNU_SOURCE
5 kx #include <getopt.h>
5 kx
5 kx #include <git2.h>
5 kx
5 kx #include <nls.h>
5 kx
5 kx #include <defs.h>
5 kx #include <cscm/bcf.h>
5 kx
5 kx #include <fatal.h>
5 kx #include <http.h>
5 kx #include <html.h>
5 kx
5 kx #include <dlist.h>
5 kx #include <strbuf.h>
5 kx #include <repolist.h>
5 kx #include <wrapper.h>
5 kx #include <system.h>
5 kx #include <date.h>
5 kx
5 kx #include <ctx.h>
5 kx #include <git-shared.h>
5 kx #include <ui-shared.h>
5 kx #include <ui-repolist.h>
5 kx #include <ui-file.h>
5 kx #include <ui-tree.h>
5 kx #include <ui-diff.h>
5 kx #include <ui-blame.h>
5 kx #include <ui-log.h>
5 kx
5 kx
5 kx static void free_resources( void )
5 kx {
5 kx git_libgit2_shutdown();
5 kx if( config ) free_config();
5 kx }
5 kx
5 kx int main( int argc, char *argv[] )
5 kx {
5 kx
5 kx __mctx_init();
5 kx
5 kx setlocale( LC_ALL, "" );
5 kx #if ENABLE_NLS == 1
5 kx bindtextdomain( PACKAGE, LOCALEDIR );
5 kx textdomain( PACKAGE );
5 kx #endif
5 kx
5 kx git_libgit2_init();
5 kx
5 kx cgit_prepare_context();
5 kx
5 kx /************************
5 kx define error handlers:
5 kx */
5 kx system_fatal = fatal_html;
5 kx wrapper_fatal = fatal_html;
5 kx dlist_fatal = fatal_html;
5 kx rlist_fatal = fatal_html;
5 kx http_fatal = fatal_html;
5 kx html_fatal = fatal_html;
5 kx cgit_fatal = fatal_html;
5 kx
5 kx if( !(config = read_config()) )
5 kx {
5 kx fatal_html( "cannot read config file: /dev/shm%s", CGIT_SHM_BCF );
5 kx free_resources();
5 kx exit( 1 );
5 kx }
5 kx
5 kx cgit_parse_query();
5 kx
5 kx cgit_prepare_template_variables();
5 kx
5 kx if( ctx.page.status == 404 )
5 kx {
5 kx cgit_print_404_page();
5 kx cgit_release_template_variables();
5 kx free_resources();
5 kx return 0;
5 kx }
5 kx
5 kx if( !strcmp( ctx.vars.page_type, ptype_repolist ) )
5 kx {
5 kx if( ctx.query.search && *ctx.query.search )
5 kx {
5 kx cgit_search_repo( ctx.query.search );
5 kx }
5 kx cgit_print_repolist_page();
5 kx }
5 kx else if( !strcmp( ctx.vars.page_type, ptype_repo ) )
5 kx {
5 kx if( ctx.query.operation && *ctx.query.operation && !strcmp( ctx.query.operation, "diff" ) )
5 kx {
5 kx cgit_print_diff_page();
5 kx }
5 kx else if( ctx.query.operation && *ctx.query.operation && !strcmp( ctx.query.operation, "log" ) )
5 kx {
5 kx cgit_print_log_page();
5 kx }
5 kx else if( ctx.repo.relative_info.kind == GIT_OBJECT_BLOB )
5 kx {
5 kx if( ctx.query.operation && *ctx.query.operation && !strcmp( ctx.query.operation, "blame" ) )
5 kx {
5 kx cgit_print_blame_page();
5 kx }
5 kx else
5 kx {
5 kx cgit_print_file_page();
5 kx }
5 kx }
5 kx else if( ctx.repo.relative_info.kind == GIT_OBJECT_TREE )
5 kx {
5 kx cgit_print_tree_page();
5 kx }
5 kx else
5 kx {
5 kx cgit_print_repolist_page();
5 kx }
5 kx }
5 kx
5 kx #if 0
5 kx {
5 kx struct strbuf buf = STRBUF_INIT;
5 kx
5 kx strbuf_addf( &buf, " <div class=\"content segment\">\n" );
5 kx strbuf_addf( &buf, " <div class=\"container\">\n" );
5 kx strbuf_addf( &buf, " <div class=\"cgit-main-content\">\n" );
5 kx
5 kx strbuf_addf( &buf, "<pre><code class='language-text'>HTTP_HOST = %s\n", ctx.env.http_host );
5 kx strbuf_addf( &buf, "HTTPS = %s\n", ctx.env.https );
5 kx strbuf_addf( &buf, "NO_HTTP = %s\n", ctx.env.no_http );
5 kx strbuf_addf( &buf, "PATH_INFO = %s\n", ctx.env.path_info );
5 kx strbuf_addf( &buf, "QUERY_STRING = %s\n", ctx.env.query_string );
5 kx strbuf_addf( &buf, "REQUEST_URI = %s\n", ctx.env.request_uri );
5 kx strbuf_addf( &buf, "REQUEST_SCHEME = %s\n", ctx.env.request_scheme );
5 kx strbuf_addf( &buf, "REQUEST_METHOD = %s\n", ctx.env.request_method );
5 kx strbuf_addf( &buf, "SCRIPT_NAME = %s\n", ctx.env.script_name );
5 kx strbuf_addf( &buf, "SERVER_NAME = %s\n", ctx.env.server_name );
5 kx strbuf_addf( &buf, "SERVER_PORT = %s\n", ctx.env.server_port );
5 kx strbuf_addf( &buf, "HTTP_COOKIE = %s\n", ctx.env.http_cookie );
5 kx strbuf_addf( &buf, "CONTENT_LENGTH = %d\n", ctx.env.content_lenght );
5 kx strbuf_addf( &buf, "HTTP_ROOT = %s\n", ctx.env.http_root );
5 kx
5 kx strbuf_addf( &buf, "\naddition:\n" );
5 kx strbuf_addf( &buf, "DOCUMENT_ROOT = %s\n", getenv("DOCUMENT_ROOT") );
5 kx strbuf_addf( &buf, "DOCUMENT_URI = %s\n", getenv("DOCUMENT_URI") );
5 kx strbuf_addf( &buf, "PATH_TRANSLATED = %s\n", getenv("PATH_TRANSLATED") );
5 kx strbuf_addf( &buf, "SERVER_SOFTWARE = %s\n", getenv("SERVER_SOFTWARE") );
5 kx strbuf_addf( &buf, "SERVER_PROTOCOL = %s\n", getenv("SERVER_PROTOCOL") );
5 kx strbuf_addf( &buf, "SERVER_ADDR = %s\n", getenv("SERVER_ADDR") );
5 kx strbuf_addf( &buf, "REMOTE_ADDR = %s\n", getenv("REMOTE_ADDR") );
5 kx strbuf_addf( &buf, "GATEWAY_INTERFACE = %s\n", getenv("GATEWAY_INTERFACE") );
5 kx strbuf_addf( &buf, "SERVER_PROTOCOL = %s\n", getenv("SERVER_PROTOCOL") );
5 kx
5 kx strbuf_addf( &buf, "\nctx:\n" );
5 kx strbuf_addf( &buf, "ctx.vars.page_size = '%s'\n", ctx.vars.page_size );
5 kx strbuf_addf( &buf, "ctx.query.ofs = %d;\n", ctx.query.ofs );
5 kx strbuf_addf( &buf, "ctx.query.rev = '%s';\n", ctx.query.rev );
5 kx strbuf_addf( &buf, "ctx.query.revision = '%s';\n", ctx.query.revision );
5 kx strbuf_addf( &buf, "ctx.query.operation = '%s';\n", ctx.query.operation );
5 kx strbuf_addf( &buf, "ctx.query.search = '%s';\n", ctx.query.search );
5 kx strbuf_addf( &buf, "\n" );
5 kx strbuf_addf( &buf, "ctx.repo.name = %s\n", ctx.repo.name );
5 kx strbuf_addf( &buf, "ctx.repo.git_root = %s\n", ctx.repo.git_root );
5 kx strbuf_addf( &buf, "ctx.repo.repo_root = %s\n", ctx.repo.repo_root );
5 kx strbuf_addf( &buf, "ctx.repo.info = {\n" );
5 kx switch( ctx.repo.info.kind )
5 kx {
5 kx case GIT_OBJECT_TREE:
5 kx strbuf_addf( &buf, " kind = '%s';\n", "tree" );
5 kx break;
5 kx case GIT_OBJECT_BLOB:
5 kx strbuf_addf( &buf, " kind = '%s';\n", "blob" );
5 kx break;
5 kx default:
5 kx strbuf_addf( &buf, " kind = '%s';\n", "unknown" );
5 kx break;
5 kx }
5 kx strbuf_addf( &buf, " revision = '%s';\n", ctx.repo.info.revision );
5 kx strbuf_addf( &buf, " oid = '%s';\n", ctx.repo.info.oid );
5 kx switch( ctx.repo.info.mode )
5 kx {
5 kx case GIT_FILEMODE_TREE:
5 kx strbuf_addf( &buf, " mode = '%s';\n", "tree" );
5 kx break;
5 kx case GIT_FILEMODE_BLOB:
5 kx strbuf_addf( &buf, " mode = '%s';\n", "blob" );
5 kx break;
5 kx case GIT_FILEMODE_BLOB_EXECUTABLE:
5 kx strbuf_addf( &buf, " mode = '%s';\n", "blob executable" );
5 kx break;
5 kx case GIT_FILEMODE_LINK:
5 kx strbuf_addf( &buf, " mode = '%s';\n", "link" );
5 kx break;
5 kx default:
5 kx strbuf_addf( &buf, " mode = '%07o';\n", ctx.repo.info.mode );
5 kx break;
5 kx }
5 kx strbuf_addf( &buf, " author = '%s';\n", ctx.repo.info.author );
5 kx strbuf_addf( &buf, " date = %"PRIdMAX";\n", ctx.repo.info.date );
5 kx strbuf_addf( &buf, " offset = %+05d;\n", ctx.repo.info.offset );
5 kx strbuf_addf( &buf, " mime = '%s';\n", ctx.repo.info.mime );
5 kx strbuf_addf( &buf, " lang = '%s';\n", ctx.repo.info.lang );
5 kx strbuf_addf( &buf, "}\n" );
5 kx
5 kx strbuf_addf( &buf, "ctx.repo.relative_path = %s\n", ctx.repo.relative_path );
5 kx strbuf_addf( &buf, "ctx.repo.relative_info = {\n" );
5 kx switch( ctx.repo.relative_info.kind )
5 kx {
5 kx case GIT_OBJECT_TREE:
5 kx strbuf_addf( &buf, " kind = '%s';\n", "tree" );
5 kx break;
5 kx case GIT_OBJECT_BLOB:
5 kx strbuf_addf( &buf, " kind = '%s';\n", "blob" );
5 kx break;
5 kx default:
5 kx strbuf_addf( &buf, " kind = '%s';\n", "unknown" );
5 kx break;
5 kx }
5 kx strbuf_addf( &buf, " revision = '%s';\n", ctx.repo.relative_info.revision );
5 kx strbuf_addf( &buf, " oid = '%s';\n", ctx.repo.relative_info.oid );
5 kx switch( ctx.repo.relative_info.mode )
5 kx {
5 kx case GIT_FILEMODE_TREE:
5 kx strbuf_addf( &buf, " mode = '%s';\n", "tree" );
5 kx break;
5 kx case GIT_FILEMODE_BLOB:
5 kx strbuf_addf( &buf, " mode = '%s';\n", "blob" );
5 kx break;
5 kx case GIT_FILEMODE_BLOB_EXECUTABLE:
5 kx strbuf_addf( &buf, " mode = '%s';\n", "blob executable" );
5 kx break;
5 kx case GIT_FILEMODE_LINK:
5 kx strbuf_addf( &buf, " mode = '%s';\n", "link" );
5 kx break;
5 kx default:
5 kx strbuf_addf( &buf, " mode = '%07o';\n", ctx.repo.relative_info.mode );
5 kx break;
5 kx }
5 kx strbuf_addf( &buf, " author = '%s';\n", ctx.repo.relative_info.author );
5 kx strbuf_addf( &buf, " date = %"PRIdMAX";\n", ctx.repo.relative_info.date );
5 kx strbuf_addf( &buf, " offset = %+05d;\n", ctx.repo.relative_info.offset );
5 kx strbuf_addf( &buf, " mime = '%s';\n", ctx.repo.relative_info.mime );
5 kx strbuf_addf( &buf, " lang = '%s';\n", ctx.repo.relative_info.lang );
5 kx strbuf_addf( &buf, "}\n" );
5 kx
5 kx strbuf_addf( &buf, "ctx.repo.relative_href = %s\n", ctx.repo.relative_href );
5 kx strbuf_addf( &buf, "ctx.repo.relative_html = %s\n", ctx.repo.relative_html );
5 kx
5 kx strbuf_addf( &buf, "ctx.repo.trunk = %s\n", ctx.repo.trunk );
5 kx strbuf_addf( &buf, "ctx.repo.clone_ro_prefix = %s\n", ctx.repo.clone_ro_prefix );
5 kx strbuf_addf( &buf, "ctx.repo.clone_prefix = %s\n", ctx.repo.clone_prefix );
5 kx strbuf_addf( &buf, "ctx.repo.nbranches = %d\n", ctx.repo.nbranches );
5 kx strbuf_addf( &buf, "ctx.repo.ntags = %d\n", ctx.repo.ntags );
5 kx
5 kx strbuf_addf( &buf, "ctx.vers.git = %s\n", ctx.vers.git );
5 kx strbuf_addf( &buf, "ctx.vers.nginx = %s\n", ctx.vers.nginx );
5 kx strbuf_addf( &buf, "ctx.vers.cgit = %s\n", ctx.vers.cgit );
5 kx
5 kx strbuf_addf( &buf, "</code></pre>\n" );
5 kx
5 kx print_config( &buf, config );
5 kx
5 kx strbuf_addf( &buf, " </div> <!-- End of cgit-main-content -->\n" );
5 kx strbuf_addf( &buf, " </div> <!-- End of container -->\n" );
5 kx strbuf_addf( &buf, " </div> <!-- End of content segment -->\n" );
5 kx
5 kx ctx.page.size = buf.len;
5 kx
5 kx strbuf_write( &buf, STDOUT_FILENO );
5 kx strbuf_release( &buf );
5 kx }
5 kx
5 kx #endif
5 kx
5 kx cgit_release_template_variables();
5 kx free_resources();
5 kx
5 kx return 0;
5 kx }