-
Notifications
You must be signed in to change notification settings - Fork 0
/
mmake.c
428 lines (384 loc) · 7.94 KB
/
mmake.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
/**
* @file mmake.c
* @author Vincent Johansson (dv14vjn@cs.umu.se)
* @brief
* @version 0.1
* @date 2022-10-19
*
* @copyright Copyright (c) 2022
*
*/
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include <unistd.h>
#include <time.h>
#include <stdbool.h>
#include <sys/wait.h>
#include <sys/stat.h>
#include <errno.h>
#include "parser.h"
#define MAX_LINE 1024
typedef struct start_args
{
int arg_b;
int arg_s;
int n_tar;
int c_tar;
int exitcode;
char *makefile;
char **target;
} start_args;
/* ---- Function declaration ---- */
void *init_struct(void);
void check_start_args(int argc, char *argv[], start_args *s);
makefile *choose_makefile(start_args *s);
void run_makefile(makefile *m, const char *target, start_args *s);
bool check_file(const char *current, const char *prereq, makefile *m);
void run_cmd(rule *tar_rule, start_args *s);
void *safe_calloc(size_t size);
void realloc_buff(char ***buffer, start_args *s);
int main(int argc, char *argv[])
{
/* Initialize start_args struct */
start_args *sa = init_struct();
/* Check start arguments */
check_start_args(argc, argv, sa);
/* If flag -s is used, prohibit from printing messages */
if (sa->arg_s == 1)
{
fclose(stdout);
}
/* If no targets specified, set target to default target */
if (sa->c_tar == 0)
{
makefile *m = choose_makefile(sa);
run_makefile(m, makefile_default_target(m), sa);
}
else
{
int i = 0;
makefile *m = choose_makefile(sa);
while (sa->target[i] != NULL)
{
run_makefile(m, sa->target[i], sa);
i++;
}
}
exit(sa->exitcode);
}
/* ---- Functions ---- */
/**
* @brief Initialize start_args struct
*
* @return void* pointer to allocated memory
*/
void *init_struct(void)
{
/* Allocate memory for struct */
start_args *sa;
sa = safe_calloc(sizeof(start_args));
/* Initialize values */
sa->arg_b = 0;
sa->arg_s = 0;
sa->n_tar = 50;
sa->c_tar = 0;
sa->exitcode = 0;
sa->makefile = NULL;
/* Allocate memory for array where target names will be stored */
sa->target = safe_calloc(sizeof(char *) * sa->n_tar);
return sa;
}
/**
* @brief Check start arguments given by user with getopt
*
* @param argc argc
* @param argv argv
* @param s start_args struct
*/
void check_start_args(int argc, char *argv[], start_args *s)
{
int flag;
while ((flag = getopt(argc, argv, ":Bsf:")) != -1)
{
switch (flag)
{
case 'f':
s->makefile = optarg;
break;
case 'B':
s->arg_b = 1;
break;
case 's':
s->arg_s = 1;
break;
case '?':
fprintf(stderr,
"usage: ./mmake [-f MAKEFILE] [-B] [-s] [TARGET]\n");
exit(errno);
}
}
for (; optind < argc; optind++)
{
if (optind > s->n_tar)
{
realloc_buff(&s->target, s);
}
s->target[s->c_tar] = safe_calloc(sizeof(char) * MAX_LINE);
strcpy(s->target[s->c_tar], argv[optind]);
s->c_tar++;
}
}
/**
* @brief Choose makefile depending on userinput.
* Either 'mmakefile' as default or file specified by user.
*
* @param s start_args struct.
* @return m The parsed makefile.
*/
makefile *choose_makefile(start_args *s)
{
FILE *file;
makefile *m;
/*
* If no makefile has been specified through -f argument
* open default makefile (mmakefile), otherwise open
* makefile specified by user.
*/
if (s->makefile == NULL)
{
if ((file = fopen("mmakefile", "r")) == NULL)
{
perror("mmakefile:");
exit(errno);
}
if ((m = parse_makefile(file)) == NULL)
{
fprintf(stderr, "mmakefile: Could not parse makefile");
exit(EXIT_FAILURE);
}
fclose(file);
return m;
}
else
{
if ((file = fopen(s->makefile, "r")) == NULL)
{
fprintf(stderr, "%s:", s->makefile);
perror("");
exit(errno);
}
if ((m = parse_makefile(file)) == NULL)
{
fprintf(stderr, "%s: Could not parse makefile", s->makefile);
exit(EXIT_FAILURE);
}
fclose(file);
return m;
}
}
/**
* @brief Take parsed makefile and build from it.
*
* @param m the makefile
* @param target name of target
* @param s start_args struct
*/
void run_makefile(makefile *m, const char *target, start_args *s)
{
rule *tar_rule;
const char **tar_prereq;
/* Get rule for target, if no rule exist return */
if ((tar_rule = makefile_rule(m, target)) == NULL)
{
return;
}
/*
* Check prerequisites and recursively call
* run_makefile() to make them
*/
tar_prereq = rule_prereq(tar_rule);
int i = 0;
int j = 0;
if (tar_prereq != NULL)
{
while (tar_prereq[i] != NULL)
{
run_makefile(m, tar_prereq[i], s);
i++;
}
/* If option -B used, skip file check to force build */
if (s->arg_b == 1)
{
run_cmd(tar_rule, s);
}
else
{
while (tar_prereq[j] != NULL)
{
if (check_file(target, tar_prereq[j], m))
{
run_cmd(tar_rule, s);
}
j++;
}
}
}
else
{
run_cmd(tar_rule, s);
}
}
/**
* @brief Check if files exist or needs to be created, if files exist
* compare to see if prerequisite file was modified more recently than the
* target. If there is no rule to make prerequisite, give error and exit.
*
* @param current name of current target
* @param prereq name of prerequisite
* @param m the makefile
* @return true or false
*/
bool check_file(const char *current, const char *prereq, makefile *m)
{
/*
* Create structs for stat() func and variables for
* time value when files were edited
*/
struct stat stat_tar;
struct stat stat_pre;
time_t time_tar;
time_t time_pre;
/* Check if files exist, return true if a file needs to be created */
if (access(prereq, F_OK) < 0)
{
if (makefile_rule(m, prereq) == NULL)
{
fprintf(stderr, "mmake: No rule to make target '%s'\n", prereq);
exit(EXIT_FAILURE);
}
return true;
}
if (access(current, F_OK) < 0)
{
return true;
}
/* Collect information about files */
lstat(prereq, &stat_pre);
lstat(current, &stat_tar);
/* Save time for last modification of files in time variables */
time_tar = stat_tar.st_mtime;
time_pre = stat_pre.st_mtime;
/* Compare if prerequisite was modified after target */
if (difftime(time_pre, time_tar) < 0 || difftime(time_pre, time_tar) == 0)
{
return false;
}
else
{
return true;
}
}
/**
* @brief Execute the command for given rule
*
* @param tar_rule rule for target to be made
* @param s start_args struct
*/
void run_cmd(rule *tar_rule, start_args *s)
{
pid_t pid;
int i = 0;
int status;
/* Get command for the rule and print it */
char **exec_cmd = rule_cmd(tar_rule);
while (exec_cmd[i] != NULL)
{
if (exec_cmd[i + 1] == NULL)
{
printf("%s\n", exec_cmd[i]);
}
else
{
printf("%s ", exec_cmd[i]);
}
i++;
}
/* Fork to execute command */
fflush(stdout);
switch (pid = fork())
{
case -1:
/* If fork failes, print error and exit */
perror(strerror(errno));
exit(errno);
case 0: /* Child */
/* Execute given command, if execvp fail print error and exit */
if (execvp(exec_cmd[0], exec_cmd) < 0)
{
perror(strerror(errno));
exit(errno);
}
break;
default: /* Parent */
/* Wait for child process to exit and save exit code */
if (waitpid(pid, &status, 0) == -1)
{
perror(strerror(errno));
exit(errno);
}
if (WIFEXITED(status))
{
s->exitcode = WEXITSTATUS(status);
}
break;
}
}
/**
* @brief Safe usage of malloc() function
*
* @param size size of memory to be allocated
* @return void* pointer to allocated memory
*/
void *safe_calloc(size_t size)
{
void *mem_block;
if ((mem_block = calloc(1, size)) == NULL)
{
perror(strerror(errno));
exit(errno);
}
return mem_block;
}
/**
* @brief Reallocate **char buffer.
*
* @param buffer Pointer to **char buffer.
* @param s start_args struct.
*/
void realloc_buff(char ***buffer, start_args *s)
{
char **temp;
s->n_tar *= 2;
temp = realloc(*buffer, sizeof(char *) * s->n_tar);
if (temp == NULL)
{
perror("realloc()");
exit(errno);
}
*buffer = temp;
for (int i = 0; i < s->n_tar; i++)
{
char *temp2;
temp2 = realloc(*buffer[i], sizeof(char) * MAX_LINE);
if (temp2 == NULL)
{
perror("realloc()");
exit(errno);
}
else
{
*buffer[i] = temp2;
}
}
}