This repository has been archived by the owner on Apr 16, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathzasm.cpp
89 lines (78 loc) · 2.07 KB
/
zasm.cpp
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
/* Copyright (C) 2017-2019 cmj. All right reserved. */
#include <iostream>
#include <fstream>
#include <string>
#include <vector>
#include <unistd.h>
#include <getopt.h>
#include <sys/stat.h>
#include <sys/types.h>
#include "zasm.h"
int __verbose__ = 0;
void help(void) {
fprintf(stderr, "ZASM (v%d.%d) - Zerg assembler\n", ZASM_VER_MAJOR, ZASM_VER_MINOR);
fprintf(stderr, "\n");
fprintf(stderr, "Usage - zasm [option] src\n");
fprintf(stderr, "\n");
fprintf(stderr, " -v, --verbose Show the verbose message\n");
fprintf(stderr, " -f, --format Format of the output file \n");
fprintf(stderr, " -o, --output <file> Output file to <file>\n");
exit(-1);
}
int main(int argc, char *argv[]) {
int optIdx = -1;
char ch, opts[] = "hvf:o:";
struct option options[] = {
{"help", no_argument, 0, 'h'},
{"verbose", optional_argument, 0, 'v'},
{"format", required_argument, 0, 'f'},
{"output", required_argument, 0, 'o'},
{NULL, 0, 0, 0}
};
std::string output = ZASM_DEFAULT_OUTPUT, fmt = "";
while (-1 != (ch = getopt_long(argc, argv, opts, options, &optIdx))) {
switch(ch) {
case 'h':
help();
break;
case 'v':
__verbose__++;
break;
case 'f':
fmt = optarg;
break;
case 'o':
output = optarg;
break;
}
}
argc -= optind;
argv += optind;
if ( argc != 1 ) help();
Zasm *zasm = new Zasm(fmt);
try {
zasm->assemble(argv[0]);
std::fstream file(output, std::ios::out | std::ios::trunc);
chmod(output.c_str(), 0755);
file << *zasm;
} catch (Exception &e) {
switch ( e.code() ) {
case ERR_IO_ERROR:
_D(LOG_CRIT, "file operation failure on L#%zu - %s", zasm->lineno(), e.what());
break;
case ERR_SYNTAX_ERROR:
_D(LOG_CRIT, "syntax error on L#%zu - %s", zasm->lineno(), e.what());
break;
case ERR_INVALID_TOKEN:
_D(LOG_CRIT, "invalid token on L#%zu - %s", zasm->lineno(), e.what());
break;
default:
_D(LOG_CRIT, "unknown exception #%d on L#%zu - %s", e.code(), zasm->lineno(), e.what());
break;
}
return -1;
}
delete zasm;
return 0;
}
/* vim set: tabstop=4 */