1999-12-31 00:37:47 +00:00
|
|
|
/*
|
|
|
|
Copyright (C) 1996-1997 Id Software, Inc.
|
|
|
|
|
|
|
|
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
|
2000-03-19 15:59:51 +00:00
|
|
|
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
|
1999-12-31 00:37:47 +00:00
|
|
|
|
|
|
|
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.
|
|
|
|
|
|
|
|
*/
|
2000-01-05 21:22:48 +00:00
|
|
|
// sys_unix.c -- Unix system driver
|
1999-12-31 00:37:47 +00:00
|
|
|
|
|
|
|
#include "quakedef.h"
|
2000-01-29 23:24:16 +00:00
|
|
|
#include "sys.h"
|
2000-02-05 16:02:39 +00:00
|
|
|
#include "qargs.h"
|
|
|
|
|
2000-01-05 21:22:48 +00:00
|
|
|
#include <stdio.h>
|
|
|
|
#include <errno.h>
|
|
|
|
#include <fcntl.h>
|
|
|
|
#include <stddef.h>
|
|
|
|
#include <signal.h>
|
1999-12-31 00:37:47 +00:00
|
|
|
#include <sys/types.h>
|
|
|
|
#include <sys/time.h>
|
|
|
|
#include <sys/stat.h>
|
|
|
|
#include <sys/mman.h>
|
|
|
|
#include <sys/param.h>
|
2000-01-05 21:22:48 +00:00
|
|
|
|
|
|
|
#ifndef MAP_FAILED
|
|
|
|
# define MAP_FAILED ((void*)-1)
|
|
|
|
#endif
|
1999-12-31 00:37:47 +00:00
|
|
|
|
2000-02-03 08:42:14 +00:00
|
|
|
#ifdef HAVE_UNISTD_H
|
|
|
|
#include <unistd.h>
|
|
|
|
#endif
|
|
|
|
|
|
|
|
#ifdef HAVE_STRINGS_H
|
|
|
|
#include <strings.h>
|
|
|
|
#endif
|
|
|
|
|
1999-12-31 00:37:47 +00:00
|
|
|
qboolean isDedicated;
|
|
|
|
|
|
|
|
/*
|
|
|
|
===============================================================================
|
|
|
|
|
|
|
|
FILE IO
|
|
|
|
|
|
|
|
===============================================================================
|
|
|
|
*/
|
|
|
|
|
|
|
|
#define MAX_HANDLES 10
|
|
|
|
|
2000-03-19 15:59:51 +00:00
|
|
|
typedef struct
|
1999-12-31 00:37:47 +00:00
|
|
|
{
|
2000-02-08 07:23:09 +00:00
|
|
|
QFile *hFile;
|
1999-12-31 00:37:47 +00:00
|
|
|
char *pMap;
|
|
|
|
int nLen;
|
|
|
|
int nPos;
|
|
|
|
} MEMFILE;
|
|
|
|
|
|
|
|
MEMFILE sys_handles[MAX_HANDLES];
|
|
|
|
|
|
|
|
int findhandle (void)
|
|
|
|
{
|
|
|
|
int i;
|
2000-03-19 15:59:51 +00:00
|
|
|
|
1999-12-31 00:37:47 +00:00
|
|
|
for (i=1 ; i<MAX_HANDLES ; i++)
|
|
|
|
if (!sys_handles[i].hFile)
|
|
|
|
return i;
|
|
|
|
Sys_Error ("out of handles");
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
================
|
|
|
|
filelength
|
|
|
|
================
|
|
|
|
*/
|
2000-02-08 07:23:09 +00:00
|
|
|
int filelength (QFile *f)
|
1999-12-31 00:37:47 +00:00
|
|
|
{
|
|
|
|
int pos;
|
|
|
|
int end;
|
|
|
|
|
2000-02-10 06:18:57 +00:00
|
|
|
pos = Qtell (f);
|
|
|
|
Qseek (f, 0, SEEK_END);
|
|
|
|
end = Qtell (f);
|
|
|
|
Qseek (f, pos, SEEK_SET);
|
1999-12-31 00:37:47 +00:00
|
|
|
|
|
|
|
return end;
|
|
|
|
}
|
|
|
|
|
2000-02-10 06:18:57 +00:00
|
|
|
/* from sys_linux.c */
|
|
|
|
int Sys_FileOpenRead (char *path, int *handle)
|
1999-12-31 00:37:47 +00:00
|
|
|
{
|
2000-02-10 06:18:57 +00:00
|
|
|
int h;
|
|
|
|
struct stat fileinfo;
|
1999-12-31 00:37:47 +00:00
|
|
|
|
2000-02-10 06:18:57 +00:00
|
|
|
h = open (path, O_RDONLY, 0666);
|
|
|
|
*handle = h;
|
|
|
|
if (h == -1)
|
|
|
|
return -1;
|
|
|
|
|
|
|
|
if (fstat (h,&fileinfo) == -1)
|
|
|
|
Sys_Error ("Error fstating %s", path);
|
|
|
|
|
|
|
|
return fileinfo.st_size;
|
1999-12-31 00:37:47 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
int Sys_FileOpenWrite (char *path)
|
|
|
|
{
|
2000-02-08 07:23:09 +00:00
|
|
|
QFile *f;
|
1999-12-31 00:37:47 +00:00
|
|
|
int i;
|
2000-03-19 15:59:51 +00:00
|
|
|
|
1999-12-31 00:37:47 +00:00
|
|
|
i = findhandle ();
|
|
|
|
|
2000-02-10 06:18:57 +00:00
|
|
|
f = Qopen(path, "wb");
|
1999-12-31 00:37:47 +00:00
|
|
|
if (!f)
|
|
|
|
Sys_Error ("Error opening %s: %s", path,strerror(errno));
|
|
|
|
sys_handles[i].hFile = f;
|
|
|
|
sys_handles[i].nLen = 0;
|
|
|
|
sys_handles[i].nPos = 0;
|
|
|
|
sys_handles[i].pMap = NULL;
|
2000-03-19 15:59:51 +00:00
|
|
|
|
1999-12-31 00:37:47 +00:00
|
|
|
return i;
|
|
|
|
}
|
|
|
|
|
|
|
|
void Sys_FileClose (int handle)
|
|
|
|
{
|
|
|
|
if (sys_handles[handle].pMap)
|
|
|
|
if (munmap( sys_handles[handle].pMap, sys_handles[handle].nLen ) != 0)
|
|
|
|
printf( "failed to unmap handle %d\n", handle );
|
|
|
|
|
2000-02-10 06:18:57 +00:00
|
|
|
Qclose (sys_handles[handle].hFile);
|
1999-12-31 00:37:47 +00:00
|
|
|
sys_handles[handle].hFile = NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
void Sys_FileSeek (int handle, int position)
|
|
|
|
{
|
|
|
|
if (sys_handles[handle].pMap)
|
|
|
|
{
|
|
|
|
sys_handles[handle].nPos = position;
|
|
|
|
}
|
2000-02-10 06:18:57 +00:00
|
|
|
else Qseek (sys_handles[handle].hFile, position, SEEK_SET);
|
1999-12-31 00:37:47 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
int Sys_FileRead (int handle, void *dest, int count)
|
|
|
|
{
|
|
|
|
if (sys_handles[handle].pMap)
|
|
|
|
{
|
|
|
|
int nPos = sys_handles[handle].nPos;
|
|
|
|
if (count < 0) count = 0;
|
|
|
|
if (nPos + count > sys_handles[handle].nLen)
|
|
|
|
count = sys_handles[handle].nLen - nPos;
|
|
|
|
memcpy( dest, &sys_handles[handle].pMap[nPos], count );
|
|
|
|
sys_handles[handle].nPos = nPos + count;
|
|
|
|
return( count );
|
|
|
|
}
|
2000-02-10 06:18:57 +00:00
|
|
|
else return Qread (sys_handles[handle].hFile, dest, count);
|
1999-12-31 00:37:47 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
int Sys_FileWrite (int handle, void *data, int count)
|
|
|
|
{
|
|
|
|
if (sys_handles[handle].pMap)
|
|
|
|
Sys_Error( "Attempted to write to read-only file %d!\n", handle );
|
2000-02-10 06:18:57 +00:00
|
|
|
return Qwrite (sys_handles[handle].hFile, data, count);
|
1999-12-31 00:37:47 +00:00
|
|
|
}
|
|
|
|
|
2000-01-29 23:24:16 +00:00
|
|
|
void Sys_DebugLog(char *file, char *fmt, ...) {
|
|
|
|
|
2000-03-19 15:59:51 +00:00
|
|
|
va_list argptr;
|
2000-01-29 23:24:16 +00:00
|
|
|
static char data[1024];
|
2000-02-08 07:23:09 +00:00
|
|
|
QFile *stream;
|
2000-01-29 23:24:16 +00:00
|
|
|
unsigned char *p;
|
|
|
|
//int fd;
|
2000-03-19 15:59:51 +00:00
|
|
|
|
2000-01-29 23:24:16 +00:00
|
|
|
va_start(argptr, fmt);
|
|
|
|
vsnprintf(data, sizeof(data), fmt, argptr);
|
|
|
|
va_end(argptr);
|
|
|
|
// fd = open(file, O_WRONLY | O_BINARY | O_CREAT | O_APPEND, 0666);
|
2000-02-10 06:18:57 +00:00
|
|
|
stream = Qopen(file, "a");
|
2000-01-29 23:24:16 +00:00
|
|
|
for (p = (unsigned char *) data; *p; p++) {
|
2000-02-10 06:18:57 +00:00
|
|
|
Qputc(stream, trans_table[*p]);
|
2000-01-29 23:24:16 +00:00
|
|
|
}
|
2000-02-10 06:18:57 +00:00
|
|
|
Qclose(stream);
|
2000-01-29 23:24:16 +00:00
|
|
|
/*
|
|
|
|
fd = open(file, O_WRONLY | O_CREAT | O_APPEND, 0666);
|
|
|
|
write(fd, data, strlen(data));
|
|
|
|
close(fd);
|
|
|
|
*/
|
|
|
|
}
|
|
|
|
|
|
|
|
|
1999-12-31 00:37:47 +00:00
|
|
|
/*
|
|
|
|
===============================================================================
|
|
|
|
|
|
|
|
SYSTEM IO
|
|
|
|
|
|
|
|
===============================================================================
|
|
|
|
*/
|
|
|
|
|
|
|
|
void Sys_MakeCodeWriteable (unsigned long startaddr, unsigned long length)
|
|
|
|
{
|
|
|
|
|
|
|
|
int r;
|
|
|
|
unsigned long addr;
|
|
|
|
int psize = getpagesize();
|
|
|
|
|
|
|
|
addr = (startaddr & ~(psize-1)) - psize;
|
|
|
|
|
|
|
|
// fprintf(stderr, "writable code %lx(%lx)-%lx, length=%lx\n", startaddr,
|
|
|
|
// addr, startaddr+length, length);
|
|
|
|
|
|
|
|
r = mprotect((char*)addr, length + startaddr - addr + psize, 7);
|
|
|
|
|
|
|
|
if (r < 0)
|
|
|
|
Sys_Error("Protection change failed\n");
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
void Sys_Error (char *error, ...)
|
|
|
|
{
|
|
|
|
va_list argptr;
|
|
|
|
|
2000-03-19 15:59:51 +00:00
|
|
|
printf ("Sys_Error: ");
|
1999-12-31 00:37:47 +00:00
|
|
|
va_start (argptr,error);
|
|
|
|
vprintf (error,argptr);
|
|
|
|
va_end (argptr);
|
|
|
|
printf ("\n");
|
|
|
|
Host_Shutdown();
|
|
|
|
exit (1);
|
|
|
|
}
|
|
|
|
|
|
|
|
void Sys_Quit (void)
|
|
|
|
{
|
|
|
|
Host_Shutdown();
|
|
|
|
exit (0);
|
|
|
|
}
|
|
|
|
|
|
|
|
char *Sys_ConsoleInput (void)
|
|
|
|
{
|
|
|
|
static char text[256];
|
|
|
|
int len;
|
|
|
|
fd_set readfds;
|
|
|
|
int ready;
|
|
|
|
struct timeval timeout;
|
2000-03-19 15:59:51 +00:00
|
|
|
|
1999-12-31 00:37:47 +00:00
|
|
|
timeout.tv_sec = 0;
|
|
|
|
timeout.tv_usec = 0;
|
|
|
|
FD_ZERO(&readfds);
|
|
|
|
FD_SET(0, &readfds);
|
|
|
|
ready = select(1, &readfds, 0, 0, &timeout);
|
2000-03-19 15:59:51 +00:00
|
|
|
|
1999-12-31 00:37:47 +00:00
|
|
|
if (ready>0)
|
|
|
|
{
|
|
|
|
len = read (0, text, sizeof(text));
|
|
|
|
if (len >= 1)
|
|
|
|
{
|
|
|
|
text[len-1] = 0; // rip off the /n and terminate
|
|
|
|
return text;
|
|
|
|
}
|
|
|
|
}
|
2000-03-19 15:59:51 +00:00
|
|
|
|
1999-12-31 00:37:47 +00:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
void Sys_Sleep (void)
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
#if !id386
|
|
|
|
void Sys_HighFPPrecision (void)
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
void Sys_LowFPPrecision (void)
|
|
|
|
{
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
|
|
|
void Sys_Init(void)
|
|
|
|
{
|
|
|
|
#if id386
|
|
|
|
Sys_SetFPCW();
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
|
|
|
//=============================================================================
|
|
|
|
|
|
|
|
int main (int argc, char **argv)
|
|
|
|
{
|
|
|
|
static quakeparms_t parms;
|
2000-02-03 08:42:14 +00:00
|
|
|
float oldtime, newtime;
|
2000-03-19 15:59:51 +00:00
|
|
|
|
1999-12-31 00:37:47 +00:00
|
|
|
parms.memsize = 16*1024*1024;
|
|
|
|
parms.membase = malloc (parms.memsize);
|
|
|
|
parms.basedir = ".";
|
|
|
|
parms.cachedir = NULL;
|
|
|
|
|
2000-01-03 08:00:46 +00:00
|
|
|
signal(SIGFPE, SIG_IGN);
|
|
|
|
|
1999-12-31 00:37:47 +00:00
|
|
|
COM_InitArgv (argc, argv);
|
|
|
|
|
|
|
|
parms.argc = com_argc;
|
|
|
|
parms.argv = com_argv;
|
|
|
|
|
|
|
|
printf ("Host_Init\n");
|
|
|
|
Host_Init (&parms);
|
|
|
|
|
|
|
|
Sys_Init();
|
|
|
|
|
|
|
|
// unroll the simulation loop to give the video side a chance to see _vid_default_mode
|
|
|
|
Host_Frame( 0.1 );
|
|
|
|
// FIXME - probably should be deleted - this is just an empty function
|
|
|
|
// VID_SetDefaultMode();
|
|
|
|
|
|
|
|
oldtime = Sys_DoubleTime();
|
|
|
|
while (1)
|
|
|
|
{
|
|
|
|
newtime = Sys_DoubleTime();
|
|
|
|
Host_Frame (newtime - oldtime);
|
|
|
|
oldtime = newtime;
|
|
|
|
}
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|