1 | #include <stdlib.h>
|
---|
2 | #include <termcap.h>
|
---|
3 | #include <sys/types.h>
|
---|
4 | #include <sys/ioctl.h>
|
---|
5 | #include <curses.h>
|
---|
6 | #include "curspriv.h"
|
---|
7 |
|
---|
8 | struct termios _orig_tty, _tty;
|
---|
9 | cursv _cursvar;
|
---|
10 |
|
---|
11 | WINDOW *stdscr, *curscr;
|
---|
12 | int LINES, COLS;
|
---|
13 | bool NONL;
|
---|
14 |
|
---|
15 | char termcap[1024]; /* termcap buffer */
|
---|
16 | char tc[200]; /* area to hold string capabilities */
|
---|
17 | char *ttytype; /* terminal type from env */
|
---|
18 | static char *arp; /* pointer for use in tgetstr */
|
---|
19 | char *cp; /* character pointer */
|
---|
20 |
|
---|
21 | char *cl; /* clear screen capability */
|
---|
22 | char *cm; /* cursor motion capability */
|
---|
23 | char *so; /* start standout capability */
|
---|
24 | char *se; /* end standout capability */
|
---|
25 | char *mr; /* start of reverse */
|
---|
26 | char *me; /* revert to normal */
|
---|
27 | char *mb; /* start of blink */
|
---|
28 | char *md; /* start of bold */
|
---|
29 | char *us; /* start of underscore */
|
---|
30 | char *ue; /* end of underscore */
|
---|
31 | char *vi; /* cursor invisible */
|
---|
32 | char *ve; /* cursor normal */
|
---|
33 | char *vs; /* cursor good visible */
|
---|
34 | char *as; /* alternative charset start */
|
---|
35 | char *ae; /* alternative charset end */
|
---|
36 | char *bl; /* ring the bell */
|
---|
37 | char *vb; /* visual bell */
|
---|
38 |
|
---|
39 | /* fatal - report error and die. Never returns */
|
---|
40 | void fatal(s)
|
---|
41 | char *s;
|
---|
42 | {
|
---|
43 | (void) fprintf(stderr, "curses: %s\n", s);
|
---|
44 | exit(1);
|
---|
45 | }
|
---|
46 |
|
---|
47 | /* Outc - call putchar, necessary because putchar is a macro. */
|
---|
48 | void outc(c)
|
---|
49 | int c;
|
---|
50 | {
|
---|
51 | putchar(c);
|
---|
52 | }
|
---|
53 |
|
---|
54 | /* Move cursor to r,c */
|
---|
55 | void poscur(r, c)
|
---|
56 | int r, c;
|
---|
57 | {
|
---|
58 | tputs(tgoto(cm, c, r), 1, outc);
|
---|
59 | }
|
---|
60 |
|
---|
61 | /* Clear the screen */
|
---|
62 | void clrscr()
|
---|
63 | {
|
---|
64 | tputs(cl, 1, outc);
|
---|
65 | }
|
---|
66 |
|
---|
67 | /* This are terminal independent characters which can be used in curses */
|
---|
68 |
|
---|
69 | unsigned int ACS_ULCORNER;
|
---|
70 | unsigned int ACS_LLCORNER;
|
---|
71 | unsigned int ACS_URCORNER;
|
---|
72 | unsigned int ACS_LRCORNER;
|
---|
73 | unsigned int ACS_RTEE;
|
---|
74 | unsigned int ACS_LTEE;
|
---|
75 | unsigned int ACS_BTEE;
|
---|
76 | unsigned int ACS_TTEE;
|
---|
77 | unsigned int ACS_HLINE;
|
---|
78 | unsigned int ACS_VLINE;
|
---|
79 | unsigned int ACS_PLUS;
|
---|
80 | unsigned int ACS_S1;
|
---|
81 | unsigned int ACS_S9;
|
---|
82 | unsigned int ACS_DIAMOND;
|
---|
83 | unsigned int ACS_CKBOARD;
|
---|
84 | unsigned int ACS_DEGREE;
|
---|
85 | unsigned int ACS_PLMINUS;
|
---|
86 | unsigned int ACS_BULLET;
|
---|
87 | unsigned int ACS_LARROW;
|
---|
88 | unsigned int ACS_RARROW;
|
---|
89 | unsigned int ACS_DARROW;
|
---|
90 | unsigned int ACS_UARROW;
|
---|
91 | unsigned int ACS_BOARD;
|
---|
92 | unsigned int ACS_LANTERN;
|
---|
93 | unsigned int ACS_BLOCK;
|
---|
94 |
|
---|
95 | /* These defines describe the full set of grafic block characters which
|
---|
96 | * can be defined via termcap.
|
---|
97 | */
|
---|
98 |
|
---|
99 | #define RIGHTARROW 0
|
---|
100 | #define LEFTARROW 1
|
---|
101 | #define DOWNARROW 2
|
---|
102 | #define UPARROW 3
|
---|
103 | #define FULLSQUARE 4
|
---|
104 | #define GREYSQUARE 5
|
---|
105 | #define EMPTYSQUARE 6
|
---|
106 | #define LATERN 7
|
---|
107 | #define DIAMOND 8
|
---|
108 | #define DEGREE 9
|
---|
109 | #define PLUSMINUS 10
|
---|
110 | #define DOWNRIGHT 11
|
---|
111 | #define UPRIGHT 12
|
---|
112 | #define UPLEFT 13
|
---|
113 | #define DOWNLEFT 14
|
---|
114 | #define CROSS 15
|
---|
115 | #define UPLINE 16
|
---|
116 | #define UPMIDLINE 17
|
---|
117 | #define MIDLINE 18
|
---|
118 | #define DOMIDLINE 19
|
---|
119 | #define DOWNLINE 20
|
---|
120 | #define TEELEFT 21
|
---|
121 | #define TEERIGHT 22
|
---|
122 | #define TEEHEAD 23
|
---|
123 | #define TEENORMAL 24
|
---|
124 | #define VERTLINE 25
|
---|
125 | #define PARAGRAPH 26
|
---|
126 |
|
---|
127 | unsigned int _cursgraftable[27] =
|
---|
128 | {
|
---|
129 | '>', '<', 'v', '^', '#', ':', ' ', '#', '+', '\'', '#', '+', '+',
|
---|
130 | '+', '+', '+', '-', ' ', '-', ' ', '_', '+', '+', '+', '+', '|'
|
---|
131 | };
|
---|
132 | char _cursident[28] = "+,.-0ahI`fgjklmnopqrstuvwx~";
|
---|
133 |
|
---|
134 | int setterm(type)
|
---|
135 | char *type;
|
---|
136 | {
|
---|
137 | unsigned char *ac;
|
---|
138 | int i;
|
---|
139 | #ifdef TIOCGWINSZ
|
---|
140 | struct winsize wsize;
|
---|
141 | #endif
|
---|
142 |
|
---|
143 | if (tgetent(termcap, type) != 1) return ERR;
|
---|
144 |
|
---|
145 | #ifdef TIOCGWINSZ
|
---|
146 | if (ioctl(0, TIOCGWINSZ, &wsize) == 0) {
|
---|
147 | LINES = wsize.ws_row != 0 ? wsize.ws_row : tgetnum("li");
|
---|
148 | COLS = wsize.ws_col != 0 ? wsize.ws_col : tgetnum("co");
|
---|
149 | } else {
|
---|
150 | #endif
|
---|
151 | LINES = tgetnum("li");
|
---|
152 | COLS = tgetnum("co");
|
---|
153 | #ifdef TIOCGWINSZ
|
---|
154 | }
|
---|
155 | #endif
|
---|
156 | arp = tc;
|
---|
157 | cl = tgetstr("cl", &arp);
|
---|
158 | so = tgetstr("so", &arp);
|
---|
159 | se = tgetstr("se", &arp);
|
---|
160 | cm = tgetstr("cm", &arp);
|
---|
161 | mr = tgetstr("mr", &arp);
|
---|
162 | me = tgetstr("me", &arp);
|
---|
163 | mb = tgetstr("mb", &arp);
|
---|
164 | md = tgetstr("md", &arp);
|
---|
165 | us = tgetstr("us", &arp);
|
---|
166 | ue = tgetstr("ue", &arp);
|
---|
167 | vi = tgetstr("vi", &arp);
|
---|
168 | ve = tgetstr("ve", &arp);
|
---|
169 | vs = tgetstr("vs", &arp);
|
---|
170 | as = tgetstr("as", &arp);
|
---|
171 | ae = tgetstr("ae", &arp);
|
---|
172 | ac = (unsigned char *) tgetstr("ac", &arp);
|
---|
173 | bl = tgetstr("bl", &arp);
|
---|
174 | vb = tgetstr("vb", &arp);
|
---|
175 |
|
---|
176 | if (ac) {
|
---|
177 | while (*ac) {
|
---|
178 | i = 0;
|
---|
179 | while (*ac != _cursident[i]) i++;
|
---|
180 | _cursgraftable[i] = *++ac | A_ALTCHARSET;
|
---|
181 | ac++;
|
---|
182 | }
|
---|
183 | }
|
---|
184 |
|
---|
185 | ACS_ULCORNER = _cursgraftable[UPLEFT];
|
---|
186 | ACS_LLCORNER = _cursgraftable[DOWNLEFT];
|
---|
187 | ACS_URCORNER = _cursgraftable[UPRIGHT];
|
---|
188 | ACS_LRCORNER = _cursgraftable[DOWNRIGHT];
|
---|
189 | ACS_RTEE = _cursgraftable[TEERIGHT];
|
---|
190 | ACS_LTEE = _cursgraftable[TEELEFT];
|
---|
191 | ACS_BTEE = _cursgraftable[TEEHEAD];
|
---|
192 | ACS_TTEE = _cursgraftable[TEENORMAL];
|
---|
193 | ACS_HLINE = _cursgraftable[MIDLINE];
|
---|
194 | ACS_VLINE = _cursgraftable[VERTLINE];
|
---|
195 | ACS_PLUS = _cursgraftable[CROSS];
|
---|
196 | ACS_S1 = _cursgraftable[UPLINE];
|
---|
197 | ACS_S9 = _cursgraftable[DOWNLINE];
|
---|
198 | ACS_DIAMOND = _cursgraftable[DIAMOND];
|
---|
199 | ACS_CKBOARD = _cursgraftable[GREYSQUARE];
|
---|
200 | ACS_DEGREE = _cursgraftable[DEGREE];
|
---|
201 | ACS_PLMINUS = _cursgraftable[PLUSMINUS];
|
---|
202 | ACS_BULLET = 'o'; /* where the hell is a bullet defined in
|
---|
203 | * termcap ??? */
|
---|
204 | ACS_LARROW = _cursgraftable[LEFTARROW];
|
---|
205 | ACS_RARROW = _cursgraftable[RIGHTARROW];
|
---|
206 | ACS_DARROW = _cursgraftable[DOWNARROW];
|
---|
207 | ACS_UARROW = _cursgraftable[UPARROW];
|
---|
208 | ACS_BOARD = _cursgraftable[EMPTYSQUARE];
|
---|
209 | ACS_LANTERN = _cursgraftable[LATERN];
|
---|
210 | ACS_BLOCK = _cursgraftable[FULLSQUARE];
|
---|
211 | /* Wow, I got it! */
|
---|
212 | return OK;
|
---|
213 | }
|
---|
214 |
|
---|
215 | void gettmode()
|
---|
216 | {
|
---|
217 | tcgetattr(0, &_orig_tty);
|
---|
218 | tcgetattr(0, &_tty);
|
---|
219 | _cursvar.echoit = (_tty.c_lflag & ECHO) != 0;
|
---|
220 | _cursvar.rawmode = (_tty.c_lflag & (ICANON|ISIG)) == 0;
|
---|
221 | _cursvar.cbrkmode = (_tty.c_lflag & (ICANON|ISIG)) == ISIG;
|
---|
222 | NONL = (_tty.c_iflag & ICRNL) != 0;
|
---|
223 | }
|
---|