Quake-2

Quake 2 GPL Source Release
Log | Files | Refs

sys_irix.c (7346B)


      1 #include <unistd.h>
      2 #include <signal.h>
      3 #include <stdlib.h>
      4 #include <limits.h>
      5 #include <sys/time.h>
      6 #include <sys/types.h>
      7 #include <unistd.h>
      8 #include <fcntl.h>
      9 #include <stdarg.h>
     10 #include <stdio.h>
     11 #include <sys/ipc.h>
     12 #include <sys/shm.h>
     13 #include <sys/stat.h>
     14 #include <string.h>
     15 #include <ctype.h>
     16 #include <sys/wait.h>
     17 #include <sys/mman.h>
     18 #include <errno.h>
     19 #include <mntent.h>
     20 
     21 #include <dlfcn.h>
     22 
     23 #include "../qcommon/qcommon.h"
     24 
     25 #include "../linux/rw_linux.h"
     26 
     27 cvar_t *nostdout;
     28 
     29 unsigned	sys_frame_time;
     30 
     31 uid_t saved_euid;
     32 qboolean stdin_active = true;
     33 
     34 // =======================================================================
     35 // General routines
     36 // =======================================================================
     37 
     38 void Sys_ConsoleOutput (char *string)
     39 {
     40 	if (nostdout && nostdout->value)
     41 		return;
     42 
     43 	fputs(string, stdout);
     44 }
     45 
     46 void Sys_Printf (char *fmt, ...)
     47 {
     48 	va_list		argptr;
     49 	char		text[1024];
     50 	unsigned char		*p;
     51 
     52 	va_start (argptr,fmt);
     53 	vsprintf (text,fmt,argptr);
     54 	va_end (argptr);
     55 
     56 	if (strlen(text) > sizeof(text))
     57 		Sys_Error("memory overwrite in Sys_Printf");
     58 
     59     if (nostdout && nostdout->value)
     60         return;
     61 
     62 	for (p = (unsigned char *)text; *p; p++) {
     63 		*p &= 0x7f;
     64 		if ((*p > 128 || *p < 32) && *p != 10 && *p != 13 && *p != 9)
     65 			printf("[%02x]", *p);
     66 		else
     67 			putc(*p, stdout);
     68 	}
     69 }
     70 
     71 void Sys_Quit (void)
     72 {
     73 	CL_Shutdown ();
     74 	Qcommon_Shutdown ();
     75     fcntl (0, F_SETFL, fcntl (0, F_GETFL, 0) & ~FNDELAY);
     76 	_exit(0);
     77 }
     78 
     79 void Sys_Init(void)
     80 {
     81 #if id386
     82 //	Sys_SetFPCW();
     83 #endif
     84 }
     85 
     86 void Sys_Error (char *error, ...)
     87 { 
     88     va_list     argptr;
     89     char        string[1024];
     90 
     91 // change stdin to non blocking
     92     fcntl (0, F_SETFL, fcntl (0, F_GETFL, 0) & ~FNDELAY);
     93     
     94     va_start (argptr,error);
     95     vsprintf (string,error,argptr);
     96     va_end (argptr);
     97 	fprintf(stderr, "Error: %s\n", string);
     98 
     99 	CL_Shutdown ();
    100 	Qcommon_Shutdown ();
    101 	_exit (1);
    102 
    103 } 
    104 
    105 void Sys_Warn (char *warning, ...)
    106 { 
    107     va_list     argptr;
    108     char        string[1024];
    109     
    110     va_start (argptr,warning);
    111     vsprintf (string,warning,argptr);
    112     va_end (argptr);
    113 	fprintf(stderr, "Warning: %s", string);
    114 } 
    115 
    116 /*
    117 ============
    118 Sys_FileTime
    119 
    120 returns -1 if not present
    121 ============
    122 */
    123 int	Sys_FileTime (char *path)
    124 {
    125 	struct	stat	buf;
    126 	
    127 	if (stat (path,&buf) == -1)
    128 		return -1;
    129 	
    130 	return buf.st_mtime;
    131 }
    132 
    133 void floating_point_exception_handler(int whatever)
    134 {
    135 //	Sys_Warn("floating point exception\n");
    136 	signal(SIGFPE, floating_point_exception_handler);
    137 }
    138 
    139 char *Sys_ConsoleInput(void)
    140 {
    141     static char text[256];
    142     int     len;
    143 	fd_set	fdset;
    144     struct timeval timeout;
    145 
    146 	if (!dedicated || !dedicated->value)
    147 		return NULL;
    148 
    149 	if (!stdin_active)
    150 		return NULL;
    151 
    152 	FD_ZERO(&fdset);
    153 	FD_SET(0, &fdset); // stdin
    154 	timeout.tv_sec = 0;
    155 	timeout.tv_usec = 0;
    156 	if (select (1, &fdset, NULL, NULL, &timeout) == -1 || !FD_ISSET(0, &fdset))
    157 		return NULL;
    158 
    159 	len = read (0, text, sizeof(text));
    160 	if (len == 0) { // eof!
    161 		stdin_active = false;
    162 		return NULL;
    163 	}
    164 
    165 	if (len < 1)
    166 		return NULL;
    167 	text[len-1] = 0;    // rip off the /n and terminate
    168 
    169 	return text;
    170 }
    171 
    172 /*****************************************************************************/
    173 
    174 static void *game_library;
    175 
    176 /*
    177 =================
    178 Sys_UnloadGame
    179 =================
    180 */
    181 void Sys_UnloadGame (void)
    182 {
    183 	if (game_library) 
    184 		dlclose (game_library);
    185 	game_library = NULL;
    186 }
    187 
    188 /*
    189 =================
    190 Sys_GetGameAPI
    191 
    192 Loads the game dll
    193 =================
    194 */
    195 void *Sys_GetGameAPI (void *parms)
    196 {
    197 #ifndef REF_HARD_LINKED
    198 	void	*(*GetGameAPI) (void *);
    199 
    200 	char	name[MAX_OSPATH];
    201 	char	curpath[MAX_OSPATH];
    202 	char	*path;
    203 #ifdef __sgi
    204 	const char *gamename = "gamemips.so";
    205 #else
    206 #error Unknown arch
    207 #endif
    208 
    209 	setreuid(getuid(), getuid());
    210 	setegid(getgid());
    211 
    212 	if (game_library)
    213 		Com_Error (ERR_FATAL, "Sys_GetGameAPI without Sys_UnloadingGame");
    214 
    215 	getcwd(curpath, sizeof(curpath));
    216 
    217 	Com_Printf("------- Loading %s -------", gamename);
    218 
    219 	// now run through the search paths
    220 	path = NULL;
    221 	while (1)
    222 	{
    223 		path = FS_NextPath (path);
    224 		if (!path)
    225 			return NULL;		// couldn't find one anywhere
    226 		sprintf (name, "%s/%s/%s", curpath, path, gamename);
    227 		Com_Printf ("Trying to load library (%s)\n",name);
    228 		game_library = dlopen (name, RTLD_NOW );
    229 		if (game_library)
    230 		{
    231 			Com_DPrintf ("LoadLibrary (%s)\n",name);
    232 			break;
    233 		}
    234 	}
    235 
    236 	GetGameAPI = (void *)dlsym (game_library, "GetGameAPI");
    237 	if (!GetGameAPI)
    238 	{
    239 		Sys_UnloadGame ();		
    240 		return NULL;
    241 	}
    242 
    243 	return GetGameAPI (parms);
    244 #else
    245 	return (void *)GetGameAPI (parms);
    246 #endif
    247 }
    248 
    249 /*****************************************************************************/
    250 
    251 void Sys_AppActivate (void)
    252 {
    253 }
    254 
    255 void Sys_SendKeyEvents (void)
    256 {
    257 	if (KBD_Update_fp)
    258 		KBD_Update_fp();
    259 
    260 	// grab frame time 
    261 	sys_frame_time = Sys_Milliseconds();
    262 }
    263 
    264 /*****************************************************************************/
    265 
    266 char *Sys_GetClipboardData(void)
    267 {
    268 	return NULL;
    269 }
    270 
    271 int main (int argc, char **argv)
    272 {
    273 	int 	time, oldtime, newtime;
    274 
    275 	// go back to real user for config loads
    276 	saved_euid = geteuid();
    277 	seteuid(getuid());
    278 
    279 	Qcommon_Init(argc, argv);
    280 
    281 /* 	fcntl(0, F_SETFL, fcntl (0, F_GETFL, 0) | FNDELAY); */
    282 
    283 	nostdout = Cvar_Get("nostdout", "0", 0);
    284 	if (!nostdout->value) {
    285 /* 		fcntl(0, F_SETFL, fcntl (0, F_GETFL, 0) | FNDELAY); */
    286 //		printf ("Linux Quake -- Version %0.3f\n", LINUX_VERSION);
    287 	}
    288 
    289     oldtime = Sys_Milliseconds ();
    290     while (1)
    291     {
    292 // find time spent rendering last frame
    293 		do {
    294 			newtime = Sys_Milliseconds ();
    295 			time = newtime - oldtime;
    296 		} while (time < 1);
    297 		Qcommon_Frame (time);
    298 		oldtime = newtime;
    299     }
    300 
    301 }
    302 
    303 void Sys_CopyProtect(void)
    304 {
    305 	FILE *mnt;
    306 	struct mntent *ent;
    307 	char path[MAX_OSPATH];
    308 	struct stat st;
    309 	qboolean found_cd = false;
    310 
    311 	static qboolean checked = false;
    312 
    313 	if (checked)
    314 		return;
    315 
    316         Com_Printf("XXX - Sys_CopyProtect disabled\n");
    317 	checked = true;
    318 	return;
    319 
    320 	if ((mnt = setmntent("/etc/mtab", "r")) == NULL)
    321 		Com_Error(ERR_FATAL, "Can't read mount table to determine mounted cd location.");
    322 
    323 	while ((ent = getmntent(mnt)) != NULL) {
    324 		if (strcmp(ent->mnt_type, "iso9660") == 0) {
    325 			// found a cd file system
    326 			found_cd = true;
    327 			sprintf(path, "%s/%s", ent->mnt_dir, "install/data/quake2.exe");
    328 			if (stat(path, &st) == 0) {
    329 				// found it
    330 				checked = true;
    331 				endmntent(mnt);
    332 				return;
    333 			}
    334 			sprintf(path, "%s/%s", ent->mnt_dir, "Install/Data/quake2.exe");
    335 			if (stat(path, &st) == 0) {
    336 				// found it
    337 				checked = true;
    338 				endmntent(mnt);
    339 				return;
    340 			}
    341 			sprintf(path, "%s/%s", ent->mnt_dir, "quake2.exe");
    342 			if (stat(path, &st) == 0) {
    343 				// found it
    344 				checked = true;
    345 				endmntent(mnt);
    346 				return;
    347 			}
    348 		}
    349 	}
    350 	endmntent(mnt);
    351 
    352 	if (found_cd)
    353 		Com_Error (ERR_FATAL, "Could not find a Quake2 CD in your CD drive.");
    354 	Com_Error (ERR_FATAL, "Unable to find a mounted iso9660 file system.\n"
    355 		"You must mount the Quake2 CD in a cdrom drive in order to play.");
    356 }
    357 
    358 #if 0
    359 /*
    360 ================
    361 Sys_MakeCodeWriteable
    362 ================
    363 */
    364 void Sys_MakeCodeWriteable (unsigned long startaddr, unsigned long length)
    365 {
    366 
    367 	int r;
    368 	unsigned long addr;
    369 	int psize = getpagesize();
    370 
    371 	addr = (startaddr & ~(psize-1)) - psize;
    372 
    373 //	fprintf(stderr, "writable code %lx(%lx)-%lx, length=%lx\n", startaddr,
    374 //			addr, startaddr+length, length);
    375 
    376 	r = mprotect((char*)addr, length + startaddr - addr + psize, 7);
    377 
    378 	if (r < 0)
    379     		Sys_Error("Protection change failed\n");
    380 
    381 }
    382 
    383 #endif