Fix bba to compile on windows (no unistd there)
This commit is contained in:
parent
c3859072d4
commit
8b60ed5fd1
@ -5,6 +5,7 @@ ENDIF(CMAKE_CROSSCOMPILING)
|
|||||||
|
|
||||||
IF(NOT CMAKE_CROSSCOMPILING)
|
IF(NOT CMAKE_CROSSCOMPILING)
|
||||||
ADD_EXECUTABLE(bbasm bba/main.cc)
|
ADD_EXECUTABLE(bbasm bba/main.cc)
|
||||||
|
target_link_libraries(bbasm LINK_PUBLIC ${Boost_LIBRARIES})
|
||||||
ENDIF(NOT CMAKE_CROSSCOMPILING)
|
ENDIF(NOT CMAKE_CROSSCOMPILING)
|
||||||
|
|
||||||
IF(NOT CMAKE_CROSSCOMPILING)
|
IF(NOT CMAKE_CROSSCOMPILING)
|
||||||
|
89
bba/main.cc
89
bba/main.cc
@ -1,12 +1,13 @@
|
|||||||
|
#include <assert.h>
|
||||||
|
#include <boost/program_options.hpp>
|
||||||
|
#include <iostream>
|
||||||
#include <map>
|
#include <map>
|
||||||
#include <vector>
|
#include <stdint.h>
|
||||||
#include <string>
|
|
||||||
#include <stdio.h>
|
#include <stdio.h>
|
||||||
#include <stdlib.h>
|
#include <stdlib.h>
|
||||||
#include <stdint.h>
|
|
||||||
#include <string.h>
|
#include <string.h>
|
||||||
#include <assert.h>
|
#include <string>
|
||||||
#include <unistd.h>
|
#include <vector>
|
||||||
|
|
||||||
enum TokenType : int8_t
|
enum TokenType : int8_t
|
||||||
{
|
{
|
||||||
@ -51,35 +52,50 @@ int main(int argc, char **argv)
|
|||||||
bool writeC = false;
|
bool writeC = false;
|
||||||
char buffer[512];
|
char buffer[512];
|
||||||
|
|
||||||
int opt;
|
namespace po = boost::program_options;
|
||||||
while ((opt = getopt(argc, argv, "vbc")) != -1)
|
po::positional_options_description pos;
|
||||||
{
|
po::options_description options("Allowed options");
|
||||||
switch (opt)
|
options.add_options()("v", "verbose output");
|
||||||
{
|
options.add_options()("b", "big endian");
|
||||||
case 'v':
|
options.add_options()("c", "write c strings");
|
||||||
|
options.add_options()("files", po::value<std::vector<std::string>>(), "file parameters");
|
||||||
|
pos.add("files", -1);
|
||||||
|
|
||||||
|
po::variables_map vm;
|
||||||
|
try {
|
||||||
|
po::parsed_options parsed = po::command_line_parser(argc, argv).options(options).positional(pos).run();
|
||||||
|
|
||||||
|
po::store(parsed, vm);
|
||||||
|
|
||||||
|
po::notify(vm);
|
||||||
|
} catch (std::exception &e) {
|
||||||
|
std::cout << e.what() << "\n";
|
||||||
|
return 1;
|
||||||
|
}
|
||||||
|
if (vm.count("v"))
|
||||||
verbose = true;
|
verbose = true;
|
||||||
break;
|
if (vm.count("b"))
|
||||||
case 'b':
|
|
||||||
bigEndian = true;
|
bigEndian = true;
|
||||||
break;
|
if (vm.count("c"))
|
||||||
case 'c':
|
|
||||||
writeC = true;
|
writeC = true;
|
||||||
break;
|
|
||||||
default:
|
if (vm.count("files") == 0) {
|
||||||
assert(0);
|
printf("File parameters are mandatory\n");
|
||||||
|
exit(-1);
|
||||||
}
|
}
|
||||||
|
std::vector<std::string> files = vm["files"].as<std::vector<std::string>>();
|
||||||
|
if (files.size() != 2) {
|
||||||
|
printf("Input and output parameters must be set\n");
|
||||||
|
exit(-1);
|
||||||
}
|
}
|
||||||
|
|
||||||
assert(optind+2 == argc);
|
FILE *fileIn = fopen(files.at(0).c_str(), "rt");
|
||||||
|
|
||||||
FILE *fileIn = fopen(argv[optind], "rt");
|
|
||||||
assert(fileIn != nullptr);
|
assert(fileIn != nullptr);
|
||||||
|
|
||||||
FILE *fileOut = fopen(argv[optind+1], writeC ? "wt" : "wb");
|
FILE *fileOut = fopen(files.at(1).c_str(), writeC ? "wt" : "wb");
|
||||||
assert(fileOut != nullptr);
|
assert(fileOut != nullptr);
|
||||||
|
|
||||||
while (fgets(buffer, 512, fileIn) != nullptr)
|
while (fgets(buffer, 512, fileIn) != nullptr) {
|
||||||
{
|
|
||||||
std::string cmd = strtok(buffer, " \t\r\n");
|
std::string cmd = strtok(buffer, " \t\r\n");
|
||||||
|
|
||||||
if (cmd == "pre") {
|
if (cmd == "pre") {
|
||||||
@ -178,8 +194,7 @@ int main(int argc, char **argv)
|
|||||||
int cursor = 0;
|
int cursor = 0;
|
||||||
for (auto &s : streams) {
|
for (auto &s : streams) {
|
||||||
for (int i = 0; i < int(s.tokenTypes.size()); i++) {
|
for (int i = 0; i < int(s.tokenTypes.size()); i++) {
|
||||||
switch (s.tokenTypes[i])
|
switch (s.tokenTypes[i]) {
|
||||||
{
|
|
||||||
case TOK_LABEL:
|
case TOK_LABEL:
|
||||||
labels[s.tokenValues[i]] = cursor;
|
labels[s.tokenValues[i]] = cursor;
|
||||||
break;
|
break;
|
||||||
@ -205,7 +220,7 @@ int main(int argc, char **argv)
|
|||||||
|
|
||||||
if (verbose) {
|
if (verbose) {
|
||||||
printf("resolved positions for %d labels.\n", int(labels.size()));
|
printf("resolved positions for %d labels.\n", int(labels.size()));
|
||||||
printf("total data (including strings): %.2f MB\n", double(cursor) / (1024*1024));
|
printf("total data (including strings): %.2f MB\n", double(cursor) / (1024 * 1024));
|
||||||
}
|
}
|
||||||
|
|
||||||
std::vector<uint8_t> data(cursor);
|
std::vector<uint8_t> data(cursor);
|
||||||
@ -216,8 +231,7 @@ int main(int argc, char **argv)
|
|||||||
uint32_t value = s.tokenValues[i];
|
uint32_t value = s.tokenValues[i];
|
||||||
int numBytes = 0;
|
int numBytes = 0;
|
||||||
|
|
||||||
switch (s.tokenTypes[i])
|
switch (s.tokenTypes[i]) {
|
||||||
{
|
|
||||||
case TOK_LABEL:
|
case TOK_LABEL:
|
||||||
break;
|
break;
|
||||||
case TOK_REF:
|
case TOK_REF:
|
||||||
@ -238,8 +252,7 @@ int main(int argc, char **argv)
|
|||||||
}
|
}
|
||||||
|
|
||||||
if (bigEndian) {
|
if (bigEndian) {
|
||||||
switch (numBytes)
|
switch (numBytes) {
|
||||||
{
|
|
||||||
case 4:
|
case 4:
|
||||||
data[cursor++] = value >> 24;
|
data[cursor++] = value >> 24;
|
||||||
data[cursor++] = value >> 16;
|
data[cursor++] = value >> 16;
|
||||||
@ -256,14 +269,13 @@ int main(int argc, char **argv)
|
|||||||
assert(0);
|
assert(0);
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
switch (numBytes)
|
switch (numBytes) {
|
||||||
{
|
|
||||||
case 4:
|
case 4:
|
||||||
data[cursor+3] = value >> 24;
|
data[cursor + 3] = value >> 24;
|
||||||
data[cursor+2] = value >> 16;
|
data[cursor + 2] = value >> 16;
|
||||||
/* fall-through */
|
/* fall-through */
|
||||||
case 2:
|
case 2:
|
||||||
data[cursor+1] = value >> 8;
|
data[cursor + 1] = value >> 8;
|
||||||
/* fall-through */
|
/* fall-through */
|
||||||
case 1:
|
case 1:
|
||||||
data[cursor] = value;
|
data[cursor] = value;
|
||||||
@ -284,7 +296,7 @@ int main(int argc, char **argv)
|
|||||||
for (auto &s : preText)
|
for (auto &s : preText)
|
||||||
fprintf(fileOut, "%s\n", s.c_str());
|
fprintf(fileOut, "%s\n", s.c_str());
|
||||||
|
|
||||||
fprintf(fileOut, "const char %s[%d] =\n\"", streams[0].name.c_str(), int(data.size())+1);
|
fprintf(fileOut, "const char %s[%d] =\n\"", streams[0].name.c_str(), int(data.size()) + 1);
|
||||||
|
|
||||||
cursor = 1;
|
cursor = 1;
|
||||||
for (auto d : data) {
|
for (auto d : data) {
|
||||||
@ -300,8 +312,7 @@ int main(int argc, char **argv)
|
|||||||
if (d < 32 || d >= 128) {
|
if (d < 32 || d >= 128) {
|
||||||
fprintf(fileOut, "\\%03o", int(d));
|
fprintf(fileOut, "\\%03o", int(d));
|
||||||
cursor += 4;
|
cursor += 4;
|
||||||
} else
|
} else if (d == '\"' || d == '\'' || d == '\\') {
|
||||||
if (d == '\"' || d == '\'' || d == '\\') {
|
|
||||||
fputc('\\', fileOut);
|
fputc('\\', fileOut);
|
||||||
fputc(d, fileOut);
|
fputc(d, fileOut);
|
||||||
cursor += 2;
|
cursor += 2;
|
||||||
|
Loading…
Reference in New Issue
Block a user