-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathmain.c
More file actions
144 lines (122 loc) · 3 KB
/
main.c
File metadata and controls
144 lines (122 loc) · 3 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
/*
*
* GROUP 5
* MAJOR 2
*
* *** compile with -lreadline ***
*
*/
#include "processor.h"
#define BUFSIZE 512
int main(int argc, char *argv[]){
char *myhistory[20]; //Array to hold 20 most recent commands
int historySize = 0; //Number of commands currently stored in history
int nextHistory = 0; //Next history index to be used
char *input; // char pointer to store user input
if((input = malloc(BUFSIZE*sizeof(char))) == NULL){
perror("malloc");
exit(1);
}
// signal handling
struct sigaction act; // signal struct
act.sa_handler = SIG_IGN;
for(int n = 1; n <= 64; n++)
{
// catching all catch-able signals
if(n ==9)
continue;
else if(n == 19)
continue;
else if(n == 32)
continue;
else if(n == 33)
continue;
// setting how to handle recieved signals
assert(sigaction(n, &act, NULL) == 0); // ERROR CHECK
}
if(argc > 2) // error
{
printf("\nUsage: ./major2 <optional batch file>\n\n");
}
else if(argc == 2) // batch mode
{
FILE* batchFile = fopen(argv[1], "r");
if(batchFile != NULL) // file is opened correctly
{
// char pointer to store user input
if((input = malloc(BUFSIZE*sizeof(char))) == NULL){
perror("malloc");
exit(1);
}
while(fgets(input, BUFSIZE, batchFile))
{
printf("From file: %s", input);
if(nextHistory >= 20){ //If next index exceeds 20, wrap index
nextHistory = 0;
}
//Update pointer to save input
char* temp;
if((temp = malloc(BUFSIZE*sizeof(char))) == NULL){
perror("malloc");
exit(1);
}
strcpy(temp, input);
myhistory[nextHistory] = temp; //Set input to history
nextHistory++; //Increase next index
historySize++; //increase size
if(historySize > 20){ //If size exceeds 20, don't
historySize = 20;
}
processor(input, &historySize, myhistory, &nextHistory);
printf("\n");
}
}
else // error
{
printf("Unable to open file\n");
exit(-1);
}
// closing file
fclose(batchFile);
}
else // interactive mode
{
while(1)
{
// char pointer to store user input
if((input = malloc(BUFSIZE*sizeof(char))) == NULL){
perror("malloc");
exit(1);
}
// prompts for and reads in users command
printf("major2> ");
if(fgets(input, BUFSIZE, stdin) == NULL){
perror("fgets");
exit(1);
}
int len = strlen(input);
if(input[len-1] == '\n'){
input[len-1] = '\0';
}
if(nextHistory >= 20){ //If index exceeds 20, wrap index
nextHistory = 0;
}
//Update pointer to save input
char* temp;
if((temp = malloc(BUFSIZE*sizeof(char))) == NULL){
perror("malloc");
exit(1);
}
strcpy(temp, input);
myhistory[nextHistory] = temp; //Set input to history
nextHistory++; //Increase next index
historySize++; //increase size
if(historySize > 20){ //If size exceeds 20, don't
historySize = 20;
}
// passing input to processor function
processor(input, &historySize, myhistory, &nextHistory);
}
}
return 0;
}