- ach = ansi_buf;
- pch = pack_buf;
- ansi_buf[0] = pack_buf[0] = '\0';
- got_escape = 0;
- ch = pack_ansi();
- if (ch == A_ESC)
- do {
- ch = pack_ansi();
- if (ch == EOF)
- return;
- } while (ch < '0' || ch > '~');
- else
- if (ch == A_CSI)
- do {
- ch = pack_ansi();
- if (ch == EOF)
- return;
- } while (ch < '@' || ch > '~');
- else
- if (ch == A_DCS)
- do {
- ch = pack_ansi();
- if (ch == EOF)
- return;
- } while (ch != A_ST);
+ read_key((char *)ansi_buf, sizeof(ansi_buf));
+ /* Throw away control characters inside CSI sequences.
+ Convert two character 7-bit sequences into 8-bit sequences. */
+ for (i = j = last_escape = 0; (ch = ansi_buf[i]) != 0; i++) {
+ if (ch == A_ESC) {
+ if (last_escape == A_ESC) {
+ pack_buf[j++] = A_ESC;
+ }
+ last_escape = A_ESC;
+ } else
+ if (last_escape == A_ESC && ch >= '@' && ch <= '_') {
+ pack_buf[j++] = last_escape = ch + 0x40;
+ } else
+ if (last_escape != A_CSI || (ch > 0x20 && ch != 0x80)) {
+ if (last_escape == A_ESC) {
+ pack_buf[j++] = A_ESC;
+ }
+ if (ch > 0x80 && ch < 0xa0) {
+ last_escape = ch;
+ }
+ pack_buf[j++] = ch;
+ }
+ }
+ if (last_escape == A_ESC) {
+ pack_buf[j++] = A_ESC;
+ }
+ pack_buf[j] = '\0';