cSvn-UI for SVN Repositories

cGit-UI – is a web interface for Subversion (SVN) Repositories. cSvn CGI script is writen in C and therefore it's fast enough

6 Commits   0 Branches   2 Tags

#ifdef HAVE_CONFIG_H
#include <config.h>
#endif

#include <stdlib.h>
#include <stdio.h>
#include <sys/sysinfo.h>
#include <sys/types.h>
#ifdef HAVE_INTTYPES_H
#include <inttypes.h>
#else
#include <stdint.h>
#endif
#include <stddef.h>   /* offsetof(3) */
#include <dirent.h>
#include <sys/stat.h> /* chmod(2)    */
#include <sys/file.h>
#include <sys/mman.h>
#include <fcntl.h>
#include <limits.h>
#include <string.h>   /* strdup(3)   */
#include <libgen.h>   /* basename(3) */
#include <ctype.h>    /* tolower(3)  */
#include <errno.h>
#include <time.h>
#include <sys/time.h>
#include <pwd.h>
#include <grp.h>
#include <stdarg.h>
#include <locale.h>
#include <unistd.h>

#include <nls.h>

#include <defs.h>

#include <fatal.h>
#include <http.h>
#include <html.h>

#include <dlist.h>
#include <strbuf.h>
#include <repolist.h>
#include <wrapper.h>
#include <system.h>
#include <date.h>

#include <ctx.h>
#include <ui-shared.h>
#include <ui-repolist.h>



void csvn_print_direction( struct strbuf *sb, int prev, int next )
{
  strbuf_addf( sb, "              <div class=\"direction\">\n" );
  strbuf_addf( sb, "                <div class=\"row\">\n" );
  strbuf_addf( sb, "                  <div class=\"left col-prev\">\n" );
  strbuf_addf( sb, "                    <div class=\"prev-direction\">\n" );

  if( prev )
  {
    if( ctx.env.query_string && *ctx.env.query_string )
      strbuf_addf( sb, "                      <a href=\"%s?ofs=%d&%s\">&#x226a;&nbsp; Prev</a>\n", ctx.repo.relative_href, prev, ctx.env.query_string );
    else
      strbuf_addf( sb, "                      <a href=\"%s?ofs=%d\">&#x226a;&nbsp; Prev</a>\n", ctx.repo.relative_href, prev );
  }
  else
  {
    if( ctx.env.query_string && *ctx.env.query_string )
      strbuf_addf( sb, "                      <a href=\"%s?%s\">&#x226a;&nbsp; Prev</a>\n", ctx.repo.relative_href, ctx.env.query_string );
    else
      strbuf_addf( sb, "                      <a href=\"%s\">&#x226a;&nbsp; Prev</a>\n", ctx.repo.relative_href );
  }

  strbuf_addf( sb, "                    </div>\n" );
  strbuf_addf( sb, "                  </div>\n" );
  strbuf_addf( sb, "                  <div class=\"right col-next\">\n" );
  strbuf_addf( sb, "                    <div class=\"next-direction\">\n" );

  if( next )
  {
    if( ctx.env.query_string && *ctx.env.query_string )
      strbuf_addf( sb, "                      <a href=\"%s?ofs=%d&%s\">Next &nbsp;&#x226b;</a>\n", ctx.repo.relative_href, next, ctx.env.query_string );
    else
      strbuf_addf( sb, "                      <a href=\"%s?ofs=%d\">Next &nbsp;&#x226b;</a>\n", ctx.repo.relative_href, next );
  }
  else
  {
    if( ctx.env.query_string && *ctx.env.query_string )
      strbuf_addf( sb, "                      <a href=\"%s?%s\">Next &nbsp;&#x226b;</a>\n", ctx.repo.relative_href, ctx.env.query_string );
    else
      strbuf_addf( sb, "                      <a href=\"%s\">Next &nbsp;&#x226b;</a>\n", ctx.repo.relative_href );
  }

  strbuf_addf( sb, "                    </div>\n" );
  strbuf_addf( sb, "                  </div>\n" );
  strbuf_addf( sb, "                </div>\n" );
  strbuf_addf( sb, "              </div>\n" );
}

