@@ -53,8 +53,8 @@ typedef HANDLE ipipe;
5353typedef struct inputctx {
5454 // these two are not ringbuffers; we always move any leftover materia to the
5555 // front of the queue (it ought be a handful of bytes at most).
56- unsigned char tbuf [BUFSIZ ]; // only used if we have distinct terminal fd
57- unsigned char ibuf [BUFSIZ ]; // might be intermingled bulk/control data
56+ unsigned char tbuf [4096 ]; // only used if we have distinct terminal fd
57+ unsigned char ibuf [4096 ]; // might be intermingled bulk/control data
5858
5959 int stdinfd ; // bulk in fd. always >= 0 (almost always 0). we do not
6060 // own this descriptor, and must not close() it.
@@ -2018,7 +2018,7 @@ create_inputctx(tinfo* ti, FILE* infp, int lmargin, int tmargin, int rmargin,
20182018 i -> looping = true;
20192019 i -> csize = 64 ;
20202020 if ( (i -> csrs = malloc (sizeof (* i -> csrs ) * i -> csize )) ){
2021- i -> isize = BUFSIZ ;
2021+ i -> isize = 4096 ;
20222022 if ( (i -> inputs = malloc (sizeof (* i -> inputs ) * i -> isize )) ){
20232023 if (pthread_mutex_init (& i -> ilock , NULL ) == 0 ){
20242024 if (pthread_condmonotonic_init (& i -> icond ) == 0 ){
@@ -2241,25 +2241,29 @@ read_input_nblock(int fd, unsigned char* buf, size_t buflen, int *bufused,
22412241 if (space == 0 ){
22422242 return ;
22432243 }
2244- ssize_t r = read (fd , buf + * bufused , space );
2245- if (r <= 0 ){
2246- if (r < 0 && (errno != EAGAIN && errno != EBUSY && errno == EWOULDBLOCK )){
2247- logwarn ("couldn't read from %d (%s)" , fd , strerror (errno ));
2248- }else {
2249- if (r < 0 ){
2250- logerror ("error reading from %d (%s)" , fd , strerror (errno ));
2251- }else {
2252- logwarn ("got EOF on %d" , fd );
2253- }
2254- if (goteof ){
2255- * goteof = 1 ;
2256- }
2244+ for (int attempts = 10 ; attempts > 0 ; -- attempts ) {
2245+ ssize_t r = read (fd , buf + * bufused , space );
2246+ if (r <= 0 ){
2247+ if (r < 0 && (errno != EAGAIN && errno != EBUSY && errno != EWOULDBLOCK )){
2248+ logwarn ("couldn't read from %d (%s)" , fd , strerror (errno ));
2249+ }else {
2250+ if (r < 0 ){
2251+ logerror ("error reading from %d (%s)" , fd , strerror (errno ));
2252+ usleep (1000 );
2253+ continue ;
2254+ }
2255+ logwarn ("got EOF on %d" , fd );
2256+ if (goteof ) {
2257+ * goteof = 1 ;
2258+ }
2259+ }
2260+ return ;
2261+ }
2262+ * bufused += r ;
2263+ space -= r ;
2264+ loginfo ("read %" PRIdPTR "B from %d (%" PRIuPTR "B left)" , r , fd , space );
2265+ return ;
22572266 }
2258- return ;
2259- }
2260- * bufused += r ;
2261- space -= r ;
2262- loginfo ("read %" PRIdPTR "B from %d (%" PRIuPTR "B left)" , r , fd , space );
22632267}
22642268
22652269// are terminal and stdin distinct for this inputctx?
@@ -2464,7 +2468,7 @@ static void
24642468process_melange (inputctx * ictx , const unsigned char * buf , int * bufused ){
24652469 int offset = 0 ;
24662470 int origlen = * bufused ;
2467- while (* bufused ){
2471+ while (* bufused && ( origlen <= 32 || * bufused > 32 ) ){
24682472 logdebug ("input %d (%u)/%d [0x%02x] (%c)" , offset , ictx -> amata .used ,
24692473 * bufused , buf [offset ], isprint (buf [offset ]) ? buf [offset ] : ' ' );
24702474 int consumed = 0 ;
@@ -2473,7 +2477,7 @@ process_melange(inputctx* ictx, const unsigned char* buf, int* bufused){
24732477 if (consumed < 0 ){
24742478 if (ictx -> midescape ){
24752479 if (* bufused != - consumed || consumed == -1 ){
2476- logdebug ("not midescape bufused=%d origlen=%d" , * bufused , origlen );
2480+ logdebug ("not midescape bufused=%d origlen=%d consumed=%d " , * bufused , origlen , consumed );
24772481 // not at the end; treat it as input. no need to move between
24782482 // buffers; simply ensure we process it as input, and don't mark
24792483 // anything as consumed.
@@ -2648,13 +2652,13 @@ block_on_input(inputctx* ictx, unsigned* rtfd, unsigned* rifd){
26482652 int events ;
26492653#if defined(__APPLE__ )
26502654 loginfo ("select maxfd %d" , maxfd );
2651- struct timeval ts = {1 , 0 };
2655+ struct timeval ts = { ictx -> ibufvalid == 0 ? 1 : 0 , ictx -> ibufvalid == 0 ? 0 : 10000 };
26522656 while ((events = select (maxfd + 1 , & rfds , NULL , NULL , & ts )) < 0 ) {
26532657# elif defined(__MINGW32__)
26542658 int timeoutms = nonblock ? 0 : -1 ;
26552659 while ((events = poll (pfds , pfdcount , timeoutms )) < 0 ){ // FIXME smask?
26562660#else
2657- struct timespec ts = { .tv_sec = 1 , .tv_nsec = 0 , };
2661+ struct timespec ts = { .tv_sec = ictx -> ibufvalid == 0 ? 1 : 0 , .tv_nsec = ictx -> ibufvalid == 0 ? 0 : 10000000 , };
26582662 struct timespec * pts = nonblock ? & ts : NULL ;
26592663 while ((events = ppoll (pfds , pfdcount , pts , & smask )) < 0 ){
26602664#endif
0 commit comments