I thought I fixed it, but apparently not.
I have it 90% working, but I still have the issue with main's args that need to change.
Instead of being vague, this time I am going to be very specific because this project is due before midnight tonight.
This is the relevant code FROM main:
Code:
int main(int argc, char **argv) {
/* ... ... ...*/
char buf[256];
char buf2[256];
char *words[32];
int num_words;
/* ... */
char *command_name = words[0];
/* ... */
cmd_fxn_t cmd_to_do = lookup_c(command_name, &num_words, words, buf2);
/* ... */
command_name = words[0];
if(cmd_to_do != NULL) {
// and do it
int elapsed_time = cmd_to_do(num_words, words);
tick_world(elapsed_time, is_player_vulnerable(command_name));
}
words MUST be changed in that call for the int elapsed_time and tick_world to work.
However, I haven't figured out how to change num_words in lookup_c.
Here is ALL the code from lookup_command, which is in a different file:
Code:
cmd_fxn_t lookup_c( char* name, int* num_words_ptr, char** argv, char* buf )
{
// Tokenizer format: tokenizer(char *string, char **argv, char *delims) --> argv[0] = 1st word in *string, etc
const char* str = find_alias( name ) ;
char* new_name ;
char* new_copy ;
char* str_tokens[ MAX_SENTENCE ] ;
int str_num, i ;
// If an alias is found, substitute the alias with the corresponding string, and change argv and num_words accordingly.
if ( str )
{
// (Specs): "Make sure you don't leave any unfreed memory with your implementation."
new_name = ( char* ) malloc( sizeof( char ) * MAX_WORD ) ;
// Copy str to buf
strncpy( buf, str, MAX_SENTENCE ) ;
// Tokenize
str_num = tokenizer( buf, str_tokens, " " ) ;
// Set name
strncpy( new_name, str_tokens[ 0 ], MAX_WORD ) ;
name = new_name ;
argv[ 0 ] = name ;
// Copy argv's stuff
for ( i = 1; i < *num_words_ptr; i++ )
{
new_copy = ( char* ) malloc( sizeof( char ) * MAX_WORD ) ;
strncpy( new_copy, argv[ i ], MAX_WORD ) ;
str_tokens[ str_num + i - 1 ] = new_copy ;
}
argv = str_tokens ;
*num_words_ptr = *num_words_ptr + str_num - 1 ;
if (iAmDebugging)
{
printf("argv[0]: %s\n", argv[0]) ;
printf("argv[1]: %s\n", argv[1]) ;
printf("argv[2]: %s\n", argv[2]) ;
printf("num_words_ptr: %d\n", *num_words_ptr) ;
}
}
for ( i = 0; i < num_commands; i++ )
if ( !strcmp( name, commands[ i ].name ) )
{
return commands[ i ].fxn;
}
// Command not found
return NULL;
}
From the description, it seems they are telling me to copy things to the buffer and point things from argv to words in the buffer, but I don't see how that would be any different...
To give an example of what it is doing, when there is some monster, for example,
A goblin approaches!
> alias c 'cast fireball'
> alias
c = 'cast fireball'
> c goblin
> You cannot cast goblin!
The words[0] = cast and words[1] = goblin after the call to lookup... but words[0] = cast because it is connected to the argument: name.
I went through gdb so many times my head is spinning. This is the output from lookup_command to main:
Code:
(gdb)
36: name = 0x8051ae0 "cast"
21: commands[0] = {name = 0x804c945 "look", fxn = 0x804931e <look>, player_vulnerable = true}
19: argv[2] = 0x8051b08 "donkey"
18: argv[1] = 0xbffff701 "fireball"
17: argv[0] = 0xbffff6fc "cast"
16: *num_words_ptr = 3
15: argv = (char **) 0xbffff210
13: str_tokens[2] = 0x8051b08 "donkey"
11: name = 0x8051ae0 "cast"
10: str_tokens[1] = 0xbffff701 "fireball"
9: str_tokens[0] = 0xbffff6fc "cast"
7: str_num = 2
6: buf = 0xbffff6fc "cast"
5: new_name = 0x8051ae0 "cast"
4: str = 0x8051540 "cast fireball"
(gdb)
main (argc=1, argv=0xbffff9c4) at main.c:134
35: num_words = 3
34: words[2] = 0xbffff80f "mi"
33: words[1] = 0xbffff7fe "donkey"
32: words[0] = 0x8051ae0 "cast"
29: argv[2] = 0xbffffb3d "ORBIT_SOCKETDIR=/tmp/orbit-ryan"
28: argv[1] = 0x0
27: argv[0] = 0xbffffb22 "/home/ryan/Code/proj1/game"
23: num_words = 3
22: buf2 = "cast\000fireball", '\000' <repeats 242 times>
21: commands[0] = {name = 0x804c945 "look", fxn = 0x804931e <look>, player_vulnerable = true}
1: command_name = 0xbffff7fc "c"
Any help is appreciated...