2001-02-21 19:35:06 +00:00
|
|
|
/*
|
|
|
|
cmd.c
|
|
|
|
|
|
|
|
script command processing module
|
|
|
|
|
|
|
|
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
|
|
|
|
|
|
|
static __attribute__ ((unused)) const char rcsid[] =
|
|
|
|
"$Id$";
|
|
|
|
|
2001-02-21 19:35:06 +00:00
|
|
|
#ifdef HAVE_STRING_H
|
|
|
|
# include <string.h>
|
|
|
|
#endif
|
|
|
|
#ifdef HAVE_STRINGS_H
|
|
|
|
# include <strings.h>
|
|
|
|
#endif
|
|
|
|
#include <ctype.h>
|
2002-05-15 22:14:17 +00:00
|
|
|
#include <errno.h>
|
2002-07-31 05:19:03 +00:00
|
|
|
#include <stdlib.h>
|
2002-06-10 22:03:51 +00:00
|
|
|
#include <sys/types.h>
|
|
|
|
#include <dirent.h>
|
|
|
|
|
2002-07-31 05:19:03 +00:00
|
|
|
#include "QF/cbuf.h"
|
2002-07-31 22:03:53 +00:00
|
|
|
#include "QF/idparse.h"
|
2001-03-27 20:33:07 +00:00
|
|
|
#include "QF/cmd.h"
|
2001-08-22 22:03:16 +00:00
|
|
|
#include "QF/cvar.h"
|
2002-07-31 05:19:03 +00:00
|
|
|
#include "QF/dstring.h"
|
2001-03-27 20:33:07 +00:00
|
|
|
#include "QF/hash.h"
|
|
|
|
#include "QF/qargs.h"
|
2002-08-27 07:16:28 +00:00
|
|
|
#include "QF/quakefs.h"
|
2001-03-27 20:33:07 +00:00
|
|
|
#include "QF/sys.h"
|
|
|
|
#include "QF/zone.h"
|
2002-06-10 22:03:51 +00:00
|
|
|
|
2001-02-21 19:35:06 +00:00
|
|
|
typedef struct cmdalias_s {
|
|
|
|
struct cmdalias_s *next;
|
2002-03-21 19:47:31 +00:00
|
|
|
const char *name;
|
|
|
|
const char *value;
|
2001-02-21 19:35:06 +00:00
|
|
|
} cmdalias_t;
|
|
|
|
|
2002-07-31 05:19:03 +00:00
|
|
|
static cmdalias_t *cmd_alias;
|
2002-03-18 23:39:03 +00:00
|
|
|
|
2001-10-03 21:25:43 +00:00
|
|
|
cvar_t *cmd_warncmd;
|
2001-02-21 19:35:06 +00:00
|
|
|
|
2002-07-31 05:19:03 +00:00
|
|
|
static hashtab_t *cmd_alias_hash;
|
|
|
|
static hashtab_t *cmd_hash;
|
2001-07-06 17:45:32 +00:00
|
|
|
|
2002-07-31 05:19:03 +00:00
|
|
|
cbuf_args_t *cmd_args;
|
|
|
|
static cbuf_t *cmd_cbuf;
|
|
|
|
cmd_source_t cmd_source;
|
2001-02-21 19:35:06 +00:00
|
|
|
|
2001-07-30 01:01:39 +00:00
|
|
|
|
2002-05-11 03:00:14 +00:00
|
|
|
/* Command parsing functions */
|
2001-02-21 19:35:06 +00:00
|
|
|
|
|
|
|
typedef struct cmd_function_s {
|
|
|
|
struct cmd_function_s *next;
|
2002-03-21 19:47:31 +00:00
|
|
|
const char *name;
|
2001-02-21 19:35:06 +00:00
|
|
|
xcommand_t function;
|
2002-03-21 19:47:31 +00:00
|
|
|
const char *description;
|
2002-07-30 02:06:37 +00:00
|
|
|
qboolean pure;
|
2001-02-21 19:35:06 +00:00
|
|
|
} cmd_function_t;
|
|
|
|
|
|
|
|
static cmd_function_t *cmd_functions; // possible commands to execute
|
|
|
|
|
|
|
|
int
|
|
|
|
Cmd_Argc (void)
|
|
|
|
{
|
2002-07-31 05:19:03 +00:00
|
|
|
return cmd_args->argc;
|
2001-02-21 19:35:06 +00:00
|
|
|
}
|
|
|
|
|
2002-03-21 19:47:31 +00:00
|
|
|
const char *
|
2001-02-21 19:35:06 +00:00
|
|
|
Cmd_Argv (int arg)
|
|
|
|
{
|
2002-07-31 05:19:03 +00:00
|
|
|
if (arg >= cmd_args->argc)
|
2002-03-03 03:36:27 +00:00
|
|
|
return "";
|
2002-07-31 05:19:03 +00:00
|
|
|
return cmd_args->argv[arg]->str;
|
2002-03-27 06:24:19 +00:00
|
|
|
}
|
|
|
|
|
2002-03-21 19:47:31 +00:00
|
|
|
const char *
|
2001-07-10 18:25:54 +00:00
|
|
|
Cmd_Args (int start)
|
2001-02-21 19:35:06 +00:00
|
|
|
{
|
2002-07-31 05:19:03 +00:00
|
|
|
if (start >= cmd_args->argc)
|
2002-04-24 22:33:04 +00:00
|
|
|
return "";
|
2002-07-31 05:19:03 +00:00
|
|
|
return cmd_args->args[start];
|
2001-02-21 19:35:06 +00:00
|
|
|
}
|
|
|
|
|
2002-05-11 03:00:14 +00:00
|
|
|
void
|
2002-07-31 05:19:03 +00:00
|
|
|
Cmd_Command (cbuf_args_t *args)
|
2002-05-11 03:00:14 +00:00
|
|
|
{
|
|
|
|
cmd_function_t *cmd;
|
|
|
|
|
2002-07-31 05:19:03 +00:00
|
|
|
cmd_args = args;
|
|
|
|
//cmd_source = src;
|
2002-05-11 03:00:14 +00:00
|
|
|
|
2002-07-31 05:19:03 +00:00
|
|
|
if (!args->argc)
|
2002-05-11 03:00:14 +00:00
|
|
|
return; // no tokens
|
|
|
|
|
|
|
|
// check functions
|
2002-07-31 05:19:03 +00:00
|
|
|
cmd = (cmd_function_t *) Hash_Find (cmd_hash, args->argv[0]->str);
|
2002-05-11 03:00:14 +00:00
|
|
|
if (cmd) {
|
2002-07-31 05:19:03 +00:00
|
|
|
if (cmd->function) {
|
2002-05-11 03:00:14 +00:00
|
|
|
cmd->function ();
|
2002-07-31 05:19:03 +00:00
|
|
|
}
|
2002-05-11 03:00:14 +00:00
|
|
|
return;
|
|
|
|
}
|
2002-07-31 05:19:03 +00:00
|
|
|
// check cvars
|
2002-05-11 03:00:14 +00:00
|
|
|
if (Cvar_Command ())
|
|
|
|
return;
|
|
|
|
|
2002-11-14 02:10:55 +00:00
|
|
|
if (cbuf_active->strict)
|
|
|
|
Cbuf_Error ("command", "Command '%s' not found.", args->argv[0]->str);
|
|
|
|
else if (cmd_warncmd->int_val || developer->int_val)
|
2002-05-11 03:00:14 +00:00
|
|
|
Sys_Printf ("Unknown command \"%s\"\n", Cmd_Argv (0));
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Registers a command and handler function */
|
|
|
|
int
|
|
|
|
Cmd_AddCommand (const char *cmd_name, xcommand_t function,
|
|
|
|
const char *description)
|
|
|
|
{
|
|
|
|
cmd_function_t *cmd;
|
|
|
|
cmd_function_t **c;
|
|
|
|
|
2001-02-21 19:35:06 +00:00
|
|
|
// fail if the command already exists
|
2002-03-21 19:47:31 +00:00
|
|
|
cmd = (cmd_function_t *) Hash_Find (cmd_hash, cmd_name);
|
2001-02-21 19:35:06 +00:00
|
|
|
if (cmd) {
|
2001-09-21 04:22:46 +00:00
|
|
|
Sys_Printf ("Cmd_AddCommand: %s already defined\n", cmd_name);
|
2002-04-12 17:19:49 +00:00
|
|
|
return 0;
|
2001-02-21 19:35:06 +00:00
|
|
|
}
|
|
|
|
|
2002-03-29 07:43:02 +00:00
|
|
|
cmd = calloc (1, sizeof (cmd_function_t));
|
2002-05-14 06:37:28 +00:00
|
|
|
SYS_CHECKMEM (cmd);
|
2001-02-21 19:35:06 +00:00
|
|
|
cmd->name = cmd_name;
|
|
|
|
cmd->function = function;
|
|
|
|
cmd->description = description;
|
|
|
|
Hash_Add (cmd_hash, cmd);
|
|
|
|
for (c = &cmd_functions; *c; c = &(*c)->next)
|
2002-03-21 19:47:31 +00:00
|
|
|
if (strcmp ((*c)->name, cmd->name) >= 0)
|
2001-02-21 19:35:06 +00:00
|
|
|
break;
|
|
|
|
cmd->next = *c;
|
|
|
|
*c = cmd;
|
2002-04-12 17:19:49 +00:00
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
2002-05-11 03:00:14 +00:00
|
|
|
/* Unregisters a command */
|
2002-04-12 17:19:49 +00:00
|
|
|
int
|
|
|
|
Cmd_RemoveCommand (const char *name)
|
|
|
|
{
|
|
|
|
cmd_function_t *cmd;
|
|
|
|
cmd_function_t **c;
|
|
|
|
|
|
|
|
cmd = (cmd_function_t *) Hash_Del (cmd_hash, name);
|
|
|
|
if (!cmd)
|
|
|
|
return 0;
|
|
|
|
for (c = &cmd_functions; *c; c = &(*c)->next)
|
|
|
|
if ((*c)->next == cmd)
|
|
|
|
break;
|
|
|
|
(*c)->next = cmd->next;
|
|
|
|
free (cmd);
|
|
|
|
return 1;
|
2001-02-21 19:35:06 +00:00
|
|
|
}
|
|
|
|
|
2002-05-11 03:00:14 +00:00
|
|
|
/* Checks for the existance of a command */
|
2001-02-21 19:35:06 +00:00
|
|
|
qboolean
|
2001-07-15 07:04:17 +00:00
|
|
|
Cmd_Exists (const char *cmd_name)
|
2001-02-21 19:35:06 +00:00
|
|
|
{
|
|
|
|
cmd_function_t *cmd;
|
|
|
|
|
2002-03-21 19:47:31 +00:00
|
|
|
cmd = (cmd_function_t *) Hash_Find (cmd_hash, cmd_name);
|
2001-02-21 19:35:06 +00:00
|
|
|
if (cmd) {
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2002-05-11 03:00:14 +00:00
|
|
|
|
|
|
|
/* Command completion functions */
|
|
|
|
|
2002-03-21 19:47:31 +00:00
|
|
|
const char *
|
2001-07-15 07:04:17 +00:00
|
|
|
Cmd_CompleteCommand (const char *partial)
|
2001-02-21 19:35:06 +00:00
|
|
|
{
|
2002-03-21 19:47:31 +00:00
|
|
|
cmd_function_t *cmd;
|
|
|
|
int len;
|
2001-02-21 19:35:06 +00:00
|
|
|
|
|
|
|
len = strlen (partial);
|
|
|
|
|
|
|
|
if (!len)
|
|
|
|
return NULL;
|
|
|
|
|
2001-08-22 22:03:16 +00:00
|
|
|
// check for exact match
|
2001-02-21 19:35:06 +00:00
|
|
|
for (cmd = cmd_functions; cmd; cmd = cmd->next)
|
|
|
|
if (!strcasecmp (partial, cmd->name))
|
|
|
|
return cmd->name;
|
|
|
|
|
2001-08-22 22:03:16 +00:00
|
|
|
// check for partial match
|
2001-02-21 19:35:06 +00:00
|
|
|
for (cmd = cmd_functions; cmd; cmd = cmd->next)
|
|
|
|
if (!strncasecmp (partial, cmd->name, len))
|
|
|
|
return cmd->name;
|
|
|
|
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
2001-06-28 04:05:14 +00:00
|
|
|
/*
|
|
|
|
Cmd_CompleteCountPossible
|
|
|
|
|
|
|
|
New function for tab-completion system
|
|
|
|
Added by EvilTypeGuy
|
|
|
|
Thanks to Fett erich@heintz.com
|
|
|
|
Thanks to taniwha
|
|
|
|
*/
|
|
|
|
int
|
2001-07-15 07:04:17 +00:00
|
|
|
Cmd_CompleteCountPossible (const char *partial)
|
2001-06-28 04:05:14 +00:00
|
|
|
{
|
2002-03-21 19:47:31 +00:00
|
|
|
cmd_function_t *cmd;
|
|
|
|
int len;
|
|
|
|
int h;
|
|
|
|
|
2001-06-28 04:05:14 +00:00
|
|
|
h = 0;
|
2002-03-21 19:47:31 +00:00
|
|
|
len = strlen (partial);
|
|
|
|
|
2001-06-28 04:05:14 +00:00
|
|
|
if (!len)
|
|
|
|
return 0;
|
2002-03-21 19:47:31 +00:00
|
|
|
|
2001-06-28 04:05:14 +00:00
|
|
|
// Loop through the command list and count all partial matches
|
|
|
|
for (cmd = cmd_functions; cmd; cmd = cmd->next)
|
2002-03-21 19:47:31 +00:00
|
|
|
if (!strncasecmp (partial, cmd->name, len))
|
2001-06-28 04:05:14 +00:00
|
|
|
h++;
|
|
|
|
|
|
|
|
return h;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
Cmd_CompleteBuildList
|
|
|
|
|
|
|
|
New function for tab-completion system
|
|
|
|
Added by EvilTypeGuy
|
|
|
|
Thanks to Fett erich@heintz.com
|
|
|
|
Thanks to taniwha
|
|
|
|
*/
|
2002-03-21 19:47:31 +00:00
|
|
|
const char **
|
2001-07-15 07:04:17 +00:00
|
|
|
Cmd_CompleteBuildList (const char *partial)
|
2001-06-28 04:05:14 +00:00
|
|
|
{
|
2002-03-21 19:47:31 +00:00
|
|
|
cmd_function_t *cmd;
|
|
|
|
int len = 0;
|
|
|
|
int bpos = 0;
|
|
|
|
int sizeofbuf;
|
|
|
|
const char **buf;
|
|
|
|
|
|
|
|
sizeofbuf = (Cmd_CompleteCountPossible (partial) + 1) * sizeof (char *);
|
|
|
|
len = strlen (partial);
|
|
|
|
buf = malloc (sizeofbuf + sizeof (char *));
|
2001-06-28 04:05:14 +00:00
|
|
|
|
2002-05-14 06:37:28 +00:00
|
|
|
SYS_CHECKMEM (buf);
|
2001-06-28 04:05:14 +00:00
|
|
|
// Loop through the alias list and print all matches
|
|
|
|
for (cmd = cmd_functions; cmd; cmd = cmd->next)
|
2002-03-21 19:47:31 +00:00
|
|
|
if (!strncasecmp (partial, cmd->name, len))
|
2001-06-28 04:05:14 +00:00
|
|
|
buf[bpos++] = cmd->name;
|
|
|
|
|
|
|
|
buf[bpos] = NULL;
|
|
|
|
return buf;
|
|
|
|
}
|
|
|
|
|
2002-05-11 03:00:14 +00:00
|
|
|
/* Hash table functions for aliases and commands */
|
|
|
|
static void
|
|
|
|
cmd_alias_free (void *_a, void *unused)
|
2002-04-16 23:56:21 +00:00
|
|
|
{
|
2002-05-11 03:00:14 +00:00
|
|
|
cmdalias_t *a = (cmdalias_t *) _a;
|
|
|
|
|
|
|
|
free ((char *) a->name);
|
|
|
|
free ((char *) a->value);
|
|
|
|
free (a);
|
|
|
|
}
|
|
|
|
|
|
|
|
static const char *
|
|
|
|
cmd_alias_get_key (void *_a, void *unused)
|
|
|
|
{
|
|
|
|
cmdalias_t *a = (cmdalias_t *) _a;
|
|
|
|
|
|
|
|
return a->name;
|
|
|
|
}
|
|
|
|
|
|
|
|
static const char *
|
|
|
|
cmd_get_key (void *c, void *unused)
|
|
|
|
{
|
|
|
|
cmd_function_t *cmd = (cmd_function_t *) c;
|
|
|
|
|
|
|
|
return cmd->name;
|
2002-04-16 23:56:21 +00:00
|
|
|
}
|
|
|
|
|
2003-01-06 18:28:13 +00:00
|
|
|
static void
|
2002-07-30 02:06:37 +00:00
|
|
|
Cmd_Runalias_f (void)
|
2002-06-01 22:13:14 +00:00
|
|
|
{
|
|
|
|
cmdalias_t *a;
|
2002-07-30 02:06:37 +00:00
|
|
|
|
2002-06-01 22:13:14 +00:00
|
|
|
a = (cmdalias_t *) Hash_Find (cmd_alias_hash, Cmd_Argv (0));
|
2002-07-30 02:06:37 +00:00
|
|
|
|
2002-06-01 22:13:14 +00:00
|
|
|
if (a) {
|
2002-07-31 05:19:03 +00:00
|
|
|
Cbuf_InsertText (cbuf_active, a->value);
|
2002-06-01 22:13:14 +00:00
|
|
|
return;
|
|
|
|
} else {
|
2002-07-30 02:06:37 +00:00
|
|
|
Sys_Printf
|
|
|
|
("BUG: No alias found for registered command. Please report this to the QuakeForge development team.");
|
2002-06-01 22:13:14 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2003-01-06 18:28:13 +00:00
|
|
|
static void
|
2002-05-11 03:00:14 +00:00
|
|
|
Cmd_Alias_f (void)
|
2001-02-21 19:35:06 +00:00
|
|
|
{
|
2002-05-11 03:00:14 +00:00
|
|
|
cmdalias_t *alias;
|
|
|
|
char *cmd;
|
|
|
|
int i, c;
|
|
|
|
const char *s;
|
2001-02-21 19:35:06 +00:00
|
|
|
|
2002-05-11 03:00:14 +00:00
|
|
|
if (Cmd_Argc () == 1) {
|
|
|
|
Sys_Printf ("Current alias commands:\n");
|
|
|
|
for (alias = cmd_alias; alias; alias = alias->next)
|
|
|
|
Sys_Printf ("alias %s \"%s\"\n", alias->name, alias->value);
|
|
|
|
return;
|
|
|
|
}
|
2001-02-21 19:35:06 +00:00
|
|
|
|
2002-05-11 03:00:14 +00:00
|
|
|
s = Cmd_Argv (1);
|
|
|
|
// if the alias already exists, reuse it
|
|
|
|
alias = (cmdalias_t *) Hash_Find (cmd_alias_hash, s);
|
2002-05-11 06:09:50 +00:00
|
|
|
if (Cmd_Argc () == 2) {
|
|
|
|
if (alias)
|
2002-07-31 05:19:03 +00:00
|
|
|
Sys_Printf ("alias %s \"%s\"\n", alias->name, alias->value);
|
2002-05-11 06:09:50 +00:00
|
|
|
return;
|
|
|
|
}
|
2002-06-01 22:13:14 +00:00
|
|
|
if (alias)
|
2002-05-11 03:00:14 +00:00
|
|
|
free ((char *) alias->value);
|
2002-12-14 20:34:20 +00:00
|
|
|
else if (!Cmd_Exists (s)) {
|
2002-05-11 03:00:14 +00:00
|
|
|
cmdalias_t **a;
|
2001-02-21 19:35:06 +00:00
|
|
|
|
2002-05-11 03:00:14 +00:00
|
|
|
alias = calloc (1, sizeof (cmdalias_t));
|
2002-05-14 06:37:28 +00:00
|
|
|
SYS_CHECKMEM (alias);
|
2002-05-11 03:00:14 +00:00
|
|
|
alias->name = strdup (s);
|
|
|
|
Hash_Add (cmd_alias_hash, alias);
|
|
|
|
for (a = &cmd_alias; *a; a = &(*a)->next)
|
|
|
|
if (strcmp ((*a)->name, alias->name) >= 0)
|
|
|
|
break;
|
|
|
|
alias->next = *a;
|
|
|
|
*a = alias;
|
2002-12-14 20:34:20 +00:00
|
|
|
Cmd_AddCommand (alias->name, Cmd_Runalias_f, "Alias command.");
|
|
|
|
} else {
|
|
|
|
Sys_Printf ("alias: a command with the name \"%s\" already exists.\n", s);
|
|
|
|
return;
|
2002-05-11 03:00:14 +00:00
|
|
|
}
|
|
|
|
// copy the rest of the command line
|
|
|
|
cmd = malloc (strlen (Cmd_Args (1)) + 2); // can never be longer
|
2002-05-14 06:37:28 +00:00
|
|
|
SYS_CHECKMEM (cmd);
|
2002-05-11 03:00:14 +00:00
|
|
|
cmd[0] = 0; // start out with a null string
|
|
|
|
c = Cmd_Argc ();
|
|
|
|
for (i = 2; i < c; i++) {
|
|
|
|
strcat (cmd, Cmd_Argv (i));
|
|
|
|
if (i != c - 1)
|
|
|
|
strcat (cmd, " ");
|
|
|
|
}
|
|
|
|
|
|
|
|
alias->value = cmd;
|
2001-02-21 19:35:06 +00:00
|
|
|
}
|
|
|
|
|
2003-01-06 18:28:13 +00:00
|
|
|
static void
|
2002-05-11 03:00:14 +00:00
|
|
|
Cmd_UnAlias_f (void)
|
|
|
|
{
|
|
|
|
cmdalias_t *alias;
|
|
|
|
const char *s;
|
|
|
|
|
|
|
|
if (Cmd_Argc () != 2) {
|
|
|
|
Sys_Printf ("unalias <alias>: erase an existing alias\n");
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
s = Cmd_Argv (1);
|
|
|
|
alias = Hash_Del (cmd_alias_hash, s);
|
|
|
|
|
|
|
|
if (alias) {
|
|
|
|
cmdalias_t **a;
|
|
|
|
|
2002-06-10 22:03:51 +00:00
|
|
|
Cmd_RemoveCommand (alias->name);
|
2002-07-30 02:06:37 +00:00
|
|
|
|
2002-07-31 05:19:03 +00:00
|
|
|
for (a = &cmd_alias; *a != alias; a = &(*a)->next)
|
|
|
|
;
|
|
|
|
*a = alias->next;
|
|
|
|
|
|
|
|
free ((char *) alias->name);
|
|
|
|
free ((char *) alias->value);
|
|
|
|
free (alias);
|
|
|
|
} else {
|
|
|
|
Sys_Printf ("Unknown alias \"%s\"\n", s);
|
2002-05-11 06:09:50 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2003-01-06 18:28:13 +00:00
|
|
|
static void
|
2001-02-21 19:35:06 +00:00
|
|
|
Cmd_CmdList_f (void)
|
|
|
|
{
|
|
|
|
cmd_function_t *cmd;
|
|
|
|
int i;
|
|
|
|
int show_description = 0;
|
2002-07-30 02:06:37 +00:00
|
|
|
|
2002-03-21 19:47:31 +00:00
|
|
|
if (Cmd_Argc () > 1)
|
2001-02-21 19:35:06 +00:00
|
|
|
show_description = 1;
|
|
|
|
for (cmd = cmd_functions, i = 0; cmd; cmd = cmd->next, i++) {
|
|
|
|
if (show_description) {
|
2001-09-21 04:22:46 +00:00
|
|
|
Sys_Printf ("%-20s :\n%s\n", cmd->name, cmd->description);
|
2001-02-21 19:35:06 +00:00
|
|
|
} else {
|
2001-09-21 04:22:46 +00:00
|
|
|
Sys_Printf ("%s\n", cmd->name);
|
2001-02-21 19:35:06 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2001-09-21 04:22:46 +00:00
|
|
|
Sys_Printf ("------------\n%d commands\n", i);
|
2001-02-21 19:35:06 +00:00
|
|
|
}
|
|
|
|
|
2003-01-06 18:28:13 +00:00
|
|
|
static void
|
2001-07-30 01:01:39 +00:00
|
|
|
Cmd_Help_f (void)
|
|
|
|
{
|
2002-03-21 19:47:31 +00:00
|
|
|
const char *name;
|
|
|
|
cvar_t *var;
|
2001-07-30 01:01:39 +00:00
|
|
|
cmd_function_t *cmd;
|
|
|
|
|
|
|
|
if (Cmd_Argc () != 2) {
|
2001-09-21 04:22:46 +00:00
|
|
|
Sys_Printf ("usage: help <cvar/command>\n");
|
2001-07-30 01:01:39 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
name = Cmd_Argv (1);
|
|
|
|
|
2001-08-22 22:03:16 +00:00
|
|
|
for (cmd = cmd_functions; cmd && strcasecmp (name, cmd->name);
|
2002-07-30 02:06:37 +00:00
|
|
|
cmd = cmd->next);
|
2001-07-30 01:01:39 +00:00
|
|
|
if (cmd) {
|
2001-09-21 04:22:46 +00:00
|
|
|
Sys_Printf ("%s\n", cmd->description);
|
2001-07-30 01:01:39 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
var = Cvar_FindVar (name);
|
|
|
|
if (!var)
|
|
|
|
var = Cvar_FindAlias (name);
|
|
|
|
if (var) {
|
2001-09-21 04:22:46 +00:00
|
|
|
Sys_Printf ("%s\n", var->description);
|
2001-07-30 01:01:39 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2001-09-21 04:22:46 +00:00
|
|
|
Sys_Printf ("variable/command not found\n");
|
2001-07-30 01:01:39 +00:00
|
|
|
}
|
2003-01-06 18:28:13 +00:00
|
|
|
static void
|
2002-07-31 05:19:03 +00:00
|
|
|
Cmd_Exec_f (void)
|
2002-03-18 23:39:03 +00:00
|
|
|
{
|
2002-07-31 05:19:03 +00:00
|
|
|
char *f;
|
|
|
|
int mark;
|
2002-05-11 03:00:14 +00:00
|
|
|
|
|
|
|
if (Cmd_Argc () != 2) {
|
2002-07-31 05:19:03 +00:00
|
|
|
Sys_Printf ("exec <filename> : execute a script file\n");
|
2002-04-22 04:23:36 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2002-07-31 05:19:03 +00:00
|
|
|
mark = Hunk_LowMark ();
|
|
|
|
f = (char *) COM_LoadHunkFile (Cmd_Argv (1));
|
|
|
|
if (!f) {
|
|
|
|
Sys_Printf ("couldn't exec %s\n", Cmd_Argv (1));
|
2002-04-22 04:23:36 +00:00
|
|
|
return;
|
|
|
|
}
|
2002-07-31 05:19:03 +00:00
|
|
|
if (!Cvar_Command ()
|
|
|
|
&& (cmd_warncmd->int_val || (developer && developer->int_val)))
|
|
|
|
Sys_Printf ("execing %s\n", Cmd_Argv (1));
|
2002-11-09 07:13:52 +00:00
|
|
|
Cbuf_InsertText (cbuf_active, f);
|
2002-07-31 05:19:03 +00:00
|
|
|
Hunk_FreeToLowMark (mark);
|
2002-04-22 04:23:36 +00:00
|
|
|
}
|
|
|
|
|
2002-05-31 05:38:06 +00:00
|
|
|
/*
|
2002-07-31 05:19:03 +00:00
|
|
|
Cmd_Echo_f
|
2002-05-31 05:38:06 +00:00
|
|
|
|
2002-07-31 05:19:03 +00:00
|
|
|
Just prints the rest of the line to the console
|
2002-05-31 05:38:06 +00:00
|
|
|
*/
|
2003-01-06 18:28:13 +00:00
|
|
|
static void
|
2002-07-31 05:19:03 +00:00
|
|
|
Cmd_Echo_f (void)
|
2002-06-13 05:58:48 +00:00
|
|
|
{
|
2002-07-31 05:19:03 +00:00
|
|
|
if (Cmd_Argc () == 2)
|
|
|
|
Sys_Printf ("%s\n", Cmd_Argv (1));
|
|
|
|
else
|
|
|
|
Sys_Printf ("%s\n", Cmd_Args (1));
|
2002-06-13 05:58:48 +00:00
|
|
|
}
|
|
|
|
|
2002-07-31 05:19:03 +00:00
|
|
|
/* Pauses execution of the current stack until
|
|
|
|
next call of Cmd_Execute (usually next frame) */
|
2003-01-06 18:28:13 +00:00
|
|
|
static void
|
2002-07-31 05:19:03 +00:00
|
|
|
Cmd_Wait_f (void)
|
2002-05-31 05:38:06 +00:00
|
|
|
{
|
2002-07-31 22:03:53 +00:00
|
|
|
cbuf_active->state = CBUF_STATE_WAIT;
|
2002-05-15 22:14:17 +00:00
|
|
|
}
|
|
|
|
|
2002-10-13 19:46:47 +00:00
|
|
|
/* Pauses execution for a specified number
|
|
|
|
of seconds */
|
2003-01-06 18:28:13 +00:00
|
|
|
static void
|
2002-10-13 19:46:47 +00:00
|
|
|
Cmd_Sleep_f (void)
|
|
|
|
{
|
|
|
|
double waittime;
|
|
|
|
cbuf_t *p;
|
|
|
|
cbuf_active->state = CBUF_STATE_WAIT;
|
|
|
|
waittime = atof (Cmd_Argv (1));
|
|
|
|
for (p = cbuf_active; p->up; p = p->up); // Get to top of stack
|
|
|
|
p->resumetime = Sys_DoubleTime() + waittime;
|
|
|
|
}
|
|
|
|
|
2002-06-10 22:03:51 +00:00
|
|
|
void
|
2002-07-31 05:19:03 +00:00
|
|
|
Cmd_StuffCmds (cbuf_t *cbuf)
|
2002-06-10 22:03:51 +00:00
|
|
|
{
|
2002-07-31 05:19:03 +00:00
|
|
|
int i, j;
|
|
|
|
int s;
|
|
|
|
char *build, c;
|
2003-01-06 18:28:13 +00:00
|
|
|
char *cmdline;
|
2002-07-30 02:06:37 +00:00
|
|
|
|
2002-07-31 05:19:03 +00:00
|
|
|
s = strlen (com_cmdline);
|
|
|
|
if (!s)
|
2002-06-10 22:03:51 +00:00
|
|
|
return;
|
|
|
|
|
2003-01-06 18:28:13 +00:00
|
|
|
cmdline = strdup (com_cmdline);
|
2002-07-31 05:19:03 +00:00
|
|
|
// pull out the commands
|
|
|
|
build = malloc (s + 1);
|
|
|
|
SYS_CHECKMEM (build);
|
|
|
|
build[0] = 0;
|
2002-07-30 02:06:37 +00:00
|
|
|
|
2002-07-31 05:19:03 +00:00
|
|
|
for (i = 0; i < s - 1; i++) {
|
2003-01-06 18:28:13 +00:00
|
|
|
if (cmdline[i] == '+') {
|
2002-07-31 05:19:03 +00:00
|
|
|
i++;
|
2002-07-30 02:06:37 +00:00
|
|
|
|
2003-01-06 18:28:13 +00:00
|
|
|
for (j = i; !((cmdline[j] == '+')
|
|
|
|
|| (cmdline[j] == '-'
|
|
|
|
&& (j == 0 || cmdline[j - 1] == ' '))
|
|
|
|
|| (cmdline[j] == 0)); j++);
|
2002-05-15 22:14:17 +00:00
|
|
|
|
2003-01-06 18:28:13 +00:00
|
|
|
c = cmdline[j];
|
|
|
|
cmdline[j] = 0;
|
2002-07-31 05:19:03 +00:00
|
|
|
|
2003-01-06 18:28:13 +00:00
|
|
|
strncat (build, cmdline + i, s - strlen (build));
|
2002-07-31 05:19:03 +00:00
|
|
|
strncat (build, "\n", s - strlen (build));
|
2003-01-06 18:28:13 +00:00
|
|
|
cmdline[j] = c;
|
2002-07-31 05:19:03 +00:00
|
|
|
i = j - 1;
|
|
|
|
}
|
2002-05-15 22:14:17 +00:00
|
|
|
}
|
|
|
|
|
2002-07-31 05:19:03 +00:00
|
|
|
if (build[0])
|
|
|
|
Cbuf_InsertText (cbuf, build);
|
2002-05-11 00:36:12 +00:00
|
|
|
|
2002-07-31 05:19:03 +00:00
|
|
|
free (build);
|
2003-01-06 18:28:13 +00:00
|
|
|
free (cmdline);
|
2002-05-11 00:36:12 +00:00
|
|
|
}
|
2002-05-11 03:00:14 +00:00
|
|
|
|
2003-01-06 18:28:13 +00:00
|
|
|
static void
|
2002-07-31 05:19:03 +00:00
|
|
|
Cmd_StuffCmds_f (void)
|
2002-05-11 00:36:12 +00:00
|
|
|
{
|
2002-07-31 05:19:03 +00:00
|
|
|
Cmd_StuffCmds (cbuf_active);
|
2002-05-11 00:36:12 +00:00
|
|
|
}
|
|
|
|
|
2001-02-21 19:35:06 +00:00
|
|
|
void
|
|
|
|
Cmd_Init_Hash (void)
|
|
|
|
{
|
2001-11-12 03:47:30 +00:00
|
|
|
cmd_hash = Hash_NewTable (1021, cmd_get_key, 0, 0);
|
2002-03-21 19:47:31 +00:00
|
|
|
cmd_alias_hash = Hash_NewTable (1021, cmd_alias_get_key, cmd_alias_free, 0);
|
2001-02-21 19:35:06 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
Cmd_Init (void)
|
|
|
|
{
|
2001-08-22 22:03:16 +00:00
|
|
|
Cmd_AddCommand ("stuffcmds", Cmd_StuffCmds_f, "Execute the commands given "
|
|
|
|
"at startup again");
|
|
|
|
Cmd_AddCommand ("alias", Cmd_Alias_f, "Used to create a reference to a "
|
|
|
|
"command or list of commands.\n"
|
|
|
|
"When used without parameters, displays all current "
|
|
|
|
"aliases.\n"
|
|
|
|
"Note: Enclose multiple commands within quotes and "
|
|
|
|
"seperate each command with a semi-colon.");
|
2001-02-21 19:35:06 +00:00
|
|
|
Cmd_AddCommand ("unalias", Cmd_UnAlias_f, "Remove the selected alias");
|
|
|
|
Cmd_AddCommand ("cmdlist", Cmd_CmdList_f, "List all commands");
|
2001-08-22 22:03:16 +00:00
|
|
|
Cmd_AddCommand ("help", Cmd_Help_f, "Display help for a command or "
|
|
|
|
"variable");
|
2002-07-31 05:19:03 +00:00
|
|
|
Cmd_AddCommand ("exec", Cmd_Exec_f, "Execute a script file");
|
|
|
|
Cmd_AddCommand ("echo", Cmd_Echo_f, "Print text to console");
|
|
|
|
Cmd_AddCommand ("wait", Cmd_Wait_f, "Wait a game tic");
|
2002-10-13 19:46:47 +00:00
|
|
|
Cmd_AddCommand ("sleep", Cmd_Sleep_f, "Wait for a certain number of seconds.");
|
2001-10-15 22:41:42 +00:00
|
|
|
cmd_warncmd = Cvar_Get ("cmd_warncmd", "0", CVAR_NONE, NULL, "Toggles the "
|
2001-10-27 04:34:53 +00:00
|
|
|
"display of error messages for unknown commands");
|
2002-08-03 06:04:00 +00:00
|
|
|
cmd_cbuf = Cbuf_New (&id_interp);
|
2001-02-21 19:35:06 +00:00
|
|
|
}
|
|
|
|
|
2002-07-31 05:19:03 +00:00
|
|
|
int
|
|
|
|
Cmd_ExecuteString (const char *text, cmd_source_t src)
|
2001-12-03 21:06:57 +00:00
|
|
|
{
|
2002-07-31 05:19:03 +00:00
|
|
|
cmd_source = src;
|
|
|
|
COM_TokenizeString (text, cmd_cbuf->args);
|
|
|
|
Cmd_Command (cmd_cbuf->args);
|
|
|
|
return 0;
|
2001-12-03 21:06:57 +00:00
|
|
|
}
|
2001-02-21 19:35:06 +00:00
|
|
|
|
2002-07-31 05:19:03 +00:00
|
|
|
void
|
2002-11-08 16:39:28 +00:00
|
|
|
Cmd_Exec_File (cbuf_t *cbuf, const char *path, int qfs)
|
2001-02-21 19:35:06 +00:00
|
|
|
{
|
2002-07-31 05:19:03 +00:00
|
|
|
char *f;
|
|
|
|
int len;
|
2002-08-27 07:16:28 +00:00
|
|
|
QFile *file;
|
2001-02-21 19:35:06 +00:00
|
|
|
|
2002-07-31 05:19:03 +00:00
|
|
|
if (!path || !*path)
|
|
|
|
return;
|
2002-11-08 16:39:28 +00:00
|
|
|
if (qfs) {
|
|
|
|
COM_FOpenFile (path, &file);
|
|
|
|
} else {
|
|
|
|
file = Qopen (path, "r");
|
|
|
|
}
|
|
|
|
if (file) {
|
2002-09-27 04:27:19 +00:00
|
|
|
len = Qfilesize (file);
|
2002-07-31 05:19:03 +00:00
|
|
|
f = (char *) malloc (len + 1);
|
|
|
|
if (f) {
|
|
|
|
f[len] = 0;
|
|
|
|
Qread (file, f, len);
|
2002-07-31 06:06:44 +00:00
|
|
|
Cbuf_InsertText (cbuf, f);
|
2002-07-31 05:19:03 +00:00
|
|
|
free (f);
|
2001-02-21 19:35:06 +00:00
|
|
|
}
|
2002-11-08 16:39:28 +00:00
|
|
|
Qclose (file);
|
2001-02-21 19:35:06 +00:00
|
|
|
}
|
2002-07-31 05:19:03 +00:00
|
|
|
}
|
2001-02-21 19:35:06 +00:00
|
|
|
|