if (style[0] != LTERM_STDOUT_STYLE) write(1, "\033[7m", 4);  writeUnicode(1, buf, n_read);  if (style[0] != LTERM_STDOUT_STYLE) write(1, "\033[27m", 5);
if (n_read > 0) { if (style[0] != LTERM_STDOUT_STYLE) write(1, "\033[7m", 4);  writeUnicode(1, buf, n_read);  if (style[0] != LTERM_STDOUT_STYLE) write(1, "\033[27m", 5); }
void *output_handler(void *arg){  int *plterm = (int *) arg;  int timeout = -1;  UNICHAR buf[MAXCOL];  UNISTYLE style[MAXCOL];  int n_read, opcodes, opvals, buf_row, buf_col, cursor_row, cursor_col;  int xmax, ymax, x, y, c;  MEVENT mev;  if (errDevice != NULL)    fprintf(stderr, "output_handler-00: thread ID = %d, LTERM=%d\n",                    pthread_self(), *plterm);  /* Get screen size */  if (ncursesFlag) {    getmaxyx(stdscr, ymax, xmax);  } else {    ymax = 24;    xmax = 80;  }  if (errDevice != NULL)    fprintf(stderr, "output_handler-00: screen xmax = %d, ymax = %d\n",            xmax,ymax);  for (;;) {    n_read = lterm_read(*plterm, timeout, buf, MAXCOL,                        style, &opcodes, &opvals,                        &buf_row, &buf_col, &cursor_row, &cursor_col);    if (n_read == -1) {      fprintf(stderr, "output_handler: Error %d return from lterm_read\n",                      n_read);      return NULL;    }    /* Exit loop if TTY has been closed;     * leave it to input handler to close the LTERM.     */    if (n_read == -2) {      if (errDevice != NULL)        fprintf(stderr, "output_handler: pseudo-TTY has been closed\n");      break;    }    if (debugFlag) {      fprintf(stderr, "output_handler-00: n_read=%d, opcodes=%x, opvals=%d, buf_row/col=%d/%d, cursor_row/col=%d/%d\n",            n_read, opcodes, opvals, buf_row, buf_col, cursor_row, cursor_col);      fprintf(stderr, "output_handler-00: U(%d): ", n_read);      printUnicode(stderr, buf, n_read, 1);      fprintf(stderr, "\n");    }    if (opcodes & LTERM_STREAMDATA_CODE) {      /* Stream data */      if (debugFlag)        fprintf(stderr, "output_handler-00: STREAMDATA\n");    } else if (opcodes & LTERM_SCREENDATA_CODE) {      /* Screen data */      if (!screenMode) {        screenMode = 1;        topScrollRow = ymax-1;        botScrollRow = 0;      }      if (debugFlag)        fprintf(stderr, "output_handler-00: SCREENDATA, topScrollRow=%d, botScrollRow=%d\n", topScrollRow, botScrollRow);      if (ncursesFlag) {        /* NCURSES mode */        if (opcodes & LTERM_CLEAR_CODE) {          clear();        } else if (opcodes & LTERM_INSERT_CODE) {          if ((botScrollRow > 0) && (opvals > 0)) {            move(ymax-botScrollRow-opvals, 0);            insdelln(-opvals);          }          move(ymax-1-buf_row, 0);          insdelln(opvals);        } else if (opcodes & LTERM_DELETE_CODE) {          move(ymax-1-buf_row, 0);          insdelln(-opvals);          if ((botScrollRow > 0) && (opvals > 0)) {            move(ymax-botScrollRow-opvals, 0);            insdelln(opvals);          }        } else if (opcodes & LTERM_SCROLL_CODE) {          topScrollRow = opvals;          botScrollRow = buf_row;        } else if (n_read > 0) {          /* Data available for display */          move(ymax-1-buf_row, buf_col);          clrtoeol();          if (style[0] != LTERM_STDOUT_STYLE)            attr_on(A_REVERSE, NULL);          writeUnicode(-1, buf, n_read);          if (style[0] != LTERM_STDOUT_STYLE)            attr_off(A_REVERSE, NULL);        }        /* Position cursor */        move(ymax-1-cursor_row, cursor_col);        refresh();      } else {        /* XTERM MODE */        char esc_seq[21];        if (opcodes & LTERM_CLEAR_CODE) {          /* Clear screen */          write(1, "\033[H\033[2J", 7);        } else if (opcodes & LTERM_INSERT_CODE) {          /* Insert lines */          sprintf(esc_seq, "\033[%d;%dH", ymax-buf_row, buf_col+1);          write(1, esc_seq, strlen(esc_seq));          sprintf(esc_seq, "\033[%dL", opvals);          write(1, esc_seq, strlen(esc_seq));        } else if (opcodes & LTERM_DELETE_CODE) {          /* Delete lines */          sprintf(esc_seq, "\033[%d;%dH", ymax-buf_row, buf_col+1);          write(1, esc_seq, strlen(esc_seq));          sprintf(esc_seq, "\033[%dM", opvals);          write(1, esc_seq, strlen(esc_seq));        } else if (opcodes & LTERM_SCROLL_CODE) {          /* Set scrolling region */          sprintf(esc_seq, "\033[%d;%dr", ymax-opvals, ymax-buf_row);          write(1, esc_seq, strlen(esc_seq));        } else if (n_read > 0) {          /* Data available for display */          sprintf(esc_seq, "\033[%d;%dH", ymax-buf_row, buf_col+1);          write(1, esc_seq, strlen(esc_seq));          if (style[0] != LTERM_STDOUT_STYLE)            write(1, "\033[7m", 4);          writeUnicode(1, buf, n_read);          if (style[0] != LTERM_STDOUT_STYLE)            write(1, "\033[27m", 5);        }        sprintf(esc_seq, "\033[%d;%dH", ymax-cursor_row, cursor_col+1);        write(1, esc_seq, strlen(esc_seq));      }    } else if (opcodes & LTERM_LINEDATA_CODE) {      /* Line data */      if (debugFlag)        fprintf(stderr, "output_handler-00: LINEDATA\n");      if (screenMode)        screenMode = 0;      if (ncursesFlag) {        /* NCURSES mode */        /* Move cursor to bottom of screen, clear line and display line */        move(ymax-1,0);        clrtoeol();        writeUnicode(-1, buf, n_read);        move(ymax-1,cursor_col);        if (opcodes & LTERM_NEWLINE_CODE) {          move(0,0);          insdelln(-1);          move(ymax-1,0);        }        refresh();      } else {        /* Screen mode */        int j;        /* Clear line */        write(1, "\033[2K", 4);        write(1, "\r", 1);        if (opcodes & LTERM_META_CODE)          write(1, "META", 4);        writeUnicode(1, buf, n_read);        for (j=0; j< (n_read-cursor_col); j++)          write(1, "\033[D", 3);        if (opcodes & LTERM_BELL_CODE)          write(1, "\007", 1);        if (opcodes & LTERM_CLEAR_CODE)          write(1, "\033[H\033[2J", 7);        if (opcodes & LTERM_NEWLINE_CODE)          write(1, "\n", 1);      }    } else if (opcodes != 0) {      fprintf(stderr, "output_handler: invalid opcodes %x\n", opcodes);    }  }  return NULL;}