-
Notifications
You must be signed in to change notification settings - Fork 0
/
aliases.c
160 lines (131 loc) · 3.49 KB
/
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
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
// aliases.c
#include "functions.h"
int numOfAliases = 0;
aliasList *head = NULL;
void addAlias(char **tokens) {
aliasList *node;
aliasList *last = head;
// Allocate space
char *name = (char *)malloc(LINE_SIZE * sizeof(char));
char *val = (char *)malloc(LINE_SIZE * sizeof(char));
if (!name || !val) {
printError("Malloc failure");
exit(1);
}
// Read values from input
strcpy(name, strtok(tokens[1], "="));
strcpy(val, strtok(NULL, "\""));
if (isAlias(name)) {
printError("Alias already exists");
return;
}
// Check validity of this
node = (aliasList *)malloc(sizeof(aliasList));
if (!node) {
printError("Malloc failure");
}
// Initialize node with values
node->aliasEntry.name = name;
node->aliasEntry.tokens = tokenize(val);
node->next = NULL;
// Add node to linked list
if (head == NULL) {
head = node;
} else {
while (last->next != NULL) {
last = last->next;
}
last->next = node;
}
numOfAliases++;
}
bool isAlias(char *token) {
aliasList *node = head;
while (node != NULL) {
// If token matches an alias name
if (strcmp(token, node->aliasEntry.name) == 0) {
return true;
}
node = node->next;
}
return false;
}
char **getAliasTokens(char *name) {
char *val;
aliasList *node = head;
while (node != NULL) {
// If name matches an alias, return alias tokens
if (strcmp(name, node->aliasEntry.name) == 0) {
return node->aliasEntry.tokens;
} else {
node = node->next;
}
}
printError("Could not find Alias Tokens");
exit(1);
return NULL;
}
void listAliases() {
int i = 0;
aliasList *node = head;
while (node != NULL) {
printf("alias %s=\"", node->aliasEntry.name);
printf("%s", node->aliasEntry.tokens[i]);
i++;
while (node->aliasEntry.tokens[i] != NULL) {
printf(" %s", node->aliasEntry.tokens[i]);
i++;
}
printf("\"\n");
i = 0;
node = node->next;
}
}
char **checkAliases(char **tokens) {
int i = 0;
char **aliasTokens;
// Ignore if NULL
if (tokens[0] == NULL) {
return tokens;
}
// Ignore if trying to unalias
if (strcmp(tokens[0], "unalias") == 0) {
return tokens;
}
while (tokens[i] != NULL) {
// If token is an alias, remove it and
// insert its tokens into the token stream
if (isAlias(tokens[i])) {
// Get alias tokens
aliasTokens = getAliasTokens(tokens[i]);
// Remove alias from tokenstream
tokens = removeToken(tokens, tokens[i]);
// Insert alias tokens in its place
tokens = insertTokens(tokens, aliasTokens, i);
}
i++;
}
return tokens;
}
void removeAlias(char *alias) {
aliasList *node = head;
aliasList *prev = head;
while (node != NULL) {
if (strcmp(node->aliasEntry.name, alias) == 0) {
// If node is head
if (prev == node) {
head = NULL;
} else {
prev->next = node->next;
}
// Free allocated memory
free(node->aliasEntry.name);
free(node->aliasEntry.tokens);
free(node);
numOfAliases--;
break;
}
prev = node;
node = node->next;
}
}