void csvn_print_repolist_header( struct strbuf *sb )
{
  strbuf_addf( sb, "              <div class=\"repo-list-header\">\n" );
  strbuf_addf( sb, "                <div class=\"row\">\n" );
  strbuf_addf( sb, "                  <div class=\"col-name\">Name</div>\n" );
  strbuf_addf( sb, "                  <div class=\"col-desc\">Description</div>\n" );
  strbuf_addf( sb, "                  <div class=\"col-owner\"><div class=\"repo-owner trunc\">Owner</div></div>\n" );
  strbuf_addf( sb, "                  <div class=\"col-rev\"><div class=\"repo-rev trunc\">Rev</div></div>\n" );
  strbuf_addf( sb, "                  <div class=\"col-idle\"><div class=\"repo-idle trunc\">Idle</div></div>\n" );
  strbuf_addf( sb, "                </div>\n" );
  strbuf_addf( sb, "              </div>\n" );
}

void csvn_print_section_start( struct strbuf *sb, const char *name )
{
  if( !sb ) return;

  strbuf_addf( sb, "              <div class=\"section\">\n" );
  if( name && *name )
    strbuf_addf( sb, "                <div class=\"section-header\">%s</div>\n\n", name );
  else
    strbuf_addf( sb, "                <div class=\"section-header\"></div>\n\n" );
}

void csvn_print_section_stop( struct strbuf *sb )
{
  if( !sb ) return;
  strbuf_addf( sb, "              </div> <!-- end of section -->\n\n" );
}

void csvn_print_repo( struct strbuf *sb, struct repo *repo )
{
  struct variable repo_root   = { (unsigned char *)"repo-root",   { 0 }, DT_PATH   },
                  owner       = { (unsigned char *)"owner",       { 0 }, DT_STRING },
                  description = { (unsigned char *)"description", { 0 }, DT_STRING };
  struct variable *rroot = NULL;
  struct variable *auth = NULL;
  struct variable *desc = NULL;

  struct strbuf buf = STRBUF_INIT;
  size_t revision   = csvn_repo_last_changed_revision( &buf, repo );

  time_t time = 0;

  if( !sb || !repo || !repo->path ) return;

  rroot = lookup( repo, &repo_root );
  auth  = lookup( repo, &owner );
  desc  = lookup( repo, &description );
  time  = csvn_repo_last_changed_time( repo );

  strbuf_addf( sb, "                <div class=\"row\">\n" );
  if( rroot )
    strbuf_addf( sb, "                  <div class=\"col-name\"><a href=\"/%s/%s/\"><div class=\"repo-name\">%s</div></a></div>\n", (const char *)rroot->_v.vptr, repo->path, repo->path );
  else
    strbuf_addf( sb, "                  <div class=\"col-name\"><a href=\"/%s/\"><div class=\"repo-name\">%s</div></a></div>\n", repo->path, repo->path );

  if( desc )
    strbuf_addf( sb, "                  <div class=\"col-desc\"><div onclick=\"trunc(this)\" class=\"repo-desc trunc\">%s</div></div>\n", (const char *)desc->_v.vptr );
  else
    strbuf_addf( sb, "                  <div class=\"col-desc\"><div onclick=\"trunc(this)\" class=\"repo-desc trunc\">none</div></div>\n" );

  if( auth )
    strbuf_addf( sb, "                  <div class=\"col-owner\"><div onclick=\"trunc(this)\" class=\"repo-owner trunc\">%s</div></div>\n", (const char *)auth->_v.vptr );
  else
    strbuf_addf( sb, "                  <div class=\"col-owner\"><div onclick=\"trunc(this)\" class=\"repo-owner trunc\">none</div></div>\n" );

  if( revision )
    strbuf_addf( sb, "                  <div class=\"col-rev\"><div onclick=\"trunc(this)\" class=\"repo-rev trunc\">%s</div></div>\n", buf.buf );
  else
    strbuf_addf( sb, "                  <div class=\"col-rev\"><div onclick=\"trunc(this)\" class=\"repo-rev trunc\">%s</div></div>\n", "0" );

  strbuf_release( &buf );

  if( time != -1 )
  {
    struct strbuf buf = STRBUF_INIT;
    csvn_print_age( &buf, time, 0, TM_YEAR );
    strbuf_addf( sb, "                  <div class=\"col-idle\"><div onclick=\"trunc(this)\" class=\"repo-idle trunc\">%s</div></div>\n", buf.buf );
    strbuf_release( &buf );
  }
  else
    strbuf_addf( sb, "                  <div class=\"col-idle\"><div onclick=\"trunc(this)\" class=\"repo-idle trunc\">%s</div></div>\n", "unknown" );

  strbuf_addf( sb, "                </div>\n\n" );
}


