Cbuf_Execute
============
*/
+static void Cmd_PreprocessString( const char *intext, char *outtext, unsigned maxoutlen, cmdalias_t *alias );
void Cbuf_Execute (void)
{
+#define EXECUTESTRING_BUFFER 2048
int i;
char *text;
char line[1024];
+ char preprocessed[EXECUTESTRING_BUFFER];
int quotes;
// LordHavoc: making sure the tokenizebuffer doesn't get filled up by repeated crashes
}
// execute the command line
- Cmd_ExecuteString (line, src_command);
+ Cmd_PreprocessString( line, preprocessed, EXECUTESTRING_BUFFER, NULL );
+ Cmd_ExecuteString (preprocessed, src_command);
if (cmd_wait)
{ // skip out while text still remains in buffer, leaving it
static int cmd_argc;
static const char *cmd_argv[MAX_ARGS];
static const char *cmd_null_string = "";
-static const char *cmd_args = NULL;
-
+static const char *cmd_args;
cmd_source_t cmd_source;
*/
static void Cmd_PreprocessString( const char *intext, char *outtext, unsigned maxoutlen, cmdalias_t *alias ) {
const char *in;
- char *out;
unsigned outlen;
int inquote;
- // HACK?
+ // don't crash if there's no room in the outtext buffer
if( maxoutlen == 0 ) {
return;
}
maxoutlen--; // because of \0
in = intext;
- out = outtext;
outlen = 0;
inquote = 0;
while( *in && outlen < maxoutlen ) {
if( *in == '$' && !inquote ) {
- // read over the $
+ // this is some kind of expansion, see what comes after the $
in++;
- // $* is replaced with all formal parameters, $num is parsed as an argument (or as $num if there arent enough parameters), $bla becomes $bla and $$bla becomes $$bla
- if( *in == '*' && alias ) {
+ // replacements that can always be used:
+ // $$ is replaced with $, to allow escaping $
+ // $<cvarname> is replaced with the contents of the cvar
+ //
+ // the following can be used in aliases only:
+ // $* is replaced with all formal parameters (including name of the alias - this probably is not desirable)
+ // $0 is replaced with the name of this alias
+ // $<number> is replaced with an argument to this alias (or copied as-is if no such parameter exists), can be multiple digits
+ if( *in == '$' ) {
+ outtext[outlen++] = *in++;
+ } else if( *in == '*' && alias ) {
const char *linein = Cmd_Args();
// include all params
if (linein) {
while( *linein && outlen < maxoutlen ) {
- *out++ = *linein++;
- outlen++;
+ outtext[outlen++] = *linein++;
}
}
argnum = strtol( in, &nexttoken, 10 );
- if( 0 < argnum && argnum < Cmd_Argc() ) {
+ if( 0 <= argnum && argnum < Cmd_Argc() ) {
const char *param = Cmd_Argv( argnum );
while( *param && outlen < maxoutlen ) {
- *out++ = *param++;
- outlen++;
+ outtext[outlen++] = *param++;
}
in = nexttoken;
} else if( argnum >= Cmd_Argc() ) {
Con_Printf( "Warning: Not enough parameters passed to alias '%s', at least %i expected:\n %s\n", alias->name, argnum, alias->value );
- *out++ = '$';
- outlen++;
+ outtext[outlen++] = '$';
}
} else {
cvar_t *cvar;
const char *tempin = in;
COM_ParseTokenConsole( &tempin );
- cvar = Cvar_FindVar(&com_token[0]);
- if (cvar) {
+ if ((cvar = Cvar_FindVar(&com_token[0]))) {
const char *cvarcontent = cvar->string;
while( *cvarcontent && outlen < maxoutlen ) {
- *out++ = *cvarcontent++;
- outlen++;
- }
- in = tempin;
- } else if( com_token[0] == '$' ) {
- // remove the first $
- char *pos = com_token;
- while( *pos && outlen < maxoutlen ) {
- *out++ = *pos++;
- outlen++;
+ outtext[outlen++] = *cvarcontent++;
}
in = tempin;
+ } else {
+ Con_Printf( "Warning: could not find cvar %s when expanding alias %s\n %s\n", com_token, alias->name, alias->value );
+ outtext[outlen++] = '$';
}
}
} else {
if( *in == '"' ) {
inquote ^= 1;
- }
- *out++ = *in++;
- outlen++;
+ }
+ outtext[outlen++] = *in++;
}
}
- *out = 0;
+ outtext[outlen] = 0;
}
/*
*/
static void Cmd_ExecuteAlias (cmdalias_t *alias)
{
- /*
-#define ALIAS_BUFFER 1024
- static char buffer[ ALIAS_BUFFER + 2 ];
- const char *in;
- char *out;
- unsigned outlen;
- int inquote;
-
- in = alias->value;
- out = buffer;
- outlen = 0;
- inquote = 0;
-
- while( *in && outlen < ALIAS_BUFFER )
- {
- if( *in == '"' )
- {
- inquote ^= 1;
- }
- else if( *in == '$' && !inquote )
- {
- // $* is replaced with all formal parameters, $num is parsed as an argument (or as $num if there arent enough parameters), $bla becomes $bla and $$bla becomes $$bla
- // read over the $
- in++;
- if( *in == '*' )
- {
- const char *linein = Cmd_Args();
- // include all params
- if (linein) {
- while( *linein && outlen < ALIAS_BUFFER ) {
- *out++ = *linein++;
- outlen++;
- }
- }
-
- in++;
- } else {
- char *nexttoken;
- int argnum;
-
- argnum = strtol( in, &nexttoken, 10 );
-
- if( 0 < argnum && argnum < Cmd_Argc() )
- {
- const char *param = Cmd_Argv( argnum );
- while( *param && outlen < ALIAS_BUFFER ) {
- *out++ = *param++;
- outlen++;
- }
- in = nexttoken;
- }
- else if( argnum >= Cmd_Argc() )
- {
- Con_Printf( "Warning: Not enough parameters passed to alias '%s', at least %i expected:\n %s\n", alias->name, argnum, alias->value );
- *out++ = '$';
- outlen++;
- }
- // not a number
- else if( argnum == 0 )
- {
- *out++ = '$';
- outlen++;
- }
- }
- } else {
- *out++ = *in++;
- outlen++;
- }
- }
- *out++ = '\n';
- *out++ = 0;*/
#define ALIAS_BUFFER 1024
static char buffer[ ALIAS_BUFFER + 2 ];
Cmd_PreprocessString( alias->value, buffer, ALIAS_BUFFER, alias );
count++;
}
- Con_Printf("%i Command%s", count, (count > 1) ? "s" : "");
if (partial)
- Con_Printf(" beginning with \"%s\"", partial);
-
- Con_Print("\n\n");
+ Con_Printf("%i Command%s beginning with \"%s\"\n\n", count, (count > 1) ? "s" : "", partial);
+ else
+ Con_Printf("%i Command%s\n\n", count, (count > 1) ? "s" : "");
}
/*
*/
void Cmd_ExecuteString (const char *text, cmd_source_t src)
{
-#define EXECUTESTRING_BUFFER 4096
- static char buffer[ EXECUTESTRING_BUFFER ];
int oldpos;
cmd_function_t *cmd;
cmdalias_t *a;
oldpos = cmd_tokenizebufferpos;
cmd_source = src;
- Cmd_PreprocessString( text, buffer, EXECUTESTRING_BUFFER, NULL );
- Cmd_TokenizeString (buffer);
+ Cmd_TokenizeString (text);
// execute the command line
if (!Cmd_Argc())