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
|
/*
===========================================================================
Copyright (C) 1999-2005 Id Software, Inc.
This file is part of Tremulous.
Tremulous is free software; you can redistribute it
and/or modify it under the terms of the GNU General Public License as
published by the Free Software Foundation; either version 2 of the License,
or (at your option) any later version.
Tremulous is distributed in the hope that it will be
useful, but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with Tremulous; if not, write to the Free Software
Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
===========================================================================
*/
#include "../qcommon/q_shared.h"
#include "../qcommon/qcommon.h"
#include <unistd.h>
#include <signal.h>
#include <termios.h>
#include <fcntl.h>
/*
=============================================================
tty console routines
NOTE: if the user is editing a line when something gets printed to the early
console then it won't look good so we provide CON_Hide and CON_Show to be
called before and after a stdout or stderr output
=============================================================
*/
// general flag to tell about tty console mode
static qboolean ttycon_on = qfalse;
static int ttycon_hide = 0;
// some key codes that the terminal may be using, initialised on start up
static int TTY_erase;
static int TTY_eof;
static struct termios TTY_tc;
static field_t TTY_con;
// This is somewhat of aduplicate of the graphical console history
// but it's safer more modular to have our own here
#define CON_HISTORY 32
static field_t ttyEditLines[ CON_HISTORY ];
static int hist_current = -1, hist_count = 0;
/*
==================
CON_FlushIn
Flush stdin, I suspect some terminals are sending a LOT of shit
FIXME relevant?
==================
*/
static void CON_FlushIn( void )
{
char key;
while (read(0, &key, 1)!=-1);
}
/*
==================
CON_Back
Output a backspace
NOTE: it seems on some terminals just sending '\b' is not enough so instead we
send "\b \b"
(FIXME there may be a way to find out if '\b' alone would work though)
==================
*/
static void CON_Back( void )
{
char key;
key = '\b';
write(1, &key, 1);
key = ' ';
write(1, &key, 1);
key = '\b';
write(1, &key, 1);
}
/*
==================
CON_Hide
Clear the display of the line currently edited
bring cursor back to beginning of line
==================
*/
void CON_Hide( void )
{
if( ttycon_on )
{
int i;
if (ttycon_hide)
{
ttycon_hide++;
return;
}
if (TTY_con.cursor>0)
{
for (i=0; i<TTY_con.cursor; i++)
{
CON_Back();
}
}
CON_Back(); // Delete "]"
ttycon_hide++;
}
}
/*
==================
CON_Show
Show the current line
FIXME need to position the cursor if needed?
==================
*/
void CON_Show( void )
{
if( ttycon_on )
{
int i;
assert(ttycon_hide>0);
ttycon_hide--;
if (ttycon_hide == 0)
{
write( 1, "]", 1 );
if (TTY_con.cursor)
{
for (i=0; i<TTY_con.cursor; i++)
{
write(1, TTY_con.buffer+i, 1);
}
}
}
}
}
/*
==================
CON_Shutdown
Never exit without calling this, or your terminal will be left in a pretty bad state
==================
*/
void CON_Shutdown( void )
{
if (ttycon_on)
{
CON_Back(); // Delete "]"
tcsetattr (0, TCSADRAIN, &TTY_tc);
}
// Restore blocking to stdin reads
fcntl( 0, F_SETFL, fcntl( 0, F_GETFL, 0 ) & ~O_NDELAY );
}
/*
==================
Hist_Add
==================
*/
void Hist_Add(field_t *field)
{
int i;
assert(hist_count <= CON_HISTORY);
assert(hist_count >= 0);
assert(hist_current >= -1);
assert(hist_current <= hist_count);
// make some room
for (i=CON_HISTORY-1; i>0; i--)
{
ttyEditLines[i] = ttyEditLines[i-1];
}
ttyEditLines[0] = *field;
if (hist_count<CON_HISTORY)
{
hist_count++;
}
hist_current = -1; // re-init
}
/*
==================
Hist_Prev
==================
*/
field_t *Hist_Prev( void )
{
int hist_prev;
assert(hist_count <= CON_HISTORY);
assert(hist_count >= 0);
assert(hist_current >= -1);
assert(hist_current <= hist_count);
hist_prev = hist_current + 1;
if (hist_prev >= hist_count)
{
return NULL;
}
hist_current++;
return &(ttyEditLines[hist_current]);
}
/*
==================
Hist_Next
==================
*/
field_t *Hist_Next( void )
{
assert(hist_count <= CON_HISTORY);
assert(hist_count >= 0);
assert(hist_current >= -1);
assert(hist_current <= hist_count);
if (hist_current >= 0)
{
hist_current--;
}
if (hist_current == -1)
{
return NULL;
}
return &(ttyEditLines[hist_current]);
}
/*
==================
CON_Init
Initialize the console input (tty mode if possible)
==================
*/
void CON_Init( void )
{
struct termios tc;
// If the process is backgrounded (running non interactively)
// then SIGTTIN or SIGTOU is emitted, if not caught, turns into a SIGSTP
signal(SIGTTIN, SIG_IGN);
signal(SIGTTOU, SIG_IGN);
// Make stdin reads non-blocking
fcntl( 0, F_SETFL, fcntl( 0, F_GETFL, 0 ) | O_NDELAY );
if (isatty(STDIN_FILENO)!=1)
{
Com_Printf( "stdin is not a tty, tty console mode disabled\n");
ttycon_on = qfalse;
return;
}
Field_Clear(&TTY_con);
tcgetattr (0, &TTY_tc);
TTY_erase = TTY_tc.c_cc[VERASE];
TTY_eof = TTY_tc.c_cc[VEOF];
tc = TTY_tc;
/*
ECHO: don't echo input characters
ICANON: enable canonical mode. This enables the special
characters EOF, EOL, EOL2, ERASE, KILL, REPRINT,
STATUS, and WERASE, and buffers by lines.
ISIG: when any of the characters INTR, QUIT, SUSP, or
DSUSP are received, generate the corresponding sig
nal
*/
tc.c_lflag &= ~(ECHO | ICANON);
/*
ISTRIP strip off bit 8
INPCK enable input parity checking
*/
tc.c_iflag &= ~(ISTRIP | INPCK);
tc.c_cc[VMIN] = 1;
tc.c_cc[VTIME] = 0;
tcsetattr (0, TCSADRAIN, &tc);
ttycon_on = qtrue;
}
/*
==================
CON_ConsoleInput
==================
*/
char *CON_ConsoleInput( void )
{
// we use this when sending back commands
static char text[256];
int avail;
char key;
field_t *history;
if( ttycon_on )
{
avail = read(0, &key, 1);
if (avail != -1)
{
// we have something
// backspace?
// NOTE TTimo testing a lot of values .. seems it's the only way to get it to work everywhere
if ((key == TTY_erase) || (key == 127) || (key == 8))
{
if (TTY_con.cursor > 0)
{
TTY_con.cursor--;
TTY_con.buffer[TTY_con.cursor] = '\0';
CON_Back();
}
return NULL;
}
// check if this is a control char
if ((key) && (key) < ' ')
{
if (key == '\n')
{
// push it in history
Hist_Add(&TTY_con);
strcpy(text, TTY_con.buffer);
Field_Clear(&TTY_con);
key = '\n';
write(1, &key, 1);
write( 1, "]", 1 );
return text;
}
if (key == '\t')
{
CON_Hide();
Field_AutoComplete( &TTY_con );
CON_Show();
return NULL;
}
avail = read(0, &key, 1);
if (avail != -1)
{
// VT 100 keys
if (key == '[' || key == 'O')
{
avail = read(0, &key, 1);
if (avail != -1)
{
switch (key)
{
case 'A':
history = Hist_Prev();
if (history)
{
CON_Hide();
TTY_con = *history;
CON_Show();
}
CON_FlushIn();
return NULL;
break;
case 'B':
history = Hist_Next();
CON_Hide();
if (history)
{
TTY_con = *history;
} else
{
Field_Clear(&TTY_con);
}
CON_Show();
CON_FlushIn();
return NULL;
break;
case 'C':
return NULL;
case 'D':
return NULL;
}
}
}
}
Com_DPrintf("droping ISCTL sequence: %d, TTY_erase: %d\n", key, TTY_erase);
CON_FlushIn();
return NULL;
}
// push regular character
TTY_con.buffer[TTY_con.cursor] = key;
TTY_con.cursor++;
// print the current line (this is differential)
write(1, &key, 1);
}
return NULL;
}
else
{
int len;
fd_set fdset;
struct timeval timeout;
static qboolean stdin_active;
if (!com_dedicated || !com_dedicated->value)
return NULL;
if (!stdin_active)
return NULL;
FD_ZERO(&fdset);
FD_SET(0, &fdset); // stdin
timeout.tv_sec = 0;
timeout.tv_usec = 0;
if (select (1, &fdset, NULL, NULL, &timeout) == -1 || !FD_ISSET(0, &fdset))
{
return NULL;
}
len = read (0, text, sizeof(text));
if (len == 0)
{ // eof!
stdin_active = qfalse;
return NULL;
}
if (len < 1)
return NULL;
text[len-1] = 0; // rip off the /n and terminate
return text;
}
}
|