diff --git a/alias.c b/alias.c new file mode 100644 index 0000000..8dd01eb --- /dev/null +++ b/alias.c @@ -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); +} diff --git a/alias_handler.c b/alias_handler.c new file mode 100644 index 0000000..df5ef37 --- /dev/null +++ b/alias_handler.c @@ -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); +} diff --git a/alias_utils.c b/alias_utils.c new file mode 100644 index 0000000..51b6f24 --- /dev/null +++ b/alias_utils.c @@ -0,0 +1,91 @@ +#include "liteshell.h" + +/** + * printAliasList - Auxilliary Function + * + * Description: This function prints an alias linked list + * + * @aliasList: pointer to linked list + * + * Return: 0 on Success, 1 on Failure + */ + +int printAliasList(alias_t *aliasList) +{ + alias_node *currAlias; + + if (!aliasList) + return (0); + + currAlias = aliasList->head; + + while (currAlias) + { + printf("%s='%s'\n", currAlias->name, currAlias->value); + currAlias = currAlias->next; + } + return (0); +} + +/** + * freeAlias - Auxilliary Function + * + * Description: This function frees an alias node + * + * @node: node to be freed + * + * Return: Nothing + */ + +void freeAlias(alias_node *node) +{ + free(node->name); + free(node->value); + free(node); + node = NULL; +} + +/** + * freeAliasList - Auxilliary Function + * + * Description: This function frees an aliasList + * + * @aliasList: pointer to the aliasList + * + * Return: Nothing + */ + +void freeAliasList(alias_t *aliasList) +{ + alias_node *currAlias, *nextAlias; + + currAlias = aliasList->head; + + while (currAlias) + { + nextAlias = currAlias->next; + freeAlias(currAlias); + currAlias = nextAlias; + } + free(aliasList); +} + +/** + * freeTokens - Auxilliary Function + * + * Description: This function frees an array of tokens + * + * @tokens: array of tokens + * + * Return: Nothing + */ + +void freeTokens(char **tokens) +{ + int i; + + for (i = 0; tokens[i]; ++i) + free(tokens[i]); + + free(tokens); +} diff --git a/liteshell.h b/liteshell.h index 58973c0..c1c3d17 100644 --- a/liteshell.h +++ b/liteshell.h @@ -90,6 +90,38 @@ lsh_t *lsh_init(void); void _free(void **ptr); #define safe_free(ptr) _free((void **)&(ptr)) +/* a function to create an alias node */ +alias_t *initAliasList(alias_t *aliasList); + +/* a function to add an alias to linked list */ +int addAlias(alias_t *aliasList, const char *name, const char *value); + +/* a function that returns the value member of an alias node*/ +alias_node *findAlias(alias_t *aliasList, const char *name); + +/* a function that prints all stored aliases in linked list */ +int printAliasList(alias_t *aliasList); + +/* a function to an alias node */ +int printAlias(alias_node *alias, const char *name); + +/* entry point function to handle single or multiple alias arguments */ +int handleAlias(alias_t *aliasList, char **arguments); + +/* a function that handles each alias argument */ +int handleAliasCommands(alias_t *aliasList, char *argument); +/* a function to remove an alias node - unalias */ +int removeAlias(alias_t *aliasList, const char *name); + +/* a function to free an alias node */ +void freeAlias(alias_node *node); + +/* a function to free an alias list */ +void freeAliasList(alias_t *aliasList); + +/* a function to free tokens */ +void freeTokens(char **tokens); + /* implements basic error checking for NULL pointers */ int check_err(void *ptr, char ptr_type); diff --git a/str_arr_size.c b/str_arr_size.c new file mode 100644 index 0000000..91b5c9b --- /dev/null +++ b/str_arr_size.c @@ -0,0 +1,18 @@ +#include "liteshell.h" + +/** + * str_arr_size - Auxilliary Functions + * Description: This is a function that counts the words in an array of + * strings. + * @str_arr: String Array + * Return: (size_t) count if array is populaated, 0 if array is NULL array + */ + +size_t str_arr_size(char **str_arr) +{ + size_t count = 0; + + while (str_arr[count]) + ++count; + return (count); +}