26 #include "../../common/common.h"
27 #include "../system.h"
53 #define CON_HISTORY 32
64 while (read(STDIN_FILENO, &key, 1) != -1)
77 write(STDOUT_FILENO, &key, 1) || 0;
79 write(STDOUT_FILENO, &key, 1) || 0;
81 write(STDOUT_FILENO, &key, 1) || 0;
90 if (ttyConsoleHistory.
cursor > 0) {
91 for (
unsigned int i = 0;
i < ttyConsoleHistory.
cursor;
i++)
103 write(STDOUT_FILENO,
"]", 1) || 0;
104 if (ttyConsoleHistory.
cursor) {
105 for (
unsigned int i = 0;
i < ttyConsoleHistory.
cursor;
i++) {
106 write(STDOUT_FILENO, ttyConsoleHistory.
buffer +
i, 1) || 0;
120 for (
int i = size - 1;
i > 0;
i--)
121 ttyEditLines[
i] = ttyEditLines[
i - 1];
123 ttyEditLines[0] = *
field;
171 static int ttyConsoleHide = 0;
177 assert(ttyConsoleHide > 0);
179 if (ttyConsoleHide == 0)
182 if (ttyConsoleHide == 0)
196 tcsetattr(STDIN_FILENO, TCSADRAIN, &
TTY_tc);
200 fcntl(STDIN_FILENO, F_SETFL, fcntl(STDIN_FILENO, F_GETFL, 0) & ~O_NONBLOCK);
210 const char* term = getenv(
"TERM");
211 return isatty(STDIN_FILENO) && !(term && (
Q_streq(term,
"raw") ||
Q_streq(term,
"dumb")));
221 signal(SIGTTIN, SIG_IGN);
222 signal(SIGTTOU, SIG_IGN);
228 fcntl(STDIN_FILENO, F_SETFL, fcntl(STDIN_FILENO, F_GETFL, 0) | O_NONBLOCK);
238 tcgetattr(STDIN_FILENO, &
TTY_tc);
241 struct termios tc =
TTY_tc;
250 tc.c_lflag &= ~(ECHO | ICANON);
256 tc.c_iflag &= ~(ISTRIP | INPCK);
259 tcsetattr(STDIN_FILENO, TCSADRAIN, &tc);
266 static char text[256];
270 int avail = read(STDIN_FILENO, &key, 1);
275 if (key ==
TTY_erase || key == 127 || key == 8) {
276 if (ttyConsoleHistory.
cursor > 0) {
277 ttyConsoleHistory.
cursor--;
278 ttyConsoleHistory.
buffer[ttyConsoleHistory.
cursor] =
'\0';
284 if (key && key <
' ') {
291 write(1, &key, 1) || 0;
292 write(1,
"]", 1) || 0;
296 const size_t size =
sizeof(ttyConsoleHistory.
buffer);
297 const char* s = ttyConsoleHistory.
buffer;
298 char* target = ttyConsoleHistory.
buffer;
304 avail = read(STDIN_FILENO, &key, 1);
307 if (key ==
'[' || key ==
'O') {
309 avail = read(STDIN_FILENO, &key, 1);
316 ttyConsoleHistory = *history;
326 ttyConsoleHistory = *history;
345 if (ttyConsoleHistory.
cursor >=
sizeof(text) - 1)
349 ttyConsoleHistory.
cursor++;
351 write(STDOUT_FILENO, &key, 1) || 0;
357 struct timeval timeout;
360 FD_SET(STDIN_FILENO, &fdset);
363 if (select(STDIN_FILENO + 1, &fdset,
nullptr,
nullptr, &timeout) == -1
364 || !FD_ISSET(STDIN_FILENO, &fdset))
367 const int len = read(STDIN_FILENO, text,
sizeof(text));
385 const int origflags = fcntl(STDOUT_FILENO, F_GETFL, 0);
387 fcntl(STDOUT_FILENO, F_SETFL, origflags & ~FNDELAY);
389 const ssize_t written = write(STDOUT_FILENO,
string, strlen(
string));
394 fcntl(STDOUT_FILENO, F_SETFL, origflags);
void Sys_ShowConsole(bool show)
static void Sys_TTYConsoleShow(void)
Show the current line.
static bool ttyConsoleActivated
static void Sys_TTYConsoleHide(void)
Clear the display of the line currently edited bring cursor back to beginning of line.
void Com_Printf(const char *const fmt,...)
void Sys_ConsoleShutdown(void)
Shutdown the console.
static consoleHistory_t ttyConsoleHistory
static bool Sys_IsATTY(void)
void Sys_ConsoleInit(void)
Initialize the console input (tty mode if possible)
void Q_strncpyz(char *dest, const char *src, size_t destsize)
Safe strncpy that ensures a trailing zero.
static void Sys_TTYConsoleSigCont(int signum)
Reinitialize console input after receiving SIGCONT, as on Linux the terminal seems to lose all set at...
static void Sys_TTYConsoleHistoryAdd(consoleHistory_t *field)
static void Sys_TTYConsoleHistoryClear(consoleHistory_t *edit)
void Sys_ConsoleOutput(const char *string)
const char * Sys_ConsoleInput(void)
static consoleHistory_t * Sys_TTYConsoleHistoryPrevious(void)
static struct termios TTY_tc
bool Com_ConsoleCompleteCommand(const char *s, char *target, size_t bufSize, uint32_t *pos, uint32_t offset)
Console completion for command and variables.
QGL_EXTERN GLuint GLchar GLuint * len
static consoleHistory_t ttyEditLines[CON_HISTORY]
static void CON_FlushIn(void)
Flush stdin, I suspect some terminals are sending a LOT of shit.
static void Sys_TTYDeleteCharacter(void)
Output a backspace.
static consoleHistory_t * Sys_TTYConsoleHistoryNext(void)