-
Notifications
You must be signed in to change notification settings - Fork 50
/
directives_include.cpp
179 lines (144 loc) · 3.53 KB
/
directives_include.cpp
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
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
/**
* naken_asm assembler.
* Author: Michael Kohn
* Email: [email protected]
* Web: https://www.mikekohn.net/
* License: GPLv3
*
* Copyright 2010-2023 by Michael Kohn
*
*/
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "common/assembler.h"
#include "common/directives_include.h"
#include "common/tokens.h"
#include "common/print_error.h"
int include_add_path(AsmContext *asm_context, const char *paths)
{
int ptr = 0;
int n = 0;
char *s;
s = asm_context->include_path;
while (!(s[ptr] == 0 && s[ptr+1] == 0)) { ptr++; }
if (ptr != 0) ptr++;
while (paths[n] != 0)
{
if (paths[n] == ':')
{
n++;
s[ptr++] = 0;
}
else
{
s[ptr++] = paths[n++];
}
if (ptr >= INCLUDE_PATH_LEN-1) return -1;
}
return 0;
}
int binfile_parse(AsmContext *asm_context)
{
FILE *in;
char token[TOKENLEN];
uint8_t buffer[8192];
//int token_type;
int len;
int n;
if (asm_context->segment == SEGMENT_BSS)
{
printf("Error: .bss segment doesn't support initialized data at %s:%d\n",
asm_context->tokens.filename,
asm_context->tokens.line);
return -1;
}
tokens_get(asm_context, token, TOKENLEN);
in = fopen(token, "rb");
if (in == NULL)
{
printf("Cannot open binfile file '%s' at %s:%d\n",
token,
asm_context->tokens.filename,
asm_context->tokens.line);
return -1;
}
while (true)
{
len = fread(buffer, 1, sizeof(buffer), in);
if (len <= 0) { break; }
for (n = 0; n < len; n++)
{
asm_context->memory_write_inc(buffer[n], DL_DATA);
}
asm_context->data_count += len;
}
fclose(in);
return 0;
}
int include_parse(AsmContext *asm_context)
{
char token[TOKENLEN];
//int token_type;
const char *oldname;
int oldline;
FILE *oldfp;
uint8_t write_list_file;
int ret;
tokens_get(asm_context, token, TOKENLEN);
#ifdef DEBUG
printf("including file %s.\n", token);
#endif
write_list_file = asm_context->write_list_file;
asm_context->write_list_file = 0;
oldfp = asm_context->tokens.in;
oldname = asm_context->tokens.filename;
if (tokens_open_file(asm_context, token) != 0)
{
int ptr = 0;
char *s = asm_context->include_path;
char filename[8192];
while (1)
{
if (s[ptr] == 0) { break; }
if (strlen(token) + strlen(s + ptr) < 1022)
{
snprintf(filename, sizeof(filename), "%s/%s", s + ptr, token);
#ifdef DEBUG
printf("Trying %s\n", filename);
#endif
if (tokens_open_file(asm_context, filename) == 0) { break; }
if (asm_context->cpu_list_index != -1)
{
snprintf(filename, sizeof(filename), "%s/%s/%s",
s + ptr, cpu_list[asm_context->cpu_list_index].name, token);
#ifdef DEBUG
printf("Trying %s\n", filename);
#endif
if (tokens_open_file(asm_context, filename) == 0) { break; }
}
}
while (s[ptr] != 0) { ptr++; }
ptr++;
}
}
if (asm_context->tokens.in == NULL)
{
printf("Cannot open include file '%s' at %s:%d\n",
token, asm_context->tokens.filename, asm_context->tokens.line);
ret = -1;
}
else
{
oldline = asm_context->tokens.line;
asm_context->tokens.filename = token;
asm_context->tokens.line = 1;
ret = assemble(asm_context);
asm_context->tokens.line = oldline;
}
if (asm_context->tokens.in != NULL) { fclose(asm_context->tokens.in); }
asm_context->tokens.filename = oldname;
asm_context->tokens.in = oldfp;
asm_context->write_list_file = write_list_file;
return ret;
}