new: add dump12 utility
This commit is contained in:
@ -38,6 +38,7 @@ typedef double f64;
|
|||||||
// # LOGGING FACILITES
|
// # LOGGING FACILITES
|
||||||
|
|
||||||
[[gnu::format(printf, 1, 2)]] void log_debug(char const *format, ...);
|
[[gnu::format(printf, 1, 2)]] void log_debug(char const *format, ...);
|
||||||
|
[[gnu::format(printf, 1, 2)]] void log_info(char const *format, ...);
|
||||||
[[gnu::format(printf, 1, 2)]] void log_warn(char const *format, ...);
|
[[gnu::format(printf, 1, 2)]] void log_warn(char const *format, ...);
|
||||||
[[gnu::format(printf, 1, 2)]] void log_err(char const *format, ...);
|
[[gnu::format(printf, 1, 2)]] void log_err(char const *format, ...);
|
||||||
[[gnu::format(printf, 1, 2)]] void log_err_errno(char const *format, ...);
|
[[gnu::format(printf, 1, 2)]] void log_err_errno(char const *format, ...);
|
||||||
|
|||||||
85
src/dump12.c
Normal file
85
src/dump12.c
Normal file
@ -0,0 +1,85 @@
|
|||||||
|
#include <common.h>
|
||||||
|
#include <fcntl.h>
|
||||||
|
#include <unistd.h>
|
||||||
|
|
||||||
|
void dump12_usage() { log_info("Usage: dump12 [-b|-t] [source] [dest]"); }
|
||||||
|
|
||||||
|
i32 dump12_main(i32 argc, char *argv[]) {
|
||||||
|
bool to_bin = true;
|
||||||
|
char *inpath = nullptr;
|
||||||
|
char *outpath = nullptr;
|
||||||
|
for (i32 i = 1; i < argc; ++i) {
|
||||||
|
char *arg = argv[i];
|
||||||
|
if (arg[0] == '-' && arg[1] == 'b' && arg[2] == '\0') {
|
||||||
|
to_bin = true;
|
||||||
|
} else if (arg[0] == '-' && arg[1] == 't' && arg[2] == '\0') {
|
||||||
|
to_bin = false;
|
||||||
|
} else if (arg[0] == '-' && arg[1] == 'h' && arg[2] == '\0') {
|
||||||
|
dump12_usage();
|
||||||
|
return 0;
|
||||||
|
} else if (inpath == nullptr) {
|
||||||
|
inpath = arg;
|
||||||
|
} else if (outpath == nullptr) {
|
||||||
|
outpath = arg;
|
||||||
|
} else {
|
||||||
|
log_err("Too many arguments");
|
||||||
|
return 1;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
int infd = (inpath == nullptr) ? 0 : open(inpath, O_RDONLY);
|
||||||
|
if (infd < 0) {
|
||||||
|
log_err_errno("Couldn't open `%s`", inpath);
|
||||||
|
return 1;
|
||||||
|
}
|
||||||
|
int outfd =
|
||||||
|
(inpath == nullptr) ? 1 : open(outpath, O_CREAT | O_TRUNC | O_WRONLY);
|
||||||
|
if (outfd < 0) {
|
||||||
|
log_err_errno("Couldn't open `%s`", outpath);
|
||||||
|
return 1;
|
||||||
|
}
|
||||||
|
if (to_bin) {
|
||||||
|
u12 byte12 = 0;
|
||||||
|
u8 count = 0;
|
||||||
|
u8 c;
|
||||||
|
while (read(infd, &c, 1) > 0) {
|
||||||
|
switch (c) {
|
||||||
|
case '0':
|
||||||
|
case '1':
|
||||||
|
case '2':
|
||||||
|
case '3':
|
||||||
|
case '4':
|
||||||
|
case '5':
|
||||||
|
case '6':
|
||||||
|
case '7':
|
||||||
|
byte12 = (byte12 << 3) | (c - '0');
|
||||||
|
count++;
|
||||||
|
break;
|
||||||
|
default:
|
||||||
|
}
|
||||||
|
if (count == 4) {
|
||||||
|
u8 bs[2] = {byte12 & 0xFF, (byte12 >> 8) & 0xFF};
|
||||||
|
write(outfd, bs, 2);
|
||||||
|
count = 0;
|
||||||
|
byte12 = 0;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
usz bytecount = 0;
|
||||||
|
u8 readbuf[2];
|
||||||
|
while (read(infd, readbuf, 2) > 0) {
|
||||||
|
u12 byte12 = readbuf[0] | (readbuf[1] << 8);
|
||||||
|
bytecount ++;
|
||||||
|
u8 outbuf[5] = {
|
||||||
|
((byte12 >> 9) & 7) + '0',
|
||||||
|
((byte12 >> 6) & 7) + '0',
|
||||||
|
((byte12 >> 3) & 7) + '0',
|
||||||
|
(byte12 & 7) + '0',
|
||||||
|
(bytecount % 8) ? ' ': '\n',
|
||||||
|
};
|
||||||
|
write(outfd, outbuf, 5);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
close(infd);
|
||||||
|
close(outfd);
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
@ -14,7 +14,7 @@ bool logcolours = true;
|
|||||||
void log_debug([[maybe_unused]] const char* format, ...) {}
|
void log_debug([[maybe_unused]] const char* format, ...) {}
|
||||||
#else
|
#else
|
||||||
void log_debug(const char* format, ...) {
|
void log_debug(const char* format, ...) {
|
||||||
fprintf(logfile, "%s[DEBUG]%s ", logcolours ? "\033[1;44;97m" : "", logcolours ? "\033[0" : "");
|
fprintf(logfile, "%s[DEBUG]%s ", logcolours ? "\033[1;44;97m" : "", logcolours ? "\033[0m" : "");
|
||||||
va_list va;
|
va_list va;
|
||||||
va_start(va);
|
va_start(va);
|
||||||
vfprintf(logfile, format, va);
|
vfprintf(logfile, format, va);
|
||||||
@ -22,6 +22,14 @@ void log_debug(const char* format, ...) {
|
|||||||
fprintf(logfile, "\n");
|
fprintf(logfile, "\n");
|
||||||
}
|
}
|
||||||
#endif
|
#endif
|
||||||
|
void log_info(const char* format, ...) {
|
||||||
|
fprintf(logfile, "%s[WARN]%s ", logcolours ? "\033[1;43;97m": "", logcolours ? "\033[0m" : "");
|
||||||
|
va_list va;
|
||||||
|
va_start(va);
|
||||||
|
vfprintf(logfile, format, va);
|
||||||
|
va_end(va);
|
||||||
|
fprintf(logfile, "\n");
|
||||||
|
}
|
||||||
void log_warn(const char* format, ...) {
|
void log_warn(const char* format, ...) {
|
||||||
fprintf(logfile, "%s[WARN]%s ", logcolours ? "\033[1;43;97m": "", logcolours ? "\033[0m" : "");
|
fprintf(logfile, "%s[WARN]%s ", logcolours ? "\033[1;43;97m": "", logcolours ? "\033[0m" : "");
|
||||||
va_list va;
|
va_list va;
|
||||||
|
|||||||
@ -7,9 +7,10 @@ enum subprorgram {
|
|||||||
};
|
};
|
||||||
|
|
||||||
const char *available = (R"m(
|
const char *available = (R"m(
|
||||||
asm)m");
|
asm, dump12)m");
|
||||||
|
|
||||||
i32 asm_main(i32 argc, char *argv[]);
|
i32 asm_main(i32 argc, char *argv[]);
|
||||||
|
i32 dump12_main(i32 argc, char *argv[]);
|
||||||
i32 main(i32 argc, char *argv[]) {
|
i32 main(i32 argc, char *argv[]) {
|
||||||
if (argc < 2) {
|
if (argc < 2) {
|
||||||
log_err("No subprogram called\nAvailable subprograms:%s", available);
|
log_err("No subprogram called\nAvailable subprograms:%s", available);
|
||||||
@ -17,6 +18,8 @@ i32 main(i32 argc, char *argv[]) {
|
|||||||
}
|
}
|
||||||
if (!strcmp(argv[1], "asm")) {
|
if (!strcmp(argv[1], "asm")) {
|
||||||
return asm_main(argc - 1, argv + 1);
|
return asm_main(argc - 1, argv + 1);
|
||||||
|
} else if (!strcmp(argv[1], "dump12")) {
|
||||||
|
return dump12_main(argc - 1, argv + 1);
|
||||||
} else {
|
} else {
|
||||||
log_err("Unknown subprogram `%s`\nAvailable subpograms:%s", argv[1], available);
|
log_err("Unknown subprogram `%s`\nAvailable subpograms:%s", argv[1], available);
|
||||||
return 1;
|
return 1;
|
||||||
|
|||||||
Reference in New Issue
Block a user