From 33f19530d4eb4e60169d895fbb4e22d38b6640bf Mon Sep 17 00:00:00 2001 From: MrMelon Date: Wed, 5 May 2021 13:22:14 +0100 Subject: [PATCH] Start adding binary output --- Makefile | 4 +-- src/{ => impl}/main.c | 63 +++++++++++++++++++++++++++++++++---------- src/impl/popen2.c | 36 +++++++++++++++++++++++++ src/intf/popen2.h | 3 +++ 4 files changed, 90 insertions(+), 16 deletions(-) rename src/{ => impl}/main.c (60%) create mode 100644 src/impl/popen2.c create mode 100644 src/intf/popen2.h diff --git a/Makefile b/Makefile index b056e52..64b28e1 100644 --- a/Makefile +++ b/Makefile @@ -1,4 +1,4 @@ -sourcefiles := $(shell find src/ -name *.c) +sourcefiles := $(shell find src/impl/ -name *.c) all: @@ -6,7 +6,7 @@ all: build-main: mkdir -p dist/ && \ - gcc -o dist/bf2c ${sourcefiles} + gcc -o dist/bf2c -I src/intf/ ${sourcefiles} package: ./scripts/package-bf2c.sh diff --git a/src/main.c b/src/impl/main.c similarity index 60% rename from src/main.c rename to src/impl/main.c index e549ca0..9922eba 100644 --- a/src/main.c +++ b/src/impl/main.c @@ -1,39 +1,74 @@ #include #include #include +#include "popen2.h" int is_arg(int ac, char **argv, char *arg) { - if (ac < 2) { - return 0; - } - for(int x=1; x < ac; x++) { - if (0 == strcmp(argv[x], arg)) { + if (ac < 2) return 0; + for(int x=1; x < ac; x++) + if (0 == strcmp(argv[x], arg)) return x; // return position of arg - } - } return 0; // arg not present } void addValueChange(FILE *out, char *variable, bool increment, int value) { if(increment) { - if(value==1) fprintf(out, "++%s;", variable); - else fprintf(out, "%s+=%d;", variable, value); + if(value==1) fprintf(out, "++%s;\n", variable); + else fprintf(out, "%s+=%d;\n", variable, value); } else { - if(value==1) fprintf(out, "--%s;", variable); - else fprintf(out, "%s-=%d;", variable); + if(value==1) fprintf(out, "--%s;\n", variable); + else fprintf(out, "%s-=%d;\n", variable); } } -int main(int argc, char **argv) -{ +int main(int argc, char **argv) { FILE *in = stdin, *out = stdout; int a; int b; int c; int cellsize = 30000; bool optimise = false; + bool binary = false; + if(is_arg(argc, argv, "-h") || is_arg(argc, argv, "--help")) { + printf("=== bf2c ===\n"); + printf("Usage: bf2c [-o] [-b] [-f filename]\n"); + printf("\n"); + printf(" -o Optimise add, subtract and pointer operations\n"); + printf(" -b Compile generated C code to binary using gcc\n"); + printf(" -f filename Output to a file instead of stdout\n"); + return 0; + } if(is_arg(argc, argv, "-o")) optimise = true; + if(is_arg(argc, argv, "-b")) binary = true; + + char *z = "a.out"; + int y=is_arg(argc, argv, "-f"); + if(y) { + if(argc>y+1 && argv[y+1][0]!='-') { + z = argv[y+1]; + } else { + fprintf(stderr, "Argument '-f' requires a value to specific the filename"); + return 1; + } + out = fopen(z, "w"); + } + + if(binary) { + char *cmd = "gcc"; + char *ar[7] = {0}; + ar[0] = cmd; + ar[1] = "-o"; + ar[2] = z; + ar[3] = "-x"; + ar[4] = "c"; + ar[5] = "-"; + ar[6] = NULL; + + int *fwd; + popen2(ar,fwd,NULL); + out = fdopen(*fwd, "w"); + } fprintf(out, "#include \n" @@ -84,7 +119,7 @@ int main(int argc, char **argv) } // Reset for next loop - a=1; + a = 1; b = c; } break; diff --git a/src/impl/popen2.c b/src/impl/popen2.c new file mode 100644 index 0000000..ee4b6cb --- /dev/null +++ b/src/impl/popen2.c @@ -0,0 +1,36 @@ +#include +#include +#include + +#define READ 0 +#define WRITE 1 + +// omg thanks +// https://stackoverflow.com/a/12788169/10719432 +pid_t popen2(char *const *command, int *infp, int *outfp) { + int p_stdin[2], p_stdout[2]; + pid_t pid; + + if (pipe(p_stdin) != 0 || pipe(p_stdout) != 0) return -1; + + pid = fork(); + + if (pid < 0) return pid; + else if (pid == 0) { + close(p_stdin[WRITE]); + dup2(p_stdin[READ], READ); + close(p_stdout[READ]); + dup2(p_stdout[WRITE], WRITE); + + execvp(command[0], command); + exit(1); + } + + if (infp == NULL) close(p_stdin[WRITE]); + else *infp = p_stdin[WRITE]; + + if (outfp == NULL) close(p_stdout[READ]); + else *outfp = p_stdout[READ]; + + return pid; +} diff --git a/src/intf/popen2.h b/src/intf/popen2.h new file mode 100644 index 0000000..835b53b --- /dev/null +++ b/src/intf/popen2.h @@ -0,0 +1,3 @@ +#pragma once +#include +pid_t popen2(char *const *command, int *infp, int *outfp);