-
Notifications
You must be signed in to change notification settings - Fork 0
/
fred.c
101 lines (87 loc) · 2.45 KB
/
fred.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
///file:fred.c
///description:main program file for the fred interpreter
/// reads a symbol file and program file if provided and
/// processes statements
///author: avv8047 : Azhur Viano
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <getopt.h>
#include "symbolTable.h"
#include "processor.h"
///Print the usage message for the main program
void printUsage(){
fprintf(stderr, "Usage: fred [ -s symbol-table-file ]"
"[ -f fred-program-file ]");
return;
}
//Set up symbol table, reading symbols from the symbol file if provided,
// and then process statements from standard input or
// a program file if provided
int main(int argc, char** argv){
//used to store options from getop
int c;
//table to use while processing
SymbolTable* table = CreateTable();
//stream for input statements
FILE* input = NULL;
//stream for symbols from a file
FILE* symbolInput = NULL;
//Check for the correct number of arguments
if((argc - 1) % 2 != 0 || argc > 5){
fprintf(stderr, "Wrong number of arguments\n");
printUsage();
return EXIT_FAILURE;
}
while((c = getopt(argc, argv, "f:s:")) != -1){
switch(c){
//program file
case 'f':
//Check if a program file was already provided
if(input){
fprintf(stderr, "Duplicate argument for program file: %s\n", optarg);
return EXIT_FAILURE;
}
input = fopen(optarg, "r");
if(!input){
fprintf(stderr, "Error opening program file %s\n", optarg);
return EXIT_FAILURE;
}
break;
//symbol file
case 's':
//Check if a symbol file was already provided
if(symbolInput){
fprintf(stderr, "Duplicate argument for symbol file: %s\n", optarg);
return EXIT_FAILURE;
}
symbolInput = fopen(optarg, "r");
if(!symbolInput){
fprintf(stderr, "Error in opening symbol file %s\n", optarg);
return EXIT_FAILURE;
}
//read symbols from the file into the table
processSymbolFile(table, symbolInput);
fclose(symbolInput);
symbolInput = NULL;
break;
default:
printUsage();
return EXIT_FAILURE;
}
}
//Read from stdin if no program file was provided
if(!input){
input = stdin;
}
//process program statements until EOF is reached
processStatements(table, input);
//print table contents
dumpTable(table);
DestroyTable(table);
//if a file was opened for reading statements from, close it
if(input != stdin){
fclose(input);
}
}