]> git.rm.cloudns.org Git - xonotic/darkplaces.git/commitdiff
Fixed alias parsing and added support for $* which pastes all formal parameters.
authorblack <black@d7cf8633-e32d-0410-b094-e92efae38249>
Wed, 10 Aug 2005 10:03:38 +0000 (10:03 +0000)
committerblack <black@d7cf8633-e32d-0410-b094-e92efae38249>
Wed, 10 Aug 2005 10:03:38 +0000 (10:03 +0000)
git-svn-id: svn://svn.icculus.org/twilight/trunk/darkplaces@5566 d7cf8633-e32d-0410-b094-e92efae38249

cmd.c

diff --git a/cmd.c b/cmd.c
index 729469fc723c449692e773f1d42009118e82f402..7f83d6dc0441a9d7f77aa8f094677bba3b43658d 100644 (file)
--- a/cmd.c
+++ b/cmd.c
@@ -151,8 +151,8 @@ void Cbuf_Execute (void)
                for (i=0 ; i< cmd_text.cursize ; i++)
                {
                        if (text[i] == '"')
-                               quotes++;
-                       if ( !(quotes&1) &&  text[i] == ';')
+                               quotes ^= 1;
+                       if ( !quotes &&  text[i] == ';')
                                break;  // don't break if inside a quoted string
                        if (text[i] == '\r' || text[i] == '\n')
                                break;
@@ -403,40 +403,75 @@ Called for aliases and fills in the alias into the cbuffer
 */
 static void Cmd_ExecuteAlias (cmdalias_t *alias)
 {
-       const char *text = alias->value;
-       
-       while( COM_ParseTokenConsole( &text ) ) 
+#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 ) 
        {
-               Cbuf_AddText( "\"" );
-
-               if( com_token[0] == '$' )
+               if( *in == '"' ) 
                {
-                       int argNum;
-                       argNum = atoi( &com_token[1] );
-
-                       // no number at all?
-                       if( argNum == 0 )
-                       {
-                               Cbuf_AddText( com_token );
-                       }
-                       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 );
-                               Cbuf_AddText( com_token );
-                       }
-                       else
+                       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 == '*' ) 
                        {
-                               Cbuf_AddText( Cmd_Argv( argNum ) );
+                               const char *linein = Cmd_Args();
+                               // include all params
+                               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++;
                }
-               else 
-               {
-                       Cbuf_AddText( com_token );
-               }
-
-               Cbuf_AddText( "\"" );
        }
-       Cbuf_AddText( "\n" );
+       *out++ = '\n';
+       *out++ = 0;
+       Cbuf_AddText( buffer );
 }
 
 /*