28 #include "../../idlib/precompiled.h"
29 #include "../sys_local.h"
31 #include <sys/types.h>
47 #define MAX_OSPATH 256
48 #define COMMAND_HISTORY 64
50 static int input_hide = 0;
53 static char input_ret[256];
56 static int history_count = 0;
57 static int history_start = 0;
58 static int history_current = 0;
64 static bool tty_enabled =
false;
65 static struct termios tty_tc;
72 static int set_exit = 0;
73 static char exit_spawn[ 1024 ];
83 if ( tcsetattr( 0, TCSADRAIN, &tty_tc ) == -1 ) {
84 Sys_Printf(
"tcsetattr failed: %s\n", strerror( errno ) );
93 if ( exit_spawn[0] ) {
134 common->
DPrintf(
"Sys_StartProcess %s (delaying until final exit)\n", exeName );
172 gettimeofday(&tp,
NULL);
201 bool dironly =
false;
214 if (extension[0] ==
'/' && extension[1] == 0) {
221 if ((fdir = opendir(directory)) ==
NULL) {
223 common->
Printf(
"Sys_ListFiles: opendir %s failed\n", directory);
228 while ((d = readdir(fdir)) !=
NULL) {
229 idStr::snPrintf(search,
sizeof(search),
"%s/%s", directory, d->d_name);
230 if (stat(search, &st) == -1)
236 if (extension[0] !=
'\0' && ext.
Icmp(&extension[1]) != 0) {
240 if ((dironly && !(st.st_mode & S_IFDIR)) ||
241 (!dironly && (st.st_mode & S_IFDIR)))
250 common->
Printf(
"Sys_ListFiles: %d entries in %s\n", list.
Num(), directory );
262 #define MAX_QUED_EVENTS 256
263 #define MASK_QUED_EVENTS ( MAX_QUED_EVENTS - 1 )
276 int ptrLength,
void *ptr ) {
318 memset(&ev, 0,
sizeof(ev));
340 getcwd( cwd,
sizeof( cwd ) - 1 );
356 common->
Printf(
"FIXME: Sys_GetCurrentMemoryStatus\n" );
360 common->
Printf(
"FIXME: Sys_GetExeLaunchMemoryStatus\n" );
389 void *handle = dlopen( path, RTLD_NOW );
391 Sys_Printf(
"dlopen '%s' failed: %s\n", path, dlerror() );
403 void *ret = dlsym( (
void *)handle, sym );
404 if ((error = dlerror()) !=
NULL) {
405 Sys_Printf(
"dlsym '%s' failed: %s\n", sym, error );
416 dlclose( (
void *)handle );
443 if ( now - last > 1000 ) {
444 Sys_Printf(
"WARNING: Sys_Sleep - %d < 20 msec is not portable\n", msec);
451 if (usleep(msec * 1000) == -1)
540 exit_spawn[0] =
'\0';
572 if ( isatty( STDIN_FILENO ) != 1 ) {
573 Sys_Printf(
"terminal support disabled: stdin is not a tty\n" );
577 if ( tcgetattr( 0, &tty_tc ) == -1 ) {
578 Sys_Printf(
"tcgetattr failed. disabling terminal support: %s\n", strerror( errno ) );
583 if ( fcntl( STDIN_FILENO, F_SETFL, fcntl( STDIN_FILENO, F_GETFL, 0 ) | O_NONBLOCK ) == -1 ) {
584 Sys_Printf(
"fcntl STDIN non blocking failed. disabling terminal support: %s\n", strerror( errno ) );
597 tc.c_lflag &= ~(ECHO | ICANON);
602 tc.c_iflag &= ~(ISTRIP | INPCK);
605 if ( tcsetattr( 0, TCSADRAIN, &tc ) == -1 ) {
606 Sys_Printf(
"tcsetattr failed: %s\n", strerror( errno ) );
607 Sys_Printf(
"terminal support may not work correctly. Use +set in_tty 0 to disable it\n" );
611 if ( fcntl( STDOUT_FILENO, F_SETFL, fcntl( STDOUT_FILENO, F_GETFL, 0 ) | O_NONBLOCK ) == -1 ) {
612 Sys_Printf(
"fcntl STDOUT non blocking failed: %s\n", strerror( errno ) );
617 char *term = getenv(
"TERM" );
619 if (
strcmp( term,
"linux" ) &&
strcmp( term,
"xterm" ) &&
strcmp( term,
"xterm-color" ) &&
strcmp( term,
"screen" ) ) {
620 Sys_Printf(
"WARNING: terminal type '%s' is unknown. terminal support may not work correctly\n", term );
623 Sys_Printf(
"terminal support enabled ( use +set in_tty 0 to disabled )\n" );
638 write( STDOUT_FILENO, &key, 1 );
640 write( STDOUT_FILENO, &key, 1 );
642 write( STDOUT_FILENO, &key, 1 );
647 write( STDOUT_FILENO, &key, 1 );
652 write( STDOUT_FILENO, &key, 1 );
653 write( STDOUT_FILENO,
"[C", 2 );
660 if ( !tty_enabled ) {
673 buf_len = strlen( input_field.
GetBuffer() );
674 while ( buf_len > 0 ) {
684 if ( !tty_enabled ) {
689 if ( input_hide == 0 ) {
692 write( STDOUT_FILENO, buf, strlen( buf ) );
693 int back = strlen( buf ) - input_field.
GetCursor();
704 while ( read(0, &key, 1) != -1 ) {
722 while ( ( ret = read( STDIN_FILENO, &key, 1 ) ) > 0 ) {
742 write( STDOUT_FILENO, &key, 1 );
745 history[ history_count ] = input_ret;
748 history[ history_start ] = input_ret;
759 ret = read( STDIN_FILENO, &key, 1 );
769 ret = read( STDIN_FILENO, &key, 1 );
771 Sys_Printf(
"dropping sequence: '27' '79' " );
787 Sys_Printf(
"dropping sequence: '27' '79' '%d' ", key );
795 ret = read( STDIN_FILENO, &key, 1 );
797 Sys_Printf(
"dropping sequence: '27' '91' " );
805 ret = read( STDIN_FILENO, &key, 1 );
806 if ( ret <= 0 || key != 126 ) {
807 Sys_Printf(
"dropping sequence: '27' '91' '49' '%d' ", key );
818 ret = read( STDIN_FILENO, &key, 1 );
819 if ( ret <= 0 || key != 126 ) {
820 Sys_Printf(
"dropping sequence: '27' '91' '50' '%d' ", key );
831 ret = read( STDIN_FILENO, &key, 1 );
832 if ( ret <= 0 || key != 126 ) {
833 Sys_Printf(
"dropping sequence: '27' '91' '52' '%d' ", key );
844 ret = read( STDIN_FILENO, &key, 1 );
846 Sys_Printf(
"dropping sequence: '27' '91' '51' " );
856 Sys_Printf(
"dropping sequence: '27' '91' '51' '%d'", key );
865 if ( history_current == 0 ) {
878 if ( history_current < 0 ) {
884 if ( history_current == 0 ) {
890 input_field.
SetBuffer( history[ index ] );
903 Sys_Printf(
"dropping sequence: '27' '91' '%d' ", key );
912 Sys_Printf(
"dropping sequence: '27' '%d' ", key );
925 Sys_Printf(
"dropping sequence: '%d' ", key );
946 FD_SET( STDIN_FILENO, &fdset );
949 if (
select( 1, &fdset,
NULL,
NULL, &timeout ) == -1 || !FD_ISSET( 0, &fdset ) ) {
953 len = read( 0, input_ret,
sizeof( input_ret ) );
960 Sys_Printf(
"read failed: %s\n", strerror( errno ) );
964 if ( len ==
sizeof( input_ret ) ) {
968 input_ret[ len-1 ] =
'\0';
986 len = strlen( s ) + 1;
1003 va_start( argptr, fmt );
1004 vprintf( fmt, argptr );
1011 vprintf( fmt, arg );
1019 va_start( argptr, msg );
1020 vprintf( msg, argptr );
1040 va_start( argptr, error );
void Sys_Mkdir(const char *path)
static int snPrintf(char *dest, int size, const char *fmt,...) id_attribute((format(printf
GLsizei const GLfloat * value
assert(prefInfo.fullscreenBtn)
idCVarSystem * cvarSystem
void Posix_SetExitSpawn(const char *exeName)
void Sys_GetExeLaunchMemoryStatus(sysMemoryStats_t &stats)
void Sys_FlushCacheMemory(void *base, int bytes)
int GetCursor(void) const
void Sys_FreeOpenAL(void)
void Sys_Printf(const char *msg,...)
unsigned long sys_timeBase
void Sys_DLL_Unload(int handle)
void Sys_DebugPrintf(const char *fmt,...)
GLuint GLuint GLsizei GLenum type
void Sys_GenerateEvents(void)
void Posix_StartAsyncThread(void)
const char * Sys_FPU_GetState(void)
int Icmp(const char *text) const
bool Sys_UnlockMemory(void *ptr, int bytes)
int Sys_DLL_Load(const char *path)
long Sys_FileTimeStamp(FILE *fp)
void Posix_InitPThreads()
void Sys_FPU_ClearStack(void)
virtual void BufferCommandText(cmdExecution_t exec, const char *text)=0
int Sys_GetDriveFreeSpace(const char *path)
int Sys_ListFiles(const char *directory, const char *extension, idStrList &list)
bool Sys_FPU_StackIsEmpty(void)
idCVar in_tty("in_tty","1", CVAR_BOOL|CVAR_INIT|CVAR_SYSTEM,"terminal tab-completion and history")
void Sys_ShowConsole(int visLevel, bool quitOnClose)
void Posix_SetExit(int ret)
void Posix_EarlyInit(void)
void Posix_InitConsoleInput(void)
void KeyDownEvent(int key)
void SetInteger(const int value)
void * Sys_DLL_GetProcAddress(int handle, const char *sym)
int GetInteger(void) const
int Sys_GetSystemRam(void)
void Posix_Shutdown(void)
static void Copynz(char *dest, const char *src, int destsize)
void Sys_DestroyThread(xthreadInfo &info)
char * Sys_GetClipboardData(void)
bool Sys_LockMemory(void *ptr, int bytes)
char * Posix_ConsoleInput(void)
virtual void Printf(const char *fmt,...) id_attribute((format(printf
int Append(const type &obj)
idCVar com_pid("com_pid","0", CVAR_INTEGER|CVAR_INIT|CVAR_SYSTEM,"process id")
void Sys_DebugVPrintf(const char *fmt, va_list arg)
void Sys_SetClipboardData(const char *string)
const char * Sys_DefaultCDPath(void)
void Sys_GetMemoryStatus(sysMemoryStats_t &stats)
void Posix_QueEvent(sysEventType_t type, int value, int value2, int ptrLength, void *ptr)
void Sys_ClearEvents(void)
idEditField history_backup
void SetBuffer(const char *buffer)
const char * Posix_Cwd(void)
sysEvent_t eventQue[MAX_QUED_EVENTS]
void Sys_VPrintf(const char *msg, va_list arg)
void Sys_SetPhysicalWorkMemory(int minBytes, int maxBytes)
virtual void StartProcess(const char *exeName, bool quit)
bool Sys_AlreadyRunning(void)
virtual bool GetCVarBool(const char *name) const =0
void SetBool(const bool value)
void Sys_GetCurrentMemoryStatus(sysMemoryStats_t &stats)
void Sys_Error(const char *error,...)
void Sys_DoStartProcess(const char *exeName, bool dofork)
void * Mem_Alloc(const int size)
int Sys_GetVideoRam(void)
void Sys_FPU_SetPrecision(int precision)
int Sys_Milliseconds(void)
void ExtractFileExtension(idStr &dest) const
sysEvent_t Sys_GetEvent(void)
virtual void DPrintf(const char *fmt,...) id_attribute((format(printf
ID_INLINE T Min(T x, T y)
void Posix_LateInit(void)