Skip to content
Snippets Groups Projects
Commit 6e69590a authored by Nikolay Tsanov's avatar Nikolay Tsanov
Browse files

Alias and unalias

parent c04161ba
No related branches found
No related tags found
No related merge requests found
......@@ -132,18 +132,50 @@ int run_command(String arguments[]) {
}
if(strcmp(arguments[0], "alias") == 0) {
if(arguments[1] != NULL && arguments[2] != NULL) {
int status = -1;
// Check if there is a place in the aliases array and store a new alias.
for(int i = 0; i < MAX_ALIASES; i++) {
if(aliases[i].key != NULL && strcmp(aliases[i].key, arguments[1]) == 0) {
status = 0;
} else if(aliases[i].key == NULL) {
status = 1;
// Count the arguments after the "alias" one.
int argumentsCount = 0;
while(arguments[argumentsCount + 1] != NULL) {
argumentsCount++;
}
// For each case, check if there is a numbering.
switch(argumentsCount) {
// Case 0: If there are no arguments, we want to print the list of the existing ones.
case 0: {
int i = 0;
// Print alises if they exist.
while(aliases[i].key != NULL) {
printf("Alias `%s` set to `%s` \n", aliases[i].key, aliases[i].command);
i++;
}
if(status != -1) {
// If no aliases exist, print a message.
if(i < 1) {
printf("ERROR: No aliases found\n");
}
break;
}
// Case 1: Only one argument which is not sufficient for the `alias` command.
case 1:
printf("ERROR: Not enough arguments supplied for `alias` command");
break;
// Case 2: More than one argument, we need to overwrite or add another alias.
default: {
int status = -1;
// Check if there is a place in the aliases array and store a new alias.
for(int i = 0; i < MAX_ALIASES; i++) {
if(aliases[i].key != NULL && strcmp(aliases[i].key, arguments[1]) == 0) {
status = 0;
} else if(aliases[i].key == NULL) {
status = 1;
}
if(status == -1) {
continue;
}
aliases[i].key = malloc(sizeof(char) * MAX_INPUT_LENGTH);
strcpy(aliases[i].key, arguments[1]);
......@@ -167,61 +199,38 @@ int run_command(String arguments[]) {
free(response);
return SUCCESS_STATUS;
}
}
printf("ERROR: No more aliases can be set");
return SUCCESS_STATUS;
printf("ERROR: No more aliases can be set");
}
break;
}
int i = 0;
// Print alises if they exist.
while(aliases[i].key != NULL) {
printf("Alias `%s` set to `%s` \n", aliases[i].key, aliases[i].command);
i++;
}
// If no aliases exist, print a message.
if(i < 1) {
printf("ERROR: No aliases found\n");
}
return SUCCESS_STATUS;
}
if(strcmp(arguments[0], "unalias") == 0) {
// if(arguments[1] == NULL) {
// printf("ERROR: Invalid arguments. Command unalias requires one argument\n");
// return SUCCESS_STATUS;
// }
// //Check if we have at least one alias set in the allias array
// Boolean hasElements = false;
// for(int i = 0; i < MAX_ALIASES; i++) {
// if(aliases[i].key != NULL) {
// hasElements = true;
// }
// }
// //If we have not set any aliases and try to unalias then display an error message and return to I/O input
// if(!hasElements) {
// printf("ERROR: No alias set\n");
// return SUCCESS_STATUS;
// }
// for(int i = 0; i < MAX_ALIASES; i++) {
// if(strcmp(aliases[i].key, arguments[1]) == 0) {
// aliases[i].key = NULL;
// int j = 0;
// while(aliases[i].command[j] != NULL) {
// aliases[i].command[j] = NULL;
// free(aliases[i].command[j]);
// }
// printf("The alias %s is removed", arguments[1]);
// return SUCCESS_STATUS;
// }
// }
printf("ERROR: Invalid arguments. Command alias requires more argument\n");
if(arguments[1] != NULL) {
Boolean isFound = false;
for(int i = 0; i < MAX_ALIASES; i++) {
if(aliases[i].key != NULL && strcmp(aliases[i].key, arguments[1]) == 0) {
for(int j = i; j < MAX_ALIASES; j++) {
aliases[j] = aliases[j+1];
}
isFound = true;
break;
}
}
if(isFound) {
printf("Alias `%s` has been removed\n", arguments[1]);
} else {
printf("Alias: No alias with key `%s` has been found\n", arguments[1]);
}
} else {
printf("ERROR: Invalid arguments. Command `unalias` requires one argument\n");
}
return SUCCESS_STATUS;
}
......
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