DarkPlaces
Game engine based on the Quake 1 engine by id Software, developed by LadyHavoc
 
cmd.h
Go to the documentation of this file.
1/*
2Copyright (C) 1996-1997 Id Software, Inc.
3
4This program is free software; you can redistribute it and/or
5modify it under the terms of the GNU General Public License
6as published by the Free Software Foundation; either version 2
7of the License, or (at your option) any later version.
8
9This program is distributed in the hope that it will be useful,
10but WITHOUT ANY WARRANTY; without even the implied warranty of
11MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
12
13See the GNU General Public License for more details.
14
15You should have received a copy of the GNU General Public License
16along with this program; if not, write to the Free Software
17Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
18
19*/
20
21// cmd.h -- Command buffer and command execution
22
23//===========================================================================
24
25/*
26
27Any number of commands can be added in a frame, from several different sources.
28Most commands come from either keybindings or console line input, but remote
29servers can also send across commands and entire text files can be execed.
30
31The + command line options are also added to the command buffer.
32
33The game starts with a Cbuf_AddText ("exec quake.rc\n"); Cbuf_Execute ();
34
35*/
36
37#ifndef CMD_H
38#define CMD_H
39
40#include "qtypes.h"
41#include "qdefs.h"
42#include "com_list.h"
43
44struct cmd_state_s;
45
46// Command flags
47#define CF_NONE 0u
48#define CF_CLIENT (1u<<0)
49#define CF_SERVER (1u<<1)
50#define CF_CLIENT_FROM_SERVER (1u<<2)
51#define CF_SERVER_FROM_CLIENT (1u<<3)
52#define CF_CHEAT (1u<<4)
53#define CF_ARCHIVE (1u<<5)
54#define CF_READONLY (1u<<6)
55#define CF_NOTIFY (1u<<7)
56#define CF_SERVERINFO (1u<<8)
57#define CF_USERINFO (1u<<9)
58#define CF_PERSISTENT (1u<<10)
59#define CF_PRIVATE (1u<<11)
60#define CF_MAXFLAGSVAL ((1u<<12) - 1)
61// for internal use only!
62#define CF_REGISTERED (1u<<29)
63#define CF_DEFAULTSET (1u<<30)
64#define CF_ALLOCATED (1u<<31)
65// UBSan: unsigned literals because left shifting by 31 causes signed overflow, although it works as expected on x86.
66
67#define CF_SHARED (CF_CLIENT | CF_SERVER)
68
69typedef void(*xcommand_t) (struct cmd_state_s *cmd);
70
71typedef enum cmd_source_s
72{
77
78typedef struct cmd_alias_s
79{
80 struct cmd_alias_s *next;
82 char *value;
86
87typedef struct cmd_function_s
88{
89 unsigned flags;
90 struct cmd_function_s *next;
91 const char *name;
92 const char *description;
95 struct cmd_function_s *overridden;
99
101typedef struct cmd_userdefined_s
102{
103 // csqc functions - this is a mess
105
106 // aliases
108}
110
111typedef struct cmd_buf_s
112{
117 size_t maxsize;
118 size_t size;
119 char tokenizebuffer[CMD_TOKENIZELENGTH];
122 void *lock;
123} cmd_buf_t;
124
126typedef struct cmd_state_s
127{
128 struct mempool_s *mempool;
129
130 int argc;
131 const char *cmdline;
132 const char *argv[MAX_ARGS];
133 const char *null_string;
134 const char *args;
136
138
140
142
143 struct cvar_state_s *cvars;
145 unsigned cmd_flagsmask;
146
147 qbool (*Handle)(struct cmd_state_s *, struct cmd_function_s *, const char *, size_t, enum cmd_source_s);
148}
150
152qbool Cmd_CL_Callback(cmd_state_t *cmd, cmd_function_t *func, const char *text, size_t textlen, cmd_source_t src);
153qbool Cmd_SV_Callback(cmd_state_t *cmd, cmd_function_t *func, const char *text, size_t textlen, cmd_source_t src);
154
155typedef struct cmd_input_s
156{
160 size_t size;
161 size_t length;
162 char *text;
165
168
171extern cmd_state_t *cmd_local;
175
177
178void Cbuf_Lock(cmd_buf_t *cbuf);
179void Cbuf_Unlock(cmd_buf_t *cbuf);
180
184void Cbuf_AddText (cmd_state_t *cmd, const char *text);
185
190void Cbuf_InsertText (cmd_state_t *cmd, const char *text);
191
197void Cbuf_Execute (cmd_buf_t *cbuf);
199void Cbuf_Frame (cmd_buf_t *cbuf);
201void Cbuf_Clear(cmd_buf_t *cbuf);
202
203//===========================================================================
204
216void Cmd_Init(void);
217void Cmd_Shutdown(void);
218
220void Cmd_SaveInitState(void);
223void Cmd_RestoreInitState(void);
224
228void Cmd_AddCommand(unsigned flags, const char *cmd_name, xcommand_t function, const char *description);
229
231qbool Cmd_Exists (cmd_state_t *cmd, const char *cmd_name);
232
235const char *Cmd_CompleteCommand (cmd_state_t *cmd, const char *partial);
236
237int Cmd_CompleteAliasCountPossible (cmd_state_t *cmd, const char *partial);
238const char **Cmd_CompleteAliasBuildList (cmd_state_t *cmd, const char *partial);
239int Cmd_CompleteCountPossible (cmd_state_t *cmd, const char *partial);
240const char **Cmd_CompleteBuildList (cmd_state_t *cmd, const char *partial);
241void Cmd_CompleteCommandPrint (cmd_state_t *cmd, const char *partial);
242const char *Cmd_CompleteAlias (cmd_state_t *cmd, const char *partial);
243void Cmd_CompleteAliasPrint (cmd_state_t *cmd, const char *partial);
244
245// Enhanced console completion by Fett erich@heintz.com
246// Added by EvilTypeGuy eviltypeguy@qeradiant.com
247
248// The functions that execute commands get their parameters with these functions.
249static inline int Cmd_Argc (cmd_state_t *cmd)
250{
251 return cmd->argc;
252}
254static inline const char *Cmd_Argv(cmd_state_t *cmd, int arg)
255{
256 if (arg >= cmd->argc )
257 return cmd->null_string;
258 return cmd->argv[arg];
259}
260static inline const char *Cmd_Args (cmd_state_t *cmd)
261{
262 return cmd->args;
263}
264
267int Cmd_CheckParm (cmd_state_t *cmd, const char *parm);
268
271void Cmd_ExecuteString(cmd_state_t *cmd, const char *text, size_t textlen, cmd_source_t src, qbool lockmutex);
273void Cmd_PreprocessAndExecuteString(cmd_state_t *cmd, const char *text, size_t textlen, cmd_source_t src, qbool lockmutex);
274
281qbool Cmd_QuoteString(char *out, size_t outlen, const char *in, const char *quoteset, qbool putquotes);
282
284
286
287#endif
288
void Cbuf_Clear(cmd_buf_t *cbuf)
Clears all command buffers.
Definition cmd.c:436
void Cmd_CompleteCommandPrint(cmd_state_t *cmd, const char *partial)
Definition cmd.c:1870
const char * Cmd_CompleteAlias(cmd_state_t *cmd, const char *partial)
Definition cmd.c:1892
void Cbuf_InsertText(cmd_state_t *cmd, const char *text)
Definition cmd.c:292
cmd_source_t
Definition cmd.h:72
@ src_client
came in over a net connection as a clc_stringcmd host_client will be valid during this state.
Definition cmd.h:73
@ src_local
from the command buffer
Definition cmd.h:75
void Cbuf_Unlock(cmd_buf_t *cbuf)
Definition cmd.c:50
void Cmd_SaveInitState(void)
called by Host_Init, this marks cvars, commands and aliases with their init values
Definition cmd.c:2159
qbool Cmd_SV_Callback(cmd_state_t *cmd, cmd_function_t *func, const char *text, size_t textlen, cmd_source_t src)
Definition cmd.c:2045
const char ** Cmd_CompleteBuildList(cmd_state_t *cmd, const char *partial)
Definition cmd.c:1847
void Cbuf_AddText(cmd_state_t *cmd, const char *text)
Definition cmd.c:264
cmd_userdefined_t cmd_userdefined_all
aliases and csqc functions
Definition cmd.c:28
int Cmd_CheckParm(cmd_state_t *cmd, const char *parm)
Returns the position (1 to argc-1) in the command's argument list where the given parameter apears,...
Definition cmd.c:2140
void Cmd_RestoreInitState(void)
Restores cvars, commands and aliases to their init values and deletes any that were added since init.
Definition cmd.c:2180
int Cmd_CompleteCountPossible(cmd_state_t *cmd, const char *partial)
Definition cmd.c:1814
void Cmd_Init(void)
Command execution takes a null terminated string, breaks it into tokens, then searches for a command ...
Definition cmd.c:1492
void Cbuf_Frame(cmd_buf_t *cbuf)
Definition cmd.c:417
void Cmd_AddCommand(unsigned flags, const char *cmd_name, xcommand_t function, const char *description)
called by the init functions of other parts of the program to register commands and functions to call...
Definition cmd.c:1661
void Cmd_ClearCSQCCommands(cmd_state_t *cmd)
Definition cmd.c:1981
const char * Cmd_CompleteCommand(cmd_state_t *cmd, const char *partial)
attempts to match a partial command for automatic command line completion returns NULL if nothing fit...
Definition cmd.c:1783
void Cmd_CompleteAliasPrint(cmd_state_t *cmd, const char *partial)
Definition cmd.c:1911
void Cmd_ExecuteString(cmd_state_t *cmd, const char *text, size_t textlen, cmd_source_t src, qbool lockmutex)
Parses a single line of text into arguments and tries to execute it.
Definition cmd.c:2068
qbool Cmd_QuoteString(char *out, size_t outlen, const char *in, const char *quoteset, qbool putquotes)
quotes a string so that it can be used as a command argument again; quoteset is a string that contain...
Definition cmd.c:1000
qbool Cmd_CL_Callback(cmd_state_t *cmd, cmd_function_t *func, const char *text, size_t textlen, cmd_source_t src)
Definition cmd.c:2017
cmd_state_t * cmd_local
command interpreter for local commands injected by SVQC, CSQC, MQC, server or client engine code uses...
Definition cmd.c:25
static int Cmd_Argc(cmd_state_t *cmd)
Definition cmd.h:249
int Cmd_CompleteAliasCountPossible(cmd_state_t *cmd, const char *partial)
Definition cmd.c:1931
static const char * Cmd_Argv(cmd_state_t *cmd, int arg)
Cmd_Argv(cmd, ) will return an empty string (not a NULL) if arg > argc, so string operations are alwa...
Definition cmd.h:254
cmd_state_t * cmd_serverfromclient
command interpreter for server commands received over network from clients uses cmddefs_null
Definition cmd.c:26
const char ** Cmd_CompleteAliasBuildList(cmd_state_t *cmd, const char *partial)
Definition cmd.c:1961
qbool Cmd_Exists(cmd_state_t *cmd, const char *cmd_name)
used by the cvar code to check for cvar / command name overlap
Definition cmd.c:1762
void Cmd_PreprocessAndExecuteString(cmd_state_t *cmd, const char *text, size_t textlen, cmd_source_t src, qbool lockmutex)
Like Cmd_ExecuteString, but with variable expansion.
Definition cmd.c:1323
void Cbuf_Lock(cmd_buf_t *cbuf)
Definition cmd.c:45
void Cbuf_Execute(cmd_buf_t *cbuf)
Definition cmd.c:351
qbool host_stuffcmdsrun
Definition cmd.c:41
cmd_userdefined_t cmd_userdefined_null
intentionally empty
Definition cmd.c:29
static const char * Cmd_Args(cmd_state_t *cmd)
Definition cmd.h:260
void Cmd_NoOperation_f(cmd_state_t *cmd)
Definition cmd.c:2240
void Cmd_Shutdown(void)
Definition cmd.c:1575
void(* xcommand_t)(struct cmd_state_s *cmd)
Definition cmd.h:69
qbool Cmd_Callback(cmd_state_t *cmd, cmd_function_t *func)
Definition cmd.c:2008
float flags
void() predraw
const GLchar * name
Definition glquake.h:601
void cmd(string command,...)
string argv(float n)
prvm_eval_t * src
#define MAX_ARGS
maximum number of parameters to a console command or alias
Definition qdefs.h:101
#define CMD_TOKENIZELENGTH
maximum tokenizable commandline length (counting trailing 0)
Definition qdefs.h:166
#define MAX_ALIAS_NAME
Definition qdefs.h:99
float vec_t
Definition qtypes.h:68
bool qbool
Definition qtypes.h:9
qbool initstate
indicates this command existed at init
Definition cmd.h:83
struct cmd_alias_s * next
Definition cmd.h:80
char * value
Definition cmd.h:82
char * initialvalue
backup copy of value at init
Definition cmd.h:84
size_t maxsize
Definition cmd.h:117
qbool wait
Definition cmd.h:116
llist_t deferred
Definition cmd.h:114
llist_t start
Definition cmd.h:113
llist_t free
Definition cmd.h:115
size_t size
Definition cmd.h:118
int tokenizebufferpos
Definition cmd.h:120
void * lock
Definition cmd.h:122
double deferred_oldtime
Definition cmd.h:121
struct cmd_function_s * overridden
the engine cmd overriden by this QC cmd, if applicable
Definition cmd.h:95
const char * name
Definition cmd.h:91
qbool autofunc
Definition cmd.h:96
struct cmd_function_s * next
Definition cmd.h:90
qbool qcfunc
Definition cmd.h:94
qbool initstate
indicates this command existed at init
Definition cmd.h:97
unsigned flags
Definition cmd.h:89
const char * description
Definition cmd.h:92
xcommand_t function
Definition cmd.h:93
vec_t delay
Definition cmd.h:159
char * text
Definition cmd.h:162
size_t length
excludes \0 terminator
Definition cmd.h:161
qbool pending
Definition cmd.h:163
cmd_state_t * source
Definition cmd.h:158
size_t size
excludes \0 terminator
Definition cmd.h:160
llist_t list
Definition cmd.h:157
command interpreter state - the tokenizing and execution of commands, as well as pointers to which cv...
Definition cmd.h:127
unsigned cmd_flagsmask
cmd flags that identify this interpreter
Definition cmd.h:145
cmd_userdefined_t * userdefined
possible csqc functions and aliases to execute
Definition cmd.h:139
unsigned cvars_flagsmask
which CVAR_* flags should be visible to this interpreter? (CF_CLIENT | CF_SERVER, or just CF_SERVER)
Definition cmd.h:144
const char * null_string
Definition cmd.h:133
cmd_function_t * engine_functions
Definition cmd.h:141
cmd_buf_t * cbuf
Definition cmd.h:137
const char * cmdline
Definition cmd.h:131
int argc
Definition cmd.h:130
struct cvar_state_s * cvars
which cvar system is this cmd state able to access? (&cvars_all or &cvars_null)
Definition cmd.h:143
const char * args
Definition cmd.h:134
struct mempool_s * mempool
Definition cmd.h:128
cmd_source_t source
Definition cmd.h:135
container for user-defined QC functions and aliases, shared between different command interpreters
Definition cmd.h:102
cmd_function_t * qc_functions
Definition cmd.h:104
cmd_alias_t * alias
Definition cmd.h:107