if (waitTime == 0) {
if (j<lts->nRows) {
int ltermRead(struct lterms *lts, struct LtermRead *ltr, int timeout){  struct LtermInput *lti = &(lts->ltermInput);  struct LtermOutput *lto = &(lts->ltermOutput);  int waitTime, nfds_all, pollCode, rePoll;  int inputAvailable, newOutputAvailable;  int outOpcodes, outOpvals, outOprow, returnCode;  int j;  LTERM_LOG(ltermRead,20,("start outputMode=%d\n", lto->outputMode));  waitTime = timeout;  if (lts->inputBufRecord) {    /* Complete input record available in buffer */    LTERM_LOG(ltermRead,21,("inputBufRecord=%d\n", lts->inputBufRecord));    waitTime = 0;  } else if ((lto->decodedChars > 0) && !lto->incompleteEscapeSequence) {    /* Do not wait if there are decoded characters to be processed that are       not part of an incomplete ESCAPE sequence */    LTERM_LOG(ltermRead,21,("decodedChars=%d,incompleteEscapeSequence=%d\n",                  lto->decodedChars, lto->incompleteEscapeSequence));    waitTime = 0;  } else if (lto->outputMode == LTERM1_SCREEN_MODE) {    /* Screen mode: do not wait if there are any modified rows */    for (j=0; j<lts->nRows; j++)      if (lto->modifiedCol[j] >= 0) {        waitTime = 0;        break;      }    if (waitTime == 0) {      LTERM_LOG(ltermRead,21,("j=%d, modifiedCol[j] = %d\n",                    j, lto->modifiedCol[j]));    }  }  /* If stream mode, do not poll STDERR output */  if (lto->outputMode == LTERM0_STREAM_MODE)    nfds_all = POLL_COUNT-1;  else    nfds_all = lto->nfds;  do {    /* Do not re-poll unless something special occurs */    rePoll = 0;    /* Default return values in LtermRead structure */    ltr->read_count = 0;    ltr->opcodes = 0;    ltr->opvals = 0;    ltr->buf_row = -1;    ltr->buf_col = -1;    ltr->cursor_row = -1;    ltr->cursor_col = -1;    LTERM_LOG(ltermRead,21,("polling nfds = %d, waitTime = %d\n",                lto->nfds, waitTime));    /* Poll all files for data to read */    pollCode = POLL(lto->pollFD, (SIZE_T) lto->nfds, waitTime);    if (pollCode == -1) {#if defined(DEBUG) && !defined(USE_NSPR_IO)      int errcode = errno;      perror("ltermRead");#else      int errcode = 0;#endif      LTERM_ERROR( "ltermRead: Error return from poll, code=%d\n", errcode);      return -1;    }    /* Do not wait for polling the second time around */    waitTime = 0;    /* Check if input data is available */    inputAvailable = lts->inputBufRecord ||                     ( (pollCode > 0) &&                       (lto->pollFD[POLL_INPUTBUF].POLL_REVENTS != 0) );    /* Check if STDOUT/STDERR data is available */    newOutputAvailable = (pollCode > 0) &&                          ( (lto->pollFD[POLL_STDOUT].POLL_REVENTS != 0) ||                            (lto->pollFD[POLL_STDERR].POLL_REVENTS != 0) );    LTERM_LOG(ltermRead,21,            ("inputAvailable=%d, newOutputAvailable=%d, completionRequest=%d\n",            inputAvailable, newOutputAvailable, lts->completionRequest));    if (inputAvailable && (!newOutputAvailable ||                           (lts->completionRequest == LTERM_NO_COMPLETION)) ) {      /* Process an input data record; either no new output is available, or       * output is available but command completion has not been requested       */      int inOpcodes;      returnCode = ltermWrite(lts, &inOpcodes);      if (returnCode < 0)        return returnCode;      if (inOpcodes != 0) {        /* Echoable input data processed; return input line */        returnCode = ltermReturnInputLine(lts, ltr);        if (returnCode < 0)          return returnCode;        if (inOpcodes & LTERM_META_CODE) {          ltr->opcodes |= LTERM_META_CODE;          if (inOpcodes & LTERM_COMPLETION_CODE)            ltr->opcodes |= LTERM_COMPLETION_CODE;        }        if (inOpcodes & LTERM_NEWLINE_CODE) {          /* Input line break */          ltr->opcodes |= LTERM_NEWLINE_CODE;          if (inOpcodes & LTERM_HIDE_CODE)            ltr->opcodes |= LTERM_HIDE_CODE;          /* Clear input line buffer */          ltermClearInputLine(lts);        }        if (lts->disabledInputEcho) {          /* Input echo is disabled, re-poll */          rePoll = 1;          LTERM_LOG(ltermRead,32,("Input echo disabled; RE-POLLING\n\n"));          continue;        }        return 0;      }      inputAvailable = 0;    }    if (newOutputAvailable) {      /* Read data from STDOUT/STDERR */      int outChars;      outChars = ltermReceiveData(lts, nfds_all==POLL_COUNT);      if (outChars < 0)        return outChars;    }    if (lto->outputMode == LTERM0_STREAM_MODE) {      /* Stream mode */      return ltermReturnStreamData(lts, ltr);    }    /* If there is some complete decoded output, process it */    if ((lto->decodedChars > 0) && !lto->incompleteEscapeSequence) {      returnCode = ltermProcessOutput(lts, &outOpcodes, &outOpvals, &outOprow);      if (returnCode < 0)        return -1;    } else {      /* No new output to process */      if (lto->outputMode == LTERM1_SCREEN_MODE) {        outOpcodes = LTERM_SCREENDATA_CODE;        outOpvals = 0;        outOprow = -1;      } else {        /* No new output in line mode; return nothing */        return 0;      }    }    if (outOpcodes & LTERM_SCREENDATA_CODE) {      /* Return screen mode output */      return ltermReturnScreenData(lts, ltr, outOpcodes, outOpvals, outOprow);    }    if (outOpcodes & LTERM_LINEDATA_CODE) {      LTERM_LOG(ltermRead,32,       ("inputLineBreak=%d, completionRequest=%d, completionChars=%d\n",        lts->inputLineBreak, lts->completionRequest, lts->completionChars));      if (outOpcodes & LTERM_OUTPUT_CODE) {        /* Return output line */        int echoMatch = 0;        returnCode = ltermReturnOutputLine(lts, ltr);        if (returnCode < 0)          return returnCode;        if (lts->inputLineBreak) {          /* Compare output line to initial portion of echo line */          echoMatch = 1;          if ( (ltr->read_count > lts->echoChars) ||               ((ltr->read_count != lts->echoChars) &&                (outOpcodes & LTERM_NEWLINE_CODE)) ) {            echoMatch = 0;          } else {            int k;            for (k=0; k<ltr->read_count; k++) {              if (ltr->buf[k] != lts->echoLine[k]) {                echoMatch = 0;                break;              }            }          }        }        if (outOpcodes & LTERM_NEWLINE_CODE) {          /* Complete output line */          if (!echoMatch)            ltr->opcodes |= LTERM_NEWLINE_CODE;          /* Clear output line buffer */          ltermClearOutputLine(lts);          /* Clear input line break flag */          lts->inputLineBreak = 0;        } else {          /* Incomplete output line */          if (lts->completionRequest != LTERM_NO_COMPLETION) {            /* Completion requested;             * Compare first/last part of output line to parts of echo line             */            int k;            int extraChars = ltr->read_count - lts->echoChars;            int compChars = ltr->read_count;            int preMatch = 1;            if (extraChars > 0)              compChars = lts->echoChars;            for (k=0; k<compChars; k++) {              if (ltr->buf[k] != lts->echoLine[k]) {                preMatch = 0;                break;              }            }            if (!preMatch) {              /* No match; cancel completion request */              if (ltermCancelCompletion(lts) != 0)                return -1;            } else if (extraChars >= 0) {              /* Matched complete echo line and perhaps more */              if ((extraChars > 0) && (extraChars <= MAXCOLM1)) {                /* Insert extra characters into input line */                lts->completionChars = extraChars;                for (k=0; k<extraChars; k++) {                  if (ltermInsertChar(lti, ltr->buf[compChars+k]) != 0)                    return -1;                }                LTERM_LOG(ltermRead,32,                          ("++++++++++++ COMPLETION SUCCESSFUL\n"));                LTERM_LOGUNICODE(ltermRead,32,                                 (ltr->buf+compChars, extraChars));              }              /* Return updated input line, prefixed with prompt */              returnCode = ltermReturnInputLine(lts, ltr);              if (returnCode < 0)                return returnCode;            } else {              /* Partial match; do not echo */              echoMatch = 1;            }          } else if (lto->promptChars == lto->outputChars) {            /* Incomplete output line with just a prompt */            LTERM_LOG(ltermRead,32,("Prompt-only line\n"));            /* Return updated input line, prefixed with prompt */            returnCode = ltermReturnInputLine(lts, ltr);            if (returnCode < 0)              return returnCode;          }        }        if (echoMatch) {          /* Do not display output line; re-poll */          rePoll = 1;          LTERM_LOG(ltermRead,32,("Echo match; RE-POLLING\n\n"));          continue;        } else {          return 0;        }      } else {        /* Line mode data with modifier flags; return just cursor position */        ltr->opcodes = outOpcodes;        ltr->opvals = 0;        /* Copy full screen cursor position */        ltr->cursor_row = -1;        ltr->cursor_col = 0;        ltr->read_count = 0;        return 0;      }    }  } while (rePoll);  return -1;}