-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.c
More file actions
219 lines (175 loc) · 5.74 KB
/
Copy pathmain.c
File metadata and controls
219 lines (175 loc) · 5.74 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
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
#include <stdint.h>
#include <regex.h>
#include "input.h"
#include "OSIIA1_pattern.h"
#include "OSIIA1_terminal.h"
#include "OSIIA1_threads.h"
#undef DEBUG
volatile int PROGRAM_IS_RUNNING = 1;
volatile uint16_t CPU_IS_EXECUTING = 0;
struct job_instance *CURRENT_JOB = NULL;
volatile int WINDOW_WIDTH = 0;
volatile int WINDOW_HEIGHT = 0;
int RET;
regex_t REGEX;
time_t TIME_QUANTA = 1;
time_t STARTING_TIME = 0;
time_t END_TIME_FOR_PREVIOUS_JOB = 0;
struct Bucket *IN_BUCKET = NULL;
struct Bucket *SUS_BUCKET = NULL;
struct job_instance_record **RECORDS = NULL;
volatile uint16_t RECORDS_COUNT = 0;
pthread_mutex_t BUCKET_MUTEX = PTHREAD_MUTEX_INITIALIZER;
pthread_mutex_t TERMINAL_MUTEX = PTHREAD_MUTEX_INITIALIZER;
volatile uint16_t NUMBER_OF_JOBS = 1;
WINDOW *CPU_EXEC_LOG_WIN = NULL;
WINDOW *HANDLE_USER_INPUT_WIN = NULL;
WINDOW *GRANTT_CHART_DISPLAY_WIN = NULL;
WINDOW *HANDLE_USER_INPUT_INNER_WIN = NULL;
WINDOW *CPU_EXEC_LOG_INNER_WIN = NULL;
char **HISTORY_COMMANDS = NULL;
static void free_history()
{
if (!HISTORY_COMMANDS)
return;
for (int i = 0; i < HISTORY_MAX; i++)
{
if (HISTORY_COMMANDS[i])
free(HISTORY_COMMANDS[i]);
}
free(HISTORY_COMMANDS);
HISTORY_COMMANDS = NULL;
}
int main(int argc, char **argv)
{
/* initialize ncurses */
if (!initscr())
{
perror("initscr");
return 1;
}
noecho();
cbreak();
get_current_terminal_dimensions();
OSIIA1_play_boot_sequence();
if (!WINDOW_HEIGHT)
{
printf("Could not assign window height value.\n");
return 1;
}
if (!WINDOW_WIDTH)
{
printf("Could not assign window width value.\n");
return 1;
}
int grant_chart_height = 7;
HANDLE_USER_INPUT_WIN = newwin(WINDOW_HEIGHT - grant_chart_height - 1, (int)(WINDOW_WIDTH / 2), 1, WINDOW_WIDTH - (int)(WINDOW_WIDTH / 2));
if (!HANDLE_USER_INPUT_WIN)
{
perror("newwin");
return 1;
}
CPU_EXEC_LOG_WIN = newwin(WINDOW_HEIGHT - grant_chart_height - 1, (int)(WINDOW_WIDTH / 2), 1, 0);
if (!CPU_EXEC_LOG_WIN)
{
perror("newwin");
return 1;
}
GRANTT_CHART_DISPLAY_WIN = newwin(grant_chart_height, WINDOW_WIDTH, WINDOW_HEIGHT - grant_chart_height, 0);
if (!GRANTT_CHART_DISPLAY_WIN)
{
perror("newwin");
return 1;
}
refresh();
/* border */
box(HANDLE_USER_INPUT_WIN, 0, 0);
box(CPU_EXEC_LOG_WIN, 0, 0);
box(GRANTT_CHART_DISPLAY_WIN, 0, 0);
// Create inner windows for content
int h, w;
getmaxyx(HANDLE_USER_INPUT_WIN, h, w);
HANDLE_USER_INPUT_INNER_WIN = derwin(HANDLE_USER_INPUT_WIN, h - 2, w - 2, 1, 1);
getmaxyx(CPU_EXEC_LOG_WIN, h, w);
CPU_EXEC_LOG_INNER_WIN = derwin(CPU_EXEC_LOG_WIN, h - 2, w - 2, 1, 1);
scrollok(HANDLE_USER_INPUT_INNER_WIN, TRUE);
scrollok(CPU_EXEC_LOG_INNER_WIN, TRUE);
idlok(HANDLE_USER_INPUT_INNER_WIN, TRUE);
idlok(CPU_EXEC_LOG_INNER_WIN, TRUE);
leaveok(CPU_EXEC_LOG_INNER_WIN, TRUE);
wrefresh(HANDLE_USER_INPUT_WIN);
wrefresh(CPU_EXEC_LOG_WIN);
wrefresh(GRANTT_CHART_DISPLAY_WIN);
wrefresh(HANDLE_USER_INPUT_INNER_WIN);
wrefresh(CPU_EXEC_LOG_INNER_WIN);
/* --- functionality --- */
STARTING_TIME = time(NULL);
/* allocate buckets */
/* incomming buckets */
IN_BUCKET = calloc(1, sizeof(struct Bucket));
/* initalize bucket values */
IN_BUCKET->maximum_ji_accummulation = MAXIMUM_IN_JI_ACCUMULATION;
IN_BUCKET->ji_accummulation = 0;
IN_BUCKET->ji = calloc(MAXIMUM_IN_JI_ACCUMULATION + 1, sizeof(struct job_instance*));
/* suspended bucket */
SUS_BUCKET = calloc(1, sizeof(struct Bucket));
/* initalize bucket values */
SUS_BUCKET->maximum_ji_accummulation = MAXIMUM_SUS_JI_ACCUMULATION;
SUS_BUCKET->ji_accummulation = 0;
SUS_BUCKET->ji = calloc(MAXIMUM_SUS_JI_ACCUMULATION + 1, sizeof(struct job_instance *));
/* allocate records */
RECORDS = calloc(MAXIMUM_RECORDS, sizeof(struct job_instance_record *));
RECORDS_COUNT = 0;
mvprintw(0, WINDOW_WIDTH / 4, "OSIIA1 v%s - https://mmu.ac.ke", VERSION);
/* allocate memory for history */
HISTORY_COMMANDS = calloc(HISTORY_MAX, sizeof(char *));
if (!HISTORY_COMMANDS)
{
perror("calloc");
return 1;
}
/* initialize history */
OSIIA1_read_history(HISTORY_FILE); /* read first 1/3 and store them into memory */
OSIIA1_stifle_history(HISTORY_MAX);
/* return value */
int retval = 0;
/* initialize regex */
RET = regcomp(®EX,_OSIIA1_PATTERN,REG_EXTENDED | REG_ICASE);
if (RET) {
char errbuff[1024];
regerror(RET,®EX,errbuff,sizeof(errbuff));
fprintf(stderr,"regcomp failed: %s\n",errbuff);
}
#if defined(TEST_THIS_CODE)
retval = testing_program(argc,argv);
#else
retval = main_program(argc, argv);
#endif // TEST_THIS_CODE
/* the write history must exists */
OSIIA1_write_history(HISTORY_FILE);
// OSIIA1_print_horirontal_line(NULL, " ", 1);
// OSIIA1_print_horirontal_line(NULL, "-", 1);
printf("This program ran for %zu seconds.\n", time(NULL) - STARTING_TIME);
// OSIIA1_print_horirontal_line(NULL, "~", 1);
// OSIIA1_print_horirontal_line(NULL, " ", 1);
printf("Goodbye. Hope well see you next time.\n");
// OSIIA1_print_horirontal_line(NULL, " ", 2);
if (IN_BUCKET)
free_bucket(IN_BUCKET);
/* --- */
if (SUS_BUCKET)
free_bucket(SUS_BUCKET);
/* --- */
if (CURRENT_JOB)
free_job_instance(CURRENT_JOB);
free_records();
free_history();
delwin(HANDLE_USER_INPUT_WIN);
delwin(CPU_EXEC_LOG_WIN);
delwin(GRANTT_CHART_DISPLAY_WIN);
endwin();
return retval;
}