+/*
+Cmd_PreprocessString
+
+Preprocesses strings and replaces $*, $param#, $cvar accordingly
+*/
+static void Cmd_PreprocessString( const char *intext, char *outtext, unsigned maxoutlen, cmdalias_t *alias ) {
+ const char *in;
+ unsigned outlen;
+ int inquote;
+
+ // don't crash if there's no room in the outtext buffer
+ if( maxoutlen == 0 ) {
+ return;
+ }
+ maxoutlen--; // because of \0
+
+ in = intext;
+ outlen = 0;
+ inquote = 0;
+
+ while( *in && outlen < maxoutlen ) {
+ if( *in == '$' && !inquote ) {
+ // this is some kind of expansion, see what comes after the $
+ in++;
+ // 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 parameters
+ if (linein) {
+ while( *linein && outlen < maxoutlen ) {
+ outtext[outlen++] = *linein++;
+ }
+ }
+
+ in++;
+ } else if( '0' <= *in && *in <= '9' && alias ) {
+ char *nexttoken;
+ int argnum;
+
+ argnum = strtol( in, &nexttoken, 10 );
+
+ if( 0 <= argnum && argnum < Cmd_Argc() ) {
+ const char *param = Cmd_Argv( argnum );
+ while( *param && outlen < maxoutlen ) {
+ 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 );
+ outtext[outlen++] = '$';
+ }
+ } else {
+ cvar_t *cvar;
+ const char *tempin = in;
+
+ COM_ParseTokenConsole( &tempin );
+ if ((cvar = Cvar_FindVar(&com_token[0]))) {
+ const char *cvarcontent = cvar->string;
+ while( *cvarcontent && outlen < maxoutlen ) {
+ outtext[outlen++] = *cvarcontent++;
+ }
+ in = tempin;
+ } else {
+ if( alias ) {
+ Con_Printf( "Warning: could not find cvar %s when expanding alias %s\n %s\n", com_token, alias->name, alias->value );
+ } else {
+ Con_Printf( "Warning: could not find cvar %s\n", com_token );
+ }
+ outtext[outlen++] = '$';
+ }
+ }
+ } else {
+ if( *in == '"' ) {
+ inquote ^= 1;
+ }
+ outtext[outlen++] = *in++;
+ }
+ }
+ outtext[outlen] = 0;
+}
+
+/*
+============
+Cmd_ExecuteAlias
+
+Called for aliases and fills in the alias into the cbuffer
+============
+*/
+static void Cmd_ExecuteAlias (cmdalias_t *alias)
+{
+ static char buffer[ MAX_INPUTLINE + 2 ];
+ Cmd_PreprocessString( alias->value, buffer, sizeof(buffer) - 2, alias );
+ // insert at start of command buffer, so that aliases execute in order
+ // (fixes bug introduced by Black on 20050705)
+ Cbuf_InsertText( buffer );
+}
+