-
Notifications
You must be signed in to change notification settings - Fork 4
Implementing the alias command #28
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: master
Are you sure you want to change the base?
Changes from all commits
06c2fce
db28e45
d708139
c1c786f
74a97d2
09652bd
eeaa2c1
b4c54de
ab264c5
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
|
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. You might want to check your line 16 well... Also, I'd like to believe memory is correctly handled in your code logic.
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Also, in your Currently, your code logic does not account for when
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Compare how
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I have concerns with the alias functions... Could you share a screenshot of the result after testing this function?
Collaborator
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Alright boss. |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,158 @@ | ||
| #include "liteshell.h" | ||
|
|
||
| /** | ||
| * initAliasList - Initializer Function | ||
| * | ||
| * Description: This function initializes the alias linked list | ||
| * | ||
| * @aliasList: a pointer to already initialized linked list | ||
| * | ||
| * Return: (alias_t *) pointer to initialized aliasList on Success, | ||
| * NULL on Failure | ||
| */ | ||
|
|
||
| alias_t *initAliasList(alias_t *aliasList) | ||
| { | ||
| if (aliasList != NULL) | ||
| return (aliasList); | ||
|
|
||
| aliasList = (alias_t *) malloc(sizeof(alias_t)); | ||
| if (aliasList == NULL) | ||
| return (NULL); | ||
|
|
||
| aliasList->head = NULL; | ||
| aliasList->tail = NULL; | ||
| aliasList->size = 0; | ||
| return (aliasList); | ||
| } | ||
|
|
||
| /** | ||
| * addAlias - Auxilliary Function | ||
| * | ||
| * Description: This function adds a new alias node to the alias linked list | ||
| * | ||
| * @aliasList: pointer to the alias list | ||
| * | ||
| * @name: name member of alias node to be added | ||
| * | ||
| * @value: value member of alias node to be added | ||
| * | ||
| * Return: 0 on Success, 1 on Failure | ||
| */ | ||
|
|
||
| int addAlias(alias_t *aliasList, const char *name, const char *value) | ||
| { | ||
| alias_node *newAlias; | ||
|
|
||
| newAlias = (alias_node *) malloc(sizeof(alias_node)); | ||
| if (newAlias == NULL) | ||
| { | ||
| dprintf(2, "Failed to malloc\n"); | ||
| return (1); | ||
| } | ||
|
|
||
| newAlias->name = strdup(name); | ||
| newAlias->value = strdup(value); | ||
| newAlias->next = NULL; | ||
|
|
||
| if (aliasList->head == NULL) | ||
| aliasList->head = newAlias; | ||
| else | ||
| aliasList->tail->next = newAlias; | ||
|
|
||
| aliasList->tail = newAlias; | ||
| aliasList->size++; | ||
| return (0); | ||
| } | ||
|
|
||
| /** | ||
| * findAlias - Auxilliary Function | ||
| * | ||
| * Description: This function locates a specific alias node in the linked list | ||
| * based on its name | ||
| * | ||
| * @aliasList: pointer to alias list | ||
| * | ||
| * @name: Name of the alias node to be found | ||
| * | ||
| * Return: (alias_node *) pointer to alias node whose name member | ||
| * matches name on Success, NULL on Failure | ||
| */ | ||
|
|
||
| alias_node *findAlias(alias_t *aliasList, const char *name) | ||
| { | ||
| alias_node *currAlias = aliasList->head; | ||
|
|
||
| while (currAlias) | ||
| { | ||
| if (strcmp(currAlias->name, name) == 0) | ||
| return (currAlias); | ||
| currAlias = currAlias->next; | ||
| } | ||
| return (NULL); | ||
| } | ||
|
|
||
| /** | ||
| * printAlias - Auxilliary Function | ||
| * | ||
| * Description: This is a function that prints the members of an alias node in | ||
| * the aliasList | ||
| * | ||
| * @alias: alias node to be printed | ||
| * | ||
| * @name: name member of the alias node | ||
| * | ||
| * Return: 0 on Success, 1 on Failure | ||
| */ | ||
|
|
||
| int printAlias(alias_node *alias, const char *name) | ||
| { | ||
| if (!alias) | ||
| { | ||
| printf("alias: %s not found\n", name); | ||
| return (1); | ||
| } | ||
| printf("%s='%s'\n", alias->name, alias->value); | ||
| return (0); | ||
| } | ||
|
|
||
| /** | ||
| * removeAlias - Auxilliary Function | ||
| * | ||
| * Description: This is the function that removes an alias node from an | ||
| * aliasList | ||
| * | ||
| * @aliasList: pointer to the alias list | ||
| * | ||
| * @name: name member of the alias node | ||
| * | ||
| * Return: 0 on Success, 1 on Failurw | ||
| */ | ||
|
|
||
| int removeAlias(alias_t *aliasList, const char *name) | ||
| { | ||
| alias_node *currAlias, *prevAlias; | ||
|
|
||
| if (!aliasList) | ||
| return (1); | ||
|
|
||
| currAlias = aliasList->head; | ||
| prevAlias = NULL; | ||
|
|
||
| while (currAlias) | ||
| { | ||
| if (strcmp(currAlias->name, name) == 0) | ||
| { | ||
| if (prevAlias == NULL) | ||
| aliasList->head = currAlias->next; | ||
| else | ||
| prevAlias->next = currAlias->next; | ||
|
|
||
| freeAlias(currAlias); | ||
| aliasList->size--; | ||
| } | ||
| prevAlias = currAlias; | ||
| currAlias = currAlias->next; | ||
| } | ||
| return (0); | ||
| } |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,163 @@ | ||
| #include "liteshell.h" | ||
|
|
||
| /** | ||
| * tokenizeAliasArguments - Tokenizer Function | ||
| * | ||
| * Description: This function tokenizes the argument (string) based on the "=" | ||
| * delimeter | ||
| * | ||
| * @argument: String argument to be tokenized | ||
| * | ||
| * Return: (char **) tokens array on Success, EXIT on Failure | ||
| */ | ||
|
|
||
| char **tokenizeAliasArguments(char *argument) | ||
| { | ||
| char **tokens, *equalToSign; | ||
| size_t len_name, len_value; | ||
|
|
||
| tokens = (char **) malloc(sizeof(char *) * 2); | ||
| if (!tokens) | ||
| { | ||
| dprintf(2, "Failed to allocate memory for tokens\n"); | ||
| exit(EXIT_FAILURE); | ||
| } | ||
| tokens[0] = NULL; | ||
| tokens[1] = NULL; | ||
| equalToSign = strchr(argument, '='); | ||
| if (equalToSign) | ||
| { | ||
| len_name = equalToSign - argument; | ||
| tokens[0] = (char *) malloc(len_name + 1); | ||
| if (!tokens[0]) | ||
| { | ||
| dprintf(2, "Failed to allocate memory for name token\n"); | ||
| exit(EXIT_FAILURE); | ||
| } | ||
| strncpy(tokens[0], argument, len_name); | ||
| tokens[0][len_name] = '\0'; | ||
| len_value = strlen(equalToSign + 1); | ||
| tokens[1] = (char *) malloc(len_value + 1); | ||
| if (!tokens[1]) | ||
| { | ||
| dprintf(2, "Failed to allocate memory for value token\n"); | ||
| exit(EXIT_FAILURE); | ||
| } | ||
| strncpy(tokens[1], equalToSign + 1, len_value); | ||
| tokens[1][len_value] = '\0'; | ||
| } | ||
| else | ||
| { | ||
| tokens[0] = strdup(argument); | ||
| tokens[1] = NULL; | ||
| } | ||
| return (tokens); | ||
| } | ||
|
|
||
| /** | ||
| * handleAlias - Socket Function | ||
| * | ||
| * Description: This function acts as an interface between the user of the | ||
| * alias feature and the alias handler functions. An aliasList must have been | ||
| * initialized before using this function. | ||
| * | ||
| * @aliasList: pointer to an alias linked list | ||
| * | ||
| * @arguments: arguments for the alias command | ||
| * | ||
| * Return: 0 on Success, 1 on Failure | ||
| */ | ||
|
|
||
| int handleAlias(alias_t *aliasList, char **arguments) | ||
| { | ||
| int i; | ||
| char *name, *value; | ||
|
|
||
| if (!arguments[0]) | ||
| return (printAliasList(aliasList)); | ||
|
|
||
| for (i = 0; arguments[i]; ++i) | ||
| handleAliasCommands(aliasList, arguments[i]); | ||
| /** will handle returns */ | ||
| } | ||
|
|
||
| /** | ||
| * handleAliasCommands - Helper Function | ||
| * | ||
| * Description: This function utilizes a tokenizer to separate the alias | ||
| * arguments into an array DS of size 2 containing the name and value pairs. | ||
| * It also calls other auxilliary functions and passes on their return values | ||
| * to handleALias() | ||
| * | ||
| * @aliasList: pointer to the alias linked list | ||
| * | ||
| * @argument: arguments for alias command | ||
| * | ||
| * Return: 0 on Success, 1 on Failure | ||
| */ | ||
|
|
||
| int handleAliasCommands(alias_t *aliasList, char *argument) | ||
| { | ||
| char **aliasTokens, *name, *value; | ||
| alias_node *alias; | ||
|
|
||
| aliasTokens = tokenizeAliasArguments(argument); | ||
|
|
||
| name = aliasTokens[0]; | ||
| value = aliasTokens[1]; | ||
|
|
||
| if (value == NULL) | ||
| { | ||
| alias = findAlias(aliasList, name); | ||
| return (printAlias(alias, name)); | ||
| } | ||
| else | ||
| { | ||
| alias = findAlias(aliasList, name); | ||
| if (alias == NULL) | ||
| return (addAlias(aliasList, name, value)); | ||
| free(alias->value); | ||
| alias->value = strdup(value); | ||
| } | ||
| return (0); | ||
| } | ||
|
|
||
| /** | ||
| * main - Entry Point | ||
| * | ||
| * Description: This is a test suit interface for users of the alias command | ||
| * | ||
| * Return: Always 0 on Success | ||
| */ | ||
|
|
||
| int main(void) | ||
| { | ||
| alias_t *aliasList = NULL; | ||
| /** | ||
| * Run the strings in this array as arguments to the alias command and | ||
| * compare with result from sh | ||
| */ | ||
| char *arguments[] = { | ||
| "pot", | ||
| "al=alias", | ||
| "ll=lampholder", | ||
| "lorem=ipsum", | ||
| "al", | ||
| "lorem", | ||
| "al=back-it-up", | ||
| NULL | ||
| }; | ||
| /**char *arguments[] = {NULL};*/ | ||
|
|
||
| aliasList = initAliasList(aliasList); | ||
|
|
||
| handleAlias(aliasList, arguments); | ||
| printAliasList(aliasList); | ||
| removeAlias(aliasList, "lorem"); | ||
| printAliasList(aliasList); | ||
|
|
||
| freeAliasList(aliasList); | ||
| printf("See alias_handler.c ;)\n"); | ||
|
|
||
| return (0); | ||
| } |
Uh oh!
There was an error while loading. Please reload this page.