From 33201ac65f74e45b4fa60822ba9a538c3cfa9b25 Mon Sep 17 00:00:00 2001 From: "Devin J. Pohly" Date: Thu, 22 Feb 2018 01:05:12 -0600 Subject: Move CRLF input processing into ttywrite This also allows us to remove the crlf field from the Key struct, since the only difference it made was converting "\r" to "\r\n" (which is now done automatically in ttywrite). In addition, MODE_CRLF is no longer referenced from x.c. Signed-off-by: Devin J. Pohly --- st.c | 32 +++++++++++++++++++++++++++++--- 1 file changed, 29 insertions(+), 3 deletions(-) (limited to 'st.c') diff --git a/st.c b/st.c index 7d546da..3ebf8c6 100644 --- a/st.c +++ b/st.c @@ -108,6 +108,7 @@ typedef struct { static void execsh(char **); static void stty(char **); static void sigchld(int); +static void ttywriteraw(const char *, size_t); static void csidump(void); static void csihandle(void); @@ -786,13 +787,38 @@ ttyread(void) void ttywrite(const char *s, size_t n, int may_echo) { - fd_set wfd, rfd; - ssize_t r; - size_t lim = 256; + const char *next; if (may_echo && IS_SET(MODE_ECHO)) twrite(s, n, 1); + if (!IS_SET(MODE_CRLF)) { + ttywriteraw(s, n); + return; + } + + /* This is similar to how the kernel handles ONLCR for ttys */ + while (n > 0) { + if (*s == '\r') { + next = s + 1; + ttywriteraw("\r\n", 2); + } else { + next = memchr(s, '\r', n); + DEFAULT(next, s + n); + ttywriteraw(s, next - s); + } + n -= next - s; + s = next; + } +} + +void +ttywriteraw(const char *s, size_t n) +{ + fd_set wfd, rfd; + ssize_t r; + size_t lim = 256; + /* * Remember that we are using a pty, which might be a modem line. * Writing too much will clog the line. That's why we are doing this -- cgit v1.2.1