Repository navigation
Expand file tree
/
Copy pathmain.c
More file actions
138 lines (118 loc) · 2.47 KB
/
Copy pathmain.c
File metadata and controls
138 lines (118 loc) · 2.47 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
/*
*
* GROUP 5
* MAJOR 2
*
*/
#include "processor.h"
#define BUFSIZE 512
int main(int argc, char *argv[]){
char *myhistory[20];
int historySize = 0;
int nextHistory = 0;
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){
nextHistory = 0;
}
char* temp;
if((temp = malloc(BUFSIZE*sizeof(char))) == NULL){
perror("malloc");
exit(1);
}
strcpy(temp, input);
myhistory[nextHistory] = temp;
nextHistory++;
historySize++;
if(historySize > 20){
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){
nextHistory = 0;
}
char* temp;
if((temp = malloc(BUFSIZE*sizeof(char))) == NULL){
perror("malloc");
exit(1);
}
strcpy(temp, input);
myhistory[nextHistory] = temp;
nextHistory++;
historySize++;
if(historySize > 20){
historySize = 20;
}
// passing input to processor function
processor(input, &historySize, myhistory, &nextHistory);
}
}
return 0;
}