This repository has been archived by the owner on Jan 23, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 4.9k
/
Copy pathpal_console.c
477 lines (398 loc) · 12.5 KB
/
pal_console.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
// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.
// See the LICENSE file in the project root for more information.
#include "pal_config.h"
#include "pal_console.h"
#include "pal_utilities.h"
#include "pal_signal.h"
#include <assert.h>
#include <errno.h>
#include <fcntl.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/ioctl.h>
#include <termios.h>
#include <unistd.h>
#include <poll.h>
#include <pthread.h>
#include <signal.h>
int32_t SystemNative_GetWindowSize(WinSize* windowSize)
{
assert(windowSize != NULL);
#if HAVE_IOCTL && HAVE_TIOCGWINSZ
int error = ioctl(STDOUT_FILENO, TIOCGWINSZ, windowSize);
if (error != 0)
{
memset(windowSize, 0, sizeof(WinSize)); // managed out param must be initialized
}
return error;
#else
errno = ENOTSUP;
return -1;
#endif
}
int32_t SystemNative_IsATty(intptr_t fd)
{
return isatty(ToFileDescriptor(fd));
}
static char* g_keypadXmit = NULL; // string used to enable application mode, from terminfo
static void WriteKeypadXmit()
{
// If a terminfo "application mode" keypad_xmit string has been supplied,
// write it out to the terminal to enter the mode.
if (g_keypadXmit != NULL)
{
ssize_t ret;
while (CheckInterrupted(ret = write(STDOUT_FILENO, g_keypadXmit, (size_t)(sizeof(char) * strlen(g_keypadXmit)))));
assert(ret >= 0); // failure to change the mode should not prevent app from continuing
}
}
void SystemNative_SetKeypadXmit(const char* terminfoString)
{
assert(terminfoString != NULL);
if (g_keypadXmit != NULL) // should only happen if initialization initially failed
{
free(g_keypadXmit);
assert(false && "g_keypadXmit was already initialized");
}
// Store the string to use to enter application mode, then enter
g_keypadXmit = strdup(terminfoString);
WriteKeypadXmit();
}
static pthread_mutex_t g_lock = PTHREAD_MUTEX_INITIALIZER; // prevents races when initializing and changing the terminal.
static bool g_signalForBreak = true; // tracks whether the terminal should send signals for breaks, such that attributes have been changed
static struct termios g_initTermios; // the initial attributes captured
static bool g_hasCurrentTermios = false; // tracks whether g_currentTermios is valid
static struct termios g_currentTermios; // the latest attributes set
// The terminal can be used by the .NET application via the Console class.
// It may also be used by child processes that are started via the Process class.
// The terminal needs to be configured differently depending on the user.
// ConfigureTerminalForXXX are called to change the configuration.
// When it is ambiguous whether we should configure for Console/a child Process,
// we prefer configuring for the Console.
static bool g_reading = false; // tracks whether the application is performing a Console.Read operation
static bool g_childUsesTerminal = false; // tracks whether a child process is using the terminal
static bool g_terminalUninitialized = false; // tracks whether the application is terminating
static bool g_hasTty = false; // cache we are not a tty
static volatile bool g_receivedSigTtou = false;
static void ttou_handler(int signo)
{
(void)signo;
g_receivedSigTtou = true;
}
static void InstallTTOUHandler(void (*handler)(int), int flags)
{
struct sigaction action;
memset(&action, 0, sizeof(action));
action.sa_handler = handler;
action.sa_flags = flags;
int rvSigaction = sigaction(SIGTTOU, &action, NULL);
assert(rvSigaction == 0);
(void)rvSigaction;
}
static bool TcSetAttr(struct termios* termios, bool blockIfBackground)
{
if (g_terminalUninitialized)
{
// The application is exiting, we mustn't change terminal settings.
return true;
}
if (!blockIfBackground)
{
// When the process is running in background, changing terminal settings
// will stop it (default SIGTTOU action).
// We change SIGTTOU's disposition to get EINTR instead.
// This thread may be used to run a signal handler, which may write to
// stdout. We set SA_RESETHAND to avoid that handler's write loops infinitly
// on EINTR when the process is running in background and the terminal
// configured with TOSTOP.
InstallTTOUHandler(ttou_handler, (int)SA_RESETHAND);
g_receivedSigTtou = false;
}
bool rv = tcsetattr(STDIN_FILENO, TCSANOW, termios) >= 0;
if (!blockIfBackground)
{
if (!rv && errno == EINTR && g_receivedSigTtou)
{
// Operation failed because we are background
// pretend it went fine.
rv = true;
}
// Restore default SIGTTOU handler.
InstallTTOUHandler(SIG_DFL, 0);
}
// On success, update the cached value.
if (rv)
{
g_hasCurrentTermios = true;
g_currentTermios = *termios;
}
return rv;
}
static bool ConfigureTerminal(bool signalForBreak, bool forChild, uint8_t minChars, uint8_t decisecondsTimeout, bool blockIfBackground)
{
if (!g_hasTty)
{
errno = ENOTTY;
return false;
}
g_childUsesTerminal = forChild;
struct termios termios = g_initTermios;
if (signalForBreak)
termios.c_lflag |= (uint32_t)ISIG;
else
termios.c_lflag &= (uint32_t)(~ISIG);
if (!forChild)
{
termios.c_iflag &= (uint32_t)(~(IXON | IXOFF));
termios.c_lflag &= (uint32_t)(~(ECHO | ICANON | IEXTEN));
}
termios.c_cc[VMIN] = minChars;
termios.c_cc[VTIME] = decisecondsTimeout;
// Check if the settings have changed.
if (g_hasCurrentTermios)
{
if (g_currentTermios.c_lflag == termios.c_lflag &&
g_currentTermios.c_iflag == termios.c_iflag &&
g_currentTermios.c_cc[VMIN] == termios.c_cc[VMIN] &&
g_currentTermios.c_cc[VMIN] == termios.c_cc[VMIN])
{
return true;
}
}
return TcSetAttr(&termios, blockIfBackground);
}
void UninitializeTerminal()
{
assert(g_hasTty);
// This method is called on SIGQUIT/SIGINT from the signal dispatching thread
// and on atexit.
if (pthread_mutex_lock(&g_lock) == 0)
{
if (!g_terminalUninitialized)
{
TcSetAttr(&g_initTermios, /* blockIfBackground */ false);
g_terminalUninitialized = true;
}
pthread_mutex_unlock(&g_lock);
}
}
void SystemNative_InitializeConsoleBeforeRead(uint8_t minChars, uint8_t decisecondsTimeout)
{
if (pthread_mutex_lock(&g_lock) == 0)
{
g_reading = true;
ConfigureTerminal(g_signalForBreak, /* forChild */ false, minChars, decisecondsTimeout, /* blockIfBackground */ true);
pthread_mutex_unlock(&g_lock);
}
}
void SystemNative_UninitializeConsoleAfterRead()
{
if (pthread_mutex_lock(&g_lock) == 0)
{
g_reading = false;
pthread_mutex_unlock(&g_lock);
}
}
void SystemNative_ConfigureTerminalForChildProcess(int32_t childUsesTerminal)
{
assert(childUsesTerminal == 0 || childUsesTerminal == 1);
if (pthread_mutex_lock(&g_lock) == 0)
{
// If the application is performing a read, assume the child process won't use the terminal.
if (g_reading)
{
return;
}
// If no more children are using the terminal, invalidate our cached termios.
if (!childUsesTerminal)
{
g_hasCurrentTermios = false;
}
ConfigureTerminal(g_signalForBreak, /* forChild */ childUsesTerminal, /* minChars */ 1, /* decisecondsTimeout */ 0, /* blockIfBackground */ false);
// Redo "Application mode" when there are no more children using the terminal.
if (!childUsesTerminal)
{
WriteKeypadXmit();
}
pthread_mutex_unlock(&g_lock);
}
}
static int TranslatePalControlCharacterName(int name)
{
switch (name)
{
#ifdef VINTR
case PAL_VINTR: return VINTR;
#endif
#ifdef VQUIT
case PAL_VQUIT: return VQUIT;
#endif
#ifdef VERASE
case PAL_VERASE: return VERASE;
#endif
#ifdef VKILL
case PAL_VKILL: return VKILL;
#endif
#ifdef VEOF
case PAL_VEOF: return VEOF;
#endif
#ifdef VTIME
case PAL_VTIME: return VTIME;
#endif
#ifdef VMIN
case PAL_VMIN: return VMIN;
#endif
#ifdef VSWTC
case PAL_VSWTC: return VSWTC;
#endif
#ifdef VSTART
case PAL_VSTART: return VSTART;
#endif
#ifdef VSTOP
case PAL_VSTOP: return VSTOP;
#endif
#ifdef VSUSP
case PAL_VSUSP: return VSUSP;
#endif
#ifdef VEOL
case PAL_VEOL: return VEOL;
#endif
#ifdef VREPRINT
case PAL_VREPRINT: return VREPRINT;
#endif
#ifdef VDISCARD
case PAL_VDISCARD: return VDISCARD;
#endif
#ifdef VWERASE
case PAL_VWERASE: return VWERASE;
#endif
#ifdef VLNEXT
case PAL_VLNEXT: return VLNEXT;
#endif
#ifdef VEOL2
case PAL_VEOL2: return VEOL2;
#endif
default: return -1;
}
}
void SystemNative_GetControlCharacters(
int32_t* controlCharacterNames, uint8_t* controlCharacterValues, int32_t controlCharacterLength,
uint8_t* posixDisableValue)
{
assert(controlCharacterNames != NULL);
assert(controlCharacterValues != NULL);
assert(controlCharacterLength >= 0);
assert(posixDisableValue != NULL);
#ifdef _POSIX_VDISABLE
*posixDisableValue = _POSIX_VDISABLE;
#else
*posixDisableValue = 0;
#endif
memset(controlCharacterValues, *posixDisableValue, sizeof(uint8_t) * Int32ToSizeT(controlCharacterLength));
if (controlCharacterLength > 0)
{
struct termios newTermios;
memset(&newTermios, 0, sizeof(struct termios));
if (tcgetattr(STDIN_FILENO, &newTermios) >= 0)
{
for (int i = 0; i < controlCharacterLength; i++)
{
int name = TranslatePalControlCharacterName(controlCharacterNames[i]);
if (name >= 0)
{
controlCharacterValues[i] = newTermios.c_cc[name];
}
}
}
}
}
int32_t SystemNative_StdinReady()
{
SystemNative_InitializeConsoleBeforeRead(1, 0);
struct pollfd fd = { .fd = STDIN_FILENO, .events = POLLIN };
int rv = poll(&fd, 1, 0) > 0 ? 1 : 0;
SystemNative_UninitializeConsoleAfterRead();
return rv;
}
int32_t SystemNative_ReadStdin(void* buffer, int32_t bufferSize)
{
assert(buffer != NULL || bufferSize == 0);
assert(bufferSize >= 0);
if (bufferSize < 0)
{
errno = EINVAL;
return -1;
}
ssize_t count;
while (CheckInterrupted(count = read(STDIN_FILENO, buffer, Int32ToSizeT(bufferSize))));
return (int32_t)count;
}
int32_t SystemNative_GetSignalForBreak()
{
return g_signalForBreak;
}
int32_t SystemNative_SetSignalForBreak(int32_t signalForBreak)
{
assert(signalForBreak == 0 || signalForBreak == 1);
int rv = 0;
if (pthread_mutex_lock(&g_lock) == 0)
{
if (ConfigureTerminal(signalForBreak, /* forChild */ false, /* minChars */ 1, /* decisecondsTimeout */ 0, /* blockIfBackground */ true))
{
g_signalForBreak = signalForBreak;
rv = 1;
}
pthread_mutex_unlock(&g_lock);
}
return rv;
}
void ReinitializeTerminal()
{
// Restores the state of the terminal after being suspended.
// pal_signal.cpp calls this on SIGCONT from the signal handling thread.
if (pthread_mutex_lock(&g_lock) == 0)
{
if (!g_childUsesTerminal)
{
if (g_hasCurrentTermios)
{
TcSetAttr(&g_currentTermios, /* blockIfBackground */ false);
}
WriteKeypadXmit();
}
pthread_mutex_unlock(&g_lock);
}
}
static void InitializeTerminalCore()
{
bool haveInitTermios = tcgetattr(STDIN_FILENO, &g_initTermios) >= 0;
if (haveInitTermios)
{
g_hasTty = true;
g_hasCurrentTermios = true;
g_currentTermios = g_initTermios;
g_signalForBreak = g_initTermios.c_lflag & (uint32_t)ISIG;
atexit(UninitializeTerminal);
}
else
{
g_signalForBreak = true;
}
}
int32_t SystemNative_InitializeTerminalAndSignalHandling()
{
static int32_t initialized = 0;
// Both the Process and Console class call this method for initialization.
if (pthread_mutex_lock(&g_lock) == 0)
{
if (initialized == 0)
{
InitializeTerminalCore();
initialized = InitializeSignalHandlingCore();
}
pthread_mutex_unlock(&g_lock);
}
return initialized;
}