|
1 | 1 | #include "shell.h" |
2 | 2 |
|
| 3 | +static int exit_code; |
| 4 | + |
3 | 5 | /** |
4 | 6 | * handle_alias - handles the processing of alias |
5 | 7 | * @head: a pointer to the list containing all aliases |
6 | 8 | * @command: the list of commands containing alias-specific lines |
7 | 9 | * |
8 | 10 | * Return: 0 on success, -1 on error |
9 | 11 | */ |
10 | | -int handle_alias(alias_t **head, char **command) |
| 12 | +int handle_alias(alias_t **head, char *command) |
11 | 13 | { |
12 | | - char *value, *loc, *name, *tmp; |
13 | | - ssize_t i, offset, exit_code = 0; |
| 14 | + if (_strlen(command) == 5) |
| 15 | + print_aliases(*head); |
| 16 | + |
| 17 | + else if (!_strncmp(command, "alias", 5)) |
| 18 | + { |
| 19 | + if (!_strchr(command, '=')) |
| 20 | + process_non_matching(*head, command + 5, 1); |
| 21 | + else |
| 22 | + parse_aliases(command, head); |
| 23 | + } |
14 | 24 |
|
15 | | - if (!_strcmp("unalias", command[0])) |
| 25 | + else if (!_strncmp(command, "unalias", 7)) |
16 | 26 | return (unalias(head, command)); |
17 | | - if ((!_strcmp(command[0], "alias") && command[1] == NULL)) |
18 | | - print_aliases(*head); |
19 | | - for (i = 1; command[i] != NULL; i++) |
| 27 | + |
| 28 | + return (exit_code); |
| 29 | +} |
| 30 | + |
| 31 | +/** |
| 32 | + * parse_aliases - extract aliases from the input string using regular |
| 33 | + * expressions. |
| 34 | + * @input: the input string containing aliases |
| 35 | + * @aliases: a pointer to a list of aliases |
| 36 | + * |
| 37 | + * Description: This function uses regular expressions to extract aliases from |
| 38 | + * the input string. It populates the provided array of Alias structures with |
| 39 | + * the parsed aliases and updates the aliasCount accordingly. |
| 40 | + */ |
| 41 | +void parse_aliases(const char *input, alias_t **aliases) |
| 42 | +{ |
| 43 | + char *input_ptr = NULL; |
| 44 | + size_t valueLength, alias_count = 0; |
| 45 | + regmatch_t matches[3]; |
| 46 | + regex_t regex; |
| 47 | + const char *pattern = |
| 48 | + "([^\"]\\S*|\"[^\"]*\"|'[^\']*')=([^\"]\\S*|\"[^\"]*\"|'[^\']*')"; |
| 49 | + |
| 50 | + if (regcomp(®ex, pattern, REG_EXTENDED) != 0) |
| 51 | + return; /* regular expression compilation failed */ |
| 52 | + input_ptr = (char *)input; |
| 53 | + while (regexec(®ex, input_ptr, 3, matches, 0) == 0) |
20 | 54 | { |
21 | | - tmp = _strdup(command[i]); |
22 | | - loc = _strchr(tmp, '='); |
23 | | - if (loc != NULL) |
| 55 | + char name[MAX_ALIAS_LENGTH] = {0}; |
| 56 | + char value[MAX_VALUE_LENGTH] = {0}; |
| 57 | + |
| 58 | + /* extract the alias name, accounts for the leading spaces */ |
| 59 | + _strncpy(name, (input_ptr + 1) + matches[1].rm_so, |
| 60 | + (matches[1].rm_eo - 1) - matches[1].rm_so); |
| 61 | + _strncpy(value, input_ptr + matches[2].rm_so, |
| 62 | + matches[2].rm_eo - matches[2].rm_so); /* extract the alias value */ |
| 63 | + name[matches[1].rm_eo - matches[1].rm_so] = '\0'; |
| 64 | + |
| 65 | + valueLength = matches[2].rm_eo - matches[2].rm_so; |
| 66 | + if (isquote(value[0])) /* remove quotes */ |
24 | 67 | { |
25 | | - offset = (&loc[0]) - (&tmp[0]); |
26 | | - tmp[offset] = '\0'; |
27 | | - name = _strdup(tmp); |
28 | | - if (name == NULL) |
29 | | - { |
30 | | - safe_free(tmp); |
31 | | - return (-1); |
32 | | - } |
33 | | - value = (tmp[offset + 1] == '"' || tmp[offset + 1] == '\'') |
34 | | - ? extract_value(&tmp[offset + 1]) : _strdup(&tmp[offset + 1]); |
35 | | - if (value == NULL) |
36 | | - { |
37 | | - multi_free("ss", value, name); |
38 | | - return (-1); |
39 | | - } |
40 | | - if (add_alias(head, name, value) == NULL) |
| 68 | + _strncpy(value, input_ptr + matches[2].rm_so + 1, valueLength - 2); |
| 69 | + value[valueLength - 2] = '\0'; |
| 70 | + } |
| 71 | + else /* not enclosed in quotes, copy as is */ |
| 72 | + value[valueLength] = '\0'; |
| 73 | + if (add_alias(aliases, name, value) == NULL) |
| 74 | + return; |
| 75 | + if (alias_count) |
| 76 | + process_non_matching(*aliases, input_ptr, 0); |
| 77 | + alias_count++; /* increment alias count */ |
| 78 | + input_ptr += matches[0].rm_eo; /* keep searching */ |
| 79 | + } |
| 80 | + if (alias_count) |
| 81 | + process_non_matching(*aliases, input_ptr, 1); |
| 82 | + regfree(®ex); |
| 83 | +} |
| 84 | + |
| 85 | +/** |
| 86 | + * process_non_matching - processes strings that do not have the `name=value` |
| 87 | + * format while parsing aliases |
| 88 | + * @aliases: a list containing aliases |
| 89 | + * @non_matching: the string to check for non-matching patterns |
| 90 | + * @end: used to signal the end. 1 means it can process the whole string at |
| 91 | + * once. 0 means it can do only word token at a time |
| 92 | + */ |
| 93 | +void process_non_matching(alias_t *aliases, const char *non_matching, int end) |
| 94 | +{ |
| 95 | + char *token, *dup; |
| 96 | + |
| 97 | + if (non_matching == NULL || *non_matching == '\0') |
| 98 | + return; /* there's nothing to work on, everything matched */ |
| 99 | + |
| 100 | + dup = _strdup(non_matching); |
| 101 | + token = strtok(dup, " "); |
| 102 | + |
| 103 | + /* it is non-matching if it doesn't contain an equal sign */ |
| 104 | + if (!_strchr(token, '=')) |
| 105 | + { |
| 106 | + if (end) |
| 107 | + { |
| 108 | + while (token != NULL) |
41 | 109 | { |
42 | | - multi_free("ss", value, name); |
43 | | - return (-1); |
| 110 | + exit_code = print_alias(aliases, token); |
| 111 | + token = strtok(NULL, " "); |
44 | 112 | } |
45 | | - multi_free("ss", value, name); |
46 | 113 | } |
47 | 114 | else |
48 | | - exit_code = print_alias(*head, command[i]); |
49 | | - safe_free(tmp); |
| 115 | + exit_code = print_alias(aliases, token); |
50 | 116 | } |
51 | | - return (exit_code); |
| 117 | + free(dup); |
52 | 118 | } |
53 | 119 |
|
54 | 120 | /** |
55 | | - * extract_value - returns the string value enclosed in quotes |
56 | | - * @value: the string containing value |
57 | | - * |
58 | | - * Return: the string data without the quotes |
| 121 | + * build_alias_cmd - builds the correct command line when the received input is |
| 122 | + * a valid alias command |
| 123 | + * @sub_command: a pointer to the array containing the commands |
| 124 | + * @alias_value: the value of the alias command |
59 | 125 | */ |
60 | | -char *extract_value(const char *value) |
| 126 | +void build_alias_cmd(char ***sub_command, char *alias_value) |
61 | 127 | { |
62 | | - int i, j; |
63 | | - char quote = value[0]; |
64 | | - int length = strlen(value); |
65 | | - char *content = malloc(length); |
| 128 | + char **dup_array = NULL; |
66 | 129 |
|
67 | | - if (content == NULL) |
| 130 | + if ((*sub_command)[1] != NULL) |
68 | 131 | { |
69 | | - return (NULL); |
70 | | - } |
| 132 | + /* save a copy of the the commands array, excluding the alias */ |
| 133 | + dup_array = duplicate_str_array((*sub_command) + 1); |
| 134 | + |
| 135 | + /* memory and build the command line string based on the alias value */ |
| 136 | + free_str(*sub_command); |
| 137 | + *sub_command = _strtok(alias_value, NULL); |
| 138 | + |
| 139 | + /* concatenate both arrays to form a complete command string */ |
| 140 | + concatenate_arrays(sub_command, dup_array); |
71 | 141 |
|
72 | | - for (i = 1, j = 0; i < length && value[i] != quote; ++i, ++j) |
| 142 | + /* clean up and return */ |
| 143 | + free_str(dup_array); |
| 144 | + } |
| 145 | + else |
73 | 146 | { |
74 | | - content[j] = value[i]; |
| 147 | + /* there was alias alright but no other arguments */ |
| 148 | + free_str(*sub_command); |
| 149 | + *sub_command = _strtok(alias_value, NULL); |
75 | 150 | } |
76 | | - content[j] = '\0'; |
77 | | - |
78 | | - return (content); |
79 | 151 | } |
0 commit comments