This repository has been archived by the owner on Sep 1, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathalias.c
264 lines (211 loc) · 6.59 KB
/
alias.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
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
/* alias.c */
#include <stdio.h>
#include <string.h>
#include <stdbool.h>
#include <stdlib.h>
#include "parser.h"
#include "list.h"
#define MAX_ALIASES 10
char* aliases[MAX_ALIASES];
char* aliasValues[MAX_ALIASES][MAX_TOKEN_ARR];
int aliasHead = 0;
int aliasTail = -1;
int aliasSize = 0;
int printAlias() {
if (aliasTail == -1) {
printf("Nothing to show!\n\n");
return 0;
}
int h = aliasHead;
while (h != aliasTail) {
printf("%s: ", aliases[h]);
fprintArr(stdout, aliasValues[h]);
printf("\n");
h = (h + 1) % MAX_ALIASES;
}
printf("%s: ", aliases[aliasTail]);
fprintArr(stdout, aliasValues[aliasTail]); // prints final line
printf("\n\n");
return 0;
}
int check_alias(char* token, char* argvOut[], int index, List alias_chain){
int curLen = 1;
//check all but tail
int h = aliasHead;
int rep = 0; // if alias match is found
for(;;) {
if (strcmp(aliases[h], token) == 0){
rep = 1;
if (contains(alias_chain, token)){
printf("ERROR: Alias loop detected.\n\n");
return -1;
}
push(alias_chain, token); // add to chain
int count = arrLength(aliasValues[h]);
if (index + (count-1) >= MAX_TOKEN_ARR) {
fprintf(stderr, "ERROR: Unaliased command has exceeded token limit (%d). Total number of commands/arguments cannot exceed this value.\n\n", MAX_TOKENS);
curLen = -1;
break;
}
copyNTArr(aliasValues[h], &argvOut[index], count);
curLen += count - 1;
for (int i = 0; i < count; i++){
int c = check_alias(aliasValues[h][i], argvOut, index + i, copy_list(alias_chain));
if (c == -1) return -1; // err
index += c - 1;
curLen += c - 1;
}
break;
}
if (h == aliasTail) break; // break if tail
h = (h + 1) % MAX_ALIASES;
}
if (!rep) {
if (index >= MAX_TOKENS) {
fprintf(stderr, "ERROR: Unaliased command has exceeded token limit (%d). Total number of commands/arguments cannot exceed this value.\n\n", MAX_TOKENS);
curLen = -1;
} else {
argvOut[index] = strdup(token);
}
}
clear(alias_chain);
free(alias_chain);
return curLen;
}
int parseAliases(int argc, char* argv[]) {
if(aliasTail == -1) return argc;
int newArrLen = 0;
char* newArr[MAX_TOKEN_ARR];
for (int i = 0; i < argc; i++){
int c = check_alias(argv[i], newArr, newArrLen, new_list());
if (c == -1) return -1; // err
newArrLen += c;
}
freeNTArr(argv); // free old argv
copyNTArr(newArr, argv, newArrLen);
return newArrLen;
}
void remove_alias(int index) {
free(aliases[index]);
aliases[index] = NULL;
freeNTArr(aliasValues[index]);
aliasValues[index][0] = NULL;
aliasSize--;
if (aliasSize == 0) {
aliasHead = 0;
aliasTail = -1;
} else {
// shift remaining aliases to the left
while (index != aliasTail) {
free(aliases[index]);
aliases[index] = strdup(aliases[(index + 1) % MAX_ALIASES]);
freeNTArr(aliasValues[index]);
copyNTArr(aliasValues[(index + 1) % MAX_ALIASES], aliasValues[index], arrLength(aliasValues[(index + 1) % MAX_ALIASES]));
index = (index + 1) % MAX_ALIASES;
}
free(aliases[aliasTail]);
aliases[aliasTail] = NULL;
freeNTArr(aliasValues[aliasTail]);
aliasValues[aliasTail][0] = NULL;
aliasTail--;
if (aliasTail == -1) aliasTail = MAX_ALIASES - 1;
}
}
int unalias(int argc, char* argv[]) {
if (aliasTail == -1) {
printf("Nothing to unalias!\n\n");
return 0;
}
if (argc != 2) {
printf("ERROR: Please specify the alias to remove.\n\n");
return 1;
}
int h = aliasHead;
for (;;) {
if (strcmp(aliases[h], argv[1]) == 0){
remove_alias(h);
printf("Alias removed!\n\n");
return 0;
}
if (h == aliasTail) break; // break if tail
h = (h + 1) % MAX_ALIASES;
}
printf("Alias not found.\n\n");
return 1;
}
void add_alias(char* alias, char* value[], int valc) {
if (aliasTail == -1) aliasTail = 0; // init
else {
// update head/tail
aliasTail = (aliasTail + 1) % MAX_ALIASES;
if (aliasTail == aliasHead) {
aliasHead = (aliasHead + 1) % MAX_ALIASES;
}
}
if (aliasSize != MAX_ALIASES) {
aliasSize++;
}
aliases[aliasTail] = strdup(alias);
copyNTArr(value, aliasValues[aliasTail], valc);
}
int alias(int argc, char* argv[]) {
if(argc == 1) return printAlias();
else if (argc == 2) {
printf("ERROR: Alias requires at least two arguments.\n");
return 0;
}
// check if alias already exists
if(aliasSize > 0) {
int h = aliasHead;
for (;;) {
if (strcmp(aliases[h], argv[1]) == 0){
printf("Alias already exists. Overriding...\n");
remove_alias(h);
break;
}
if (h == aliasTail) break; // break if tail
h = (h + 1) % MAX_ALIASES;
}
}
if(aliasSize == MAX_ALIASES){
printf("Aliases full!\n\n");
return 0;
}
add_alias(argv[1], &argv[2], argc - 2);
printf("Added alias '%s'!\n\n", aliases[aliasTail]);
return 0;
}
int saveAliases(){
char path[512];
strcpy(path, getenv("HOME"));
strcat(path, "/.aliases");
FILE* fptr = fopen(path, "w");
if (aliasTail == -1) return 0; // No aliases.
int h = aliasHead;
while (h != aliasTail) {
fprintf(fptr, "alias %s ", aliases[h]);
fprintArr(fptr, aliasValues[h]);
fprintf(fptr, "\n");
h = (h + 1) % MAX_ALIASES;
}
fprintf(fptr, "alias %s ", aliases[aliasTail]);
fprintArr(fptr, aliasValues[aliasTail]); // writes final line
fclose(fptr);
return 0;
}
int loadAliases(){
char path[512];
strcpy(path, getenv("HOME"));
strcat(path, "/.aliases");
FILE* fptr = fopen(path, "r");
if (fptr == NULL) return 1; // file does not exist
char buffer[512]; // stores each line of file over loop
while(fgets(buffer, sizeof(buffer), fptr) != NULL) {
buffer[strcspn(buffer, "\n")] = 0; // removes newline from end
char* argv[MAX_TOKEN_ARR];
int argc = parseDelimiterArray(argv, buffer, " ");
add_alias(argv[1], &argv[2], argc - 2);
}
fclose(fptr);
return 0;
}