2001-03-30 23:24:57 +00:00
|
|
|
/*
|
|
|
|
sys.c
|
|
|
|
|
|
|
|
virtual filesystem functions
|
|
|
|
|
|
|
|
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
|
|
|
|
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
|
|
|
|
|
|
|
|
*/
|
|
|
|
#ifdef HAVE_CONFIG_H
|
|
|
|
# include "config.h"
|
|
|
|
#endif
|
2003-01-15 15:31:36 +00:00
|
|
|
|
2001-03-30 23:24:57 +00:00
|
|
|
#ifdef HAVE_STRING_H
|
|
|
|
# include <string.h>
|
|
|
|
#endif
|
|
|
|
#ifdef HAVE_STRINGS_H
|
|
|
|
# include <strings.h>
|
|
|
|
#endif
|
|
|
|
#ifdef HAVE_UNISTD_H
|
2001-04-10 20:11:50 +00:00
|
|
|
# include <unistd.h>
|
|
|
|
#endif
|
|
|
|
#ifdef HAVE_LIMITS_H
|
|
|
|
# include <limits.h>
|
2001-03-30 23:24:57 +00:00
|
|
|
#endif
|
2002-06-04 16:17:10 +00:00
|
|
|
#ifdef HAVE_CONIO_H
|
|
|
|
# include <conio.h>
|
|
|
|
#endif
|
2001-03-30 23:24:57 +00:00
|
|
|
#ifdef HAVE_IO_H
|
2001-04-10 20:11:50 +00:00
|
|
|
# include <io.h>
|
|
|
|
#endif
|
|
|
|
#ifdef HAVE_WINDOWS_H
|
2002-10-11 20:47:17 +00:00
|
|
|
# include "winquake.h"
|
2013-01-23 02:10:19 +00:00
|
|
|
# include "shlobj.h"
|
2001-03-30 23:24:57 +00:00
|
|
|
#endif
|
2001-07-05 03:28:40 +00:00
|
|
|
#ifdef HAVE_SYS_MMAN_H
|
|
|
|
# include <sys/mman.h>
|
|
|
|
#endif
|
2012-08-18 01:57:42 +00:00
|
|
|
#ifdef HAVE_SYS_SELECT_H
|
|
|
|
# include <sys/select.h>
|
|
|
|
#endif
|
2010-08-24 07:01:01 +00:00
|
|
|
#ifdef HAVE_PWD_H
|
|
|
|
# include <pwd.h>
|
|
|
|
#endif
|
2001-03-30 23:24:57 +00:00
|
|
|
|
2022-03-31 07:31:13 +00:00
|
|
|
#include <inttypes.h>
|
2002-08-20 23:04:57 +00:00
|
|
|
#include <signal.h>
|
|
|
|
#include <setjmp.h>
|
2001-08-22 22:03:16 +00:00
|
|
|
#include <errno.h>
|
|
|
|
#include <fcntl.h>
|
|
|
|
#include <stdarg.h>
|
2001-11-27 04:50:41 +00:00
|
|
|
#include <time.h>
|
2013-02-20 04:59:31 +00:00
|
|
|
#include <sys/types.h>
|
2001-08-22 22:03:16 +00:00
|
|
|
#include <sys/stat.h>
|
2007-03-22 23:20:57 +00:00
|
|
|
#ifdef HAVE_SYS_TIME_H
|
2001-08-22 22:03:16 +00:00
|
|
|
#include <sys/time.h>
|
2007-03-22 23:20:57 +00:00
|
|
|
#endif
|
2001-08-22 22:03:16 +00:00
|
|
|
#include <sys/types.h>
|
|
|
|
|
2007-03-22 23:20:57 +00:00
|
|
|
#ifdef HAVE_DIRECT_H
|
|
|
|
#include <direct.h>
|
|
|
|
#endif
|
|
|
|
|
2013-01-22 05:09:41 +00:00
|
|
|
#include "qfalloca.h"
|
|
|
|
|
2016-01-03 14:04:00 +00:00
|
|
|
#include "QF/alloc.h"
|
2011-09-11 05:56:47 +00:00
|
|
|
#include "QF/cmd.h"
|
2001-03-30 23:24:57 +00:00
|
|
|
#include "QF/cvar.h"
|
2002-03-08 23:11:42 +00:00
|
|
|
#include "QF/dstring.h"
|
2011-12-24 01:04:33 +00:00
|
|
|
#include "QF/mathlib.h"
|
2001-03-30 23:24:57 +00:00
|
|
|
#include "QF/sys.h"
|
2002-11-14 05:28:54 +00:00
|
|
|
#include "QF/quakefs.h"
|
2010-08-24 07:01:01 +00:00
|
|
|
#include "QF/va.h"
|
2001-03-30 23:24:57 +00:00
|
|
|
|
2001-08-22 22:03:16 +00:00
|
|
|
#include "compat.h"
|
2021-09-28 01:53:51 +00:00
|
|
|
#define IMPLEMENT_QFSELECT_Funcs
|
|
|
|
#include "qfselect.h"
|
2001-08-22 22:03:16 +00:00
|
|
|
|
2021-03-27 10:52:59 +00:00
|
|
|
static void Sys_StdPrintf (const char *fmt, va_list args) __attribute__((format(PRINTF, 1, 0)));
|
|
|
|
static void Sys_ErrPrintf (const char *fmt, va_list args) __attribute__((format(PRINTF, 1, 0)));
|
2001-07-05 04:59:43 +00:00
|
|
|
|
[cvar] Make cvars properly typed
This is an extremely extensive patch as it hits every cvar, and every
usage of the cvars. Cvars no longer store the value they control,
instead, they use a cexpr value object to reference the value and
specify the value's type (currently, a null type is used for strings).
Non-string cvars are passed through cexpr, allowing expressions in the
cvars' settings. Also, cvars have returned to an enhanced version of the
original (id quake) registration scheme.
As a minor benefit, relevant code having direct access to the
cvar-controlled variables is probably a slight optimization as it
removed a pointer dereference, and the variables can be located for data
locality.
The static cvar descriptors are made private as an additional safety
layer, though there's nothing stopping external modification via
Cvar_FindVar (which is needed for adding listeners).
While not used yet (partly due to working out the design), cvars can
have a validation function.
Registering a cvar allows a primary listener (and its data) to be
specified: it will always be called first when the cvar is modified. The
combination of proper listeners and direct access to the controlled
variable greatly simplifies the more complex cvar interactions as much
less null checking is required, and there's no need for one cvar's
callback to call another's.
nq-x11 is known to work at least well enough for the demos. More testing
will come.
2022-04-23 03:22:45 +00:00
|
|
|
VISIBLE int sys_nostdout;
|
|
|
|
static cvar_t sys_nostdout_cvar = {
|
|
|
|
.name = "sys_nostdout",
|
|
|
|
.description =
|
|
|
|
"Set to disable std out",
|
|
|
|
.default_value = "0",
|
|
|
|
.flags = CVAR_NONE,
|
|
|
|
.value = { .type = &cexpr_int, .value = &sys_nostdout },
|
|
|
|
};
|
|
|
|
VISIBLE int sys_extrasleep;
|
|
|
|
static cvar_t sys_extrasleep_cvar = {
|
|
|
|
.name = "sys_extrasleep",
|
|
|
|
.description =
|
|
|
|
"Set to cause whatever amount delay in microseconds you want. Mostly "
|
|
|
|
"useful to generate simulated bad connections.",
|
|
|
|
.default_value = "0",
|
|
|
|
.flags = CVAR_NONE,
|
|
|
|
.value = { .type = &cexpr_int, .value = &sys_extrasleep },
|
|
|
|
};
|
|
|
|
int sys_dead_sleep;
|
|
|
|
static cvar_t sys_dead_sleep_cvar = {
|
|
|
|
.name = "sys_dead_sleep",
|
|
|
|
.description =
|
|
|
|
"When set, the server gets NO cpu if no clients are connected and "
|
|
|
|
"there's no other activity. *MIGHT* cause problems with some mods.",
|
|
|
|
.default_value = "0",
|
|
|
|
.flags = CVAR_NONE,
|
|
|
|
.value = { .type = &cexpr_int, .value = &sys_dead_sleep },
|
|
|
|
};
|
|
|
|
int sys_sleep;
|
|
|
|
static cvar_t sys_sleep_cvar = {
|
|
|
|
.name = "sys_sleep",
|
|
|
|
.description =
|
|
|
|
"Sleep how long in seconds between checking for connections. Minimum "
|
|
|
|
"is 0, maximum is 13",
|
|
|
|
.default_value = "8",
|
|
|
|
.flags = CVAR_NONE,
|
|
|
|
.value = { .type = &cexpr_int, .value = &sys_sleep },
|
|
|
|
};
|
2001-08-29 17:45:53 +00:00
|
|
|
|
2002-02-19 20:47:45 +00:00
|
|
|
int sys_checksum;
|
|
|
|
|
2023-03-05 07:41:13 +00:00
|
|
|
static __thread sys_printf_t sys_std_printf_function = Sys_StdPrintf;
|
|
|
|
static __thread sys_printf_t sys_err_printf_function = Sys_ErrPrintf;
|
2001-03-30 23:24:57 +00:00
|
|
|
|
2001-09-24 21:00:23 +00:00
|
|
|
typedef struct shutdown_list_s {
|
|
|
|
struct shutdown_list_s *next;
|
2020-03-21 13:24:11 +00:00
|
|
|
void (*func) (void *);
|
|
|
|
void *data;
|
2001-09-24 21:00:23 +00:00
|
|
|
} shutdown_list_t;
|
|
|
|
|
2016-01-03 14:04:00 +00:00
|
|
|
typedef struct error_handler_s {
|
|
|
|
struct error_handler_s *next;
|
|
|
|
sys_error_t func;
|
|
|
|
void *data;
|
|
|
|
} error_handler_t;
|
|
|
|
|
2023-03-05 07:41:13 +00:00
|
|
|
static __thread shutdown_list_t *shutdown_list;
|
2001-09-24 21:00:23 +00:00
|
|
|
|
2023-03-05 07:41:13 +00:00
|
|
|
static __thread error_handler_t *error_handler_freelist;
|
|
|
|
static __thread error_handler_t *error_handler;
|
2016-01-03 14:04:00 +00:00
|
|
|
|
2012-05-21 23:23:22 +00:00
|
|
|
#ifndef _WIN32
|
2002-06-03 19:11:48 +00:00
|
|
|
static int do_stdin = 1;
|
|
|
|
qboolean stdin_ready;
|
2004-03-26 02:52:47 +00:00
|
|
|
#endif
|
2002-06-03 19:11:48 +00:00
|
|
|
|
2001-03-30 23:24:57 +00:00
|
|
|
/* The translation table between the graphical font and plain ASCII --KB */
|
2007-03-10 12:00:59 +00:00
|
|
|
VISIBLE const char sys_char_map[256] = {
|
2020-02-23 10:05:43 +00:00
|
|
|
0, '#', '#', '#', '#', '.', '#', '#',
|
|
|
|
'#', 9, 10, '#', ' ', 13, '.', '.',
|
2001-03-30 23:24:57 +00:00
|
|
|
'[', ']', '0', '1', '2', '3', '4', '5',
|
|
|
|
'6', '7', '8', '9', '.', '<', '=', '>',
|
2020-02-23 10:05:43 +00:00
|
|
|
' ', '!', '"', '#', '$', '%', '&','\'',
|
2001-03-30 23:24:57 +00:00
|
|
|
'(', ')', '*', '+', ',', '-', '.', '/',
|
|
|
|
'0', '1', '2', '3', '4', '5', '6', '7',
|
|
|
|
'8', '9', ':', ';', '<', '=', '>', '?',
|
|
|
|
'@', 'A', 'B', 'C', 'D', 'E', 'F', 'G',
|
|
|
|
'H', 'I', 'J', 'K', 'L', 'M', 'N', 'O',
|
|
|
|
'P', 'Q', 'R', 'S', 'T', 'U', 'V', 'W',
|
2020-02-23 10:05:43 +00:00
|
|
|
'X', 'Y', 'Z', '[','\\', ']', '^', '_',
|
2001-03-30 23:24:57 +00:00
|
|
|
'`', 'a', 'b', 'c', 'd', 'e', 'f', 'g',
|
|
|
|
'h', 'i', 'j', 'k', 'l', 'm', 'n', 'o',
|
|
|
|
'p', 'q', 'r', 's', 't', 'u', 'v', 'w',
|
|
|
|
'x', 'y', 'z', '{', '|', '}', '~', '<',
|
|
|
|
|
|
|
|
'<', '=', '>', '#', '#', '.', '#', '#',
|
|
|
|
'#', '#', ' ', '#', ' ', '>', '.', '.',
|
|
|
|
'[', ']', '0', '1', '2', '3', '4', '5',
|
|
|
|
'6', '7', '8', '9', '.', '<', '=', '>',
|
2020-02-23 10:05:43 +00:00
|
|
|
' ', '!', '"', '#', '$', '%', '&','\'',
|
2001-03-30 23:24:57 +00:00
|
|
|
'(', ')', '*', '+', ',', '-', '.', '/',
|
|
|
|
'0', '1', '2', '3', '4', '5', '6', '7',
|
|
|
|
'8', '9', ':', ';', '<', '=', '>', '?',
|
|
|
|
'@', 'A', 'B', 'C', 'D', 'E', 'F', 'G',
|
|
|
|
'H', 'I', 'J', 'K', 'L', 'M', 'N', 'O',
|
|
|
|
'P', 'Q', 'R', 'S', 'T', 'U', 'V', 'W',
|
2020-02-23 10:05:43 +00:00
|
|
|
'X', 'Y', 'Z', '[','\\', ']', '^', '_',
|
2001-03-30 23:24:57 +00:00
|
|
|
'`', 'a', 'b', 'c', 'd', 'e', 'f', 'g',
|
|
|
|
'h', 'i', 'j', 'k', 'l', 'm', 'n', 'o',
|
|
|
|
'p', 'q', 'r', 's', 't', 'u', 'v', 'w',
|
|
|
|
'x', 'y', 'z', '{', '|', '}', '~', '<'
|
|
|
|
};
|
|
|
|
|
|
|
|
#define MAXPRINTMSG 4096
|
|
|
|
|
2001-08-22 22:03:16 +00:00
|
|
|
|
2002-06-03 19:11:48 +00:00
|
|
|
#ifndef USE_INTEL_ASM
|
|
|
|
void
|
2002-08-20 23:04:57 +00:00
|
|
|
Sys_MaskFPUExceptions (void)
|
2002-06-03 19:11:48 +00:00
|
|
|
{
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
2013-02-20 04:59:31 +00:00
|
|
|
int
|
|
|
|
Sys_isdir (const char *path)
|
|
|
|
{
|
|
|
|
int res;
|
|
|
|
#ifdef _WIN32
|
|
|
|
struct _stat st;
|
|
|
|
res = _stat (path, &st);
|
|
|
|
#else
|
|
|
|
struct stat st;
|
|
|
|
res = stat (path, &st);
|
|
|
|
#endif
|
|
|
|
if (res < 0) {
|
|
|
|
// assume any error means path does not refer to a directory. certainly
|
|
|
|
// true if errno == ENOENT
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
return S_ISDIR (st.st_mode);
|
|
|
|
}
|
|
|
|
|
2004-05-09 22:58:37 +00:00
|
|
|
int
|
2001-03-30 23:24:57 +00:00
|
|
|
Sys_mkdir (const char *path)
|
|
|
|
{
|
2001-05-23 03:01:03 +00:00
|
|
|
#ifdef HAVE_MKDIR
|
2001-04-10 20:15:52 +00:00
|
|
|
# ifdef _WIN32
|
2001-04-10 20:11:50 +00:00
|
|
|
if (mkdir (path) == 0)
|
2004-05-09 22:58:37 +00:00
|
|
|
return 0;
|
2001-04-10 20:11:50 +00:00
|
|
|
# else
|
2001-03-30 23:24:57 +00:00
|
|
|
if (mkdir (path, 0777) == 0)
|
2004-05-09 22:58:37 +00:00
|
|
|
return 0;
|
2001-04-10 20:11:50 +00:00
|
|
|
# endif
|
2001-03-30 23:24:57 +00:00
|
|
|
#else
|
2001-05-23 03:01:03 +00:00
|
|
|
# ifdef HAVE__MKDIR
|
2001-03-30 23:24:57 +00:00
|
|
|
if (_mkdir (path) == 0)
|
2004-05-09 22:58:37 +00:00
|
|
|
return 0;
|
2001-03-30 23:24:57 +00:00
|
|
|
# else
|
|
|
|
# error do not know how to make directories
|
|
|
|
# endif
|
|
|
|
#endif
|
2013-02-20 05:03:03 +00:00
|
|
|
return -1;
|
2001-03-30 23:24:57 +00:00
|
|
|
}
|
|
|
|
|
2007-03-10 12:00:59 +00:00
|
|
|
VISIBLE int
|
2013-02-07 06:43:26 +00:00
|
|
|
Sys_FileExists (const char *path)
|
2001-03-30 23:24:57 +00:00
|
|
|
{
|
2001-05-23 03:01:03 +00:00
|
|
|
#ifdef HAVE_ACCESS
|
2001-03-30 23:24:57 +00:00
|
|
|
if (access (path, R_OK) == 0)
|
|
|
|
return 0;
|
|
|
|
#else
|
2001-05-23 03:01:03 +00:00
|
|
|
# ifdef HAVE__ACCESS
|
2001-03-30 23:24:57 +00:00
|
|
|
if (_access (path, R_OK) == 0)
|
|
|
|
return 0;
|
|
|
|
# else
|
2012-08-18 02:45:26 +00:00
|
|
|
int fd;
|
|
|
|
|
|
|
|
if ((fd = open (path, O_RDONLY)) >= 0) {
|
|
|
|
close (fd);
|
|
|
|
return 0;
|
|
|
|
}
|
2001-03-30 23:24:57 +00:00
|
|
|
# endif
|
|
|
|
#endif
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
2001-07-05 04:59:43 +00:00
|
|
|
Sys_SetPrintf
|
|
|
|
|
|
|
|
for want of a better name, but it sets the function pointer for the
|
|
|
|
actual implementation of Sys_Printf.
|
2001-03-30 23:24:57 +00:00
|
|
|
*/
|
2020-02-23 02:41:19 +00:00
|
|
|
VISIBLE sys_printf_t
|
2002-02-20 19:22:52 +00:00
|
|
|
Sys_SetStdPrintf (sys_printf_t func)
|
2001-07-05 04:59:43 +00:00
|
|
|
{
|
2020-02-23 02:41:19 +00:00
|
|
|
sys_printf_t prev = sys_std_printf_function;
|
2021-12-29 12:50:38 +00:00
|
|
|
if (!func) {
|
|
|
|
func = Sys_StdPrintf;
|
|
|
|
}
|
2002-02-20 19:22:52 +00:00
|
|
|
sys_std_printf_function = func;
|
2020-02-23 02:41:19 +00:00
|
|
|
return prev;
|
2001-07-05 04:59:43 +00:00
|
|
|
}
|
|
|
|
|
2020-02-23 02:41:19 +00:00
|
|
|
VISIBLE sys_printf_t
|
2002-02-20 19:22:52 +00:00
|
|
|
Sys_SetErrPrintf (sys_printf_t func)
|
2001-03-30 23:24:57 +00:00
|
|
|
{
|
2020-02-23 02:41:19 +00:00
|
|
|
sys_printf_t prev = sys_err_printf_function;
|
2021-12-29 12:50:38 +00:00
|
|
|
if (!func) {
|
|
|
|
func = Sys_ErrPrintf;
|
|
|
|
}
|
2002-02-20 19:22:52 +00:00
|
|
|
sys_err_printf_function = func;
|
2020-02-23 02:41:19 +00:00
|
|
|
return prev;
|
2002-02-20 19:22:52 +00:00
|
|
|
}
|
2001-03-30 23:24:57 +00:00
|
|
|
|
2023-03-05 07:41:13 +00:00
|
|
|
static __thread dstring_t *sys_print_msg;
|
2002-02-20 19:22:52 +00:00
|
|
|
void
|
|
|
|
Sys_Print (FILE *stream, const char *fmt, va_list args)
|
|
|
|
{
|
2001-03-30 23:24:57 +00:00
|
|
|
unsigned char *p;
|
|
|
|
|
2023-03-05 07:41:13 +00:00
|
|
|
if (!sys_print_msg)
|
|
|
|
sys_print_msg = dstring_new ();
|
2002-03-08 23:11:42 +00:00
|
|
|
|
2023-03-05 07:41:13 +00:00
|
|
|
dvsprintf (sys_print_msg, fmt, args);
|
2001-03-30 23:24:57 +00:00
|
|
|
|
2002-05-21 21:03:46 +00:00
|
|
|
if (stream == stderr) {
|
2004-01-20 03:47:27 +00:00
|
|
|
#ifdef _WIN32
|
2023-03-05 07:41:13 +00:00
|
|
|
MessageBox (NULL, sys_print_msg->str, "Fatal Error", 0 /* MB_OK */ );
|
2002-02-20 19:22:52 +00:00
|
|
|
#endif
|
2002-05-21 21:03:46 +00:00
|
|
|
fputs ("Fatal Error: ", stream);
|
|
|
|
}
|
2002-02-20 19:22:52 +00:00
|
|
|
|
2001-03-30 23:24:57 +00:00
|
|
|
/* translate to ASCII instead of printing [xx] --KB */
|
2023-03-05 07:41:13 +00:00
|
|
|
for (p = (unsigned char *) sys_print_msg->str; *p; p++)
|
2002-02-20 19:22:52 +00:00
|
|
|
putc (sys_char_map[*p], stream);
|
2001-03-30 23:24:57 +00:00
|
|
|
|
2002-05-21 21:03:46 +00:00
|
|
|
if (stream == stderr) {
|
|
|
|
fputs ("\n", stream);
|
|
|
|
}
|
2002-02-20 19:22:52 +00:00
|
|
|
fflush (stream);
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
Sys_StdPrintf (const char *fmt, va_list args)
|
|
|
|
{
|
[cvar] Make cvars properly typed
This is an extremely extensive patch as it hits every cvar, and every
usage of the cvars. Cvars no longer store the value they control,
instead, they use a cexpr value object to reference the value and
specify the value's type (currently, a null type is used for strings).
Non-string cvars are passed through cexpr, allowing expressions in the
cvars' settings. Also, cvars have returned to an enhanced version of the
original (id quake) registration scheme.
As a minor benefit, relevant code having direct access to the
cvar-controlled variables is probably a slight optimization as it
removed a pointer dereference, and the variables can be located for data
locality.
The static cvar descriptors are made private as an additional safety
layer, though there's nothing stopping external modification via
Cvar_FindVar (which is needed for adding listeners).
While not used yet (partly due to working out the design), cvars can
have a validation function.
Registering a cvar allows a primary listener (and its data) to be
specified: it will always be called first when the cvar is modified. The
combination of proper listeners and direct access to the controlled
variable greatly simplifies the more complex cvar interactions as much
less null checking is required, and there's no need for one cvar's
callback to call another's.
nq-x11 is known to work at least well enough for the demos. More testing
will come.
2022-04-23 03:22:45 +00:00
|
|
|
if (sys_nostdout)
|
2002-02-20 19:22:52 +00:00
|
|
|
return;
|
|
|
|
Sys_Print (stdout, fmt, args);
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
Sys_ErrPrintf (const char *fmt, va_list args)
|
|
|
|
{
|
|
|
|
Sys_Print (stderr, fmt, args);
|
2001-03-30 23:24:57 +00:00
|
|
|
}
|
|
|
|
|
2007-03-10 12:00:59 +00:00
|
|
|
VISIBLE void
|
2001-07-05 04:59:43 +00:00
|
|
|
Sys_Printf (const char *fmt, ...)
|
|
|
|
{
|
|
|
|
va_list args;
|
|
|
|
va_start (args, fmt);
|
2002-02-20 19:22:52 +00:00
|
|
|
sys_std_printf_function (fmt, args);
|
2001-07-05 06:31:05 +00:00
|
|
|
va_end (args);
|
2001-07-05 04:59:43 +00:00
|
|
|
}
|
|
|
|
|
2007-03-10 12:00:59 +00:00
|
|
|
VISIBLE void
|
2022-06-03 03:04:27 +00:00
|
|
|
Sys_MaskPrintf (sys_developer_e mask, const char *fmt, ...)
|
2001-09-21 04:22:46 +00:00
|
|
|
{
|
|
|
|
va_list args;
|
|
|
|
|
[cvar] Make cvars properly typed
This is an extremely extensive patch as it hits every cvar, and every
usage of the cvars. Cvars no longer store the value they control,
instead, they use a cexpr value object to reference the value and
specify the value's type (currently, a null type is used for strings).
Non-string cvars are passed through cexpr, allowing expressions in the
cvars' settings. Also, cvars have returned to an enhanced version of the
original (id quake) registration scheme.
As a minor benefit, relevant code having direct access to the
cvar-controlled variables is probably a slight optimization as it
removed a pointer dereference, and the variables can be located for data
locality.
The static cvar descriptors are made private as an additional safety
layer, though there's nothing stopping external modification via
Cvar_FindVar (which is needed for adding listeners).
While not used yet (partly due to working out the design), cvars can
have a validation function.
Registering a cvar allows a primary listener (and its data) to be
specified: it will always be called first when the cvar is modified. The
combination of proper listeners and direct access to the controlled
variable greatly simplifies the more complex cvar interactions as much
less null checking is required, and there's no need for one cvar's
callback to call another's.
nq-x11 is known to work at least well enough for the demos. More testing
will come.
2022-04-23 03:22:45 +00:00
|
|
|
if (!(developer & mask))
|
2001-09-21 04:22:46 +00:00
|
|
|
return;
|
|
|
|
va_start (args, fmt);
|
2002-02-20 19:22:52 +00:00
|
|
|
sys_std_printf_function (fmt, args);
|
2001-09-21 04:22:46 +00:00
|
|
|
va_end (args);
|
|
|
|
}
|
|
|
|
|
2021-08-27 03:10:05 +00:00
|
|
|
static int64_t sys_starttime = -1;
|
|
|
|
|
|
|
|
VISIBLE int64_t
|
|
|
|
Sys_StartTime (void)
|
|
|
|
{
|
|
|
|
return sys_starttime;
|
|
|
|
}
|
|
|
|
|
2013-02-27 05:29:28 +00:00
|
|
|
VISIBLE int64_t
|
|
|
|
Sys_LongTime (void)
|
2001-03-30 23:24:57 +00:00
|
|
|
{
|
2001-04-01 03:30:17 +00:00
|
|
|
static qboolean first = true;
|
2001-03-30 23:24:57 +00:00
|
|
|
#ifdef _WIN32
|
2013-01-05 08:42:27 +00:00
|
|
|
# if 0
|
2001-03-30 23:24:57 +00:00
|
|
|
static DWORD starttime;
|
|
|
|
DWORD now;
|
|
|
|
|
|
|
|
now = timeGetTime ();
|
|
|
|
|
|
|
|
if (first) {
|
|
|
|
first = false;
|
|
|
|
starttime = now;
|
|
|
|
return 0.0;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (now < starttime) // wrapped?
|
|
|
|
return (now / 1000.0) + (LONG_MAX - starttime / 1000.0);
|
|
|
|
|
|
|
|
if (now - starttime == 0)
|
|
|
|
return 0.0;
|
|
|
|
|
|
|
|
return (now - starttime) / 1000.0;
|
2013-01-05 08:42:27 +00:00
|
|
|
# else
|
|
|
|
// MH's solution combining timeGetTime for stability and
|
|
|
|
// QueryPerformanceCounter for precision.
|
2013-02-27 05:29:28 +00:00
|
|
|
static int64_t qpcfreq = 0;
|
|
|
|
static int64_t currqpccount = 0;
|
|
|
|
static int64_t lastqpccount = 0;
|
|
|
|
static int64_t qpcfudge = 0;
|
|
|
|
int64_t currtime = 0;
|
|
|
|
static int64_t lasttime = 0;
|
2013-01-05 08:42:27 +00:00
|
|
|
|
|
|
|
if (first) {
|
|
|
|
timeBeginPeriod (1);
|
2021-08-27 03:10:05 +00:00
|
|
|
sys_starttime = lasttime = timeGetTime () * 1000;
|
2013-01-05 08:42:27 +00:00
|
|
|
QueryPerformanceFrequency ((LARGE_INTEGER *) &qpcfreq);
|
|
|
|
QueryPerformanceCounter ((LARGE_INTEGER *) &lastqpccount);
|
|
|
|
first = false;
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
// get the current time from both counters
|
2021-08-27 03:10:05 +00:00
|
|
|
currtime = timeGetTime () * 1000;
|
2013-01-05 08:42:27 +00:00
|
|
|
QueryPerformanceCounter ((LARGE_INTEGER *) &currqpccount);
|
|
|
|
|
|
|
|
if (currtime != lasttime) {
|
|
|
|
// requery the frequency in case it changes (which it can on multicore
|
|
|
|
// machines)
|
|
|
|
QueryPerformanceFrequency ((LARGE_INTEGER *) &qpcfreq);
|
|
|
|
|
|
|
|
// store back times and calc a fudge factor as timeGetTime can
|
|
|
|
// overshoot on a sub-millisecond scale
|
2013-02-27 05:29:28 +00:00
|
|
|
qpcfudge = (( (currqpccount - lastqpccount) * 1000000 / qpcfreq))
|
2021-08-27 03:10:05 +00:00
|
|
|
- (currtime - lasttime);
|
2013-01-05 08:42:27 +00:00
|
|
|
lastqpccount = currqpccount;
|
|
|
|
lasttime = currtime;
|
|
|
|
} else {
|
|
|
|
qpcfudge = 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
// the final time is the base from timeGetTime plus an addition from QPC
|
2021-08-27 03:10:05 +00:00
|
|
|
return ((currtime - sys_starttime)
|
2013-02-27 05:29:28 +00:00
|
|
|
+ ((currqpccount - lastqpccount) * 1000000 / qpcfreq) + qpcfudge);
|
2013-01-05 08:42:27 +00:00
|
|
|
# endif
|
2001-03-30 23:24:57 +00:00
|
|
|
#else
|
2013-02-27 05:29:28 +00:00
|
|
|
int64_t now;
|
2021-08-27 02:22:57 +00:00
|
|
|
#ifdef CLOCK_BOOTTIME
|
|
|
|
struct timespec tp;
|
|
|
|
|
|
|
|
clock_gettime (CLOCK_BOOTTIME, &tp);
|
|
|
|
|
|
|
|
now = tp.tv_sec * 1000000 + tp.tv_nsec / 1000;
|
|
|
|
#else
|
|
|
|
struct timeval tp;
|
|
|
|
struct timezone tzp;
|
2001-03-30 23:24:57 +00:00
|
|
|
|
|
|
|
gettimeofday (&tp, &tzp);
|
2002-06-03 23:29:34 +00:00
|
|
|
|
2013-02-27 05:29:28 +00:00
|
|
|
now = tp.tv_sec * 1000000 + tp.tv_usec;
|
2021-08-27 02:22:57 +00:00
|
|
|
#endif
|
2001-03-30 23:24:57 +00:00
|
|
|
|
2001-04-01 03:30:17 +00:00
|
|
|
if (first) {
|
|
|
|
first = false;
|
2021-08-27 03:10:05 +00:00
|
|
|
sys_starttime = now;
|
2001-03-30 23:24:57 +00:00
|
|
|
}
|
|
|
|
|
2021-08-27 03:10:05 +00:00
|
|
|
return now - sys_starttime;
|
2001-03-30 23:24:57 +00:00
|
|
|
#endif
|
|
|
|
}
|
2001-07-05 03:28:40 +00:00
|
|
|
|
2021-08-27 03:10:05 +00:00
|
|
|
VISIBLE int64_t
|
|
|
|
Sys_TimeBase (void)
|
|
|
|
{
|
2022-01-06 13:27:42 +00:00
|
|
|
return INT64_C (4294967296000000);
|
2021-08-27 03:10:05 +00:00
|
|
|
}
|
|
|
|
|
2013-02-27 05:29:28 +00:00
|
|
|
VISIBLE double
|
|
|
|
Sys_DoubleTime (void)
|
|
|
|
{
|
2021-08-27 03:10:05 +00:00
|
|
|
return (Sys_TimeBase () + Sys_LongTime ()) / 1e6;
|
2013-02-27 05:29:28 +00:00
|
|
|
}
|
|
|
|
|
2021-07-10 23:18:02 +00:00
|
|
|
VISIBLE double
|
|
|
|
Sys_DoubleTimeBase (void)
|
|
|
|
{
|
2021-08-27 03:10:05 +00:00
|
|
|
return Sys_TimeBase () / 1e6;
|
2021-07-10 23:18:02 +00:00
|
|
|
}
|
|
|
|
|
2007-03-10 12:00:59 +00:00
|
|
|
VISIBLE void
|
2003-11-20 07:00:07 +00:00
|
|
|
Sys_TimeOfDay (date_t *date)
|
|
|
|
{
|
|
|
|
struct tm *newtime;
|
|
|
|
time_t long_time;
|
|
|
|
|
|
|
|
time (&long_time);
|
|
|
|
newtime = localtime (&long_time);
|
|
|
|
|
|
|
|
date->day = newtime->tm_mday;
|
|
|
|
date->mon = newtime->tm_mon;
|
|
|
|
date->year = newtime->tm_year + 1900;
|
|
|
|
date->hour = newtime->tm_hour;
|
|
|
|
date->min = newtime->tm_min;
|
|
|
|
date->sec = newtime->tm_sec;
|
|
|
|
strftime (date->str, 128, "%a %b %d, %H:%M %Y", newtime);
|
|
|
|
}
|
|
|
|
|
2007-03-24 12:28:47 +00:00
|
|
|
VISIBLE void
|
2007-04-04 07:48:14 +00:00
|
|
|
Sys_MakeCodeWriteable (uintptr_t startaddr, size_t length)
|
2001-07-05 03:28:40 +00:00
|
|
|
{
|
2001-07-05 18:47:19 +00:00
|
|
|
#ifdef _WIN32
|
|
|
|
DWORD flOldProtect;
|
|
|
|
|
|
|
|
if (!VirtualProtect
|
2021-04-02 13:17:32 +00:00
|
|
|
((LPVOID) startaddr, length, PAGE_EXECUTE_READWRITE, &flOldProtect))
|
2002-05-21 21:03:46 +00:00
|
|
|
Sys_Error ("Protection change failed");
|
2001-07-05 18:47:19 +00:00
|
|
|
#else
|
|
|
|
# ifdef HAVE_MPROTECT
|
2001-07-05 03:28:40 +00:00
|
|
|
int r;
|
2021-04-01 09:20:56 +00:00
|
|
|
long psize = Sys_PageSize ();
|
2003-07-17 21:01:25 +00:00
|
|
|
unsigned long endaddr = startaddr + length;
|
|
|
|
startaddr &= ~(psize - 1);
|
|
|
|
endaddr = (endaddr + psize - 1) & ~(psize - 1);
|
2003-07-21 22:24:28 +00:00
|
|
|
// systems with mprotect but not getpagesize (or similar) probably don't
|
|
|
|
// need to page align the arguments to mprotect (eg, QNX)
|
2003-07-17 21:45:01 +00:00
|
|
|
r = mprotect ((char *) startaddr, endaddr - startaddr,
|
|
|
|
PROT_EXEC | PROT_READ | PROT_WRITE);
|
2001-07-05 03:28:40 +00:00
|
|
|
|
|
|
|
if (r < 0)
|
2002-05-21 21:03:46 +00:00
|
|
|
Sys_Error ("Protection change failed");
|
2001-07-05 03:28:40 +00:00
|
|
|
# endif
|
|
|
|
#endif
|
|
|
|
}
|
2001-08-29 17:45:53 +00:00
|
|
|
|
2007-03-10 12:00:59 +00:00
|
|
|
VISIBLE void
|
2001-08-29 17:45:53 +00:00
|
|
|
Sys_Init_Cvars (void)
|
|
|
|
{
|
[cvar] Make cvars properly typed
This is an extremely extensive patch as it hits every cvar, and every
usage of the cvars. Cvars no longer store the value they control,
instead, they use a cexpr value object to reference the value and
specify the value's type (currently, a null type is used for strings).
Non-string cvars are passed through cexpr, allowing expressions in the
cvars' settings. Also, cvars have returned to an enhanced version of the
original (id quake) registration scheme.
As a minor benefit, relevant code having direct access to the
cvar-controlled variables is probably a slight optimization as it
removed a pointer dereference, and the variables can be located for data
locality.
The static cvar descriptors are made private as an additional safety
layer, though there's nothing stopping external modification via
Cvar_FindVar (which is needed for adding listeners).
While not used yet (partly due to working out the design), cvars can
have a validation function.
Registering a cvar allows a primary listener (and its data) to be
specified: it will always be called first when the cvar is modified. The
combination of proper listeners and direct access to the controlled
variable greatly simplifies the more complex cvar interactions as much
less null checking is required, and there's no need for one cvar's
callback to call another's.
nq-x11 is known to work at least well enough for the demos. More testing
will come.
2022-04-23 03:22:45 +00:00
|
|
|
Cvar_Register (&sys_nostdout_cvar, 0, 0);
|
|
|
|
Cvar_Register (&sys_extrasleep_cvar, 0, 0);
|
|
|
|
Cvar_Register (&sys_dead_sleep_cvar, 0, 0);
|
|
|
|
Cvar_Register (&sys_sleep_cvar, 0, 0);
|
2001-08-29 17:45:53 +00:00
|
|
|
}
|
2001-09-24 21:00:23 +00:00
|
|
|
|
2007-03-10 12:00:59 +00:00
|
|
|
VISIBLE void
|
2001-09-24 21:00:23 +00:00
|
|
|
Sys_Quit (void)
|
|
|
|
{
|
2002-02-27 06:55:21 +00:00
|
|
|
Sys_Shutdown ();
|
2001-09-24 21:00:23 +00:00
|
|
|
|
|
|
|
exit (0);
|
|
|
|
}
|
|
|
|
|
2016-01-03 14:04:00 +00:00
|
|
|
VISIBLE void
|
|
|
|
Sys_PushErrorHandler (sys_error_t func, void *data)
|
|
|
|
{
|
|
|
|
error_handler_t *eh;
|
2023-03-05 07:26:37 +00:00
|
|
|
if (error_handler_freelist) {
|
|
|
|
eh = error_handler_freelist;
|
|
|
|
} else {
|
|
|
|
eh = malloc (sizeof (error_handler_t));
|
|
|
|
eh->next = 0;
|
|
|
|
}
|
2016-01-03 14:04:00 +00:00
|
|
|
eh->func = func;
|
|
|
|
eh->data = data;
|
2023-03-05 07:26:37 +00:00
|
|
|
error_handler_freelist = eh->next;
|
2016-01-03 14:04:00 +00:00
|
|
|
eh->next = error_handler;
|
|
|
|
error_handler = eh;
|
|
|
|
}
|
|
|
|
|
|
|
|
VISIBLE void
|
|
|
|
Sys_PopErrorHandler (void)
|
|
|
|
{
|
|
|
|
error_handler_t *eh;
|
|
|
|
|
|
|
|
if (!error_handler) {
|
|
|
|
Sys_Error ("Sys_PopErrorHandler: no handler to pop");
|
|
|
|
}
|
|
|
|
eh = error_handler;
|
|
|
|
error_handler = eh->next;
|
2023-03-05 07:26:37 +00:00
|
|
|
eh->next = error_handler_freelist;
|
|
|
|
error_handler_freelist = eh;
|
2016-01-03 14:04:00 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2007-03-10 12:00:59 +00:00
|
|
|
VISIBLE void
|
2001-09-24 21:00:23 +00:00
|
|
|
Sys_Error (const char *error, ...)
|
|
|
|
{
|
2002-08-07 15:31:56 +00:00
|
|
|
va_list args;
|
|
|
|
va_list tmp_args;
|
2011-08-20 04:13:43 +00:00
|
|
|
static int in_sys_error = 0;
|
|
|
|
|
|
|
|
if (in_sys_error) {
|
2012-04-25 22:28:22 +00:00
|
|
|
ssize_t cnt;
|
2011-08-20 04:13:43 +00:00
|
|
|
const char *msg = "\nSys_Error: recursive error condition\n";
|
2012-04-24 23:48:46 +00:00
|
|
|
cnt = write (2, msg, strlen (msg));
|
|
|
|
if (cnt < 0)
|
|
|
|
perror ("write failed");
|
2011-08-20 04:13:43 +00:00
|
|
|
abort ();
|
|
|
|
}
|
|
|
|
in_sys_error = 1;
|
2001-09-24 21:00:23 +00:00
|
|
|
|
2002-08-07 15:31:56 +00:00
|
|
|
va_start (args, error);
|
2016-01-03 12:16:23 +00:00
|
|
|
va_copy (tmp_args, args);
|
2002-08-07 15:31:56 +00:00
|
|
|
sys_err_printf_function (error, args);
|
|
|
|
va_end (args);
|
2001-09-24 21:00:23 +00:00
|
|
|
|
2016-01-03 14:04:00 +00:00
|
|
|
if (error_handler) {
|
|
|
|
error_handler->func (error_handler->data);
|
|
|
|
}
|
|
|
|
|
2002-02-27 06:55:21 +00:00
|
|
|
Sys_Shutdown ();
|
2001-09-24 21:00:23 +00:00
|
|
|
|
2002-11-14 22:17:25 +00:00
|
|
|
if (sys_err_printf_function != Sys_ErrPrintf) {
|
|
|
|
// print the message again using the default error printer to increase
|
|
|
|
// the chances of the error being seen.
|
2016-01-03 12:16:23 +00:00
|
|
|
va_copy (args, tmp_args);
|
2002-11-14 22:17:25 +00:00
|
|
|
Sys_ErrPrintf (error, args);
|
|
|
|
}
|
2002-08-07 15:31:56 +00:00
|
|
|
|
2001-09-24 21:00:23 +00:00
|
|
|
exit (1);
|
|
|
|
}
|
|
|
|
|
2007-03-10 12:00:59 +00:00
|
|
|
VISIBLE void
|
2020-03-21 13:24:11 +00:00
|
|
|
Sys_RegisterShutdown (void (*func) (void *), void *data)
|
2001-09-24 21:00:23 +00:00
|
|
|
{
|
|
|
|
shutdown_list_t *p;
|
|
|
|
if (!func)
|
|
|
|
return;
|
|
|
|
p = malloc (sizeof (*p));
|
|
|
|
if (!p)
|
|
|
|
Sys_Error ("Sys_RegisterShutdown: insufficient memory");
|
|
|
|
p->func = func;
|
2020-03-21 13:24:11 +00:00
|
|
|
p->data = data;
|
2001-09-24 21:00:23 +00:00
|
|
|
p->next = shutdown_list;
|
|
|
|
shutdown_list = p;
|
|
|
|
}
|
2001-11-27 04:50:41 +00:00
|
|
|
|
2007-03-10 12:00:59 +00:00
|
|
|
VISIBLE int
|
2001-11-27 04:50:41 +00:00
|
|
|
Sys_TimeID (void) //FIXME I need a new name, one that doesn't make me feel 3 feet thick
|
|
|
|
{
|
|
|
|
int val;
|
2012-08-18 12:37:54 +00:00
|
|
|
#ifdef HAVE_GETUID
|
2001-11-27 04:50:41 +00:00
|
|
|
val = ((int) (getpid () + getuid () * 1000) * time (NULL)) & 0xffff;
|
2012-08-18 12:37:54 +00:00
|
|
|
#else
|
|
|
|
val = ((int) (Sys_DoubleTime () * 1000) * time (NULL)) & 0xffff;
|
2001-11-27 04:50:41 +00:00
|
|
|
#endif
|
|
|
|
return val;
|
|
|
|
}
|
2002-02-19 20:47:45 +00:00
|
|
|
|
2007-03-10 12:00:59 +00:00
|
|
|
VISIBLE void
|
2021-02-03 04:19:19 +00:00
|
|
|
Sys_PageIn (void *ptr, size_t size)
|
2002-02-19 20:47:45 +00:00
|
|
|
{
|
2004-01-20 03:47:27 +00:00
|
|
|
//may or may not be useful in linux #ifdef _WIN32
|
2002-02-19 20:47:45 +00:00
|
|
|
byte *x;
|
2021-02-03 04:19:19 +00:00
|
|
|
size_t m, n;
|
2002-02-19 20:47:45 +00:00
|
|
|
|
|
|
|
// touch all the memory to make sure it's there. The 16-page skip is to
|
|
|
|
// keep Win 95 from thinking we're trying to page ourselves in (we are
|
|
|
|
// doing that, of course, but there's no reason we shouldn't)
|
|
|
|
x = (byte *) ptr;
|
|
|
|
|
|
|
|
for (n = 0; n < 4; n++) {
|
|
|
|
for (m = 0; m < (size - 16 * 0x1000); m += 4) {
|
|
|
|
sys_checksum += *(int *) &x[m];
|
|
|
|
sys_checksum += *(int *) &x[m + 16 * 0x1000];
|
|
|
|
}
|
|
|
|
}
|
|
|
|
//#endif
|
|
|
|
}
|
2002-05-31 22:50:43 +00:00
|
|
|
|
2022-03-09 10:57:28 +00:00
|
|
|
#if defined(_WIN32) && !defined(_WIN64)
|
|
|
|
// this is a hack to make memory allocations 16-byte aligned on 32-bit
|
|
|
|
// systems (in particular for this case, windows) as vectors and
|
|
|
|
// matrices require 16-byte alignment but system malloc (etc) provide only
|
|
|
|
// 8-byte alignment.
|
|
|
|
void *__cdecl
|
|
|
|
calloc (size_t nume, size_t sizee)
|
|
|
|
{
|
|
|
|
size_t size = nume * sizee;
|
|
|
|
void *mem = _aligned_malloc (size, 16);
|
|
|
|
memset (mem, 0, size);
|
|
|
|
return mem;
|
|
|
|
}
|
|
|
|
|
|
|
|
void __cdecl
|
|
|
|
free (void *mem)
|
|
|
|
{
|
|
|
|
_aligned_free (mem);
|
|
|
|
}
|
|
|
|
|
|
|
|
void *__cdecl
|
|
|
|
malloc (size_t size)
|
|
|
|
{
|
|
|
|
return _aligned_malloc (size, 16);
|
|
|
|
}
|
|
|
|
|
|
|
|
void *__cdecl
|
|
|
|
realloc (void *mem, size_t size)
|
|
|
|
{
|
|
|
|
return _aligned_realloc (mem, size, 16);
|
|
|
|
}
|
2022-03-21 10:23:49 +00:00
|
|
|
|
|
|
|
char *__cdecl
|
|
|
|
strdup(const char *src)
|
|
|
|
{
|
|
|
|
size_t len = strlen (src);
|
|
|
|
char *dup = malloc (len + 1);
|
|
|
|
strcpy (dup, src);
|
|
|
|
return dup;
|
|
|
|
}
|
2022-03-09 10:57:28 +00:00
|
|
|
#endif
|
|
|
|
|
2021-07-12 09:55:16 +00:00
|
|
|
VISIBLE size_t
|
2021-04-01 09:20:56 +00:00
|
|
|
Sys_PageSize (void)
|
|
|
|
{
|
|
|
|
#ifdef _WIN32
|
|
|
|
SYSTEM_INFO si;
|
|
|
|
GetSystemInfo (&si);
|
|
|
|
return si.dwPageSize;
|
|
|
|
#else
|
|
|
|
# ifdef HAVE__SC_PAGESIZE
|
|
|
|
return sysconf (_SC_PAGESIZE);
|
|
|
|
# else
|
|
|
|
# ifdef HAVE_GETPAGESIZE
|
|
|
|
return getpagesize ();
|
|
|
|
# endif
|
|
|
|
# endif
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
2021-02-03 04:19:19 +00:00
|
|
|
VISIBLE void *
|
|
|
|
Sys_Alloc (size_t size)
|
|
|
|
{
|
2021-04-01 09:20:56 +00:00
|
|
|
size_t page_size = Sys_PageSize ();
|
2021-03-27 11:09:37 +00:00
|
|
|
size_t page_mask = page_size - 1;
|
|
|
|
size = (size + page_mask) & ~page_mask;
|
2021-04-01 09:20:56 +00:00
|
|
|
#ifdef _WIN32
|
2021-03-27 11:09:37 +00:00
|
|
|
return VirtualAlloc (0, size, MEM_COMMIT | MEM_RESERVE, PAGE_READWRITE);
|
|
|
|
#else
|
2021-02-03 04:19:19 +00:00
|
|
|
return mmap (0, size, PROT_READ | PROT_WRITE, MAP_PRIVATE | MAP_ANONYMOUS,
|
|
|
|
-1, 0);
|
2021-03-27 11:09:37 +00:00
|
|
|
#endif
|
2021-02-03 04:19:19 +00:00
|
|
|
}
|
|
|
|
|
2021-07-12 07:33:47 +00:00
|
|
|
VISIBLE void
|
|
|
|
Sys_Free (void *mem, size_t size)
|
|
|
|
{
|
|
|
|
size_t page_size = Sys_PageSize ();
|
|
|
|
size_t page_mask = page_size - 1;
|
|
|
|
size = (size + page_mask) & ~page_mask;
|
|
|
|
#ifdef _WIN32
|
2021-07-12 09:55:16 +00:00
|
|
|
VirtualFree (mem, 0, MEM_RELEASE | MEM_DECOMMIT);
|
2021-07-12 07:33:47 +00:00
|
|
|
#else
|
|
|
|
munmap (mem, size);
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
2022-06-04 03:24:53 +00:00
|
|
|
VISIBLE int
|
|
|
|
Sys_LockMemory (void *mem, size_t size)
|
|
|
|
{
|
|
|
|
size_t page_size = Sys_PageSize ();
|
|
|
|
size_t page_mask = page_size - 1;
|
|
|
|
size = (size + page_mask) & ~page_mask;
|
|
|
|
#ifdef _WIN32
|
|
|
|
return VirtualLock (mem, size) != 0;
|
|
|
|
#else
|
|
|
|
return mlock (mem, size) == 0;
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
2021-08-13 12:26:48 +00:00
|
|
|
VISIBLE int
|
|
|
|
Sys_ProcessorCount (void)
|
|
|
|
{
|
|
|
|
int cpus = 1;
|
|
|
|
#if defined (_SC_NPROCESSORS_ONLN)
|
|
|
|
cpus = sysconf(_SC_NPROCESSORS_ONLN);
|
|
|
|
#elif defined (_WIN32)
|
|
|
|
SYSTEM_INFO sysinfo;
|
|
|
|
GetSystemInfo(&sysinfo);
|
|
|
|
cpus = sysinfo.dwNumberOfProcessors;
|
|
|
|
#endif
|
|
|
|
if (cpus < 1) {
|
|
|
|
cpus = 1;
|
|
|
|
}
|
|
|
|
return cpus;
|
|
|
|
}
|
|
|
|
|
2023-03-05 07:41:13 +00:00
|
|
|
static __thread dstring_t *sys_debuglog_data;
|
2007-03-10 12:00:59 +00:00
|
|
|
VISIBLE void
|
2002-05-31 22:50:43 +00:00
|
|
|
Sys_DebugLog (const char *file, const char *fmt, ...)
|
|
|
|
{
|
2002-08-07 15:31:56 +00:00
|
|
|
va_list args;
|
2002-05-31 22:50:43 +00:00
|
|
|
int fd;
|
2003-03-13 05:41:55 +00:00
|
|
|
|
2023-03-05 07:41:13 +00:00
|
|
|
if (!sys_debuglog_data)
|
|
|
|
sys_debuglog_data = dstring_newstr ();
|
2002-08-27 22:13:34 +00:00
|
|
|
|
2002-08-07 15:31:56 +00:00
|
|
|
va_start (args, fmt);
|
2023-03-05 07:41:13 +00:00
|
|
|
dvsprintf (sys_debuglog_data, fmt, args);
|
2002-08-07 15:31:56 +00:00
|
|
|
va_end (args);
|
2007-01-06 21:31:03 +00:00
|
|
|
if ((fd = open (file, O_WRONLY | O_CREAT | O_APPEND, 0644)) >= 0) {
|
2023-03-05 07:41:13 +00:00
|
|
|
if (write (fd, sys_debuglog_data->str, sys_debuglog_data->size - 1)
|
|
|
|
!= (ssize_t) (sys_debuglog_data->size - 1))
|
2007-01-06 21:31:03 +00:00
|
|
|
Sys_Printf ("Error writing %s: %s\n", file, strerror(errno));
|
|
|
|
close (fd);
|
|
|
|
}
|
2002-05-31 22:50:43 +00:00
|
|
|
}
|
2002-06-01 03:00:13 +00:00
|
|
|
|
2021-09-26 05:54:24 +00:00
|
|
|
VISIBLE int
|
2021-09-28 01:53:51 +00:00
|
|
|
Sys_Select (int maxfd, qf_fd_set *fdset, int64_t usec)
|
2021-09-26 05:54:24 +00:00
|
|
|
{
|
|
|
|
struct timeval _timeout;
|
|
|
|
struct timeval *timeout = 0;
|
|
|
|
|
|
|
|
if (usec >= 0) {
|
|
|
|
timeout = &_timeout;
|
|
|
|
if (usec < 1000000) {
|
|
|
|
_timeout.tv_sec = 0;
|
|
|
|
_timeout.tv_usec = usec;
|
|
|
|
} else {
|
|
|
|
_timeout.tv_sec = usec / 1000000;
|
|
|
|
_timeout.tv_usec = usec % 1000000;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-09-28 01:53:51 +00:00
|
|
|
return select (maxfd + 1, &fdset->fdset, NULL, NULL, timeout);
|
2021-09-26 05:54:24 +00:00
|
|
|
}
|
|
|
|
|
2007-03-10 12:00:59 +00:00
|
|
|
VISIBLE int
|
2005-01-26 01:19:36 +00:00
|
|
|
Sys_CheckInput (int idle, int net_socket)
|
2002-06-01 03:00:13 +00:00
|
|
|
{
|
2021-09-28 01:53:51 +00:00
|
|
|
qf_fd_set fdset;
|
2003-01-16 16:11:28 +00:00
|
|
|
int res;
|
2021-09-26 05:54:24 +00:00
|
|
|
int64_t usec;
|
2002-06-01 03:00:13 +00:00
|
|
|
|
2002-06-03 19:11:48 +00:00
|
|
|
#ifdef _WIN32
|
2002-06-04 16:17:10 +00:00
|
|
|
int sleep_msec;
|
2002-06-03 19:11:48 +00:00
|
|
|
// Now we want to give some processing time to other applications,
|
|
|
|
// such as qw_client, running on this machine.
|
[cvar] Make cvars properly typed
This is an extremely extensive patch as it hits every cvar, and every
usage of the cvars. Cvars no longer store the value they control,
instead, they use a cexpr value object to reference the value and
specify the value's type (currently, a null type is used for strings).
Non-string cvars are passed through cexpr, allowing expressions in the
cvars' settings. Also, cvars have returned to an enhanced version of the
original (id quake) registration scheme.
As a minor benefit, relevant code having direct access to the
cvar-controlled variables is probably a slight optimization as it
removed a pointer dereference, and the variables can be located for data
locality.
The static cvar descriptors are made private as an additional safety
layer, though there's nothing stopping external modification via
Cvar_FindVar (which is needed for adding listeners).
While not used yet (partly due to working out the design), cvars can
have a validation function.
Registering a cvar allows a primary listener (and its data) to be
specified: it will always be called first when the cvar is modified. The
combination of proper listeners and direct access to the controlled
variable greatly simplifies the more complex cvar interactions as much
less null checking is required, and there's no need for one cvar's
callback to call another's.
nq-x11 is known to work at least well enough for the demos. More testing
will come.
2022-04-23 03:22:45 +00:00
|
|
|
sleep_msec = sys_sleep;
|
2002-06-03 19:11:48 +00:00
|
|
|
if (sleep_msec > 0) {
|
|
|
|
if (sleep_msec > 13)
|
|
|
|
sleep_msec = 13;
|
|
|
|
Sleep (sleep_msec);
|
|
|
|
}
|
2002-06-01 03:00:13 +00:00
|
|
|
|
2021-09-26 05:54:24 +00:00
|
|
|
usec = net_socket < 0 ? 0 : 20;
|
2002-06-03 19:11:48 +00:00
|
|
|
#else
|
2021-09-26 05:54:24 +00:00
|
|
|
usec = net_socket < 0 ? 0 : 2000;
|
2002-06-03 19:11:48 +00:00
|
|
|
#endif
|
|
|
|
// select on the net socket and stdin
|
|
|
|
// the only reason we have a timeout at all is so that if the last
|
|
|
|
// connected client times out, the message would not otherwise
|
|
|
|
// be printed until the next event.
|
2021-09-28 01:53:51 +00:00
|
|
|
QF_FD_ZERO (&fdset);
|
2004-03-26 02:52:47 +00:00
|
|
|
#ifndef _WIN32
|
2002-06-03 19:11:48 +00:00
|
|
|
if (do_stdin)
|
2021-09-28 01:53:51 +00:00
|
|
|
QF_FD_SET (0, &fdset);
|
2004-03-26 02:52:47 +00:00
|
|
|
#endif
|
2003-04-16 04:07:55 +00:00
|
|
|
if (net_socket >= 0)
|
2021-09-28 01:53:51 +00:00
|
|
|
QF_FD_SET (((unsigned) net_socket), &fdset);// cast needed for windows
|
2002-08-03 01:29:26 +00:00
|
|
|
|
[cvar] Make cvars properly typed
This is an extremely extensive patch as it hits every cvar, and every
usage of the cvars. Cvars no longer store the value they control,
instead, they use a cexpr value object to reference the value and
specify the value's type (currently, a null type is used for strings).
Non-string cvars are passed through cexpr, allowing expressions in the
cvars' settings. Also, cvars have returned to an enhanced version of the
original (id quake) registration scheme.
As a minor benefit, relevant code having direct access to the
cvar-controlled variables is probably a slight optimization as it
removed a pointer dereference, and the variables can be located for data
locality.
The static cvar descriptors are made private as an additional safety
layer, though there's nothing stopping external modification via
Cvar_FindVar (which is needed for adding listeners).
While not used yet (partly due to working out the design), cvars can
have a validation function.
Registering a cvar allows a primary listener (and its data) to be
specified: it will always be called first when the cvar is modified. The
combination of proper listeners and direct access to the controlled
variable greatly simplifies the more complex cvar interactions as much
less null checking is required, and there's no need for one cvar's
callback to call another's.
nq-x11 is known to work at least well enough for the demos. More testing
will come.
2022-04-23 03:22:45 +00:00
|
|
|
if (idle && sys_dead_sleep)
|
2021-09-26 05:54:24 +00:00
|
|
|
usec = -1;
|
2002-06-03 19:11:48 +00:00
|
|
|
|
2021-09-26 05:54:24 +00:00
|
|
|
res = Sys_Select (max (net_socket, 0), &fdset, usec);
|
2003-01-16 16:11:28 +00:00
|
|
|
if (res == 0 || res == -1)
|
2002-06-03 19:11:48 +00:00
|
|
|
return 0;
|
2004-03-26 02:52:47 +00:00
|
|
|
#ifndef _WIN32
|
2021-09-28 01:53:51 +00:00
|
|
|
stdin_ready = QF_FD_ISSET (0, &fdset);
|
2004-03-26 02:52:47 +00:00
|
|
|
#endif
|
2002-06-03 19:11:48 +00:00
|
|
|
return 1;
|
2002-06-01 03:00:13 +00:00
|
|
|
}
|
|
|
|
|
2002-06-03 19:11:48 +00:00
|
|
|
/*
|
|
|
|
Sys_ConsoleInput
|
2002-06-01 03:00:13 +00:00
|
|
|
|
2002-06-03 19:11:48 +00:00
|
|
|
Checks for a complete line of text typed in at the console, then forwards
|
|
|
|
it to the host command processor
|
|
|
|
*/
|
2007-03-10 12:00:59 +00:00
|
|
|
VISIBLE const char *
|
2002-06-03 19:11:48 +00:00
|
|
|
Sys_ConsoleInput (void)
|
2002-06-01 03:00:13 +00:00
|
|
|
{
|
2002-06-03 19:11:48 +00:00
|
|
|
static char text[256];
|
2004-01-22 03:43:09 +00:00
|
|
|
static int len = 0;
|
2002-06-03 19:11:48 +00:00
|
|
|
|
|
|
|
#ifdef _WIN32
|
|
|
|
int c;
|
|
|
|
|
|
|
|
// read a line out
|
2004-01-22 03:43:09 +00:00
|
|
|
while (_kbhit ()) {
|
2002-06-03 19:11:48 +00:00
|
|
|
c = _getch ();
|
|
|
|
putch (c);
|
|
|
|
if (c == '\r') {
|
|
|
|
text[len] = 0;
|
|
|
|
putch ('\n');
|
|
|
|
len = 0;
|
|
|
|
return text;
|
|
|
|
}
|
|
|
|
if (c == 8) {
|
|
|
|
if (len) {
|
|
|
|
putch (' ');
|
|
|
|
putch (c);
|
|
|
|
len--;
|
|
|
|
text[len] = 0;
|
|
|
|
}
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
text[len] = c;
|
|
|
|
len++;
|
2010-12-23 02:40:16 +00:00
|
|
|
if (len < (int) sizeof (text))
|
2010-11-21 04:55:03 +00:00
|
|
|
text[len] = 0;
|
|
|
|
else {
|
|
|
|
// buffer is full
|
2002-06-03 19:11:48 +00:00
|
|
|
len = 0;
|
2010-11-21 04:55:03 +00:00
|
|
|
text[sizeof (text) - 1] = 0;
|
|
|
|
return text;
|
|
|
|
}
|
2002-06-03 19:11:48 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return NULL;
|
|
|
|
#else
|
|
|
|
if (!stdin_ready || !do_stdin)
|
|
|
|
return NULL; // the select didn't say it was ready
|
|
|
|
stdin_ready = false;
|
|
|
|
|
|
|
|
len = read (0, text, sizeof (text));
|
|
|
|
if (len == 0) {
|
|
|
|
// end of file
|
|
|
|
do_stdin = 0;
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
if (len < 1)
|
|
|
|
return NULL;
|
|
|
|
text[len - 1] = 0; // rip off the \n and terminate
|
|
|
|
|
|
|
|
return text;
|
2002-06-01 03:00:13 +00:00
|
|
|
#endif
|
2002-06-03 19:11:48 +00:00
|
|
|
}
|
2002-08-20 23:04:57 +00:00
|
|
|
|
2022-09-19 15:36:29 +00:00
|
|
|
#ifdef _WIN32
|
2023-03-05 07:41:13 +00:00
|
|
|
static __thread jmp_buf aiee_abort;
|
2022-09-19 15:36:29 +00:00
|
|
|
#else
|
2023-03-05 07:41:13 +00:00
|
|
|
static __thread sigjmp_buf aiee_abort;
|
2022-09-19 15:36:29 +00:00
|
|
|
#endif
|
2002-08-20 23:04:57 +00:00
|
|
|
|
|
|
|
typedef struct sh_stack_s {
|
|
|
|
struct sh_stack_s *next;
|
2002-11-13 19:26:44 +00:00
|
|
|
int (*signal_hook)(int,void*);
|
2002-08-20 23:04:57 +00:00
|
|
|
void *data;
|
|
|
|
} sh_stack_t;
|
|
|
|
|
2023-03-05 07:41:13 +00:00
|
|
|
static __thread sh_stack_t *sh_stack;
|
|
|
|
static __thread sh_stack_t *free_sh;
|
|
|
|
static __thread int (*signal_hook)(int,void*);
|
|
|
|
static __thread void *signal_hook_data;
|
2002-08-20 23:04:57 +00:00
|
|
|
|
2007-03-10 12:00:59 +00:00
|
|
|
VISIBLE void
|
2002-11-13 19:26:44 +00:00
|
|
|
Sys_PushSignalHook (int (*hook)(int, void *), void *data)
|
2002-08-20 23:04:57 +00:00
|
|
|
{
|
|
|
|
sh_stack_t *s;
|
|
|
|
|
|
|
|
if (free_sh) {
|
|
|
|
s = free_sh;
|
|
|
|
} else {
|
|
|
|
s = malloc (sizeof (sh_stack_t));
|
|
|
|
s->next = 0;
|
|
|
|
}
|
|
|
|
s->signal_hook = signal_hook;
|
|
|
|
s->data = signal_hook_data;
|
|
|
|
signal_hook = hook;
|
|
|
|
signal_hook_data = data;
|
|
|
|
|
|
|
|
free_sh = s->next;
|
|
|
|
s->next = sh_stack;
|
|
|
|
sh_stack = s;
|
|
|
|
}
|
|
|
|
|
2007-03-10 12:00:59 +00:00
|
|
|
VISIBLE void
|
2002-08-20 23:04:57 +00:00
|
|
|
Sys_PopSignalHook (void)
|
|
|
|
{
|
|
|
|
if (sh_stack) {
|
|
|
|
sh_stack_t *s;
|
|
|
|
|
|
|
|
signal_hook = sh_stack->signal_hook;
|
|
|
|
signal_hook_data = sh_stack->data;
|
|
|
|
|
|
|
|
s = sh_stack->next;
|
|
|
|
sh_stack->next = free_sh;
|
|
|
|
free_sh = sh_stack;
|
|
|
|
sh_stack = s;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-03-23 06:27:42 +00:00
|
|
|
static void __attribute__((noreturn))
|
2002-08-20 23:04:57 +00:00
|
|
|
aiee (int sig)
|
|
|
|
{
|
|
|
|
printf ("AIEE, signal %d in shutdown code, giving up\n", sig);
|
2021-03-27 11:09:37 +00:00
|
|
|
#ifdef _WIN32
|
|
|
|
longjmp (aiee_abort, 1);
|
|
|
|
#else
|
2020-03-23 06:27:42 +00:00
|
|
|
siglongjmp (aiee_abort, 1);
|
2021-03-27 11:09:37 +00:00
|
|
|
#endif
|
2002-08-20 23:04:57 +00:00
|
|
|
}
|
|
|
|
|
2021-03-27 11:09:37 +00:00
|
|
|
#ifdef _WIN32
|
|
|
|
static void
|
|
|
|
signal_handler (int sig)
|
|
|
|
{
|
|
|
|
int volatile recover = 0; // volatile for longjump
|
|
|
|
static volatile int in_signal_handler = 0;
|
|
|
|
|
|
|
|
if (in_signal_handler) {
|
|
|
|
aiee (sig);
|
|
|
|
}
|
|
|
|
printf ("Received signal %d, exiting...\n", sig);
|
|
|
|
|
|
|
|
switch (sig) {
|
|
|
|
case SIGINT:
|
|
|
|
case SIGTERM:
|
|
|
|
signal (SIGINT, SIG_DFL);
|
|
|
|
signal (SIGTERM, SIG_DFL);
|
2021-11-05 00:56:08 +00:00
|
|
|
exit(1);
|
2021-03-27 11:09:37 +00:00
|
|
|
default:
|
|
|
|
if (!setjmp (aiee_abort)) {
|
|
|
|
if (signal_hook)
|
|
|
|
recover = signal_hook (sig, signal_hook_data);
|
|
|
|
Sys_Shutdown ();
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!recover) {
|
|
|
|
signal (SIGILL, SIG_DFL);
|
|
|
|
signal (SIGSEGV, SIG_DFL);
|
|
|
|
signal (SIGFPE, SIG_DFL);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
hook_signlas (void)
|
|
|
|
{
|
|
|
|
// catch signals
|
|
|
|
signal (SIGINT, signal_handler);
|
|
|
|
signal (SIGILL, signal_handler);
|
|
|
|
signal (SIGSEGV, signal_handler);
|
|
|
|
signal (SIGTERM, signal_handler);
|
|
|
|
signal (SIGFPE, signal_handler);
|
|
|
|
}
|
|
|
|
#else
|
|
|
|
|
2023-03-05 07:41:13 +00:00
|
|
|
static __thread struct sigaction save_hup;
|
|
|
|
static __thread struct sigaction save_quit;
|
|
|
|
static __thread struct sigaction save_trap;
|
|
|
|
static __thread struct sigaction save_iot;
|
|
|
|
static __thread struct sigaction save_bus;
|
|
|
|
static __thread struct sigaction save_int;
|
|
|
|
static __thread struct sigaction save_ill;
|
|
|
|
static __thread struct sigaction save_segv;
|
|
|
|
static __thread struct sigaction save_term;
|
|
|
|
static __thread struct sigaction save_fpe;
|
2021-03-27 11:09:37 +00:00
|
|
|
|
2002-08-20 23:04:57 +00:00
|
|
|
static void
|
2020-03-23 06:15:39 +00:00
|
|
|
signal_handler (int sig, siginfo_t *info, void *ucontext)
|
2002-08-20 23:04:57 +00:00
|
|
|
{
|
2002-11-14 15:37:09 +00:00
|
|
|
int volatile recover = 0; // volatile for longjump
|
2020-03-23 06:27:42 +00:00
|
|
|
static volatile int in_signal_handler = 0;
|
2002-11-13 19:26:44 +00:00
|
|
|
|
2020-03-23 06:27:42 +00:00
|
|
|
if (in_signal_handler) {
|
|
|
|
aiee (sig);
|
|
|
|
}
|
2002-08-20 23:04:57 +00:00
|
|
|
printf ("Received signal %d, exiting...\n", sig);
|
|
|
|
|
|
|
|
switch (sig) {
|
|
|
|
case SIGINT:
|
|
|
|
case SIGTERM:
|
2002-08-21 01:51:27 +00:00
|
|
|
case SIGHUP:
|
2021-11-05 00:56:08 +00:00
|
|
|
case SIGQUIT:
|
2020-03-23 06:27:42 +00:00
|
|
|
sigaction (SIGHUP, &save_hup, 0);
|
|
|
|
sigaction (SIGINT, &save_int, 0);
|
|
|
|
sigaction (SIGTERM, &save_term, 0);
|
2021-11-05 00:56:08 +00:00
|
|
|
sigaction (SIGQUIT, &save_quit, 0);
|
|
|
|
exit(1);
|
2002-08-20 23:04:57 +00:00
|
|
|
default:
|
2020-03-23 06:27:42 +00:00
|
|
|
if (!sigsetjmp (aiee_abort, 1)) {
|
2002-08-20 23:04:57 +00:00
|
|
|
if (signal_hook)
|
2002-11-13 19:26:44 +00:00
|
|
|
recover = signal_hook (sig, signal_hook_data);
|
2002-08-20 23:04:57 +00:00
|
|
|
Sys_Shutdown ();
|
|
|
|
}
|
|
|
|
|
2020-03-23 06:15:39 +00:00
|
|
|
if (!recover) {
|
|
|
|
sigaction (SIGTRAP, &save_trap, 0);
|
|
|
|
sigaction (SIGIOT, &save_iot, 0);
|
|
|
|
sigaction (SIGBUS, &save_bus, 0);
|
|
|
|
sigaction (SIGILL, &save_ill, 0);
|
|
|
|
sigaction (SIGSEGV, &save_segv, 0);
|
|
|
|
sigaction (SIGFPE, &save_fpe, 0);
|
2002-11-13 19:26:44 +00:00
|
|
|
}
|
2002-08-20 23:04:57 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-03-27 11:09:37 +00:00
|
|
|
static void
|
|
|
|
hook_signlas (void)
|
2002-08-20 23:04:57 +00:00
|
|
|
{
|
|
|
|
// catch signals
|
2020-03-23 06:15:39 +00:00
|
|
|
struct sigaction action = {};
|
|
|
|
action.sa_flags = SA_SIGINFO;
|
|
|
|
action.sa_sigaction = signal_handler;
|
2002-08-21 01:51:27 +00:00
|
|
|
#ifndef _WIN32
|
2020-03-23 06:15:39 +00:00
|
|
|
sigaction (SIGHUP, &action, &save_hup);
|
|
|
|
sigaction (SIGQUIT, &action, &save_quit);
|
|
|
|
sigaction (SIGTRAP, &action, &save_trap);
|
|
|
|
sigaction (SIGIOT, &action, &save_iot);
|
|
|
|
sigaction (SIGBUS, &action, &save_bus);
|
2002-08-21 01:51:27 +00:00
|
|
|
#endif
|
2020-03-23 06:15:39 +00:00
|
|
|
sigaction (SIGINT, &action, &save_int);
|
|
|
|
sigaction (SIGILL, &action, &save_ill);
|
|
|
|
sigaction (SIGSEGV, &action, &save_segv);
|
|
|
|
sigaction (SIGTERM, &action, &save_term);
|
|
|
|
sigaction (SIGFPE, &action, &save_fpe);
|
2021-03-27 11:09:37 +00:00
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
|
|
|
VISIBLE void
|
|
|
|
Sys_Init (void)
|
|
|
|
{
|
|
|
|
hook_signlas ();
|
2011-09-11 05:56:47 +00:00
|
|
|
|
|
|
|
Cvar_Init_Hash ();
|
|
|
|
Cmd_Init_Hash ();
|
|
|
|
Cvar_Init ();
|
|
|
|
Sys_Init_Cvars ();
|
|
|
|
|
|
|
|
Cmd_Init ();
|
2002-08-20 23:04:57 +00:00
|
|
|
}
|
2010-08-24 23:21:07 +00:00
|
|
|
|
|
|
|
VISIBLE int
|
|
|
|
Sys_CreatePath (const char *path)
|
|
|
|
{
|
|
|
|
char *ofs;
|
|
|
|
char *e_path = alloca (strlen (path) + 1);
|
|
|
|
|
|
|
|
strcpy (e_path, path);
|
|
|
|
for (ofs = e_path + 1; *ofs; ofs++) {
|
2013-02-20 05:03:03 +00:00
|
|
|
if (*ofs == '/') {
|
2010-08-24 23:21:07 +00:00
|
|
|
*ofs = 0;
|
2013-02-20 05:03:03 +00:00
|
|
|
if (!Sys_isdir (e_path)) {
|
|
|
|
// create the directory
|
|
|
|
if (Sys_mkdir (e_path) == -1)
|
|
|
|
return -1;
|
|
|
|
}
|
2010-08-24 23:21:07 +00:00
|
|
|
*ofs = '/';
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return 0;
|
|
|
|
}
|
2010-08-24 07:01:01 +00:00
|
|
|
|
|
|
|
char *
|
|
|
|
Sys_ExpandSquiggle (const char *path)
|
|
|
|
{
|
2013-01-23 02:10:19 +00:00
|
|
|
const char *home = 0;
|
|
|
|
#ifdef _WIN32
|
|
|
|
char userpath[MAX_PATH]; // sigh, why can't windows give the size?
|
|
|
|
#else
|
2012-08-18 12:37:54 +00:00
|
|
|
# ifdef HAVE_GETUID
|
2010-08-24 07:01:01 +00:00
|
|
|
struct passwd *pwd_ent;
|
2012-08-18 12:37:54 +00:00
|
|
|
# endif
|
2010-08-24 07:01:01 +00:00
|
|
|
#endif
|
|
|
|
|
|
|
|
if (strncmp (path, "~/", 2) != 0) {
|
|
|
|
return strdup (path);
|
|
|
|
}
|
|
|
|
|
|
|
|
#ifdef _WIN32
|
2013-01-23 02:10:19 +00:00
|
|
|
if (SHGetFolderPathA (0, CSIDL_LOCAL_APPDATA, 0, 0, userpath) == S_OK) {
|
|
|
|
home = userpath;
|
|
|
|
}
|
2010-08-24 07:01:01 +00:00
|
|
|
// LordHavoc: first check HOME to duplicate previous version behavior
|
|
|
|
// (also handy if someone wants it elsewhere than their windows directory)
|
2013-01-23 02:10:19 +00:00
|
|
|
if (!home)
|
|
|
|
home = getenv ("HOME");
|
|
|
|
if (!home || !home[0])
|
|
|
|
home = getenv ("USERPROFILE");
|
2010-08-24 07:01:01 +00:00
|
|
|
if (!home || !home[0])
|
2013-01-23 02:10:19 +00:00
|
|
|
home = 0;
|
2010-08-24 07:01:01 +00:00
|
|
|
#else
|
2012-08-18 12:37:54 +00:00
|
|
|
# ifdef HAVE_GETUID
|
2010-08-24 07:01:01 +00:00
|
|
|
if ((pwd_ent = getpwuid (getuid ()))) {
|
|
|
|
home = pwd_ent->pw_dir;
|
|
|
|
} else
|
|
|
|
home = getenv ("HOME");
|
2012-08-18 12:37:54 +00:00
|
|
|
# endif
|
2010-08-24 07:01:01 +00:00
|
|
|
#endif
|
|
|
|
|
2013-01-23 02:10:19 +00:00
|
|
|
if (!home)
|
|
|
|
home = ".";
|
|
|
|
return nva ("%s%s", home, path + 1); // skip leading ~
|
2010-08-24 07:01:01 +00:00
|
|
|
}
|
2022-03-31 07:31:13 +00:00
|
|
|
|
|
|
|
VISIBLE int
|
|
|
|
Sys_UniqueFile (dstring_t *name, const char *prefix, const char *suffix,
|
|
|
|
int mindigits)
|
|
|
|
{
|
|
|
|
const int flags = O_CREAT | O_EXCL | O_RDWR;
|
|
|
|
const int mode = 0644;
|
|
|
|
int64_t seq = 0; // it should take a while to run out
|
|
|
|
|
|
|
|
if (!suffix) {
|
|
|
|
suffix = "";
|
|
|
|
}
|
|
|
|
while (1) {
|
|
|
|
dsprintf (name, "%s%0*"PRIi64"%s", prefix, mindigits, seq, suffix);
|
|
|
|
int fd = open (name->str, flags, mode);
|
|
|
|
if (fd >= 0) {
|
|
|
|
return fd;
|
|
|
|
}
|
|
|
|
int err = errno;
|
|
|
|
if (err != EEXIST) {
|
|
|
|
dsprintf (name, "%s", strerror (err));
|
|
|
|
return -err;
|
|
|
|
}
|
|
|
|
seq++;
|
|
|
|
}
|
|
|
|
}
|
2023-03-05 08:04:52 +00:00
|
|
|
|
|
|
|
void
|
|
|
|
Sys_Shutdown (void)
|
|
|
|
{
|
|
|
|
shutdown_list_t *t;
|
|
|
|
|
|
|
|
while (shutdown_list) {
|
|
|
|
void (*func) (void *) = shutdown_list->func;
|
|
|
|
void *data = shutdown_list->data;
|
|
|
|
t = shutdown_list;
|
|
|
|
shutdown_list = shutdown_list->next;
|
|
|
|
free (t);
|
|
|
|
|
|
|
|
func (data);
|
|
|
|
}
|
|
|
|
while (free_sh) {
|
|
|
|
__auto_type t = free_sh->next;
|
|
|
|
free (free_sh);
|
|
|
|
free_sh = t;
|
|
|
|
}
|
|
|
|
while (sh_stack) {
|
|
|
|
__auto_type t = sh_stack->next;
|
|
|
|
free (sh_stack);
|
|
|
|
sh_stack = t;
|
|
|
|
}
|
|
|
|
while (error_handler_freelist) {
|
|
|
|
__auto_type t = error_handler_freelist->next;
|
|
|
|
free (error_handler_freelist);
|
|
|
|
error_handler_freelist = t;
|
|
|
|
}
|
|
|
|
while (error_handler) {
|
|
|
|
__auto_type t = error_handler->next;
|
|
|
|
free (error_handler);
|
|
|
|
error_handler = t;
|
|
|
|
}
|
|
|
|
if (sys_print_msg) {
|
|
|
|
dstring_delete (sys_print_msg);
|
|
|
|
}
|
|
|
|
if (sys_debuglog_data) {
|
|
|
|
dstring_delete (sys_debuglog_data);
|
|
|
|
}
|
|
|
|
}
|