]> git.cameronkatri.com Git - cgit.git/blobdiff - shared.c
Generalize doc generation
[cgit.git] / shared.c
index d0973ab8dfa50fd77fadf59d6111c7df5f62c97e..72ac140d605cfe9b0144d757e041e9dfbb07e5ee 100644 (file)
--- a/shared.c
+++ b/shared.c
@@ -59,9 +59,10 @@ struct cgit_repo *cgit_add_repo(const char *url)
        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->max_stats = ctx.cfg.max_stats;
        ret->module_link = ctx.cfg.module_link;
-       ret->readme = NULL;
+       ret->readme = ctx.cfg.readme;
        ret->mtime = -1;
        ret->about_filter = ctx.cfg.about_filter;
        ret->commit_filter = ctx.cfg.commit_filter;
@@ -279,6 +280,10 @@ int cgit_diff_files(const unsigned char *old_sha1,
        if ((file1.ptr && buffer_is_binary(file1.ptr, file1.size)) ||
            (file2.ptr && buffer_is_binary(file2.ptr, file2.size))) {
                *binary = 1;
+               if (file1.size)
+                       free(file1.ptr);
+               if (file2.size)
+                       free(file2.ptr);
                return 0;
        }
 
@@ -293,6 +298,10 @@ int cgit_diff_files(const unsigned char *old_sha1,
        emit_cb.outf = filediff_cb;
        emit_cb.priv = fn;
        xdl_diff(&file1, &file2, &diff_params, &emit_params, &emit_cb);
+       if (file1.size)
+               free(file1.ptr);
+       if (file2.size)
+               free(file2.ptr);
        return 0;
 }
 
@@ -428,3 +437,74 @@ int readfile(const char *path, char **buf, size_t *size)
        close(fd);
        return (*size == st.st_size ? 0 : e);
 }
+
+int is_token_char(char c)
+{
+       return isalnum(c) || c == '_';
+}
+
+/* Replace name with getenv(name), return pointer to zero-terminating char
+ */
+char *expand_macro(char *name, int maxlength)
+{
+       char *value;
+       int len;
+
+       len = 0;
+       value = getenv(name);
+       if (value) {
+               len = strlen(value);
+               if (len > maxlength)
+                       len = maxlength;
+               strncpy(name, value, 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;
+}