+/*
+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;
+ char *out;
+ unsigned outlen;
+ int inquote;
+
+ // HACK?
+ 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 $
+ 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 ) {
+ const char *linein = Cmd_Args();
+ // include all params
+ if (linein) {
+ while( *linein && outlen < maxoutlen ) {
+ *out++ = *linein++;
+ outlen++;
+ }
+ }
+
+ 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 ) {
+ *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++;
+ }
+ } else {
+ cvar_t *cvar;
+ const char *tempin = in;
+
+ COM_ParseTokenConsole( &tempin );
+ cvar = Cvar_FindVar(&com_token[0]);
+ if (cvar) {
+ 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++;
+ }
+ in = tempin;
+ }
+ }
+ } else {
+ if( *in == '"' ) {
+ inquote ^= 1;
+ }
+ *out++ = *in++;
+ outlen++;
+ }
+ }
+ *out = 0;
+}
+