-
Notifications
You must be signed in to change notification settings - Fork 1
/
all.c
100 lines (78 loc) · 2.08 KB
/
all.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
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include "allkernel.h"
#define ALL_HELP "help"
#define ALL_BUILD "build"
#define ALL_OUTFILE "main.vms"
enum {
ERR_NONE = 0x00,
ERR_ARGLEN = 0x01,
ERR_COMMAND = 0x02,
ERR_INOPEN = 0x03,
ERR_OUTOPEN = 0x04,
ERR_COMPILE = 0x05,
};
static const char *errors[] = {
[ERR_NONE] = "",
[ERR_ARGLEN] = "len argc < 3",
[ERR_COMMAND] = "unknown command",
[ERR_INOPEN] = "open input file",
[ERR_OUTOPEN] = "open output file",
[ERR_COMPILE] = "compile code",
};
static int file_build(const char *outputf, const char *inputf);
int main(int argc, char const *argv[]) {
const char *outfile;
int retcode;
int is_build;
outfile = ALL_OUTFILE;
retcode = ERR_COMMAND;
// all help
if (argc == 2 && strcmp(argv[1], ALL_HELP) == 0) {
printf("help: \n\t$ all build file [-o outfile]\n");
return ERR_NONE;
}
// all | all undefined
if (argc < 3) {
fprintf(stderr, "error: %s\n", errors[ERR_ARGLEN]);
return ERR_ARGLEN;
}
is_build = strcmp(argv[1], ALL_BUILD) == 0;
// all undefined x
if (!is_build) {
fprintf(stderr, "error: %s\n", errors[ERR_COMMAND]);
return ERR_COMMAND;
}
// all build file [-o outfile]
if (is_build) {
if (argc == 5 && strcmp(argv[3], "-o") == 0) {
outfile = argv[4];
}
retcode = file_build(outfile, argv[2]);
if (retcode != ERR_NONE) {
fprintf(stderr, "error: %s\n", errors[retcode]);
}
}
return retcode;
}
static int file_build(const char *outputf, const char *inputf) {
FILE *output, *input;
int retcode;
input = fopen(inputf, "r");
if (input == NULL) {
return ERR_INOPEN;
}
output = fopen(outputf, "wb");
if (output == NULL) {
fclose(input);
return ERR_OUTOPEN;
}
retcode = all_compile(output, input);
fclose(input);
fclose(output);
if (retcode != ERR_NONE) {
return ERR_COMPILE;
}
return ERR_NONE;
}