The main logic trunk in AGROS:
- Parse commands
- Do the following infinitely until closed:
---- Switch over built in commands
---- Execute command
The other features are all secondary when it comes to the above path of the program, so let's concentrate on this for now.
The following is called in main, inside the main loop.
parse_command (commandline, &cmd);
cmd is declared early up in main, and initialized to the following:
command_t cmd = {NULL, 0, {NULL}};
The type command_t is found inside agros.h and looks like following:
typedef struct command_t command_t;
struct command_t{
char* name;
int argc;
char* argv[MAX_ARGS+1];
};
parse_command is found inside agros.c file.
Here's the source code:
void parse_command (char *cmdline, command_t *cmd){
int count = 0;
char* word;
word = strtok (cmdline, WHITESPACE);
if (word == NULL) { word = ""; } // Fixes blank line bug
while (word) {
cmd->argv[count] = word;
word = strtok (NULL, WHITESPACE);
count++;
}
cmd->argv[count] = NULL;
cmd->argc = count;
cmd->name = (char *) malloc (strlen (cmd->argv[0])+1);
strcpy (cmd->name, cmd->argv[0]);
}
The only problem I could find with this snippet was the fact that you are not deallocating cmd->name before assigning it a new value.
The thing I want you to notice however, which I should refer to in a future post is the fact that you declared cmd inside main(...) in main.c, and used it in a function in agros.c
This should have been a practice followed throughout the code, but seems there are some violations: specifically with built_in_commands, my_commands. However the fact is actually debatable, and there are some instances in standard libraries that follow this technique (especially when the data is immutable). The rand() function from stdlib uses a similar "translation unit" local variable. Translation unit here means a single compilable source file, which is a .c file (.h file does not qualify).
This does have repercussions on parallelism when using threads.