forked from MidnightCommander/mc
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathsldisply.c
2239 lines (1923 loc) · 51 KB
/
sldisply.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) 1992, 1995 John E. Davis
* All rights reserved.
*
* You may distribute under the terms of either the GNU General Public
* License or the Perl Artistic License.
*/
/*
* SLTT_TRANSP_ACS_PATCH (#define/#undef):
*
* The problem: some terminals (e.g. QNX/qansi*) map the whole upper half of
* the ASCII table to the lower half, when alt-char-set is activated with
* the smacs/as string-sequence. This means, that if 0 <= ch < 128 written
* to the terminal, it will be translated to (ch+128) automatically by the
* terminal: so not only the line-drawing characters can be written, when
* the alt-char-set is activated. It implicitly means, that space, NL, CR,
* etc. characters (exactly: anything besides the "standard" line drawing
* characters) can not be written directly to the terminal, when the
* alt-char-set is activated, because writing these characters doesn't cause
* an implicit/temporary switching-back to the standard char-set!
*
* The original code in SLang assumes that space, NL, CR, etc. can be
* printed when alt-char-set is activated. If SLTT_TRANSP_ACS_PATCH is
* defined, the modified code will not use this assumption.
* [Remark: the patch-code is not the most exact solution, but works...]
*/
/*#define SLTT_TRANSP_ACS_PATCH 1*/
/*
* QNX_QANSI_SLANG_COMPAT_ACS (#define/#undef):
*
* A more OS/terminal-specific solution for the problem mentioned above
* (->SLTT_TRANSP_ACS_PATCH).
*
* If QNX_QANSI_SLANG_COMPAT_ACS is defined, the default smacs/sa, rmacs/ae,
* acsc/ac [and sgr/sa, if it would be used!] command sequences will be
* replaced internally with the "old style" (pre-QNX 4.23) sequences in case
* of QNX/qansi terminals. Using these optional command sequences the terminal
* remains compatible with the original SLang code (without using the
* workaround-code enabled by defining SLTT_TRANSP_ACS_PATCH).
*/
/*#define QNX_QANSI_SLANG_COMPAT_ACS 1*/
/* auto-configuration */
#ifdef SLTT_TRANSP_ACS_PATCH
# if defined(__QNX__) && defined(QNX_QANSI_SLANG_COMPAT_ACS)
# undef SLTT_TRANSP_ACS_PATCH
# endif
#else
# if defined(__QNX__) && !defined(QNX_QANSI_SLANG_COMPAT_ACS)
# define QNX_QANSI_SLANG_COMPAT_ACS 1
# endif
#endif
#include "sl-feat.h"
#include "config.h"
#include <stdio.h>
#include <string.h>
#ifdef SCO_FLAVOR
# include <sys/types.h>
# include <sys/timeb.h> /* for struct timeb, used in time.h */
#endif
#include <time.h>
#include <ctype.h>
#ifndef VMS
# include <sys/time.h>
# ifdef __QNX__
# include <sys/select.h>
# endif
# include <sys/types.h>
#endif
#ifdef __BEOS__
/* Prototype for select */
# include <net/socket.h>
#endif
#ifdef HAVE_TERMIOS_H
# include <termios.h>
#endif
#ifdef VMS
# include <unixlib.h>
# include <unixio.h>
# include <dvidef.h>
# include <descrip.h>
# include <lib$routines.h>
# include <starlet.h>
#else
# if !defined(sun)
# include <sys/ioctl.h>
# endif
#endif
#ifdef SYSV
# include <sys/termio.h>
# include <sys/stream.h>
# include <sys/ptem.h>
# include <sys/tty.h>
#endif
#if defined (_AIX) && !defined (FD_SET)
# include <sys/select.h> /* for FD_ISSET, FD_SET, FD_ZERO */
#endif
#include <errno.h>
#include "_slang.h"
#ifdef HAVE_STDLIB_H
# include <stdlib.h>
#endif
#ifdef HAVE_UNISTD_H
# include <unistd.h>
#endif
#if defined(__DECC) && defined(VMS)
/* These get prototypes for write an sleep */
# include <unixio.h>
#endif
#include <signal.h>
/* Colors: These definitions are used for the display. However, the
* application only uses object handles which get mapped to this
* internal representation. The mapping is performed by the Color_Map
* structure below. */
#define CHAR_MASK 0x000000FF
#define FG_MASK 0x0000FF00
#define BG_MASK 0x00FF0000
#define ATTR_MASK 0x1F000000
#define BGALL_MASK 0x0FFF0000
/* The 0x10000000 bit represents the alternate character set. BGALL_MASK does
* not include this attribute.
*/
#define GET_FG(color) ((color & FG_MASK) >> 8)
#define GET_BG(color) ((color & BG_MASK) >> 16)
#define MAKE_COLOR(fg, bg) (((fg) | ((bg) << 8)) << 8)
int SLtt_Screen_Cols;
int SLtt_Screen_Rows;
int SLtt_Term_Cannot_Insert;
int SLtt_Term_Cannot_Scroll;
int SLtt_Use_Ansi_Colors;
int SLtt_Blink_Mode = 1;
int SLtt_Use_Blink_For_ACS = 0;
int SLtt_Newline_Ok = 0;
int SLtt_Has_Alt_Charset = 0;
int SLtt_Force_Keypad_Init = 0;
/* -1 means unknown */
int SLtt_Has_Status_Line = -1; /* hs */
static int Automatic_Margins;
/* static int No_Move_In_Standout; */
static int Worthless_Highlight;
#define HP_GLITCH_CODE
#ifdef HP_GLITCH_CODE
/* This glitch is exclusive to HP term. Basically it means that to clear
* attributes, one has to erase to the end of the line.
*/
static int Has_HP_Glitch;
#endif
static char *Reset_Color_String;
static int Linux_Console;
/* It is crucial that JMAX_COLORS must be less than 128 since the high bit
* is used to indicate a character from the ACS (alt char set). The exception
* to this rule is if SLtt_Use_Blink_For_ACS is true. This means that of
* the highbit is set, we interpret that as a blink character. This is
* exploited by DOSemu.
*/
#define JMAX_COLORS 256
#define JNORMAL_COLOR 0
typedef struct
{
SLtt_Char_Type fgbg;
SLtt_Char_Type mono;
char *custom_esc;
} Ansi_Color_Type;
#define RGB1(r, g, b) ((r) | ((g) << 1) | ((b) << 2))
#define RGB(r, g, b, br, bg, bb) ((RGB1(r, g, b) << 8) | (RGB1(br, bg, bb) << 16))
static Ansi_Color_Type Ansi_Color_Map[JMAX_COLORS] =
{
{RGB(1, 1, 1, 0, 0, 0), 0x00000000, NULL}, /* white/black */
{RGB(0, 1, 0, 0, 0, 0), SLTT_REV_MASK, NULL}, /* green/black */
{RGB(1, 0, 1, 0, 0, 0), SLTT_REV_MASK, NULL}, /* magenta/black */
{RGB(0, 1, 1, 0, 0, 0), SLTT_REV_MASK, NULL}, /* cyan/black */
{RGB(1, 0, 0, 0, 0, 0), SLTT_REV_MASK, NULL},
{RGB(0, 1, 0, 0, 0, 1), SLTT_REV_MASK, NULL},
{RGB(1, 0, 0, 0, 0, 1), SLTT_REV_MASK, NULL},
{RGB(1, 0, 0, 0, 1, 0), SLTT_REV_MASK, NULL},
{RGB(0, 0, 1, 1, 0, 0), SLTT_REV_MASK, NULL},
{RGB(0, 1, 0, 1, 0, 0), SLTT_REV_MASK, NULL},
{RGB(0, 1, 1, 1, 1, 1), SLTT_REV_MASK, NULL},
{RGB(1, 1, 0, 1, 1, 1), SLTT_REV_MASK, NULL},
{RGB(1, 0, 1, 1, 1, 1), SLTT_REV_MASK, NULL},
{RGB(0, 0, 0, 0, 1, 1), SLTT_REV_MASK, NULL},
{RGB(0, 1, 0, 1, 1, 1), SLTT_REV_MASK, NULL},
{RGB(0, 1, 0, 1, 1, 1), SLTT_REV_MASK, NULL},
{RGB(0, 1, 0, 1, 1, 1), SLTT_REV_MASK, NULL},
{RGB(0, 1, 0, 1, 1, 1), SLTT_REV_MASK, NULL}
};
/* This is the string to use to use when outputting color information.
*/
#ifdef M_UNIX
/* work around for sco console bug that can't handle combined sequences */
static char *Color_Escape_Sequence = "\033[3%dm\033[4%dm";
#else
/* Believe it or not, this is what is in the linux terminfo database. It
* produces the same escape sequence but it is much more CPU intensive.
* Why not just encode it as "\033[3%p1%dm\033[4%p2%dm" ???
*/
/* static char *Color_Escape_Sequence = "\033[%p1%{30}%+%dm\033[%p2%{40}%+%dm"; */
static char *Color_Escape_Sequence = "\033[3%d;4%dm";
#endif
char *SLtt_Graphics_Char_Pairs; /* ac termcap string -- def is vt100 */
/* 1 if terminal lacks the ability to do into insert mode or into delete
mode. Currently controlled by S-Lang but later perhaps termcap. */
static char *UnderLine_Vid_Str;
static char *Blink_Vid_Str;
static char *Bold_Vid_Str;
static char *Ins_Mode_Str; /* = "\033[4h"; */ /* ins mode (im) */
static char *Eins_Mode_Str; /* = "\033[4l"; */ /* end ins mode (ei) */
static char *Scroll_R_Str; /* = "\033[%d;%dr"; */ /* scroll region */
static char *Cls_Str; /* = "\033[2J\033[H"; */ /* cl termcap STR for ansi terminals */
static char *Rev_Vid_Str; /* = "\033[7m"; */ /* mr,so termcap string */
static char *Norm_Vid_Str; /* = "\033[m"; */ /* me,se termcap string */
static char *Del_Eol_Str; /* = "\033[K"; */ /* ce */
static char *Del_Char_Str; /* = "\033[P"; */ /* dc */
static char *Del_N_Lines_Str; /* = "\033[%dM"; */ /* DL */
static char *Add_N_Lines_Str; /* = "\033[%dL"; */ /* AL */
static char *Rev_Scroll_Str;
static char *Curs_Up_Str;
static char *Curs_F_Str; /* RI termcap string */
static char *Cursor_Visible_Str; /* ve termcap string */
static char *Cursor_Invisible_Str; /* vi termcap string */
static char *Start_Alt_Chars_Str; /* as */
static char *End_Alt_Chars_Str; /* ae */
static char *Enable_Alt_Char_Set; /* eA */
static char *Term_Init_Str;
static char *Keypad_Init_Str;
static char *Term_Reset_Str;
static char *Keypad_Reset_Str;
/* status line functions */
static char *Disable_Status_line_Str; /* ds */
static char *Return_From_Status_Line_Str; /* fs */
static char *Goto_Status_Line_Str; /* ts */
static int Num_Status_Line_Columns; /* ws */
static int Status_Line_Esc_Ok; /* es */
/* static int Len_Curs_F_Str = 5; */
/* cm string has %i%d since termcap numbers columns from 0 */
/* char *CURS_POS_STR = "\033[%d;%df"; ansi-- hor and vert pos */
static char *Curs_Pos_Str; /* = "\033[%i%d;%dH";*/ /* cm termcap string */
/* scrolling region */
static int Scroll_r1 = 0, Scroll_r2 = 23;
static int Cursor_r, Cursor_c; /* 0 based */
/* current attributes --- initialized to impossible value */
static SLtt_Char_Type Current_Fgbg = 0xFFFFFFFFU;
static int Cursor_Set; /* 1 if cursor position known, 0
* if not. -1 if only row is known
*/
#define MAX_OUTPUT_BUFFER_SIZE 4096
static unsigned char Output_Buffer[MAX_OUTPUT_BUFFER_SIZE];
static unsigned char *Output_Bufferp = Output_Buffer;
unsigned long SLtt_Num_Chars_Output;
#ifdef SLTT_TRANSP_ACS_PATCH
static int SLtt_ACS_Active = 0;
#endif
static int sl_usleep (unsigned long usecs)
{
#ifndef VMS
struct timeval tv;
tv.tv_sec = usecs / 1000000;
tv.tv_usec = usecs % 1000000;
return select(0, NULL, NULL, NULL, &tv);
#else
return 0;
#endif
}
int SLtt_flush_output (void)
{
int nwrite = 0;
unsigned int total;
int n = (int) (Output_Bufferp - Output_Buffer);
SLtt_Num_Chars_Output += n;
total = 0;
while (n > 0)
{
nwrite = write (fileno(stdout), (char *) Output_Buffer + total, n);
if (nwrite == -1)
{
nwrite = 0;
#ifdef EAGAIN
if (errno == EAGAIN)
{
sl_usleep (100000); /* 1/10 sec */
continue;
}
#endif
#ifdef EWOULDBLOCK
if (errno == EWOULDBLOCK)
{
sl_usleep (100000);
continue;
}
#endif
#ifdef EINTR
if (errno == EINTR) continue;
#endif
break;
}
n -= nwrite;
total += nwrite;
}
Output_Bufferp = Output_Buffer;
return n;
}
int SLtt_Baud_Rate;
static void tt_write(char *str, unsigned int n)
{
static unsigned long last_time;
static int total;
unsigned long now;
unsigned int ndiff;
if ((str == NULL) || (n == 0)) return;
total += n;
while (1)
{
ndiff = MAX_OUTPUT_BUFFER_SIZE - (int) (Output_Bufferp - Output_Buffer);
if (ndiff < n)
{
SLMEMCPY ((char *) Output_Bufferp, (char *) str, ndiff);
Output_Bufferp += ndiff;
SLtt_flush_output ();
n -= ndiff;
str += ndiff;
}
else
{
SLMEMCPY ((char *) Output_Bufferp, str, n);
Output_Bufferp += n;
break;
}
}
if (((SLtt_Baud_Rate > 150) && (SLtt_Baud_Rate <= 9600))
&& (10 * total > SLtt_Baud_Rate))
{
total = 0;
if ((now = (unsigned long) time(NULL)) - last_time <= 1)
{
SLtt_flush_output ();
sleep((unsigned) 1);
}
last_time = now;
}
}
void SLtt_write_string (char *str)
{
if (str != NULL) tt_write(str, strlen(str));
}
void SLtt_putchar (char ch)
{
#ifdef SLTT_TRANSP_ACS_PATCH
int restore_acs = 0;
#endif
SLtt_normal_video ();
if (Cursor_Set == 1)
{
if (ch >= ' ') Cursor_c++;
#ifndef SLTT_TRANSP_ACS_PATCH
else if (ch == '\b') Cursor_c--;
#else
if (ch <= ' ' && SLtt_ACS_Active)
{
SLtt_set_alt_char_set (0);
restore_acs = 1;
}
if (ch == '\b') Cursor_c--;
#endif
else if (ch == '\r') Cursor_c = 0;
else Cursor_Set = 0;
if ((Cursor_c + 1 == SLtt_Screen_Cols)
&& Automatic_Margins) Cursor_Set = 0;
}
if (Output_Bufferp < Output_Buffer + MAX_OUTPUT_BUFFER_SIZE)
{
*Output_Bufferp++ = (unsigned char) ch;
}
else tt_write (&ch, 1);
#ifdef SLTT_TRANSP_ACS_PATCH
if (restore_acs)
{
SLtt_set_alt_char_set (1);
}
#endif
}
/* this is supposed to be fast--- also handles
termcap: %d, &i, %., %+, %r strings as well as terminfo stuff */
static unsigned int tt_sprintf(char *buf, char *fmt, int x, int y)
{
register unsigned char *f = (unsigned char *) fmt, *b, ch;
int offset = 0, tinfo = 0;
int stack[10];
int i = 0, z;
stack[0] = y; stack[1] = x; i = 2;
b = (unsigned char *) buf;
if (fmt != NULL) while ((ch = *f++) != 0)
{
if (ch != '%') *b++ = ch;
else
{
ch = *f++;
if (tinfo)
{
if ((ch <= '3') && (ch >= '0'))
{
/* map it to termcap. Since this is terminfo,
* it must be one of:
* %2d, %3d, %02d, %03d
*
* I am assuming that a terminal that understands
* %2d form will also understand the %02d form. These
* only differ by a space padding the field.
*/
/* skip the 'd'-- hope it is there */
if (ch == '0')
{
ch = *f;
f += 2;
}
else f++;
}
}
switch (ch)
{
case 'p':
tinfo = 1;
ch = *f++;
if (ch == '1') stack[i++] = x; else stack[i++] = y;
break;
case '\'': /* 'x' */
stack[i++] = *f++;
f++;
break;
case '{': /* literal constant, e.g. {30} */
z = 0;
while (((ch = *f) <= '9') && (ch >= '0'))
{
z = z * 10 + (ch - '0');
f++;
}
stack[i++] = z;
if (ch == '}') f++;
break;
case 'd':
case '2':
case '3':
z = stack[--i];
z += offset;
if (z >= 100)
{
*b++ = z / 100 + '0';
z = z % 100;
goto ten;
}
else if (ch == 3)
{
*b++ = '0';
ch = '2';
}
if (z >= 10)
{
ten:
*b++ = z / 10 + '0';
z = z % 10;
}
else if (ch == 2) *b++ = '0';
*b++ = z + '0';
break;
case 'i':
offset = 1;
break;
case '+':
if (tinfo)
{
z = stack[--i];
stack[i-1] += z;
}
else
{
ch = *f++;
if ((unsigned char) ch == 128) ch = 0;
ch = ch + (unsigned char) stack[--i];
if (ch == '\n') ch++;
*b++ = ch;
}
break;
case 'r':
stack[0] = x;
stack[1] = y;
break;
case '.':
case 'c':
ch = (unsigned char) stack[--i];
if (ch == '\n') ch++;
*b++ = ch;
break;
default:
*b++ = ch;
}
}
}
*b = 0;
return((unsigned int) (b - (unsigned char *) buf));
}
static void tt_printf(char *fmt, int x, int y)
{
char buf[256];
unsigned int n;
if (fmt == NULL) return;
n = tt_sprintf(buf, fmt, x, y);
tt_write(buf, n);
}
void SLtt_set_scroll_region (int r1, int r2)
{
Scroll_r1 = r1;
Scroll_r2 = r2;
tt_printf (Scroll_R_Str, Scroll_r1, Scroll_r2);
Cursor_Set = 0;
}
void SLtt_reset_scroll_region (void)
{
SLtt_set_scroll_region(0, SLtt_Screen_Rows - 1);
}
int SLtt_set_cursor_visibility (int show)
{
if ((Cursor_Visible_Str == NULL) || (Cursor_Invisible_Str == NULL))
return -1;
SLtt_write_string (show ? Cursor_Visible_Str : Cursor_Invisible_Str);
return 0;
}
/* the goto_rc function moves to row relative to scrolling region */
void SLtt_goto_rc(int r, int c)
{
char *s = NULL;
int n;
char buf[6];
#ifdef SLTT_TRANSP_ACS_PATCH
int check_alt_acs = 0;
#endif
if (c < 0)
{
c = -c - 1;
Cursor_Set = 0;
}
/* if (No_Move_In_Standout && Current_Fgbg) SLtt_normal_video (); */
r += Scroll_r1;
if ((Cursor_Set > 0) || ((Cursor_Set < 0) && !Automatic_Margins))
{
n = r - Cursor_r;
if ((n == -1) && (Cursor_Set > 0) && (Cursor_c == c)
&& (Curs_Up_Str != NULL))
{
s = Curs_Up_Str;
}
else if ((n >= 0) && (n <= 4))
{
if ((n == 0) && (Cursor_Set == 1)
&& ((c > 1) || (c == Cursor_c)))
{
if (Cursor_c == c) return;
if (Cursor_c == c + 1)
{
s = buf;
*s++ = '\b'; *s = 0;
s = buf;
#ifdef SLTT_TRANSP_ACS_PATCH
check_alt_acs = 1;
#endif
}
}
else if (c == 0)
{
s = buf;
if ((Cursor_Set != 1) || (Cursor_c != 0)) *s++ = '\r';
while (n--) *s++ = '\n';
#ifdef VMS
/* Need to add this after \n to start a new record. Sheesh. */
*s++ = '\r';
#endif
*s = 0;
s = buf;
#ifdef SLTT_TRANSP_ACS_PATCH
check_alt_acs = 1;
#endif
}
/* Will fail on VMS */
#ifndef VMS
else if (SLtt_Newline_Ok && (Cursor_Set == 1) &&
(Cursor_c >= c) && (c + 3 > Cursor_c))
{
s = buf;
while (n--) *s++ = '\n';
n = Cursor_c - c;
while (n--) *s++ = '\b';
*s = 0;
s = buf;
#ifdef SLTT_TRANSP_ACS_PATCH
check_alt_acs = 1;
#endif
}
#endif
}
}
#ifndef SLTT_TRANSP_ACS_PATCH
if (s != NULL) SLtt_write_string(s);
#else
if (s != NULL)
{
if (check_alt_acs && SLtt_ACS_Active)
{
SLtt_set_alt_char_set (0);
SLtt_write_string(s);
SLtt_set_alt_char_set (1);
}
else SLtt_write_string(s);
}
#endif
else tt_printf(Curs_Pos_Str, r, c);
Cursor_c = c; Cursor_r = r;
Cursor_Set = 1;
}
void SLtt_begin_insert (void)
{
SLtt_write_string(Ins_Mode_Str);
}
void SLtt_end_insert (void)
{
SLtt_write_string(Eins_Mode_Str);
}
void SLtt_delete_char (void)
{
SLtt_normal_video ();
SLtt_write_string(Del_Char_Str);
}
void SLtt_erase_line (void)
{
SLtt_write_string("\r");
Cursor_Set = 1; Cursor_c = 0;
SLtt_del_eol();
}
void SLtt_delete_nlines (int n)
{
int r1, curs;
char buf[132];
#ifdef SLTT_TRANSP_ACS_PATCH
int restore_acs = 0;
#endif
if (n <= 0) return;
SLtt_normal_video ();
if (Del_N_Lines_Str != NULL) tt_printf(Del_N_Lines_Str,n, 0);
else
/* get a new terminal */
{
r1 = Scroll_r1;
curs = Cursor_r;
SLtt_set_scroll_region(curs, Scroll_r2);
SLtt_goto_rc(Scroll_r2 - Scroll_r1, 0);
#ifdef SLTT_TRANSP_ACS_PATCH
if (SLtt_ACS_Active)
{
SLtt_set_alt_char_set (0);
restore_acs = 1;
}
#endif
SLMEMSET(buf, '\n', (unsigned int) n);
tt_write(buf, (unsigned int) n);
#ifdef SLTT_TRANSP_ACS_PATCH
if (restore_acs) SLtt_set_alt_char_set (1);
#endif
/* while (n--) tt_putchar('\n'); */
SLtt_set_scroll_region(r1, Scroll_r2);
SLtt_goto_rc(curs, 0);
}
}
void SLtt_cls (void)
{
SLtt_normal_video();
SLtt_reset_scroll_region ();
SLtt_write_string(Cls_Str);
}
void SLtt_reverse_index (int n)
{
if (!n) return;
SLtt_normal_video();
if (Add_N_Lines_Str != NULL) tt_printf(Add_N_Lines_Str,n, 0);
else
{
while(n--) SLtt_write_string(Rev_Scroll_Str);
}
}
int SLtt_Ignore_Beep = 1;
static char *Visible_Bell_Str;
void SLtt_beep (void)
{
if (SLtt_Ignore_Beep & 0x1) SLtt_putchar('\007');
if (SLtt_Ignore_Beep & 0x2)
{
if (Visible_Bell_Str != NULL) SLtt_write_string (Visible_Bell_Str);
#ifdef __linux__
else if (Linux_Console)
{
SLtt_write_string ("\033[?5h");
SLtt_flush_output ();
sl_usleep (50000);
SLtt_write_string ("\033[?5l");
}
#endif
}
SLtt_flush_output ();
}
void SLtt_del_eol (void)
{
if (Current_Fgbg != 0xFFFFFFFFU) SLtt_normal_video ();
SLtt_write_string(Del_Eol_Str);
}
typedef const struct
{
char *name;
SLtt_Char_Type color;
}
Color_Def_Type;
#define MAX_COLOR_NAMES 17
static Color_Def_Type Color_Defs [MAX_COLOR_NAMES] =
{
{"black", SLSMG_COLOR_BLACK},
{"red", SLSMG_COLOR_RED},
{"green", SLSMG_COLOR_GREEN},
{"brown", SLSMG_COLOR_BROWN},
{"blue", SLSMG_COLOR_BLUE},
{"magenta", SLSMG_COLOR_MAGENTA},
{"cyan", SLSMG_COLOR_CYAN},
{"lightgray", SLSMG_COLOR_LGRAY},
{"gray", SLSMG_COLOR_GRAY},
{"brightred", SLSMG_COLOR_BRIGHT_RED},
{"brightgreen", SLSMG_COLOR_BRIGHT_GREEN},
{"yellow", SLSMG_COLOR_BRIGHT_BROWN},
{"brightblue", SLSMG_COLOR_BRIGHT_BLUE},
{"brightmagenta", SLSMG_COLOR_BRIGHT_CYAN},
{"brightcyan", SLSMG_COLOR_BRIGHT_MAGENTA},
{"white", SLSMG_COLOR_BRIGHT_WHITE},
{"default", 25}
};
void SLtt_set_mono (int obj, char *what, SLtt_Char_Type mask)
{
(void) what;
if ((obj < 0) || (obj >= JMAX_COLORS))
{
return;
}
Ansi_Color_Map[obj].mono = mask & ATTR_MASK;
}
static char *check_color_for_digit_form (char *color)
{
unsigned int i, ich;
char *s = color;
i = 0;
while ((ich = (int) *s) != 0)
{
if ((ich < '0') || (ich > '9'))
return color;
i = i * 10 + (ich - '0');
s++;
}
if (i < MAX_COLOR_NAMES)
color = Color_Defs[i].name;
return color;
}
static int get_default_colors (char **fgp, char **bgp)
{
static char fg_buf[16], bg_buf[16], *bg, *fg;
static int already_parsed;
char *p, *pmax;
if (already_parsed == -1)
return -1;
if (already_parsed)
{
*fgp = fg;
*bgp = bg;
return 0;
}
already_parsed = -1;
bg = getenv ("COLORFGBG");
if (bg == NULL)
{
bg = getenv ("DEFAULT_COLORS");
if (bg == NULL)
return -1;
}
p = fg_buf;
pmax = p + (sizeof (fg_buf) - 1);
while ((*bg != 0) && (*bg != ';'))
{
if (p < pmax) *p++ = *bg;
bg++;
}
*p = 0;
if (*bg) bg++;
p = bg_buf;
pmax = p + (sizeof (bg_buf) - 1);
/* Mark suggested allowing for extra spplication specific stuff following
* the background color. That is what the check for the semi-colon is for.
*/
while ((*bg != 0) && (*bg != ';'))
{
if (p < pmax) *p++ = *bg;
bg++;
}
*p = 0;
if (!strcmp (fg_buf, "default") || !strcmp(bg_buf, "default"))
{
*fgp = *bgp = fg = bg = "default";
}
else
{
*fgp = fg = check_color_for_digit_form (fg_buf);
*bgp = bg = check_color_for_digit_form (bg_buf);
}
already_parsed = 1;
return 0;
}
static unsigned char FgBg_Stats[JMAX_COLORS];
static int Color_0_Modified = 0;
void SLtt_set_color_object (int obj, SLtt_Char_Type attr)
{
char *cust_esc;
if ((obj < 0) || (obj >= JMAX_COLORS)) return;
cust_esc = Ansi_Color_Map[obj].custom_esc;
if (cust_esc != NULL)
{
SLFREE (cust_esc);
FgBg_Stats[(Ansi_Color_Map[obj].fgbg >> 8) & 0x7F] -= 1;
Ansi_Color_Map[obj].custom_esc = NULL;
}
Ansi_Color_Map[obj].fgbg = attr;
if (obj == 0) Color_0_Modified = 1;
}
SLtt_Char_Type SLtt_get_color_object (int obj)
{
if ((obj < 0) || (obj >= JMAX_COLORS)) return 0;
return Ansi_Color_Map[obj].fgbg;
}
void SLtt_add_color_attribute (int obj, SLtt_Char_Type attr)
{
if ((obj < 0) || (obj >= JMAX_COLORS)) return;
Ansi_Color_Map[obj].fgbg |= (attr & ATTR_MASK);
if (obj == 0) Color_0_Modified = 1;
}
static SLtt_Char_Type fb_to_fgbg (SLtt_Char_Type f, SLtt_Char_Type b)
{
SLtt_Char_Type attr = 0;
if ((f & 0xF0) == 0)
{
if (f & 0x8) attr = SLTT_BOLD_MASK;
f &= 0x7;
}
else f = 9;
if ((b & 0xF0) == 0)
{
if (b & 0x8) attr |= SLTT_BLINK_MASK;
b &= 0x7;
}
else b = 9;
return ((f << 8) | (b << 16) | attr);