-
Notifications
You must be signed in to change notification settings - Fork 1
/
main.c
164 lines (152 loc) · 3.62 KB
/
main.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
/* CITS2002 Project 2018
* Names: Bruce How, Vincent Tian
* Student numbers: 22242664, 22262122
*/
#include <stdio.h>
#include <stdlib.h>
#include <getopt.h>
#include <stdbool.h>
#include <string.h>
#include "main.h"
#include "process.h"
#include "variables.h"
#include "targets.h"
#include "append.h"
#include "readfile.h"
#include "bake.h"
#include "expandvar.h"
#define OPTLIST "C:f:inps"
// Argument flags
bool iflag = false;
bool nflag = false;
bool sflag = false;
/**
* Prints all variables, targets and actions to the user. The function iterates
* through each LinkedList to print the relevant info. Uses bolded and tab
* indentation in the output for clear output.
*/
void printInfo() {
// Variables
variable *varItem = variableList;
printf("\n\033[1m\033[37mVARIABLES\033[0m\n");
while(varItem != NULL) {
printf("\t%s = %s\n", varItem->variable, varItem->value);
varItem = varItem->next;
}
// Targets
printf("\033[1m\033[37mTARGETS\033[0m\n");
target *targetItem = targetList;
while(targetItem != NULL) {
printf("\t%s : ", targetItem->target);
for(int i = 0; i < targetItem->numDep; i++) {
printf("%s ", targetItem->dependencies[i]);
}
targetItem = targetItem->next;
printf("\n");
}
// Actions
printf("\033[1m\033[37mACTIONS\033[0m");
target *actionItem = targetList;
while(actionItem != NULL) {
printf("\n\t%s\n", actionItem->target);
for(int i = 0; i < actionItem->numAct; i++) {
printf("\t%i. %s\n", i+1, actionItem->actions[i]);
}
actionItem = actionItem->next;
}
printf("\n");
}
/**
* Main function to be called when ./bake is used. Handles command line
* arguments using getopt() and prints appropiate error messages where needed
*/
int main(int argc, char *argv[]) {
int opt;
bool pflag = false;
char *filename = NULL;
char *dirpath = NULL;
opterr = 0; // Use custom error printing
// Argument handler
while((opt = getopt(argc, argv, OPTLIST)) != -1) {
switch(opt) {
case 'i':
iflag = !iflag;
break;
case 'n':
nflag = !nflag;
break;
case 'p':
pflag = !pflag;
break;
case 's':
sflag = !sflag;
break;
case 'C':
dirpath = strdup(optarg);
break;
case 'f':
filename = strdup(optarg);
break;
case '?':
if(optopt == 'C') {
fprintf(stderr, "Usage: %s [-C] [dirname]\n", argv[0]);
exit(EXIT_FAILURE);
} else if(optopt == 'f') {
fprintf(stderr, "Usage: %s [-f] [filename]\n", argv[0]);
exit(EXIT_FAILURE);
} else {
fprintf(stderr, "Illegal argument -%c\nUsage: %s [-Cfinps] [file..]\n", optopt, argv[0]);
exit(EXIT_FAILURE);
}
}
}
// Check for any targetname options
char *targetToBuild = NULL;
argc -= optind;
argv += optind;
if(argc > 0) {
targetToBuild = *argv;
}
// Check argument values
if(dirpath != NULL) {
int result = chdir(dirpath);
if(result != 0) {
fprintf(stderr, "%s: No such directory\n", dirpath);
exit(EXIT_FAILURE);
}
}
if(filename == NULL) {
filename = "bakefile";
}
FILE *fp = fopen(filename, "r");
if(fp == NULL) {
if(dirpath == NULL) {
perror(filename);
} else {
fprintf(stderr, "%s%s: No such file\n", dirpath, filename);
}
exit(EXIT_FAILURE);
}
// Iterate through the file
while(!feof(fp)) {
// Reads extended '\' lines
char *extend = readFile(fp);
// Process the line if valid
if(extend && extend[0] != '\0') {
char *line = expandVariables(extend);
if(currentTarget != NULL && *line == '\t') {
processActionDef(line);
} else {
currentTarget = NULL;
readLine(line);
}
}
}
if(pflag) {
printInfo();
} else {
bake(targetToBuild);
}
fclose(fp);
exit(EXIT_SUCCESS);
}