/
ansi.c
3188 lines (3021 loc) · 68.7 KB
/
ansi.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/* Copyright (c) 2008, 2009
* Juergen Weigert (jnweiger@immd4.informatik.uni-erlangen.de)
* Michael Schroeder (mlschroe@immd4.informatik.uni-erlangen.de)
* Micah Cowan (micah@cowan.name)
* Sadrul Habib Chowdhury (sadrul@users.sourceforge.net)
* Copyright (c) 1993-2002, 2003, 2005, 2006, 2007
* Juergen Weigert (jnweiger@immd4.informatik.uni-erlangen.de)
* Michael Schroeder (mlschroe@immd4.informatik.uni-erlangen.de)
* Copyright (c) 1987 Oliver Laumann
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 3, or (at your option)
* any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program (see the file COPYING); if not, see
* http://www.gnu.org/licenses/, or contact Free Software Foundation, Inc.,
* 51 Franklin Street, Fifth Floor, Boston, MA 02111-1301 USA
*
****************************************************************
*/
#include <sys/types.h>
#include <fcntl.h>
#ifndef sun /* we want to know about TIOCPKT. */
# include <sys/ioctl.h>
#endif
#include "config.h"
#include "screen.h"
#include "braille.h"
#include "extern.h"
#include "logfile.h"
extern struct display *display, *displays;
extern struct win *fore; /* for 83 escape */
extern struct layer *flayer; /* for 83 escape */
extern struct NewWindow nwin_default; /* for ResetWindow() */
extern int nversion; /* numerical version of screen */
extern int log_flush, logtstamp_on, logtstamp_after;
extern char *logtstamp_string;
extern char *captionstring;
extern char *hstatusstring;
extern char *wliststr;
#ifdef COPY_PASTE
extern int compacthist;
#endif
#ifdef MULTIUSER
extern struct acluser *EffectiveAclUser;
#endif
int Z0width, Z1width; /* widths for Z0/Z1 switching */
/* globals set in WriteString */
static struct win *curr; /* window we are working on */
static int rows, cols; /* window size of the curr window */
int visual_bell = 0;
int use_hardstatus = 1; /* display status line in hs */
char *printcmd = 0;
int use_altscreen = 0; /* enable alternate screen support? */
unsigned char *blank; /* line filled with spaces */
unsigned char *null; /* line filled with '\0' */
struct mline mline_old;
struct mline mline_blank;
struct mline mline_null;
struct mchar mchar_null;
struct mchar mchar_blank = {' ' /* , 0, 0, ... */};
struct mchar mchar_so = {' ', A_SO /* , 0, 0, ... */};
int renditions[NUM_RENDS] = {65529 /* =ub */, 65531 /* =b */, 65533 /* =u */ };
/* keep string_t and string_t_string in sync! */
static char *string_t_string[] =
{
"NONE",
"DCS", /* Device control string */
"OSC", /* Operating system command */
"APC", /* Application program command */
/* - used for status change */
"PM", /* Privacy message */
"AKA", /* title for current screen */
"GM", /* Global message to every display */
"STATUS" /* User hardstatus line */
};
/* keep state_t and state_t_string in sync! */
static char *state_t_string[] =
{
"LIT", /* Literal input */
"ESC", /* Start of escape sequence */
"ASTR", /* Start of control string */
"STRESC", /* ESC seen in control string */
"CSI", /* Reading arguments in "CSI Pn ;...*/
"PRIN", /* Printer mode */
"PRINESC", /* ESC seen in printer mode */
"PRINCSI", /* CSI seen in printer mode */
"PRIN4" /* CSI 4 seen in printer mode */
};
static int Special __P((int));
static void DoESC __P((int, int));
static void DoCSI __P((int, int));
static void StringStart __P((enum string_t));
static void StringChar __P((int));
static int StringEnd __P((void));
static void PrintStart __P((void));
static void PrintChar __P((int));
static void PrintFlush __P((void));
#ifdef FONT
static void DesignateCharset __P((int, int));
static void MapCharset __P((int));
static void MapCharsetR __P((int));
#endif
static void SaveCursor __P((struct cursor *));
static void RestoreCursor __P((struct cursor *));
static void BackSpace __P((void));
static void Return __P((void));
static void LineFeed __P((int));
static void ReverseLineFeed __P((void));
static void InsertChar __P((int));
static void DeleteChar __P((int));
static void DeleteLine __P((int));
static void InsertLine __P((int));
static void Scroll __P((char *, int, int, char *));
static void ForwardTab __P((void));
static void BackwardTab __P((void));
static void ClearScreen __P((void));
static void ClearFromBOS __P((void));
static void ClearToEOS __P((void));
static void ClearLineRegion __P((int, int));
static void CursorRight __P((int));
static void CursorUp __P((int));
static void CursorDown __P((int));
static void CursorLeft __P((int));
static void ASetMode __P((int));
static void SelectRendition __P((void));
static void RestorePosRendition __P((void));
static void FillWithEs __P((void));
static void FindAKA __P((void));
static void Report __P((char *, int, int));
static void ScrollRegion __P((int));
#ifdef COPY_PASTE
static void WAddLineToHist __P((struct win *, struct mline *));
#endif
static void WLogString __P((struct win *, char *, int));
static void WReverseVideo __P((struct win *, int));
static int WindowChangedCheck __P((char *, int, int *));
static void MFixLine __P((struct win *, int, struct mchar *));
static void MScrollH __P((struct win *, int, int, int, int, int));
static void MScrollV __P((struct win *, int, int, int, int));
static void MClearArea __P((struct win *, int, int, int, int, int));
static void MInsChar __P((struct win *, struct mchar *, int, int));
static void MPutChar __P((struct win *, struct mchar *, int, int));
static void MPutStr __P((struct win *, char *, int, struct mchar *, int, int));
static void MWrapChar __P((struct win *, struct mchar *, int, int, int, int));
#ifdef COLOR
static void MBceLine __P((struct win *, int, int, int, int));
#endif
#ifdef COLOR
# define CURR_BCE (curr->w_bce ? rend_getbg(&curr->w_rend) : 0)
#else
# define CURR_BCE 0
#endif
void
ResetAnsiState(p)
struct win *p;
{
p->w_state = LIT;
p->w_StringType = NONE;
}
void
ResetWindow(p)
register struct win *p;
{
register int i;
p->w_wrap = nwin_default.wrap;
p->w_origin = 0;
p->w_insert = 0;
p->w_revvid = 0;
p->w_mouse = 0;
p->w_curinv = 0;
p->w_curvvis = 0;
p->w_autolf = 0;
p->w_keypad = 0;
p->w_cursorkeys = 0;
p->w_top = 0;
p->w_bot = p->w_height - 1;
p->w_saved.on = 0;
p->w_x = p->w_y = 0;
p->w_state = LIT;
p->w_StringType = NONE;
bzero(p->w_tabs, p->w_width);
for (i = 8; i < p->w_width; i += 8)
p->w_tabs[i] = 1;
p->w_rend = mchar_null;
#ifdef FONT
ResetCharsets(p);
#endif
#ifdef COLOR
p->w_bce = nwin_default.bce;
#endif
}
/* adds max 22 bytes */
int
GetAnsiStatus(w, buf)
struct win *w;
char *buf;
{
char *p = buf;
if (w->w_state == LIT)
return 0;
strcpy(p, state_t_string[w->w_state]);
p += strlen(p);
if (w->w_intermediate)
{
*p++ = '-';
if (w->w_intermediate > 0xff)
p += AddXChar(p, w->w_intermediate >> 8);
p += AddXChar(p, w->w_intermediate & 0xff);
*p = 0;
}
if (w->w_state == ASTR || w->w_state == STRESC)
sprintf(p, "-%s", string_t_string[w->w_StringType]);
p += strlen(p);
return p - buf;
}
#ifdef FONT
void
ResetCharsets(p)
struct win *p;
{
p->w_gr = nwin_default.gr;
p->w_c1 = nwin_default.c1;
SetCharsets(p, "BBBB02");
if (nwin_default.charset)
SetCharsets(p, nwin_default.charset);
#ifdef ENCODINGS
ResetEncoding(p);
#endif
}
void
SetCharsets(p, s)
struct win *p;
char *s;
{
int i;
for (i = 0; i < 4 && *s; i++, s++)
if (*s != '.')
p->w_charsets[i] = ((*s == 'B') ? ASCII : *s);
if (*s && *s++ != '.')
p->w_Charset = s[-1] - '0';
if (*s && *s != '.')
p->w_CharsetR = *s - '0';
p->w_ss = 0;
p->w_FontL = p->w_charsets[p->w_Charset];
p->w_FontR = p->w_charsets[p->w_CharsetR];
}
#endif /* FONT */
/*****************************************************************/
/*
* Here comes the vt100 emulator
* - writes logfiles,
* - sets timestamp and flags activity in window.
* - record program output in window scrollback
* - translate program output for the display and put it into the obuf.
*
*/
void
WriteString(wp, buf, len)
struct win *wp;
register char *buf;
register int len;
{
register int c;
#ifdef FONT
register int font;
#endif
struct canvas *cv;
if (!len)
return;
if (wp->w_log)
WLogString(wp, buf, len);
/* set global variables (yuck!) */
curr = wp;
cols = curr->w_width;
rows = curr->w_height;
if (curr->w_silence)
SetTimeout(&curr->w_silenceev, curr->w_silencewait * 1000);
if (curr->w_monitor == MON_ON)
{
debug2("ACTIVITY %d %d\n", curr->w_monitor, curr->w_bell);
curr->w_monitor = MON_FOUND;
}
if (cols > 0 && rows > 0)
{
do
{
c = (unsigned char)*buf++;
#ifdef FONT
# ifdef DW_CHARS
if (!curr->w_mbcs)
# endif
curr->w_rend.font = curr->w_FontL; /* Default: GL */
#endif
/* The next part is only for speedup */
if (curr->w_state == LIT &&
#ifdef UTF8
curr->w_encoding != UTF8 &&
#endif
#ifdef DW_CHARS
!is_dw_font(curr->w_rend.font) &&
# ifdef ENCODINGS
curr->w_rend.font != KANA && !curr->w_mbcs &&
# endif
#endif
#ifdef FONT
curr->w_rend.font != '<' &&
#endif
c >= ' ' && c != 0x7f &&
((c & 0x80) == 0 || ((c >= 0xa0 || !curr->w_c1) && !curr->w_gr)) && !curr->w_ss &&
!curr->w_insert && curr->w_x < cols - 1)
{
register int currx = curr->w_x;
char *imp = buf - 1;
while (currx < cols - 1)
{
currx++;
if (--len == 0)
break;
c = (unsigned char)*buf++;
if (c < ' ' || c == 0x7f || ((c & 0x80) && ((c < 0xa0 && curr->w_c1) || curr->w_gr)))
break;
}
currx -= curr->w_x;
if (currx > 0)
{
MPutStr(curr, imp, currx, &curr->w_rend, curr->w_x, curr->w_y);
LPutStr(&curr->w_layer, imp, currx, &curr->w_rend, curr->w_x, curr->w_y);
curr->w_x += currx;
}
if (len == 0)
break;
}
/* end of speedup code */
#ifdef UTF8
if (curr->w_encoding == UTF8)
{
c = FromUtf8(c, &curr->w_decodestate);
if (c == -1)
continue;
if (c == -2)
{
c = UCS_REPL;
/* try char again */
buf--;
len++;
}
if (c > 0xff)
debug1("read UNICODE %04x\n", c);
}
#endif
tryagain:
switch (curr->w_state)
{
case PRIN:
switch (c)
{
case '\033':
curr->w_state = PRINESC;
break;
default:
PrintChar(c);
}
break;
case PRINESC:
switch (c)
{
case '[':
curr->w_state = PRINCSI;
break;
default:
PrintChar('\033');
PrintChar(c);
curr->w_state = PRIN;
}
break;
case PRINCSI:
switch (c)
{
case '4':
curr->w_state = PRIN4;
break;
default:
PrintChar('\033');
PrintChar('[');
PrintChar(c);
curr->w_state = PRIN;
}
break;
case PRIN4:
switch (c)
{
case 'i':
curr->w_state = LIT;
PrintFlush();
if (curr->w_pdisplay && curr->w_pdisplay->d_printfd >= 0)
{
close(curr->w_pdisplay->d_printfd);
curr->w_pdisplay->d_printfd = -1;
}
curr->w_pdisplay = 0;
break;
default:
PrintChar('\033');
PrintChar('[');
PrintChar('4');
PrintChar(c);
curr->w_state = PRIN;
}
break;
case ASTR:
if (c == 0)
break;
if (c == '\033')
{
curr->w_state = STRESC;
break;
}
/* special xterm hack: accept SetStatus sequence. Yucc! */
/* allow ^E for title escapes */
if (!(curr->w_StringType == OSC && c < ' ' && c != '\005'))
if (!curr->w_c1 || c != ('\\' ^ 0xc0))
{
StringChar(c);
break;
}
c = '\\';
/* FALLTHROUGH */
case STRESC:
switch (c)
{
case '\\':
if (StringEnd() == 0 || len <= 1)
break;
/* check if somewhere a status is displayed */
for (cv = curr->w_layer.l_cvlist; cv; cv = cv->c_lnext)
{
display = cv->c_display;
if (D_status == STATUS_ON_WIN)
break;
}
if (cv)
{
if (len > IOSIZE + 1)
len = IOSIZE + 1;
curr->w_outlen = len - 1;
bcopy(buf, curr->w_outbuf, len - 1);
return; /* wait till status is gone */
}
break;
case '\033':
StringChar('\033');
break;
default:
curr->w_state = ASTR;
StringChar('\033');
StringChar(c);
break;
}
break;
case ESC:
switch (c)
{
case '[':
curr->w_NumArgs = 0;
curr->w_intermediate = 0;
bzero((char *) curr->w_args, MAXARGS * sizeof(int));
curr->w_state = CSI;
break;
case ']':
StringStart(OSC);
break;
case '_':
StringStart(APC);
break;
case 'P':
StringStart(DCS);
break;
case '^':
StringStart(PM);
break;
case '!':
StringStart(GM);
break;
case '"':
case 'k':
StringStart(AKA);
break;
default:
if (Special(c))
{
curr->w_state = LIT;
break;
}
debug1("not special. c = %x\n", c);
if (c >= ' ' && c <= '/')
{
if (curr->w_intermediate)
{
#ifdef DW_CHARS
if (curr->w_intermediate == '$')
c |= '$' << 8;
else
#endif
c = -1;
}
curr->w_intermediate = c;
}
else if (c >= '0' && c <= '~')
{
DoESC(c, curr->w_intermediate);
curr->w_state = LIT;
}
else
{
curr->w_state = LIT;
goto tryagain;
}
}
break;
case CSI:
switch (c)
{
case '0': case '1': case '2': case '3': case '4':
case '5': case '6': case '7': case '8': case '9':
if (curr->w_NumArgs < MAXARGS)
{
if (curr->w_args[curr->w_NumArgs] < 100000000)
curr->w_args[curr->w_NumArgs] =
10 * curr->w_args[curr->w_NumArgs] + (c - '0');
}
break;
case ';':
case ':':
if (curr->w_NumArgs < MAXARGS)
curr->w_NumArgs++;
break;
default:
if (Special(c))
break;
if (c >= '@' && c <= '~')
{
if (curr->w_NumArgs < MAXARGS)
curr->w_NumArgs++;
DoCSI(c, curr->w_intermediate);
if (curr->w_state != PRIN)
curr->w_state = LIT;
}
else if ((c >= ' ' && c <= '/') || (c >= '<' && c <= '?'))
curr->w_intermediate = curr->w_intermediate ? -1 : c;
else
{
curr->w_state = LIT;
goto tryagain;
}
}
break;
case LIT:
default:
#ifdef DW_CHARS
if (curr->w_mbcs)
if (c <= ' ' || c == 0x7f || (c >= 0x80 && c < 0xa0 && curr->w_c1))
curr->w_mbcs = 0;
#endif
if (c < ' ')
{
if (c == '\033')
{
curr->w_intermediate = 0;
curr->w_state = ESC;
if (curr->w_autoaka < 0)
curr->w_autoaka = 0;
}
else
Special(c);
break;
}
if (c >= 0x80 && c < 0xa0 && curr->w_c1)
#ifdef FONT
if ((curr->w_FontR & 0xf0) != 0x20
# ifdef UTF8
|| curr->w_encoding == UTF8
# endif
)
#endif
{
switch (c)
{
case 0xc0 ^ 'D':
case 0xc0 ^ 'E':
case 0xc0 ^ 'H':
case 0xc0 ^ 'M':
case 0xc0 ^ 'N': /* SS2 */
case 0xc0 ^ 'O': /* SS3 */
DoESC(c ^ 0xc0, 0);
break;
case 0xc0 ^ '[':
if (curr->w_autoaka < 0)
curr->w_autoaka = 0;
curr->w_NumArgs = 0;
curr->w_intermediate = 0;
bzero((char *) curr->w_args, MAXARGS * sizeof(int));
curr->w_state = CSI;
break;
case 0xc0 ^ 'P':
StringStart(DCS);
break;
default:
break;
}
break;
}
#ifdef FONT
# ifdef DW_CHARS
if (!curr->w_mbcs)
{
# endif
if (c < 0x80 || curr->w_gr == 0)
curr->w_rend.font = curr->w_FontL;
# ifdef ENCODINGS
else if (curr->w_gr == 2 && !curr->w_ss)
curr->w_rend.font = curr->w_FontE;
# endif
else
curr->w_rend.font = curr->w_FontR;
# ifdef DW_CHARS
}
# endif
# ifdef UTF8
if (curr->w_encoding == UTF8)
{
if (curr->w_rend.font == '0')
{
struct mchar mc, *mcp;
debug1("SPECIAL %x\n", c);
mc.image = c;
mc.mbcs = 0;
mc.font = '0';
mc.fontx = 0;
mcp = recode_mchar(&mc, 0, UTF8);
debug2("%02x %02x\n", mcp->image, mcp->font);
c = mcp->image | mcp->font << 8;
}
curr->w_rend.font = 0;
}
# ifdef DW_CHARS
if (curr->w_encoding == UTF8 && utf8_isdouble(c))
curr->w_mbcs = 0xff;
# endif
if (curr->w_encoding == UTF8 && c >= 0x0300 && utf8_iscomb(c))
{
int ox, oy;
struct mchar omc;
ox = curr->w_x - 1;
oy = curr->w_y;
if (ox < 0)
{
ox = curr->w_width - 1;
oy--;
}
if (oy < 0)
oy = 0;
copy_mline2mchar(&omc, &curr->w_mlines[oy], ox);
if (omc.image == 0xff && omc.font == 0xff && omc.fontx == 0)
{
ox--;
if (ox >= 0)
{
copy_mline2mchar(&omc, &curr->w_mlines[oy], ox);
omc.mbcs = 0xff;
}
}
if (ox >= 0)
{
utf8_handle_comb(c, &omc);
MFixLine(curr, oy, &omc);
copy_mchar2mline(&omc, &curr->w_mlines[oy], ox);
LPutChar(&curr->w_layer, &omc, ox, oy);
LGotoPos(&curr->w_layer, curr->w_x, curr->w_y);
}
break;
}
font = curr->w_rend.font;
# endif
# ifdef DW_CHARS
# ifdef ENCODINGS
if (font == KANA && curr->w_encoding == SJIS && curr->w_mbcs == 0)
{
/* Lets see if it is the first byte of a kanji */
debug1("%x may be first of SJIS\n", c);
if ((0x81 <= c && c <= 0x9f) || (0xe0 <= c && c <= 0xef))
{
debug("YES!\n");
curr->w_mbcs = c;
break;
}
}
# endif
if (font == 031 && c == 0x80 && !curr->w_mbcs)
font = curr->w_rend.font = 0;
if (is_dw_font(font) && c == ' ')
font = curr->w_rend.font = 0;
if (is_dw_font(font) || curr->w_mbcs)
{
int t = c;
if (curr->w_mbcs == 0)
{
curr->w_mbcs = c;
break;
}
if (curr->w_x == cols - 1)
{
curr->w_x += curr->w_wrap ? 1 : -1;
debug1("Patched w_x to %d\n", curr->w_x);
}
# ifdef UTF8
if (curr->w_encoding != UTF8)
# endif
{
c = curr->w_mbcs;
# ifdef ENCODINGS
if (font == KANA && curr->w_encoding == SJIS)
{
debug2("SJIS !! %x %x\n", c, t);
/*
* SJIS -> EUC mapping:
* First byte:
* 81,82...9f -> 21,23...5d
* e0,e1...ef -> 5f,61...7d
* Second byte:
* 40-7e -> 21-5f
* 80-9e -> 60-7e
* 9f-fc -> 21-7e (increment first byte!)
*/
if (0x40 <= t && t <= 0xfc && t != 0x7f)
{
if (c <= 0x9f) c = (c - 0x81) * 2 + 0x21;
else c = (c - 0xc1) * 2 + 0x21;
if (t <= 0x7e) t -= 0x1f;
else if (t <= 0x9e) t -= 0x20;
else t -= 0x7e, c++;
curr->w_rend.font = KANJI;
}
else
{
/* Incomplete shift-jis - skip first byte */
c = t;
t = 0;
}
debug2("SJIS after %x %x\n", c, t);
}
# endif
if (t && curr->w_gr && font != 030 && font != 031)
{
t &= 0x7f;
if (t < ' ')
goto tryagain;
}
if (t == '\177')
break;
curr->w_mbcs = t;
}
}
# endif /* DW_CHARS */
if (font == '<' && c >= ' ')
{
font = curr->w_rend.font = 0;
c |= 0x80;
}
# ifdef UTF8
else if (curr->w_gr && curr->w_encoding != UTF8)
# else
else if (curr->w_gr)
# endif
{
#ifdef ENCODINGS
if (c == 0x80 && font == 0 && curr->w_encoding == GBK)
c = 0xa4;
else
c &= 0x7f;
if (c < ' ' && font != 031)
goto tryagain;
#else
c &= 0x7f;
if (c < ' ') /* this is ugly but kanji support */
goto tryagain; /* prevents nicer programming */
#endif
}
#endif /* FONT */
if (c == '\177')
break;
curr->w_rend.image = c;
#ifdef UTF8
if (curr->w_encoding == UTF8)
{
curr->w_rend.font = c >> 8;
curr->w_rend.fontx = c >> 16;
}
#endif
#ifdef DW_CHARS
curr->w_rend.mbcs = curr->w_mbcs;
#endif
if (curr->w_x < cols - 1)
{
if (curr->w_insert)
{
save_mline(&curr->w_mlines[curr->w_y], cols);
MInsChar(curr, &curr->w_rend, curr->w_x, curr->w_y);
LInsChar(&curr->w_layer, &curr->w_rend, curr->w_x, curr->w_y, &mline_old);
curr->w_x++;
}
else
{
MPutChar(curr, &curr->w_rend, curr->w_x, curr->w_y);
LPutChar(&curr->w_layer, &curr->w_rend, curr->w_x, curr->w_y);
curr->w_x++;
}
}
else if (curr->w_x == cols - 1)
{
MPutChar(curr, &curr->w_rend, curr->w_x, curr->w_y);
LPutChar(&curr->w_layer, &curr->w_rend, curr->w_x, curr->w_y);
if (curr->w_wrap)
curr->w_x++;
}
else
{
MWrapChar(curr, &curr->w_rend, curr->w_y, curr->w_top, curr->w_bot, curr->w_insert);
LWrapChar(&curr->w_layer, &curr->w_rend, curr->w_y, curr->w_top, curr->w_bot, curr->w_insert);
if (curr->w_y != curr->w_bot && curr->w_y != curr->w_height - 1)
curr->w_y++;
curr->w_x = 1;
}
#ifdef FONT
# ifdef DW_CHARS
if (curr->w_mbcs)
{
curr->w_rend.mbcs = curr->w_mbcs = 0;
curr->w_x++;
}
# endif
if (curr->w_ss)
{
curr->w_FontL = curr->w_charsets[curr->w_Charset];
curr->w_FontR = curr->w_charsets[curr->w_CharsetR];
curr->w_rend.font = curr->w_FontL;
LSetRendition(&curr->w_layer, &curr->w_rend);
curr->w_ss = 0;
}
#endif /* FONT */
break;
}
}
while (--len);
}
if (!printcmd && curr->w_state == PRIN)
PrintFlush();
}
static void
WLogString(p, buf, len)
struct win *p;
char *buf;
int len;
{
if (!p->w_log)
return;
if (logtstamp_on && p->w_logsilence >= logtstamp_after * 2)
{
char *t = MakeWinMsg(logtstamp_string, p, '%');
logfwrite(p->w_log, t, strlen(t)); /* long time no write */
}
p->w_logsilence = 0;
if (logfwrite(p->w_log, buf, len) < 1)
{
WMsg(p, errno, "Error writing logfile");
logfclose(p->w_log);
p->w_log = 0;
}
if (!log_flush)
logfflush(p->w_log);
}
static int
Special(c)
register int c;
{
switch (c)
{
case '\b':
BackSpace();
return 1;
case '\r':
Return();
return 1;
case '\n':
if (curr->w_autoaka)
FindAKA();
case '\013': /* Vertical tab is the same as Line Feed */
LineFeed(0);
return 1;
case '\007':
WBell(curr, visual_bell);
return 1;
case '\t':
ForwardTab();
return 1;
#ifdef FONT
case '\017': /* SI */
MapCharset(G0);
return 1;
case '\016': /* SO */
MapCharset(G1);
return 1;
#endif
}
return 0;
}
static void
DoESC(c, intermediate)
int c, intermediate;
{
debug2("DoESC: %x - inter = %x\n", c, intermediate);
switch (intermediate)
{
case 0:
switch (c)
{
case 'E':
LineFeed(1);
break;
case 'D':
LineFeed(0);
break;
case 'M':
ReverseLineFeed();
break;
case 'H':
curr->w_tabs[curr->w_x] = 1;
break;
case 'Z': /* jph: Identify as VT100 */
Report("\033[?%d;%dc", 1, 2);
break;
case '7':
SaveCursor(&curr->w_saved);
break;
case '8':
RestoreCursor(&curr->w_saved);
break;
case 'c':
ClearScreen();