Skip to content
Snippets Groups Projects
Commit 657d2f4c authored by Sébastien Gendre's avatar Sébastien Gendre
Browse files

Rewrite command parsing to simplify it

parent fa84dcbd
No related branches found
No related tags found
No related merge requests found
......@@ -3,100 +3,53 @@
#include "command.h"
int parse_user_input_for_command(char *user_input, cmd_t *cmd) {
int parse_user_input_to_command(char *user_input, cmd_t *cmd) {
// Stor next command argument, used in the do…while loop
char* next_arg;
//Initialize a simple command (empty, simple, foreground)
cmd->command = NULL;
cmd->argv = NULL;
cmd->argc = -1;
cmd->argc = 0;
cmd->foreground = true;
cmd->redirect_to = NULL;
//Separate string in different token (i.e. command name + params + &)
do {
//A new element will be added
cmd->argc += 1;
//Allocate a new pointer on char for next argv element
if((cmd->argv = realloc(cmd->argv, (cmd->argc+1)*sizeof(char*))) == NULL)
die_errno("parse_command::realloc");
//Get the adress opf the next token (could be NULL to indicate end of argv)
cmd->argv[cmd->argc] = strtok(user_input, DELIMIERS);
user_input = NULL; //Useless to execute it each time but easier than having two different strtok calls
} while (cmd->argv[cmd->argc] != NULL); // while there are still tokens
//Search for rediction symbols (>, >>, <) in the first cmd
for(int i=0; i < cmd->argc; i++) {
if(strcmp(">", cmd->argv[i]) == 0)
cmd->type = CMD_FILEOUT;
if(strcmp(">>", cmd->argv[i]) == 0)
cmd->type = CMD_FILEAPPEND;
if(strcmp("<", cmd->argv[i]) == 0)
cmd->type = CMD_FILEIN;
// if symbol was found at i
if((cmd->type == CMD_FILEOUT) | (cmd->type == CMD_FILEAPPEND) | (cmd->type == CMD_FILEIN)) {
if(cmd->argc > i+1) {
//Allocate a size two table of char* to store file + NULL
if((cmd->argv2 = realloc(cmd->argv2, 2 * sizeof(char*))) == NULL)
die_errno("parse_command::realloc");
// Extract the commmand name
cmd->command = strtok(user_input, DELIMITERS);
// copy argument following symbol to argv2
cmd->argv2[0] = cmd->argv[i+1];
cmd->argv2[1] = NULL;
cmd->argc2 = 1;
// update argv to ignore symbol on the following args
cmd->argv[i] = NULL;
cmd->argc = i;
// Extract the command arguments
do {
// Get the next argument
next_arg = strtok(NULL, DELIMITERS);
if (next_arg != NULL) {
// A new element will be added to cmd->argv
cmd->argc += 1;
// Extend the cmd->argv array witha new char pointer
cmd->argv = realloc(cmd->argv, (cmd->argc)*sizeof(char*));
if( cmd->argv == NULL) {
// In case the reallocation didn't work
die_errno("parse_command::realloc");
}
else
return -1; //Nothing after symbol
}
}
//Search for pipe symbols in the first cmd
// TODO: this look very similar to indirection symbols search -> combine / creat common function ?
for(int i=0; i < cmd->argc; i++) {
if(strcmp("|", cmd->argv[i]) == 0) {
if(cmd->argc > i+1) { // If not args after & ignore it
cmd->type = CMD_PIPE;
//Allocate a new pointer on char for next argv element
cmd->argc2 = cmd->argc - i - 1;
if((cmd->argv2 = realloc(cmd->argv2, (cmd->argc - i) * sizeof(char*))) == NULL) //one more than argc cause last is NULL
die_errno("parse_command::realloc");
//copy arguments after the & to the argv2
for(int j=0; j < cmd->argc2; j++)
// copy argument following symbol to argv2
cmd->argv2[j] = cmd->argv[i+j+1];
// update argv to ignore symbol on the following args
cmd->argv[i] = NULL;
cmd->argc = i;
// Only first pipe symbol is considered no more should be found
break;
}
// Store the argument inside cmd->argv
cmd->argv[cmd->argc-1] = next_arg;
}
}
//Check if the last command is a '&' to decide if we should run command
//as a foreground or background job
if(strcmp(cmd->argv[cmd->argc-1], "&") == 0) {
cmd->foreground = false;
//Delete the & from the command for execution
cmd->argv[cmd->argc-1] = NULL;
cmd->argc--;
}
} while (next_arg != NULL);
return 0;
}
void dispose_command(cmd_t *cmd) {
// Free the command name str
free(cmd->command);
// Free all the command arguments str inside argv
for (int i = 0; i < cmd->argc; ++i) {
free(cmd->argv[i]);
}
// Then, finally, free argv
free(cmd->argv);
}
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment