diff options
Diffstat (limited to 'st.c')
-rw-r--r-- | st.c | 38 |
1 files changed, 20 insertions, 18 deletions
@@ -415,7 +415,7 @@ static int32_t tdefcolor(int *, int *, int); | |||
415 | static void tdeftran(char); | 415 | static void tdeftran(char); |
416 | static inline int match(uint, uint); | 416 | static inline int match(uint, uint); |
417 | static void ttynew(void); | 417 | static void ttynew(void); |
418 | static void ttyread(void); | 418 | static size_t ttyread(void); |
419 | static void ttyresize(void); | 419 | static void ttyresize(void); |
420 | static void ttysend(char *, size_t); | 420 | static void ttysend(char *, size_t); |
421 | static void ttywrite(const char *, size_t); | 421 | static void ttywrite(const char *, size_t); |
@@ -1464,7 +1464,7 @@ ttynew(void) | |||
1464 | } | 1464 | } |
1465 | } | 1465 | } |
1466 | 1466 | ||
1467 | void | 1467 | size_t |
1468 | ttyread(void) | 1468 | ttyread(void) |
1469 | { | 1469 | { |
1470 | static char buf[BUFSIZ]; | 1470 | static char buf[BUFSIZ]; |
@@ -1489,14 +1489,16 @@ ttyread(void) | |||
1489 | 1489 | ||
1490 | /* keep any uncomplete utf8 char for the next call */ | 1490 | /* keep any uncomplete utf8 char for the next call */ |
1491 | memmove(buf, ptr, buflen); | 1491 | memmove(buf, ptr, buflen); |
1492 | |||
1493 | return ret; | ||
1492 | } | 1494 | } |
1493 | 1495 | ||
1494 | void | 1496 | void |
1495 | ttywrite(const char *s, size_t n) | 1497 | ttywrite(const char *s, size_t n) |
1496 | { | 1498 | { |
1497 | fd_set wfd; | 1499 | fd_set wfd, rfd; |
1498 | struct timespec tv; | ||
1499 | ssize_t r; | 1500 | ssize_t r; |
1501 | size_t lim = 256; | ||
1500 | 1502 | ||
1501 | /* | 1503 | /* |
1502 | * Remember that we are using a pty, which might be a modem line. | 1504 | * Remember that we are using a pty, which might be a modem line. |
@@ -1506,38 +1508,34 @@ ttywrite(const char *s, size_t n) | |||
1506 | */ | 1508 | */ |
1507 | while (n > 0) { | 1509 | while (n > 0) { |
1508 | FD_ZERO(&wfd); | 1510 | FD_ZERO(&wfd); |
1511 | FD_ZERO(&rfd); | ||
1509 | FD_SET(cmdfd, &wfd); | 1512 | FD_SET(cmdfd, &wfd); |
1510 | tv.tv_sec = 0; | 1513 | FD_SET(cmdfd, &rfd); |
1511 | tv.tv_nsec = 0; | ||
1512 | 1514 | ||
1513 | /* Check if we can write. */ | 1515 | /* Check if we can write. */ |
1514 | if (pselect(cmdfd+1, NULL, &wfd, NULL, &tv, NULL) < 0) { | 1516 | if (pselect(cmdfd+1, &rfd, &wfd, NULL, NULL, NULL) < 0) { |
1515 | if (errno == EINTR) | 1517 | if (errno == EINTR) |
1516 | continue; | 1518 | continue; |
1517 | die("select failed: %s\n", strerror(errno)); | 1519 | die("select failed: %s\n", strerror(errno)); |
1518 | } | 1520 | } |
1519 | if(!FD_ISSET(cmdfd, &wfd)) { | 1521 | if (FD_ISSET(cmdfd, &rfd)) |
1520 | /* No, then free some buffer space. */ | 1522 | lim = ttyread(); |
1521 | ttyread(); | 1523 | if (FD_ISSET(cmdfd, &wfd)) { |
1522 | } else { | ||
1523 | /* | 1524 | /* |
1524 | * Only write 256 bytes at maximum. This seems to be a | 1525 | * Only write 256 bytes at maximum. This seems to be a |
1525 | * reasonable value for a serial line. Bigger values | 1526 | * reasonable value for a serial line. Bigger values |
1526 | * might clog the I/O. | 1527 | * might clog the I/O. |
1527 | */ | 1528 | */ |
1528 | r = write(cmdfd, s, (n < 256)? n : 256); | 1529 | if ((r = write(cmdfd, s, (n < 256)? n : 256)) < 0) |
1529 | if (r < 0) { | 1530 | goto write_error; |
1530 | die("write error on tty: %s\n", | ||
1531 | strerror(errno)); | ||
1532 | } | ||
1533 | if (r < n) { | 1531 | if (r < n) { |
1534 | /* | 1532 | /* |
1535 | * We weren't able to write out everything. | 1533 | * We weren't able to write out everything. |
1536 | * This means the buffer is getting full | 1534 | * This means the buffer is getting full |
1537 | * again. Empty it. | 1535 | * again. Empty it. |
1538 | */ | 1536 | */ |
1539 | if (n < 256) | 1537 | if (n < lim) |
1540 | ttyread(); | 1538 | lim = ttyread(); |
1541 | n -= r; | 1539 | n -= r; |
1542 | s += r; | 1540 | s += r; |
1543 | } else { | 1541 | } else { |
@@ -1546,6 +1544,10 @@ ttywrite(const char *s, size_t n) | |||
1546 | } | 1544 | } |
1547 | } | 1545 | } |
1548 | } | 1546 | } |
1547 | return; | ||
1548 | |||
1549 | write_error: | ||
1550 | die("write error on tty: %s\n", strerror(errno)); | ||
1549 | } | 1551 | } |
1550 | 1552 | ||
1551 | void | 1553 | void |