From 06c2fce32dcaea639d34da423373421601909810 Mon Sep 17 00:00:00 2001 From: NairaMescudi Date: Mon, 4 Dec 2023 22:30:08 +0100 Subject: [PATCH 1/9] Added first 3 functions to handle aliases and a string array word counter --- alias.c | 76 ++++++++++++++++++++++++++++++++++++++++++++++++++ liteshell.h | 9 ++++++ str_arr_size.c | 18 ++++++++++++ 3 files changed, 103 insertions(+) create mode 100644 alias.c create mode 100644 str_arr_size.c diff --git a/alias.c b/alias.c new file mode 100644 index 0000000..1759cb9 --- /dev/null +++ b/alias.c @@ -0,0 +1,76 @@ +#include "liteshell.h" + +/** + * createNode - Auxilliary Function + * Description: This function creates an alias node and populates the members + * with the key and value params + * @key: key to be assigned a value + * @value: value assigned to a key + * Return: (alias_node *) node on Success, NULL on Failure + */ + +alias_node *createNode(const char *key, const char *value) +{ + alias_node *node; + + node = (alias_t *) malloc(sizeof(alias_t)); + if (node == NULL) + return (NULL); + node->key = key; + node->value = value; + node->next = NULL; + return (node); +} + +/** + * addAlias - Auxilliary Function + * Description: This function creates a node and adds the newNode to the alias + * linked list. + * @aliasList: pointer to alias_t custom type (points to the list) + * @key: key to be assigned a value + * @value: value assigned to a key + * Return: Nothing + */ + +void addAlias(alias_t *aliasList, const char *key, const char *value) +{ + alias_node *newNode; + + newNode = createNode(key, value); + if (newNode == NULL) + { + dprintf(STDERR_FILENO, "Error creating\n"); + exit(EXIT_FAILURE); + } + + if (!aliasList || !(aliasList->head)) + aliasList->head = newNode; + else + { + newNode->next = aliasList->head; + aliasList->head = newNode; + } +} + +/** + * findAlias - Auxilliary Function + * Description: Finds a key that matches key param in the list, and returns the + * corresponding value. + * @aliasList: pointer to linked list + * @key: key entry to be matched + * Return: (char *) current->value on Success, NULL on Failure + */ + +char *findAlias(alias_t *aliasList, const char *key) +{ + alias_node *current; + + current = aliasList->head; + while (current) + { + if (current->key == key) + return (current->value); + current = current->next; + } + return (NULL); +} diff --git a/liteshell.h b/liteshell.h index 58973c0..e5f4781 100644 --- a/liteshell.h +++ b/liteshell.h @@ -90,6 +90,15 @@ lsh_t *lsh_init(void); void _free(void **ptr); #define safe_free(ptr) _free((void **)&(ptr)) +/* a function to create an alias node */ +alias_node *createAliasNode(const char *, const char *); + +/* a function to add an alias to linked list */ +void addAlias(alias_t *, const char *, const char *); + +/* a function that counts the words in a string array */ +size_t str_arr_size(char **); + /* 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); +} From db28e45e3c9f6485b6f2ac71f525bd501c27d91b Mon Sep 17 00:00:00 2001 From: NairaMescudi Date: Tue, 5 Dec 2023 15:02:33 +0100 Subject: [PATCH 2/9] Added function prototype: void aliasExecutor(alias_t *, char*[]); Refactored 'key' to 'name' Corrected some syntax errors --- alias.c | 51 +++++++++++++++++++++++++++++++++++++++++++-------- liteshell.h | 5 ++++- 2 files changed, 47 insertions(+), 9 deletions(-) diff --git a/alias.c b/alias.c index 1759cb9..5574479 100644 --- a/alias.c +++ b/alias.c @@ -9,15 +9,15 @@ * Return: (alias_node *) node on Success, NULL on Failure */ -alias_node *createNode(const char *key, const char *value) +alias_node *createNode(const char *name, const char *value) { alias_node *node; - node = (alias_t *) malloc(sizeof(alias_t)); + node = (alias_node *) malloc(sizeof(alias_t)); if (node == NULL) return (NULL); - node->key = key; - node->value = value; + node->name = (char *) name; + node->value = (char *) value; node->next = NULL; return (node); } @@ -32,11 +32,11 @@ alias_node *createNode(const char *key, const char *value) * Return: Nothing */ -void addAlias(alias_t *aliasList, const char *key, const char *value) +void addAlias(alias_t *aliasList, const char *name, const char *value) { alias_node *newNode; - newNode = createNode(key, value); + newNode = createNode(name, value); if (newNode == NULL) { dprintf(STDERR_FILENO, "Error creating\n"); @@ -61,16 +61,51 @@ void addAlias(alias_t *aliasList, const char *key, const char *value) * Return: (char *) current->value on Success, NULL on Failure */ -char *findAlias(alias_t *aliasList, const char *key) +char *findAlias(alias_t *aliasList, const char *name) { alias_node *current; current = aliasList->head; while (current) { - if (current->key == key) + if (current->name == name) return (current->value); current = current->next; } return (NULL); } + +void aliasExecutor(alias_t *aliasList, char *arguments[]) +{ + int i = 0; + char **als, *name, *value; + + while (arguments[i]) + { + als = tokenize(arguments[i], "="); + + if (str_arr_size(als) == 1) + { + name = als[0]; + value = findAlias(aliasList, name); + if (value == NULL) + { + dprintf(STDERR_FILENO, "alias %s not found\n", + value); + exit(1); + } + printf("alias %s='%s'\n", name, value); + } + else if (str_arr_size(als) == 2) + { + name = als[0]; + value = als[1]; + addAlias(aliasList, name, value); + } + else + { + dprintf(STDERR_FILENO, "Unsupported alias format\n"); + exit(EXIT_FAILURE); + } + } +} diff --git a/liteshell.h b/liteshell.h index e5f4781..8088638 100644 --- a/liteshell.h +++ b/liteshell.h @@ -94,7 +94,10 @@ void _free(void **ptr); alias_node *createAliasNode(const char *, const char *); /* a function to add an alias to linked list */ -void addAlias(alias_t *, const char *, const char *); +void addAlias(alias_t *, const char *, const char *); + +/* a function to implement the alias command */ +void aliasExecutor(alias_t *, char *[]); /* a function that counts the words in a string array */ size_t str_arr_size(char **); From d708139422f3f3e3d57e44ed66d0aeef24839ee6 Mon Sep 17 00:00:00 2001 From: NairaMescudi Date: Tue, 5 Dec 2023 15:13:11 +0100 Subject: [PATCH 3/9] Made alias.c betty compliant --- alias.c | 23 ++++++++++++++++------- 1 file changed, 16 insertions(+), 7 deletions(-) diff --git a/alias.c b/alias.c index 5574479..5af96a9 100644 --- a/alias.c +++ b/alias.c @@ -4,8 +4,8 @@ * createNode - Auxilliary Function * Description: This function creates an alias node and populates the members * with the key and value params - * @key: key to be assigned a value - * @value: value assigned to a key + * @name: name to be assigned a value + * @value: value assigned to a name * Return: (alias_node *) node on Success, NULL on Failure */ @@ -27,8 +27,8 @@ alias_node *createNode(const char *name, const char *value) * Description: This function creates a node and adds the newNode to the alias * linked list. * @aliasList: pointer to alias_t custom type (points to the list) - * @key: key to be assigned a value - * @value: value assigned to a key + * @name: name to be assigned a value + * @value: value assigned to a name * Return: Nothing */ @@ -54,10 +54,10 @@ void addAlias(alias_t *aliasList, const char *name, const char *value) /** * findAlias - Auxilliary Function - * Description: Finds a key that matches key param in the list, and returns the - * corresponding value. + * Description: Finds a name that matches name param in the list, and returns + * the corresponding value. * @aliasList: pointer to linked list - * @key: key entry to be matched + * @name: name entry to be matched * Return: (char *) current->value on Success, NULL on Failure */ @@ -75,6 +75,15 @@ char *findAlias(alias_t *aliasList, const char *name) return (NULL); } +/** + * aliasExecutor - Entry Point + * Description: This function executes the alias command function call + * sequence + * @aliasList: pointer to linked list + * @arguments: array of strings (decays into char **) + * Return: Nothing + */ + void aliasExecutor(alias_t *aliasList, char *arguments[]) { int i = 0; From c1c786f564dab6d08932d321aee86b29b1a5d62a Mon Sep 17 00:00:00 2001 From: NairaMescudi Date: Tue, 5 Dec 2023 15:28:02 +0100 Subject: [PATCH 4/9] Added comment in alias.c to highlight how char *arguments[] should be passed from the shell main file --- alias.c | 7 ++++++- 1 file changed, 6 insertions(+), 1 deletion(-) diff --git a/alias.c b/alias.c index 5af96a9..9fea346 100644 --- a/alias.c +++ b/alias.c @@ -80,7 +80,9 @@ char *findAlias(alias_t *aliasList, const char *name) * Description: This function executes the alias command function call * sequence * @aliasList: pointer to linked list - * @arguments: array of strings (decays into char **) + * @arguments: tokenized array of strings from user input starting from the first + * index i.e after the command string + * alias command) * Return: Nothing */ @@ -89,6 +91,9 @@ void aliasExecutor(alias_t *aliasList, char *arguments[]) int i = 0; char **als, *name, *value; + /** char *arguments[] should be passed in as arguments[1:] in the main + * shell file. This passes the array starting from the first index + */ while (arguments[i]) { als = tokenize(arguments[i], "="); From 74a97d227c1584677526738ecda15da0417838db Mon Sep 17 00:00:00 2001 From: NairaMescudi Date: Wed, 6 Dec 2023 05:12:14 +0100 Subject: [PATCH 5/9] Refactored createNode to initAlias Debugged addAlias function taking into account the tail member of an aliasList Adjusted aliasExecutor function to account for changes in initAlias, and addAlias functions Corrected other errors in alias.c --- alias.c | 111 +++++++++++++++++++++++++++++++++------------------- liteshell.h | 12 ++++-- 2 files changed, 79 insertions(+), 44 deletions(-) diff --git a/alias.c b/alias.c index 9fea346..182bb49 100644 --- a/alias.c +++ b/alias.c @@ -3,22 +3,25 @@ /** * createNode - Auxilliary Function * Description: This function creates an alias node and populates the members - * with the key and value params + * with the name and value params + * @aliasList: pointer to alias list * @name: name to be assigned a value * @value: value assigned to a name * Return: (alias_node *) node on Success, NULL on Failure */ -alias_node *createNode(const char *name, const char *value) +alias_node *initAlias(alias_t *aliasList, const char *name, const char *value) { alias_node *node; - node = (alias_node *) malloc(sizeof(alias_t)); + node = (alias_node *) malloc(sizeof(alias_node)); if (node == NULL) return (NULL); node->name = (char *) name; node->value = (char *) value; node->next = NULL; + aliasList->head = node; + aliasList->tail = node; return (node); } @@ -26,30 +29,26 @@ alias_node *createNode(const char *name, const char *value) * addAlias - Auxilliary Function * Description: This function creates a node and adds the newNode to the alias * linked list. - * @aliasList: pointer to alias_t custom type (points to the list) + * @aliasList: pointer to the alias list) * @name: name to be assigned a value * @value: value assigned to a name - * Return: Nothing + * Return: (alias_node *) newnode on Success, NULL on Failure */ -void addAlias(alias_t *aliasList, const char *name, const char *value) +alias_node *addAlias(alias_t *aliasList, const char *name, const char *value) { - alias_node *newNode; + alias_node *newnode; - newNode = createNode(name, value); - if (newNode == NULL) - { - dprintf(STDERR_FILENO, "Error creating\n"); - exit(EXIT_FAILURE); - } + newnode = (alias_node *) malloc(sizeof(alias_node)); + if (newnode == NULL) + return (NULL); - if (!aliasList || !(aliasList->head)) - aliasList->head = newNode; - else - { - newNode->next = aliasList->head; - aliasList->head = newNode; - } + newnode->next = NULL; + newnode->name = (char *) name; + newnode->value = (char *) value; + aliasList->tail->next = newnode; + aliasList->tail = newnode; + return (newnode); } /** @@ -68,58 +67,88 @@ char *findAlias(alias_t *aliasList, const char *name) current = aliasList->head; while (current) { - if (current->name == name) + if (!strcmp(current->name, name)) return (current->value); current = current->next; } return (NULL); } +/** + * printAliases - Auxilliary Function + * Description: This function prints the aliases in an alias list + * @aliasList: pointer to the alias list + * Return: Nothing + */ + +void printAliases(alias_t *aliasList) +{ + alias_node *current; + + if (aliasList->head) + { + current = aliasList->head; + while (current) + { + printf("%s='%s'", current->name, current->value); + current = current->next; + } + } + else + return; +} + + /** * aliasExecutor - Entry Point * Description: This function executes the alias command function call * sequence - * @aliasList: pointer to linked list - * @arguments: tokenized array of strings from user input starting from the first - * index i.e after the command string - * alias command) + * @aliasList: pointer to alias list + * @arguments: tokenized array of strings from user input starting from the + * first index i.e after the command string * Return: Nothing */ -void aliasExecutor(alias_t *aliasList, char *arguments[]) +void aliasExecutor(alias_t *aliasList, char **arguments) { int i = 0; char **als, *name, *value; - /** char *arguments[] should be passed in as arguments[1:] in the main - * shell file. This passes the array starting from the first index - */ - while (arguments[i]) + while (arguments) { als = tokenize(arguments[i], "="); - - if (str_arr_size(als) == 1) + if (!als) /** could not tokenize arguments[i] */ + exit(1); + /** alias "name" */ + else if (str_arr_size(als) == 1) { name = als[0]; value = findAlias(aliasList, name); if (value == NULL) { - dprintf(STDERR_FILENO, "alias %s not found\n", - value); + dprintf(2, "alias: %s not found\n", value); exit(1); } - printf("alias %s='%s'\n", name, value); + printf("%s='%s'\n", name, value); + exit(0); } + /** alias "name=value" */ else if (str_arr_size(als) == 2) { name = als[0]; value = als[1]; - addAlias(aliasList, name, value); - } - else - { - dprintf(STDERR_FILENO, "Unsupported alias format\n"); - exit(EXIT_FAILURE); + if (!(aliasList->head)) /** empty alias list*/ + { + /** alias list inits if condition is false */ + if (initAlias(aliasList, name, value) == NULL) + exit(1); + } + if (addAlias(aliasList, name, value) == NULL) + exit(1); + exit(0); } } + /** alias */ + printAliases(aliasList); + exit(0); } diff --git a/liteshell.h b/liteshell.h index 8088638..f780f60 100644 --- a/liteshell.h +++ b/liteshell.h @@ -91,13 +91,19 @@ void _free(void **ptr); #define safe_free(ptr) _free((void **)&(ptr)) /* a function to create an alias node */ -alias_node *createAliasNode(const char *, const char *); +alias_node *initAlias(alias_t *aliasList, const char *, const char *); /* a function to add an alias to linked list */ -void addAlias(alias_t *, const char *, const char *); +alias_node *addAlias(alias_t *, const char *, const char *); + +/* a function that returns the value member of an alias node*/ +char *findAlias(alias_t *, const char *); + +/* a function that prints all stored aliases in linked list */ +void printAliases(alias_t *); /* a function to implement the alias command */ -void aliasExecutor(alias_t *, char *[]); +void aliasExecutor(alias_t *, char **); /* a function that counts the words in a string array */ size_t str_arr_size(char **); From 09652bd09b44ba77c400a4529264eaee4d72b998 Mon Sep 17 00:00:00 2001 From: NairaMescudi Date: Fri, 8 Dec 2023 21:02:22 +0100 Subject: [PATCH 6/9] This file contains 4 alias and 1 unalias functions addAlias git commit -m --- alias.c | 216 +++++++++++++++++++++++++++++--------------------------- 1 file changed, 110 insertions(+), 106 deletions(-) diff --git a/alias.c b/alias.c index 182bb49..8dd01eb 100644 --- a/alias.c +++ b/alias.c @@ -1,154 +1,158 @@ #include "liteshell.h" /** - * createNode - Auxilliary Function - * Description: This function creates an alias node and populates the members - * with the name and value params - * @aliasList: pointer to alias list - * @name: name to be assigned a value - * @value: value assigned to a name - * Return: (alias_node *) node on Success, NULL on Failure + * 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_node *initAlias(alias_t *aliasList, const char *name, const char *value) +alias_t *initAliasList(alias_t *aliasList) { - alias_node *node; + if (aliasList != NULL) + return (aliasList); - node = (alias_node *) malloc(sizeof(alias_node)); - if (node == NULL) + aliasList = (alias_t *) malloc(sizeof(alias_t)); + if (aliasList == NULL) return (NULL); - node->name = (char *) name; - node->value = (char *) value; - node->next = NULL; - aliasList->head = node; - aliasList->tail = node; - return (node); + + aliasList->head = NULL; + aliasList->tail = NULL; + aliasList->size = 0; + return (aliasList); } /** * addAlias - Auxilliary Function - * Description: This function creates a node and adds the newNode to the alias - * linked list. - * @aliasList: pointer to the alias list) - * @name: name to be assigned a value - * @value: value assigned to a name - * Return: (alias_node *) newnode on Success, NULL on Failure + * + * 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 */ -alias_node *addAlias(alias_t *aliasList, const char *name, const char *value) +int addAlias(alias_t *aliasList, const char *name, const char *value) { - alias_node *newnode; + alias_node *newAlias; - newnode = (alias_node *) malloc(sizeof(alias_node)); - if (newnode == NULL) - return (NULL); + newAlias = (alias_node *) malloc(sizeof(alias_node)); + if (newAlias == NULL) + { + dprintf(2, "Failed to malloc\n"); + return (1); + } - newnode->next = NULL; - newnode->name = (char *) name; - newnode->value = (char *) value; - aliasList->tail->next = newnode; - aliasList->tail = newnode; - return (newnode); + 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: Finds a name that matches name param in the list, and returns - * the corresponding value. - * @aliasList: pointer to linked list - * @name: name entry to be matched - * Return: (char *) current->value on Success, NULL on Failure + * + * 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 */ -char *findAlias(alias_t *aliasList, const char *name) +alias_node *findAlias(alias_t *aliasList, const char *name) { - alias_node *current; + alias_node *currAlias = aliasList->head; - current = aliasList->head; - while (current) + while (currAlias) { - if (!strcmp(current->name, name)) - return (current->value); - current = current->next; + if (strcmp(currAlias->name, name) == 0) + return (currAlias); + currAlias = currAlias->next; } return (NULL); } /** - * printAliases - Auxilliary Function - * Description: This function prints the aliases in an alias list - * @aliasList: pointer to the alias list - * Return: Nothing + * 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 */ -void printAliases(alias_t *aliasList) +int printAlias(alias_node *alias, const char *name) { - alias_node *current; - - if (aliasList->head) + if (!alias) { - current = aliasList->head; - while (current) - { - printf("%s='%s'", current->name, current->value); - current = current->next; - } + printf("alias: %s not found\n", name); + return (1); } - else - return; + printf("%s='%s'\n", alias->name, alias->value); + return (0); } - /** - * aliasExecutor - Entry Point - * Description: This function executes the alias command function call - * sequence - * @aliasList: pointer to alias list - * @arguments: tokenized array of strings from user input starting from the - * first index i.e after the command string - * Return: Nothing + * 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 */ -void aliasExecutor(alias_t *aliasList, char **arguments) +int removeAlias(alias_t *aliasList, const char *name) { - int i = 0; - char **als, *name, *value; + alias_node *currAlias, *prevAlias; - while (arguments) + if (!aliasList) + return (1); + + currAlias = aliasList->head; + prevAlias = NULL; + + while (currAlias) { - als = tokenize(arguments[i], "="); - if (!als) /** could not tokenize arguments[i] */ - exit(1); - /** alias "name" */ - else if (str_arr_size(als) == 1) + if (strcmp(currAlias->name, name) == 0) { - name = als[0]; - value = findAlias(aliasList, name); - if (value == NULL) - { - dprintf(2, "alias: %s not found\n", value); - exit(1); - } - printf("%s='%s'\n", name, value); - exit(0); - } - /** alias "name=value" */ - else if (str_arr_size(als) == 2) - { - name = als[0]; - value = als[1]; - if (!(aliasList->head)) /** empty alias list*/ - { - /** alias list inits if condition is false */ - if (initAlias(aliasList, name, value) == NULL) - exit(1); - } - if (addAlias(aliasList, name, value) == NULL) - exit(1); - exit(0); + if (prevAlias == NULL) + aliasList->head = currAlias->next; + else + prevAlias->next = currAlias->next; + + freeAlias(currAlias); + aliasList->size--; } + prevAlias = currAlias; + currAlias = currAlias->next; } - /** alias */ - printAliases(aliasList); - exit(0); + return (0); } From eeaa2c190e2f7446339d5c28e3ab048ef113befa Mon Sep 17 00:00:00 2001 From: NairaMescudi Date: Fri, 8 Dec 2023 21:05:12 +0100 Subject: [PATCH 7/9] Updated liteshell.h --- liteshell.h | 30 ++++++++++++++++++++++-------- 1 file changed, 22 insertions(+), 8 deletions(-) diff --git a/liteshell.h b/liteshell.h index f780f60..c1c3d17 100644 --- a/liteshell.h +++ b/liteshell.h @@ -91,22 +91,36 @@ void _free(void **ptr); #define safe_free(ptr) _free((void **)&(ptr)) /* a function to create an alias node */ -alias_node *initAlias(alias_t *aliasList, const char *, const char *); +alias_t *initAliasList(alias_t *aliasList); /* a function to add an alias to linked list */ -alias_node *addAlias(alias_t *, const char *, const char *); +int addAlias(alias_t *aliasList, const char *name, const char *value); /* a function that returns the value member of an alias node*/ -char *findAlias(alias_t *, const char *); +alias_node *findAlias(alias_t *aliasList, const char *name); /* a function that prints all stored aliases in linked list */ -void printAliases(alias_t *); +int printAliasList(alias_t *aliasList); -/* a function to implement the alias command */ -void aliasExecutor(alias_t *, char **); +/* a function to an alias node */ +int printAlias(alias_node *alias, const char *name); -/* a function that counts the words in a string array */ -size_t str_arr_size(char **); +/* 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); From b4c54de8b8d07c37e9c37cf6deaa28eaae0eeebd Mon Sep 17 00:00:00 2001 From: NairaMescudi Date: Fri, 8 Dec 2023 21:06:52 +0100 Subject: [PATCH 8/9] This file contains all functions used to handle the alias command arguments tokenizeAliasArguments handleAliasCommands handleAlias main --- alias_handler.c | 163 ++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 163 insertions(+) create mode 100644 alias_handler.c 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); +} From ab264c52f1717f7462d70f82b19745589dce7cb9 Mon Sep 17 00:00:00 2001 From: NairaMescudi Date: Fri, 8 Dec 2023 21:08:19 +0100 Subject: [PATCH 9/9] This file contains all utility functions that support the alias functionality printAliasList freeAlias freeAliasList freeTokens --- alias_utils.c | 91 +++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 91 insertions(+) create mode 100644 alias_utils.c 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); +}