Fix bba to compile on windows (no unistd there)

This commit is contained in:
Miodrag Milanovic 2018-07-25 09:31:44 +02:00
parent c3859072d4
commit 8b60ed5fd1
2 changed files with 55 additions and 43 deletions

View File

@ -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)

View File

@ -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;
@ -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,8 +269,7 @@ 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;
@ -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;