2001-09-28 07:09:38 +00:00
|
|
|
/*
|
2021-12-20 07:09:45 +00:00
|
|
|
main.c
|
2001-09-28 07:09:38 +00:00
|
|
|
|
2020-03-20 15:01:36 +00:00
|
|
|
Qwaq
|
2001-09-28 07:09:38 +00:00
|
|
|
|
2020-03-20 15:01:36 +00:00
|
|
|
Copyright (C) 2001 Bill Currie
|
2001-09-28 07:09:38 +00:00
|
|
|
|
2020-03-20 15:01:36 +00:00
|
|
|
Author: Bill Currie <bill@taniwha.org>
|
|
|
|
Date: 2001/06/01
|
2001-09-28 07:09:38 +00:00
|
|
|
|
|
|
|
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
|
|
|
|
|
|
|
|
*/
|
2003-01-14 20:18:29 +00:00
|
|
|
#ifdef HAVE_CONFIG_H
|
|
|
|
# include "config.h"
|
|
|
|
#endif
|
|
|
|
|
2001-06-01 21:57:59 +00:00
|
|
|
#include <stdlib.h>
|
2020-03-08 13:19:37 +00:00
|
|
|
#include <stdio.h>
|
2020-03-20 06:47:30 +00:00
|
|
|
#include <string.h>
|
2020-03-20 15:01:36 +00:00
|
|
|
#include <pthread.h>
|
2020-03-08 13:19:37 +00:00
|
|
|
#include <errno.h>
|
2020-03-20 06:47:30 +00:00
|
|
|
#include <getopt.h>
|
2001-06-01 21:57:59 +00:00
|
|
|
|
2020-02-26 13:10:59 +00:00
|
|
|
#include "QF/cbuf.h"
|
|
|
|
#include "QF/cmd.h"
|
|
|
|
#include "QF/cvar.h"
|
|
|
|
#include "QF/gib.h"
|
|
|
|
#include "QF/idparse.h"
|
2020-03-22 04:47:44 +00:00
|
|
|
#include "QF/keys.h"
|
2020-02-26 13:10:59 +00:00
|
|
|
#include "QF/progs.h"
|
|
|
|
#include "QF/qargs.h"
|
|
|
|
#include "QF/quakefs.h"
|
2004-01-16 08:02:31 +00:00
|
|
|
#include "QF/ruamoko.h"
|
2020-02-26 13:10:59 +00:00
|
|
|
#include "QF/sys.h"
|
2002-11-12 02:30:08 +00:00
|
|
|
#include "QF/va.h"
|
2020-02-26 13:10:59 +00:00
|
|
|
#include "QF/zone.h"
|
2001-06-01 21:57:59 +00:00
|
|
|
|
2021-03-27 11:09:37 +00:00
|
|
|
#include "compat.h"
|
2022-02-01 00:24:02 +00:00
|
|
|
#include "rua_internal.h"
|
2021-03-27 11:09:37 +00:00
|
|
|
|
2020-06-21 14:15:17 +00:00
|
|
|
#include "ruamoko/qwaq/qwaq.h"
|
|
|
|
#include "ruamoko/qwaq/debugger/debug.h"
|
2003-01-06 18:28:13 +00:00
|
|
|
|
2001-06-05 23:53:55 +00:00
|
|
|
#define MAX_EDICTS 1024
|
|
|
|
|
2020-02-26 13:10:59 +00:00
|
|
|
cbuf_t *qwaq_cbuf;
|
|
|
|
|
2020-03-20 06:47:30 +00:00
|
|
|
const char *this_program;
|
|
|
|
|
|
|
|
enum {
|
|
|
|
start_opts = 255,
|
|
|
|
OPT_QARGS,
|
|
|
|
};
|
|
|
|
|
|
|
|
static struct option const long_options[] = {
|
|
|
|
{"qargs", no_argument, 0, OPT_QARGS},
|
|
|
|
{NULL, 0, NULL, 0},
|
|
|
|
};
|
|
|
|
|
|
|
|
static const char *short_options =
|
|
|
|
"-" // magic option parsing mode doohicky (must come first)
|
|
|
|
;
|
|
|
|
|
2021-07-06 02:55:29 +00:00
|
|
|
qwaq_thread_set_t thread_data;
|
2020-03-20 06:47:30 +00:00
|
|
|
|
2002-11-12 02:30:08 +00:00
|
|
|
static QFile *
|
|
|
|
open_file (const char *path, int *len)
|
|
|
|
{
|
|
|
|
QFile *file = Qopen (path, "rbz");
|
2020-03-22 04:49:42 +00:00
|
|
|
char errbuff[1024];
|
2001-06-01 21:57:59 +00:00
|
|
|
|
2002-11-12 02:30:08 +00:00
|
|
|
if (!file) {
|
2020-03-22 04:49:42 +00:00
|
|
|
strerror_r(errno, errbuff, sizeof (errbuff));
|
|
|
|
Sys_Printf ("%s\n", errbuff);
|
2020-12-20 17:12:51 +00:00
|
|
|
*len = 0;
|
2002-11-12 02:30:08 +00:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
*len = Qfilesize (file);
|
|
|
|
return file;
|
|
|
|
}
|
2001-06-06 20:05:08 +00:00
|
|
|
|
2002-11-12 02:30:08 +00:00
|
|
|
static void *
|
2020-02-21 12:17:28 +00:00
|
|
|
load_file (progs_t *pr, const char *name, off_t *_size)
|
2001-06-01 21:57:59 +00:00
|
|
|
{
|
2002-11-12 02:30:08 +00:00
|
|
|
QFile *file;
|
|
|
|
int size;
|
2004-11-02 05:12:00 +00:00
|
|
|
char *sym;
|
2002-11-12 02:30:08 +00:00
|
|
|
|
|
|
|
file = open_file (name, &size);
|
|
|
|
if (!file) {
|
2021-01-31 07:01:20 +00:00
|
|
|
file = open_file (va (0, "%s.gz", name), &size);
|
2002-11-12 02:30:08 +00:00
|
|
|
if (!file) {
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
}
|
2004-11-02 05:12:00 +00:00
|
|
|
sym = malloc (size + 1);
|
|
|
|
sym[size] = 0;
|
2002-11-12 02:30:08 +00:00
|
|
|
Qread (file, sym, size);
|
2022-02-14 05:41:14 +00:00
|
|
|
Qclose (file);
|
2020-02-21 12:17:28 +00:00
|
|
|
*_size = size;
|
2002-11-12 02:30:08 +00:00
|
|
|
return sym;
|
|
|
|
}
|
|
|
|
|
|
|
|
static void *
|
|
|
|
allocate_progs_mem (progs_t *pr, int size)
|
|
|
|
{
|
2022-03-05 05:17:48 +00:00
|
|
|
#ifdef _WIN32
|
|
|
|
return _aligned_malloc (size, 64);
|
|
|
|
#else
|
2022-01-31 14:40:21 +00:00
|
|
|
return aligned_alloc (64, size);
|
2022-03-05 05:17:48 +00:00
|
|
|
#endif
|
2002-11-12 02:30:08 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
free_progs_mem (progs_t *pr, void *mem)
|
|
|
|
{
|
2022-03-05 05:17:48 +00:00
|
|
|
#ifdef _WIN32
|
|
|
|
_aligned_free (mem);
|
|
|
|
#else
|
2002-11-12 02:30:08 +00:00
|
|
|
free (mem);
|
2022-03-05 05:17:48 +00:00
|
|
|
#endif
|
2002-11-12 02:30:08 +00:00
|
|
|
}
|
2001-06-01 21:57:59 +00:00
|
|
|
|
2002-11-12 02:30:08 +00:00
|
|
|
static void
|
|
|
|
init_qf (void)
|
|
|
|
{
|
2020-02-26 13:10:59 +00:00
|
|
|
qwaq_cbuf = Cbuf_New (&id_interp);
|
|
|
|
|
2011-09-11 05:56:47 +00:00
|
|
|
Sys_Init ();
|
2021-04-12 13:09:09 +00:00
|
|
|
COM_ParseConfig (qwaq_cbuf);
|
2020-02-26 13:10:59 +00:00
|
|
|
|
|
|
|
//Cvar_Set (developer, "1");
|
2001-06-01 21:57:59 +00:00
|
|
|
|
2021-02-03 04:19:19 +00:00
|
|
|
Memory_Init (Sys_Alloc (8 * 1024 * 1024), 8 * 1024 * 1024);
|
2022-04-27 09:07:30 +00:00
|
|
|
PR_Init_Cvars ();
|
2020-03-20 01:13:37 +00:00
|
|
|
}
|
|
|
|
|
2021-07-06 02:55:29 +00:00
|
|
|
static void
|
2022-02-14 03:28:38 +00:00
|
|
|
bi_printf (progs_t *pr, void *_res)
|
2021-07-06 02:55:29 +00:00
|
|
|
{
|
|
|
|
dstring_t *dstr = dstring_new ();
|
|
|
|
|
2022-02-05 11:24:17 +00:00
|
|
|
RUA_Sprintf (pr, dstr, "printf", 0);
|
2021-07-06 02:55:29 +00:00
|
|
|
if (dstr->str) {
|
|
|
|
Sys_Printf ("%s", dstr->str);
|
|
|
|
}
|
|
|
|
dstring_delete (dstr);
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
2022-02-14 03:28:38 +00:00
|
|
|
bi_traceon (progs_t *pr, void *_res)
|
2021-07-06 02:55:29 +00:00
|
|
|
{
|
|
|
|
pr->pr_trace = true;
|
|
|
|
pr->pr_trace_depth = pr->pr_depth;
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
2022-02-14 03:28:38 +00:00
|
|
|
bi_traceoff (progs_t *pr, void *_res)
|
2021-07-06 02:55:29 +00:00
|
|
|
{
|
|
|
|
pr->pr_trace = false;
|
|
|
|
}
|
|
|
|
|
2022-01-23 12:54:03 +00:00
|
|
|
#define bi(x,n,np,params...) {#x, bi_##x, n, np, {params}}
|
|
|
|
#define p(type) PR_PARAM(type)
|
2021-07-06 02:55:29 +00:00
|
|
|
static builtin_t common_builtins[] = {
|
2022-01-23 12:54:03 +00:00
|
|
|
bi(printf, -1, -2, p(string)),
|
|
|
|
bi(traceon, -1, 0),
|
|
|
|
bi(traceoff, -1, 0),
|
2021-07-06 02:55:29 +00:00
|
|
|
{},
|
|
|
|
};
|
|
|
|
|
|
|
|
static void
|
|
|
|
common_builtins_init (progs_t *pr)
|
|
|
|
{
|
2022-01-23 15:20:05 +00:00
|
|
|
PR_RegisterBuiltins (pr, common_builtins, 0);
|
2021-07-06 02:55:29 +00:00
|
|
|
}
|
|
|
|
|
2021-12-20 11:13:21 +00:00
|
|
|
static void
|
|
|
|
qwaq_thread_clear (progs_t *pr, void *_thread)
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
2022-05-12 09:23:32 +00:00
|
|
|
static void
|
|
|
|
qwaq_thread_destroy (progs_t *pr, void *_res)
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
2020-03-20 01:13:37 +00:00
|
|
|
static progs_t *
|
2020-04-01 15:11:47 +00:00
|
|
|
create_progs (qwaq_thread_t *thread)
|
2020-03-20 01:13:37 +00:00
|
|
|
{
|
|
|
|
progs_t *pr = calloc (1, sizeof (*pr));
|
2020-04-01 15:11:47 +00:00
|
|
|
progsinit_f *funcs = thread->progsinit;
|
2020-03-20 01:13:37 +00:00
|
|
|
|
|
|
|
pr->load_file = load_file;
|
|
|
|
pr->allocate_progs_mem = allocate_progs_mem;
|
|
|
|
pr->free_progs_mem = free_progs_mem;
|
|
|
|
pr->no_exec_limit = 1;
|
2022-05-12 08:54:23 +00:00
|
|
|
pr->hashctx = &thread->hashctx;
|
2001-07-14 23:52:56 +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
|
|
|
pr_debug = 2;
|
|
|
|
pr_boundscheck = 0;
|
2020-03-20 01:13:37 +00:00
|
|
|
PR_Init (pr);
|
2022-05-12 09:23:32 +00:00
|
|
|
PR_Resources_Register (pr, "qwaq_thread", thread, qwaq_thread_clear,
|
|
|
|
qwaq_thread_destroy);
|
2021-12-20 11:13:21 +00:00
|
|
|
RUA_Init (pr, thread->rua_security);
|
2021-07-06 02:55:29 +00:00
|
|
|
common_builtins_init (pr);
|
2020-03-24 11:15:35 +00:00
|
|
|
while (*funcs) {
|
|
|
|
(*funcs++) (pr);
|
|
|
|
}
|
2020-03-20 01:13:37 +00:00
|
|
|
|
|
|
|
return pr;
|
2002-11-12 02:30:08 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
static int
|
2020-03-20 01:13:37 +00:00
|
|
|
load_progs (progs_t *pr, const char *name)
|
2002-11-12 02:30:08 +00:00
|
|
|
{
|
|
|
|
QFile *file;
|
|
|
|
int size;
|
|
|
|
|
|
|
|
file = open_file (name, &size);
|
|
|
|
if (!file) {
|
|
|
|
return 0;
|
2001-06-01 21:57:59 +00:00
|
|
|
}
|
2020-03-20 01:13:37 +00:00
|
|
|
pr->progs_name = name;
|
|
|
|
pr->max_edicts = 1;
|
2022-02-01 03:45:18 +00:00
|
|
|
pr->zone_size = 2*1024*1024;
|
2022-02-01 03:24:46 +00:00
|
|
|
pr->stack_size = 64*1024;
|
2020-03-20 01:13:37 +00:00
|
|
|
PR_LoadProgsFile (pr, file, size);
|
2002-11-12 02:30:08 +00:00
|
|
|
Qclose (file);
|
2020-03-20 01:13:37 +00:00
|
|
|
if (!PR_RunLoadFuncs (pr))
|
|
|
|
PR_Error (pr, "unable to load %s", pr->progs_name);
|
2002-11-12 02:30:08 +00:00
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
2020-03-20 07:59:59 +00:00
|
|
|
static void
|
2020-04-01 15:11:47 +00:00
|
|
|
spawn_progs (qwaq_thread_t *thread)
|
2020-03-20 07:59:59 +00:00
|
|
|
{
|
|
|
|
dfunction_t *dfunc;
|
2020-03-20 08:30:26 +00:00
|
|
|
const char *name = 0;
|
2022-01-18 03:11:14 +00:00
|
|
|
pr_string_t *pr_argv;
|
2020-03-20 07:59:59 +00:00
|
|
|
int pr_argc = 1, i;
|
|
|
|
progs_t *pr;
|
|
|
|
|
|
|
|
thread->main_func = 0;
|
2020-04-01 15:11:47 +00:00
|
|
|
pr = thread->pr = create_progs (thread);
|
2020-03-20 07:59:59 +00:00
|
|
|
if (thread->args.size) {
|
|
|
|
name = thread->args.a[0];
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!load_progs (pr, name)) {
|
|
|
|
Sys_Error ("couldn't load %s", name);
|
|
|
|
}
|
|
|
|
|
2020-04-02 08:28:37 +00:00
|
|
|
if ((dfunc = PR_FindFunction (pr, ".main"))
|
|
|
|
|| (dfunc = PR_FindFunction (pr, "main"))) {
|
|
|
|
thread->main_func = dfunc - pr->pr_functions;
|
|
|
|
} else {
|
|
|
|
PR_Undefined (pr, "function", "main");
|
|
|
|
}
|
|
|
|
|
2020-04-02 08:44:53 +00:00
|
|
|
if (thread->pr->debug_handler) {
|
|
|
|
thread->pr->debug_handler (prd_begin, &thread->main_func,
|
|
|
|
thread->pr->debug_data);
|
|
|
|
}
|
|
|
|
|
2020-04-02 08:28:37 +00:00
|
|
|
if (!PR_RunPostLoadFuncs (pr)) {
|
|
|
|
PR_Error (pr, "unable to load %s", pr->progs_name);
|
|
|
|
}
|
|
|
|
|
2020-03-20 07:59:59 +00:00
|
|
|
PR_PushFrame (pr);
|
2020-06-28 09:30:01 +00:00
|
|
|
if (thread->args.size) {
|
|
|
|
pr_argc = thread->args.size;
|
2020-03-20 07:59:59 +00:00
|
|
|
}
|
|
|
|
pr_argv = PR_Zone_Malloc (pr, (pr_argc + 1) * 4);
|
|
|
|
pr_argv[0] = PR_SetTempString (pr, name);
|
2021-06-04 04:35:53 +00:00
|
|
|
for (i = 1; i < pr_argc; i++) {
|
2020-06-28 09:30:01 +00:00
|
|
|
pr_argv[i] = PR_SetTempString (pr, thread->args.a[i]);
|
2021-06-04 04:35:53 +00:00
|
|
|
}
|
2020-03-20 07:59:59 +00:00
|
|
|
pr_argv[i] = 0;
|
|
|
|
|
|
|
|
PR_RESET_PARAMS (pr);
|
|
|
|
P_INT (pr, 0) = pr_argc;
|
|
|
|
P_POINTER (pr, 1) = PR_SetPointer (pr, pr_argv);
|
|
|
|
pr->pr_argc = 2;
|
|
|
|
}
|
|
|
|
|
2020-03-20 15:01:36 +00:00
|
|
|
static void *
|
|
|
|
run_progs (void *data)
|
2020-03-20 07:59:59 +00:00
|
|
|
{
|
2020-03-20 15:01:36 +00:00
|
|
|
__auto_type thread = (qwaq_thread_t *) data;
|
|
|
|
|
2020-04-01 15:11:47 +00:00
|
|
|
spawn_progs (thread);
|
2021-07-06 03:27:21 +00:00
|
|
|
//Sys_Printf ("starting thread for %s\n", thread->args.a[0]);
|
2020-04-01 15:11:47 +00:00
|
|
|
|
2020-03-20 07:59:59 +00:00
|
|
|
PR_ExecuteProgram (thread->pr, thread->main_func);
|
|
|
|
PR_PopFrame (thread->pr);
|
2020-04-02 01:44:16 +00:00
|
|
|
thread->return_code = R_INT (thread->pr);
|
2020-03-25 09:28:28 +00:00
|
|
|
if (thread->pr->debug_handler) {
|
2020-04-02 01:44:16 +00:00
|
|
|
thread->pr->debug_handler (prd_terminate, &thread->return_code,
|
|
|
|
thread->pr->debug_data);
|
2020-03-25 09:28:28 +00:00
|
|
|
}
|
2020-03-20 15:01:36 +00:00
|
|
|
return thread;
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
start_progs_thread (qwaq_thread_t *thread)
|
|
|
|
{
|
|
|
|
pthread_create (&thread->thread_id, 0, run_progs, thread);
|
|
|
|
}
|
|
|
|
|
|
|
|
qwaq_thread_t *
|
2020-03-20 15:14:33 +00:00
|
|
|
create_thread (void *(*thread_func) (qwaq_thread_t *), void *data)
|
2020-03-20 15:01:36 +00:00
|
|
|
{
|
|
|
|
qwaq_thread_t *thread = calloc (1, sizeof (*thread));
|
|
|
|
|
2020-03-20 15:14:33 +00:00
|
|
|
thread->data = data;
|
2020-03-20 15:01:36 +00:00
|
|
|
DARRAY_APPEND (&thread_data, thread);
|
2020-03-20 15:14:33 +00:00
|
|
|
pthread_create (&thread->thread_id, 0,
|
|
|
|
(void*(*)(void*))thread_func, thread);
|
2020-03-20 15:01:36 +00:00
|
|
|
return thread;
|
2020-03-20 07:59:59 +00:00
|
|
|
}
|
|
|
|
|
2020-03-20 06:47:30 +00:00
|
|
|
static void
|
|
|
|
usage (int status)
|
|
|
|
{
|
|
|
|
printf ("%s - QuakeForge runtime\n", this_program);
|
|
|
|
printf ("sorry, no help yet\n");
|
|
|
|
exit (status);
|
|
|
|
}
|
|
|
|
|
|
|
|
static int
|
|
|
|
parse_argset (int argc, char **argv)
|
|
|
|
{
|
2020-03-20 14:17:06 +00:00
|
|
|
qwaq_thread_t *thread = calloc (1, sizeof (*thread));
|
|
|
|
DARRAY_INIT (&thread->args, 8);
|
2020-03-20 06:47:30 +00:00
|
|
|
|
2020-03-20 14:17:06 +00:00
|
|
|
DARRAY_APPEND (&thread->args, 0);
|
2020-03-20 06:47:30 +00:00
|
|
|
while (optind < argc && strcmp (argv[optind], "--")) {
|
2020-03-20 14:17:06 +00:00
|
|
|
DARRAY_APPEND (&thread->args, argv[optind++]);
|
2020-03-20 06:47:30 +00:00
|
|
|
}
|
|
|
|
if (optind < argc) {
|
|
|
|
optind++;
|
|
|
|
}
|
2020-03-20 12:16:56 +00:00
|
|
|
DARRAY_APPEND (&thread_data, thread);
|
|
|
|
return thread_data.size - 1;
|
2020-03-20 06:47:30 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
static int
|
|
|
|
parse_args (int argc, char **argv)
|
|
|
|
{
|
|
|
|
int c;
|
2020-03-20 14:17:06 +00:00
|
|
|
qwaq_thread_t *main_thread = calloc (1, sizeof (*main_thread));
|
2020-03-20 06:47:30 +00:00
|
|
|
int qargs_ind = -1;
|
|
|
|
|
2020-03-20 14:17:06 +00:00
|
|
|
DARRAY_INIT (&main_thread->args, 8);
|
2020-03-20 06:47:30 +00:00
|
|
|
|
|
|
|
while ((c = getopt_long (argc, argv,
|
|
|
|
short_options, long_options, 0)) != -1) {
|
|
|
|
switch (c) {
|
|
|
|
case 1:
|
2020-03-20 14:17:06 +00:00
|
|
|
DARRAY_APPEND (&main_thread->args, argv[optind - 1]);
|
2020-03-20 06:47:30 +00:00
|
|
|
break;
|
|
|
|
case OPT_QARGS:
|
|
|
|
if (qargs_ind < 0) {
|
|
|
|
qargs_ind = parse_argset (argc, argv);
|
2020-03-20 14:17:06 +00:00
|
|
|
thread_data.a[qargs_ind]->args.a[0] = "--qargs";
|
2020-03-20 06:47:30 +00:00
|
|
|
goto done;
|
|
|
|
} else {
|
|
|
|
printf ("more than one set of qargs given");
|
|
|
|
exit (1);
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
usage (1);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
done:
|
|
|
|
|
2020-03-20 14:17:06 +00:00
|
|
|
free (thread_data.a[0]->args.a);
|
|
|
|
free (thread_data.a[0]);
|
2020-03-20 12:16:56 +00:00
|
|
|
thread_data.a[0] = main_thread;
|
2020-03-20 06:47:30 +00:00
|
|
|
|
|
|
|
while (optind < argc) {
|
|
|
|
parse_argset (argc, argv);
|
|
|
|
}
|
|
|
|
return qargs_ind;
|
|
|
|
}
|
|
|
|
|
2020-03-24 11:15:35 +00:00
|
|
|
|
2002-11-12 02:30:08 +00:00
|
|
|
int
|
2020-03-20 06:47:30 +00:00
|
|
|
main (int argc, char **argv)
|
2002-11-12 02:30:08 +00:00
|
|
|
{
|
2020-03-20 07:59:59 +00:00
|
|
|
int qargs_ind = -1;
|
2020-03-20 08:30:26 +00:00
|
|
|
int main_ind = -1;
|
|
|
|
int ret = 0;
|
|
|
|
size_t num_sys = 1;
|
2002-11-12 02:30:08 +00:00
|
|
|
|
2020-03-20 06:47:30 +00:00
|
|
|
this_program = argv[0];
|
|
|
|
|
2020-03-20 12:16:56 +00:00
|
|
|
DARRAY_INIT (&thread_data, 4);
|
2020-03-20 06:47:30 +00:00
|
|
|
for (optind = 1; optind < argc; ) {
|
|
|
|
parse_argset (argc, argv);
|
|
|
|
}
|
2020-03-20 12:16:56 +00:00
|
|
|
if (thread_data.size) {
|
2020-03-20 14:17:06 +00:00
|
|
|
qwaq_thread_t *thread = thread_data.a[0];
|
2020-03-20 06:47:30 +00:00
|
|
|
// the first arg is initialized to null, but this is for getopt, so
|
|
|
|
// set to main program name
|
|
|
|
thread->args.a[0] = this_program;
|
|
|
|
optind = 0;
|
|
|
|
qargs_ind = parse_args (thread->args.size, (char **) thread->args.a);
|
|
|
|
} else {
|
|
|
|
// create a blank main thread set
|
2020-03-20 14:17:06 +00:00
|
|
|
qwaq_thread_t *thread = calloc (1, sizeof (*thread));
|
|
|
|
DARRAY_INIT (&thread->args, 4);
|
2020-03-20 12:16:56 +00:00
|
|
|
DARRAY_APPEND (&thread_data, thread);
|
2020-03-20 06:47:30 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if (qargs_ind >= 0) {
|
2020-03-20 14:17:06 +00:00
|
|
|
qwaq_thread_t *qargs = thread_data.a[qargs_ind];
|
2020-03-20 06:47:30 +00:00
|
|
|
// the first arg is initialized to --qargs, so
|
|
|
|
// set to main program name for now
|
2020-03-20 07:59:59 +00:00
|
|
|
qargs->args.a[0] = this_program;
|
|
|
|
COM_InitArgv (qargs->args.size, qargs->args.a);
|
2020-03-20 08:30:26 +00:00
|
|
|
num_sys++;
|
2020-03-20 07:59:59 +00:00
|
|
|
} else {
|
|
|
|
qwaq_thread_t qargs = {};
|
|
|
|
DARRAY_INIT (&qargs.args, 2);
|
|
|
|
DARRAY_APPEND (&qargs.args, this_program);
|
|
|
|
COM_InitArgv (qargs.args.size, qargs.args.a);
|
2020-03-20 06:47:30 +00:00
|
|
|
}
|
|
|
|
|
2002-11-12 02:30:08 +00:00
|
|
|
init_qf ();
|
|
|
|
|
2020-03-20 14:17:06 +00:00
|
|
|
if (thread_data.a[0]->args.size < 1) {
|
|
|
|
DARRAY_APPEND (&thread_data.a[0]->args, "qwaq-app.dat");
|
2020-03-20 08:30:26 +00:00
|
|
|
}
|
|
|
|
|
2020-03-20 14:17:06 +00:00
|
|
|
while (thread_data.size < thread_data.a[0]->args.size + num_sys) {
|
|
|
|
qwaq_thread_t *thread = calloc (1, sizeof (*thread));
|
|
|
|
DARRAY_INIT (&thread->args, 4);
|
|
|
|
DARRAY_APPEND (&thread->args, 0);
|
2020-03-20 12:16:56 +00:00
|
|
|
DARRAY_APPEND (&thread_data, thread);
|
2020-03-20 08:30:26 +00:00
|
|
|
}
|
|
|
|
|
2021-07-06 02:55:29 +00:00
|
|
|
main_ind = qwaq_init_threads (&thread_data);
|
2020-03-20 08:30:26 +00:00
|
|
|
if (main_ind >= 0) {
|
2020-03-20 15:01:36 +00:00
|
|
|
// threads might start new threads before the end is reached
|
|
|
|
size_t count = thread_data.size;
|
|
|
|
for (size_t i = 0; i < count; i++) {
|
2020-04-01 15:11:47 +00:00
|
|
|
if (thread_data.a[i]->progsinit) {
|
2020-03-24 15:44:04 +00:00
|
|
|
start_progs_thread (thread_data.a[i]);
|
2020-03-20 15:01:36 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
pthread_join (thread_data.a[main_ind]->thread_id, 0);
|
2020-07-16 06:44:37 +00:00
|
|
|
ret = thread_data.a[main_ind]->return_code;
|
2020-03-20 08:30:26 +00:00
|
|
|
}
|
2020-03-20 15:01:36 +00:00
|
|
|
|
2020-02-26 16:18:38 +00:00
|
|
|
Sys_Shutdown ();
|
2020-03-20 07:59:59 +00:00
|
|
|
return ret;
|
2001-06-01 21:57:59 +00:00
|
|
|
}
|