This diff puts the hardcoded '\n' character which is found throughout mg into a buffer specific variable. The diff should not produce any behavourial
difference in mg, it is only to facilitate other work I am doing on mg.

In order for me to validate that there is no behavioural difference I created some regression tests:

https://mg-regress.s3-eu-west-1.amazonaws.com/regress.dir.007.tgz

If you want to run the regressions tests, the instructions are in the file: regress/mg.regress.tests. The tests are performed by the mg eval-current-buffer function, they are not 'make' style tests. I will add more tests as time allows.

Are there any objections to the diff? Or oks?

Feedback on the tests, or more tests added would be appreciated.

Mark

Index: buffer.c
===================================================================
RCS file: /cvs/src/usr.bin/mg/buffer.c,v
retrieving revision 1.107
diff -u -p -u -p -r1.107 buffer.c
--- buffer.c    24 Jun 2019 14:57:56 -0000      1.107
+++ buffer.c    26 Feb 2021 14:33:43 -0000
@@ -589,6 +589,8 @@ bnew(const char *bname)
        bheadp = bp;
        bp->b_dotline = bp->b_markline = 1;
        bp->b_lines = 1;
+       bp->b_nlseq = "\n";                /* use unix default */
+       bp->b_nlchr = bp->b_nlseq;
        if ((bp->b_bname = strdup(bname)) == NULL) {
                dobeep();
                ewprintf("Can't get %d bytes", strlen(bname) + 1);
@@ -1022,7 +1024,7 @@ diffbuffer(int f, int n)
                        ttext += llength(lp);
                }
                if (lforw(lp) != lpend)         /* no implied \n on last line */
-                       *ttext++ = '\n';
+                       *ttext++ = *curbp->b_nlchr;
        }

        bp = bfind("*Diff*", TRUE);
Index: cscope.c
===================================================================
RCS file: /cvs/src/usr.bin/mg/cscope.c,v
retrieving revision 1.18
diff -u -p -u -p -r1.18 cscope.c
--- cscope.c    3 Jul 2019 03:24:02 -0000       1.18
+++ cscope.c    26 Feb 2021 14:33:43 -0000
@@ -226,7 +226,7 @@ cscreatelist(int f, int n)
        addline(bp, title);
        addline(bp, "");
        while ((len = getline(&line, &sz, fpipe)) != -1) {
-               if (line[len - 1] == '\n')
+               if (line[len - 1] == *bp->b_nlchr)
                        line[len - 1] = '\0';
                addline(bp, line);
        }
