From a0def2b0eacd5a71df4a6af1deb2067ed7bc1ba0 Mon Sep 17 00:00:00 2001 From: lum Date: Mon, 1 Mar 2021 10:51:14 +0000 Subject: [PATCH] Put the hardcoded '\n' character which is found throughout mg into a buffer specific variable. The diff should not produce any behavourial changes in mg. --- usr.bin/mg/buffer.c | 4 +++- usr.bin/mg/cscope.c | 6 +++--- usr.bin/mg/def.h | 4 +++- usr.bin/mg/dired.c | 7 ++++--- usr.bin/mg/echo.c | 4 ++-- usr.bin/mg/extend.c | 10 ++++++---- usr.bin/mg/fileio.c | 8 ++++---- usr.bin/mg/grep.c | 4 ++-- usr.bin/mg/kbd.c | 4 ++-- usr.bin/mg/line.c | 4 ++-- usr.bin/mg/match.c | 4 ++-- usr.bin/mg/region.c | 12 ++++++------ usr.bin/mg/tty.c | 6 +++--- usr.bin/mg/ttyio.c | 4 ++-- usr.bin/mg/util.c | 7 ++++--- usr.bin/mg/yank.c | 4 ++-- 16 files changed, 50 insertions(+), 42 deletions(-) diff --git a/usr.bin/mg/buffer.c b/usr.bin/mg/buffer.c index ac2a2721eae..42ab09a25bd 100644 --- a/usr.bin/mg/buffer.c +++ b/usr.bin/mg/buffer.c @@ -585,6 +585,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); @@ -1010,7 +1012,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); diff --git a/usr.bin/mg/cscope.c b/usr.bin/mg/cscope.c index 0beb59a2b6e..9ad06e49bc7 100644 --- a/usr.bin/mg/cscope.c +++ b/usr.bin/mg/cscope.c @@ -1,4 +1,4 @@ -/* $OpenBSD: cscope.c,v 1.19 2021/02/28 15:30:35 lum Exp $ */ +/* $OpenBSD: cscope.c,v 1.20 2021/03/01 10:51:14 lum Exp $ */ /* * This file is in the public domain. @@ -215,7 +215,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); } @@ -423,7 +423,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); diff --git a/usr.bin/mg/def.h b/usr.bin/mg/def.h index cb1d67356d4..e3cc9e7cfe4 100644 --- a/usr.bin/mg/def.h +++ b/usr.bin/mg/def.h @@ -1,4 +1,4 @@ -/* $OpenBSD: def.h,v 1.167 2021/02/23 08:10:51 lum Exp $ */ +/* $OpenBSD: def.h,v 1.168 2021/03/01 10:51:14 lum Exp $ */ /* This file is in the public domain. */ @@ -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; diff --git a/usr.bin/mg/dired.c b/usr.bin/mg/dired.c index 0cdd8365a7c..7fa232c8904 100644 --- a/usr.bin/mg/dired.c +++ b/usr.bin/mg/dired.c @@ -1,4 +1,4 @@ -/* $OpenBSD: dired.c,v 1.96 2021/02/26 07:21:23 lum Exp $ */ +/* $OpenBSD: dired.c,v 1.97 2021/03/01 10:51:14 lum Exp $ */ /* This file is in the public domain. */ @@ -698,11 +698,12 @@ d_exec(int space, struct buffer *bp, const char *input, const char *cmd, ...) 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) diff --git a/usr.bin/mg/echo.c b/usr.bin/mg/echo.c index 6966c00b634..de93d454176 100644 --- a/usr.bin/mg/echo.c +++ b/usr.bin/mg/echo.c @@ -1,4 +1,4 @@ -/* $OpenBSD: echo.c,v 1.66 2016/10/24 17:18:42 jasper Exp $ */ +/* $OpenBSD: echo.c,v 1.67 2021/03/01 10:51:14 lum Exp $ */ /* This file is in the public domain. */ @@ -323,7 +323,7 @@ veread(const char *fp, char *buf, size_t nbuf, int flag, va_list ap) 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; diff --git a/usr.bin/mg/extend.c b/usr.bin/mg/extend.c index 0898da96798..4cb08fea1c0 100644 --- a/usr.bin/mg/extend.c +++ b/usr.bin/mg/extend.c @@ -1,4 +1,4 @@ -/* $OpenBSD: extend.c,v 1.71 2019/07/18 15:52:11 lum Exp $ */ +/* $OpenBSD: extend.c,v 1.72 2021/03/01 10:51:14 lum Exp $ */ /* This file is in the public domain. */ /* @@ -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, const char *str) break; case 'n': case 'N': - key.k_chars[i] = '\n'; + key.k_chars[i] = *curbp->b_nlchr; break; case 'r': case 'R': diff --git a/usr.bin/mg/fileio.c b/usr.bin/mg/fileio.c index 567f74c3a54..6f912d2e5cb 100644 --- a/usr.bin/mg/fileio.c +++ b/usr.bin/mg/fileio.c @@ -1,4 +1,4 @@ -/* $OpenBSD: fileio.c,v 1.107 2021/02/23 08:10:51 lum Exp $ */ +/* $OpenBSD: fileio.c,v 1.108 2021/03/01 10:51:14 lum Exp $ */ /* This file is in the public domain. */ @@ -163,11 +163,11 @@ ffputbuf(FILE *ffp, struct buffer *bp, int eobnl) 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 *nbytes) 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); diff --git a/usr.bin/mg/grep.c b/usr.bin/mg/grep.c index 97d22c68a81..016256f64d0 100644 --- a/usr.bin/mg/grep.c +++ b/usr.bin/mg/grep.c @@ -1,4 +1,4 @@ -/* $OpenBSD: grep.c,v 1.48 2019/07/11 18:20:18 lum Exp $ */ +/* $OpenBSD: grep.c,v 1.49 2021/03/01 10:51:14 lum Exp $ */ /* This file is in the public domain */ @@ -216,7 +216,7 @@ compile_mode(const char *name, const char *command) 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); } diff --git a/usr.bin/mg/kbd.c b/usr.bin/mg/kbd.c index 43c72320b09..2015a8b41a0 100644 --- a/usr.bin/mg/kbd.c +++ b/usr.bin/mg/kbd.c @@ -1,4 +1,4 @@ -/* $OpenBSD: kbd.c,v 1.34 2020/02/09 10:13:13 florian Exp $ */ +/* $OpenBSD: kbd.c,v 1.35 2021/03/01 10:51:14 lum Exp $ */ /* This file is in the public domain. */ @@ -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); diff --git a/usr.bin/mg/line.c b/usr.bin/mg/line.c index 527893b119f..5a635ae5440 100644 --- a/usr.bin/mg/line.c +++ b/usr.bin/mg/line.c @@ -1,4 +1,4 @@ -/* $OpenBSD: line.c,v 1.62 2020/07/22 13:22:53 tb Exp $ */ +/* $OpenBSD: line.c,v 1.63 2021/03/01 10:51:14 lum Exp $ */ /* This file is in the public domain. */ @@ -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; } diff --git a/usr.bin/mg/match.c b/usr.bin/mg/match.c index 97985577e78..d35fcfdb66e 100644 --- a/usr.bin/mg/match.c +++ b/usr.bin/mg/match.c @@ -1,4 +1,4 @@ -/* $OpenBSD: match.c,v 1.21 2019/07/02 16:25:39 lum Exp $ */ +/* $OpenBSD: match.c,v 1.22 2021/03/01 10:51:14 lum Exp $ */ /* This file is in the public domain. */ @@ -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 */ diff --git a/usr.bin/mg/region.c b/usr.bin/mg/region.c index bd51fd54825..21c5174f52d 100644 --- a/usr.bin/mg/region.c +++ b/usr.bin/mg/region.c @@ -1,4 +1,4 @@ -/* $OpenBSD: region.c,v 1.38 2019/06/17 11:39:26 lum Exp $ */ +/* $OpenBSD: region.c,v 1.39 2021/03/01 10:51:14 lum Exp $ */ /* This file is in the public domain. */ @@ -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 *buf, int len) 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; diff --git a/usr.bin/mg/tty.c b/usr.bin/mg/tty.c index 5a0cf116a1a..75237af2475 100644 --- a/usr.bin/mg/tty.c +++ b/usr.bin/mg/tty.c @@ -1,4 +1,4 @@ -/* $OpenBSD: tty.c,v 1.37 2020/02/09 10:13:13 florian Exp $ */ +/* $OpenBSD: tty.c,v 1.38 2021/03/01 10:51:14 lum Exp $ */ /* This file is in the public domain. */ @@ -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) diff --git a/usr.bin/mg/ttyio.c b/usr.bin/mg/ttyio.c index f88ce7f8b9e..57c03e40f0f 100644 --- a/usr.bin/mg/ttyio.c +++ b/usr.bin/mg/ttyio.c @@ -1,4 +1,4 @@ -/* $OpenBSD: ttyio.c,v 1.38 2019/06/28 13:35:02 deraadt Exp $ */ +/* $OpenBSD: ttyio.c,v 1.39 2021/03/01 10:51:14 lum Exp $ */ /* This file is in the public domain. */ @@ -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); } diff --git a/usr.bin/mg/util.c b/usr.bin/mg/util.c index 9e3cdf000bc..4d38284f3b8 100644 --- a/usr.bin/mg/util.c +++ b/usr.bin/mg/util.c @@ -1,4 +1,4 @@ -/* $OpenBSD: util.c,v 1.42 2019/06/22 15:38:15 lum Exp $ */ +/* $OpenBSD: util.c,v 1.43 2021/03/01 10:51:14 lum Exp $ */ /* This file is in the public domain. */ @@ -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)"; diff --git a/usr.bin/mg/yank.c b/usr.bin/mg/yank.c index ec8c3890978..2ada0edcbd4 100644 --- a/usr.bin/mg/yank.c +++ b/usr.bin/mg/yank.c @@ -1,4 +1,4 @@ -/* $OpenBSD: yank.c,v 1.14 2015/12/11 20:21:23 mmcc Exp $ */ +/* $OpenBSD: yank.c,v 1.15 2021/03/01 10:51:14 lum Exp $ */ /* This file is in the public domain. */ @@ -239,7 +239,7 @@ yank(int f, int n) isetmark(); i = 0; while ((c = kremove(i)) >= 0) { - if (c == '\n') { + if (c == *curbp->b_nlchr) { if (enewline(FFRAND, 1) == FALSE) return (FALSE); ++nline; -- 2.20.1