]> git.armaanb.net Git - stagit.git/blobdiff - stagit.c
Redirect stdout to correct file.
[stagit.git] / stagit.c
index 20f6b659e018528dc580deb8f4fb04ae027ae47f..9a8ccd0245b1e55af35fad1dd86f3477ec779e22 100644 (file)
--- a/stagit.c
+++ b/stagit.c
@@ -394,42 +394,52 @@ writefooter(FILE *fp)
        fputs("</div>\n</body>\n</html>\n", fp);
 }
 
-void
-syntax_highlight(const char *fpath, FILE *fp, const char *s, size_t len)
+int
+syntax_highlight(const char *filename, FILE *fp, const char *s, size_t len)
 {
+       // Copy STDOUT
+       int stdout_copy = dup(1);
+       // Redirect STDOUT
+       dup2(fileno(fp), 1);
+
        // Ruby script for syntax highlighting.
        FILE *child = popen("./highlight", "w");
-       // Give filename:
-       fprintf(child, "%s\n", fpath);
-       // Give code to highlight:
+       if (child == NULL) {
+               printf("child is null: %s", strerror(errno));
+               exit(1);
+       }
+       // Give filename through STDIN:
+       fprintf(child, "%s\n", filename);
+       // Give code to highlight through STDIN:
+       int lc;
        size_t i;
-       for (i = 0; *s && i < len; s++, i++)
-               fputc(*s, child);
-
-       // Write returned HTML to the HTML file.
-       char c;
-       while ((c = fgetc(child)) != EOF)
-               fputc(c, fp);
+       for (i = 0; *s && i < len; s++, i++) {
+               if (*s == '\n') lc++;
+               fprintf(child, "%c", *s);
+       }
 
        pclose(child);
+       // Give back STDOUT.
+       dup2(stdout_copy, 1);
+       return lc;
 }
 
 int
-writeblobhtml(const char *fpath, FILE *fp, const git_blob *blob)
+writeblobhtml(const char *filename, FILE *fp, const git_blob *blob)
 {
-       size_t n = 0, i, prev;
+       int lc = 0;
        const char *s = git_blob_rawcontent(blob);
        git_off_t len = git_blob_rawsize(blob);
 
        fputs("<div id=\"blob\">\n", fp);
 
        if (len > 0) {
-               syntax_highlight(fpath, fp, s, len);
+               lc = syntax_highlight(filename, fp, s, len);
        }
 
        fputs("</div>\n", fp);
 
-       return n;
+       return lc;
 }
 
 void