aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorDevin J. Pohly <djpohly@gmail.com>2017-10-15 20:35:48 -0500
committerDevin J. Pohly <djpohly@gmail.com>2018-02-25 21:53:24 -0600
commit32d3b1d00f66eda4f5446f3b32cabed2c9a77a40 (patch)
treed7e106917d1db250e23979818616665fb69437ba
parent69e32a61df15787c410a48eaa10a89240c36257d (diff)
downloadst-patched-32d3b1d00f66eda4f5446f3b32cabed2c9a77a40.tar.bz2
st-patched-32d3b1d00f66eda4f5446f3b32cabed2c9a77a40.tar.xz
st-patched-32d3b1d00f66eda4f5446f3b32cabed2c9a77a40.zip
Factor out equivalent code from ttyread/ttysend
The echo-to-terminal portions of ttyread and ttysend were actually doing the same thing. New function twrite() now handles this. The parameter show_ctrl determines whether control characters are shown as "^A". This was the only difference between tputc and techo, and techo is now unused and removed. (This commit should not change st's behaviour.) Signed-off-by: Devin J. Pohly <djpohly@gmail.com>
-rw-r--r--st.c101
-rw-r--r--st.h2
2 files changed, 41 insertions, 62 deletions
diff --git a/st.c b/st.c
index 58f7941..641f896 100644
--- a/st.c
+++ b/st.c
@@ -161,8 +161,8 @@ static void tsetchar(Rune, Glyph *, int, int);
161static void tsetscroll(int, int); 161static void tsetscroll(int, int);
162static void tswapscreen(void); 162static void tswapscreen(void);
163static void tsetmode(int, int, int *, int); 163static void tsetmode(int, int, int *, int);
164static int twrite(const char *, int, int);
164static void tfulldirt(void); 165static void tfulldirt(void);
165static void techo(Rune);
166static void tcontrolcode(uchar ); 166static void tcontrolcode(uchar );
167static void tdectest(char ); 167static void tdectest(char );
168static void tdefutf8(char); 168static void tdefutf8(char);
@@ -254,7 +254,7 @@ xstrdup(char *s)
254} 254}
255 255
256size_t 256size_t
257utf8decode(char *c, Rune *u, size_t clen) 257utf8decode(const char *c, Rune *u, size_t clen)
258{ 258{
259 size_t i, j, len, type; 259 size_t i, j, len, type;
260 Rune udecoded; 260 Rune udecoded;
@@ -768,38 +768,19 @@ ttyread(void)
768{ 768{
769 static char buf[BUFSIZ]; 769 static char buf[BUFSIZ];
770 static int buflen = 0; 770 static int buflen = 0;
771 char *ptr; 771 int written;
772 int charsize; /* size of utf8 char in bytes */
773 Rune unicodep;
774 int ret; 772 int ret;
775 773
776 /* append read bytes to unprocessed bytes */ 774 /* append read bytes to unprocessed bytes */
777 if ((ret = read(cmdfd, buf+buflen, LEN(buf)-buflen)) < 0) 775 if ((ret = read(cmdfd, buf+buflen, LEN(buf)-buflen)) < 0)
778 die("Couldn't read from shell: %s\n", strerror(errno)); 776 die("Couldn't read from shell: %s\n", strerror(errno));
779
780 buflen += ret; 777 buflen += ret;
781 ptr = buf;
782 778
783 for (;;) { 779 written = twrite(buf, buflen, 0);
784 if (IS_SET(MODE_UTF8) && !IS_SET(MODE_SIXEL)) { 780 buflen -= written;
785 /* process a complete utf8 char */
786 charsize = utf8decode(ptr, &unicodep, buflen);
787 if (charsize == 0)
788 break;
789 tputc(unicodep);
790 ptr += charsize;
791 buflen -= charsize;
792
793 } else {
794 if (buflen <= 0)
795 break;
796 tputc(*ptr++ & 0xFF);
797 buflen--;
798 }
799 }
800 /* keep any uncomplete utf8 char for the next call */ 781 /* keep any uncomplete utf8 char for the next call */
801 if (buflen > 0) 782 if (buflen > 0)
802 memmove(buf, ptr, buflen); 783 memmove(buf, buf + written, buflen);
803 784
804 return ret; 785 return ret;
805} 786}
@@ -864,27 +845,9 @@ write_error:
864void 845void
865ttysend(char *s, size_t n) 846ttysend(char *s, size_t n)
866{ 847{
867 int len;
868 char *t, *lim;
869 Rune u;
870
871 ttywrite(s, n); 848 ttywrite(s, n);
872 if (!IS_SET(MODE_ECHO)) 849 if (IS_SET(MODE_ECHO))
873 return; 850 twrite(s, n, 1);
874
875 lim = &s[n];
876 for (t = s; t < lim; t += len) {
877 if (IS_SET(MODE_UTF8) && !IS_SET(MODE_SIXEL)) {
878 len = utf8decode(t, &u, n);
879 } else {
880 u = *t & 0xFF;
881 len = 1;
882 }
883 if (len <= 0)
884 break;
885 techo(u);
886 n -= len;
887 }
888} 851}
889 852
890void 853void
@@ -2032,22 +1995,6 @@ tputtab(int n)
2032} 1995}
2033 1996
2034void 1997void
2035techo(Rune u)
2036{
2037 if (ISCONTROL(u)) { /* control code */
2038 if (u & 0x80) {
2039 u &= 0x7f;
2040 tputc('^');
2041 tputc('[');
2042 } else if (u != '\n' && u != '\r' && u != '\t') {
2043 u ^= 0x40;
2044 tputc('^');
2045 }
2046 }
2047 tputc(u);
2048}
2049
2050void
2051tdefutf8(char ascii) 1998tdefutf8(char ascii)
2052{ 1999{
2053 if (ascii == 'G') 2000 if (ascii == 'G')
@@ -2437,6 +2384,38 @@ check_control_code:
2437 } 2384 }
2438} 2385}
2439 2386
2387int
2388twrite(const char *buf, int buflen, int show_ctrl)
2389{
2390 int charsize;
2391 Rune u;
2392 int n;
2393
2394 for (n = 0; n < buflen; n += charsize) {
2395 if (IS_SET(MODE_UTF8) && !IS_SET(MODE_SIXEL)) {
2396 /* process a complete utf8 char */
2397 charsize = utf8decode(buf + n, &u, buflen - n);
2398 if (charsize == 0)
2399 break;
2400 } else {
2401 u = buf[n] & 0xFF;
2402 charsize = 1;
2403 }
2404 if (show_ctrl && ISCONTROL(u)) {
2405 if (u & 0x80) {
2406 u &= 0x7f;
2407 tputc('^');
2408 tputc('[');
2409 } else if (u != '\n' && u != '\r' && u != '\t') {
2410 u ^= 0x40;
2411 tputc('^');
2412 }
2413 }
2414 tputc(u);
2415 }
2416 return n;
2417}
2418
2440void 2419void
2441tresize(int col, int row) 2420tresize(int col, int row)
2442{ 2421{
diff --git a/st.h b/st.h
index 09473c2..3d9b6e7 100644
--- a/st.h
+++ b/st.h
@@ -209,7 +209,7 @@ void selnormalize(void);
209int selected(int, int); 209int selected(int, int);
210char *getsel(void); 210char *getsel(void);
211 211
212size_t utf8decode(char *, Rune *, size_t); 212size_t utf8decode(const char *, Rune *, size_t);
213size_t utf8encode(Rune, char *); 213size_t utf8encode(Rune, char *);
214 214
215void *xmalloc(size_t); 215void *xmalloc(size_t);