-
Notifications
You must be signed in to change notification settings - Fork 0
/
expand_aliases.c
68 lines (57 loc) · 1.44 KB
/
expand_aliases.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
#include "hsh.h"
/**
* expand_aliases - perform recursive alias expansion on the current command
* @aliases: alias list
* @tokptr: pointer to the current tokens
*
* Return: If expansion succeeds, return a pointer t to the otherwise 0
*/
void expand_aliases(alias_t *aliases, char ***tokptr)
{
char **new, **old, *name, *value, *temp;
if (!*tokptr)
return;
do {
name = expand_alias(aliases, tokptr);
value = get_dict_val(aliases, name);
if (value && *value && _isspace(value[_strlen(value) - 1]))
{
old = *tokptr;
new = arrdup(old + 1);
expand_aliases(aliases, &new);
temp = *(old + 1);
*(old + 1) = NULL;
*tokptr = arrjoin(old, new);
*(old + 1) = temp;
free_tokens(&old);
free_tokens(&new);
}
} while (name && **tokptr && _strcmp(name, **tokptr));
}
/**
* expand_alias - perform a single alias expansion on the current command
* @aliases: alias list
* @tokptr: pointer to the current tokens
*
* Return: If expansion succeeds, return a pointer the alias name.
* Otherwise, return NULL.
*/
char *expand_alias(alias_t *aliases, char ***tokptr)
{
char **alias_tokens, **tokens = *tokptr;
if (!*tokens)
return (NULL);
while (aliases)
{
if (!_strcmp(*tokens, aliases->key))
{
alias_tokens = tokenize(aliases->val);
*tokptr = arrjoin(alias_tokens, tokens + 1);
free_tokens(&tokens);
free_tokens(&alias_tokens);
return (aliases->key);
}
aliases = aliases->next;
}
return (NULL);
}