Aliases

Posted by Neves on Fri 24 May 2002 04:34 AM — 5 posts, 20,138 views.

USA #0
I downloades the alias snippet from Samson's page (alsherok.net), and it works nicely, but I am trying to make it so that if a player makes an alias with a semicolon in it, it will interpret it as more then one command, and first execute the command before the semicolon then try doing the one after the semicolon. Here is what I've come up with so far, and it isn't working too well, if you have any suggestions how I can get it to work.


    char *x;
    char arg2[MAX_INPUT_LENGTH];
    int y;
    y = 0;
    x = arg;

    for( x = arg; *x != '\0'; x++ )
        {
      if(*x == ';')
      {
        y = 0;
        interpret(ch, arg2);
        strcpy(arg2, arg);
        arg2 == NULL;
        return TRUE;
      }
            else
            {
              arg2[y] = *x;
        y++;
      }
        }


This is only the code meant to look for a semicolon, but it doesn't seem to work properly. I'm not sure how clear I was, I can explain again if you can't understand what I want to do.

-Jay
Amended on Fri 24 May 2002 05:40 AM by Nick Gammon
Australia Forum Administrator #1
For a start, you aren't terminating the new string with a null byte. Second, you are returning after finding a semicolon, which won't help. Third, the line "arg2 == NULL;" won't achieve much.

Try something like this:


char *x;
char arg2[MAX_INPUT_LENGTH];
int y;
y = 0;
x = arg;

for( x = arg; *x != '\0'; x++ )
  {
  if(*x == ';')
    {
    arg2[y] = 0;		// terminate arg2
    interpret(ch, arg2);  // interpret it
    y = 0;  // start at start of arg2 again
    }  // end of semicolon found
  else
    arg2[y++] = *x;  // copy it
  }  // end of for loop
  
// do final one after the semicolon

arg2[y] = 0;		// terminate arg2
if (arg2 [0] != '\0')   // if not empty
  interpret(ch, arg2);  // interpret it
USA #2
Thanks, but there was a slight error in it that I found for all those who want to use this code, change:


      if(*x == ';')
      {
        y = 0;
        interpret(ch, arg2);
        strcpy(arg2, arg);
        arg2 == NULL;
        return TRUE;
      }
            
to:

      if(*x == ';')
      {
        y = 0;
        if (arg2 [0] != '\0')   // if not empty
          interpret(ch, arg2);
        strcpy(arg2, arg);
        arg2 == NULL;
        return TRUE;
      }


In case a person puts two semicolons in a row this will make sure it doesn't turn up as a null char.

-Jay
Amended on Mon 27 May 2002 03:37 AM by Nick Gammon
Australia Forum Administrator #3
I'm surprised it works with this:



strcpy(arg2, arg);
arg2 == NULL;


First, I don't think the strcpy will achieve much, and the line:


arg2 == NULL;


definitely won't, because that is just a test (== means "is equal to").

That line doesn't change anything, it just tests to see if arg2 is NULL, and throws away the result.
USA #4
I made a mistake and copied my original code, I meant to say in your code there is a slight error that if a person puts two semicolons in a row it will come out with a null argument, so to fix it, change:

if(*x == ';')
{
arg2[y] = 0; // terminate arg2
interpret(ch, arg2); // interpret it
y = 0; // start at start of arg2 again
} // end of semicolon found

to:

if(*x == ';')
{
arg2[y] = 0; // terminate arg2
if (arg2 [0] != '\0') // if not empty
interpret(ch, arg2); // interpret it
y = 0; // start at start of arg2 again
} // end of semicolon found

Basically just adding a check to make sure arg2 is not empty before interpreting it.

Thanks,
Jay