-
Notifications
You must be signed in to change notification settings - Fork 10
/
terms.c
2270 lines (2034 loc) · 44.7 KB
/
terms.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
/* $Id: terms.c,v 1.63 2010/08/20 09:34:47 htrb Exp $ */
/*
* An original curses library for EUC-kanji by Akinori ITO, December 1989
* revised by Akinori ITO, January 1995
*/
#include <stdio.h>
#include <signal.h>
#include <sys/types.h>
#include <fcntl.h>
#include <errno.h>
#include <sys/time.h>
#include <unistd.h>
#include "config.h"
#include <string.h>
#ifdef HAVE_SYS_SELECT_H
#include <sys/select.h>
#endif
#ifndef __MINGW32_VERSION
#include <sys/ioctl.h>
#else
#include <winsock.h>
#endif /* __MINGW32_VERSION */
#ifdef USE_MOUSE
#ifdef USE_GPM
#include <gpm.h>
#endif /* USE_GPM */
#ifdef USE_SYSMOUSE
#include <osreldate.h>
#if (__FreeBSD_version >= 400017) || (__FreeBSD_kernel_version >= 400017)
#include <sys/consio.h>
#include <sys/fbio.h>
#else
#include <machine/console.h>
#endif
int (*sysm_handler) (int x, int y, int nbs, int obs);
static int cwidth = 8, cheight = 16;
static int xpix, ypix, nbs, obs = 0;
#endif /* use_SYSMOUSE */
static int is_xterm = 0;
void mouse_init(), mouse_end();
int mouseActive = 0;
#endif /* USE_MOUSE */
static char *title_str = NULL;
static int tty;
#include "terms.h"
#include "fm.h"
#include "myctype.h"
#ifdef __EMX__
#define INCL_DOSNLS
#include <os2.h>
#endif /* __EMX__ */
#if defined(__CYGWIN__)
#include <windows.h>
#include <sys/cygwin.h>
static int isWinConsole = 0;
#define TERM_CYGWIN 1
#define TERM_CYGWIN_RESERVE_IME 2
static int isLocalConsole = 0;
#if CYGWIN_VERSION_DLL_MAJOR < 1005 && defined(USE_MOUSE)
int cygwin_mouse_btn_swapped = 0;
#endif
#if defined(SUPPORT_WIN9X_CONSOLE_MBCS)
static HANDLE hConIn = INVALID_HANDLE_VALUE;
static int isWin95 = 0;
static char *ConInV;
static int iConIn, nConIn, nConInMax;
static void
check_win9x(void)
{
OSVERSIONINFO winVersionInfo;
winVersionInfo.dwOSVersionInfoSize = sizeof(OSVERSIONINFO);
if (GetVersionEx(&winVersionInfo) == 0) {
fprintf(stderr, "can't get Windows version information.\n");
exit(1);
}
if (winVersionInfo.dwPlatformId == VER_PLATFORM_WIN32_WINDOWS) {
isWin95 = 1;
}
else {
isWin95 = 0;
}
}
void
enable_win9x_console_input(void)
{
if (isWin95 && isWinConsole && isLocalConsole &&
hConIn == INVALID_HANDLE_VALUE) {
hConIn = CreateFile("CONIN$", GENERIC_READ | GENERIC_WRITE,
FILE_SHARE_READ | FILE_SHARE_WRITE,
NULL, OPEN_EXISTING, 0, NULL);
if (hConIn != INVALID_HANDLE_VALUE) {
getch();
}
}
}
void
disable_win9x_console_input(void)
{
if (hConIn != INVALID_HANDLE_VALUE) {
CloseHandle(hConIn);
hConIn = INVALID_HANDLE_VALUE;
}
}
static void
expand_win32_console_input_buffer(int n)
{
if (nConIn + n >= nConInMax) {
char *oldv;
nConInMax = ((nConIn + n) / 2 + 1) * 3;
oldv = ConInV;
ConInV = GC_MALLOC_ATOMIC(nConInMax);
memcpy(ConInV, oldv, nConIn);
}
}
static int
read_win32_console_input(void)
{
INPUT_RECORD rec;
DWORD nevents;
if (PeekConsoleInput(hConIn, &rec, 1, &nevents) && nevents) {
switch (rec.EventType) {
case KEY_EVENT:
expand_win32_console_input_buffer(3);
if (ReadConsole(hConIn, &ConInV[nConIn], 1, &nevents, NULL)) {
nConIn += nevents;
return nevents;
}
break;
default:
break;
}
ReadConsoleInput(hConIn, &rec, 1, &nevents);
}
return 0;
}
static int
read_win32_console(char *s, int n)
{
KEY_EVENT_RECORD *ker;
if (hConIn == INVALID_HANDLE_VALUE)
return read(tty, s, n);
if (n > 0)
for (;;) {
if (iConIn < nConIn) {
if (n > nConIn - iConIn)
n = nConIn - iConIn;
memcpy(s, ConInV, n);
if ((iConIn += n) >= nConIn)
iConIn = nConIn = 0;
break;
}
iConIn = nConIn = 0;
while (!read_win32_console_input()) ;
}
return n;
}
#endif /* SUPPORT_WIN9X_CONSOLE_MBCS */
static HWND
GetConsoleHwnd(void)
{
#define MY_BUFSIZE 1024
HWND hwndFound;
char pszNewWindowTitle[MY_BUFSIZE];
char pszOldWindowTitle[MY_BUFSIZE];
GetConsoleTitle(pszOldWindowTitle, MY_BUFSIZE);
wsprintf(pszNewWindowTitle, "%d/%d",
GetTickCount(), GetCurrentProcessId());
SetConsoleTitle(pszNewWindowTitle);
Sleep(40);
hwndFound = FindWindow(NULL, pszNewWindowTitle);
SetConsoleTitle(pszOldWindowTitle);
return (hwndFound);
}
#if CYGWIN_VERSION_DLL_MAJOR < 1005 && defined(USE_MOUSE)
static unsigned long
cygwin_version(void)
{
struct per_process *p;
p = (struct per_process *)cygwin_internal(CW_USER_DATA);
if (p != NULL) {
return (p->dll_major * 1000) + p->dll_minor;
}
return 0;
}
#endif
static void
check_cygwin_console(void)
{
char *term = getenv("TERM");
HANDLE hWnd;
if (term == NULL)
term = DEFAULT_TERM;
if (term && strncmp(term, "cygwin", 6) == 0) {
isWinConsole = TERM_CYGWIN;
}
if (isWinConsole) {
hWnd = GetConsoleHwnd();
if (hWnd != INVALID_HANDLE_VALUE) {
if (IsWindowVisible(hWnd)) {
isLocalConsole = 1;
}
}
if (strncmp(getenv("LANG"), "ja", 2) == 0) {
isWinConsole = TERM_CYGWIN_RESERVE_IME;
}
#ifdef SUPPORT_WIN9X_CONSOLE_MBCS
check_win9x();
if (isWin95 && ttyslot() != -1) {
isLocalConsole = 0;
}
#endif
}
#if CYGWIN_VERSION_DLL_MAJOR < 1005 && defined(USE_MOUSE)
if (cygwin_version() <= 1003015) {
/* cygwin DLL 1.3.15 or earler */
cygwin_mouse_btn_swapped = 1;
}
#endif
}
#endif /* __CYGWIN__ */
char *getenv(const char *);
MySignalHandler reset_exit(SIGNAL_ARG), reset_error_exit(SIGNAL_ARG), error_dump(SIGNAL_ARG);
void setlinescols(void);
void flush_tty();
#ifndef SIGIOT
#define SIGIOT SIGABRT
#endif /* not SIGIOT */
#ifdef HAVE_TERMIO_H
#include <termio.h>
typedef struct termio TerminalMode;
#define TerminalSet(fd,x) ioctl(fd,TCSETA,x)
#define TerminalGet(fd,x) ioctl(fd,TCGETA,x)
#define MODEFLAG(d) ((d).c_lflag)
#define IMODEFLAG(d) ((d).c_iflag)
#endif /* HAVE_TERMIO_H */
#ifdef HAVE_TERMIOS_H
#include <termios.h>
#include <unistd.h>
typedef struct termios TerminalMode;
#define TerminalSet(fd,x) tcsetattr(fd,TCSANOW,x)
#define TerminalGet(fd,x) tcgetattr(fd,x)
#define MODEFLAG(d) ((d).c_lflag)
#define IMODEFLAG(d) ((d).c_iflag)
#endif /* HAVE_TERMIOS_H */
#ifdef HAVE_SGTTY_H
#include <sgtty.h>
typedef struct sgttyb TerminalMode;
#define TerminalSet(fd,x) ioctl(fd,TIOCSETP,x)
#define TerminalGet(fd,x) ioctl(fd,TIOCGETP,x)
#define MODEFLAG(d) ((d).sg_flags)
#endif /* HAVE_SGTTY_H */
#ifdef __MINGW32_VERSION
/* dummy struct */
typedef unsigned char cc_t;
typedef unsigned int speed_t;
typedef unsigned int tcflag_t;
#define NCCS 32
struct termios
{
tcflag_t c_iflag; /* input mode flags */
tcflag_t c_oflag; /* output mode flags */
tcflag_t c_cflag; /* control mode flags */
tcflag_t c_lflag; /* local mode flags */
cc_t c_line; /* line discipline */
cc_t c_cc[NCCS]; /* control characters */
speed_t c_ispeed; /* input speed */
speed_t c_ospeed; /* output speed */
};
typedef struct termios TerminalMode;
#define TerminalSet(fd,x) (0)
#define TerminalGet(fd,x) (0)
#define MODEFLAG(d) (0)
/* dummy defines */
#define SIGHUP (0)
#define SIGQUIT (0)
#define ECHO (0)
#define ISIG (0)
#define VEOF (0)
#define ICANON (0)
#define IXON (0)
#define IXOFF (0)
char *ttyname(int);
#endif /* __MINGW32_VERSION */
#define MAX_LINE 200
#define MAX_COLUMN 400
/* Screen properties */
#define S_SCREENPROP 0x0f
#define S_NORMAL 0x00
#define S_STANDOUT 0x01
#define S_UNDERLINE 0x02
#define S_BOLD 0x04
#define S_EOL 0x08
/* Sort of Character */
#define C_WHICHCHAR 0xc0
#define C_ASCII 0x00
#ifdef USE_M17N
#define C_WCHAR1 0x40
#define C_WCHAR2 0x80
#endif
#define C_CTRL 0xc0
#define CHMODE(c) ((c)&C_WHICHCHAR)
#define SETCHMODE(var,mode) ((var) = (((var)&~C_WHICHCHAR) | mode))
#ifdef USE_M17N
#define SETCH(var,ch,len) ((var) = New_Reuse(char, (var), (len) + 1), \
strncpy((var), (ch), (len)), (var)[len] = '\0')
#else
#define SETCH(var,ch,len) ((var) = (ch))
#endif
/* Charactor Color */
#define COL_FCOLOR 0xf00
#define COL_FBLACK 0x800
#define COL_FRED 0x900
#define COL_FGREEN 0xa00
#define COL_FYELLOW 0xb00
#define COL_FBLUE 0xc00
#define COL_FMAGENTA 0xd00
#define COL_FCYAN 0xe00
#define COL_FWHITE 0xf00
#define COL_FTERM 0x000
#define S_COLORED 0xf00
#ifdef USE_BG_COLOR
/* Background Color */
#define COL_BCOLOR 0xf000
#define COL_BBLACK 0x8000
#define COL_BRED 0x9000
#define COL_BGREEN 0xa000
#define COL_BYELLOW 0xb000
#define COL_BBLUE 0xc000
#define COL_BMAGENTA 0xd000
#define COL_BCYAN 0xe000
#define COL_BWHITE 0xf000
#define COL_BTERM 0x0000
#define S_BCOLORED 0xf000
#endif /* USE_BG_COLOR */
#define S_GRAPHICS 0x10
#define S_DIRTY 0x20
#define SETPROP(var,prop) (var = (((var)&S_DIRTY) | prop))
/* Line status */
#define L_DIRTY 0x01
#define L_UNUSED 0x02
#define L_NEED_CE 0x04
#define L_CLRTOEOL 0x08
#define ISDIRTY(d) ((d) & L_DIRTY)
#define ISUNUSED(d) ((d) & L_UNUSED)
#define NEED_CE(d) ((d) & L_NEED_CE)
typedef unsigned short l_prop;
typedef struct scline {
#ifdef USE_M17N
char **lineimage;
#else
char *lineimage;
#endif
l_prop *lineprop;
short isdirty;
short eol;
} Screen;
static TerminalMode d_ioval;
static int tty = -1;
static FILE *ttyf = NULL;
static
char bp[1024], funcstr[256];
char *T_cd, *T_ce, *T_kr, *T_kl, *T_cr, *T_bt, *T_ta, *T_sc, *T_rc,
*T_so, *T_se, *T_us, *T_ue, *T_cl, *T_cm, *T_al, *T_sr, *T_md, *T_me,
*T_ti, *T_te, *T_nd, *T_as, *T_ae, *T_eA, *T_ac, *T_op;
int LINES, COLS;
#if defined(__CYGWIN__)
int LASTLINE;
#endif /* defined(__CYGWIN__) */
static int max_LINES = 0, max_COLS = 0;
static int tab_step = 8;
static int CurLine, CurColumn;
static Screen *ScreenElem = NULL, **ScreenImage = NULL;
static l_prop CurrentMode = 0;
static int graph_enabled = 0;
static char gcmap[96];
extern int tgetent(char *, char *);
extern int tgetnum(char *);
extern int tgetflag(char *);
extern char *tgetstr(char *, char **);
extern char *tgoto(char *, int, int);
extern int tputs(char *, int, int (*)(char));
void clear(), wrap(), touch_line(), touch_column(int);
#if 0
void need_clrtoeol(void);
#endif
void clrtoeol(void); /* conflicts with curs_clear(3)? */
static int write1(char);
static void
writestr(char *s)
{
tputs(s, 1, write1);
}
#define MOVE(line,column) writestr(tgoto(T_cm,column,line));
#ifdef USE_MOUSE
#define W3M_TERM_INFO(name, title, mouse) name, title, mouse
#define NEED_XTERM_ON (1)
#define NEED_XTERM_OFF (1<<1)
#ifdef __CYGWIN__
#define NEED_CYGWIN_ON (1<<2)
#define NEED_CYGWIN_OFF (1<<3)
#endif
#else
#define W3M_TERM_INFO(name, title, mouse) name, title
#endif
static char XTERM_TITLE[] = "\033]0;w3m: %s\007";
static char SCREEN_TITLE[] = "\033k%s\033\134";
#ifdef __CYGWIN__
static char CYGWIN_TITLE[] = "w3m: %s";
#endif
/* *INDENT-OFF* */
static struct w3m_term_info {
char *term;
char *title_str;
#ifdef USE_MOUSE
int mouse_flag;
#endif
} w3m_term_info_list[] = {
{W3M_TERM_INFO("xterm", XTERM_TITLE, (NEED_XTERM_ON|NEED_XTERM_OFF))},
{W3M_TERM_INFO("kterm", XTERM_TITLE, (NEED_XTERM_ON|NEED_XTERM_OFF))},
{W3M_TERM_INFO("rxvt", XTERM_TITLE, (NEED_XTERM_ON|NEED_XTERM_OFF))},
{W3M_TERM_INFO("Eterm", XTERM_TITLE, (NEED_XTERM_ON|NEED_XTERM_OFF))},
{W3M_TERM_INFO("mlterm", XTERM_TITLE, (NEED_XTERM_ON|NEED_XTERM_OFF))},
{W3M_TERM_INFO("screen", SCREEN_TITLE, 0)},
#ifdef __CYGWIN__
{W3M_TERM_INFO("cygwin", CYGWIN_TITLE, (NEED_CYGWIN_ON|NEED_CYGWIN_OFF))},
#endif
{W3M_TERM_INFO(NULL, NULL, 0)}
};
#undef W3M_TERM_INFO
/* *INDENT-ON * */
int
set_tty(void)
{
char *ttyn;
if (isatty(0)) /* stdin */
ttyn = ttyname(0);
else
ttyn = DEV_TTY_PATH;
tty = open(ttyn, O_RDWR);
if (tty < 0) {
/* use stderr instead of stdin... is it OK???? */
tty = 2;
}
ttyf = fdopen(tty, "w");
#ifdef __CYGWIN__
check_cygwin_console();
#endif
TerminalGet(tty, &d_ioval);
if (displayTitleTerm != NULL) {
struct w3m_term_info *p;
for (p = w3m_term_info_list; p->term != NULL; p++) {
if (!strncmp(displayTitleTerm, p->term, strlen(p->term))) {
title_str = p->title_str;
break;
}
}
}
#ifdef USE_MOUSE
{
char *term = getenv("TERM");
if (term != NULL) {
struct w3m_term_info *p;
for (p = w3m_term_info_list; p->term != NULL; p++) {
if (!strncmp(term, p->term, strlen(p->term))) {
is_xterm = p->mouse_flag;
break;
}
}
}
}
#endif
return 0;
}
void
ttymode_set(int mode, int imode)
{
#ifndef __MINGW32_VERSION
TerminalMode ioval;
TerminalGet(tty, &ioval);
MODEFLAG(ioval) |= mode;
#ifndef HAVE_SGTTY_H
IMODEFLAG(ioval) |= imode;
#endif /* not HAVE_SGTTY_H */
while (TerminalSet(tty, &ioval) == -1) {
if (errno == EINTR || errno == EAGAIN)
continue;
printf("Error occured while set %x: errno=%d\n", mode, errno);
reset_error_exit(SIGNAL_ARGLIST);
}
#endif
}
void
ttymode_reset(int mode, int imode)
{
#ifndef __MINGW32_VERSION
TerminalMode ioval;
TerminalGet(tty, &ioval);
MODEFLAG(ioval) &= ~mode;
#ifndef HAVE_SGTTY_H
IMODEFLAG(ioval) &= ~imode;
#endif /* not HAVE_SGTTY_H */
while (TerminalSet(tty, &ioval) == -1) {
if (errno == EINTR || errno == EAGAIN)
continue;
printf("Error occured while reset %x: errno=%d\n", mode, errno);
reset_error_exit(SIGNAL_ARGLIST);
}
#endif /* __MINGW32_VERSION */
}
#ifndef HAVE_SGTTY_H
void
set_cc(int spec, int val)
{
TerminalMode ioval;
TerminalGet(tty, &ioval);
ioval.c_cc[spec] = val;
while (TerminalSet(tty, &ioval) == -1) {
if (errno == EINTR || errno == EAGAIN)
continue;
printf("Error occured: errno=%d\n", errno);
reset_error_exit(SIGNAL_ARGLIST);
}
}
#endif /* not HAVE_SGTTY_H */
void
close_tty(void)
{
if (tty > 2)
close(tty);
}
char *
ttyname_tty(void)
{
return ttyname(tty);
}
void
reset_tty(void)
{
writestr(T_op); /* turn off */
writestr(T_me);
if (!Do_not_use_ti_te) {
if (T_te && *T_te)
writestr(T_te);
else
writestr(T_cl);
}
writestr(T_se); /* reset terminal */
flush_tty();
TerminalSet(tty, &d_ioval);
close_tty();
}
static MySignalHandler
reset_exit_with_value(SIGNAL_ARG, int rval)
{
#ifdef USE_MOUSE
if (mouseActive)
mouse_end();
#endif /* USE_MOUSE */
reset_tty();
w3m_exit(rval);
SIGNAL_RETURN;
}
MySignalHandler
reset_error_exit(SIGNAL_ARG)
{
reset_exit_with_value(SIGNAL_ARGLIST, 1);
}
MySignalHandler
reset_exit(SIGNAL_ARG)
{
reset_exit_with_value(SIGNAL_ARGLIST, 0);
}
MySignalHandler
error_dump(SIGNAL_ARG)
{
mySignal(SIGIOT, SIG_DFL);
reset_tty();
abort();
SIGNAL_RETURN;
}
void
set_int(void)
{
mySignal(SIGHUP, reset_exit);
mySignal(SIGINT, reset_exit);
mySignal(SIGQUIT, reset_exit);
mySignal(SIGTERM, reset_exit);
mySignal(SIGILL, error_dump);
mySignal(SIGIOT, error_dump);
mySignal(SIGFPE, error_dump);
#ifdef SIGBUS
mySignal(SIGBUS, error_dump);
#endif /* SIGBUS */
/* mySignal(SIGSEGV, error_dump); */
}
static void
setgraphchar(void)
{
int c, i, n;
for (c = 0; c < 96; c++)
gcmap[c] = (char)(c + ' ');
if (!T_ac)
return;
n = strlen(T_ac);
for (i = 0; i < n - 1; i += 2) {
c = (unsigned)T_ac[i] - ' ';
if (c >= 0 && c < 96)
gcmap[c] = T_ac[i + 1];
}
}
#define graphchar(c) (((unsigned)(c)>=' ' && (unsigned)(c)<128)? gcmap[(c)-' '] : (c))
#define GETSTR(v,s) {v = pt; suc = tgetstr(s,&pt); if (!suc) v = ""; else v = allocStr(suc, -1); }
void
getTCstr(void)
{
char *ent;
char *suc;
char *pt = funcstr;
int r;
ent = getenv("TERM") ? getenv("TERM") : DEFAULT_TERM;
if (ent == NULL) {
fprintf(stderr, "TERM is not set\n");
reset_error_exit(SIGNAL_ARGLIST);
}
r = tgetent(bp, ent);
if (r != 1) {
/* Can't find termcap entry */
fprintf(stderr, "Can't find termcap entry %s\n", ent);
reset_error_exit(SIGNAL_ARGLIST);
}
GETSTR(T_ce, "ce"); /* clear to the end of line */
GETSTR(T_cd, "cd"); /* clear to the end of display */
GETSTR(T_kr, "nd"); /* cursor right */
if (suc == NULL)
GETSTR(T_kr, "kr");
if (tgetflag("bs"))
T_kl = "\b"; /* cursor left */
else {
GETSTR(T_kl, "le");
if (suc == NULL)
GETSTR(T_kl, "kb");
if (suc == NULL)
GETSTR(T_kl, "kl");
}
GETSTR(T_cr, "cr"); /* carriage return */
GETSTR(T_ta, "ta"); /* tab */
GETSTR(T_sc, "sc"); /* save cursor */
GETSTR(T_rc, "rc"); /* restore cursor */
GETSTR(T_so, "so"); /* standout mode */
GETSTR(T_se, "se"); /* standout mode end */
GETSTR(T_us, "us"); /* underline mode */
GETSTR(T_ue, "ue"); /* underline mode end */
GETSTR(T_md, "md"); /* bold mode */
GETSTR(T_me, "me"); /* bold mode end */
GETSTR(T_cl, "cl"); /* clear screen */
GETSTR(T_cm, "cm"); /* cursor move */
GETSTR(T_al, "al"); /* append line */
GETSTR(T_sr, "sr"); /* scroll reverse */
GETSTR(T_ti, "ti"); /* terminal init */
GETSTR(T_te, "te"); /* terminal end */
GETSTR(T_nd, "nd"); /* move right one space */
GETSTR(T_eA, "eA"); /* enable alternative charset */
GETSTR(T_as, "as"); /* alternative (graphic) charset start */
GETSTR(T_ae, "ae"); /* alternative (graphic) charset end */
GETSTR(T_ac, "ac"); /* graphics charset pairs */
GETSTR(T_op, "op"); /* set default color pair to its original value */
#if defined( CYGWIN ) && CYGWIN < 1
/* for TERM=pcansi on MS-DOS prompt. */
#if 0
T_eA = "";
T_as = "\033[12m";
T_ae = "\033[10m";
T_ac = "l\001k\002m\003j\004x\005q\006n\020a\024v\025w\026u\027t\031";
#endif
T_eA = "";
T_as = "";
T_ae = "";
T_ac = "";
#endif /* CYGWIN */
LINES = COLS = 0;
setlinescols();
setgraphchar();
}
void
setlinescols(void)
{
char *p;
int i;
#ifdef __EMX__
{
int s[2];
_scrsize(s);
COLS = s[0];
LINES = s[1];
if (getenv("WINDOWID")) {
FILE *fd = popen("scrsize", "rt");
if (fd) {
fscanf(fd, "%i %i", &COLS, &LINES);
pclose(fd);
}
}
}
#elif defined(HAVE_TERMIOS_H) && defined(TIOCGWINSZ)
struct winsize wins;
i = ioctl(tty, TIOCGWINSZ, &wins);
if (i >= 0 && wins.ws_row != 0 && wins.ws_col != 0) {
LINES = wins.ws_row;
COLS = wins.ws_col;
}
#endif /* defined(HAVE-TERMIOS_H) && defined(TIOCGWINSZ) */
if (LINES <= 0 && (p = getenv("LINES")) != NULL && (i = atoi(p)) >= 0)
LINES = i;
if (COLS <= 0 && (p = getenv("COLUMNS")) != NULL && (i = atoi(p)) >= 0)
COLS = i;
if (LINES <= 0)
LINES = tgetnum("li"); /* number of line */
if (COLS <= 0)
COLS = tgetnum("co"); /* number of column */
if (COLS > MAX_COLUMN)
COLS = MAX_COLUMN;
if (LINES > MAX_LINE)
LINES = MAX_LINE;
#if defined(__CYGWIN__)
LASTLINE = LINES - (isWinConsole == TERM_CYGWIN_RESERVE_IME ? 2 : 1);
#endif /* defined(__CYGWIN__) */
}
void
setupscreen(void)
{
int i;
if (LINES + 1 > max_LINES) {
max_LINES = LINES + 1;
max_COLS = 0;
ScreenElem = New_N(Screen, max_LINES);
ScreenImage = New_N(Screen *, max_LINES);
}
if (COLS + 1 > max_COLS) {
max_COLS = COLS + 1;
for (i = 0; i < max_LINES; i++) {
#ifdef USE_M17N
ScreenElem[i].lineimage = New_N(char *, max_COLS);
bzero((void *)ScreenElem[i].lineimage, max_COLS * sizeof(char *));
#else
ScreenElem[i].lineimage = New_N(char, max_COLS);
#endif
ScreenElem[i].lineprop = New_N(l_prop, max_COLS);
}
}
for (i = 0; i < LINES; i++) {
ScreenImage[i] = &ScreenElem[i];
ScreenImage[i]->lineprop[0] = S_EOL;
ScreenImage[i]->isdirty = 0;
}
for (; i < max_LINES; i++) {
ScreenElem[i].isdirty = L_UNUSED;
}
clear();
}
/*
* Screen initialize
*/
int
initscr(void)
{
if (set_tty() < 0)
return -1;
set_int();
getTCstr();
if (T_ti && !Do_not_use_ti_te)
writestr(T_ti);
setupscreen();
return 0;
}
static int
write1(char c)
{
putc(c, ttyf);
#ifdef SCREEN_DEBUG
flush_tty();
#endif /* SCREEN_DEBUG */
return 0;
}
void
move(int line, int column)
{
if (line >= 0 && line < LINES)
CurLine = line;
if (column >= 0 && column < COLS)
CurColumn = column;
}
#ifdef USE_BG_COLOR
#define M_SPACE (S_SCREENPROP|S_COLORED|S_BCOLORED|S_GRAPHICS)
#else /* not USE_BG_COLOR */
#define M_SPACE (S_SCREENPROP|S_COLORED|S_GRAPHICS)
#endif /* not USE_BG_COLOR */
static int
#ifdef USE_M17N
need_redraw(char *c1, l_prop pr1, char *c2, l_prop pr2)
{
if (!c1 || !c2 || strcmp(c1, c2))
return 1;
if (*c1 == ' ')
#else
need_redraw(char c1, l_prop pr1, char c2, l_prop pr2)
{
if (c1 != c2)
return 1;
if (c1 == ' ')
#endif
return (pr1 ^ pr2) & M_SPACE & ~S_DIRTY;
if ((pr1 ^ pr2) & ~S_DIRTY)
return 1;
return 0;
}
#define M_CEOL (~(M_SPACE|C_WHICHCHAR))
#ifdef USE_M17N
#define SPACE " "
#else
#define SPACE ' '
#endif
#ifdef USE_M17N
void
addch(char c)
{
addmch(&c, 1);
}
void
addmch(char *pc, size_t len)
#else
void
addch(char pc)
#endif
{
l_prop *pr;
int dest, i;
short *dirty;
#ifdef USE_M17N
static Str tmp = NULL;
char **p;
char c = *pc;
int width = wtf_width((wc_uchar *) pc);
if (tmp == NULL)
tmp = Strnew();
Strcopy_charp_n(tmp, pc, len);
pc = tmp->ptr;
#else
char *p;
char c = pc;
#endif
if (CurColumn == COLS)
wrap();
if (CurColumn >= COLS)
return;
p = ScreenImage[CurLine]->lineimage;
pr = ScreenImage[CurLine]->lineprop;
dirty = &ScreenImage[CurLine]->isdirty;
#ifndef USE_M17N
/* Eliminate unprintables according to * iso-8859-*.
* Particularly 0x96 messes up T.Dickey's * (xfree-)xterm */
if (IS_INTSPACE(c))
c = ' ';
#endif
if (pr[CurColumn] & S_EOL) {
if (c == ' ' && !(CurrentMode & M_SPACE)) {
CurColumn++;
return;
}
for (i = CurColumn; i >= 0 && (pr[i] & S_EOL); i--) {
SETCH(p[i], SPACE, 1);
SETPROP(pr[i], (pr[i] & M_CEOL) | C_ASCII);
}
}
if (c == '\t' || c == '\n' || c == '\r' || c == '\b')
SETCHMODE(CurrentMode, C_CTRL);
#ifdef USE_M17N