+ erase_lots(term, FALSE, TRUE, TRUE);
+ }
+}
+
+/*
+ * Update the scroll bar.
+ */
+static void update_sbar(Terminal *term)
+{
+ int nscroll = sblines(term);
+ set_sbar(term->frontend, nscroll + term->rows,
+ nscroll + term->disptop, term->rows);
+}
+
+/*
+ * Check whether the region bounded by the two pointers intersects
+ * the scroll region, and de-select the on-screen selection if so.
+ */
+static void check_selection(Terminal *term, pos from, pos to)
+{
+ if (poslt(from, term->selend) && poslt(term->selstart, to))
+ deselect(term);
+}
+
+/*
+ * Scroll the screen. (`lines' is +ve for scrolling forward, -ve
+ * for backward.) `sb' is TRUE if the scrolling is permitted to
+ * affect the scrollback buffer.
+ */
+static void scroll(Terminal *term, int topline, int botline, int lines, int sb)
+{
+ termline *line;
+ int i, seltop, olddisptop, shift;
+
+ if (topline != 0 || term->alt_which != 0)
+ sb = FALSE;
+
+ olddisptop = term->disptop;
+ shift = lines;
+ if (lines < 0) {
+ while (lines < 0) {
+ line = delpos234(term->screen, botline);
+ resizeline(term, line, term->cols);
+ for (i = 0; i < term->cols; i++)
+ copy_termchar(line, i, &term->erase_char);
+ line->lattr = LATTR_NORM;
+ addpos234(term->screen, line, topline);
+
+ if (term->selstart.y >= topline && term->selstart.y <= botline) {
+ term->selstart.y++;
+ if (term->selstart.y > botline) {
+ term->selstart.y = botline + 1;
+ term->selstart.x = 0;
+ }
+ }
+ if (term->selend.y >= topline && term->selend.y <= botline) {
+ term->selend.y++;
+ if (term->selend.y > botline) {
+ term->selend.y = botline + 1;
+ term->selend.x = 0;
+ }
+ }
+
+ lines++;
+ }
+ } else {
+ while (lines > 0) {
+ line = delpos234(term->screen, topline);
+ cc_check(line); /* XXX-REMOVE-BEFORE-RELEASE */
+ if (sb && term->savelines > 0) {
+ int sblen = count234(term->scrollback);
+ /*
+ * We must add this line to the scrollback. We'll
+ * remove a line from the top of the scrollback if
+ * the scrollback is full.
+ */
+ if (sblen == term->savelines) {
+ unsigned char *cline;
+
+ sblen--;
+ cline = delpos234(term->scrollback, 0);
+ sfree(cline);
+ } else
+ term->tempsblines += 1;
+
+ addpos234(term->scrollback, compressline(line), sblen);
+
+ /* now `line' itself can be reused as the bottom line */
+
+ /*
+ * If the user is currently looking at part of the
+ * scrollback, and they haven't enabled any options
+ * that are going to reset the scrollback as a
+ * result of this movement, then the chances are
+ * they'd like to keep looking at the same line. So
+ * we move their viewpoint at the same rate as the
+ * scroll, at least until their viewpoint hits the
+ * top end of the scrollback buffer, at which point
+ * we don't have the choice any more.
+ *
+ * Thanks to Jan Holmen Holsten for the idea and
+ * initial implementation.
+ */
+ if (term->disptop > -term->savelines && term->disptop < 0)
+ term->disptop--;
+ }
+ resizeline(term, line, term->cols);
+ for (i = 0; i < term->cols; i++)
+ copy_termchar(line, i, &term->erase_char);
+ line->lattr = LATTR_NORM;
+ addpos234(term->screen, line, botline);
+
+ /*
+ * If the selection endpoints move into the scrollback,
+ * we keep them moving until they hit the top. However,
+ * of course, if the line _hasn't_ moved into the
+ * scrollback then we don't do this, and cut them off
+ * at the top of the scroll region.
+ *
+ * This applies to selstart and selend (for an existing
+ * selection), and also selanchor (for one being
+ * selected as we speak).
+ */
+ seltop = sb ? -term->savelines : topline;
+
+ if (term->selstate != NO_SELECTION) {
+ if (term->selstart.y >= seltop &&
+ term->selstart.y <= botline) {
+ term->selstart.y--;
+ if (term->selstart.y < seltop) {
+ term->selstart.y = seltop;
+ term->selstart.x = 0;
+ }
+ }
+ if (term->selend.y >= seltop && term->selend.y <= botline) {
+ term->selend.y--;
+ if (term->selend.y < seltop) {
+ term->selend.y = seltop;
+ term->selend.x = 0;
+ }
+ }
+ if (term->selanchor.y >= seltop &&
+ term->selanchor.y <= botline) {
+ term->selanchor.y--;
+ if (term->selanchor.y < seltop) {
+ term->selanchor.y = seltop;
+ term->selanchor.x = 0;
+ }
+ }
+ }
+
+ lines--;
+ }
+ }
+#ifdef OPTIMISE_SCROLL
+ shift += term->disptop - olddisptop;
+ if (shift < term->rows && shift > -term->rows && shift != 0)
+ scroll_display(term, topline, botline, shift);
+#endif /* OPTIMISE_SCROLL */
+}
+
+#ifdef OPTIMISE_SCROLL
+/*
+ * Add a scroll of a region on the screen into the pending scroll list.
+ * `lines' is +ve for scrolling forward, -ve for backward.
+ *
+ * If the scroll is on the same area as the last scroll in the list,
+ * merge them.
+ */
+static void save_scroll(Terminal *term, int topline, int botline, int lines)
+{
+ struct scrollregion *newscroll;
+ if (term->scrolltail &&
+ term->scrolltail->topline == topline &&
+ term->scrolltail->botline == botline) {
+ term->scrolltail->lines += lines;
+ } else {
+ newscroll = snew(struct scrollregion);
+ newscroll->topline = topline;
+ newscroll->botline = botline;
+ newscroll->lines = lines;
+ newscroll->next = NULL;
+
+ if (!term->scrollhead)
+ term->scrollhead = newscroll;
+ else
+ term->scrolltail->next = newscroll;
+ term->scrolltail = newscroll;
+ }
+}
+
+/*
+ * Scroll the physical display, and our conception of it in disptext.
+ */
+static void scroll_display(Terminal *term, int topline, int botline, int lines)
+{
+ int distance, nlines, i, j;
+
+ distance = lines > 0 ? lines : -lines;
+ nlines = botline - topline + 1 - distance;
+ if (lines > 0) {
+ for (i = 0; i < nlines; i++)
+ for (j = 0; j < term->cols; j++)
+ copy_termchar(term->disptext[start+i], j,
+ term->disptext[start+i+distance]->chars+j);
+ if (term->dispcursy >= 0 &&
+ term->dispcursy >= topline + distance &&
+ term->dispcursy < topline + distance + nlines)
+ term->dispcursy -= distance;
+ for (i = 0; i < distance; i++)
+ for (j = 0; j < term->cols; j++)
+ term->disptext[start+nlines+i]->chars[j].attr |= ATTR_INVALID;
+ } else {
+ for (i = nlines; i-- ;)
+ for (j = 0; j < term->cols; j++)
+ copy_termchar(term->disptext[start+i+distance], j,
+ term->disptext[start+i]->chars+j);
+ if (term->dispcursy >= 0 &&
+ term->dispcursy >= topline &&
+ term->dispcursy < topline + nlines)
+ term->dispcursy += distance;
+ for (i = 0; i < distance; i++)
+ for (j = 0; j < term->cols; j++)
+ term->disptext[start+i]->chars[j].attr |= ATTR_INVALID;
+ }
+ save_scroll(term, topline, botline, lines);
+}
+#endif /* OPTIMISE_SCROLL */
+
+/*
+ * Move the cursor to a given position, clipping at boundaries. We
+ * may or may not want to clip at the scroll margin: marg_clip is 0
+ * not to, 1 to disallow _passing_ the margins, and 2 to disallow
+ * even _being_ outside the margins.
+ */
+static void move(Terminal *term, int x, int y, int marg_clip)
+{
+ if (x < 0)
+ x = 0;
+ if (x >= term->cols)
+ x = term->cols - 1;
+ if (marg_clip) {
+ if ((term->curs.y >= term->marg_t || marg_clip == 2) &&
+ y < term->marg_t)
+ y = term->marg_t;
+ if ((term->curs.y <= term->marg_b || marg_clip == 2) &&
+ y > term->marg_b)
+ y = term->marg_b;
+ }
+ if (y < 0)
+ y = 0;
+ if (y >= term->rows)
+ y = term->rows - 1;
+ term->curs.x = x;
+ term->curs.y = y;
+ term->wrapnext = FALSE;
+}
+
+/*
+ * Save or restore the cursor and SGR mode.
+ */
+static void save_cursor(Terminal *term, int save)
+{
+ if (save) {
+ term->savecurs = term->curs;
+ term->save_attr = term->curr_attr;
+ term->save_cset = term->cset;
+ term->save_utf = term->utf;
+ term->save_wnext = term->wrapnext;
+ term->save_csattr = term->cset_attr[term->cset];
+ term->save_sco_acs = term->sco_acs;
+ } else {
+ term->curs = term->savecurs;
+ /* Make sure the window hasn't shrunk since the save */
+ if (term->curs.x >= term->cols)
+ term->curs.x = term->cols - 1;
+ if (term->curs.y >= term->rows)
+ term->curs.y = term->rows - 1;
+
+ term->curr_attr = term->save_attr;
+ term->cset = term->save_cset;
+ term->utf = term->save_utf;
+ term->wrapnext = term->save_wnext;
+ /*
+ * wrapnext might reset to False if the x position is no
+ * longer at the rightmost edge.
+ */
+ if (term->wrapnext && term->curs.x < term->cols-1)
+ term->wrapnext = FALSE;
+ term->cset_attr[term->cset] = term->save_csattr;
+ term->sco_acs = term->save_sco_acs;
+ set_erase_char(term);
+ }
+}
+
+/*
+ * This function is called before doing _anything_ which affects
+ * only part of a line of text. It is used to mark the boundary
+ * between two character positions, and it indicates that some sort
+ * of effect is going to happen on only one side of that boundary.
+ *
+ * The effect of this function is to check whether a CJK
+ * double-width character is straddling the boundary, and to remove
+ * it and replace it with two spaces if so. (Of course, one or
+ * other of those spaces is then likely to be replaced with
+ * something else again, as a result of whatever happens next.)
+ *
+ * Also, if the boundary is at the right-hand _edge_ of the screen,
+ * it implies something deliberate is being done to the rightmost
+ * column position; hence we must clear LATTR_WRAPPED2.
+ *
+ * The input to the function is the coordinates of the _second_
+ * character of the pair.
+ */
+static void check_boundary(Terminal *term, int x, int y)
+{
+ termline *ldata;
+
+ /* Validate input coordinates, just in case. */
+ if (x == 0 || x > term->cols)
+ return;
+
+ ldata = scrlineptr(y);
+ if (x == term->cols) {
+ ldata->lattr &= ~LATTR_WRAPPED2;
+ } else {
+ if (ldata->chars[x].chr == UCSWIDE) {
+ clear_cc(ldata, x-1);
+ clear_cc(ldata, x);
+ ldata->chars[x-1].chr = ' ' | CSET_ASCII;
+ ldata->chars[x] = ldata->chars[x-1];
+ }
+ }
+}
+
+/*
+ * Erase a large portion of the screen: the whole screen, or the
+ * whole line, or parts thereof.
+ */
+static void erase_lots(Terminal *term,
+ int line_only, int from_begin, int to_end)
+{
+ pos start, end;
+ int erase_lattr;
+ int erasing_lines_from_top = 0;
+
+ if (line_only) {
+ start.y = term->curs.y;
+ start.x = 0;
+ end.y = term->curs.y + 1;
+ end.x = 0;
+ erase_lattr = FALSE;
+ } else {
+ start.y = 0;
+ start.x = 0;
+ end.y = term->rows;
+ end.x = 0;
+ erase_lattr = TRUE;
+ }
+ if (!from_begin) {
+ start = term->curs;
+ }
+ if (!to_end) {
+ end = term->curs;
+ incpos(end);
+ }
+ if (!from_begin || !to_end)
+ check_boundary(term, term->curs.x, term->curs.y);
+ check_selection(term, start, end);
+
+ /* Clear screen also forces a full window redraw, just in case. */
+ if (start.y == 0 && start.x == 0 && end.y == term->rows)
+ term_invalidate(term);
+
+ /* Lines scrolled away shouldn't be brought back on if the terminal
+ * resizes. */
+ if (start.y == 0 && start.x == 0 && end.x == 0 && erase_lattr)
+ erasing_lines_from_top = 1;
+
+ if (term->cfg.erase_to_scrollback && erasing_lines_from_top) {
+ /* If it's a whole number of lines, starting at the top, and
+ * we're fully erasing them, erase by scrolling and keep the
+ * lines in the scrollback. */
+ int scrolllines = end.y;
+ if (end.y == term->rows) {
+ /* Shrink until we find a non-empty row.*/
+ scrolllines = find_last_nonempty_line(term, term->screen) + 1;
+ }
+ if (scrolllines > 0)
+ scroll(term, 0, scrolllines - 1, scrolllines, TRUE);
+ } else {
+ termline *ldata = scrlineptr(start.y);
+ while (poslt(start, end)) {
+ if (start.x == term->cols) {
+ if (!erase_lattr)
+ ldata->lattr &= ~(LATTR_WRAPPED | LATTR_WRAPPED2);
+ else
+ ldata->lattr = LATTR_NORM;
+ } else {
+ copy_termchar(ldata, start.x, &term->erase_char);
+ }
+ if (incpos(start) && start.y < term->rows) {
+ ldata = scrlineptr(start.y);
+ }
+ }
+ }
+
+ /* After an erase of lines from the top of the screen, we shouldn't
+ * bring the lines back again if the terminal enlarges (since the user or
+ * application has explictly thrown them away). */
+ if (erasing_lines_from_top && !(term->alt_which))
+ term->tempsblines = 0;
+}
+
+/*
+ * Insert or delete characters within the current line. n is +ve if
+ * insertion is desired, and -ve for deletion.
+ */
+static void insch(Terminal *term, int n)
+{
+ int dir = (n < 0 ? -1 : +1);
+ int m, j;
+ pos cursplus;
+ termline *ldata;
+
+ n = (n < 0 ? -n : n);
+ if (n > term->cols - term->curs.x)
+ n = term->cols - term->curs.x;
+ m = term->cols - term->curs.x - n;
+ cursplus.y = term->curs.y;
+ cursplus.x = term->curs.x + n;
+ check_selection(term, term->curs, cursplus);
+ check_boundary(term, term->curs.x, term->curs.y);
+ if (dir < 0)
+ check_boundary(term, term->curs.x + n, term->curs.y);
+ ldata = scrlineptr(term->curs.y);
+ if (dir < 0) {
+ for (j = 0; j < m; j++)
+ move_termchar(ldata,
+ ldata->chars + term->curs.x + j,
+ ldata->chars + term->curs.x + j + n);
+ while (n--)
+ copy_termchar(ldata, term->curs.x + m++, &term->erase_char);
+ } else {
+ for (j = m; j-- ;)
+ move_termchar(ldata,
+ ldata->chars + term->curs.x + j + n,
+ ldata->chars + term->curs.x + j);
+ while (n--)
+ copy_termchar(ldata, term->curs.x + n, &term->erase_char);
+ }
+}
+
+/*
+ * Toggle terminal mode `mode' to state `state'. (`query' indicates
+ * whether the mode is a DEC private one or a normal one.)
+ */
+static void toggle_mode(Terminal *term, int mode, int query, int state)
+{
+ if (query)
+ switch (mode) {
+ case 1: /* DECCKM: application cursor keys */
+ term->app_cursor_keys = state;
+ break;
+ case 2: /* DECANM: VT52 mode */
+ term->vt52_mode = !state;
+ if (term->vt52_mode) {
+ term->blink_is_real = FALSE;
+ term->vt52_bold = FALSE;
+ } else {
+ term->blink_is_real = term->cfg.blinktext;
+ }
+ term_schedule_tblink(term);
+ break;
+ case 3: /* DECCOLM: 80/132 columns */
+ deselect(term);
+ if (!term->cfg.no_remote_resize)
+ request_resize(term->frontend, state ? 132 : 80, term->rows);
+ term->reset_132 = state;
+ term->alt_t = term->marg_t = 0;
+ term->alt_b = term->marg_b = term->rows - 1;
+ move(term, 0, 0, 0);
+ erase_lots(term, FALSE, TRUE, TRUE);
+ break;
+ case 5: /* DECSCNM: reverse video */
+ /*
+ * Toggle reverse video. If we receive an OFF within the
+ * visual bell timeout period after an ON, we trigger an
+ * effective visual bell, so that ESC[?5hESC[?5l will
+ * always be an actually _visible_ visual bell.
+ */
+ if (term->rvideo && !state) {
+ /* This is an OFF, so set up a vbell */
+ term_schedule_vbell(term, TRUE, term->rvbell_startpoint);
+ } else if (!term->rvideo && state) {
+ /* This is an ON, so we notice the time and save it. */
+ term->rvbell_startpoint = GETTICKCOUNT();
+ }
+ term->rvideo = state;
+ seen_disp_event(term);
+ break;
+ case 6: /* DECOM: DEC origin mode */
+ term->dec_om = state;
+ break;
+ case 7: /* DECAWM: auto wrap */
+ term->wrap = state;
+ break;
+ case 8: /* DECARM: auto key repeat */
+ term->repeat_off = !state;
+ break;
+ case 10: /* DECEDM: set local edit mode */
+ term->term_editing = state;
+ if (term->ldisc) /* cause ldisc to notice changes */
+ ldisc_send(term->ldisc, NULL, 0, 0);
+ break;
+ case 25: /* DECTCEM: enable/disable cursor */
+ compatibility2(OTHER, VT220);
+ term->cursor_on = state;
+ seen_disp_event(term);
+ break;
+ case 47: /* alternate screen */
+ compatibility(OTHER);
+ deselect(term);
+ swap_screen(term, term->cfg.no_alt_screen ? 0 : state, FALSE, FALSE);
+ term->disptop = 0;
+ break;
+ case 1000: /* xterm mouse 1 */
+ term->xterm_mouse = state ? 1 : 0;
+ set_raw_mouse_mode(term->frontend, state);
+ break;
+ case 1002: /* xterm mouse 2 */
+ term->xterm_mouse = state ? 2 : 0;
+ set_raw_mouse_mode(term->frontend, state);
+ break;
+ case 1047: /* alternate screen */
+ compatibility(OTHER);
+ deselect(term);
+ swap_screen(term, term->cfg.no_alt_screen ? 0 : state, TRUE, TRUE);
+ term->disptop = 0;
+ break;
+ case 1048: /* save/restore cursor */
+ if (!term->cfg.no_alt_screen)
+ save_cursor(term, state);
+ if (!state) seen_disp_event(term);
+ break;
+ case 1049: /* cursor & alternate screen */
+ if (state && !term->cfg.no_alt_screen)
+ save_cursor(term, state);
+ if (!state) seen_disp_event(term);
+ compatibility(OTHER);
+ deselect(term);
+ swap_screen(term, term->cfg.no_alt_screen ? 0 : state, TRUE, FALSE);
+ if (!state && !term->cfg.no_alt_screen)
+ save_cursor(term, state);
+ term->disptop = 0;
+ break;
+ } else
+ switch (mode) {
+ case 4: /* IRM: set insert mode */
+ compatibility(VT102);
+ term->insert = state;
+ break;
+ case 12: /* SRM: set echo mode */
+ term->term_echoing = !state;
+ if (term->ldisc) /* cause ldisc to notice changes */
+ ldisc_send(term->ldisc, NULL, 0, 0);
+ break;
+ case 20: /* LNM: Return sends ... */
+ term->cr_lf_return = state;
+ break;
+ case 34: /* WYULCURM: Make cursor BIG */
+ compatibility2(OTHER, VT220);
+ term->big_cursor = !state;
+ }
+}
+
+/*
+ * Process an OSC sequence: set window title or icon name.
+ */
+static void do_osc(Terminal *term)
+{
+ if (term->osc_w) {
+ while (term->osc_strlen--)
+ term->wordness[(unsigned char)
+ term->osc_string[term->osc_strlen]] = term->esc_args[0];
+ } else {
+ term->osc_string[term->osc_strlen] = '\0';
+ switch (term->esc_args[0]) {
+ case 0:
+ case 1:
+ if (!term->cfg.no_remote_wintitle)
+ set_icon(term->frontend, term->osc_string);
+ if (term->esc_args[0] == 1)
+ break;
+ /* fall through: parameter 0 means set both */
+ case 2:
+ case 21:
+ if (!term->cfg.no_remote_wintitle)
+ set_title(term->frontend, term->osc_string);
+ break;
+ }
+ }
+}
+
+/*
+ * ANSI printing routines.
+ */
+static void term_print_setup(Terminal *term)
+{
+ bufchain_clear(&term->printer_buf);
+ term->print_job = printer_start_job(term->cfg.printer);
+}
+static void term_print_flush(Terminal *term)
+{
+ void *data;
+ int len;
+ int size;
+ while ((size = bufchain_size(&term->printer_buf)) > 5) {
+ bufchain_prefix(&term->printer_buf, &data, &len);
+ if (len > size-5)
+ len = size-5;
+ printer_job_data(term->print_job, data, len);
+ bufchain_consume(&term->printer_buf, len);
+ }
+}
+static void term_print_finish(Terminal *term)
+{
+ void *data;
+ int len, size;
+ char c;
+
+ if (!term->printing && !term->only_printing)
+ return; /* we need do nothing */
+
+ term_print_flush(term);
+ while ((size = bufchain_size(&term->printer_buf)) > 0) {
+ bufchain_prefix(&term->printer_buf, &data, &len);
+ c = *(char *)data;
+ if (c == '\033' || c == '\233') {
+ bufchain_consume(&term->printer_buf, size);
+ break;
+ } else {
+ printer_job_data(term->print_job, &c, 1);
+ bufchain_consume(&term->printer_buf, 1);
+ }
+ }
+ printer_finish_job(term->print_job);
+ term->print_job = NULL;
+ term->printing = term->only_printing = FALSE;
+}
+
+/*
+ * Remove everything currently in `inbuf' and stick it up on the
+ * in-memory display. There's a big state machine in here to
+ * process escape sequences...
+ */
+static void term_out(Terminal *term)
+{
+ unsigned long c;
+ int unget;
+ unsigned char localbuf[256], *chars;
+ int nchars = 0;
+
+ unget = -1;
+
+ chars = NULL; /* placate compiler warnings */
+ while (nchars > 0 || bufchain_size(&term->inbuf) > 0) {
+ if (unget == -1) {
+ if (nchars == 0) {
+ void *ret;
+ bufchain_prefix(&term->inbuf, &ret, &nchars);
+ if (nchars > sizeof(localbuf))
+ nchars = sizeof(localbuf);
+ memcpy(localbuf, ret, nchars);
+ bufchain_consume(&term->inbuf, nchars);
+ chars = localbuf;
+ assert(chars != NULL);
+ }
+ c = *chars++;
+ nchars--;
+
+ /*
+ * Optionally log the session traffic to a file. Useful for
+ * debugging and possibly also useful for actual logging.
+ */
+ if (term->cfg.logtype == LGTYP_DEBUG && term->logctx)
+ logtraffic(term->logctx, (unsigned char) c, LGTYP_DEBUG);
+ } else {
+ c = unget;
+ unget = -1;
+ }
+
+ /* Note only VT220+ are 8-bit VT102 is seven bit, it shouldn't even
+ * be able to display 8-bit characters, but I'll let that go 'cause
+ * of i18n.
+ */
+
+ /*
+ * If we're printing, add the character to the printer
+ * buffer.
+ */
+ if (term->printing) {
+ bufchain_add(&term->printer_buf, &c, 1);
+
+ /*
+ * If we're in print-only mode, we use a much simpler
+ * state machine designed only to recognise the ESC[4i
+ * termination sequence.
+ */
+ if (term->only_printing) {
+ if (c == '\033')
+ term->print_state = 1;
+ else if (c == (unsigned char)'\233')
+ term->print_state = 2;
+ else if (c == '[' && term->print_state == 1)
+ term->print_state = 2;
+ else if (c == '4' && term->print_state == 2)
+ term->print_state = 3;
+ else if (c == 'i' && term->print_state == 3)
+ term->print_state = 4;
+ else
+ term->print_state = 0;
+ if (term->print_state == 4) {
+ term_print_finish(term);
+ }
+ continue;
+ }
+ }
+
+ /* First see about all those translations. */
+ if (term->termstate == TOPLEVEL) {
+ if (in_utf(term))
+ switch (term->utf_state) {
+ case 0:
+ if (c < 0x80) {
+ /* UTF-8 must be stateless so we ignore iso2022. */
+ if (term->ucsdata->unitab_ctrl[c] != 0xFF)
+ c = term->ucsdata->unitab_ctrl[c];
+ else c = ((unsigned char)c) | CSET_ASCII;
+ break;
+ } else if ((c & 0xe0) == 0xc0) {
+ term->utf_size = term->utf_state = 1;
+ term->utf_char = (c & 0x1f);
+ } else if ((c & 0xf0) == 0xe0) {
+ term->utf_size = term->utf_state = 2;
+ term->utf_char = (c & 0x0f);
+ } else if ((c & 0xf8) == 0xf0) {
+ term->utf_size = term->utf_state = 3;
+ term->utf_char = (c & 0x07);
+ } else if ((c & 0xfc) == 0xf8) {
+ term->utf_size = term->utf_state = 4;
+ term->utf_char = (c & 0x03);
+ } else if ((c & 0xfe) == 0xfc) {
+ term->utf_size = term->utf_state = 5;
+ term->utf_char = (c & 0x01);
+ } else {
+ c = UCSERR;
+ break;
+ }
+ continue;
+ case 1:
+ case 2:
+ case 3:
+ case 4:
+ case 5:
+ if ((c & 0xC0) != 0x80) {
+ unget = c;
+ c = UCSERR;
+ term->utf_state = 0;
+ break;
+ }
+ term->utf_char = (term->utf_char << 6) | (c & 0x3f);
+ if (--term->utf_state)
+ continue;
+
+ c = term->utf_char;
+
+ /* Is somebody trying to be evil! */
+ if (c < 0x80 ||
+ (c < 0x800 && term->utf_size >= 2) ||
+ (c < 0x10000 && term->utf_size >= 3) ||
+ (c < 0x200000 && term->utf_size >= 4) ||
+ (c < 0x4000000 && term->utf_size >= 5))
+ c = UCSERR;
+
+ /* Unicode line separator and paragraph separator are CR-LF */
+ if (c == 0x2028 || c == 0x2029)
+ c = 0x85;
+
+ /* High controls are probably a Baaad idea too. */
+ if (c < 0xA0)
+ c = 0xFFFD;
+
+ /* The UTF-16 surrogates are not nice either. */
+ /* The standard give the option of decoding these:
+ * I don't want to! */
+ if (c >= 0xD800 && c < 0xE000)
+ c = UCSERR;
+
+ /* ISO 10646 characters now limited to UTF-16 range. */
+ if (c > 0x10FFFF)
+ c = UCSERR;
+
+ /* This is currently a TagPhobic application.. */
+ if (c >= 0xE0000 && c <= 0xE007F)
+ continue;
+
+ /* U+FEFF is best seen as a null. */
+ if (c == 0xFEFF)
+ continue;
+ /* But U+FFFE is an error. */
+ if (c == 0xFFFE || c == 0xFFFF)
+ c = UCSERR;
+
+ break;
+ }
+ /* Are we in the nasty ACS mode? Note: no sco in utf mode. */
+ else if(term->sco_acs &&
+ (c!='\033' && c!='\012' && c!='\015' && c!='\b'))
+ {
+ if (term->sco_acs == 2) c |= 0x80;
+ c |= CSET_SCOACS;
+ } else {
+ switch (term->cset_attr[term->cset]) {
+ /*
+ * Linedraw characters are different from 'ESC ( B'
+ * only for a small range. For ones outside that
+ * range, make sure we use the same font as well as
+ * the same encoding.
+ */
+ case CSET_LINEDRW:
+ if (term->ucsdata->unitab_ctrl[c] != 0xFF)
+ c = term->ucsdata->unitab_ctrl[c];
+ else
+ c = ((unsigned char) c) | CSET_LINEDRW;
+ break;
+
+ case CSET_GBCHR:
+ /* If UK-ASCII, make the '#' a LineDraw Pound */
+ if (c == '#') {
+ c = '}' | CSET_LINEDRW;
+ break;
+ }
+ /*FALLTHROUGH*/ case CSET_ASCII:
+ if (term->ucsdata->unitab_ctrl[c] != 0xFF)
+ c = term->ucsdata->unitab_ctrl[c];
+ else
+ c = ((unsigned char) c) | CSET_ASCII;
+ break;
+ case CSET_SCOACS:
+ if (c>=' ') c = ((unsigned char)c) | CSET_SCOACS;
+ break;
+ }
+ }
+ }
+
+ /* How about C1 controls ? */
+ if ((c & -32) == 0x80 && term->termstate < DO_CTRLS &&
+ !term->vt52_mode && has_compat(VT220)) {
+ term->termstate = SEEN_ESC;
+ term->esc_query = FALSE;
+ c = '@' + (c & 0x1F);
+ }
+
+ /* Or the GL control. */
+ if (c == '\177' && term->termstate < DO_CTRLS && has_compat(OTHER)) {
+ if (term->curs.x && !term->wrapnext)
+ term->curs.x--;
+ term->wrapnext = FALSE;
+ /* destructive backspace might be disabled */
+ if (!term->cfg.no_dbackspace) {
+ check_boundary(term, term->curs.x, term->curs.y);
+ check_boundary(term, term->curs.x+1, term->curs.y);
+ copy_termchar(scrlineptr(term->curs.y),
+ term->curs.x, &term->erase_char);
+ }
+ } else
+ /* Or normal C0 controls. */
+ if ((c & ~0x1F) == 0 && term->termstate < DO_CTRLS) {