/* shared.c: global vars + some callback functions
*
- * Copyright (C) 2006 Lars Hjemli
+ * Copyright (C) 2006-2014 cgit Development Team <cgit@lists.zx2c4.com>
*
* Licensed under GNU General Public License v2
* (see COPYING for full license text)
int chk_zero(int result, char *msg)
{
if (result != 0)
- die("%s: %s", msg, strerror(errno));
+ die_errno("%s", msg);
return result;
}
int chk_positive(int result, char *msg)
{
if (result <= 0)
- die("%s: %s", msg, strerror(errno));
+ die_errno("%s", msg);
return result;
}
int chk_non_negative(int result, char *msg)
{
if (result < 0)
- die("%s: %s", msg, strerror(errno));
+ die_errno("%s", msg);
return result;
}
ret->enable_remote_branches = ctx.cfg.enable_remote_branches;
ret->enable_subject_links = ctx.cfg.enable_subject_links;
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;
return xstrndup(str, len);
}
+char *ensure_end(const char *str, char c)
+{
+ size_t len = strlen(str);
+ char *result;
+
+ if (len && str[len - 1] == c)
+ return xstrndup(str, len);
+
+ result = xmalloc(len + 2);
+ memcpy(result, str, len);
+ result[len] = '/';
+ result[len + 1] = '\0';
+ return result;
+}
+
+void strbuf_ensure_end(struct strbuf *sb, char c)
+{
+ if (!sb->len || sb->buf[sb->len - 1] != c)
+ strbuf_addch(sb, c);
+}
+
char *strlpart(char *txt, int maxlen)
{
char *result;
return ref;
}
+static void cgit_free_taginfo(struct taginfo *tag)
+{
+ if (tag->tagger)
+ free(tag->tagger);
+ if (tag->tagger_email)
+ free(tag->tagger_email);
+ if (tag->msg)
+ free(tag->msg);
+ free(tag);
+}
+
+static void cgit_free_refinfo(struct refinfo *ref)
+{
+ if (ref->refname)
+ free((char *)ref->refname);
+ switch (ref->object->type) {
+ case OBJ_TAG:
+ cgit_free_taginfo(ref->tag);
+ break;
+ case OBJ_COMMIT:
+ cgit_free_commitinfo(ref->commit);
+ break;
+ }
+ free(ref);
+}
+
+void cgit_free_reflist_inner(struct reflist *list)
+{
+ int i;
+
+ for (i = 0; i < list->count; i++) {
+ cgit_free_refinfo(list->refs[i]);
+ }
+ free(list->refs);
+}
+
int cgit_refs_cb(const char *refname, const unsigned char *sha1, int flags,
void *cb_data)
{
int cgit_parse_snapshots_mask(const char *str)
{
+ struct string_list tokens = STRING_LIST_INIT_DUP;
+ struct string_list_item *item;
const struct cgit_snapshot_format *f;
- static const char *delim = " \t,:/|;";
- int tl, sl, rv = 0;
+ int rv = 0;
/* favor legacy setting */
if (atoi(str))
return 1;
- for (;;) {
- str += strspn(str, delim);
- tl = strcspn(str, delim);
- if (!tl)
- break;
+
+ string_list_split(&tokens, str, ' ', -1);
+ string_list_remove_empty_items(&tokens, 0);
+
+ for_each_string_list_item(item, &tokens) {
for (f = cgit_snapshot_formats; f->suffix; f++) {
- sl = strlen(f->suffix);
- if ((tl == sl && !strncmp(f->suffix, str, tl)) ||
- (tl == sl - 1 && !strncmp(f->suffix + 1, str, tl - 1))) {
+ if (!strcmp(item->string, f->suffix) ||
+ !strcmp(item->string, f->suffix + 1)) {
rv |= f->bit;
break;
}
}
- str += tl;
}
+
+ string_list_clear(&tokens, 0);
return rv;
}
int cgit_open_filter(struct cgit_filter *filter)
{
-
filter->old_stdout = chk_positive(dup(STDOUT_FILENO),
"Unable to duplicate STDOUT");
chk_zero(pipe(filter->pipe_fh), "Unable to create pipe to subprocess");
chk_non_negative(dup2(filter->pipe_fh[0], STDIN_FILENO),
"Unable to use pipe as STDIN");
execvp(filter->cmd, filter->argv);
- die("Unable to exec subprocess %s: %s (%d)", filter->cmd,
- strerror(errno), errno);
+ die_errno("Unable to exec subprocess %s", filter->cmd);
}
close(filter->pipe_fh[0]);
chk_non_negative(dup2(filter->pipe_fh[1], STDOUT_FILENO),
int cgit_close_filter(struct cgit_filter *filter)
{
+ int exit_status;
+
chk_non_negative(dup2(filter->old_stdout, STDOUT_FILENO),
"Unable to restore STDOUT");
close(filter->old_stdout);
if (filter->pid < 0)
return 0;
- waitpid(filter->pid, &filter->exitstatus, 0);
- if (WIFEXITED(filter->exitstatus) && !WEXITSTATUS(filter->exitstatus))
+ waitpid(filter->pid, &exit_status, 0);
+ if (WIFEXITED(exit_status) && !WEXITSTATUS(exit_status))
return 0;
die("Subprocess %s exited abnormally", filter->cmd);
}