int csvn_print_page_repolist( struct strbuf *sb, struct dlist *config, int start_no, int n )
{
  struct dlist *list = NULL;
  int length = 0;

  if( !config || !sb ) return length;

  list = config;

  if( start_no > 0 )
  {
    list = parent_section_node_repolist_nth( config, start_no );
    {
      struct section *section = (struct section *)list->data;
      struct dlist *rlist = parent_rlist_node_repolist_nth( config, start_no );

      csvn_print_section_start( sb, (const char *)section->name );

      while( rlist )
      {
        struct repo *repo = (struct repo *)rlist->data;

        csvn_print_repo( sb, repo );
        if( n && ++length == n )
        {
          csvn_print_section_stop( sb );
          return length;
        }

        rlist = dlist_next( rlist );
      }
      csvn_print_section_stop( sb );
    }
    list = dlist_next( list );
  }

  while( list )
  {
    struct section *section = (struct section *)list->data;

    if( section->type == ST_REPOS )
    {
      csvn_print_section_start( sb, (const char *)section->name );

      struct dlist *rlist = section->list;
      while( rlist )
      {
        struct repo *repo = (struct repo *)rlist->data;

        csvn_print_repo( sb, repo );
        if( n && ++length == n )
        {
          csvn_print_section_stop( sb );
          return length;
        }

        rlist = dlist_next( rlist );
      }

      csvn_print_section_stop( sb );
    }
    list = dlist_next( list );
  }

  return length;
}


void csvn_print_repolist_page( void )
{
  FILE  *fp;
  struct strbuf buf = STRBUF_INIT;
  int psize = 200, rnum = 0, pos = 0, prev, next;

  fp = xfopen( ctx.page.header, "r" );
  (void)strbuf_env_fread( &buf, fp );
  fclose( fp );

  strbuf_addf( &buf, "        <div class=\"content segment\">\n" );
  strbuf_addf( &buf, "          <div class=\"container\">\n" );
  strbuf_addf( &buf, "            <div class=\"csvn-main-content\">\n" );

  if( ctx.vars.page_size && *ctx.vars.page_size )
    sscanf( ctx.vars.page_size, "%d", &psize );

  if( ctx.vars.num_of_repos && *ctx.vars.num_of_repos )
    sscanf( ctx.vars.num_of_repos, "%d", &rnum );

  if( ctx.query.ofs > 0)       pos = ctx.query.ofs;
  if( ctx.query.ofs > rnum-1 ) pos = rnum - rnum % psize;
  if( pos < 0 )                pos = 0;

  prev = pos - psize;
  if( prev < 0 ) prev = 0;

  next = pos + psize;
  if( next > rnum-1 ) next = rnum - rnum % psize;
  if( next < 0 ) next = 0;

  csvn_print_repolist_header( &buf );
  (void)csvn_print_page_repolist( &buf, config, pos, psize );
  csvn_print_direction( &buf, prev, next );

  strbuf_addf( &buf, "            </div> <!-- End of csvn-main-content -->\n" );
  strbuf_addf( &buf, "          </div> <!-- End of container -->\n" );
  strbuf_addf( &buf, "        </div> <!-- End of content segment -->\n" );

  fp = xfopen( ctx.page.footer, "r" );
  (void)strbuf_env_fread( &buf, fp );
  fclose( fp );

  ctx.page.size = buf.len;
  csvn_print_http_headers();
  strbuf_write( &buf, STDOUT_FILENO );
  strbuf_release( &buf );
}