/* * Copyright(c) 1997-2001 Id Software, Inc. * Copyright(c) 2002 The Quakeforge Project. * Copyright(c) 2006 Quetoo. * * This program 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. * * This program 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 this program; if not, write to the Free Software * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. */ #include #include #include #include #include #include #include #include #include #include #include #include #include "qcommon.h" #include "keys.h" #include "game.h" cvar_t *nostdout; unsigned sys_frame_time; qboolean stdin_active = true; /* Sys_ConsoleInput Handles text input from stdin for dedicated servers. */ char *Sys_ConsoleInput(void){ static char text[256]; int len; fd_set fdset; struct timeval timeout; if(!dedicated || !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) return NULL; if((len = read(0, text, sizeof(text))) == 0){ // eof, shell io redirection stdin_active = false; // don't select again return NULL; } if(len < 1) return NULL; text[len -1] = 0; // rip off the /n and terminate return text; } /* Sys_ConsoleOutput Dumps string to stdout. Any high bit prefixes for in-game colors are removed. */ void Sys_ConsoleOutput(char *string){ if(nostdout && nostdout->value) return; if(!string) return; if(string[0] == 1 || string[0] == 2){ // skip color prefix string++; } fputs(string, stdout); } /* Sys_Printf For formatted output. */ void Sys_Printf(char *fmt, ...){ va_list argptr; char text[1024]; unsigned char *p; va_start(argptr, fmt); vsnprintf(text, 1024, fmt, argptr); va_end(argptr); if(nostdout && nostdout->value) return; for(p =(unsigned char *)text; *p; p++){ *p &= 0x7f; if((*p > 128 || *p < 32) && *p != 10 && *p != 13 && *p != 9) printf("[%02x]", *p); else putc(*p, stdout); } } /* Sys_Milliseconds */ int curtime; int Sys_Milliseconds(void){ struct timeval tp; struct timezone tzp; static int secbase; gettimeofday(&tp, &tzp); if(!secbase){ secbase = tp.tv_sec; return tp.tv_usec / 1000; } curtime = (tp.tv_sec - secbase) * 1000 + tp.tv_usec / 1000; return curtime; } /* Sys_Mkdir Create the specified directory path. */ void Sys_Mkdir(char *path){ mkdir(path, 0777); } static char findbase[MAX_OSPATH]; static char findpath[MAX_OSPATH]; static char findpattern[MAX_OSPATH]; static DIR *fdir; /* Sys_FindFirst Returns the first full path name matched by the specified search path in the Quake file system. Wildcards are partially supported. */ char *Sys_FindFirst(char *path){ struct dirent *d; char *p; if(fdir) Sys_Error("Sys_FindFirst without Sys_FindClose"); strcpy(findbase, path); if((p = strrchr(findbase, '/')) != NULL){ *p = 0; strcpy(findpattern, p + 1); } else strcpy(findpattern, "*"); if(strcmp(findpattern, "*.*") == 0) strcpy(findpattern, "*"); if((fdir = opendir(findbase)) == NULL) return NULL; while((d = readdir(fdir)) != NULL){ if(!*findpattern || COM_GlobMatch(findpattern, d->d_name)){ sprintf(findpath, "%s/%s", findbase, d->d_name); return findpath; } } return NULL; } char *Sys_FindNext(){ struct dirent *d; if(fdir == NULL) return NULL; while((d = readdir(fdir)) != NULL){ if(!*findpattern || COM_GlobMatch(findpattern, d->d_name)){ sprintf(findpath, "%s/%s", findbase, d->d_name); return findpath; } } return NULL; } void Sys_FindClose(void){ if(fdir != NULL) closedir(fdir); fdir = NULL; } static void *game_library = NULL; typedef game_export_t *gameapi_t(game_import_t *); /* Sys_UnloadGame Closes an open game module. */ void Sys_UnloadGame(void){ if(game_library) dlclose(game_library); game_library = NULL; } /* Sys_GetGameAPI Attempts to open and load the game module. */ void *Sys_GetGameAPI(void *parms){ gameapi_t *GetGameAPI; FILE *fp; char name[MAX_OSPATH]; char *path; setreuid(getuid(), getuid()); setegid(getgid()); if(game_library) Com_Error(ERR_FATAL, "Sys_GetGameAPI without Sys_UnloadGame"); Com_Printf("Game initialization..\n"); // now run through the search paths path = NULL; while(1){ path = FS_NextPath(path); if(!path) return NULL; snprintf(name, MAX_OSPATH, "%s/game.so", path); fp = fopen(name, "rb"); if(fp == NULL) continue; fclose(fp); Com_Printf("Trying %s..\n", name); game_library = dlopen(name, RTLD_NOW); if(game_library) break; Com_Printf("%s\n", dlerror()); } GetGameAPI = (gameapi_t *)dlsym(game_library, "GetGameAPI"); if(!GetGameAPI){ Sys_UnloadGame(); return NULL; } Com_Printf("Game initialized.\n"); return GetGameAPI(parms); } /* Sys_Quit The final exit point of the program under normal exit conditions. */ void Sys_Quit(void){ CL_Shutdown(); Qcommon_Shutdown(); fcntl(0, F_SETFL, fcntl(0, F_GETFL, 0) & ~FNDELAY); exit(0); } /* The final exit point of the program under abnormal exit conditions. */ void Sys_Error(char *error, ...){ va_list argptr; char string[1024]; // change stdin to non blocking fcntl(0, F_SETFL, fcntl(0, F_GETFL, 0) & ~FNDELAY); CL_Shutdown(); Qcommon_Shutdown(); va_start(argptr, error); vsnprintf(string, 1024, error, argptr); va_end(argptr); fprintf(stderr, "Error: %s\n", string); exit(1); } /* Sys_Signal Catch kernel interrupts and dispatch the appropriate exit routine. */ void Sys_Signal(int s){ switch(s){ case SIGHUP: case SIGINT: case SIGQUIT: case SIGTERM: Com_Printf("Received signal %d, quitting..\n", s); Sys_Quit(); break; default: Sys_Error("Received signal %d.\n", s); break; } } /* main The entry point of the program. */ int main(int argc, char **argv){ int time, oldtime, newtime; printf("Quetoo %s\n", VERSION); Qcommon_Init(argc, argv); signal(SIGHUP, Sys_Signal); signal(SIGINT, Sys_Signal); signal(SIGQUIT, Sys_Signal); signal(SIGILL, Sys_Signal); signal(SIGABRT, Sys_Signal); signal(SIGFPE, Sys_Signal); signal(SIGSEGV, Sys_Signal); signal(SIGTERM, Sys_Signal); nostdout = Cvar_Get("nostdout", "0", 0); if(!nostdout->value) fcntl(0, F_SETFL, fcntl(0, F_GETFL, 0) | FNDELAY); oldtime = Sys_Milliseconds(); while(1){ // this is our main loop do { // resolve time spent in last frame newtime = Sys_Milliseconds(); time = newtime - oldtime; } while(time < 1); Qcommon_Frame(time); oldtime = newtime; } }