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
|
#if !defined(lint) && !defined(DOS)
static char rcsid[] = "$Id: tty.c 769 2007-10-24 00:15:40Z hubert@u.washington.edu $";
#endif
/*
* ========================================================================
* Copyright 2006-2007 University of Washington
* Copyright 2013-2019 Eduardo Chappa
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* ========================================================================
*
* Program: tty routines
*/
#include <system.h>
#include <general.h>
#include "../estruct.h"
#include "../mode.h"
#include "../pico.h"
#include "../edef.h"
#include "../efunc.h"
#include "../keydefs.h"
#include "signals.h"
#ifndef _WINDOWS
#include "terminal.h"
#include "raw.h"
#include "read.h"
#else
#include "mswin.h"
#endif /* _WINDOWS */
#ifdef MOUSE
#include "mouse.h"
#endif /* MOUSE */
#include "tty.h"
#ifndef _WINDOWS
/*
* ttopen - this function is called once to set up the terminal device
* streams. if called as pine composer, don't mess with
* tty modes, but set signal handlers.
*/
int
ttopen(void)
{
if(Pmaster == NULL){
Raw(1);
#ifdef MOUSE
if(gmode & MDMOUSE)
init_mouse();
#endif /* MOUSE */
xonxoff_proc(preserve_start_stop);
}
picosigs();
return(1);
}
/*
* ttclose - this function gets called just before we go back home to
* the command interpreter. If called as pine composer, don't
* worry about modes, but set signals to default, pine will
* rewire things as needed.
*/
int
ttclose(void)
{
if(Pmaster){
signal(SIGHUP, SIG_DFL);
#ifdef SIGCONT
signal(SIGCONT, SIG_DFL);
#endif
#if defined(SIGWINCH) && defined(TIOCGWINSZ)
signal(SIGWINCH, SIG_DFL);
#endif
}
else{
Raw(0);
#ifdef MOUSE
end_mouse();
#endif
}
return(1);
}
/*
* ttgetc - Read a character from the terminal, performing no editing
* and doing no echo at all.
*
* Args: return_on_intr -- Function to get a single character from stdin,
* recorder -- If non-NULL, function used to record keystroke.
* bail_handler -- Function used to bail out on read error.
*
* Returns: The character read from stdin.
* Return_on_intr is returned if read is interrupted.
* If read error, BAIL_OUT is returned unless bail_handler is
* non-NULL, in which case it is called (and usually it exits).
*
* If recorder is non-null, it is used to record the keystroke.
*/
int
ttgetc(int return_on_intr, int (*recorder)(int), void (*bail_handler)(void))
{
int c;
switch(c = read_one_char()){
case READ_INTR:
return(return_on_intr);
case BAIL_OUT:
if(bail_handler)
(*bail_handler)();
else
return(BAIL_OUT);
default:
return(recorder ? (*recorder)(c) : c);
}
}
/*
* Simple version of ttgetc with simple error handling
*
* Args: recorder -- If non-NULL, function used to record keystroke.
* bail_handler -- Function used to bail out on read error.
*
* Returns: The character read from stdin.
* If read error, BAIL_OUT is returned unless bail_handler is
* non-NULL, in which case it is called (and usually it exits).
*
* If recorder is non-null, it is used to record the keystroke.
* Retries if interrupted.
*/
int
simple_ttgetc(int (*recorder)(int), void (*bail_handler)(void))
{
int res;
unsigned char c;
while((res = read(STDIN_FD, &c, 1)) <= 0)
if(!(res < 0 && errno == EINTR))
(*bail_handler)();
return(recorder ? (*recorder)((int)c) : (int)c);
}
/*
* ttputc - Write a character to the display.
*/
int
ttputc(UCS ucs)
{
unsigned char obuf[MAX(MB_LEN_MAX,32)];
int r, i, width = 0, outchars = 0;
int ret = 0;
if(ucs < 0x80)
return(putchar((unsigned char) ucs));
width = wcellwidth(ucs);
if(width < 0){
width = 1;
obuf[outchars++] = '?';
}
else{
/*
* Convert the ucs into the multibyte
* character that corresponds to the
* ucs in the users locale.
*/
outchars = wtomb((char *) obuf, ucs);
if(outchars < 0){
width = 1;
obuf[0] = '?';
outchars = 1;
}
}
for(i = 0; i < outchars; i++){
r = putchar(obuf[i]);
ret = (ret == EOF) ? EOF : r;
}
return(ret);
}
/*
* ttflush - flush terminal buffer. Does real work where the terminal
* output is buffered up. A no-operation on systems where byte
* at a time terminal I/O is done.
*/
int
ttflush(void)
{
return(fflush(stdout));
}
/*
* ttresize - recompute the screen dimensions if necessary, and then
* adjust pico's internal buffers accordingly.
*/
void
ttresize(void)
{
int row = -1, col = -1;
ttgetwinsz(&row, &col);
resize_pico(row, col);
}
/*
* ttgetwinsz - set global row and column values (if we can get them)
* and return.
*/
void
ttgetwinsz(int *row, int *col)
{
extern int _tlines, _tcolumns;
if(*row < 0)
*row = (_tlines > 0) ? _tlines - 1 : NROW - 1;
if(*col <= 0)
*col = (_tcolumns > 0) ? _tcolumns : NCOL;
#if defined(SIGWINCH) && defined(TIOCGWINSZ)
{
struct winsize win;
if (ioctl(0, TIOCGWINSZ, &win) == 0) { /* set to anything useful.. */
if(win.ws_row) /* ... the tty drivers says */
*row = win.ws_row - 1;
if(win.ws_col)
*col = win.ws_col;
}
signal(SIGWINCH, winch_handler); /* window size changes */
}
#endif
if(*col > NLINE-1)
*col = NLINE-1;
}
#else /* _WINDOWS */
#define MARGIN 8 /* size of minimim margin and */
#define SCRSIZ 64 /* scroll size for extended lines */
#define MROW 2 /* rows in menu */
/* internal prototypes */
int mswin_resize (int, int);
/*
* Standard terminal interface dispatch table. Fields point to functions
* that operate the terminal. All these functions live in mswin.c, but
* this structure is defined here because it is specific to pico.
*/
TERM term = {
0,
0,
MARGIN,
MROW,
ttopen,
NULL,
ttclose,
NULL, /* was mswin_getc, but not used? */
mswin_putc,
mswin_flush,
mswin_move,
mswin_eeol,
mswin_eeop,
mswin_beep,
mswin_rev
};
/*
* This function is called once to set up the terminal device streams.
*/
int
ttopen(void)
{
int rows, columns;
mswin_getscreensize (&rows, &columns);
term.t_nrow = rows - 1;
term.t_ncol = columns;
/* term.t_scrsiz = (columns * 2) / 3; */
/*
* Do we implement optimized character insertion and deletion?
* o_insert() and o_delete()
*/
/* inschar = delchar = FALSE; */
/* revexist = TRUE; dead code? */
mswin_setresizecallback (mswin_resize);
init_mouse();
return(1);
}
/*
* This function gets called just before we go back home to the command
* interpreter.
*/
int
ttclose(void)
{
mswin_clearresizecallback (mswin_resize);
return(1);
}
/*
* Flush terminal buffer. Does real work where the terminal output is buffered
* up. A no-operation on systems where byte at a time terminal I/O is done.
*/
int
ttflush(void)
{
return(1);
}
/*
* ttresize - recompute the screen dimensions if necessary, and then
* adjust pico's internal buffers accordingly.
*/
void
ttresize(void)
{
int row, col;
mswin_getscreensize(&row, &col);
resize_pico (row-1, col);
}
/*
* mswin_resize - windows specific callback to set pico's internal tables
* to new screen dimensions.
*/
int
mswin_resize(int row, int col)
{
if (wheadp)
resize_pico (row-1, col);
return (0);
}
#endif /* _WINDOWS */
|