├── bf.exe ├── infrabuck.exe ├── sources ├── mx.ico ├── ide.h ├── ide.rc ├── bf.c └── ide.c └── README.md /bf.exe: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/mitxela/infrabuck/HEAD/bf.exe -------------------------------------------------------------------------------- /infrabuck.exe: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/mitxela/infrabuck/HEAD/infrabuck.exe -------------------------------------------------------------------------------- /sources/mx.ico: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/mitxela/infrabuck/HEAD/sources/mx.ico -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # infrabuck 2 | 3 | A simple windows/x86 brainfuck compiler. Performs only the most basic optimization, condensing runs of sequential increment/decrement operations into single instructions. 4 | 5 | bf.exe is the command-line compiler, infrabuck.exe is the GUI that invokes it. 6 | 7 | Lots more info here: http://mitxela.com/projects/infrabuck 8 | -------------------------------------------------------------------------------- /sources/ide.h: -------------------------------------------------------------------------------- 1 | 2 | #define IDC_MAIN_EDIT 101 3 | #define IDR_MAINMENU 102 4 | #define IDR_ACCEL1 103 5 | #define ID_ICON1 104 6 | #define IDC_STATUSBAR 105 7 | #define ID_FILE_EXIT 40001 8 | #define ID_FILE_OPEN 40002 9 | #define ID_FILE_SAVE 40003 10 | #define ID_FILE_SAVEAS 40004 11 | #define ID_FILE_NEW 40005 12 | #define ID_COMPILE 40006 13 | #define ID_SELECTALL 40007 14 | #define ID_ABOUT 40009 15 | 16 | -------------------------------------------------------------------------------- /sources/ide.rc: -------------------------------------------------------------------------------- 1 | 2 | #include "ide.h" 3 | 4 | ID_ICON1 ICON "mx.ico" 5 | 6 | IDR_MAINMENU MENU DISCARDABLE 7 | BEGIN 8 | POPUP "&File" 9 | BEGIN 10 | MENUITEM "&New\tCtrl+N", ID_FILE_NEW 11 | MENUITEM "&Open...\tCtrl+O", ID_FILE_OPEN 12 | MENUITEM "&Save\tCtrl+S", ID_FILE_SAVE 13 | MENUITEM "Save &As...", ID_FILE_SAVEAS 14 | MENUITEM SEPARATOR 15 | MENUITEM "E&xit\tAlt+F4", ID_FILE_EXIT 16 | END 17 | POPUP "&Edit" 18 | BEGIN 19 | MENUITEM "Select A&ll\tCtrl+A", ID_SELECTALL 20 | END 21 | POPUP "&Compile" 22 | BEGIN 23 | MENUITEM "&Run\tF5", ID_COMPILE 24 | END 25 | POPUP "&Help" 26 | BEGIN 27 | MENUITEM "Abo&ut", ID_ABOUT 28 | END 29 | END 30 | 31 | 32 | IDR_ACCEL1 ACCELERATORS 33 | BEGIN 34 | 0x4E, ID_FILE_NEW, VIRTKEY, CONTROL // ctrl-N 35 | 0x4F, ID_FILE_OPEN, VIRTKEY, CONTROL // ctrl-O 36 | 0x53, ID_FILE_SAVE, VIRTKEY, CONTROL // ctrl-S 37 | 0x41, ID_SELECTALL, VIRTKEY, CONTROL // ctrl-A 38 | 0x74, ID_COMPILE, VIRTKEY // F5 39 | 40 | END 41 | -------------------------------------------------------------------------------- /sources/bf.c: -------------------------------------------------------------------------------- 1 | /* windows/x86 brainfuck compiler 2 | * written by mitxela 3 | * http://mitxela.com/projects/infrabuck 4 | */ 5 | 6 | #include 7 | #include 8 | #include 9 | 10 | // Header prototype 11 | // MSDOS Stub 12 | // PE Header 13 | // .data Reserve 32768 bytes for tape 14 | // .idata Import: 15 | // kernel32: ExitProcess 16 | // msvcrt: putchar, getchar 17 | const unsigned char exeHead[]={ 18 | 0x4D,0x5A,0x80,0x00,0x01,0x00,0x00,0x00,0x04,0x00,0x10,0x00,0xFF,0xFF,0x00,0x00, 19 | 0x40,0x01,0x00,0x00,0x00,0x00,0x00,0x00,0x40,0x00,0x00,0x00,0x00,0x00,0x00,0x00, 20 | 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00, 21 | 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x80,0x00,0x00,0x00, 22 | 0x0E,0x1F,0xBA,0x0E,0x00,0xB4,0x09,0xCD,0x21,0xB8,0x01,0x4C,0xCD,0x21,0x54,0x68, 23 | 0x69,0x73,0x20,0x70,0x72,0x6F,0x67,0x72,0x61,0x6D,0x20,0x63,0x61,0x6E,0x6E,0x6F, 24 | 0x74,0x20,0x62,0x65,0x20,0x72,0x75,0x6E,0x20,0x69,0x6E,0x20,0x44,0x4F,0x53,0x20, 25 | 0x6D,0x6F,0x64,0x65,0x2E,0x0D,0x0A,0x24,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00, 26 | 0x50,0x45,0x00,0x00,0x4C,0x01,0x03,0x00,0x44,0xC8,0x05,0x59,0x00,0x00,0x00,0x00, 27 | 0x00,0x00,0x00,0x00,0xE0,0x00,0x0F,0x01,0x0B,0x01,0x01,0x47,0x00,0x02,0x00,0x00, 28 | 0x00,0x02,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0xA0,0x00,0x00,0x00,0xA0,0x00,0x00, 29 | 0x00,0x10,0x00,0x00,0x00,0x00,0x40,0x00,0x00,0x10,0x00,0x00,0x00,0x02,0x00,0x00, 30 | 0x01,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x03,0x00,0x0A,0x00,0x00,0x00,0x00,0x00, 31 | 0x00,0xB0,0x00,0x00,0x00,0x02,0x00,0x00,0x00,0x00,0x00,0x00,0x03,0x00,0x00,0x00, 32 | 0x00,0x10,0x00,0x00,0x00,0x10,0x00,0x00,0x00,0x00,0x01,0x00,0x00,0x00,0x00,0x00, 33 | 0x00,0x00,0x00,0x00,0x10,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00, 34 | 0x00,0x90,0x00,0x00,0xA4,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00, 35 | 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00, 36 | 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00, 37 | 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00, 38 | 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00, 39 | 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00, 40 | 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00, 41 | 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x2E,0x64,0x61,0x74,0x61,0x00,0x00,0x00, 42 | 0x00,0x80,0x00,0x00,0x00,0x10,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x02,0x00,0x00, 43 | 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0xC0,0x00,0x00,0xC0, 44 | 0x2E,0x69,0x64,0x61,0x74,0x61,0x00,0x00,0xA4,0x00,0x00,0x00,0x00,0x90,0x00,0x00, 45 | 0x00,0x02,0x00,0x00,0x00,0x02,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00, 46 | 0x00,0x00,0x00,0x00,0x40,0x00,0x00,0x40,0x2E,0x74,0x65,0x78,0x74,0x00,0x00,0x00, 47 | 0x29,0x00,0x00,0x00,0x00,0xA0,0x00,0x00,0x00,0x02,0x00,0x00,0x00,0x04,0x00,0x00, 48 | 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x20,0x00,0x00,0x60, 49 | 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00, 50 | 0x58,0x90,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x3C,0x90,0x00,0x00, 51 | 0x60,0x90,0x00,0x00,0x78,0x90,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00, 52 | 0x4A,0x90,0x00,0x00,0x84,0x90,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00, 53 | 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x6B,0x65,0x72,0x6E, 54 | 0x65,0x6C,0x33,0x32,0x2E,0x64,0x6C,0x6C,0x00,0x00,0x6D,0x73,0x76,0x63,0x72,0x74, 55 | 0x2E,0x64,0x6C,0x6C,0x00,0x00,0x00,0x00,0x68,0x90,0x00,0x00,0x00,0x00,0x00,0x00, 56 | 0x68,0x90,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x45,0x78,0x69,0x74,0x50,0x72, 57 | 0x6F,0x63,0x65,0x73,0x73,0x00,0x00,0x00,0x90,0x90,0x00,0x00,0x9A,0x90,0x00,0x00, 58 | 0x00,0x00,0x00,0x00,0x90,0x90,0x00,0x00,0x9A,0x90,0x00,0x00,0x00,0x00,0x00,0x00, 59 | 0x00,0x00,0x70,0x75,0x74,0x63,0x68,0x61,0x72,0x00,0x00,0x00,0x67,0x65,0x74,0x63, 60 | 0x68,0x61,0x72,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00, 61 | 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00, 62 | 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00, 63 | 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00, 64 | 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00, 65 | 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00, 66 | 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00, 67 | 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00, 68 | 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00, 69 | 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00, 70 | 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00, 71 | 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00, 72 | 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00, 73 | 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00, 74 | 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00, 75 | 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00, 76 | 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00, 77 | 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00, 78 | 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00, 79 | 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00, 80 | 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00, 81 | 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00, 82 | 0x55,0x89,0xE5,0xBB,0x00,0x10,0x40,0x00 83 | }; 84 | const unsigned char tail[]= { 0x89,0xEC,0x5D,0xFF,0x15,0x60,0x90,0x40 }; 85 | 86 | const unsigned char _putchar[] = { 0x53,0x8B,0x03,0x50,0xFF,0x15,0x84,0x90,0x40,0x00,0x83,0xC4,0x04,0x5B }; 87 | const unsigned char _getchar[] = { 0x53,0xFF,0x15,0x88,0x90,0x40,0x00,0x5B,0x88,0x03 }; 88 | unsigned char _inc[] = { 0x80,0x03,0x01 }; 89 | unsigned char _dec[] = { 0x80,0x2B,0x01 }; 90 | unsigned char _next[] = { 0x83,0xC3,0x01 }; 91 | unsigned char _prev[] = { 0x83,0xEB,0x01 }; 92 | unsigned char _nextLong[] = { 0x81,0xC3,0x01,0x00,0x00,0x00 }; 93 | unsigned char _prevLong[] = { 0x81,0xEB,0x01,0x00,0x00,0x00 }; 94 | 95 | unsigned char _open[] = { 0x8B,0x03,0x3C,0x00,0x74,0x0A }; 96 | unsigned char _openLong[] = { 0x8B,0x03,0x3C,0x00,0x0F,0x84,0x80,0x00,0x00,0x00 }; 97 | unsigned char _close[] = { 0x8B,0x03,0x3C,0x00,0x75,0xFA }; 98 | unsigned char _closeLong[] = { 0x8B,0x03,0x3C,0x00,0x0F,0x85,0x7B,0xFF,0xFF,0xFF }; 99 | 100 | 101 | FILE *in; 102 | FILE *out; 103 | int *stack; 104 | 105 | int error(char* msg) { 106 | printf(msg); 107 | fclose(in); 108 | fclose(out); 109 | free(stack); 110 | return 1; 111 | }; 112 | 113 | // Condense runs of sequential ops 114 | int run(char m){ 115 | int num=1; 116 | char j; 117 | do { 118 | while (m==(j=fgetc(in))) num++; 119 | } while (j!='['&&j!=']'&&j!='+'&&j!='-'&&j!='.'&&j!=','&&j!='>'&&j!='<'&&j!=EOF); 120 | if (j!=EOF) fseek(in, -1, SEEK_CUR); 121 | return num; 122 | } 123 | 124 | int main(int argc, char *argv[]) { 125 | int depth=0; 126 | int i; 127 | 128 | if ( argc != 3 ){ 129 | printf("\nx86 Brainfuck Compiler by mitxela\nhttp://mitxela.com/\n\nUsage: bf infile outfile\n\n"); 130 | return 1; 131 | } 132 | 133 | if ((in = fopen(argv[1],"r"))==NULL) { 134 | printf("Unable to open file '%s'",argv[1]); 135 | return 1; 136 | } 137 | 138 | if ((out = fopen(argv[2],"wb+"))==NULL) { 139 | printf("Unable to open file '%s' (still running?)",argv[2]); 140 | return 1; 141 | } 142 | 143 | fwrite(exeHead,sizeof(unsigned char),sizeof(exeHead),out); 144 | 145 | // Preprocess: count the number of loops for mem allocation 146 | while ((i=fgetc(in)) !=EOF){ 147 | if (i=='[') depth++; 148 | } 149 | stack = malloc(depth * sizeof(int)); 150 | depth = 0; 151 | 152 | rewind(in); 153 | 154 | // Main pass 155 | 156 | #define op(d) fwrite(d, sizeof(unsigned char), sizeof(d), out) 157 | 158 | while ((i=fgetc(in)) !=EOF){ 159 | switch (i) { 160 | case '.': 161 | op(_putchar); 162 | break; 163 | case ',': 164 | op(_getchar); 165 | break; 166 | case '+': 167 | _inc[2] = (unsigned char)run(i); 168 | op(_inc); 169 | break; 170 | case '-': 171 | _dec[2] = (unsigned char)run(i); 172 | op(_dec); 173 | break; 174 | case '>': 175 | { 176 | int j = run(i); 177 | if (j>127) { 178 | fwrite(_nextLong, sizeof(unsigned char), sizeof(_nextLong)-4, out); 179 | fwrite(&j,4,1,out); 180 | } else { 181 | _next[2]=(unsigned char)j; 182 | op(_next); 183 | } 184 | } 185 | break; 186 | case '<': 187 | { 188 | int j = run(i); 189 | if (j>127) { 190 | fwrite(_prevLong, sizeof(unsigned char), sizeof(_prevLong)-4, out); 191 | fwrite(&j,4,1,out); 192 | } else { 193 | _prev[2]=(unsigned char)j; 194 | op(_prev); 195 | } 196 | } 197 | break; 198 | case '[': 199 | op(_openLong); 200 | stack[depth++] = ftell(out); 201 | break; 202 | case ']': 203 | { 204 | if (depth == 0) return error("Syntax error: ] before [\n"); 205 | 206 | int dist = ftell(out) - stack[--depth]; 207 | 208 | if ( sizeof(_close)+dist < 128) { 209 | _close[5] = -sizeof(_close)-dist; 210 | op(_close); 211 | dist += sizeof(_close); 212 | } else { 213 | fwrite(_closeLong, sizeof(unsigned char), sizeof(_closeLong)-4, out); 214 | int l = -dist-sizeof(_closeLong); 215 | fwrite(&l, 4, 1, out); 216 | dist += sizeof(_closeLong); 217 | } 218 | fseek(out, -4 -dist, SEEK_CUR); 219 | fwrite(&dist, sizeof(int),1,out); 220 | fseek(out, dist, SEEK_CUR); 221 | } 222 | break; 223 | } 224 | } 225 | 226 | if (depth != 0) return error("Syntax error: missing ]\n"); 227 | free(stack); 228 | op(tail); 229 | 230 | 231 | int codeSize = ftell(out)-0x400; 232 | int rawSize = (1+(codeSize / 0x200)) * 0x200; 233 | int imageSize = ((rawSize-0x200 ) /0x1000 ) *0x1000 + 0xB000 ; 234 | 235 | fseek(out,rawSize+0x3FF,SEEK_SET); 236 | fputc(0,out); 237 | 238 | // 0x88 Timestamp 239 | fseek(out,0x88,SEEK_SET); 240 | int t = (int)time(NULL); 241 | fwrite(&t,sizeof(int),1,out); 242 | 243 | 244 | // 0x1D0 .text VirtualSize (appears to be code size +1) 245 | codeSize++; 246 | fseek(out,0x1D0,SEEK_SET); 247 | fwrite(&codeSize, sizeof(int),1,out); 248 | codeSize--; 249 | 250 | // 0x1D8 Size of Raw Data 251 | fseek(out,0x1D8,SEEK_SET); 252 | fwrite(&rawSize, sizeof(int),1,out); 253 | 254 | //0x9C PE Size of Code (= size of raw data) 255 | fseek(out,0x9C,SEEK_SET); 256 | fwrite(&rawSize, sizeof(int),1,out); 257 | 258 | //d0 size of image 259 | fseek(out,0xD0,SEEK_SET); 260 | fwrite(&imageSize, sizeof(int),1,out); 261 | 262 | 263 | fclose(in); 264 | fclose(out); 265 | 266 | return 0; 267 | } 268 | -------------------------------------------------------------------------------- /sources/ide.c: -------------------------------------------------------------------------------- 1 | /* infrabuck - GUI for windows/x86 brainfuck compiler 2 | * invokes bf.exe, must be in the same folder. 3 | * written by mitxela 4 | * http://mitxela.com/projects/infrabuck 5 | */ 6 | 7 | #include 8 | #include 9 | #include "ide.h" 10 | 11 | #define wClass "ideClass" 12 | #define ideTitle "infrabuck" 13 | #define fileFilter "Brainfuck (*.bf;*.b)\0*.bf;*.b\0All Files (*.*)\0*.*\0" 14 | 15 | char workingFileName[MAX_PATH]=""; 16 | BOOL fileChanged=FALSE; 17 | 18 | void SetTitleBar(HWND hwnd) { 19 | char titleBar[MAX_PATH + 20]; 20 | char unt[] = "Untitled"; 21 | char * filename = unt; 22 | 23 | if (workingFileName!= NULL && workingFileName[0]!=0) filename = workingFileName; 24 | 25 | sprintf(titleBar, "%s%s - %s",fileChanged?"*":"",filename,ideTitle); 26 | 27 | SetWindowText(hwnd, titleBar); 28 | } 29 | 30 | void UpdateStatusBar(HWND hwnd) { 31 | DWORD selstart, selend; 32 | char status[100] =""; 33 | 34 | SendMessage(GetDlgItem(hwnd, IDC_MAIN_EDIT), EM_GETSEL, (WPARAM)&selstart, (LPARAM)&selend); 35 | 36 | if (selend!=selstart) sprintf(status, "Selection: %d", selend-selstart); 37 | 38 | SendMessage(GetDlgItem(hwnd, IDC_STATUSBAR), SB_SETTEXT, 0, (LPARAM)&status); 39 | } 40 | 41 | BOOL LoadTextFileToEdit(HWND hEdit, LPCTSTR pszFileName) { 42 | HANDLE hFile; 43 | BOOL bSuccess = FALSE; 44 | 45 | hFile = CreateFile(pszFileName, GENERIC_READ, FILE_SHARE_READ, NULL,OPEN_EXISTING, 0, NULL); 46 | if(hFile != INVALID_HANDLE_VALUE) 47 | { 48 | DWORD dwFileSize; 49 | 50 | dwFileSize = GetFileSize(hFile, NULL); 51 | if(dwFileSize != 0xFFFFFFFF) 52 | { 53 | LPSTR pszFileText; 54 | 55 | pszFileText = (LPSTR)GlobalAlloc(GPTR, dwFileSize + 1); 56 | if(pszFileText != NULL) 57 | { 58 | DWORD dwRead; 59 | 60 | if(ReadFile(hFile, pszFileText, dwFileSize, &dwRead, NULL)) 61 | { 62 | pszFileText[dwFileSize] = 0; 63 | if(SetWindowText(hEdit, pszFileText)) 64 | bSuccess = TRUE; 65 | } 66 | GlobalFree(pszFileText); 67 | } 68 | } 69 | CloseHandle(hFile); 70 | } 71 | return bSuccess; 72 | } 73 | 74 | BOOL SaveTextFileFromEdit(HWND hEdit, LPCTSTR pszFileName) 75 | { 76 | HANDLE hFile; 77 | BOOL bSuccess = FALSE; 78 | 79 | hFile = CreateFile(pszFileName, GENERIC_WRITE, 0, NULL,CREATE_ALWAYS, FILE_ATTRIBUTE_NORMAL, NULL); 80 | if(hFile != INVALID_HANDLE_VALUE) 81 | { 82 | DWORD dwTextLength; 83 | 84 | dwTextLength = GetWindowTextLength(hEdit); 85 | // No need to bother if there's no text. 86 | if(dwTextLength > 0) 87 | { 88 | LPSTR pszText; 89 | DWORD dwBufferSize = dwTextLength + 1; 90 | 91 | pszText = (LPSTR)GlobalAlloc(GPTR, dwBufferSize); 92 | if(pszText != NULL) 93 | { 94 | if(GetWindowText(hEdit, pszText, dwBufferSize)) 95 | { 96 | DWORD dwWritten; 97 | 98 | if(WriteFile(hFile, pszText, dwTextLength, &dwWritten, NULL)) 99 | bSuccess = TRUE; 100 | } 101 | GlobalFree(pszText); 102 | } 103 | } 104 | CloseHandle(hFile); 105 | } 106 | return bSuccess; 107 | } 108 | 109 | void DoFileOpen(HWND hwnd) 110 | { 111 | OPENFILENAME ofn; 112 | char szFileName[MAX_PATH] = ""; 113 | 114 | ZeroMemory(&ofn, sizeof(ofn)); 115 | 116 | ofn.lStructSize = sizeof(OPENFILENAME); 117 | ofn.hwndOwner = hwnd; 118 | ofn.lpstrFilter = fileFilter; 119 | ofn.lpstrFile = szFileName; 120 | ofn.nMaxFile = MAX_PATH; 121 | ofn.Flags = OFN_EXPLORER | OFN_FILEMUSTEXIST | OFN_HIDEREADONLY; 122 | ofn.lpstrDefExt = "bf"; 123 | 124 | if(GetOpenFileName(&ofn)) 125 | { 126 | HWND hEdit = GetDlgItem(hwnd, IDC_MAIN_EDIT); 127 | LoadTextFileToEdit(hEdit, szFileName); 128 | strcpy(workingFileName, szFileName); 129 | fileChanged=FALSE; 130 | SetTitleBar(hwnd); 131 | } 132 | } 133 | 134 | void DoFileSaveAs(HWND hwnd) 135 | { 136 | OPENFILENAME ofn; 137 | char szFileName[MAX_PATH] = ""; 138 | 139 | ZeroMemory(&ofn, sizeof(ofn)); 140 | 141 | ofn.lStructSize = sizeof(OPENFILENAME); 142 | ofn.hwndOwner = hwnd; 143 | ofn.lpstrFilter = fileFilter; 144 | ofn.lpstrFile = szFileName; 145 | ofn.nMaxFile = MAX_PATH; 146 | ofn.lpstrDefExt = "bf"; 147 | ofn.Flags = OFN_EXPLORER | OFN_PATHMUSTEXIST | OFN_HIDEREADONLY | OFN_OVERWRITEPROMPT; 148 | 149 | if(GetSaveFileName(&ofn)) { 150 | HWND hEdit = GetDlgItem(hwnd, IDC_MAIN_EDIT); 151 | SaveTextFileFromEdit(hEdit, szFileName); 152 | strcpy(workingFileName, szFileName); 153 | fileChanged=FALSE; 154 | SetTitleBar(hwnd); 155 | } 156 | } 157 | void DoFileSave(HWND hwnd){ 158 | if (workingFileName!= NULL && workingFileName[0]!=0) { 159 | HWND hEdit = GetDlgItem(hwnd, IDC_MAIN_EDIT); 160 | SaveTextFileFromEdit(hEdit, workingFileName); 161 | fileChanged=FALSE; 162 | SetTitleBar(hwnd); 163 | } else DoFileSaveAs(hwnd); 164 | } 165 | BOOL checkSave(HWND hwnd) { 166 | if (!fileChanged) return 0; 167 | 168 | char msg[MAX_PATH + 20]="Save file?"; 169 | char fname[MAX_PATH]; 170 | char ext[MAX_PATH]; 171 | _splitpath(workingFileName,NULL,NULL,fname,ext); 172 | 173 | if (workingFileName!= NULL && workingFileName[0]!=0) sprintf(msg,"Save changes to %s%s?",fname,ext); 174 | 175 | int q = MessageBox(hwnd, msg, "Save", MB_YESNOCANCEL); 176 | if (q == IDYES) { 177 | DoFileSave(hwnd); 178 | } 179 | return (q==IDCANCEL); 180 | } 181 | int compile(HWND hwnd){ 182 | 183 | if (checkSave(hwnd)) return 0; 184 | if (workingFileName== NULL || workingFileName[0]==0) { 185 | MessageBox(hwnd,"File must be saved in order to compile", "Error", MB_ICONEXCLAMATION | MB_OK); 186 | return 1; 187 | } 188 | 189 | char fname[MAX_PATH]; 190 | _splitpath(workingFileName,NULL,NULL,fname,NULL); 191 | 192 | char outfile[MAX_PATH]; 193 | sprintf(outfile, "%s.exe",fname); 194 | 195 | HANDLE stdOutRd = NULL; 196 | HANDLE stdOutWr = NULL; 197 | SECURITY_ATTRIBUTES sa; 198 | 199 | sa.nLength = sizeof(SECURITY_ATTRIBUTES); 200 | sa.bInheritHandle = TRUE; 201 | sa.lpSecurityDescriptor = NULL; 202 | 203 | if ( !CreatePipe(&stdOutRd, &stdOutWr, &sa, 0) 204 | || !SetHandleInformation(stdOutRd, HANDLE_FLAG_INHERIT, 0)) { 205 | MessageBox(hwnd,"Error: create pipe", "Error", MB_ICONEXCLAMATION | MB_OK); 206 | return 1; 207 | } 208 | 209 | char cmd[MAX_PATH + 200]; 210 | PROCESS_INFORMATION ProcessInfo; 211 | STARTUPINFO StartupInfo; 212 | 213 | sprintf(cmd,"bf.exe \"%s\" \"%s\"", workingFileName, outfile); 214 | 215 | ZeroMemory( &StartupInfo, sizeof(STARTUPINFO) ); 216 | StartupInfo.cb = sizeof(STARTUPINFO); 217 | StartupInfo.hStdOutput = stdOutWr; 218 | StartupInfo.dwFlags = STARTF_USESTDHANDLES; 219 | 220 | if (!CreateProcess(NULL, cmd, NULL, NULL, TRUE, CREATE_NO_WINDOW, NULL, NULL, &StartupInfo, &ProcessInfo)){ 221 | MessageBox(hwnd,"Unable to invoke bf.exe", "Error", MB_ICONEXCLAMATION | MB_OK); 222 | return 1; 223 | } 224 | WaitForSingleObject(ProcessInfo.hProcess,INFINITE); 225 | 226 | LPDWORD ecode; 227 | GetExitCodeProcess(ProcessInfo.hProcess,&ecode); 228 | if (ecode!=0) { 229 | DWORD dwRead; 230 | CHAR chBuf[200]; 231 | ReadFile( stdOutRd, chBuf, 200, &dwRead, NULL); 232 | chBuf[dwRead]=0; 233 | MessageBox(hwnd, chBuf, "Compile Error", MB_ICONEXCLAMATION | MB_OK); 234 | } else { 235 | PROCESS_INFORMATION pinfo; 236 | STARTUPINFO sinfo; 237 | ZeroMemory( &sinfo, sizeof(STARTUPINFO) ); 238 | sinfo.cb = sizeof(STARTUPINFO); 239 | CreateProcess(NULL, outfile, NULL, NULL, FALSE, 0, NULL, NULL, &sinfo, &pinfo); 240 | } 241 | CloseHandle(stdOutWr); 242 | CloseHandle(stdOutRd); 243 | return 0; 244 | } 245 | 246 | LRESULT CALLBACK WndProc(HWND hwnd, UINT msg, WPARAM wParam, LPARAM lParam) { 247 | switch(msg) { 248 | case WM_CREATE: 249 | { 250 | HFONT hfDefault; 251 | HWND hEdit; 252 | HWND hStatus; 253 | 254 | hStatus = CreateWindowEx(0, STATUSCLASSNAME, NULL, WS_CHILD | WS_VISIBLE | SBARS_SIZEGRIP, 0, 0, 0, 0, hwnd, (HMENU)IDC_STATUSBAR, GetModuleHandle(NULL), NULL); 255 | if (hStatus == NULL) { 256 | MessageBox(NULL, "Window Creation Failed!", "Error!", MB_ICONEXCLAMATION | MB_OK); 257 | return 0; 258 | } 259 | 260 | hEdit = CreateWindowEx(WS_EX_CLIENTEDGE, "EDIT", "", 261 | WS_CHILD | WS_VISIBLE | WS_VSCROLL | WS_HSCROLL | ES_MULTILINE | ES_AUTOVSCROLL | ES_AUTOHSCROLL, 262 | 0, 0, 100, 100, hwnd, (HMENU)IDC_MAIN_EDIT, GetModuleHandle(NULL), NULL); 263 | if(hEdit == NULL) 264 | MessageBox(hwnd, "Could not create edit box.", "Error", MB_OK | MB_ICONERROR); 265 | 266 | hfDefault = (HFONT)GetStockObject(ANSI_FIXED_FONT); 267 | SendMessage(hEdit, WM_SETFONT, (WPARAM)hfDefault, MAKELPARAM(FALSE, 0)); 268 | 269 | if (workingFileName!= NULL && workingFileName[0]!=0) { 270 | LoadTextFileToEdit(hEdit, workingFileName); 271 | } 272 | SetTitleBar(hwnd); 273 | SetFocus(hEdit); 274 | } 275 | break; 276 | case WM_SIZE: 277 | { 278 | HWND hEdit; 279 | RECT rcClient; 280 | HWND hStatus; 281 | RECT rcStatus; 282 | 283 | hStatus = GetDlgItem(hwnd, IDC_STATUSBAR); 284 | SendMessage(hStatus, WM_SIZE, 0, 0); 285 | GetWindowRect(hStatus, &rcStatus); 286 | 287 | GetClientRect(hwnd, &rcClient); 288 | 289 | hEdit = GetDlgItem(hwnd, IDC_MAIN_EDIT); 290 | SetWindowPos(hEdit, NULL, 0, 0, rcClient.right, rcClient.bottom-(rcStatus.bottom - rcStatus.top), SWP_NOZORDER); 291 | } 292 | break; 293 | case WM_CLOSE: 294 | if (checkSave(hwnd)) return 0; 295 | DestroyWindow(hwnd); 296 | break; 297 | case WM_DESTROY: 298 | PostQuitMessage(0); 299 | break; 300 | case WM_COMMAND: 301 | switch (LOWORD(wParam)) { 302 | case ID_FILE_EXIT: 303 | PostMessage(hwnd, WM_CLOSE, 0, 0); 304 | break; 305 | case ID_FILE_NEW: 306 | if (checkSave(hwnd)) return 0; 307 | SetDlgItemText(hwnd, IDC_MAIN_EDIT, ""); 308 | sprintf(workingFileName,""); 309 | fileChanged=FALSE; 310 | SetTitleBar(hwnd); 311 | break; 312 | case ID_FILE_OPEN: 313 | if (checkSave(hwnd)) return 0; 314 | DoFileOpen(hwnd); 315 | break; 316 | case ID_FILE_SAVE: 317 | DoFileSave(hwnd); 318 | break; 319 | case ID_FILE_SAVEAS: 320 | DoFileSaveAs(hwnd); 321 | break; 322 | case ID_SELECTALL: 323 | SendMessage(GetDlgItem(hwnd, IDC_MAIN_EDIT), EM_SETSEL, 0, -1); 324 | break; 325 | case ID_ABOUT: 326 | MessageBox(hwnd,"Infrabuck\n\nA simple brainfuck compiler for windows\n\nWritten by mitxela","About infrabuck", MB_ICONINFORMATION | MB_OK); 327 | break; 328 | case IDC_MAIN_EDIT: 329 | if (HIWORD(wParam)==EN_CHANGE && !fileChanged) { 330 | fileChanged=TRUE; 331 | SetTitleBar(hwnd); 332 | } 333 | break; 334 | case ID_COMPILE: 335 | compile(hwnd); 336 | break; 337 | } 338 | break; 339 | case WM_ACTIVATE: 340 | if (LOWORD(wParam)!=WA_INACTIVE) { 341 | SetFocus(GetDlgItem(hwnd, IDC_MAIN_EDIT)); 342 | } 343 | break; 344 | default: 345 | if (msg==WM_CTLCOLOREDIT || msg==WM_MOUSEACTIVATE) UpdateStatusBar(hwnd); 346 | 347 | return DefWindowProc(hwnd, msg, wParam, lParam); 348 | } 349 | return 0; 350 | } 351 | 352 | int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine, int nCmdShow) { 353 | WNDCLASSEX wc; 354 | MSG Msg; 355 | 356 | if (lpCmdLine != NULL && lpCmdLine[0]!=0) strcpy(workingFileName, lpCmdLine); 357 | 358 | wc.cbSize = sizeof(WNDCLASSEX); 359 | wc.style = 0; 360 | wc.lpfnWndProc = WndProc; 361 | wc.cbClsExtra = 0; 362 | wc.cbWndExtra = 0; 363 | wc.hInstance = hInstance; 364 | wc.hIcon = (HICON)LoadImage(hInstance, MAKEINTRESOURCE(ID_ICON1),IMAGE_ICON,16,16,LR_DEFAULTCOLOR); 365 | wc.hCursor = LoadCursor(NULL, IDC_ARROW); 366 | wc.hbrBackground = (HBRUSH)(COLOR_WINDOW+1); 367 | wc.lpszMenuName = MAKEINTRESOURCE(IDR_MAINMENU); 368 | wc.lpszClassName = wClass; 369 | wc.hIconSm = (HICON)LoadImage(hInstance, MAKEINTRESOURCE(ID_ICON1),IMAGE_ICON,16,16,LR_DEFAULTCOLOR); 370 | 371 | if (!RegisterClassEx(&wc)) { 372 | MessageBox(NULL, "Window Registration Failed!", "Error!", MB_ICONEXCLAMATION | MB_OK); 373 | return 0; 374 | } 375 | 376 | INITCOMMONCONTROLSEX icex; 377 | icex.dwSize = sizeof(INITCOMMONCONTROLSEX); 378 | InitCommonControlsEx(&icex); 379 | 380 | HWND hwnd = CreateWindowEx(0, wClass, ideTitle, WS_OVERLAPPEDWINDOW, CW_USEDEFAULT, CW_USEDEFAULT, 800, 600, NULL, NULL, hInstance, NULL); 381 | 382 | if (hwnd == NULL) { 383 | MessageBox(NULL, "Window Creation Failed!", "Error!", MB_ICONEXCLAMATION | MB_OK); 384 | return 0; 385 | } 386 | 387 | ShowWindow(hwnd, nCmdShow); 388 | UpdateWindow(hwnd); 389 | 390 | HACCEL hAccel = LoadAccelerators(hInstance, MAKEINTRESOURCE(IDR_ACCEL1)); 391 | 392 | while (GetMessage(&Msg, NULL, 0, 0) > 0) { 393 | if (Msg.message == WM_KEYDOWN || Msg.message == WM_KEYUP) UpdateStatusBar(hwnd); 394 | 395 | if (!TranslateAccelerator(hwnd, hAccel, &Msg)) { 396 | TranslateMessage(&Msg); 397 | DispatchMessage(&Msg); 398 | } 399 | } 400 | return Msg.wParam; 401 | } 402 | --------------------------------------------------------------------------------