@@ -459,7 +459,7 @@ do_cscope(int i)
        addline(bp, "");
        addline(bp, 
"-------------------------------------------------------------------------------");
        while ((len = getline(&buf, &sz, fpipe)) != -1) {
-               if (buf[len - 1] == '\n')
+               if (buf[len - 1] == *bp->b_nlchr)
                        buf[len - 1] = '\0';
                if (addentry(bp, buf) != TRUE) {
                        free(buf);
Index: def.h
===================================================================
RCS file: /cvs/src/usr.bin/mg/def.h,v
retrieving revision 1.167
diff -u -p -u -p -r1.167 def.h
--- def.h       23 Feb 2021 08:10:51 -0000      1.167
+++ def.h       26 Feb 2021 14:33:43 -0000
@@ -267,6 +267,8 @@ struct buffer {
        char             b_flag;        /* Flags                         */
        char             b_fname[NFILEN]; /* File name                   */
        char             b_cwd[NFILEN]; /* working directory             */
+       char            *b_nlseq;       /* Newline sequence of chars     */
+       char            *b_nlchr;       /* 1st newline character         */
        struct fileinfo  b_fi;          /* File attributes               */
        struct undoq     b_undo;        /* Undo actions list             */
        struct undo_rec *b_undoptr;
Index: dired.c
===================================================================
RCS file: /cvs/src/usr.bin/mg/dired.c,v
retrieving revision 1.96
diff -u -p -u -p -r1.96 dired.c
--- dired.c     26 Feb 2021 07:21:23 -0000      1.96
+++ dired.c     26 Feb 2021 14:33:43 -0000
@@ -698,11 +698,12 @@ d_exec(int space, struct buffer *bp, con
                if ((fin = fdopen(fds[0], "r")) == NULL)
                        goto out;
                while (fgets(buf, sizeof(buf), fin) != NULL) {
-                       cp = strrchr(buf, '\n');
+                       cp = strrchr(buf, *bp->b_nlchr);
                        if (cp == NULL && !feof(fin)) { /* too long a line */
                                int c;
                                addlinef(bp, "%*s%s...", space, "", buf);
-                               while ((c = getc(fin)) != EOF && c != '\n')
+                               while ((c = getc(fin)) != EOF &&
+                                   c != *bp->b_nlchr)
                                        ;
                                continue;
                        } else if (cp)
Index: echo.c
===================================================================
RCS file: /cvs/src/usr.bin/mg/echo.c,v
retrieving revision 1.66
diff -u -p -u -p -r1.66 echo.c
--- echo.c      24 Oct 2016 17:18:42 -0000      1.66
+++ echo.c      26 Feb 2021 14:33:43 -0000
@@ -323,7 +323,7 @@ veread(const char *fp, char *buf, size_t
                        break;
                case CCHR('Y'): /* yank from kill buffer */
                        i = 0;
-                       while ((y = kremove(i++)) >= 0 && y != '\n') {
+                       while ((y = kremove(i++)) >= 0 && y != *curbp->b_nlchr) 
{
                                int t;
                                if (dynbuf && epos + 1 >= nbuf) {
                                        void *newp;
Index: extend.c
===================================================================
RCS file: /cvs/src/usr.bin/mg/extend.c,v
retrieving revision 1.71
diff -u -p -u -p -r1.71 extend.c
--- extend.c    18 Jul 2019 15:52:11 -0000      1.71
+++ extend.c    26 Feb 2021 14:33:43 -0000
@@ -42,7 +42,8 @@ insert(int f, int n)
        if (inmacro) {
                while (--n >= 0) {
                        for (count = 0; count < maclcur->l_used; count++) {
-                               if ((((c = maclcur->l_text[count]) == '\n')
+                               if ((((c = maclcur->l_text[count]) ==
+                                   *curbp->b_nlchr)
                                    ? lnewline() : linsert(1, c)) != TRUE)
                                        return (FALSE);
                        }
@@ -61,7 +62,8 @@ insert(int f, int n)
        while (--n >= 0) {
                cp = buf;
                while (*cp) {
-                       if (((*cp == '\n') ? lnewline() : linsert(1, *cp))
+                       if (((*cp == *curbp->b_nlchr) ?
+                           lnewline() : linsert(1, *cp))
                            != TRUE)
                                return (FALSE);
                        cp++;
@@ -434,7 +436,7 @@ dobindkey(KEYMAP *map, const char *func,
                                break;
                        case 'n':
                        case 'N':
-                               key.k_chars[i] = '\n';
+                               key.k_chars[i] = *curbp->b_nlchr;
                                break;
                        case 'r':
                        case 'R':
Index: fileio.c
===================================================================
RCS file: /cvs/src/usr.bin/mg/fileio.c,v
retrieving revision 1.107
diff -u -p -u -p -r1.107 fileio.c
--- fileio.c    23 Feb 2021 08:10:51 -0000      1.107
+++ fileio.c    26 Feb 2021 14:33:43 -0000
@@ -163,11 +163,11 @@ ffputbuf(FILE *ffp, struct buffer *bp, i
                        return (FIOERR);
                }
                if (lforw(lp) != lpend)         /* no implied \n on last line */
-                       putc('\n', ffp);
+                       putc(*bp->b_nlchr, ffp);
        }
        if (eobnl) {
                lnewline_at(lback(lpend), llength(lback(lpend)));
-               putc('\n', ffp);
+               putc(*bp->b_nlchr, ffp);
        }
        return (FIOSUC);
 }
@@ -185,7 +185,7 @@ ffgetline(FILE *ffp, char *buf, int nbuf
        int     c, i;

        i = 0;
-       while ((c = getc(ffp)) != EOF && c != '\n') {
+       while ((c = getc(ffp)) != EOF && c != *curbp->b_nlchr) {
                buf[i++] = c;
                if (i >= nbuf)
                        return (FIOLONG);
Index: grep.c
===================================================================
RCS file: /cvs/src/usr.bin/mg/grep.c,v
retrieving revision 1.48
diff -u -p -u -p -r1.48 grep.c
--- grep.c      11 Jul 2019 18:20:18 -0000      1.48
+++ grep.c      26 Feb 2021 14:33:43 -0000
@@ -216,7 +216,7 @@ compile_mode(const char *name, const cha
                return (NULL);
        }
        while ((len = getline(&buf, &sz, fpipe)) != -1) {
-               if (buf[len - 1] == '\n')
+               if (buf[len - 1] == *bp->b_nlchr)
                        buf[len - 1] = '\0';
                addline(bp, buf);
        }
Index: kbd.c
===================================================================
RCS file: /cvs/src/usr.bin/mg/kbd.c,v
retrieving revision 1.34
diff -u -p -u -p -r1.34 kbd.c
--- kbd.c       9 Feb 2020 10:13:13 -0000       1.34
+++ kbd.c       26 Feb 2021 14:33:43 -0000
@@ -371,7 +371,7 @@ selfinsert(int f, int n)
                }
                thisflag |= CFINS;
        }
-       if (c == '\n') {
+       if (c == *curbp->b_nlchr) {
                do {
                        count = lnewline();
                } while (--n && count == TRUE);
Index: line.c
===================================================================
RCS file: /cvs/src/usr.bin/mg/line.c,v
retrieving revision 1.62
diff -u -p -u -p -r1.62 line.c
--- line.c      22 Jul 2020 13:22:53 -0000      1.62
+++ line.c      26 Feb 2021 14:33:43 -0000
@@ -397,7 +397,7 @@ ldelete(RSIZE n, int kflag)
                        lchange(WFFULL);
                        if (ldelnewline() == FALSE)
                                goto out;
-                       end = strlcat(sv, "\n", len + 1);
+                       end = strlcat(sv, curbp->b_nlchr, len + 1);
                        --n;
                        continue;
                }
Index: match.c
===================================================================
RCS file: /cvs/src/usr.bin/mg/match.c,v
retrieving revision 1.21
diff -u -p -u -p -r1.21 match.c
--- match.c     2 Jul 2019 16:25:39 -0000       1.21
+++ match.c     26 Feb 2021 14:33:43 -0000
@@ -99,7 +99,7 @@ balance(void)
                        cbo = llength(clp) + 1;
                }
                if (--cbo == llength(clp))
-                       c = '\n';               /* end of line          */
+                       c = *curbp->b_nlchr; /* end of line          */
                else
                        c = lgetc(clp, cbo);    /* somewhere in middle  */

Index: region.c
===================================================================
RCS file: /cvs/src/usr.bin/mg/region.c,v
retrieving revision 1.38
diff -u -p -u -p -r1.38 region.c
--- region.c    17 Jun 2019 11:39:26 -0000      1.38
+++ region.c    26 Feb 2021 14:33:43 -0000
@@ -91,7 +91,7 @@ copyregion(int f, int n)

        while (region.r_size--) {
                if (loffs == llength(linep)) {  /* End of line.          */
-                       if ((s = kinsert('\n', KFORW)) != TRUE)
+                       if ((s = kinsert(*curbp->b_nlchr, KFORW)) != TRUE)
                                return (s);
                        linep = lforw(linep);
                        loffs = 0;
@@ -372,7 +372,7 @@ region_get_data(struct region *reg, char
                        if (lp == curbp->b_headp)
                                break;
                        off = 0;
-                       buf[i] = '\n';
+                       buf[i] = *curbp->b_nlchr;
                } else {
                        buf[i] = lgetc(lp, off);
                        off++;
@@ -388,7 +388,7 @@ region_put_data(const char *buf, int len
        int i;

        for (i = 0; buf[i] != '\0' && i < len; i++) {
-               if (buf[i] == '\n')
+               if (buf[i] == *curbp->b_nlchr)
                        lnewline();
                else
                        linsert(1, buf[i]);
@@ -656,7 +656,7 @@ preadin(int fd, struct buffer *bp)

        buf[len] = '\0';
        p = q = buf;
-       if (leftover[0] != '\0' && ((q = strchr(p, '\n')) != NULL)) {
+       if (leftover[0] != '\0' && ((q = strchr(p, *bp->b_nlchr)) != NULL)) {
                *q++ = '\0';
                if (strlcat(leftover, p, sizeof(leftover)) >=
                    sizeof(leftover)) {
@@ -668,7 +668,7 @@ preadin(int fd, struct buffer *bp)
                leftover[0] = '\0';
                p = q;
        }
-       while ((q = strchr(p, '\n')) != NULL) {
+       while ((q = strchr(p, *bp->b_nlchr)) != NULL) {
                *q++ = '\0';
                addline(bp, p);
                p = q;
Index: tty.c
===================================================================
RCS file: /cvs/src/usr.bin/mg/tty.c,v
retrieving revision 1.37
diff -u -p -u -p -r1.37 tty.c
--- tty.c       9 Feb 2020 10:13:13 -0000       1.37
+++ tty.c       26 Feb 2021 14:33:44 -0000
@@ -41,7 +41,7 @@ static int     charcost(const char *);

 static int      cci;
 static int      insdel;        /* Do we have both insert & delete line? */
-static const char      *scroll_fwd;    /* How to scroll forward. */
+static char    *scroll_fwd;    /* How to scroll forward. */

 static void     winchhandler(int);

@@ -78,7 +78,7 @@ ttinit(void)
                /* this is what GNU Emacs does */
                scroll_fwd = parm_down_cursor;
                if (scroll_fwd == NULL || *scroll_fwd == '\0')
-                       scroll_fwd = "\n";
+                       scroll_fwd = curbp->b_nlchr;
        }

        if (cursor_address == NULL || cursor_up == NULL)
Index: ttyio.c
===================================================================
RCS file: /cvs/src/usr.bin/mg/ttyio.c,v
retrieving revision 1.38
diff -u -p -u -p -r1.38 ttyio.c
--- ttyio.c     28 Jun 2019 13:35:02 -0000      1.38
+++ ttyio.c     26 Feb 2021 14:33:44 -0000
@@ -206,7 +206,7 @@ panic(char *s)
        ttclose();
        (void) fputs("panic: ", stderr);
        (void) fputs(s, stderr);
-       (void) fputc('\n', stderr);
+       (void) fputc('\n', stderr);     /* Use '\n' as no buffers now. */
        exit(1);
 }

Index: util.c
===================================================================
RCS file: /cvs/src/usr.bin/mg/util.c,v
retrieving revision 1.42
diff -u -p -u -p -r1.42 util.c
--- util.c      22 Jun 2019 15:38:15 -0000      1.42
+++ util.c      26 Feb 2021 14:33:44 -0000
@@ -56,7 +56,7 @@ showcpos(int f, int n)
                        cchar = nchar + curwp->w_doto;
                        if (curwp->w_doto == llength(clp))
                                /* fake a \n at end of line */
-                               cbyte = '\n';
+                               cbyte = *curbp->b_nlchr;
                        else
                                cbyte = lgetc(clp, curwp->w_doto);
                }
@@ -64,7 +64,8 @@ showcpos(int f, int n)
                nchar += llength(clp);
                clp = lforw(clp);
                if (clp == curbp->b_headp) {
-                       if (cbyte == '\n' && cline == curbp->b_lines) {
+                       if (cbyte == *curbp->b_nlchr &&
+                           cline == curbp->b_lines) {
                                /* swap faked \n for EOB msg */
                                cbyte = EOF;
                                msg = "(EOB)";

Reply via email to