forked from batari-Basic/batari-Basic
-
Notifications
You must be signed in to change notification settings - Fork 0
/
postprocess.c
102 lines (98 loc) · 2.78 KB
/
postprocess.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
// Provided under the GPL v2 license. See the included LICENSE.txt for details.
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
// This reads the includes file created by bB and builds the
// final assembly that will be sent to DASM.
//
// This task used to be done with a batch file/shell script when
// the files in the final asm were static.
// Now, since the files in the final asm can be different, and it doesn't
// make sense to require the user to create a new batch file/shell script.
int main(int argc, char *argv[])
{
FILE *includesfile;
FILE *asmfile;
char ***readbBfile;
char path[500];
char prepend[500];
char line[500];
char asmline[500];
int bB = 2; // part of bB file
int writebBfile[17] = { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 };
int i;
int j;
while ((i = getopt(argc, argv, "i:")) != -1)
{
switch (i)
{
case '?':
path[0] = '\0';
break;
case 'i':
strcpy(path, optarg);
}
}
if ((path[strlen(path) - 1] == '\\') || (path[strlen(path) - 1] == '/'))
strcat(path, "includes/");
else
strcat(path, "/includes/");
if ((includesfile = fopen("includes.bB", "r")) == NULL) // open file
{
fprintf(stderr, "Cannot open includes.bB for reading\n");
exit(1);
}
readbBfile = (char ***) malloc(sizeof(char **) * 17);
// while (fgets(line,500,includesfile))
while (1)
{
if (fscanf(includesfile, "%s", line) == EOF)
break;
if ((!strncmp(line, "bB", 2)) && (line[2] != '.'))
{
i = (int) (line[2]) - 48;
for (j = 1; j < writebBfile[i]; ++j)
printf("%s", readbBfile[i][j]);
continue;
}
else
{
strcpy(prepend, path);
strcat(prepend, line);
if ((asmfile = fopen(line, "r")) == NULL) // try file w/o includes path
{
if ((asmfile = fopen(prepend, "r")) == NULL) // open file
{
fprintf(stderr, "Cannot open %s for reading\n", line);
exit(1);
}
}
else if (strncmp(line, "bB\0", 2))
{
fprintf(stderr, "User-defined %s found in current directory\n", line);
}
}
while (fgets(asmline, 500, asmfile))
{
if (!strncmp(asmline, "; bB.asm file is split here", 20))
{
writebBfile[bB]++;
readbBfile[bB] = (char **) malloc(sizeof(char *) * 50000);
readbBfile[bB][writebBfile[bB]] = (char *) malloc(strlen(line) + 3);
sprintf(readbBfile[bB][writebBfile[bB]], ";%s\n", line);
}
if (!writebBfile[bB])
printf("%s", asmline);
else
{
readbBfile[bB][++writebBfile[bB]] = (char *) malloc(strlen(asmline) + 3);
sprintf(readbBfile[bB][writebBfile[bB]], "%s", asmline);
}
}
fclose(asmfile);
if (writebBfile[bB])
bB++;
// if (writebBfile) fclose(bBfile);
}
}