2001-02-19 21:15:25 +00:00
|
|
|
/*
|
|
|
|
sys_unix.c
|
|
|
|
|
|
|
|
(description)
|
|
|
|
|
|
|
|
Copyright (C) 1996-1997 Id Software, Inc.
|
|
|
|
Copyright (C) 2000 Marcus Sundberg [mackan@stacken.kth.se]
|
|
|
|
|
|
|
|
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:
|
|
|
|
|
|
|
|
Free Software Foundation, Inc.
|
|
|
|
59 Temple Place - Suite 330
|
|
|
|
Boston, MA 02111-1307, USA
|
|
|
|
|
|
|
|
$Id$
|
|
|
|
*/
|
|
|
|
|
|
|
|
#ifdef HAVE_CONFIG_H
|
|
|
|
# include "config.h"
|
|
|
|
#endif
|
2001-05-09 05:41:34 +00:00
|
|
|
#ifdef HAVE_STRING_H
|
|
|
|
# include <string.h>
|
|
|
|
#endif
|
|
|
|
#ifdef HAVE_STRINGS_H
|
|
|
|
# include <strings.h>
|
|
|
|
#endif
|
2001-02-19 21:15:25 +00:00
|
|
|
|
|
|
|
#include <stdio.h>
|
|
|
|
#include <stdlib.h>
|
|
|
|
#include <errno.h>
|
|
|
|
#include <unistd.h>
|
|
|
|
#include <sys/stat.h>
|
|
|
|
#include <sys/time.h>
|
|
|
|
#include <fcntl.h>
|
|
|
|
#include <sys/mman.h>
|
|
|
|
#include <signal.h>
|
|
|
|
#include <time.h>
|
|
|
|
|
2001-03-27 20:33:07 +00:00
|
|
|
#include "QF/sys.h"
|
|
|
|
#include "QF/qargs.h"
|
|
|
|
#include "QF/cvar.h"
|
2001-02-19 21:15:25 +00:00
|
|
|
#include "server.h"
|
|
|
|
#include "host.h"
|
|
|
|
|
|
|
|
|
|
|
|
|
2001-02-26 06:48:02 +00:00
|
|
|
qboolean isDedicated;
|
2001-02-19 21:15:25 +00:00
|
|
|
|
2001-02-26 06:48:02 +00:00
|
|
|
char *basedir = ".";
|
|
|
|
char *cachedir = "/tmp";
|
2001-02-19 21:15:25 +00:00
|
|
|
|
2001-02-26 06:48:02 +00:00
|
|
|
cvar_t *sys_linerefresh;
|
|
|
|
cvar_t *timestamps;
|
|
|
|
cvar_t *timeformat;
|
2001-02-19 21:15:25 +00:00
|
|
|
|
|
|
|
void
|
2001-02-26 06:48:02 +00:00
|
|
|
Sys_DebugLog (char *file, char *fmt, ...)
|
2001-02-19 21:15:25 +00:00
|
|
|
{
|
2001-02-26 06:48:02 +00:00
|
|
|
va_list argptr;
|
|
|
|
static char data[1024];
|
|
|
|
int fd;
|
|
|
|
|
|
|
|
va_start (argptr, fmt);
|
|
|
|
vsnprintf (data, sizeof (data), fmt, argptr);
|
|
|
|
va_end (argptr);
|
|
|
|
|
|
|
|
fd = open (file, O_WRONLY | O_CREAT | O_APPEND, 0666);
|
|
|
|
write (fd, data, strlen (data));
|
|
|
|
close (fd);
|
2001-02-19 21:15:25 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
Sys_EditFile (char *filename)
|
|
|
|
{
|
2001-02-26 06:48:02 +00:00
|
|
|
char cmd[256];
|
|
|
|
char *term;
|
|
|
|
char *editor;
|
2001-02-19 21:15:25 +00:00
|
|
|
|
|
|
|
term = getenv ("TERM");
|
2001-02-26 06:48:02 +00:00
|
|
|
if (term && !strcmp (term, "xterm")) {
|
2001-02-19 21:15:25 +00:00
|
|
|
editor = getenv ("VISUAL");
|
|
|
|
if (!editor)
|
|
|
|
editor = getenv ("EDITOR");
|
|
|
|
if (!editor)
|
|
|
|
editor = getenv ("EDIT");
|
|
|
|
if (!editor)
|
|
|
|
editor = "vi";
|
2001-02-26 06:48:02 +00:00
|
|
|
snprintf (cmd, sizeof (cmd), "xterm -e %s %s", editor, filename);
|
2001-02-19 21:15:25 +00:00
|
|
|
system (cmd);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* System I/O
|
|
|
|
*/
|
|
|
|
|
|
|
|
void
|
|
|
|
Sys_MakeCodeWriteable (unsigned long startaddr, unsigned long length)
|
|
|
|
{
|
2001-02-26 06:48:02 +00:00
|
|
|
int r;
|
|
|
|
unsigned long addr;
|
|
|
|
int psize = getpagesize ();
|
2001-02-19 21:15:25 +00:00
|
|
|
|
2001-02-26 06:48:02 +00:00
|
|
|
addr = (startaddr & ~(psize - 1)) - psize;
|
2001-02-19 21:15:25 +00:00
|
|
|
|
2001-02-26 06:48:02 +00:00
|
|
|
// fprintf(stderr, "writable code %lx(%lx)-%lx, length=%lx\n", startaddr,
|
|
|
|
// addr, startaddr+length, length);
|
2001-02-19 21:15:25 +00:00
|
|
|
|
|
|
|
r = mprotect ((char *) addr, length + startaddr - addr + psize, 7);
|
|
|
|
|
|
|
|
if (r < 0)
|
2001-02-26 06:48:02 +00:00
|
|
|
Sys_Error ("Protection change failed\n");
|
2001-02-19 21:15:25 +00:00
|
|
|
|
|
|
|
}
|
|
|
|
|
2001-02-26 06:48:02 +00:00
|
|
|
void
|
|
|
|
Sys_DebugNumber (int y, int val)
|
2001-02-19 21:15:25 +00:00
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
2001-03-30 23:24:57 +00:00
|
|
|
Sys_Error (const char *error, ...)
|
2001-02-26 06:48:02 +00:00
|
|
|
{
|
|
|
|
va_list argptr;
|
|
|
|
char string[1024];
|
2001-02-19 21:15:25 +00:00
|
|
|
|
|
|
|
// change stdin to non blocking
|
2001-02-26 06:48:02 +00:00
|
|
|
fcntl (0, F_SETFL, fcntl (0, F_GETFL, 0) & ~FNDELAY);
|
|
|
|
|
|
|
|
va_start (argptr, error);
|
|
|
|
vsnprintf (string, sizeof (string), error, argptr);
|
|
|
|
va_end (argptr);
|
|
|
|
fprintf (stderr, "Error: %s\n", string);
|
2001-02-19 21:15:25 +00:00
|
|
|
|
|
|
|
Host_Shutdown ();
|
|
|
|
exit (1);
|
|
|
|
|
2001-02-26 06:48:02 +00:00
|
|
|
}
|
2001-02-19 21:15:25 +00:00
|
|
|
|
|
|
|
void
|
|
|
|
Sys_Quit (void)
|
|
|
|
{
|
|
|
|
Host_Shutdown ();
|
2001-02-26 06:48:02 +00:00
|
|
|
fcntl (0, F_SETFL, fcntl (0, F_GETFL, 0) & ~FNDELAY);
|
|
|
|
fflush (stdout);
|
2001-02-19 21:15:25 +00:00
|
|
|
exit (0);
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
Sys_Init (void)
|
|
|
|
{
|
|
|
|
#ifdef USE_INTEL_ASM
|
2001-02-26 06:48:02 +00:00
|
|
|
Sys_SetFPCW ();
|
2001-02-19 21:15:25 +00:00
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
Sys_Warn (char *warning, ...)
|
2001-02-26 06:48:02 +00:00
|
|
|
{
|
|
|
|
va_list argptr;
|
|
|
|
char string[1024];
|
2001-02-19 21:15:25 +00:00
|
|
|
|
|
|
|
va_start (argptr, warning);
|
2001-02-26 06:48:02 +00:00
|
|
|
vsnprintf (string, sizeof (string), warning, argptr);
|
2001-02-19 21:15:25 +00:00
|
|
|
va_end (argptr);
|
|
|
|
fprintf (stderr, "Warning: %s", string);
|
2001-02-26 06:48:02 +00:00
|
|
|
}
|
2001-02-19 21:15:25 +00:00
|
|
|
|
|
|
|
// =======================================================================
|
|
|
|
// Sleeps for microseconds
|
|
|
|
// =======================================================================
|
|
|
|
|
|
|
|
static volatile int oktogo;
|
|
|
|
|
2001-02-26 06:48:02 +00:00
|
|
|
void
|
|
|
|
alarm_handler (int x)
|
2001-02-19 21:15:25 +00:00
|
|
|
{
|
2001-02-26 06:48:02 +00:00
|
|
|
oktogo = 1;
|
2001-02-19 21:15:25 +00:00
|
|
|
}
|
|
|
|
|
2001-02-26 06:48:02 +00:00
|
|
|
void
|
|
|
|
Sys_LineRefresh (void)
|
2001-02-19 21:15:25 +00:00
|
|
|
{
|
|
|
|
}
|
|
|
|
|
2001-02-26 06:48:02 +00:00
|
|
|
void
|
|
|
|
floating_point_exception_handler (int whatever)
|
2001-02-19 21:15:25 +00:00
|
|
|
{
|
2001-02-26 06:48:02 +00:00
|
|
|
// Sys_Warn("floating point exception\n");
|
|
|
|
signal (SIGFPE, floating_point_exception_handler);
|
2001-02-19 21:15:25 +00:00
|
|
|
}
|
|
|
|
|
2001-02-26 06:48:02 +00:00
|
|
|
char *
|
|
|
|
Sys_ConsoleInput (void)
|
2001-02-19 21:15:25 +00:00
|
|
|
{
|
2001-02-26 06:48:02 +00:00
|
|
|
static char text[256];
|
|
|
|
int len;
|
|
|
|
fd_set fdset;
|
|
|
|
struct timeval timeout;
|
2001-02-19 21:15:25 +00:00
|
|
|
|
|
|
|
if (cls.state == ca_dedicated) {
|
2001-02-26 06:48:02 +00:00
|
|
|
FD_ZERO (&fdset);
|
|
|
|
FD_SET (0, &fdset); // stdin
|
2001-02-19 21:15:25 +00:00
|
|
|
timeout.tv_sec = 0;
|
|
|
|
timeout.tv_usec = 0;
|
2001-02-26 06:48:02 +00:00
|
|
|
if (select (1, &fdset, NULL, NULL, &timeout) == -1
|
|
|
|
|| !FD_ISSET (0, &fdset)) return NULL;
|
2001-02-19 21:15:25 +00:00
|
|
|
|
2001-02-26 06:48:02 +00:00
|
|
|
len = read (0, text, sizeof (text));
|
2001-02-19 21:15:25 +00:00
|
|
|
if (len < 1)
|
|
|
|
return NULL;
|
2001-02-26 06:48:02 +00:00
|
|
|
text[len - 1] = 0; // rip off the /n and terminate
|
2001-02-19 21:15:25 +00:00
|
|
|
|
|
|
|
return text;
|
|
|
|
}
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
#ifndef USE_INTEL_ASM
|
2001-02-26 06:48:02 +00:00
|
|
|
void
|
|
|
|
Sys_HighFPPrecision (void)
|
2001-02-19 21:15:25 +00:00
|
|
|
{
|
|
|
|
}
|
|
|
|
|
2001-02-26 06:48:02 +00:00
|
|
|
void
|
|
|
|
Sys_LowFPPrecision (void)
|
2001-02-19 21:15:25 +00:00
|
|
|
{
|
|
|
|
}
|
2001-02-26 06:48:02 +00:00
|
|
|
|
2001-02-19 21:15:25 +00:00
|
|
|
#endif
|
|
|
|
|
2001-02-26 06:48:02 +00:00
|
|
|
int
|
|
|
|
main (int c, char **v)
|
2001-02-19 21:15:25 +00:00
|
|
|
{
|
|
|
|
|
2001-02-26 06:48:02 +00:00
|
|
|
double time, oldtime, newtime;
|
2001-02-19 21:15:25 +00:00
|
|
|
quakeparms_t parms;
|
2001-02-26 06:48:02 +00:00
|
|
|
extern int vcrFile;
|
|
|
|
extern int recording;
|
|
|
|
int j;
|
2001-02-19 21:15:25 +00:00
|
|
|
|
2001-02-26 06:48:02 +00:00
|
|
|
// static char cwd[1024];
|
2001-02-19 21:15:25 +00:00
|
|
|
|
2001-02-26 06:48:02 +00:00
|
|
|
// signal(SIGFPE, floating_point_exception_handler);
|
|
|
|
signal (SIGFPE, SIG_IGN);
|
2001-02-19 21:15:25 +00:00
|
|
|
|
2001-02-26 06:48:02 +00:00
|
|
|
memset (&parms, 0, sizeof (parms));
|
2001-02-19 21:15:25 +00:00
|
|
|
|
2001-02-26 06:48:02 +00:00
|
|
|
COM_InitArgv (c, v);
|
2001-02-19 21:15:25 +00:00
|
|
|
parms.argc = com_argc;
|
|
|
|
parms.argv = com_argv;
|
|
|
|
|
2001-02-26 06:48:02 +00:00
|
|
|
parms.memsize = 16 * 1024 * 1024;
|
2001-02-19 21:15:25 +00:00
|
|
|
|
2001-02-26 06:48:02 +00:00
|
|
|
j = COM_CheckParm ("-mem");
|
2001-02-19 21:15:25 +00:00
|
|
|
if (j)
|
2001-02-26 06:48:02 +00:00
|
|
|
parms.memsize = (int) (atof (com_argv[j + 1]) * 1024 * 1024);
|
2001-02-19 21:15:25 +00:00
|
|
|
parms.membase = malloc (parms.memsize);
|
|
|
|
|
|
|
|
parms.basedir = basedir;
|
|
|
|
// caching is disabled by default, use -cachedir to enable
|
2001-02-26 06:48:02 +00:00
|
|
|
// parms.cachedir = cachedir;
|
2001-02-19 21:15:25 +00:00
|
|
|
|
2001-02-26 06:48:02 +00:00
|
|
|
fcntl (0, F_SETFL, fcntl (0, F_GETFL, 0) | FNDELAY);
|
2001-02-19 21:15:25 +00:00
|
|
|
|
2001-02-26 06:48:02 +00:00
|
|
|
Host_Init (&parms);
|
2001-02-19 21:15:25 +00:00
|
|
|
|
2001-02-26 06:48:02 +00:00
|
|
|
Sys_Init ();
|
2001-02-19 21:15:25 +00:00
|
|
|
|
2001-04-10 23:39:30 +00:00
|
|
|
sys_nostdout = Cvar_Get ("sys_nostdout", "0", CVAR_NONE, NULL,
|
|
|
|
"set to disable std out");
|
2001-02-26 06:48:02 +00:00
|
|
|
if (COM_CheckParm ("-nostdout"))
|
2001-03-30 23:24:57 +00:00
|
|
|
Cvar_Set (sys_nostdout, "1");
|
2001-02-19 21:15:25 +00:00
|
|
|
else {
|
2001-02-26 06:48:02 +00:00
|
|
|
fcntl (0, F_SETFL, fcntl (0, F_GETFL, 0) | FNDELAY);
|
2001-02-20 04:57:07 +00:00
|
|
|
printf ("Quake -- Version %s\n", NQ_VERSION);
|
2001-02-19 21:15:25 +00:00
|
|
|
}
|
|
|
|
|
2001-02-26 06:48:02 +00:00
|
|
|
oldtime = Sys_DoubleTime () - 0.1;
|
|
|
|
while (1) {
|
2001-02-19 21:15:25 +00:00
|
|
|
// find time spent rendering last frame
|
2001-02-26 06:48:02 +00:00
|
|
|
newtime = Sys_DoubleTime ();
|
|
|
|
time = newtime - oldtime;
|
|
|
|
|
|
|
|
if (cls.state == ca_dedicated) { // play vcrfiles at max speed
|
|
|
|
if (time < sys_ticrate->value && (vcrFile == -1 || recording)) {
|
|
|
|
usleep (1);
|
|
|
|
continue; // not time to run a server only tic
|
|
|
|
// yet
|
|
|
|
}
|
|
|
|
time = sys_ticrate->value;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (time > sys_ticrate->value * 2)
|
|
|
|
oldtime = newtime;
|
|
|
|
else
|
|
|
|
oldtime += time;
|
|
|
|
|
|
|
|
Host_Frame (time);
|
2001-02-19 21:15:25 +00:00
|
|
|
|
|
|
|
// graphic debugging aids
|
2001-02-26 06:48:02 +00:00
|
|
|
// if (sys_linerefresh->value)
|
|
|
|
// Sys_LineRefresh ();
|
|
|
|
}
|
2001-02-19 21:15:25 +00:00
|
|
|
|
|
|
|
}
|