forked from Audakel/cs-345
-
Notifications
You must be signed in to change notification settings - Fork 0
/
os345p1.c
executable file
·433 lines (359 loc) · 15.4 KB
/
os345p1.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
// os345p1.c - Command Line Processor
// ***********************************************************************
// ** DISCLAMER ** DISCLAMER ** DISCLAMER ** DISCLAMER ** DISCLAMER **
// ** **
// ** The code given here is the basis for the CS345 projects. **
// ** It comes "as is" and "unwarranted." As such, when you use part **
// ** or all of the code, it becomes "yours" and you are responsible to **
// ** understand any algorithm or method presented. Likewise, any **
// ** errors or problems become your responsibility to fix. **
// ** **
// ** NOTES: **
// ** -Comments beginning with "// ??" may require some implementation. **
// ** -Tab stops are set at every 3 spaces. **
// ** -The function API's in "OS345.h" should not be altered. **
// ** **
// ** DISCLAMER ** DISCLAMER ** DISCLAMER ** DISCLAMER ** DISCLAMER **
// ***********************************************************************
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <ctype.h>
#include <setjmp.h>
#include <assert.h>
#include <regex.h>
#include "os345.h"
#include "os345signals.h"
// The 'reset_context' comes from 'main' in os345.c. Proper shut-down
// procedure is to long jump to the 'reset_context' passing in the
// power down code from 'os345.h' that indicates the desired behavior.
extern jmp_buf reset_context;
extern TCB tcb[];
// -----
#define NUM_COMMANDS 52
typedef struct // command struct
{
char* command;
char* shortcut;
int (*func)(int, char**);
char* description;
} Command;
// ***********************************************************************
// project 1 variables
//
extern long swapCount; // number of scheduler cycles
extern char inBuffer[]; // character input buffer
extern Semaphore* inBufferReady; // input buffer ready semaphore
extern bool diskMounted; // disk has been mounted
extern char dirPath[]; // directory path
Command** commands; // shell commands
// ***********************************************************************
// project 1 prototypes
Command** P1_init(void);
Command* newCommand(char*, char*, int (*func)(int, char**), char*);
// ***********************************************************************
// myShell - command line interpreter
//
// Project 1 - implement a Shell (CLI) that:
//
// 1. Prompts the user for a command line.
// 2. WAIT's until a user line has been entered.
// 3. Parses the global char array inBuffer.
// 4. Creates new argc, argv variables using malloc.
// 5. Searches a command list for valid OS commands.
// 6. If found, perform a function variable call passing argc/argv variables.
// 7. Supports background execution of non-intrinsic commands.
//
int P1_shellTask(int argc, char* argv[])
{
int i, found, newArgc, memI; // # of arguments
char** newArgv; // pointers to arguments
const char amp[2] = "&";
// initialize shell commands
commands = P1_init(); // init shell commands
while (1)
{
bool backgroundTask = FALSE;
// output prompt
if (diskMounted) printf("\n%s>>", dirPath);
else printf("\n%ld>>", swapCount);
SEM_WAIT(inBufferReady); // wait for input buffer semaphore
if (!inBuffer[0]) continue; // ignore blank lines
// printf("%s", inBuffer);
SWAP // do context switch
{
// ?? >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>
// ?? parse command line into argc, argv[] variables
// ?? must use malloc for argv storage!
static char *sp, *myArgv[MAX_ARGS];
// init arguments
newArgc = 0;
sp = inBuffer; // point to input string
for (i=1; i<MAX_ARGS; i++)
myArgv[i] = 0;
// parse input string
int start = 0, end = 0, len = (int)strlen(sp);
while (start < len) {
while (start < len && isspace(sp[start])) {
start++;
}
while (end < len + 1
&& (end <= start
|| (sp[start] == '"' && (isspace(sp[end]) == 0 || sp[end - 1] != '"'))
|| (sp[start] != '"' && isspace(sp[end]) == 0))
) {
end++;
}
if (start == end) {
break;
}
myArgv[newArgc] = (char *) malloc(sizeof(char) * (end - start + 1));
int nA = 0;
while (start < end) {
myArgv[newArgc][nA++] = sp[start++];
}
myArgv[newArgc][nA] = '\0';
start++;
newArgc++;
}
newArgv = (char **) malloc(sizeof(char*) * newArgc); // malloc returns a void * to the allocated memory or on failure returns a null pointer
if (newArgv == NULL) { exit(1); } // if null malloc failed exit the program
for (memI = 0; memI < newArgc; memI++) {
if (myArgv[memI][0] == 0) {
newArgc--;
continue;
}
if (memI == (newArgc - 1) && strcmp(myArgv[memI],amp) == 0 ) {
// last argument is an &
backgroundTask = TRUE;
newArgc--;
break;
}
newArgv[memI] = myArgv[memI];
if (newArgv[memI][0] == '"' && newArgv[memI][strlen(newArgv[memI]) - 1] == '"') {
// if the arg is a string
// in this case we do nothing to transform the string
continue;
}
//convert the string to lowercase to ease comparisons
for(int sI = 0; newArgv[memI][sI]; sI++) {
newArgv[memI][sI] = tolower(newArgv[memI][sI]);
}
}
} // ?? >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>
SWAP // do context switch
if (DEBUG_PARSER) {
printf("\nArgc = %d\nparsed arguments:", newArgc);
for (memI = 0; memI < newArgc; memI++) {
printf("\n\t|%s|", newArgv[memI]);
}
printf("\nTask is scheduled for background: %s\n", backgroundTask ? "true" : "false");
}
// look for command
for (found = i = 0; i < NUM_COMMANDS; i++)
{
if (!strcmp(newArgv[0], commands[i]->command) ||
!strcmp(newArgv[0], commands[i]->shortcut))
{
// command found
if (backgroundTask) {
int tid = createTask(
&(*commands[i]->description),
&(*commands[i]->func),
tcb[0].priority,
newArgc,
newArgv
);
if (tid == -1) {
printf("\nTCB is full");
}
} else {
int retValue = (*commands[i]->func)(newArgc, newArgv);
if (retValue) printf("\nCommand Error %d", retValue);
}
found = TRUE;
break;
}
}
SWAP // do context switch
if (!found) printf("\nInvalid command!");
// ?? free up any malloc'd argv parameters
for (i=0; i<INBUF_SIZE; i++) inBuffer[i] = 0;
for (newArgc--; newArgc > -1; newArgc--) {
free(newArgv[newArgc]);
}
SWAP // do context switch
}
return 0; // terminate task
} // end P1_shellTask
// ***********************************************************************
// ***********************************************************************
// P1 Project
//
int P1_project1(int argc, char** argv)
{
SWAP // do context switch
return 0;
} // end P1_project1
// ***********************************************************************
// ***********************************************************************
// quit command
//
int P1_quit(int argc, char** argv)
{
int i;
// free P1 commands
for (i = 0; i < NUM_COMMANDS; i++)
{
free(commands[i]->command);
free(commands[i]->shortcut);
free(commands[i]->description);
}
free(commands);
// powerdown OS345
longjmp(reset_context, POWER_DOWN_QUIT);
return 0;
} // end P1_quit
// **************************************************************************
// **************************************************************************
// lc3 command
//
int P1_lc3(int argc, char** argv)
{
strcpy (argv[0], "0");
return lc3Task(argc, argv);
} // end P1_lc3
// **************************************************************************
// **************************************************************************
// add command
//
int P1_add(int argc, char** argv)
{
double result = 0;
for (int i = 1; i < argc; i++) {
double val = atof(argv[i]);
if (val) {
result += val;
} else {
result += strtol(argv[i], NULL, 16);
}
}
printf("\nResult: %f", result);
return 0;
} // end add
// **************************************************************************
// **************************************************************************
// args command
//
int P1_args(int argc, char** argv)
{
printf("\nArgc = %d\nparsed arguments:", argc);
for (int memI = 0; memI < argc; memI++) {
printf("\n\t|%s|", argv[memI]);
}
return 0;
} // end args
// ***********************************************************************
// ***********************************************************************
// help command
//
int P1_help(int argc, char** argv)
{
int i;
// list commands
for (i = 0; i < NUM_COMMANDS; i++)
{
SWAP // do context switch
if (strstr(commands[i]->description, ":")) printf("\n");
printf("\n%4s: %s", commands[i]->shortcut, commands[i]->description);
}
return 0;
} // end P1_help
// ***********************************************************************
// ***********************************************************************
// initialize shell commands
//
Command* newCommand(char* command, char* shortcut, int (*func)(int, char**), char* description)
{
Command* cmd = (Command*)malloc(sizeof(Command));
// get long command
cmd->command = (char*)malloc(strlen(command) + 1);
strcpy(cmd->command, command);
// get shortcut command
cmd->shortcut = (char*)malloc(strlen(shortcut) + 1);
strcpy(cmd->shortcut, shortcut);
// get function pointer
cmd->func = func;
// get description
cmd->description = (char*)malloc(strlen(description) + 1);
strcpy(cmd->description, description);
return cmd;
} // end newCommand
Command** P1_init()
{
int i = 0;
Command** commands = (Command**)malloc(sizeof(Command*) * NUM_COMMANDS);
// system
commands[i++] = newCommand("quit", "q", P1_quit, "Quit");
commands[i++] = newCommand("kill", "kt", P2_killTask, "Kill task");
commands[i++] = newCommand("reset", "rs", P2_reset, "Reset system");
// P1: Shell
commands[i++] = newCommand("project1", "p1", P1_project1, "P1: Shell");
commands[i++] = newCommand("help", "he", P1_help, "OS345 Help");
commands[i++] = newCommand("add", "add", P1_add, "Add - adds follow args (handles int and hex)");
commands[i++] = newCommand("args", "args", P1_args, "Prints out args given in command line");
commands[i++] = newCommand("lc3", "lc3", P1_lc3, "Execute LC3 program");
// P2: Tasking
commands[i++] = newCommand("project2", "p2", P2_project2, "P2: Tasking");
commands[i++] = newCommand("semaphores", "sem", P2_listSems, "List semaphores");
commands[i++] = newCommand("tasks", "lt", P2_listTasks, "List tasks");
commands[i++] = newCommand("signal1", "s1", P2_signal1, "Signal sem1 semaphore");
commands[i++] = newCommand("signal2", "s2", P2_signal2, "Signal sem2 semaphore");
// P3: Jurassic Park
commands[i++] = newCommand("project3", "p3", P3_project3, "P3: Jurassic Park");
commands[i++] = newCommand("deltaclock", "dc", P3_dc, "List deltaclock entries");
commands[i++] = newCommand("testDeltaClock", "tdc", P3_tdc, "Test deltaclock implementation");
// P4: Virtual Memory
commands[i++] = newCommand("project4", "p4", P4_project4, "P4: Virtual Memory");
commands[i++] = newCommand("frametable", "dft", P4_dumpFrameTable, "Dump bit frame table");
commands[i++] = newCommand("initmemory", "im", P4_initMemory, "Initialize virtual memory");
commands[i++] = newCommand("touch", "vma", P4_vmaccess, "Access LC-3 memory location");
commands[i++] = newCommand("stats", "vms", P4_virtualMemStats, "Output virtual memory stats");
commands[i++] = newCommand("crawler", "cra", P4_crawler, "Execute crawler.hex");
commands[i++] = newCommand("memtest", "mem", P4_memtest, "Execute memtest.hex");
commands[i++] = newCommand("frame", "dfm", P4_dumpFrame, "Dump LC-3 memory frame");
commands[i++] = newCommand("memory", "dm", P4_dumpLC3Mem, "Dump LC-3 memory");
commands[i++] = newCommand("page", "dp", P4_dumpPageMemory, "Dump swap page");
commands[i++] = newCommand("virtual", "dvm", P4_dumpVirtualMem, "Dump virtual memory page");
commands[i++] = newCommand("root", "rpt", P4_rootPageTable, "Display root page table");
commands[i++] = newCommand("user", "upt", P4_userPageTable, "Display user page table");
// P5: Scheduling
commands[i++] = newCommand("project5", "p5", P5_project5, "P5: Scheduling");
// commands[i++] = newCommand("stress1", "t1", P5_stress1, "ATM stress test1");
// commands[i++] = newCommand("stress2", "t2", P5_stress2, "ATM stress test2");
// P6: FAT
commands[i++] = newCommand("project6", "p6", P6_project6, "P6: FAT");
commands[i++] = newCommand("change", "cd", P6_cd, "Change directory");
commands[i++] = newCommand("copy", "cf", P6_copy, "Copy file");
commands[i++] = newCommand("define", "df", P6_define, "Define file");
commands[i++] = newCommand("delete", "dl", P6_del, "Delete file");
commands[i++] = newCommand("directory", "dir", P6_dir, "List current directory");
commands[i++] = newCommand("mount", "md", P6_mount, "Mount disk");
commands[i++] = newCommand("mkdir", "mk", P6_mkdir, "Create directory");
commands[i++] = newCommand("run", "run", P6_run, "Execute LC-3 program");
commands[i++] = newCommand("space", "sp", P6_space, "Space on disk");
commands[i++] = newCommand("type", "ty", P6_type, "Type file");
commands[i++] = newCommand("unmount", "um", P6_unmount, "Unmount disk");
commands[i++] = newCommand("fat", "ft", P6_dfat, "Display fat table");
commands[i++] = newCommand("fileslots", "fs", P6_fileSlots, "Display current open slots");
commands[i++] = newCommand("sector", "ds", P6_dumpSector, "Display disk sector");
commands[i++] = newCommand("chkdsk", "ck", P6_chkdsk, "Check disk");
commands[i++] = newCommand("final", "ft", P6_finalTest, "Execute file test");
commands[i++] = newCommand("open", "op", P6_open, "Open file test");
commands[i++] = newCommand("read", "rd", P6_read, "Read file test");
commands[i++] = newCommand("write", "wr", P6_write, "Write file test");
commands[i++] = newCommand("seek", "sk", P6_seek, "Seek file test");
commands[i++] = newCommand("close", "cl", P6_close, "Close file test");
assert(i == NUM_COMMANDS);
return commands;
} // end P1_init