-
Notifications
You must be signed in to change notification settings - Fork 0
/
history.c
468 lines (437 loc) · 15.1 KB
/
history.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
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include "c/cs323/Hwk4/history.h"
//GlobalVariables
struct token **tokenHistory = NULL;
int currentPossition = 0; //Next possition in history to be filled
int historyTotal = 0; //Total # of items added
int currentCount = 0; //Number of items since last clear
int historySize = 323;
//Creates a new string equal to string1 + string2
char *combine(char *string1, char *string2) {
int firstLength = strlen(string1);
int secondLength = strlen(string2);
char *result = malloc(firstLength + secondLength + 1);
strcpy(result, string1);
strcat(result, string2);
result[firstLength + secondLength] = '\0';
free(string1);
free(string2);
return result;
}
//Returns the correct index for the possition in history
//Returns 1 if it has been cleared or overwritten
int getIndexHistoryPossition (int input){
int relativePosition = currentCount - (historyTotal - input);
if (input > historyTotal ||
input <= historyTotal - currentCount ||
(relativePosition <= currentCount - historySize && historyTotal > historySize)) {
return -1;
}
return (relativePosition - 1) % historySize;
}
char *stringCopy (char *string) {
char *copy = malloc (strlen(string) + 1);
strcpy(copy, string);
copy[strlen(string)] = '\0';
return copy;
}
char *convertTokensToString (struct token *list) {
struct token *current = list;
char *outputString = malloc(1);
outputString[0] = '\0';
while (current) {
if (outputString[0]) {
char *lineSpace = malloc(2);
lineSpace[0] = ' ';
lineSpace[1] = '\0';
outputString = combine(outputString, lineSpace);
}
char *tokenText = stringCopy(current -> text);
outputString = combine(outputString, tokenText);
current = current -> next;
}
return outputString;
}
//Creates a string out of all the texts in a token list
//Excluding the first token
char *convertAllButFirst (struct token *list) {
struct token *current = list;
char *outputString = malloc(1);
outputString[0] = '\0';
while (current -> next) {
if (outputString[0]) {
char *lineSpace = malloc(2);
lineSpace[0] = ' ';
lineSpace[1] = '\0';
outputString = combine(outputString, lineSpace);
}
char *tokenText = stringCopy(current -> next -> text);
outputString = combine(outputString, tokenText);
current = current -> next;
}
return outputString;
}
//Returns the text for the token at possition pos
char *getTokenForPos (struct token *list, int pos) {
int count = 0;
struct token *current = list;
while (count != pos) {
if (current -> next) {
current = current -> next;
}
else {
return NULL;
}
count ++;
}
return stringCopy(current -> text);
}
//Gets the last token of a list
char *getLastToken (struct token *list) {
struct token *current = list;
while (current -> next) {
current = current -> next;
}
return stringCopy(current -> text);
}
//Gets the length of an integer
int getIntLength (int tokenNum) {
int numLength = 0;
while (tokenNum >= 1) {
tokenNum /= 10;
numLength ++;
}
return numLength;
}
//Determines which token(s) from the list that should be in the replacement
char *getTokenSequenceForList (struct token *list,
int *iPointer,
const char *oldLine,
int *replacementStatus) {
if (oldLine[iPointer[0]] == '^'){
return getTokenForPos (list, 1);
}
else if (oldLine[iPointer[0]] == '$'){
return getLastToken (list);
}
else if (oldLine[iPointer[0]] == ':'){
if (strlen(&oldLine[iPointer[0]]) > 1) {
if (oldLine[iPointer[0] + 1] >= 48 && oldLine[iPointer[0] + 1] <= 57) {
char *endptr;
int tokenNum = strtol (&oldLine[iPointer[0] + 1], &endptr, 10);
char *tokenReturned = getTokenForPos (list, tokenNum);
int numLength = getIntLength (tokenNum);
iPointer[0] += numLength;
return tokenReturned;
}
}
}
else if (oldLine[iPointer[0]] == '*'){
return convertAllButFirst (list);
}
iPointer[0] --;
return convertTokensToString(list);
}
//Extracts the string from !?STRING?
char *getString (const char *oldLine) {
int length = strlen(oldLine);
char *outputString = malloc(length);
int i;
for (i = 0; oldLine[i] != '?' ; i ++) {
if (!oldLine[i] || oldLine[i] == '\n' || oldLine[i] == '!') {
break;
}
outputString[i] = oldLine[i];
}
outputString[i] = '\0';
return outputString;
}
int searchTokenList (char *string, struct token *list) {
struct token *current = list;
while (current) {
if (strstr(current -> text, string)) {
return 1;
}
current = current -> next;
}
return 0;
}
struct token *findStringInHistory (char *string) {
for (int i = currentPossition - 1; i >= 0; i --) {
if (searchTokenList(string, tokenHistory[i])){
return tokenHistory[i];
}
}
return NULL;
}
//Skips to the end of an escape sequence
void skipToEnd (const char *oldLine, int *iPointer) {
char *characters = "^*$";
int foundChar = 0;
for (int i = 0; i < 4; i++) {
if(oldLine[iPointer[0] + 1] == characters[i]) {
foundChar = 1;
}
}
if (foundChar){
iPointer[0] ++;
}
else if (oldLine[iPointer[0] + 1] == ':' ){
if (strlen(&oldLine[iPointer[0]]) > 1) {
if (oldLine[iPointer[0] + 2] >= 48 && oldLine[iPointer[0] + 2] <= 57) {
char *endptr;
int tokenNum = strtol (&oldLine[iPointer[0] + 2], &endptr, 10);
int numLength = getIntLength (tokenNum);
iPointer[0] += numLength + 1;
return;
}
}
}
else {
return;
}
}
//Determines if a replacement designator has been passed in and
//replases and locates the appropriate list
char *getReplacement (int *iPointer, const char *oldLine, int *replacementStatus) {
int lineLength = strlen(&oldLine[iPointer[0]]);
char *endptr;
struct token *listToSearch;
int listNum;
if (oldLine[iPointer[0]] == '!') {
if (!tokenHistory){
if (oldLine[iPointer[0] + 1]) {
skipToEnd(oldLine, iPointer);
}
return NULL;
}
iPointer[0] += 1;
return getTokenSequenceForList (tokenHistory[currentPossition - 1],
iPointer, oldLine,
replacementStatus);
replacementStatus[0] = 1;
}
else if (oldLine[iPointer[0]] == '-'){
if (lineLength > 1) {
if (oldLine[iPointer[0] + 1] >= 48 && oldLine[iPointer[0] + 1] <= 57) {
listNum = strtol (&oldLine[iPointer[0] + 1], &endptr, 10);
int index = getIndexHistoryPossition (historyTotal + 1 - listNum);
int numLength = getIntLength (listNum);
iPointer[0] += numLength + 1;
if (!tokenHistory){
if (oldLine[iPointer[0] + 1]) {
skipToEnd(oldLine, iPointer);
}
return NULL;
}
if (index >= 0){
listToSearch = tokenHistory[index];
return getTokenSequenceForList (listToSearch,
iPointer,
oldLine,
replacementStatus);
}
}
}
replacementStatus[0] = 1;
}
else if (oldLine[iPointer[0]] >= 48 && oldLine[iPointer[0]] <= 57) {
listNum = strtol (&oldLine[iPointer[0]], &endptr, 10);
int index = getIndexHistoryPossition(listNum);
int numLength = getIntLength (listNum);
iPointer[0] += numLength;
if (!tokenHistory){
if (oldLine[iPointer[0] + 1]) {
if (oldLine[iPointer[0] + 1] ==':') {
iPointer[0] ++;
}
skipToEnd(oldLine, iPointer);
}
return NULL;
}
if (index >= 0){
listToSearch = tokenHistory[index];
return getTokenSequenceForList (listToSearch,
iPointer,
oldLine,
replacementStatus);
}
replacementStatus[0] = 1;
}
else if (oldLine[iPointer[0]] == '?') {
if (lineLength > 1) {
char *searchString = getString (&oldLine[iPointer[0] + 1]);
listToSearch = findStringInHistory (searchString);
iPointer[0] += strlen(searchString) + 1;
if (oldLine[iPointer[0]] == '?'){
iPointer[0] ++;
}
if (!tokenHistory || !listToSearch){
if (oldLine[iPointer[0] + 1]) {
skipToEnd(oldLine, iPointer);
}
return NULL;
}
free(searchString);
return getTokenSequenceForList (listToSearch,
iPointer,
oldLine,
replacementStatus);
}
replacementStatus[0] = 1;
}
if (replacementStatus[0] != 1) {
replacementStatus[0] = -1;
}
return NULL;
}
// hExpand() returns a copy of the string OLDLINE in which any substring of
// // the command line of the form !! or !N or !-N or !?STRING? as follows:
// //
// // !! -> last remembered command
// //
// // !N -> N-th command read (N is any sequence of digits)
// //
// // !-N -> N-th last remembered command (N is any sequence of digits)
// //
// // !?STRING? -> most recent remembered command with a token that contains
// // STRING (= a nonempty sequence of characters that does not
// // contain whitespace or ! or ?)
// //
// // The replacement string consists of the specified sequence of tokens
// // separated by single blanks.
// //
// // If the notation above is followed by a *, then the leading token in the
// // command is omitted. If it is followed by a ^, then only the first token is
// // used. If it is followed by a :M (where M is any sequence of digits), then
// // only the M-th token is used. If it is followed by a $, then only the last
// // token is used.
// //
// // The search for substrings to expand proceeds from left to right and
// // continues after each replacement string. If the requested substitution
// // is impossible, then the substring is deleted, but the search continues.
// // Storage for the new line is allocated by malloc()/realloc(). The string
// // OLDLINE is not modified.
// //
// // hExpand() sets STATUS to 1 if substitutions were made and all succeeded, to
// // 0 if no substitutions were requested, and to -1 if some substitution failed.
//
//
char *hExpand (const char *oldLine, int *status) {
char *expanded = malloc(1);
expanded[0] = '\0';
int stringLength = strlen(oldLine);
int statusTracker = 0;
int iPointer[1];
for (int i = 0; i < stringLength - 1; i++) {
if (oldLine[i] == '!' && i + 1 < stringLength - 1){
int replacementStatus[1];
iPointer[0] = i;
replacementStatus[0] = 0;
iPointer[0] ++;
char *replacement = getReplacement (iPointer, oldLine, replacementStatus);
if (replacementStatus[0] == -1) {
char *letterToAdd = malloc(2);
letterToAdd[0] = oldLine[i];
letterToAdd[1] = '\0';
expanded = combine (expanded, letterToAdd);
}
else if (!replacement) {
statusTracker = -1;
i = iPointer[0];
}
else if (replacementStatus[0] != -1) {
if (statusTracker != -1){
statusTracker = 1;
}
expanded = combine (expanded, replacement);
i = iPointer[0];
}
}
else {
char *letterToAdd = malloc(2);
letterToAdd[0] = oldLine[i];
letterToAdd[1] = '\0';
expanded = combine (expanded, letterToAdd);
}
}
status[0] = statusTracker;
char *endString = malloc(2);
endString[0] = '\n';
endString[1] = '\0';
expanded = combine(expanded, endString);
return expanded;
}
//Creates the array where tokens are storred
void createTokenHistory (void) {
char *histString = getenv("HISTSIZE");
if (histString) {
historySize = atoi(histString);
}
if (!tokenHistory) {
tokenHistory = malloc(historySize * sizeof(struct token *));
for (int i = 0; i < historySize; i ++) {
tokenHistory[i] = NULL;
}
}
}
// // hRemember() adds (a copy of) the command represented by the token list LIST
// // to the list of remembered commands as the NCMD-th command (where NCMD = 1 on
// // the first invocation and increases by one on every subsequent invocation).
void hRemember (int ncmd, token *list) {
createTokenHistory();
char *tokenString = convertTokensToString(list);
if (tokenHistory[currentPossition]) {
freeList(tokenHistory[currentPossition]);
}
tokenHistory[currentPossition] = lex(tokenString);
free(tokenString);
currentCount ++;
historyTotal = ncmd;
currentPossition = currentCount % historySize;
}
//Clears all the tokens from history
void hClear (void) {
if (tokenHistory){
for (int i = 0; i < historySize; i++) {
if (tokenHistory[i]){
freeList(tokenHistory[i]);
tokenHistory[i] = NULL;
}
}
free(tokenHistory);
tokenHistory = NULL;
}
currentPossition = 0;
currentCount = 0;
}
//Prints all tokens in a list
void printAllTokens (struct token *headToken) {
struct token *currentToken = headToken;
while (currentToken -> next) {
printf("%s ",currentToken -> text);
currentToken = currentToken -> next;
}
printf("%s\n",currentToken -> text);
}
// hDump(n) writes the most recent N remembered commands by increasing number
// // to the standard output using something equivalent to
// //
// // printf ("%6d %s %s ... %s\n", icmd, token0, token1, ..., tokenLast)
// //
// // where ICMD is the number of the command and TOKEN0, ..., TOKENLAST are its
// // tokens.
void hDump (int n) {
int start = historyTotal - (n - 1);
while (start <= historyTotal) {
int index = getIndexHistoryPossition(start);
if (index >= 0) {
printf("%6d ", start);
struct token *tokenToPrint = tokenHistory[index];
printAllTokens (tokenToPrint);
}
start ++;
}
}