-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathpastevents.c
More file actions
253 lines (229 loc) · 9.43 KB
/
pastevents.c
File metadata and controls
253 lines (229 loc) · 9.43 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
249
250
251
252
#include "headers.h"
int history(char** argument_tokens, int no_of_arguments, int* pastevents_present, int ip, char* curr_command, int store, int* pastevents_execute_present, char* input_string) {
*pastevents_present = 1; // flag to denote pastevents command is executed
if (no_of_arguments == 0) { // no arguments are passed than just print the pastevents
return pastevents();
} else { // if some argument is present
if (strcmp(argument_tokens[1], "purge") == 0) {
if (no_of_arguments > 1) {
fprintf(stderr, "\033[1;31mpastevents: invalid arguments\033[1;0m\n");
return 0;
}
// clears the stored list
return purge();
} else if (strcmp(argument_tokens[1], "execute") == 0) { // execute some pastevent whose event is given
if (no_of_arguments == 1 && ip == 0) {
// if no index is given which command to execute then show error
fprintf(stderr, "\033[1;31mpastevents: missing argument in \"%s\"\033[1;0m\n", curr_command);
return 0;
} else if (no_of_arguments == 1 && ip == 1) {
char number[MAX_LEN] = {0};
int num = 0; // variable to store the index of command to execute
int flag = 1; // valid index is passed between 1 and 15 (both inclusive)
char inp_buff[999999] = {0};
if (read(STDIN_FILENO, inp_buff, 999998) < 0) {
fprintf(stderr, "\033[1;31mpastevents : Error in reading\033[1;0m\n");
return 0;
}
if (inp_buff[strlen(inp_buff) - 1] == '\n') {
inp_buff[strlen(inp_buff) - 1] = '\0';
}
strcpy(number, inp_buff);
// converting char to int
// doing it the long way since some invalid string can also be given in argument so i had to handle it manually and not use atoi()
convert_to_int(number, &num, &flag);
if (flag) {
// executing the command at the given index
if (execute(num, store, curr_command, pastevents_execute_present, input_string) == 0) {
return 0;
}
}
} else if (no_of_arguments == 2) {
char* number = argument_tokens[2];
int num = 0; // variable to store the index of command to execute
int flag = 1; // valid index is passed between 1 and 15 (both inclusive)
// converting char to int
// doing it the long way since some invalid string can also be given in argument so i had to handle it manually and not use atoi()
convert_to_int(number, &num, &flag);
if (flag) {
// executing the command at the given index
if (execute(num, store, curr_command, pastevents_execute_present, input_string) == 0) {
return 0;
}
}
} else {
// excess of arguments are passed
fprintf(stderr, "\033[1;31mpastevents : excess arguments in \"%s\"\033[1;0m\n", curr_command);
return 0;
}
} else {
// invalid arguments are passed
fprintf(stderr, "\033[1;31mpastevents : invalid arguments in \"%s\"\033[1;0m\n", curr_command);
return 0;
}
}
return 1;
}
// Stores the input command in past_commands.txt file
int store_command(char* command) {
char past_commands_path[MAX_LEN];
strcpy(past_commands_path, home_directory);
strcat(past_commands_path, "/");
strcat(past_commands_path, "past_commands.txt");
FILE *fptr;
fptr = fopen(past_commands_path, "r");
if (fptr == NULL) { // if past_commands.txt file does not exist and we are creating it for the first time
fptr = fopen(past_commands_path, "w");
fprintf(fptr, "%s\n", command);
fclose(fptr);
} else {
// reading 15 commands at max from past_commands.txt
char** past_commands = (char**) calloc(15, sizeof(char*));
int write = 1;
for (int i = 0; i < 15; i++) {
past_commands[i] = (char*) calloc(MAX_LEN, sizeof(char));
fscanf(fptr, " %[^\n]", past_commands[i]);
if (i == 0) {
remove_leading_and_trailing_spaces(command);
remove_leading_and_trailing_spaces(past_commands[0]);
if (strcmp(command, past_commands[0]) == 0) {
write = 0;
break;
}
}
}
if (write == 0) {
fclose(fptr);
for (int i = 0; i < 15; i++) {
free(past_commands[i]);
}
free(past_commands);
} else {
fclose(fptr);
FILE *fptr2;
fptr2 = fopen(past_commands_path, "w");
if (fptr2 == NULL) {
fprintf(stderr, "\033[1;31mpastevents : error opening file for writing\033[1;0m\n");
return 0;
} else {
fprintf(fptr2, "%s\n", command);
for (int i = 0; i < 14; i++) {
if (past_commands[i][0] != '\0') {
fprintf(fptr2, "%s\n", past_commands[i]);
} else {
break;
}
}
fclose(fptr2);
}
for (int i = 0; i < 15; i++) {
free(past_commands[i]);
}
free(past_commands);
}
}
return 1;
}
// Prints the latest 15 (or less) pastevents
int pastevents() {
char past_commands_path[MAX_LEN] = {0};
strcpy(past_commands_path, home_directory);
strcat(past_commands_path, "/past_commands.txt");
FILE *fptr;
fptr = fopen(past_commands_path, "r");
if (fptr == NULL) {
fprintf(stderr, "\033[1;31mpastevents : fopen: Error while opening\033[1;0m\n");
return 0;
} else {
char** past_commands = (char**) calloc(15, sizeof(char*));
int write = 1;
for (int i = 0; i < 15; i++) {
past_commands[i] = (char*) calloc(MAX_LEN, sizeof(char));
fscanf(fptr, " %[^\n]", past_commands[i]);
}
for (int i = 14; i >= 0; i--) {
if (past_commands[i][0] != '\0'){
printf("%s\n", past_commands[i]);
}
}
for (int i = 0; i < 15; i++) {
free(past_commands[i]);
}
free(past_commands);
}
return 1;
}
// Clears the past_commands.txt file
int purge() {
FILE *fptr;
// Generating 'past_commands.txt' file path
char past_commands_path[MAX_LEN] = {0};
strcpy(past_commands_path, home_directory);
strcat(past_commands_path, "/past_commands.txt");
// Opening file in write mode and then closing it (it will clear out all the previous contents in the file)
fptr = fopen(past_commands_path, "w");
if (fptr == NULL) {
fprintf(stderr, "\033[1;31mpastevents : Error while opening file for writing\033[1;0m\n");
return 0;
}
fclose(fptr);
return 1;
}
// Executes a particular command from the history based on index [1, 15]
int execute(int num, int store, char* curr_command, int* pastevents_execute_present, char* input_string) {
FILE *fptr;
char past_commands_path[MAX_LEN] = {0};
strcpy(past_commands_path, home_directory);
strcat(past_commands_path, "/past_commands.txt");
fptr = fopen(past_commands_path, "r");
if (fptr == NULL) {
fprintf(stderr, "\033[1;31mpastevents : fopen: File not foound\033[1;0m\n");
return 0;
} else {
char** past_commands = (char**) calloc(15, sizeof(char*));
for (int i = 0; i < 15; i++) {
past_commands[i] = (char*) calloc(MAX_LEN, sizeof(char));
fscanf(fptr, " %[^\n]", past_commands[i]);
}
fclose(fptr);
if (past_commands[num - 1][0] == '\0') {
fprintf(stderr, "\033[1;31mpastevents : Invalid Argument\033[1;0m\n");
return 0;
} else {
char** tkns = generate_tokens(input_string, ' ');
int y = 0;
int pastevents_idx = -1;
while (tkns[y] != NULL) {
if (strcmp(tkns[y], "pastevents") == 0 && strcmp(tkns[y + 1], "execute") == 0) {
pastevents_idx = y;
break;
}
y++;
}
if (pastevents_idx == -1) {
fprintf(stderr, "\033[1;31mpastevents : Error occured in replacement\033[1;0m\n");
return 0;
} else {
char temp[MAX_LEN] = {0};
for (int u = 0; u < y; u++) {
strcat(temp, " ");
strcat(temp, tkns[u]);
}
strcat(temp, " ");
strcat(temp, past_commands[num - 1]);
for (int u = y + 3; tkns[u] != NULL; u++) {
strcat(temp, " ");
strcat(temp, tkns[u]);
}
strcpy(input_string, temp);
}
*pastevents_execute_present = 1;
input(past_commands[num - 1], store, 0);
}
for (int i = 0; i < 15; i++) {
free(past_commands[i]);
}
free(past_commands);
}
return 1;
}