#ifdef HAVE_CONFIG_H #include #endif #include #include #include #include #ifdef HAVE_INTTYPES_H #include #else #include #endif #include /* offsetof(3) */ #include #include /* chmod(2) */ #include #include #include #include #include /* strdup(3) */ #include /* basename(3) */ #include /* tolower(3) */ #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include static int csvn_file_last_changed_revision( const char *relative_path ) { const char *co_prefix = ctx.repo.checkout_ro_prefix; const char *name = ctx.repo.name; const char *repo_root = ctx.repo.repo_root; int ret = 0; if( !relative_path ) return ret; if( co_prefix ) { char repo_path[PATH_MAX] = { 0 }; char cmd[PATH_MAX]; struct strbuf buf = STRBUF_INIT; pid_t p = (pid_t) -1; int rc; if( repo_root && *repo_root ) { strcat( (char *)&repo_path[0], repo_root ); strcat( (char *)&repo_path[0], "/" ); } strcat( (char *)&repo_path[0], name ); snprintf( (char *)&cmd[0], 1024, "svn info --show-item last-changed-revision --no-newline %s/%s/%s 2>/dev/null", co_prefix, (char *)&repo_path[0], relative_path ); p = sys_exec_command( &buf, cmd ); rc = sys_wait_command( p, NULL ); if( rc != 0 ) { return ret; } strbuf_trim( &buf ); ret = atoi( buf.buf ); strbuf_release( &buf ); } return ret; } static void csvn_print_file_links( struct strbuf *sb, const char *relative_path, int revision, int top ) { const char *query_string = NULL; const char *place = NULL; char rev[80] = { 0 }; if( !sb || !relative_path ) return; if( top ) place = "top"; else place = "bottom"; strbuf_addf( sb, "
\n", place ); strbuf_addf( sb, "
\n" ); query_string = ctx_remove_query_param( ctx.env.query_string, "rev" ); query_string = ctx_remove_query_param( query_string, "op" ); if( !revision ) revision = csvn_file_last_changed_revision( relative_path ); sprintf( (char *)&rev[0], "%d", revision ); if( query_string && *query_string ) { if( ctx.repo.repo_root && *ctx.repo.repo_root ) { strbuf_addf( sb, " \n", ctx.repo.repo_root, ctx.repo.name, relative_path, (char *)&rev[0], query_string ); strbuf_addf( sb, " \n", ctx.repo.repo_root, ctx.repo.name, relative_path, (char *)&rev[0], query_string ); strbuf_addf( sb, " \n", ctx.repo.repo_root, ctx.repo.name, relative_path, (char *)&rev[0], query_string ); } else { strbuf_addf( sb, " \n", ctx.repo.name, relative_path, (char *)&rev[0], query_string ); strbuf_addf( sb, " \n", ctx.repo.name, relative_path, (char *)&rev[0], query_string ); strbuf_addf( sb, " \n", ctx.repo.name, relative_path, (char *)&rev[0], query_string ); } } else { if( ctx.repo.repo_root && *ctx.repo.repo_root ) { strbuf_addf( sb, " \n", ctx.repo.repo_root, ctx.repo.name, relative_path, (char *)&rev[0] ); strbuf_addf( sb, " \n", ctx.repo.repo_root, ctx.repo.name, relative_path, (char *)&rev[0] ); strbuf_addf( sb, " \n", ctx.repo.repo_root, ctx.repo.name, relative_path, (char *)&rev[0] ); } else { strbuf_addf( sb, " \n", ctx.repo.name, relative_path, (char *)&rev[0] ); strbuf_addf( sb, " \n", ctx.repo.name, relative_path, (char *)&rev[0] ); strbuf_addf( sb, " \n", ctx.repo.name, relative_path, (char *)&rev[0] ); } } strbuf_addf( sb, "
\n" ); strbuf_addf( sb, "
\n" ); } static void process_markdown_output( const MD_CHAR *text, MD_SIZE size, void *sb ) { strbuf_add( (struct strbuf *)sb, (const void *)text, (size_t)size ); } static int csvn_write_markdown_content( struct strbuf *sb, const struct strbuf *input ) { unsigned parser_flags = MD_DIALECT_GITHUB; /* | MD_DIALECT_COMMONMARK */ unsigned renderer_flags = MD_FLAG_WIKILINKS; /* | MD_HTML_FLAG_DEBUG */ /********************************* md_html() returns 0 on success: */ return md_html( input->buf, input->len, process_markdown_output, (void *)sb, parser_flags, renderer_flags ); } static void csvn_print_markdown_file( struct strbuf *sb, const char *relative_path, int revision ) { const char *co_prefix = ctx.repo.checkout_ro_prefix; const char *name = ctx.repo.name; const char *repo_root = ctx.repo.repo_root; if( !sb || !relative_path ) return; csvn_print_file_links( sb, relative_path, revision, 1 ); strbuf_addstr( sb, "
" ); if( co_prefix ) { char repo_path[PATH_MAX] = { 0 }; char cmd[PATH_MAX]; struct strbuf buf = STRBUF_INIT; pid_t p = (pid_t) -1; int rc; if( repo_root && *repo_root ) { strcat( (char *)&repo_path[0], repo_root ); strcat( (char *)&repo_path[0], "/" ); } strcat( (char *)&repo_path[0], name ); if( revision ) snprintf( (char *)&cmd[0], 1024, "svn cat --revision %d %s/%s/%s 2>/dev/null", revision, co_prefix, (char *)&repo_path[0], relative_path ); else snprintf( (char *)&cmd[0], 1024, "svn cat %s/%s/%s 2>/dev/null", co_prefix, (char *)&repo_path[0], relative_path ); p = sys_exec_command( &buf, cmd ); rc = sys_wait_command( p, NULL ); if( rc != 0 ) { strbuf_release( &buf ); return; } (void)csvn_write_markdown_content( sb, (const struct strbuf *)&buf ); strbuf_release( &buf ); } strbuf_addstr( sb, "\n
\n" ); csvn_print_file_links( sb, relative_path, revision, 0 ); return; } static void csvn_print_file( struct strbuf *sb, const char *relative_path, int revision, const char *lang ) { const char *co_prefix = ctx.repo.checkout_ro_prefix; const char *name = ctx.repo.name; const char *repo_root = ctx.repo.repo_root; if( !sb || !relative_path ) return; csvn_print_file_links( sb, relative_path, revision, 1 ); if( lang ) strbuf_addf( sb, "
", lang );
  else
    strbuf_addstr( sb, "
" );

  if( co_prefix )
  {
    char repo_path[PATH_MAX] = { 0 };
    char cmd[PATH_MAX];
    struct strbuf buf = STRBUF_INIT;
    char  *raw = NULL;
    pid_t  p = (pid_t) -1;
    int    rc;

    if( repo_root && *repo_root )
    {
      strcat( (char *)&repo_path[0], repo_root );
      strcat( (char *)&repo_path[0], "/" );
    }
    strcat( (char *)&repo_path[0], name );

    if( revision )
      snprintf( (char *)&cmd[0], 1024,
                "svn cat --revision %d %s/%s/%s 2>/dev/null",
                revision, co_prefix, (char *)&repo_path[0], relative_path );
    else
      snprintf( (char *)&cmd[0], 1024,
                "svn cat %s/%s/%s 2>/dev/null",
                co_prefix, (char *)&repo_path[0], relative_path );
    p = sys_exec_command( &buf, cmd );
    rc = sys_wait_command( p, NULL );
    if( rc != 0 )
    {
      strbuf_release( &buf );
      return;
    }

    raw = strbuf_detach( &buf, NULL );
    strbuf_addstr_xml_quoted( &buf, (const char *)raw );
    free( raw );

    strbuf_addbuf( sb, (const struct strbuf *)&buf );
    strbuf_release( &buf );
  }

  strbuf_addstr( sb, "\n
\n" ); csvn_print_file_links( sb, relative_path, revision, 0 ); return; } static void csvn_print_image_file( struct strbuf *sb, const char *relative_path, int revision ) { const char *co_prefix = ctx.repo.checkout_ro_prefix; const char *name = ctx.repo.name; const char *repo_root = ctx.repo.repo_root; if( !sb || !relative_path ) return; if( co_prefix ) { char repo_path[PATH_MAX] = { 0 }; char cmd[PATH_MAX]; struct strbuf buf = STRBUF_INIT; pid_t p = (pid_t) -1; int rc; if( repo_root && *repo_root ) { strcat( (char *)&repo_path[0], repo_root ); strcat( (char *)&repo_path[0], "/" ); } strcat( (char *)&repo_path[0], name ); if( revision ) snprintf( (char *)&cmd[0], 1024, "svn cat --revision %d %s/%s/%s 2>/dev/null", revision, co_prefix, (char *)&repo_path[0], relative_path ); else snprintf( (char *)&cmd[0], 1024, "svn cat %s/%s/%s 2>/dev/null", co_prefix, (char *)&repo_path[0], relative_path ); p = sys_exec_command( &buf, cmd ); rc = sys_wait_command( p, NULL ); if( rc != 0 ) { strbuf_release( &buf ); return; } /**************************************************** This call should terminate the program on success: */ csvn_print_raw_file( &buf, ctx.repo.relative_info.mime ); strbuf_release( &buf ); } return; } void csvn_print_file_page( void ) { FILE *fp; struct strbuf buf = STRBUF_INIT; fp = xfopen( ctx.page.header, "r" ); (void)strbuf_env_fread( &buf, fp ); fclose( fp ); strbuf_addf( &buf, "
\n" ); strbuf_addf( &buf, "
\n" ); strbuf_addf( &buf, "
\n" ); if( ctx.repo.relative_info.kind == KIND_FILE ) { if( !strncmp( ctx.repo.relative_info.mime, "text/", 5 ) ) { if( ctx.repo.name ) { if( ctx.repo.relative_info.lang ) { if( !strcmp( ctx.repo.relative_info.lang, "Markdown" ) ) csvn_print_markdown_file( &buf, ctx.repo.relative_path, ctx.query.rev ); else csvn_print_file( &buf, ctx.repo.relative_path, ctx.query.rev, ctx.repo.relative_info.lang ); } else csvn_print_file( &buf, ctx.repo.relative_path, ctx.query.rev, NULL ); } else { strbuf_addf( &buf, "

Requested resource cannot be shown

\n" ); strbuf_addf( &buf, "

Repository '%s' not found.

\n", ctx.repo.name ); } } else if( !strncmp( ctx.repo.relative_info.mime, "image/", 6 ) ) { /**************************************************** This call should terminate the program on success: */ csvn_print_image_file( &buf, ctx.repo.relative_path, ctx.query.rev ); strbuf_addf( &buf, "

Requested file cannot be shown

\n" ); strbuf_addf( &buf, "

Files with mime type such as '%s' cannot be present.

\n", ctx.repo.relative_info.mime ); } else { strbuf_addf( &buf, "

Requested file cannot be shown

\n" ); strbuf_addf( &buf, "

Files with mime type such as '%s' cannot be present.

\n", ctx.repo.relative_info.mime ); } } else { strbuf_addf( &buf, "

Requested resource cannot be shown

\n" ); strbuf_addf( &buf, "

This page assume plain text files to be present.

\n" ); } strbuf_addf( &buf, "
\n" ); strbuf_addf( &buf, "
\n" ); strbuf_addf( &buf, "
\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 ); }