aboutsummarylogtreecommitdiffstats
path: root/st.c
diff options
context:
space:
mode:
authorAurélien Aptel2010-11-27 21:19:31 +0100
committerAurélien Aptel2010-11-27 21:19:31 +0100
commita13cd5897a0ca1bba0972d021191cc4670349bf6 (patch)
tree595ab8b20a6c6f10bd6f099b26319b6b41b1c6c8 /st.c
parent2b44fff3f126ef92d1523417b7a7728a5d5c370d (diff)
downloadaur-a13cd5897a0ca1bba0972d021191cc4670349bf6.tar.gz
fix and clean ttyread(). buf wasn't static.
Diffstat (limited to 'st.c')
-rw-r--r--st.c34
1 files changed, 20 insertions, 14 deletions
diff --git a/st.c b/st.c
index e988696e604a..3829e7383620 100644
--- a/st.c
+++ b/st.c
@@ -606,24 +606,31 @@ dump(char c) {
void
ttyread(void) {
- char buf[BUFSIZ], *ptr;
+ static char buf[BUFSIZ];
+ static int buflen = 0;
+ char *ptr;
char s[UTF_SIZ];
- int ret, br;
- static int buflen = 0;
- long u;
+ int charsize; /* size of utf8 char in bytes */
+ long utf8c;
+ int ret;
+ /* append read bytes to unprocessed bytes */
if((ret = read(cmdfd, buf+buflen, LEN(buf)-buflen)) < 0)
die("Couldn't read from shell: %s\n", SERRNO);
- else {
- buflen += ret;
- for(ptr=buf; buflen>=UTF_SIZ||isfullutf8(ptr,buflen); buflen-=br) {
- br = utf8decode(ptr, &u);
- utf8encode(&u, s);
- tputc(s);
- ptr += br;
- }
- memcpy(buf, ptr, buflen);
+
+ /* process every complete utf8 char */
+ buflen += ret;
+ ptr = buf;
+ while(buflen >= UTF_SIZ || isfullutf8(ptr,buflen)) {
+ charsize = utf8decode(ptr, &utf8c);
+ utf8encode(&utf8c, s);
+ tputc(s);
+ ptr += charsize;
+ buflen -= charsize;
}
+
+ /* keep any uncomplete utf8 char for the next call */
+ memcpy(buf, ptr, buflen);
}
void
@@ -1774,7 +1781,6 @@ kpress(XEvent *ev) {
/* 3. X lookup */
default:
if(len > 0) {
- buf[sizeof(buf)-1] = '\0';
if(meta && len == 1)
ttywrite("\033", 1);
ttywrite(buf, len);