improve .gph text escaping - stagit-gopher - A git gopher frontend. (mirror)
git clone git://bitreich.org/stagit-gopher/ git://enlrupgkhuxnvlhsf6lc3fziv5h2hhfrinws65d7roiv6bfj7d652fid.onion/stagit-gopher/
Log
Files
Refs
Tags
README
LICENSE
---
commit 3ef0c66de5c3087af621b94db15d28c9054586e1
parent 60eefdd22bc7d6b1a48b00906eb1d94e7322d9ea
Author: Hiltjo Posthuma 
Date:   Sat,  1 Jul 2017 14:56:46 +0200

improve .gph text escaping

- also escape links [ in stagit-gopher-index.
- always prefix dynamic text content with 't'.
- don't escape patch lines starting with 't', it does not need to
  be escaped (it has a " ", "+" or "-" already).
- don't allow newlines in certain fields (gphtext()), this would
  break escaping on lines.
- add a separate gphtextnl() for a multi-line commit message.

Diffstat:
  M stagit-gopher-index.c               |       2 +-
  M stagit-gopher.c                     |      33 ++++++++++++++++++++++++++-----

2 files changed, 29 insertions(+), 6 deletions(-)
---
diff --git a/stagit-gopher-index.c b/stagit-gopher-index.c
@@ -114,7 +114,7 @@ writeheader(FILE *fp)
         char buf[256];
 
         trim(buf, sizeof(buf), description);
-        if (buf[0] == 't')
+        if (buf[0] == 't' || buf[0] == '[')
                 fputc('t', fp);
         fprintf(fp, "%s\n\n", buf);
 
diff --git a/stagit-gopher.c b/stagit-gopher.c
@@ -291,9 +291,10 @@ trim(char *buf, size_t bufsiz, const char *src)
                         for (i = 0; i < 8; i++)
                                 buf[d++] = ' ';
                         break;
-                case '|':
+                case '\r': /* ignore CR */
+                case '|': /* ignore separators here */
+                        break;
                 case '\n':
-                case '\r':
                         buf[d++] = ' ';
                         break;
                 default:
@@ -305,9 +306,9 @@ end:
         buf[d] = '\0';
 }
 
-/* Escape characters in text in geomyidae .gph format */
+/* Escape characters in text in geomyidae .gph format, with newlines */
 void
-gphtext(FILE *fp, const char *s, size_t len)
+gphtextnl(FILE *fp, const char *s, size_t len)
 {
         size_t i, n = 0;
 
@@ -330,6 +331,23 @@ gphtext(FILE *fp, const char *s, size_t len)
         }
 }
 
+/* Escape characters in text in geomyidae .gph format,
+   newlines are ignored */
+void
+gphtext(FILE *fp, const char *s, size_t len)
+{
+        size_t i;
+
+        for (i = 0; *s && i < len; i++) {
+                switch (s[i]) {
+                case '\r':
+                case '\n': break;
+                case '\t': fputs("        ", fp); break;
+                default: fputc(s[i], fp);
+                }
+        }
+}
+
 /* Escape characters in links in geomyidae .gph format */
 void
 gphlink(FILE *fp, const char *s, size_t len)
@@ -425,6 +443,7 @@ printtimeshort(FILE *fp, const git_time *intime)
 void
 writeheader(FILE *fp, const char *title)
 {
+        fputc('t', fp);
         gphtext(fp, title, strlen(title));
         if (title[0] && strippedname[0])
                 fputs(" - ", fp);
@@ -521,7 +540,7 @@ printcommit(FILE *fp, struct commitinfo *ci)
         }
         if (ci->msg) {
                 fputc('\n', fp);
-                gphtext(fp, ci->msg, strlen(ci->msg));
+                gphtextnl(fp, ci->msg, strlen(ci->msg));
                 fputc('\n', fp);
         }
 }
@@ -608,7 +627,9 @@ printshowfile(FILE *fp, struct commitinfo *ci)
                         if (git_patch_get_hunk(&hunk, &nhunklines, patch, j))
                                 break;
 
+                        fputc('t', fp);
                         gphtext(fp, hunk->header, hunk->header_len);
+                        fputc('\n', fp);
 
                         for (k = 0; ; k++) {
                                 if (git_patch_get_line_in_hunk(&line, patch, j, k))
@@ -620,6 +641,7 @@ printshowfile(FILE *fp, struct commitinfo *ci)
                                 else
                                         fputs(" ", fp);
                                 gphtext(fp, line->content, line->content_len);
+                                fputc('\n', fp);
                         }
                 }
         }
@@ -797,6 +819,7 @@ writeblob(git_object *obj, const char *fpath, const char *filename, git_off_t fi
 
         fp = efopen(fpath, "w");
         writeheader(fp, filename);
+        fputc('t', fp);
         gphtext(fp, filename, strlen(filename));
         fprintf(fp, " (%juB)\n", (uintmax_t)filesize);
         fputs("---\n", fp);