/* shared.c: global vars + some callback functions * * Copyright (C) 2006-2014 cgit Development Team * * Licensed under GNU General Public License v2 * (see COPYING for full license text) */ #include "cgit.h" struct cgit_repolist cgit_repolist; struct cgit_context ctx; int chk_zero(int result, char *msg) { if (result != 0) die_errno("%s", msg); return result; } int chk_positive(int result, char *msg) { if (result <= 0) die_errno("%s", msg); return result; } int chk_non_negative(int result, char *msg) { if (result < 0) die_errno("%s", msg); return result; } char *cgit_default_repo_desc = "[no description]"; struct cgit_repo *cgit_add_repo(const char *url) { struct cgit_repo *ret; if (++cgit_repolist.count > cgit_repolist.length) { if (cgit_repolist.length == 0) cgit_repolist.length = 8; else cgit_repolist.length *= 2; cgit_repolist.repos = xrealloc(cgit_repolist.repos, cgit_repolist.length * sizeof(struct cgit_repo)); } ret = &cgit_repolist.repos[cgit_repolist.count-1]; memset(ret, 0, sizeof(struct cgit_repo)); ret->url = trim_end(url, '/'); ret->name = ret->url; ret->path = NULL; ret->desc = cgit_default_repo_desc; ret->extra_head_content = NULL; ret->owner = NULL; ret->homepage = NULL; ret->section = ctx.cfg.section; ret->snapshots = ctx.cfg.snapshots; ret->enable_commit_graph = ctx.cfg.enable_commit_graph; ret->enable_log_filecount = ctx.cfg.enable_log_filecount; ret->enable_log_linecount = ctx.cfg.enable_log_linecount; ret->enable_remote_branches = ctx.cfg.enable_remote_branches; ret->enable_subject_links = ctx.cfg.enable_subject_links; ret->enable_html_serving = ctx.cfg.enable_html_serving; ret->max_stats = ctx.cfg.max_stats; ret->branch_sort = ctx.cfg.branch_sort; ret->commit_sort = ctx.cfg.commit_sort; ret->module_link = ctx.cfg.module_link; ret->readme = ctx.cfg.readme; ret->mtime = -1; ret->about_filter = ctx.cfg.about_filter; ret->commit_filter = ctx.cfg.commit_filter; ret->source_filter = ctx.cfg.source_filter; ret->email_filter = ctx.cfg.email_filter; ret->owner_filter = ctx.cfg.owner_filter; ret->clone_url = ctx.cfg.clone_url; ret->submodules.strdup_strings = 1; ret->hide = ret->ignore = 0; return ret; } struct cgit_repo *cgit_get_repoinfo(const char *url) { int i; struct cgit_repo *repo; for (i = 0; i < cgit_repolist.count; i++) { repo = &cgit_repolist.repos[i]; if (repo->ignore) continue; if (!strcmp(repo->url, url)) return repo; } return NULL; } void cgit_free_commitinfo(struct commitinfo *info) { free(info->author); free(info->author_email); free(info->committer); free(info->committer_email); free(info->subject); free(info->msg); free(info->msg_encoding); free(info); } char *trim_end(const char *str, char c) { int len; if (str == NULL) return NULL; len = strlen(str); w
#ifndef UI_BLOB_H
#define UI_BLOB_H

extern int cgit_ref_path_exists(const char *path, const char *ref, int file_only);
extern int cgit_print_file(char *path, const char *head, int file_only);
extern void cgit_print_blob(const char *hex, char *path, const char *head, int file_only);

#endif /* UI_BLOB_H */
= errno; close(fd); return e; } if (!S_ISREG(st.st_mode)) { close(fd); return EISDIR; } *buf = xmalloc(st.st_size + 1); *size = read_in_full(fd, *buf, st.st_size); e = errno; (*buf)[*size] = '\0'; close(fd); return (*size == st.st_size ? 0 : e); } static int is_token_char(char c) { return isalnum(c) || c == '_'; } /* Replace name with getenv(name), return pointer to zero-terminating char */ static char *expand_macro(char *name, int maxlength) { char *value; size_t len; len = 0; value = getenv(name); if (value) { len = strlen(value) + 1; if (len > maxlength) len = maxlength; strlcpy(name, value, len); --len; } return name + len; } #define EXPBUFSIZE (1024 * 8) /* Replace all tokens prefixed by '$' in the specified text with the * value of the named environment variable. * NB: the return value is a static buffer, i.e. it must be strdup'd * by the caller. */ char *expand_macros(const char *txt) { static char result[EXPBUFSIZE]; char *p, *start; int len; p = result; start = NULL; while (p < result + EXPBUFSIZE - 1 && txt && *txt) { *p = *txt; if (start) { if (!is_token_char(*txt)) { if (p - start > 0) { *p = '\0'; len = result + EXPBUFSIZE - start - 1; p = expand_macro(start, len) - 1; } start = NULL; txt--; } p++; txt++; continue; } if (*txt == '$') { start = p; txt++; continue; } p++; txt++; } *p = '\0'; if (start && p - start > 0) { len = result + EXPBUFSIZE - start - 1; p = expand_macro(start, len); *p = '\0'; } return result; } char *get_mimetype_for_filename(const char *filename) { char *ext, *mimetype, *token, line[1024], *saveptr; FILE *file; struct string_list_item *mime; if (!filename) return NULL; ext = strrchr(filename, '.'); if (!ext) return NULL; ++ext; if (!ext[0]) return NULL; mime = string_list_lookup(&ctx.cfg.mimetypes, ext); if (mime) return xstrdup(mime->util); if (!ctx.cfg.mimetype_file) return NULL; file = fopen(ctx.cfg.mimetype_file, "r"); if (!file) return NULL; while (fgets(line, sizeof(line), file)) { if (!line[0] || line[0] == '#') continue; mimetype = strtok_r(line, " \t\r\n", &saveptr); while ((token = strtok_r(NULL, " \t\r\n", &saveptr))) { if (!strcasecmp(ext, token)) { fclose(file); return xstrdup(mimetype); } } } fclose(file); return NULL; }