Repository navigation
Expand file tree
/
Copy pathmain.c
More file actions
248 lines (224 loc) · 8.29 KB
/
Copy pathmain.c
File metadata and controls
248 lines (224 loc) · 8.29 KB
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
#include "headerFile.h"
int main(void) {
char input[MAX_LEN+1];
char original_path[1024]; strcpy (original_path, getenv("PATH"));
char *tknCmd;
char *tknArg[50];
int history_count = 0;
char history[20][MAX_LEN+1];
char historyPath[] = ".hist_list";
// Get the path to the user's home directory - goes at start
char* home = getenv("HOME");
if (home == NULL) {
fprintf(stderr, "Unable to get home directory\n");
exit(EXIT_FAILURE);
}
// Changes the current working directory to the user's home directory - goes at start
if (chdir(home) != 0) {
fprintf(stderr, "Unable to change directory to %s\n", home);
exit(EXIT_FAILURE);
}
Alias aliases[10];
int numAliases=0;
readAliasesFromFile(".aliases.txt",aliases,&numAliases);
// read in from savedHistory.txt and update the counter to contain the latest from the history
FILE *readFile = fopen(historyPath,"r");
if (!readFile){
printf("Error! Could not open file, so we made you one \n");
}
else{
char line[20][MAX_LEN];
while(fgets(line[history_count], MAX_LEN, readFile)){
line[history_count][strcspn(line[history_count], "\n")] = '\0';
strcpy(history[history_count], line[history_count]);
history_count++;
}
fclose(readFile);
}
// Checks for "exit" and EOF (generated by Ctrl-D)
while (!feof(stdin)) {
for (int u = 0; u < 49; u++) {
tknArg[u] = NULL;
}
//Take input from user
printf(">~>~> ");
fgets(input, MAX_LEN, stdin);
if(DEBUG){ printf("%s", input); printf("\n"); }
//Takes user input again if it is null or over 512 chars
while(checkInputLength(input)==1 || is_null_string(input)==1){
flushInput(input);
printf(">~>~> ");
fgets(input, MAX_LEN, stdin);
}
//Remove newline character at the end of the string
input[strcspn(input, "\n")] = '\0';
// functions here that replace input with command saved in history if !! / !<no.> / !-<no.>
if (input[0] == '!') {
// for commands that start with !-
if (input[1] == '-') {
if (strlen(input) > 4){
printf("error in calling history command, please use correct formatting\n");
continue;
}
int position = atoi(input + 2);
if (position < history_count + 1 && position > 0) {
printf("%s \n",history[history_count - position]);
strcpy(input, history[history_count - position]);
}
else {
printf("theres nothing in that history you muffin \n");
continue;
}
}
else if (input[1] == '!') {
if (strlen(input) > 2 ){
printf("error in calling history command, please use correct formatting\n");
continue;
}
if (history_count > 0) {
printf("%s \n",history[history_count-1]);
strcpy(input, history[history_count-1]);
}
else{
printf("theres nothing in that history you muffin \n");
continue;
}
}
else{
if (strlen(input) > 3 ){
printf("error in calling history command, please use correct formatting\n");
continue;
}
int position = atoi (input + 1);
if (position < history_count + 1 && position > 0) {
printf("%s \n",history[position-1]);
strcpy(input, history[position - 1]);
}
else {
printf("theres nothing in that history you muffin \n");
continue;
}
}
}
// saves the input to history after it's been validated
else if (strlen(input) != 0){
if (history_count == MAX_HISTORY_SIZE) {
// Shift history array to make room for new command
for (int i = 0; i < MAX_HISTORY_SIZE - 1; i++) {
strcpy(history[i], history[i+1]);
}
strcpy(history[MAX_HISTORY_SIZE-1], input);
} else {
strcpy(history[history_count++], input);
}
}
// Checks for " " in the input line and creates a token
char *token = strtok(input, DELIMITERS);
//Check if token is exit command
if(token!=NULL){
if(checkExit(token)){
break;
}
}
//Splits the input tokens into tknArgs and tknCmd
int i = 0;
while (token != NULL) {
tknArg[i] = token;
i++;
token = strtok(NULL, DELIMITERS);
}
tknArg[i] = NULL;
tknCmd = tknArg[0];
// Alias parameter handling
if(tknCmd!=NULL){
if(strcmp(tknCmd,"alias")==0) {
if (tknArg[1] == NULL) {
printAliases(aliases, numAliases);
} else {
if(tknArg[2]==NULL){
printf("Error alias must contain parameters\n");
}
else if(numAliases==10&&(posInAlias(aliases,tknArg[1]))==-1){
printf("Error aliases are full, remove an alias if you want to map a new one!\n");
} else {
addAlias(aliases, &numAliases, tknArg[1], &tknArg[2], i - 2);
}
}
continue;
}
if(strcmp(tknCmd,"unalias")==0){
if(tknArg[1]==NULL){
printf("Error unalias must contain parameters\n");
}
else{
unAlias(tknArg[1],aliases,&numAliases);}
continue;
}
}
int numOut=0;
if(tknCmd!=NULL){
//substitute for aliases
char** substituted = substituteAlias(aliases,numAliases,tknArg,i,&numOut);
//converts char** to char* []
convertCharArr(substituted, countItemsOfArray(substituted), (char **) &tknArg);
free(substituted);
}
tknCmd = tknArg[0];
i = countItemsOfArray(tknArg);
tknArg[i]=NULL;
tknArg[numOut]=NULL;
if(tknCmd!=NULL){
if(checkExit(tknCmd)){
break;
}
}
if (tknCmd != NULL){
if (strcmp(tknCmd, "getpath") == 0) {
if (tknArg[1] == NULL){
printf("%s", getpath());
printf("\n");
}
else{
printf("Error! command has extra parameters when not needed \n");
}
}
else if (strcmp(tknCmd, "setpath") == 0) {
if (tknArg[1] == NULL) {
printf("Error! command has no parameters it needs path \n");
}
else if (tknArg[2] != NULL) {
printf("Error! command has extra parameters, just use one path \n");
}
else {
setpath(tknArg[1]);
}
}
else if (strcmp(tknCmd, "cd") == 0) {
if(tknArg[1] == NULL) {
cd();
}
else if (tknArg[2] == NULL){
cdArg(tknArg[1]);
}
else {
printf("too many parameters to cd, %s not needed\n", tknArg[2]);
}
}
else if (strcmp(tknArg[0], "history") == 0){
if (tknArg[1] == NULL){
printHistory(history, history_count);
}
else {
printf("cannot print history, %s not needed\n", tknArg[1]);
}
}
else {
processFork(tknCmd,tknArg);
}
}
}
writeHistoryToFile(historyPath, history_count, history);
writeAliasToFile(aliases, numAliases);
restorePath(original_path);
exit(0);
}