2002-08-03 06:04:00 +00:00
|
|
|
/*
|
|
|
|
#FILENAME#
|
|
|
|
|
|
|
|
#DESCRIPTION#
|
|
|
|
|
|
|
|
Copyright (C) 2002 #AUTHOR#
|
|
|
|
|
|
|
|
Author: #AUTHOR#
|
|
|
|
Date: #DATE#
|
|
|
|
|
|
|
|
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
|
|
|
|
|
|
|
|
*/
|
|
|
|
|
2002-08-08 09:20:00 +00:00
|
|
|
#ifdef HAVE_CONFIG_H
|
|
|
|
# include "config.h"
|
|
|
|
#endif
|
|
|
|
|
2003-02-14 22:42:11 +00:00
|
|
|
static __attribute__ ((unused))
|
|
|
|
const char rcsid[] =
|
|
|
|
"$Id$";
|
2003-01-15 15:31:36 +00:00
|
|
|
|
2002-08-28 23:00:30 +00:00
|
|
|
#include <ctype.h>
|
2002-08-03 06:04:00 +00:00
|
|
|
#include <stdlib.h>
|
2002-08-07 06:17:50 +00:00
|
|
|
#include <string.h>
|
2002-08-27 23:25:52 +00:00
|
|
|
#include <sys/types.h>
|
|
|
|
#include <dirent.h>
|
|
|
|
#include <fnmatch.h>
|
|
|
|
#include <errno.h>
|
|
|
|
|
2002-11-19 04:15:36 +00:00
|
|
|
#include "QF/cvar.h"
|
2002-08-27 23:25:52 +00:00
|
|
|
#include "QF/quakefs.h"
|
|
|
|
#include "QF/zone.h"
|
2002-08-07 06:17:50 +00:00
|
|
|
#include "QF/va.h"
|
2002-08-03 06:04:00 +00:00
|
|
|
#include "QF/sys.h"
|
|
|
|
#include "QF/cmd.h"
|
|
|
|
#include "QF/cbuf.h"
|
|
|
|
#include "QF/hash.h"
|
|
|
|
#include "QF/dstring.h"
|
2003-04-13 22:07:58 +00:00
|
|
|
#include "QF/gib.h"
|
|
|
|
|
2002-11-19 04:15:36 +00:00
|
|
|
#include "regex.h"
|
2003-04-13 22:07:58 +00:00
|
|
|
#include "gib_buffer.h"
|
|
|
|
#include "gib_parse.h"
|
|
|
|
#include "gib_function.h"
|
|
|
|
#include "gib_vars.h"
|
|
|
|
#include "gib_regex.h"
|
|
|
|
#include "gib_thread.h"
|
2003-04-14 01:17:55 +00:00
|
|
|
#include "gib_handle.h"
|
2003-04-13 22:07:58 +00:00
|
|
|
#include "gib_builtin.h"
|
2002-08-03 06:04:00 +00:00
|
|
|
|
2003-02-14 22:42:11 +00:00
|
|
|
char gib_null_string[] = "";
|
2003-01-28 21:16:21 +00:00
|
|
|
|
2003-02-14 22:42:11 +00:00
|
|
|
hashtab_t *gib_builtins;
|
2002-08-03 06:04:00 +00:00
|
|
|
|
2002-08-14 05:28:07 +00:00
|
|
|
/*
|
|
|
|
Hashtable callbacks
|
|
|
|
*/
|
2003-02-14 22:42:11 +00:00
|
|
|
static const char *
|
2002-08-03 06:04:00 +00:00
|
|
|
GIB_Builtin_Get_Key (void *ele, void *ptr)
|
|
|
|
{
|
2003-04-13 20:43:52 +00:00
|
|
|
return ((gib_builtin_t *) ele)->name;
|
2002-08-03 06:04:00 +00:00
|
|
|
}
|
2003-01-06 18:28:13 +00:00
|
|
|
static void
|
2002-08-03 06:04:00 +00:00
|
|
|
GIB_Builtin_Free (void *ele, void *ptr)
|
|
|
|
{
|
|
|
|
gib_builtin_t *b;
|
2003-02-14 22:42:11 +00:00
|
|
|
|
2002-08-03 06:04:00 +00:00
|
|
|
b = (gib_builtin_t *) ele;
|
2003-04-13 20:43:52 +00:00
|
|
|
free ((void *)b->name);
|
2002-08-03 06:04:00 +00:00
|
|
|
free (b);
|
|
|
|
}
|
|
|
|
|
2002-08-14 05:28:07 +00:00
|
|
|
/*
|
|
|
|
GIB_Builtin_Add
|
|
|
|
|
|
|
|
Registers a new builtin GIB command.
|
|
|
|
*/
|
2003-04-13 20:43:52 +00:00
|
|
|
|
2003-04-13 22:07:58 +00:00
|
|
|
void
|
2003-01-28 21:16:21 +00:00
|
|
|
GIB_Builtin_Add (const char *name, void (*func) (void))
|
2002-08-03 06:04:00 +00:00
|
|
|
{
|
|
|
|
gib_builtin_t *new;
|
2003-02-14 22:42:11 +00:00
|
|
|
|
2002-08-03 06:04:00 +00:00
|
|
|
if (!gib_builtins)
|
2003-02-14 22:42:11 +00:00
|
|
|
gib_builtins =
|
|
|
|
Hash_NewTable (1024, GIB_Builtin_Get_Key, GIB_Builtin_Free, 0);
|
|
|
|
|
2002-08-03 06:04:00 +00:00
|
|
|
new = calloc (1, sizeof (gib_builtin_t));
|
|
|
|
new->func = func;
|
2003-04-13 20:43:52 +00:00
|
|
|
new->name = strdup (name);
|
2002-08-03 06:04:00 +00:00
|
|
|
Hash_Add (gib_builtins, new);
|
2003-04-13 20:43:52 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
GIB_Builtin_Remove (const char *name)
|
|
|
|
{
|
|
|
|
gib_builtin_t *del;
|
|
|
|
|
2003-05-14 21:13:41 +00:00
|
|
|
if ((del = Hash_Del (gib_builtins, name)))
|
|
|
|
Hash_Free (gib_builtins, del);
|
2003-04-13 20:43:52 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
qboolean
|
|
|
|
GIB_Builtin_Exists (const char *name)
|
|
|
|
{
|
|
|
|
return Hash_Find (gib_builtins, name) ? true : false;
|
2002-08-03 06:04:00 +00:00
|
|
|
}
|
|
|
|
|
2002-08-14 05:28:07 +00:00
|
|
|
/*
|
|
|
|
GIB_Builtin_Find
|
|
|
|
|
|
|
|
Looks up the builtin name in the builtin hash,
|
|
|
|
returning a pointer to the struct on success,
|
|
|
|
zero otherwise.
|
|
|
|
*/
|
2002-08-03 06:04:00 +00:00
|
|
|
gib_builtin_t *
|
|
|
|
GIB_Builtin_Find (const char *name)
|
|
|
|
{
|
|
|
|
if (!gib_builtins)
|
|
|
|
return 0;
|
|
|
|
return (gib_builtin_t *) Hash_Find (gib_builtins, name);
|
|
|
|
}
|
|
|
|
|
2002-12-13 23:36:05 +00:00
|
|
|
dstring_t *
|
2002-08-23 21:18:00 +00:00
|
|
|
GIB_Return (const char *str)
|
|
|
|
{
|
2003-02-14 22:42:11 +00:00
|
|
|
dstring_t *dstr;
|
|
|
|
|
|
|
|
if (GIB_DATA (cbuf_active)->waitret) {
|
2003-01-28 21:16:21 +00:00
|
|
|
dstr = GIB_Buffer_Dsarray_Get (cbuf_active);
|
|
|
|
dstring_clearstr (dstr);
|
2003-02-14 22:42:11 +00:00
|
|
|
if (!str)
|
2003-01-28 21:16:21 +00:00
|
|
|
return dstr;
|
|
|
|
else
|
|
|
|
dstring_appendstr (dstr, str);
|
|
|
|
}
|
2002-12-13 23:36:05 +00:00
|
|
|
return 0;
|
2002-08-23 21:18:00 +00:00
|
|
|
}
|
|
|
|
|
2003-02-14 08:06:01 +00:00
|
|
|
void
|
|
|
|
GIB_Error (const char *type, const char *fmt, ...)
|
|
|
|
{
|
2003-02-14 22:42:11 +00:00
|
|
|
va_list args;
|
|
|
|
|
2003-02-14 08:06:01 +00:00
|
|
|
va_start (args, fmt);
|
|
|
|
GIB_Buffer_Error (cbuf_active, type, fmt, args);
|
|
|
|
va_end (args);
|
|
|
|
}
|
|
|
|
|
2002-08-14 05:28:07 +00:00
|
|
|
/*
|
|
|
|
GIB Builtin functions
|
|
|
|
|
|
|
|
See GIB docs for information.
|
|
|
|
*/
|
2003-01-06 18:28:13 +00:00
|
|
|
static void
|
2002-08-03 06:04:00 +00:00
|
|
|
GIB_Function_f (void)
|
|
|
|
{
|
2003-01-28 21:16:21 +00:00
|
|
|
gib_tree_t *program;
|
|
|
|
|
2002-08-05 00:01:06 +00:00
|
|
|
if (GIB_Argc () != 3)
|
2002-12-13 23:36:05 +00:00
|
|
|
GIB_USAGE ("name program");
|
2003-01-28 21:16:21 +00:00
|
|
|
else {
|
|
|
|
// Is the function program already tokenized?
|
|
|
|
if (GIB_Argm (2)->delim != '{') {
|
|
|
|
// Parse on the fly
|
2003-02-25 06:52:27 +00:00
|
|
|
if (!(program = GIB_Parse_Lines (GIB_Argv (2), 0))) {
|
2003-01-28 21:16:21 +00:00
|
|
|
// Error!
|
2003-02-14 22:42:11 +00:00
|
|
|
GIB_Error ("parse", "Parse error while defining function '%s'.",
|
|
|
|
GIB_Argv (1));
|
2003-01-28 21:16:21 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
} else
|
|
|
|
program = GIB_Argm (2)->children;
|
2003-02-14 22:42:11 +00:00
|
|
|
GIB_Function_Define (GIB_Argv (1), GIB_Argv (2), program,
|
|
|
|
GIB_DATA (cbuf_active)->script,
|
|
|
|
GIB_DATA (cbuf_active)->globals);
|
2003-01-28 21:16:21 +00:00
|
|
|
}
|
2003-02-14 22:42:11 +00:00
|
|
|
}
|
2002-08-03 06:04:00 +00:00
|
|
|
|
2003-01-06 18:28:13 +00:00
|
|
|
static void
|
2002-11-11 01:57:34 +00:00
|
|
|
GIB_Function_Get_f (void)
|
2002-08-23 21:18:00 +00:00
|
|
|
{
|
|
|
|
if (GIB_Argc () != 2)
|
2002-12-13 23:36:05 +00:00
|
|
|
GIB_USAGE ("name");
|
2002-08-23 21:18:00 +00:00
|
|
|
else {
|
|
|
|
gib_function_t *f;
|
2003-02-14 22:42:11 +00:00
|
|
|
|
2002-08-23 21:18:00 +00:00
|
|
|
if ((f = GIB_Function_Find (GIB_Argv (1))))
|
2003-01-28 21:16:21 +00:00
|
|
|
GIB_Return (f->text->str);
|
2002-08-23 21:18:00 +00:00
|
|
|
else
|
|
|
|
GIB_Return ("");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2003-01-06 18:28:13 +00:00
|
|
|
static void
|
2002-08-08 09:20:00 +00:00
|
|
|
GIB_Local_f (void)
|
2002-08-05 00:01:06 +00:00
|
|
|
{
|
2003-02-14 22:42:11 +00:00
|
|
|
gib_var_t *var;
|
2003-04-17 00:01:48 +00:00
|
|
|
unsigned int index;
|
|
|
|
int i;
|
2003-02-16 02:44:24 +00:00
|
|
|
static hashtab_t *zero = 0;
|
2003-02-14 22:42:11 +00:00
|
|
|
|
2002-11-11 01:57:34 +00:00
|
|
|
if (GIB_Argc () < 2)
|
2003-02-16 02:44:24 +00:00
|
|
|
GIB_USAGE ("var [= value1 value2 ...] || var [var2 var3 ...]");
|
|
|
|
else if (!strcmp (GIB_Argv(2), "=")) {
|
|
|
|
var = GIB_Var_Get_Complex (&GIB_DATA (cbuf_active)->locals, &zero,
|
2003-02-14 22:42:11 +00:00
|
|
|
GIB_Argv (1), &index, true);
|
|
|
|
if (GIB_Argc () >= 3)
|
|
|
|
GIB_Var_Assign (var, index, cbuf_active->args->argv + 3,
|
2003-05-14 21:13:41 +00:00
|
|
|
GIB_Argc () - 3, GIB_Argv (1)[strlen (GIB_Argv(1)) - 1] != ']');
|
2003-04-11 02:57:11 +00:00
|
|
|
if (GIB_CanReturn ())
|
|
|
|
for (i = 3; i < GIB_Argc(); i++)
|
|
|
|
GIB_Return (GIB_Argv(i));
|
2003-02-16 02:44:24 +00:00
|
|
|
} else for (i = 1; i < GIB_Argc(); i++)
|
|
|
|
var = GIB_Var_Get_Complex (&GIB_DATA (cbuf_active)->locals, &zero,
|
2003-02-16 19:46:34 +00:00
|
|
|
GIB_Argv (i), &index, true);
|
2002-08-08 09:20:00 +00:00
|
|
|
}
|
|
|
|
|
2003-01-28 21:16:21 +00:00
|
|
|
|
2003-01-06 18:28:13 +00:00
|
|
|
static void
|
2002-08-08 09:20:00 +00:00
|
|
|
GIB_Global_f (void)
|
|
|
|
{
|
2003-02-14 22:42:11 +00:00
|
|
|
gib_var_t *var;
|
2003-04-17 00:01:48 +00:00
|
|
|
unsigned int index;
|
|
|
|
int i;
|
2003-02-16 02:44:24 +00:00
|
|
|
static hashtab_t *zero = 0;
|
2003-02-14 22:42:11 +00:00
|
|
|
|
2002-11-11 01:57:34 +00:00
|
|
|
if (GIB_Argc () < 2)
|
2003-02-16 02:44:24 +00:00
|
|
|
GIB_USAGE ("var [= value1 value2 ...] || var [var2 var3 ...]");
|
|
|
|
else if (!strcmp (GIB_Argv(2), "=")) {
|
|
|
|
var = GIB_Var_Get_Complex (&GIB_DATA (cbuf_active)->globals, &zero,
|
2003-02-14 22:42:11 +00:00
|
|
|
GIB_Argv (1), &index, true);
|
|
|
|
if (GIB_Argc () >= 3)
|
|
|
|
GIB_Var_Assign (var, index, cbuf_active->args->argv + 3,
|
2003-05-14 21:13:41 +00:00
|
|
|
GIB_Argc () - 3, GIB_Argv (1)[strlen (GIB_Argv(1)) - 1] != ']');
|
2003-04-11 02:57:11 +00:00
|
|
|
if (GIB_CanReturn ())
|
|
|
|
for (i = 3; i < GIB_Argc(); i++)
|
|
|
|
GIB_Return (GIB_Argv(i));
|
2003-02-16 02:44:24 +00:00
|
|
|
} else for (i = 1; i < GIB_Argc(); i++)
|
|
|
|
var = GIB_Var_Get_Complex (&GIB_DATA (cbuf_active)->globals, &zero,
|
2003-02-16 19:46:34 +00:00
|
|
|
GIB_Argv (i), &index, true);
|
2003-02-16 02:44:24 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
2003-02-16 19:46:34 +00:00
|
|
|
GIB_Delete_f (void)
|
2003-02-16 02:44:24 +00:00
|
|
|
{
|
|
|
|
gib_var_t *var;
|
2003-04-17 00:01:48 +00:00
|
|
|
unsigned int index;
|
|
|
|
int i;
|
2003-02-16 02:44:24 +00:00
|
|
|
hashtab_t *source;
|
|
|
|
char *c;
|
|
|
|
|
|
|
|
if (GIB_Argc () < 2)
|
|
|
|
GIB_USAGE ("var [var2 var2 ...]");
|
|
|
|
else for (i = 1; i < GIB_Argc(); i++) {
|
|
|
|
if ((c = strrchr (GIB_Argv(i), '.'))) {
|
|
|
|
*(c++) = 0;
|
2003-02-16 19:46:34 +00:00
|
|
|
if (!(var = GIB_Var_Get_Complex (&GIB_DATA (cbuf_active)->locals,
|
|
|
|
&GIB_DATA(cbuf_active)->globals,
|
2003-02-16 02:44:24 +00:00
|
|
|
GIB_Argv (i), &index, false)))
|
|
|
|
continue;
|
|
|
|
source = var->array[index].leaves;
|
|
|
|
} else {
|
|
|
|
c = GIB_Argv(i);
|
|
|
|
source = GIB_DATA(cbuf_active)->globals;
|
|
|
|
}
|
|
|
|
Hash_Free (source, Hash_Del (source, c));
|
2003-01-29 04:34:23 +00:00
|
|
|
}
|
2002-08-05 00:01:06 +00:00
|
|
|
}
|
|
|
|
|
2003-01-06 18:28:13 +00:00
|
|
|
static void
|
2003-01-29 04:34:23 +00:00
|
|
|
GIB_Domain_f (void)
|
2002-10-23 03:56:57 +00:00
|
|
|
{
|
2003-02-14 22:42:11 +00:00
|
|
|
if (GIB_Argc () != 2)
|
2003-01-28 21:16:21 +00:00
|
|
|
GIB_USAGE ("domain");
|
|
|
|
else
|
2003-02-14 22:42:11 +00:00
|
|
|
GIB_DATA (cbuf_active)->globals = GIB_Domain_Get (GIB_Argv (1));
|
2002-10-23 03:56:57 +00:00
|
|
|
}
|
|
|
|
|
2003-02-14 08:06:01 +00:00
|
|
|
static void
|
|
|
|
GIB_Domain_Clear_f (void)
|
|
|
|
{
|
2003-02-14 22:42:11 +00:00
|
|
|
if (GIB_Argc () != 2)
|
2003-02-14 08:06:01 +00:00
|
|
|
GIB_USAGE ("domain");
|
|
|
|
else
|
2003-02-14 22:42:11 +00:00
|
|
|
Hash_FlushTable (GIB_Domain_Get (GIB_Argv (2)));
|
2003-02-14 08:06:01 +00:00
|
|
|
}
|
2003-02-14 22:42:11 +00:00
|
|
|
|
2003-03-01 02:44:58 +00:00
|
|
|
static gib_tree_t fakeip = {0,0,0,0,0,0,0,0};
|
|
|
|
|
2003-01-06 18:28:13 +00:00
|
|
|
static void
|
2002-08-06 08:15:20 +00:00
|
|
|
GIB_Return_f (void)
|
|
|
|
{
|
2003-02-14 22:42:11 +00:00
|
|
|
cbuf_t *sp = cbuf_active->up;
|
|
|
|
|
2003-03-01 02:44:58 +00:00
|
|
|
GIB_DATA (cbuf_active)->ip = &fakeip;
|
2003-02-14 22:42:11 +00:00
|
|
|
|
|
|
|
if (GIB_Argc () > 1 && sp && sp->interpreter == &gib_interp
|
|
|
|
&& GIB_DATA (sp)->waitret) {
|
2003-04-17 00:01:48 +00:00
|
|
|
int i;
|
2003-02-14 22:42:11 +00:00
|
|
|
dstring_t *dstr;
|
|
|
|
|
|
|
|
for (i = 1; i < GIB_Argc (); i++) {
|
2003-01-28 21:16:21 +00:00
|
|
|
dstr = GIB_Buffer_Dsarray_Get (sp);
|
|
|
|
dstring_clearstr (dstr);
|
2003-02-14 22:42:11 +00:00
|
|
|
dstring_appendstr (dstr, GIB_Argv (i));
|
2002-08-07 06:17:50 +00:00
|
|
|
}
|
2002-08-28 08:00:35 +00:00
|
|
|
}
|
|
|
|
}
|
2003-02-14 22:42:11 +00:00
|
|
|
|
2003-01-06 18:28:13 +00:00
|
|
|
static void
|
2003-01-28 21:16:21 +00:00
|
|
|
GIB_For_f (void)
|
2002-08-28 08:00:35 +00:00
|
|
|
{
|
2003-02-14 22:42:11 +00:00
|
|
|
dstring_t *dstr;
|
2003-04-17 00:01:48 +00:00
|
|
|
int i;
|
2003-02-14 22:42:11 +00:00
|
|
|
|
2003-01-28 21:16:21 +00:00
|
|
|
GIB_Buffer_Push_Sstack (cbuf_active);
|
|
|
|
dstr = GIB_Buffer_Dsarray_Get (cbuf_active);
|
|
|
|
dstring_clearstr (dstr);
|
2003-02-14 22:42:11 +00:00
|
|
|
dstring_appendstr (dstr, GIB_Argv (1));
|
|
|
|
for (i = GIB_Argc () - 2; i > 2; i--) {
|
2003-01-28 21:16:21 +00:00
|
|
|
dstr = GIB_Buffer_Dsarray_Get (cbuf_active);
|
2003-02-14 22:42:11 +00:00
|
|
|
dstring_appendstr (dstr, GIB_Argv (i));
|
2002-08-28 23:00:30 +00:00
|
|
|
}
|
2002-12-08 20:55:40 +00:00
|
|
|
}
|
|
|
|
|
2002-11-11 01:57:34 +00:00
|
|
|
// Note: this is a standard console command, not a GIB builtin
|
2003-01-06 18:28:13 +00:00
|
|
|
static void
|
2002-08-07 06:17:50 +00:00
|
|
|
GIB_Runexported_f (void)
|
|
|
|
{
|
|
|
|
gib_function_t *f;
|
2003-04-11 02:57:11 +00:00
|
|
|
const char **args;
|
2003-02-14 22:42:11 +00:00
|
|
|
|
2002-08-07 06:17:50 +00:00
|
|
|
if (!(f = GIB_Function_Find (Cmd_Argv (0))))
|
|
|
|
Sys_Printf ("Error: No function found for exported command \"%s\".\n"
|
|
|
|
"This is most likely a bug, please report it to"
|
2003-02-14 22:42:11 +00:00
|
|
|
"The QuakeForge developers.", Cmd_Argv (0));
|
2002-08-27 04:47:49 +00:00
|
|
|
else {
|
2003-04-11 02:57:11 +00:00
|
|
|
cbuf_t *sub = Cbuf_PushStack (&gib_interp);
|
2003-04-17 00:01:48 +00:00
|
|
|
int i;
|
2003-02-14 22:42:11 +00:00
|
|
|
|
2003-04-11 02:57:11 +00:00
|
|
|
args = malloc (sizeof (char *) * Cmd_Argc());
|
|
|
|
for (i = 0; i < Cmd_Argc(); i++)
|
|
|
|
args[i] = Cmd_Argv(i);
|
|
|
|
GIB_Function_Execute (sub, f, args, Cmd_Argc());
|
|
|
|
free (args);
|
2002-08-27 04:47:49 +00:00
|
|
|
}
|
2002-08-07 06:17:50 +00:00
|
|
|
}
|
|
|
|
|
2003-01-06 18:28:13 +00:00
|
|
|
static void
|
2002-11-11 01:57:34 +00:00
|
|
|
GIB_Function_Export_f (void)
|
2002-08-07 06:17:50 +00:00
|
|
|
{
|
|
|
|
gib_function_t *f;
|
2003-02-14 22:42:11 +00:00
|
|
|
int i;
|
|
|
|
|
|
|
|
if (GIB_Argc () < 2)
|
2002-12-13 23:36:05 +00:00
|
|
|
GIB_USAGE ("function1 [function2 function3 ...]");
|
2003-02-14 22:42:11 +00:00
|
|
|
for (i = 1; i < GIB_Argc (); i++) {
|
2002-11-11 01:57:34 +00:00
|
|
|
if (!(f = GIB_Function_Find (GIB_Argv (i))))
|
2003-02-14 22:42:11 +00:00
|
|
|
GIB_Error ("function", "%s: function '%s' not found.", GIB_Argv (0),
|
|
|
|
GIB_Argv (i));
|
2002-11-11 01:57:34 +00:00
|
|
|
else if (!f->exported) {
|
2003-02-14 08:06:01 +00:00
|
|
|
if (Cmd_Exists (f->name)) {
|
2003-02-14 22:42:11 +00:00
|
|
|
GIB_Error ("export",
|
|
|
|
"%s: A console command with the name '%s' already exists.",
|
|
|
|
GIB_Argv (0), GIB_Argv (i));
|
2003-02-14 08:06:01 +00:00
|
|
|
return;
|
|
|
|
} else {
|
2003-02-14 22:42:11 +00:00
|
|
|
Cmd_AddCommand (f->name, GIB_Runexported_f,
|
|
|
|
"Exported GIB function.");
|
2003-02-14 08:06:01 +00:00
|
|
|
f->exported = true;
|
|
|
|
}
|
2002-11-11 01:57:34 +00:00
|
|
|
}
|
2002-08-07 06:17:50 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2003-01-06 18:28:13 +00:00
|
|
|
static void
|
2003-01-30 23:26:43 +00:00
|
|
|
GIB_Length_f (void)
|
2002-08-24 05:14:46 +00:00
|
|
|
{
|
2003-02-14 22:42:11 +00:00
|
|
|
dstring_t *ret;
|
|
|
|
|
|
|
|
if (GIB_Argc () != 2)
|
2002-12-13 23:36:05 +00:00
|
|
|
GIB_USAGE ("string");
|
|
|
|
else if ((ret = GIB_Return (0)))
|
2003-02-14 22:42:11 +00:00
|
|
|
dsprintf (ret, "%i", (int) strlen (GIB_Argv (1)));
|
2002-08-24 05:14:46 +00:00
|
|
|
}
|
|
|
|
|
2003-01-06 18:28:13 +00:00
|
|
|
static void
|
2003-01-30 23:26:43 +00:00
|
|
|
GIB_Equal_f (void)
|
2002-08-24 05:14:46 +00:00
|
|
|
{
|
2003-02-14 22:42:11 +00:00
|
|
|
if (GIB_Argc () != 3)
|
2002-12-13 23:36:05 +00:00
|
|
|
GIB_USAGE ("string1 string2");
|
2003-02-14 22:42:11 +00:00
|
|
|
else if (strcmp (GIB_Argv (1), GIB_Argv (2)))
|
2002-12-13 23:36:05 +00:00
|
|
|
GIB_Return ("0");
|
2002-08-24 05:14:46 +00:00
|
|
|
else
|
2002-12-13 23:36:05 +00:00
|
|
|
GIB_Return ("1");
|
2002-08-24 05:14:46 +00:00
|
|
|
}
|
|
|
|
|
2003-02-16 19:46:34 +00:00
|
|
|
static void
|
|
|
|
GIB_Count_f (void)
|
|
|
|
{
|
|
|
|
if (GIB_CanReturn())
|
|
|
|
dsprintf (GIB_Return(0), "%u", GIB_Argc() - 1);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
static void
|
|
|
|
GIB_Contains_f (void)
|
|
|
|
{
|
2003-04-17 00:01:48 +00:00
|
|
|
int i;
|
2003-02-16 19:46:34 +00:00
|
|
|
if (GIB_Argc () < 2)
|
|
|
|
GIB_USAGE ("needle [straw1 straw2 ...]");
|
|
|
|
else if (GIB_CanReturn())
|
|
|
|
for (i = 2; i < GIB_Argc(); i++)
|
2003-02-22 07:19:51 +00:00
|
|
|
if (!strcmp(GIB_Argv(1), GIB_Argv(i))) {
|
2003-02-16 19:46:34 +00:00
|
|
|
GIB_Return("1");
|
2003-02-22 07:19:51 +00:00
|
|
|
return;
|
|
|
|
}
|
2003-02-16 19:46:34 +00:00
|
|
|
GIB_Return ("0");
|
|
|
|
}
|
|
|
|
|
2003-01-06 18:28:13 +00:00
|
|
|
static void
|
2003-01-30 23:26:43 +00:00
|
|
|
GIB_Slice_f (void)
|
2003-01-29 04:34:23 +00:00
|
|
|
{
|
2003-02-14 22:42:11 +00:00
|
|
|
dstring_t *ret;
|
|
|
|
int start, end, len;
|
|
|
|
|
|
|
|
if (GIB_Argc () < 3 || GIB_Argc () > 4)
|
2003-01-31 05:22:20 +00:00
|
|
|
GIB_USAGE ("string start [end]");
|
2003-01-29 04:34:23 +00:00
|
|
|
else {
|
2003-02-14 22:42:11 +00:00
|
|
|
len = strlen (GIB_Argv (1));
|
|
|
|
start = atoi (GIB_Argv (2));
|
|
|
|
end = *GIB_Argv (3) ? atoi (GIB_Argv (3)) : len;
|
2003-01-31 05:22:20 +00:00
|
|
|
if (end < 0)
|
2003-01-29 04:34:23 +00:00
|
|
|
end += len;
|
2003-01-31 03:43:56 +00:00
|
|
|
else if (end > len)
|
2003-01-29 04:34:23 +00:00
|
|
|
end = len;
|
2003-01-31 03:43:56 +00:00
|
|
|
if (start < 0) {
|
|
|
|
start += len;
|
|
|
|
if (start < 0)
|
|
|
|
start = 0;
|
|
|
|
} else if (start >= len || start >= end)
|
|
|
|
return;
|
2003-01-29 04:34:23 +00:00
|
|
|
if ((ret = GIB_Return (0)))
|
2003-02-14 22:42:11 +00:00
|
|
|
dstring_appendsubstr (ret, GIB_Argv (1) + start, end - start);
|
2003-01-29 04:34:23 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2003-02-14 08:06:01 +00:00
|
|
|
|
2003-01-29 04:34:23 +00:00
|
|
|
static void
|
2003-02-14 08:06:01 +00:00
|
|
|
GIB_Slice_Find_f (void)
|
2002-11-16 20:56:04 +00:00
|
|
|
{
|
2003-02-14 22:42:11 +00:00
|
|
|
char *res;
|
|
|
|
|
|
|
|
if (GIB_Argc () != 3) {
|
2003-01-29 04:34:23 +00:00
|
|
|
GIB_USAGE ("haystack needle");
|
2002-11-16 20:56:04 +00:00
|
|
|
return;
|
2003-02-14 08:06:01 +00:00
|
|
|
} else if (!GIB_CanReturn ())
|
|
|
|
return;
|
2003-02-14 22:42:11 +00:00
|
|
|
else if ((res = strstr (GIB_Argv (1), GIB_Argv (2)))) {
|
|
|
|
dsprintf (GIB_Return (0), "%lu",
|
|
|
|
(unsigned long int) (res - GIB_Argv (1)));
|
|
|
|
dsprintf (GIB_Return (0), "%lu",
|
|
|
|
(unsigned long int) (res - GIB_Argv (1)) +
|
|
|
|
strlen (GIB_Argv (2)));
|
|
|
|
}
|
2002-11-16 20:56:04 +00:00
|
|
|
}
|
2003-02-14 08:06:01 +00:00
|
|
|
|
2003-01-30 23:26:43 +00:00
|
|
|
|
|
|
|
static void
|
|
|
|
GIB_Split_f (void)
|
|
|
|
{
|
2003-02-14 22:42:11 +00:00
|
|
|
char *end, *start;
|
2003-01-30 23:26:43 +00:00
|
|
|
const char *ifs;
|
2003-02-14 22:42:11 +00:00
|
|
|
|
|
|
|
if (GIB_Argc () < 2 || GIB_Argc () > 3) {
|
2003-01-30 23:26:43 +00:00
|
|
|
GIB_USAGE ("string [fs]");
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2003-02-14 22:42:11 +00:00
|
|
|
ifs = GIB_Argc () == 3 ? GIB_Argv (2) : " \t\r\n";
|
|
|
|
|
|
|
|
end = GIB_Argv (1);
|
2003-01-30 23:26:43 +00:00
|
|
|
while (*end) {
|
2003-02-14 22:42:11 +00:00
|
|
|
for (; strchr (ifs, *end); end++)
|
2003-01-30 23:26:43 +00:00
|
|
|
if (!*end)
|
|
|
|
return;
|
|
|
|
start = end;
|
2003-02-14 22:42:11 +00:00
|
|
|
while (!strchr (ifs, *end))
|
2003-01-30 23:26:43 +00:00
|
|
|
end++;
|
|
|
|
if (*end)
|
|
|
|
*(end++) = 0;
|
2003-02-14 22:42:11 +00:00
|
|
|
GIB_Return (start);
|
2003-01-30 23:26:43 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2003-05-14 21:13:41 +00:00
|
|
|
static void
|
|
|
|
GIB_Chomp_f (void)
|
|
|
|
{
|
|
|
|
char *str;
|
|
|
|
const char *junk;
|
|
|
|
unsigned int i;
|
|
|
|
|
|
|
|
if (GIB_Argc () < 2 || GIB_Argc () > 3) {
|
|
|
|
GIB_USAGE ("string [junk]");
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
str = GIB_Argv (1);
|
|
|
|
if (GIB_Argc () == 2)
|
|
|
|
junk = " \t\n\r";
|
|
|
|
else
|
|
|
|
junk = GIB_Argv (2);
|
|
|
|
|
|
|
|
for (; *str && strchr (junk, *str); str++);
|
|
|
|
for (i = strlen(str) - 1; i && strchr (junk, str[i]); i--);
|
|
|
|
str[i+1] = 0;
|
|
|
|
GIB_Return (str);
|
|
|
|
}
|
|
|
|
|
2003-01-06 18:28:13 +00:00
|
|
|
static void
|
2002-11-19 04:15:36 +00:00
|
|
|
GIB_Regex_Match_f (void)
|
|
|
|
{
|
2003-02-14 22:42:11 +00:00
|
|
|
regex_t *reg;
|
|
|
|
|
|
|
|
if (GIB_Argc () != 4) {
|
2002-11-19 06:24:29 +00:00
|
|
|
GIB_USAGE ("string regex options");
|
2002-11-19 04:15:36 +00:00
|
|
|
return;
|
|
|
|
}
|
2003-02-14 22:42:11 +00:00
|
|
|
|
|
|
|
if (!
|
|
|
|
(reg =
|
|
|
|
GIB_Regex_Compile (GIB_Argv (2),
|
|
|
|
REG_EXTENDED |
|
|
|
|
GIB_Regex_Translate_Options (GIB_Argv (3)))))
|
|
|
|
GIB_Error ("regex", "%s: %s", GIB_Argv (0), GIB_Regex_Error ());
|
|
|
|
else if (regexec (reg, GIB_Argv (1), 0, 0, 0))
|
2002-11-19 04:15:36 +00:00
|
|
|
GIB_Return ("0");
|
|
|
|
else
|
|
|
|
GIB_Return ("1");
|
|
|
|
}
|
|
|
|
|
2003-01-06 18:28:13 +00:00
|
|
|
static void
|
2002-11-19 04:15:36 +00:00
|
|
|
GIB_Regex_Replace_f (void)
|
|
|
|
{
|
2003-02-14 22:42:11 +00:00
|
|
|
regex_t *reg;
|
|
|
|
int ofs, len;
|
|
|
|
regmatch_t match[10];
|
|
|
|
|
|
|
|
if (GIB_Argc () != 5) {
|
2002-11-19 06:24:29 +00:00
|
|
|
GIB_USAGE ("string regex options replacement");
|
2002-11-19 04:15:36 +00:00
|
|
|
return;
|
|
|
|
}
|
2003-02-14 22:42:11 +00:00
|
|
|
|
2002-11-19 04:15:36 +00:00
|
|
|
ofs = 0;
|
2003-02-14 22:42:11 +00:00
|
|
|
len = strlen (GIB_Argv (4));
|
|
|
|
|
|
|
|
if (!
|
|
|
|
(reg =
|
|
|
|
GIB_Regex_Compile (GIB_Argv (2),
|
|
|
|
REG_EXTENDED |
|
|
|
|
GIB_Regex_Translate_Options (GIB_Argv (3)))))
|
|
|
|
GIB_Error ("regex", "%s: %s", GIB_Argv (0), GIB_Regex_Error ());
|
|
|
|
else if (strchr (GIB_Argv (3), 'g'))
|
|
|
|
while (!regexec
|
|
|
|
(reg, GIB_Argv (1) + ofs, 10, match, ofs > 0 ? REG_NOTBOL : 0)
|
|
|
|
&& match[0].rm_eo)
|
|
|
|
ofs +=
|
|
|
|
GIB_Regex_Apply_Match (match, GIB_Argd (1), ofs, GIB_Argv (4));
|
|
|
|
else if (!regexec (reg, GIB_Argv (1), 10, match, 0) && match[0].rm_eo)
|
|
|
|
GIB_Regex_Apply_Match (match, GIB_Argd (1), 0, GIB_Argv (4));
|
|
|
|
GIB_Return (GIB_Argv (1));
|
2002-11-19 04:15:36 +00:00
|
|
|
}
|
|
|
|
|
2003-01-06 18:28:13 +00:00
|
|
|
static void
|
2002-11-19 04:15:36 +00:00
|
|
|
GIB_Regex_Extract_f (void)
|
|
|
|
{
|
2003-02-14 22:42:11 +00:00
|
|
|
regex_t *reg;
|
2002-11-19 04:15:36 +00:00
|
|
|
regmatch_t *match;
|
2003-02-14 22:42:11 +00:00
|
|
|
int i;
|
|
|
|
char o;
|
|
|
|
|
|
|
|
if (GIB_Argc () != 4) {
|
2003-01-28 21:16:21 +00:00
|
|
|
GIB_USAGE ("string regex options");
|
2002-11-19 04:15:36 +00:00
|
|
|
return;
|
2003-02-14 08:06:01 +00:00
|
|
|
} else if (!GIB_CanReturn ())
|
|
|
|
return;
|
2003-02-14 22:42:11 +00:00
|
|
|
match = calloc (32, sizeof (regmatch_t));
|
|
|
|
|
|
|
|
if (!
|
|
|
|
(reg =
|
|
|
|
GIB_Regex_Compile (GIB_Argv (2),
|
|
|
|
REG_EXTENDED |
|
|
|
|
GIB_Regex_Translate_Options (GIB_Argv (3)))))
|
|
|
|
GIB_Error ("regex", "%s: %s", GIB_Argv (0), GIB_Regex_Error ());
|
|
|
|
else if (!regexec (reg, GIB_Argv (1), 32, match, 0) && match[0].rm_eo) {
|
2003-02-14 08:06:01 +00:00
|
|
|
dsprintf (GIB_Return (0), "%lu", (unsigned long) match[0].rm_eo);
|
2003-01-28 21:16:21 +00:00
|
|
|
for (i = 0; i < 32; i++) {
|
|
|
|
if (match[i].rm_so != -1) {
|
2003-02-14 22:42:11 +00:00
|
|
|
o = GIB_Argv (1)[match[i].rm_eo];
|
|
|
|
GIB_Argv (1)[match[i].rm_eo] = 0;
|
|
|
|
GIB_Return (GIB_Argv (1) + match[i].rm_so);
|
|
|
|
GIB_Argv (1)[match[i].rm_eo] = o;
|
2002-11-19 04:15:36 +00:00
|
|
|
}
|
|
|
|
}
|
2003-02-14 08:06:01 +00:00
|
|
|
}
|
2002-11-19 04:15:36 +00:00
|
|
|
free (match);
|
|
|
|
}
|
2002-11-16 20:56:04 +00:00
|
|
|
|
2003-05-16 07:24:58 +00:00
|
|
|
static void
|
|
|
|
GIB_Text_White_f (void)
|
|
|
|
{
|
|
|
|
if (GIB_Argc () != 2)
|
|
|
|
GIB_USAGE ("text");
|
|
|
|
else if (GIB_CanReturn ()) {
|
|
|
|
unsigned int i;
|
|
|
|
dstring_t *dstr;
|
|
|
|
char *str;
|
|
|
|
|
|
|
|
dstr = GIB_Return (0);
|
|
|
|
dstring_appendstr (dstr, GIB_Argv(1));
|
|
|
|
str = dstr->str;
|
|
|
|
|
|
|
|
for (i = 0; i < dstr->size-1; i++)
|
|
|
|
str[i] = str[i] & ~0x80;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
GIB_Text_Brown_f (void)
|
|
|
|
{
|
|
|
|
if (GIB_Argc () != 2)
|
|
|
|
GIB_USAGE ("text");
|
|
|
|
else if (GIB_CanReturn ()) {
|
|
|
|
unsigned int i;
|
|
|
|
dstring_t *dstr;
|
|
|
|
char *str;
|
|
|
|
|
|
|
|
dstr = GIB_Return (0);
|
|
|
|
dstring_appendstr (dstr, GIB_Argv(1));
|
|
|
|
str = dstr->str;
|
|
|
|
|
|
|
|
for (i = 0; i < dstr->size-1; i++)
|
|
|
|
str[i] = str[i] | 0x80;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2003-01-06 18:28:13 +00:00
|
|
|
static void
|
2002-08-27 04:47:49 +00:00
|
|
|
GIB_Thread_Create_f (void)
|
|
|
|
{
|
2003-01-30 23:26:43 +00:00
|
|
|
gib_function_t *f;
|
2003-02-14 22:42:11 +00:00
|
|
|
|
|
|
|
if (GIB_Argc () < 2)
|
2003-01-30 23:26:43 +00:00
|
|
|
GIB_USAGE ("function [arg1 arg2 ...]");
|
2003-02-14 22:42:11 +00:00
|
|
|
else if (!(f = GIB_Function_Find (GIB_Argv (1))))
|
|
|
|
GIB_Error ("function", "%s: no function named '%s' exists.",
|
|
|
|
GIB_Argv (0), GIB_Argv (1));
|
2002-08-27 04:47:49 +00:00
|
|
|
else {
|
|
|
|
gib_thread_t *thread = GIB_Thread_New ();
|
2003-02-14 22:42:11 +00:00
|
|
|
|
2003-04-11 02:57:11 +00:00
|
|
|
GIB_Function_Execute_D (thread->cbuf, f, cbuf_active->args->argv + 1,
|
2003-02-14 22:42:11 +00:00
|
|
|
cbuf_active->args->argc - 1);
|
2002-08-27 04:47:49 +00:00
|
|
|
GIB_Thread_Add (thread);
|
2003-02-14 08:06:01 +00:00
|
|
|
if (GIB_CanReturn ())
|
2003-02-14 22:42:11 +00:00
|
|
|
dsprintf (GIB_Return (0), "%lu", thread->id);
|
2002-08-27 04:47:49 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2003-01-06 18:28:13 +00:00
|
|
|
static void
|
2002-08-27 04:47:49 +00:00
|
|
|
GIB_Thread_Kill_f (void)
|
|
|
|
{
|
2003-02-14 22:42:11 +00:00
|
|
|
if (GIB_Argc () != 2)
|
2002-12-13 23:36:05 +00:00
|
|
|
GIB_USAGE ("id");
|
2002-08-27 04:47:49 +00:00
|
|
|
else {
|
|
|
|
gib_thread_t *thread;
|
2003-02-14 22:42:11 +00:00
|
|
|
cbuf_t *cur;
|
|
|
|
unsigned long int id = strtoul (GIB_Argv (1), 0, 10);
|
|
|
|
|
2003-04-14 01:17:55 +00:00
|
|
|
thread = GIB_Handle_Get (id, gib_thread_class);
|
2002-08-27 04:47:49 +00:00
|
|
|
if (!thread) {
|
2003-04-14 01:17:55 +00:00
|
|
|
GIB_Error ("thread", "%s: thread %lu does not exist.", GIB_Argv (0), id);
|
2002-08-27 04:47:49 +00:00
|
|
|
return;
|
|
|
|
}
|
2003-04-11 05:58:58 +00:00
|
|
|
|
|
|
|
// If we are currently running this thread, set an error state so we exit it cleanly
|
|
|
|
// if it were simply nuked, a crash would result
|
|
|
|
for (cur = thread->cbuf; cur->down && cur->down->state != CBUF_STATE_JUNK; cur = cur->down)
|
|
|
|
if (cur == cbuf_active) {
|
|
|
|
cur->state = CBUF_STATE_ERROR;
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
GIB_Thread_Remove (thread);
|
|
|
|
GIB_Thread_Delete (thread);
|
2003-02-14 08:06:01 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
GIB_Thread_List_f (void)
|
|
|
|
{
|
2003-02-14 22:42:11 +00:00
|
|
|
if (GIB_Argc () != 1)
|
2003-02-14 08:06:01 +00:00
|
|
|
GIB_USAGE ("");
|
2003-02-14 22:42:11 +00:00
|
|
|
else if (GIB_CanReturn ()) {
|
2003-02-14 08:06:01 +00:00
|
|
|
gib_thread_t *cur;
|
2003-02-14 22:42:11 +00:00
|
|
|
|
2003-04-14 01:17:55 +00:00
|
|
|
for (cur = gib_thread_first; cur; cur = cur->next)
|
2003-02-14 08:06:01 +00:00
|
|
|
dsprintf (GIB_Return (0), "%lu", cur->id);
|
2002-08-27 04:47:49 +00:00
|
|
|
}
|
|
|
|
}
|
2002-08-27 23:25:52 +00:00
|
|
|
|
2003-01-06 18:28:13 +00:00
|
|
|
static void
|
2003-01-03 04:30:38 +00:00
|
|
|
GIB_Event_Register_f (void)
|
|
|
|
{
|
|
|
|
gib_function_t *func;
|
2003-02-14 22:42:11 +00:00
|
|
|
|
|
|
|
if (GIB_Argc () != 3)
|
2003-01-03 04:30:38 +00:00
|
|
|
GIB_USAGE ("event function");
|
2003-02-14 22:42:11 +00:00
|
|
|
else if (!(func = GIB_Function_Find (GIB_Argv (2))) && GIB_Argv (2)[0])
|
|
|
|
GIB_Error ("function", "Function %s not found.", GIB_Argv (2));
|
|
|
|
else if (GIB_Event_Register (GIB_Argv (1), func))
|
|
|
|
GIB_Error ("event", "Event %s not found.", GIB_Argv (1));
|
2003-01-03 04:30:38 +00:00
|
|
|
}
|
|
|
|
|
2002-08-27 23:25:52 +00:00
|
|
|
/* File access */
|
|
|
|
|
2003-02-14 22:42:11 +00:00
|
|
|
int (*GIB_File_Transform_Path) (dstring_t * path) = NULL;
|
2002-11-14 05:28:54 +00:00
|
|
|
|
2003-01-06 18:28:13 +00:00
|
|
|
static int
|
2003-02-14 22:42:11 +00:00
|
|
|
GIB_File_Transform_Path_Null (dstring_t * path)
|
2002-11-14 05:28:54 +00:00
|
|
|
{
|
2003-02-14 22:42:11 +00:00
|
|
|
char *s;
|
|
|
|
|
2002-11-16 03:52:07 +00:00
|
|
|
// Convert backslash to forward slash
|
2003-02-14 22:42:11 +00:00
|
|
|
for (s = strchr (path->str, '\\'); s; s = strchr (s, '\\'))
|
2002-11-16 03:52:07 +00:00
|
|
|
*s = '/';
|
2002-11-14 05:28:54 +00:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2003-01-06 18:28:13 +00:00
|
|
|
static int
|
2003-02-14 22:42:11 +00:00
|
|
|
GIB_File_Transform_Path_Secure (dstring_t * path)
|
2002-11-14 05:28:54 +00:00
|
|
|
{
|
2003-05-23 17:17:01 +00:00
|
|
|
char *s;
|
2003-02-14 22:42:11 +00:00
|
|
|
|
|
|
|
for (s = strchr (path->str, '\\'); s; s = strchr (s, '\\'))
|
2002-12-13 23:36:05 +00:00
|
|
|
*s = '/';
|
|
|
|
if (Sys_PathType (path->str) != PATHTYPE_RELATIVE_BELOW)
|
|
|
|
return -1;
|
2003-05-22 02:32:38 +00:00
|
|
|
|
2002-12-13 23:36:05 +00:00
|
|
|
dstring_insertstr (path, 0, "/");
|
2003-02-16 21:27:18 +00:00
|
|
|
dstring_insertstr (path, 0, qfs_gamedir->dir.def);
|
2003-02-14 22:36:10 +00:00
|
|
|
dstring_insertstr (path, 0, "/");
|
2003-05-23 17:17:01 +00:00
|
|
|
dstring_insertstr (path, 0, qfs_userpath);
|
2002-12-13 23:36:05 +00:00
|
|
|
return 0;
|
2002-11-14 05:28:54 +00:00
|
|
|
}
|
2003-02-14 22:42:11 +00:00
|
|
|
|
2003-01-06 18:28:13 +00:00
|
|
|
static void
|
2002-08-27 23:25:52 +00:00
|
|
|
GIB_File_Read_f (void)
|
|
|
|
{
|
2002-11-15 23:27:07 +00:00
|
|
|
QFile *file;
|
2003-01-28 21:16:21 +00:00
|
|
|
char *path;
|
2003-02-14 22:42:11 +00:00
|
|
|
int len;
|
2002-12-13 23:36:05 +00:00
|
|
|
dstring_t *ret;
|
2002-08-27 23:25:52 +00:00
|
|
|
|
|
|
|
if (GIB_Argc () != 2) {
|
2002-12-13 23:36:05 +00:00
|
|
|
GIB_USAGE ("file");
|
2002-11-11 01:57:34 +00:00
|
|
|
return;
|
|
|
|
}
|
2002-11-14 05:28:54 +00:00
|
|
|
if (!*GIB_Argv (1)) {
|
2003-02-14 22:42:11 +00:00
|
|
|
GIB_Error ("file", "%s: null filename provided", GIB_Argv (0));
|
2002-08-27 23:25:52 +00:00
|
|
|
return;
|
|
|
|
}
|
2003-05-22 02:32:38 +00:00
|
|
|
|
2002-12-13 23:36:05 +00:00
|
|
|
if (!(ret = GIB_Return (0)))
|
|
|
|
return;
|
2002-11-14 05:28:54 +00:00
|
|
|
path = GIB_Argv (1);
|
2003-05-22 02:32:38 +00:00
|
|
|
QFS_FOpenFile (path, &file);
|
2002-11-15 23:27:07 +00:00
|
|
|
if (file) {
|
|
|
|
len = Qfilesize (file);
|
2003-02-14 22:42:11 +00:00
|
|
|
ret->size = len + 1;
|
2002-12-13 23:36:05 +00:00
|
|
|
dstring_adjust (ret);
|
|
|
|
Qread (file, ret->str, len);
|
|
|
|
ret->str[len] = 0;
|
2002-11-15 23:27:07 +00:00
|
|
|
Qclose (file);
|
2003-05-22 02:32:38 +00:00
|
|
|
} else {
|
2003-02-14 08:06:01 +00:00
|
|
|
GIB_Error ("file",
|
2003-02-14 22:42:11 +00:00
|
|
|
"%s: could not read %s: %s", GIB_Argv (0), path,
|
|
|
|
strerror (errno));
|
2002-08-27 23:25:52 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2003-01-06 18:28:13 +00:00
|
|
|
static void
|
2002-08-27 23:25:52 +00:00
|
|
|
GIB_File_Write_f (void)
|
|
|
|
{
|
2003-01-28 21:16:21 +00:00
|
|
|
char *path;
|
2003-02-14 22:42:11 +00:00
|
|
|
|
2002-08-27 23:25:52 +00:00
|
|
|
if (GIB_Argc () != 3) {
|
2002-12-13 23:36:05 +00:00
|
|
|
GIB_USAGE ("file data");
|
2002-08-27 23:25:52 +00:00
|
|
|
return;
|
|
|
|
}
|
2003-02-14 22:42:11 +00:00
|
|
|
if (!*GIB_Argv (1)) {
|
|
|
|
GIB_Error ("file", "%s: null filename provided", GIB_Argv (0));
|
2002-11-11 01:57:34 +00:00
|
|
|
return;
|
|
|
|
}
|
2003-05-22 02:32:38 +00:00
|
|
|
|
2003-02-14 22:42:11 +00:00
|
|
|
path = GIB_Argv (1);
|
2003-05-23 17:17:01 +00:00
|
|
|
QFS_WriteFile (va ("%s/%s", qfs_gamedir->dir.def, path),
|
|
|
|
GIB_Argv(2), GIB_Argd(2)->size-1);
|
2002-08-27 23:25:52 +00:00
|
|
|
}
|
|
|
|
|
2003-01-06 18:28:13 +00:00
|
|
|
static void
|
2002-08-27 23:25:52 +00:00
|
|
|
GIB_File_Find_f (void)
|
|
|
|
{
|
|
|
|
DIR *directory;
|
|
|
|
struct dirent *entry;
|
2003-02-14 22:42:11 +00:00
|
|
|
const char *path, *glob = 0;
|
|
|
|
char *s;
|
2002-08-27 23:25:52 +00:00
|
|
|
|
2002-11-16 03:52:07 +00:00
|
|
|
if (GIB_Argc () != 2) {
|
2002-12-13 23:36:05 +00:00
|
|
|
GIB_USAGE ("glob");
|
2002-08-27 23:25:52 +00:00
|
|
|
return;
|
|
|
|
}
|
2003-02-14 22:42:11 +00:00
|
|
|
if (GIB_File_Transform_Path (GIB_Argd (1))) {
|
2003-02-14 08:06:01 +00:00
|
|
|
GIB_Error ("access",
|
2003-02-14 22:42:11 +00:00
|
|
|
"%s: access to %s denied", GIB_Argv (0), GIB_Argv (1));
|
2002-11-16 03:52:07 +00:00
|
|
|
return;
|
|
|
|
}
|
2003-02-14 22:42:11 +00:00
|
|
|
path = GIB_Argv (1);
|
2002-11-16 03:52:07 +00:00
|
|
|
s = strrchr (path, '/');
|
2003-02-14 22:42:11 +00:00
|
|
|
if (!s) { // No slash in path
|
|
|
|
glob = path; // The glob is the entire argument
|
|
|
|
path = "."; // The path is the current directory
|
|
|
|
} else if (s == path) // Unix filesystem root (carne only)
|
2002-12-13 23:36:05 +00:00
|
|
|
path = "/";
|
|
|
|
else {
|
2003-02-14 22:42:11 +00:00
|
|
|
*s = 0; // Split the string at the final slash
|
|
|
|
glob = s + 1;
|
2002-08-27 23:25:52 +00:00
|
|
|
}
|
2002-11-14 05:28:54 +00:00
|
|
|
directory = opendir (path);
|
2002-08-27 23:25:52 +00:00
|
|
|
if (!directory) {
|
2003-02-14 08:06:01 +00:00
|
|
|
GIB_Error ("file",
|
2003-02-14 22:42:11 +00:00
|
|
|
"%s: could not open directory %s: %s", GIB_Argv (0), path,
|
|
|
|
strerror (errno));
|
2002-08-27 23:25:52 +00:00
|
|
|
return;
|
|
|
|
}
|
2003-01-28 21:16:21 +00:00
|
|
|
while ((entry = readdir (directory)))
|
2003-02-14 22:42:11 +00:00
|
|
|
if (strcmp (entry->d_name, ".") && strcmp (entry->d_name, "..")
|
|
|
|
&& !fnmatch (glob, entry->d_name, 0))
|
2003-01-28 21:16:21 +00:00
|
|
|
GIB_Return (entry->d_name);
|
2002-12-13 23:36:05 +00:00
|
|
|
closedir (directory);
|
2002-08-27 23:25:52 +00:00
|
|
|
}
|
2002-10-16 04:59:34 +00:00
|
|
|
|
2003-01-06 18:28:13 +00:00
|
|
|
static void
|
2002-10-16 04:59:34 +00:00
|
|
|
GIB_File_Move_f (void)
|
|
|
|
{
|
2003-02-14 22:42:11 +00:00
|
|
|
char *path1, *path2;
|
2002-10-16 04:59:34 +00:00
|
|
|
|
|
|
|
if (GIB_Argc () != 3) {
|
2002-12-13 23:36:05 +00:00
|
|
|
GIB_USAGE ("from_file to_file");
|
2002-10-16 04:59:34 +00:00
|
|
|
return;
|
|
|
|
}
|
2003-02-14 22:42:11 +00:00
|
|
|
if (GIB_File_Transform_Path (GIB_Argd (1))) {
|
2003-02-14 08:06:01 +00:00
|
|
|
GIB_Error ("access",
|
2003-02-14 22:42:11 +00:00
|
|
|
"%s: access to %s denied", GIB_Argv (0), GIB_Argv (1));
|
2002-10-16 04:59:34 +00:00
|
|
|
return;
|
|
|
|
}
|
2003-02-14 22:42:11 +00:00
|
|
|
if (GIB_File_Transform_Path (GIB_Argd (2))) {
|
2003-02-14 08:06:01 +00:00
|
|
|
GIB_Error ("access",
|
2003-02-14 22:42:11 +00:00
|
|
|
"%s: access to %s denied", GIB_Argv (0), GIB_Argv (2));
|
2002-10-16 04:59:34 +00:00
|
|
|
return;
|
|
|
|
}
|
2003-02-14 22:42:11 +00:00
|
|
|
path1 = GIB_Argv (1);
|
|
|
|
path2 = GIB_Argv (2);
|
2003-05-25 05:42:01 +00:00
|
|
|
if (QFS_Rename (path1, path2))
|
2003-02-14 22:42:11 +00:00
|
|
|
GIB_Error ("file", "%s: could not move %s to %s: %s", GIB_Argv (0),
|
|
|
|
path1, path2, strerror (errno));
|
2002-10-16 04:59:34 +00:00
|
|
|
}
|
|
|
|
|
2003-01-06 18:28:13 +00:00
|
|
|
static void
|
2002-10-16 04:59:34 +00:00
|
|
|
GIB_File_Delete_f (void)
|
|
|
|
{
|
2003-02-14 22:42:11 +00:00
|
|
|
char *path;
|
2002-10-16 04:59:34 +00:00
|
|
|
|
|
|
|
if (GIB_Argc () != 2) {
|
2002-12-13 23:36:05 +00:00
|
|
|
GIB_USAGE ("file");
|
2002-10-16 04:59:34 +00:00
|
|
|
return;
|
|
|
|
}
|
2003-02-14 22:42:11 +00:00
|
|
|
if (GIB_File_Transform_Path (GIB_Argd (1))) {
|
2003-02-14 08:06:01 +00:00
|
|
|
GIB_Error ("access",
|
2003-02-14 22:42:11 +00:00
|
|
|
"%s: access to %s denied", GIB_Argv (0), GIB_Argv (1));
|
2002-10-16 04:59:34 +00:00
|
|
|
return;
|
|
|
|
}
|
2002-11-14 05:28:54 +00:00
|
|
|
path = GIB_Argv (1);
|
2003-05-25 05:42:01 +00:00
|
|
|
if (QFS_Remove (path))
|
2003-02-14 22:42:11 +00:00
|
|
|
GIB_Error ("file", "%s: could not delete %s: %s", GIB_Argv (0), path,
|
|
|
|
strerror (errno));
|
2002-10-16 04:59:34 +00:00
|
|
|
}
|
|
|
|
|
2003-01-06 18:28:13 +00:00
|
|
|
static void
|
2002-08-28 08:00:35 +00:00
|
|
|
GIB_Range_f (void)
|
|
|
|
{
|
2003-02-14 22:42:11 +00:00
|
|
|
double i, inc, start, limit;
|
|
|
|
dstring_t *dstr;
|
|
|
|
|
2002-08-28 08:00:35 +00:00
|
|
|
if (GIB_Argc () < 3 || GIB_Argc () > 4) {
|
2002-12-13 23:36:05 +00:00
|
|
|
GIB_USAGE ("lower upper [step]");
|
2002-08-28 08:00:35 +00:00
|
|
|
return;
|
|
|
|
}
|
2003-02-14 22:42:11 +00:00
|
|
|
limit = atof (GIB_Argv (2));
|
|
|
|
start = atof (GIB_Argv (1));
|
2003-01-30 23:26:43 +00:00
|
|
|
if (GIB_Argc () == 4) {
|
2003-02-14 22:42:11 +00:00
|
|
|
if ((inc = atof (GIB_Argv (3))) == 0.0)
|
2003-01-30 23:26:43 +00:00
|
|
|
return;
|
|
|
|
} else
|
2002-08-28 08:00:35 +00:00
|
|
|
inc = limit < start ? -1.0 : 1.0;
|
2003-02-14 22:42:11 +00:00
|
|
|
for (i = atof (GIB_Argv (1)); inc < 0 ? i >= limit : i <= limit; i += inc) {
|
|
|
|
if (!(dstr = GIB_Return (0)))
|
2003-01-28 21:16:21 +00:00
|
|
|
return;
|
2003-02-14 22:42:11 +00:00
|
|
|
dsprintf (dstr, "%.10g", i);
|
2003-01-28 21:16:21 +00:00
|
|
|
}
|
2002-08-28 08:00:35 +00:00
|
|
|
}
|
|
|
|
|
2003-01-06 18:28:13 +00:00
|
|
|
static void
|
2002-10-06 04:40:18 +00:00
|
|
|
GIB_Print_f (void)
|
|
|
|
{
|
2003-02-14 22:42:11 +00:00
|
|
|
if (GIB_Argc () != 2) {
|
2002-12-13 23:36:05 +00:00
|
|
|
GIB_USAGE ("text");
|
2002-10-06 04:40:18 +00:00
|
|
|
return;
|
|
|
|
}
|
2003-02-14 22:42:11 +00:00
|
|
|
Sys_Printf ("%s", GIB_Argv (1));
|
2002-10-06 04:40:18 +00:00
|
|
|
}
|
|
|
|
|
2003-02-25 06:52:27 +00:00
|
|
|
static void
|
|
|
|
GIB_bp1_f (void)
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
GIB_bp2_f (void)
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
GIB_bp3_f (void)
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
GIB_bp4_f (void)
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
2002-08-03 06:04:00 +00:00
|
|
|
void
|
2002-11-14 05:28:54 +00:00
|
|
|
GIB_Builtin_Init (qboolean sandbox)
|
2002-08-03 06:04:00 +00:00
|
|
|
{
|
2002-08-24 05:14:46 +00:00
|
|
|
|
2002-11-14 05:28:54 +00:00
|
|
|
if (sandbox)
|
|
|
|
GIB_File_Transform_Path = GIB_File_Transform_Path_Secure;
|
|
|
|
else
|
|
|
|
GIB_File_Transform_Path = GIB_File_Transform_Path_Null;
|
2003-02-14 22:42:11 +00:00
|
|
|
|
2003-01-28 21:16:21 +00:00
|
|
|
GIB_Builtin_Add ("function", GIB_Function_f);
|
|
|
|
GIB_Builtin_Add ("function::get", GIB_Function_Get_f);
|
|
|
|
GIB_Builtin_Add ("function::export", GIB_Function_Export_f);
|
|
|
|
GIB_Builtin_Add ("local", GIB_Local_f);
|
|
|
|
GIB_Builtin_Add ("global", GIB_Global_f);
|
2003-02-16 19:46:34 +00:00
|
|
|
GIB_Builtin_Add ("delete", GIB_Delete_f);
|
2003-01-29 04:34:23 +00:00
|
|
|
GIB_Builtin_Add ("domain", GIB_Domain_f);
|
2003-02-14 08:06:01 +00:00
|
|
|
GIB_Builtin_Add ("domain::clear", GIB_Domain_Clear_f);
|
2003-01-28 21:16:21 +00:00
|
|
|
GIB_Builtin_Add ("return", GIB_Return_f);
|
|
|
|
GIB_Builtin_Add ("for", GIB_For_f);
|
2003-01-30 23:26:43 +00:00
|
|
|
GIB_Builtin_Add ("length", GIB_Length_f);
|
|
|
|
GIB_Builtin_Add ("equal", GIB_Equal_f);
|
2003-02-16 19:46:34 +00:00
|
|
|
GIB_Builtin_Add ("count", GIB_Count_f);
|
|
|
|
GIB_Builtin_Add ("contains", GIB_Contains_f);
|
2003-01-30 23:26:43 +00:00
|
|
|
GIB_Builtin_Add ("slice", GIB_Slice_f);
|
2003-02-14 08:06:01 +00:00
|
|
|
GIB_Builtin_Add ("slice::find", GIB_Slice_Find_f);
|
2003-01-30 23:26:43 +00:00
|
|
|
GIB_Builtin_Add ("split", GIB_Split_f);
|
2003-05-14 21:13:41 +00:00
|
|
|
GIB_Builtin_Add ("chomp", GIB_Chomp_f);
|
2003-01-28 21:16:21 +00:00
|
|
|
GIB_Builtin_Add ("regex::match", GIB_Regex_Match_f);
|
|
|
|
GIB_Builtin_Add ("regex::replace", GIB_Regex_Replace_f);
|
|
|
|
GIB_Builtin_Add ("regex::extract", GIB_Regex_Extract_f);
|
2003-05-16 07:24:58 +00:00
|
|
|
GIB_Builtin_Add ("text::white", GIB_Text_White_f);
|
|
|
|
GIB_Builtin_Add ("text::brown", GIB_Text_Brown_f);
|
2003-01-28 21:16:21 +00:00
|
|
|
GIB_Builtin_Add ("thread::create", GIB_Thread_Create_f);
|
|
|
|
GIB_Builtin_Add ("thread::kill", GIB_Thread_Kill_f);
|
2003-02-16 19:46:34 +00:00
|
|
|
GIB_Builtin_Add ("thread::getList", GIB_Thread_List_f);
|
2003-01-28 21:16:21 +00:00
|
|
|
GIB_Builtin_Add ("event::register", GIB_Event_Register_f);
|
|
|
|
GIB_Builtin_Add ("file::read", GIB_File_Read_f);
|
|
|
|
GIB_Builtin_Add ("file::write", GIB_File_Write_f);
|
|
|
|
GIB_Builtin_Add ("file::find", GIB_File_Find_f);
|
|
|
|
GIB_Builtin_Add ("file::move", GIB_File_Move_f);
|
|
|
|
GIB_Builtin_Add ("file::delete", GIB_File_Delete_f);
|
|
|
|
GIB_Builtin_Add ("range", GIB_Range_f);
|
|
|
|
GIB_Builtin_Add ("print", GIB_Print_f);
|
2003-02-25 06:52:27 +00:00
|
|
|
GIB_Builtin_Add ("bp1", GIB_bp1_f);
|
|
|
|
GIB_Builtin_Add ("bp2", GIB_bp2_f);
|
|
|
|
GIB_Builtin_Add ("bp3", GIB_bp3_f);
|
|
|
|
GIB_Builtin_Add ("bp4", GIB_bp4_f);
|
2002-08-03 06:04:00 +00:00
|
|
|
}
|