-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathTerminal.c
More file actions
2416 lines (2198 loc) · 62.1 KB
/
Copy pathTerminal.c
File metadata and controls
2416 lines (2198 loc) · 62.1 KB
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
#include <windows.h>
#include <stdio.h>
#include <stdlib.h>
#include <ctype.h>
#include "putty.h"
#include "powdefines.h"
#include "powhook.h"
#include "powmain.h"
#include "powedit.h"
#include "powlog.h"
#define CL_ANSIMIN 0x0001 /* Codes in all ANSI like terminals. */
#define CL_VT100 0x0002 /* VT100 */
#define CL_VT100AVO 0x0004 /* VT100 +AVO; 132x24 (not 132x14) & attrs */
#define CL_VT102 0x0008 /* VT102 */
#define CL_VT220 0x0010 /* VT220 */
#define CL_VT320 0x0020 /* VT320 */
#define CL_VT420 0x0040 /* VT420 */
#define CL_VT510 0x0080 /* VT510, NB VT510 includes ANSI */
#define CL_VT340TEXT 0x0100 /* VT340 extensions that appear in the VT420 */
#define CL_SCOANSI 0x1000 /* SCOANSI not in ANSIMIN. */
#define CL_ANSI 0x2000 /* ANSI ECMA-48 not in the VT100..VT420 */
#define CL_OTHER 0x4000 /* Others, Xterm, linux, putty, dunno, etc */
#define TM_VT100 (CL_ANSIMIN|CL_VT100)
#define TM_VT100AVO (TM_VT100|CL_VT100AVO)
#define TM_VT102 (TM_VT100AVO|CL_VT102)
#define TM_VT220 (TM_VT102|CL_VT220)
#define TM_VTXXX (TM_VT220|CL_VT340TEXT|CL_VT510|CL_VT420|CL_VT320)
#define TM_SCOANSI (CL_ANSIMIN|CL_SCOANSI)
#define TM_PUTTY (0xFFFF)
#define compatibility(x) \
if ( ((CL_##x)&compatibility_level) == 0 ) { \
termstate=TOPLEVEL; \
break; \
}
#define compatibility2(x,y) \
if ( ((CL_##x|CL_##y)&compatibility_level) == 0 ) { \
termstate=TOPLEVEL; \
break; \
}
#define has_compat(x) ( ((CL_##x)&compatibility_level) != 0 )
static int compatibility_level = TM_PUTTY;
static unsigned long *text; /* buffer of text on terminal screen */
static unsigned long *scrtop; /* top of working screen */
static unsigned long *disptop; /* top of displayed screen */
static unsigned long *sbtop; /* top of scrollback */
static unsigned long *sbbot; /* furthest extent of scrollback */
static unsigned long *cpos; /* cursor position (convenience) */
static unsigned long *disptext; /* buffer of text on real screen */
static unsigned long *wanttext; /* buffer of text we want on screen */
static unsigned long *alttext; /* buffer of text on alt. screen */
static unsigned char *selspace; /* buffer for building selections in */
#define TSIZE (sizeof(*text))
#define fix_cpos do { cpos = scrtop + curs_y * (cols+1) + curs_x; } while(0)
static unsigned long curr_attr, save_attr;
static unsigned long erase_char = ERASE_CHAR;
static int curs_x, curs_y; /* cursor */
static int save_x, save_y; /* saved cursor position */
static int marg_t, marg_b; /* scroll margins */
static int dec_om; /* DEC origin mode flag */
static int wrap, wrapnext; /* wrap flags */
static int insert; /* insert-mode flag */
static int cset; /* 0 or 1: which char set */
static int save_cset, save_csattr; /* saved with cursor position */
static int rvideo; /* global reverse video flag */
static int cursor_on; /* cursor enabled flag */
static int reset_132; /* Flag ESC c resets to 80 cols */
static int use_bce; /* Use Background coloured erase */
static int blinker; /* When blinking is the cursor on ? */
static int tblinker; /* When the blinking text is on */
static int blink_is_real; /* Actually blink blinking text */
static unsigned long cset_attr[2];
/*
* Saved settings on the alternate screen.
*/
static int alt_x, alt_y, alt_om, alt_wrap, alt_wnext, alt_ins, alt_cset;
static int alt_t, alt_b;
static int alt_which;
#define ARGS_MAX 32 /* max # of esc sequence arguments */
#define ARG_DEFAULT 0 /* if an arg isn't specified */
#define def(a,d) ( (a) == ARG_DEFAULT ? (d) : (a) )
static int esc_args[ARGS_MAX];
static int esc_nargs;
static int esc_query;
#define ANSI(x,y) ((x)+((y)<<8))
#define ANSI_QUE(x) ANSI(x,TRUE)
#define OSC_STR_MAX 2048
static int osc_strlen;
static char osc_string[OSC_STR_MAX+1];
static int osc_w;
static char id_string[1024] = "\033[?6c";
static unsigned char *tabs;
static enum {
TOPLEVEL,
SEEN_ESC,
SEEN_CSI,
SEEN_OSC,
SEEN_OSC_W,
DO_CTRLS,
IGNORE_NEXT,
SET_GL, SET_GR,
SEEN_OSC_P,
OSC_STRING, OSC_MAYBE_ST,
SEEN_ESCHASH,
VT52_ESC,
VT52_Y1,
VT52_Y2
} termstate;
static enum {
NO_SELECTION, ABOUT_TO, DRAGGING, SELECTED
} selstate;
static enum {
SM_CHAR, SM_WORD, SM_LINE
} selmode;
static unsigned long *selstart, *selend, *selanchor;
static short wordness[256] = {
0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0, 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0, /* 01 */
0,1,2,1,1,1,1,1,1,1,1,1,1,2,2,2, 2,2,2,2,2,2,2,2,2,2,1,1,1,1,1,1, /* 23 */
1,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2, 2,2,2,2,2,2,2,2,2,2,2,1,1,1,1,2, /* 45 */
1,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2, 2,2,2,2,2,2,2,2,2,2,2,1,1,1,1,1, /* 67 */
1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1, 1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1, /* 89 */
1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1, 1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1, /* AB */
2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2, 2,2,2,2,2,2,2,1,2,2,2,2,2,2,2,2, /* CD */
2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2, 2,2,2,2,2,2,2,1,2,2,2,2,2,2,2,2, /* EF */
};
static unsigned char sel_nl[] = SEL_NL;
static char * paste_buffer = 0;
static int paste_len, paste_pos, paste_hold;
static char telnet_text[2048];
static int telnet_text_len;
/*
* Internal prototypes.
*/
static void do_paint (Context, int);
static void erase_lots (int, int, int);
static void swap_screen (int);
static void update_sbar (void);
static void deselect (void);
/*
* Set up power-on settings for the terminal.
*/
static void power_on(void) {
curs_x = curs_y = alt_x = alt_y = save_x = save_y = 0;
alt_t = marg_t = 0;
if (rows != -1)
alt_b = marg_b = rows - 1;
else
alt_b = marg_b = 0;
if (cols != -1) {
int i;
for (i = 0; i < cols; i++)
tabs[i] = (i % 8 == 0 ? TRUE : FALSE);
}
alt_om = dec_om = cfg.dec_om;
alt_wnext = wrapnext = alt_ins = insert = FALSE;
alt_wrap = wrap = cfg.wrap_mode;
alt_cset = cset = 0;
cset_attr[0] = cset_attr[1] = ATTR_ASCII;
rvideo = 0;
cursor_on = 1;
save_attr = curr_attr = ATTR_DEFAULT;
app_cursor_keys = cfg.app_cursor;
app_keypad_keys = cfg.app_keypad;
use_bce = cfg.bce;
blink_is_real = cfg.blinktext;
erase_char = ERASE_CHAR;
alt_which = 0;
{
int i;
for (i = 0; i < 256; i++)
wordness[i] = cfg.wordness[i];
}
if (text) {
swap_screen (1);
erase_lots (FALSE, TRUE, TRUE);
swap_screen (0);
erase_lots (FALSE, TRUE, TRUE);
}
}
/*
* Force a screen update.
*/
void term_update(void) {
Context ctx;
ctx = get_ctx();
if (ctx) {
if ( (seen_key_event && (cfg.scroll_on_key)) ||
(seen_disp_event && (cfg.scroll_on_disp)) ) {
disptop = scrtop;
seen_disp_event = seen_key_event = 0;
update_sbar();
}
do_paint (ctx, TRUE);
sys_cursor(curs_x, curs_y + (scrtop - disptop) / (cols+1));
free_ctx (ctx);
}
}
/*
* Same as power_on(), but an external function.
*/
void term_pwron(void) {
power_on();
fix_cpos;
disptop = scrtop;
deselect();
term_update();
}
/*
* Clear the scrollback.
*/
void term_clrsb(void) {
disptop = sbtop = scrtop;
update_sbar();
}
/*
* Initialise the terminal.
*/
void term_init(void) {
text = sbtop = sbbot = scrtop = disptop = cpos = NULL;
disptext = wanttext = NULL;
tabs = NULL;
selspace = NULL;
deselect();
rows = cols = -1;
power_on();
}
/*
* Set up the terminal for a given size.
*/
void term_size(int newrows, int newcols, int newsavelines) {
unsigned long *newtext, *newdisp, *newwant, *newalt;
int i, j, crows, ccols;
int save_alt_which = alt_which;
if (newrows == rows && newcols == cols && newsavelines == savelines)
return; /* nothing to do */
deselect();
swap_screen(0);
alt_t = marg_t = 0;
alt_b = marg_b = newrows - 1;
newtext = smalloc ((newrows+newsavelines)*(newcols+1)*TSIZE);
sbbot = newtext + newsavelines*(newcols+1);
for (i=0; i<(newrows+newsavelines)*(newcols+1); i++)
newtext[i] = erase_char;
if (rows != -1) {
crows = rows + (scrtop - sbtop) / (cols+1);
if (crows > newrows+newsavelines)
crows = newrows+newsavelines;
if (newrows>crows)
disptop = newtext;
else
disptop = newtext + (crows-newrows)*(newcols+1);
ccols = (cols < newcols ? cols : newcols);
for (i=0; i<crows; i++) {
int oldidx = (rows - crows + i) * (cols+1);
int newidx = (newrows - crows + i) * (newcols+1);
for (j=0; j<ccols; j++)
disptop[newidx+j] = scrtop[oldidx+j];
disptop[newidx+newcols] =
(cols == newcols ? scrtop[oldidx+cols]
: (scrtop[oldidx+cols]&LATTR_MODE));
}
sbtop = newtext;
} else {
sbtop = disptop = newtext;
}
scrtop = disptop;
sfree (text);
text = newtext;
newdisp = smalloc (newrows*(newcols+1)*TSIZE);
for (i=0; i<newrows*(newcols+1); i++)
newdisp[i] = ATTR_INVALID;
sfree (disptext);
disptext = newdisp;
newwant = smalloc (newrows*(newcols+1)*TSIZE);
for (i=0; i<newrows*(newcols+1); i++)
newwant[i] = ATTR_INVALID;
sfree (wanttext);
wanttext = newwant;
newalt = smalloc (newrows*(newcols+1)*TSIZE);
for (i=0; i<newrows*(newcols+1); i++)
newalt[i] = erase_char;
sfree (alttext);
alttext = newalt;
sfree (selspace);
selspace = smalloc ( (newrows+newsavelines) * (newcols+sizeof(sel_nl)) );
tabs = srealloc (tabs, newcols*sizeof(*tabs));
{
int i;
for (i = (cols > 0 ? cols : 0); i < newcols; i++)
tabs[i] = (i % 8 == 0 ? TRUE : FALSE);
}
if (rows > 0)
curs_y += newrows - rows;
if (curs_y < 0)
curs_y = 0;
if (curs_y >= newrows)
curs_y = newrows-1;
if (curs_x >= newcols)
curs_x = newcols-1;
alt_x = alt_y = 0;
wrapnext = alt_wnext = FALSE;
rows = newrows;
cols = newcols;
savelines = newsavelines;
fix_cpos;
swap_screen(save_alt_which);
update_sbar();
term_update();
}
/*
* Swap screens.
*/
static void swap_screen (int which) {
int t;
unsigned long tt;
if (which == alt_which)
return;
alt_which = which;
for (t=0; t<rows*(cols+1); t++) {
tt = scrtop[t]; scrtop[t] = alttext[t]; alttext[t] = tt;
}
t = curs_x; curs_x = alt_x; alt_x = t;
t = curs_y; curs_y = alt_y; alt_y = t;
t = marg_t; marg_t = alt_t; alt_t = t;
t = marg_b; marg_b = alt_b; alt_b = t;
t = dec_om; dec_om = alt_om; alt_om = t;
t = wrap; wrap = alt_wrap; alt_wrap = t;
t = wrapnext; wrapnext = alt_wnext; alt_wnext = t;
t = insert; insert = alt_ins; alt_ins = t;
t = cset; cset = alt_cset; alt_cset = t;
fix_cpos;
}
/*
* Update the scroll bar.
*/
static void update_sbar(void) {
int min;
min = (sbtop - text) / (cols+1);
set_sbar ((scrtop - text) / (cols+1) + rows - min,
(disptop - text) / (cols+1) - min,
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 (unsigned long *from, unsigned long *to) {
if (from < selend && selstart < to)
deselect();
}
/*
* 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 (int topline, int botline, int lines, int sb) {
unsigned long *scroll_top;
unsigned long *newscr;
int scroll_size, size, i;
int scrtop_is_disptop = (scrtop==disptop);
static int recursive = 0;
/* Only scroll more than the window if we're doing a 10% scroll */
if (!recursive && lines > botline - topline + 1)
lines = botline - topline + 1;
scroll_top = scrtop + topline*(cols+1);
size = (lines < 0 ? -lines : lines) * (cols+1);
scroll_size = (botline - topline + 1) * (cols+1) - size;
if (lines > 0 && topline == 0 && alt_which == 0 && sb) {
/*
* Since we're going to scroll the top line and we're on the
* scrolling screen let's also effect the scrollback buffer.
*
* This is normally done by moving the position the screen
* painter reads from to reduce the amount of memory copying
* required.
*/
if (scroll_size >= 0 && !recursive) {
newscr = scrtop + lines * (cols+1);
if (newscr > sbbot && botline == rows-1) {
/* We've hit the bottom of memory, so we have to do a
* physical scroll. But instead of just 1 line do it
* by 10% of the available memory.
*
* If the scroll region isn't the whole screen then we can't
* do this as it stands. We would need to recover the bottom
* of the screen from the scroll buffer after being sure that
* it doesn't get deleted.
*/
i = (rows+savelines)/10;
/* Make it simple and ensure safe recursion */
if ( i<savelines-1) {
recursive ++;
scroll(topline, botline, i, sb);
recursive --;
newscr = scrtop - i * (cols+1);
if (scrtop_is_disptop) disptop = newscr;
scrtop = newscr;
}
newscr = scrtop + lines * (cols+1);
}
if (newscr <= sbbot) {
if (scrtop_is_disptop) disptop = newscr;
scrtop = newscr;
if (botline == rows-1 )
for (i = 0; i < size; i++)
scrtop[i+scroll_size] = erase_char;
update_sbar();
fix_cpos;
if (botline != rows-1) {
/* This fastscroll only works for full window scrolls.
* If the caller wanted a partial one we have to reverse
* scroll the bottom of the screen.
*/
scroll(botline-lines+1, rows-1, -lines, 0);
}
return ;
}
/* If we can't scroll by memory remapping do it physically.
* But rather than expensivly doing the scroll buffer just
* scroll the screen. All it means is that sometimes we choose
* to not add lines from a scroll region to the scroll buffer.
*/
if (savelines <= 400) {
sbtop -= lines * (cols+1);
if (sbtop < text)
sbtop = text;
scroll_size += scroll_top - sbtop;
scroll_top = sbtop;
update_sbar();
}
} else {
/* Ho hum, expensive scroll required. */
sbtop -= lines * (cols+1);
if (sbtop < text)
sbtop = text;
scroll_size += scroll_top - sbtop;
scroll_top = sbtop;
update_sbar();
}
}
if (scroll_size < 0) {
size += scroll_size;
scroll_size = 0;
}
if (lines > 0) {
if (scroll_size)
memmove (scroll_top, scroll_top + size, scroll_size*TSIZE);
for (i = 0; i < size; i++)
scroll_top[i+scroll_size] = erase_char;
if (selstart > scroll_top &&
selstart < scroll_top + size + scroll_size) {
selstart -= size;
if (selstart < scroll_top)
selstart = scroll_top;
}
if (selend > scroll_top &&
selend < scroll_top + size + scroll_size) {
selend -= size;
if (selend < scroll_top)
selend = scroll_top;
}
if (scrtop_is_disptop)
disptop = scrtop;
else
if (disptop > scroll_top &&
disptop < scroll_top + size + scroll_size) {
disptop -= size;
if (disptop < scroll_top)
disptop = scroll_top;
}
} else {
if (scroll_size)
memmove (scroll_top + size, scroll_top, scroll_size*TSIZE);
for (i = 0; i < size; i++)
scroll_top[i] = erase_char;
if (selstart > scroll_top &&
selstart < scroll_top + size + scroll_size) {
selstart += size;
if (selstart > scroll_top + size + scroll_size)
selstart = scroll_top + size + scroll_size;
}
if (selend > scroll_top &&
selend < scroll_top + size + scroll_size) {
selend += size;
if (selend > scroll_top + size + scroll_size)
selend = scroll_top + size + scroll_size;
}
if (scrtop_is_disptop)
disptop = scrtop;
else if (disptop > scroll_top &&
disptop < scroll_top + size + scroll_size) {
disptop += size;
if (disptop > scroll_top + size + scroll_size)
disptop = scroll_top + size + scroll_size;
}
}
}
/*
* 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 (int x, int y, int marg_clip) {
if (x < 0)
x = 0;
if (x >= cols)
x = cols-1;
if (marg_clip) {
if ((curs_y >= marg_t || marg_clip == 2) && y < marg_t)
y = marg_t;
if ((curs_y <= marg_b || marg_clip == 2) && y > marg_b)
y = marg_b;
}
if (y < 0)
y = 0;
if (y >= rows)
y = rows-1;
curs_x = x;
curs_y = y;
fix_cpos;
wrapnext = FALSE;
}
void move_for_pow (int x) {
int y=curs_y;
x+=curs_x;
if (x>=cols) {
while (x>=cols) {
x-=cols;
y+=1;
}
} else if (x<0) {
while (x<0) {
x+=cols;
y-=1;
}
}
if (x<0) x=0;
if (y>=rows) {
y=rows-1;
scroll(marg_t,marg_b,1,TRUE);
}
if (y<0) y=0;
move(x,y,0);
return;
}
/*
* Save or restore the cursor and SGR mode.
*/
static void save_cursor(int save) {
if (save) {
save_x = curs_x;
save_y = curs_y;
save_attr = curr_attr;
save_cset = cset;
save_csattr = cset_attr[cset];
} else {
curs_x = save_x;
curs_y = save_y;
/* Make sure the window hasn't shrunk since the save */
if (curs_x >= cols) curs_x = cols-1;
if (curs_y >= rows) curs_y = rows-1;
curr_attr = save_attr;
cset = save_cset;
cset_attr[cset] = save_csattr;
fix_cpos;
if (use_bce) erase_char = (' ' |(curr_attr&(ATTR_FGMASK|ATTR_BGMASK)));
}
}
/*
* Erase a large portion of the screen: the whole screen, or the
* whole line, or parts thereof.
*/
static void erase_lots (int line_only, int from_begin, int to_end) {
unsigned long *startpos, *endpos;
if (line_only) {
startpos = cpos - curs_x;
endpos = startpos + cols;
/* I've removed the +1 so that the Wide screen stuff is not
* removed when it shouldn't be.
*/
} else {
startpos = scrtop;
endpos = startpos + rows * (cols+1);
}
if (!from_begin)
startpos = cpos;
if (!to_end)
endpos = cpos+1;
check_selection (startpos, endpos);
/* Clear screen also forces a full window redraw, just in case. */
if (startpos == scrtop && endpos == scrtop + rows * (cols+1))
term_invalidate();
while (startpos < endpos)
*startpos++ = erase_char;
}
/*
* Insert or delete characters within the current line. n is +ve if
* insertion is desired, and -ve for deletion.
*/
void insch (int n) {
int dir = (n < 0 ? -1 : +1);
int m;
int do_next_line=0; // added by billj for input lines that wrap around
n = (n < 0 ? -n : n);
if ((n > cols - curs_x) && (dir>0)) {
do_next_line=1;
n = cols - curs_x;
}
m = cols - curs_x - n;
if (m<0) {
m+=cols;
}
check_selection (cpos, cpos+n);
if (dir < 0) {
memmove (cpos, cpos+n, m*TSIZE);
while (n--)
cpos[m++] = erase_char;
} else {
memmove (cpos+n, cpos, m*TSIZE);
while (n--)
cpos[n] = erase_char;
}
/* if (do_next_line_too) {
int save_y = curs_y;
int save_x= curs_x;
curs_y++;
curs_x=0;
fix_cpos;
}
*/
/*original code
n = (n < 0 ? -n : n);
if (n > cols - curs_x) {
n = cols - curs_x;
do_next_line_too=1;
}
m = cols - curs_x - n;
check_selection (cpos, cpos+n);
if (dir < 0) {
memmove (cpos, cpos+n, m*TSIZE);
while (n--)
cpos[m++] = erase_char;
} else {
memmove (cpos+n, cpos, m*TSIZE);
while (n--)
cpos[n] = 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 (int mode, int query, int state) {
if (query) switch (mode) {
case 1: /* application cursor keys */
app_cursor_keys = state;
break;
case 2: /* VT52 mode */
vt52_mode = !state;
break;
case 3: /* 80/132 columns */
deselect();
request_resize (state ? 132 : 80, rows, 1);
reset_132 = state;
break;
case 5: /* reverse video */
rvideo = state;
seen_disp_event = TRUE;
if (state) term_update();
break;
case 6: /* DEC origin mode */
dec_om = state;
break;
case 7: /* auto wrap */
wrap = state;
break;
case 8: /* auto key repeat */
repeat_off = !state;
break;
case 25: /* enable/disable cursor */
compatibility2(OTHER,VT220);
cursor_on = state;
seen_disp_event = TRUE;
break;
case 47: /* alternate screen */
compatibility(OTHER);
deselect();
swap_screen (state);
disptop = scrtop;
break;
} else switch (mode) {
case 4: /* set insert mode */
compatibility(VT102);
insert = state;
break;
case 12: /* set echo mode */
/*
* This may be very good in smcup and rmcup (or smkx & rmkx) if you
* have a long RTT and the telnet client/daemon doesn't understand
* linemode.
*
* DONT send TS_RECHO/TS_LECHO; the telnet daemon tries to fix the
* tty and _really_ confuses some programs.
*/
compatibility2(OTHER,VT220);
ldisc = (state? &ldisc_simple : &ldisc_term);
break;
case 20: /* Return sends ... */
cr_lf_return = state;
break;
}
}
/*
* Process an OSC sequence: set window title or icon name.
*/
static void do_osc(void) {
if (osc_w) {
while (osc_strlen--)
wordness[(unsigned char)osc_string[osc_strlen]] = esc_args[0];
} else {
osc_string[osc_strlen] = '\0';
switch (esc_args[0]) {
case 0:
case 1:
set_icon (osc_string);
if (esc_args[0] == 1)
break;
/* fall through: parameter 0 means set both */
case 2:
case 21:
set_title (osc_string);
break;
}
}
}
/*
* 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...
*/
void term_out(int windowflag) {
int c, inbuf_reap;
static int beep_overload = 0;
int beep_count = 0;
for(inbuf_reap = 0; inbuf_reap < inbuf_head; inbuf_reap++)
{
c = inbuf[inbuf_reap];
/*
* Optionally log the session traffic to a file. Useful for
* debugging and possibly also useful for actual logging.
*/
if (logfile) {
static FILE *fp = NULL;
if (!fp) fp = fopen(logfile, "wb");
if (fp) fputc (c, fp);
}
// powwow has a whole logging structure set up.. we'll use it
/* 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( ( (c&0x60) == 0 || c == '\177') &&
termstate < DO_CTRLS &&
( (c&0x80) == 0 || has_compat(VT220))) {
switch (c) {
case '\005': /* terminal type query */
/* Strictly speaking this is VT100 but a VT100 defaults to
* no response. Other terminals respond at their option.
*
* Don't put a CR in the default string as this tends to
* upset some weird software.
*
* An xterm returns "xterm" (5 characters)
*/
compatibility(OTHER);
ldisc->send ("PowTTY", 6);
break;
case '\007':
beep_count++;
if(beep_count>6) beep_overload=1;
disptop = scrtop;
break;
case '\b':
if (curs_x == 0 && curs_y == 0)
;
else if (curs_x == 0 && curs_y > 0)
curs_x = cols-1, curs_y--;
else if (wrapnext)
wrapnext = FALSE;
else
curs_x--;
fix_cpos;
seen_disp_event = TRUE;
break;
case '\016':
compatibility(VT100);
cset = 1;
break;
case '\017':
compatibility(VT100);
cset = 0;
break;
case '\033':
if (vt52_mode)
termstate = VT52_ESC;
else {
compatibility(ANSIMIN);
termstate = SEEN_ESC;
}
break;
case 0233:
compatibility(VT220);
termstate = SEEN_CSI;
esc_nargs = 1;
esc_args[0] = ARG_DEFAULT;
esc_query = FALSE;
break;
case 0235:
compatibility(VT220);
termstate = SEEN_OSC;
esc_args[0] = 0;
break;
case '\r':
curs_x = 0;
wrapnext = FALSE;
fix_cpos;
seen_disp_event = TRUE;
paste_hold = 0;
break;
case '\013':
case '\014':
compatibility(VT100);
case '\n':
log_write((char *)&c,1,1); // hook for the powwow logger - this is CLEAN - no ansi!
if (curs_y == marg_b)
scroll (marg_t, marg_b, 1, TRUE);
else if (curs_y < rows-1)
curs_y++;
if (cfg.lfhascr)
curs_x = 0;
fix_cpos;
wrapnext = FALSE;
seen_disp_event = 1;
paste_hold = 0;
break;
case '\t':
{
unsigned long *old_cpos = cpos;
unsigned long *p = scrtop + curs_y * (cols+1) + cols;
do {
curs_x++;
} while (curs_x < cols-1 && !tabs[curs_x]);
if ((*p & LATTR_MODE) != LATTR_NORM)
{
if (curs_x >= cols/2)
curs_x = cols/2-1;
}
else
{
if (curs_x >= cols)
curs_x = cols-1;
}
fix_cpos;
check_selection (old_cpos, cpos);
}
seen_disp_event = TRUE;
break;
case '\177': /* Destructive backspace
This does nothing on a real VT100 */
compatibility(OTHER);
if (curs_x && !wrapnext) curs_x--;
wrapnext = FALSE;
fix_cpos;
*cpos = (' ' | curr_attr | ATTR_ASCII);
break;
}
}
else switch (termstate) {
case TOPLEVEL:
/* Only graphic characters get this far, ctrls are stripped above */
if (wrapnext) {
cpos[1] |= ATTR_WRAPPED;
if (curs_y == marg_b)