|
ds@1952
|
1 |
/* LIBPAWNC.C |
|
ds@1952
|
2 |
* |
|
ds@1952
|
3 |
* A "glue file" for building the Pawn compiler as a DLL or shared library. |
|
ds@1952
|
4 |
* |
|
ds@1952
|
5 |
* Copyright (c) ITB CompuPhase, 2000-2006 |
|
ds@1952
|
6 |
* |
|
ds@1952
|
7 |
* This software is provided "as-is", without any express or implied warranty. |
|
ds@1952
|
8 |
* In no event will the authors be held liable for any damages arising from |
|
ds@1952
|
9 |
* the use of this software. |
|
ds@1952
|
10 |
* |
|
ds@1952
|
11 |
* Permission is granted to anyone to use this software for any purpose, |
|
ds@1952
|
12 |
* including commercial applications, and to alter it and redistribute it |
|
ds@1952
|
13 |
* freely, subject to the following restrictions: |
|
ds@1952
|
14 |
* |
|
ds@1952
|
15 |
* 1. The origin of this software must not be misrepresented; you must not |
|
ds@1952
|
16 |
* claim that you wrote the original software. If you use this software in |
|
ds@1952
|
17 |
* a product, an acknowledgment in the product documentation would be |
|
ds@1952
|
18 |
* appreciated but is not required. |
|
ds@1952
|
19 |
* 2. Altered source versions must be plainly marked as such, and must not be |
|
ds@1952
|
20 |
* misrepresented as being the original software. |
|
ds@1952
|
21 |
* 3. This notice may not be removed or altered from any source distribution. |
|
ds@1952
|
22 |
* |
|
ds@1952
|
23 |
* Version: $Id$ |
|
ds@1952
|
24 |
*/ |
|
ds@1952
|
25 |
#include <assert.h> |
|
ds@1952
|
26 |
#include <stdio.h> |
|
ds@1952
|
27 |
#include <stdlib.h> |
|
ds@1952
|
28 |
#include <string.h> |
|
ds@1952
|
29 |
#include "sc.h" |
|
ds@1952
|
30 |
#include "memfile.h" |
|
ds@1952
|
31 |
|
|
ds@1952
|
32 |
/* pc_printf() |
|
ds@1952
|
33 |
* Called for general purpose "console" output. This function prints general |
|
ds@1952
|
34 |
* purpose messages; errors go through pc_error(). The function is modelled |
|
ds@1952
|
35 |
* after printf(). |
|
ds@1952
|
36 |
*/ |
|
ds@1952
|
37 |
int pc_printf(const char *message,...) |
|
ds@1952
|
38 |
{ |
|
ds@1952
|
39 |
int ret; |
|
ds@1952
|
40 |
va_list argptr; |
|
ds@1952
|
41 |
|
|
ds@1952
|
42 |
va_start(argptr,message); |
|
ds@1952
|
43 |
ret=vprintf(message,argptr); |
|
ds@1952
|
44 |
va_end(argptr); |
|
ds@1952
|
45 |
|
|
ds@1952
|
46 |
return ret; |
|
ds@1952
|
47 |
} |
|
ds@1952
|
48 |
|
|
ds@1952
|
49 |
/* pc_error() |
|
ds@1952
|
50 |
* Called for producing error output. |
|
ds@1952
|
51 |
* number the error number (as documented in the manual) |
|
ds@1952
|
52 |
* message a string describing the error with embedded %d and %s tokens |
|
ds@1952
|
53 |
* filename the name of the file currently being parsed |
|
ds@1952
|
54 |
* firstline the line number at which the expression started on which |
|
ds@1952
|
55 |
* the error was found, or -1 if there is no "starting line" |
|
ds@1952
|
56 |
* lastline the line number at which the error was detected |
|
ds@1952
|
57 |
* argptr a pointer to the first of a series of arguments (for macro |
|
ds@1952
|
58 |
* "va_arg") |
|
ds@1952
|
59 |
* Return: |
|
ds@1952
|
60 |
* If the function returns 0, the parser attempts to continue compilation. |
|
ds@1952
|
61 |
* On a non-zero return value, the parser aborts. |
|
ds@1952
|
62 |
*/ |
|
ds@1952
|
63 |
int pc_error(int number,char *message,char *filename,int firstline,int lastline,va_list argptr) |
|
ds@1952
|
64 |
{ |
|
ds@1952
|
65 |
static char *prefix[3]={ "error", "fatal error", "warning" }; |
|
ds@1952
|
66 |
|
|
ds@1952
|
67 |
if (number!=0) { |
|
ds@1952
|
68 |
char *pre; |
|
ds@1952
|
69 |
int idx; |
|
ds@1952
|
70 |
|
|
ds@1952
|
71 |
if (number < 120) |
|
ds@1952
|
72 |
idx = 0; |
|
ds@1952
|
73 |
else if (number < 200) |
|
ds@1952
|
74 |
idx = 1; |
|
ds@1952
|
75 |
else |
|
ds@1952
|
76 |
idx = 2; |
|
ds@1952
|
77 |
|
|
ds@1952
|
78 |
pre=prefix[idx]; |
|
ds@1952
|
79 |
if (firstline>=0) |
|
ds@1952
|
80 |
fprintf(stdout,"%s(%d -- %d) : %s %03d: ",filename,firstline,lastline,pre,number); |
|
ds@1952
|
81 |
else |
|
ds@1952
|
82 |
fprintf(stdout,"%s(%d) : %s %03d: ",filename,lastline,pre,number); |
|
ds@1952
|
83 |
} /* if */ |
|
ds@1952
|
84 |
vfprintf(stdout,message,argptr); |
|
ds@1952
|
85 |
fflush(stdout); |
|
ds@1952
|
86 |
return 0; |
|
ds@1952
|
87 |
} |
|
ds@1952
|
88 |
|
|
ds@1952
|
89 |
/* pc_opensrc() |
|
ds@1952
|
90 |
* Opens a source file (or include file) for reading. The "file" does not have |
|
ds@1952
|
91 |
* to be a physical file, one might compile from memory. |
|
ds@1952
|
92 |
* filename the name of the "file" to read from |
|
ds@1952
|
93 |
* Return: |
|
ds@1952
|
94 |
* The function must return a pointer, which is used as a "magic cookie" to |
|
ds@1952
|
95 |
* all I/O functions. When failing to open the file for reading, the |
|
ds@1952
|
96 |
* function must return NULL. |
|
ds@1952
|
97 |
* Note: |
|
ds@1952
|
98 |
* Several "source files" may be open at the same time. Specifically, one |
|
ds@1952
|
99 |
* file can be open for reading and another for writing. |
|
ds@1952
|
100 |
*/ |
|
ds@1952
|
101 |
void *pc_opensrc(char *filename) |
|
ds@1952
|
102 |
{ |
|
ds@1952
|
103 |
return fopen(filename,"rt"); |
|
ds@1952
|
104 |
} |
|
ds@1952
|
105 |
|
|
ds@1952
|
106 |
/* pc_createsrc() |
|
ds@1952
|
107 |
* Creates/overwrites a source file for writing. The "file" does not have |
|
ds@1952
|
108 |
* to be a physical file, one might compile from memory. |
|
ds@1952
|
109 |
* filename the name of the "file" to create |
|
ds@1952
|
110 |
* Return: |
|
ds@1952
|
111 |
* The function must return a pointer which is used as a "magic cookie" to |
|
ds@1952
|
112 |
* all I/O functions. When failing to open the file for reading, the |
|
ds@1952
|
113 |
* function must return NULL. |
|
ds@1952
|
114 |
* Note: |
|
ds@1952
|
115 |
* Several "source files" may be open at the same time. Specifically, one |
|
ds@1952
|
116 |
* file can be open for reading and another for writing. |
|
ds@1952
|
117 |
*/ |
|
ds@1952
|
118 |
void *pc_createsrc(char *filename) |
|
ds@1952
|
119 |
{ |
|
ds@1952
|
120 |
return fopen(filename,"wt"); |
|
ds@1952
|
121 |
} |
|
ds@1952
|
122 |
|
|
ds@1952
|
123 |
/* pc_closesrc() |
|
ds@1952
|
124 |
* Closes a source file (or include file). The "handle" parameter has the |
|
ds@1952
|
125 |
* value that pc_opensrc() returned in an earlier call. |
|
ds@1952
|
126 |
*/ |
|
ds@1952
|
127 |
void pc_closesrc(void *handle) |
|
ds@1952
|
128 |
{ |
|
ds@1952
|
129 |
assert(handle!=NULL); |
|
ds@1952
|
130 |
fclose((FILE*)handle); |
|
ds@1952
|
131 |
} |
|
ds@1952
|
132 |
|
|
ds@1952
|
133 |
/* pc_resetsrc() |
|
ds@1952
|
134 |
* "position" may only hold a pointer that was previously obtained from |
|
ds@1952
|
135 |
* pc_getpossrc() |
|
ds@1952
|
136 |
*/ |
|
ds@1952
|
137 |
void pc_resetsrc(void *handle,void *position) |
|
ds@1952
|
138 |
{ |
|
ds@1952
|
139 |
assert(handle!=NULL); |
|
ds@1952
|
140 |
fsetpos((FILE*)handle,(fpos_t *)position); |
|
ds@1952
|
141 |
} |
|
ds@1952
|
142 |
|
|
ds@1952
|
143 |
/* pc_readsrc() |
|
ds@1952
|
144 |
* Reads a single line from the source file (or up to a maximum number of |
|
ds@1952
|
145 |
* characters if the line in the input file is too long). |
|
ds@1952
|
146 |
*/ |
|
ds@1952
|
147 |
char *pc_readsrc(void *handle,unsigned char *target,int maxchars) |
|
ds@1952
|
148 |
{ |
|
ds@1952
|
149 |
return fgets((char*)target,maxchars,(FILE*)handle); |
|
ds@1952
|
150 |
} |
|
ds@1952
|
151 |
|
|
ds@1952
|
152 |
/* pc_writesrc() |
|
ds@1952
|
153 |
* Writes to to the source file. There is no automatic line ending; to end a |
|
ds@1952
|
154 |
* line, write a "\n". |
|
ds@1952
|
155 |
*/ |
|
ds@1952
|
156 |
int pc_writesrc(void *handle,unsigned char *source) |
|
ds@1952
|
157 |
{ |
|
ds@1952
|
158 |
return fputs((char*)source,(FILE*)handle) >= 0; |
|
ds@1952
|
159 |
} |
|
ds@1952
|
160 |
|
|
ds@1952
|
161 |
void *pc_getpossrc(void *handle) |
|
ds@1952
|
162 |
{ |
|
ds@1952
|
163 |
static fpos_t lastpos; /* may need to have a LIFO stack of such positions */ |
|
ds@1952
|
164 |
|
|
ds@1952
|
165 |
fgetpos((FILE*)handle,&lastpos); |
|
ds@1952
|
166 |
return &lastpos; |
|
ds@1952
|
167 |
} |
|
ds@1952
|
168 |
|
|
ds@1952
|
169 |
int pc_eofsrc(void *handle) |
|
ds@1952
|
170 |
{ |
|
ds@1952
|
171 |
return feof((FILE*)handle); |
|
ds@1952
|
172 |
} |
|
ds@1952
|
173 |
|
|
ds@1952
|
174 |
/* should return a pointer, which is used as a "magic cookie" to all I/O |
|
ds@1952
|
175 |
* functions; return NULL for failure |
|
ds@1952
|
176 |
*/ |
|
ds@1952
|
177 |
void *pc_openasm(char *filename) |
|
ds@1952
|
178 |
{ |
|
ds@1952
|
179 |
#if defined __MSDOS__ || defined SC_LIGHT |
|
ds@1952
|
180 |
return fopen(filename,"w+t"); |
|
ds@1952
|
181 |
#else |
|
ds@1952
|
182 |
return mfcreate(filename); |
|
ds@1952
|
183 |
#endif |
|
ds@1952
|
184 |
} |
|
ds@1952
|
185 |
|
|
ds@1952
|
186 |
void pc_closeasm(void *handle, int deletefile) |
|
ds@1952
|
187 |
{ |
|
ds@1952
|
188 |
#if defined __MSDOS__ || defined SC_LIGHT |
|
ds@1952
|
189 |
if (handle!=NULL) |
|
ds@1952
|
190 |
fclose((FILE*)handle); |
|
ds@1952
|
191 |
if (deletefile) |
|
ds@1952
|
192 |
remove(outfname); |
|
ds@1952
|
193 |
#else |
|
ds@1952
|
194 |
if (handle!=NULL) { |
|
ds@1952
|
195 |
if (!deletefile) |
|
ds@1952
|
196 |
mfdump((MEMFILE*)handle); |
|
ds@1952
|
197 |
mfclose((MEMFILE*)handle); |
|
ds@1952
|
198 |
} /* if */ |
|
ds@1952
|
199 |
#endif |
|
ds@1952
|
200 |
} |
|
ds@1952
|
201 |
|
|
ds@1952
|
202 |
void pc_resetasm(void *handle) |
|
ds@1952
|
203 |
{ |
|
ds@1952
|
204 |
assert(handle!=NULL); |
|
ds@1952
|
205 |
#if defined __MSDOS__ || defined SC_LIGHT |
|
ds@1952
|
206 |
fflush((FILE*)handle); |
|
ds@1952
|
207 |
fseek((FILE*)handle,0,SEEK_SET); |
|
ds@1952
|
208 |
#else |
|
ds@1952
|
209 |
mfseek((MEMFILE*)handle,0,SEEK_SET); |
|
ds@1952
|
210 |
#endif |
|
ds@1952
|
211 |
} |
|
ds@1952
|
212 |
|
|
ds@1952
|
213 |
int pc_writeasm(void *handle,char *string) |
|
ds@1952
|
214 |
{ |
|
ds@1952
|
215 |
#if defined __MSDOS__ || defined SC_LIGHT |
|
ds@1952
|
216 |
return fputs(string,(FILE*)handle) >= 0; |
|
ds@1952
|
217 |
#else |
|
ds@1952
|
218 |
return mfputs((MEMFILE*)handle,string); |
|
ds@1952
|
219 |
#endif |
|
ds@1952
|
220 |
} |
|
ds@1952
|
221 |
|
|
ds@1952
|
222 |
char *pc_readasm(void *handle, char *string, int maxchars) |
|
ds@1952
|
223 |
{ |
|
ds@1952
|
224 |
#if defined __MSDOS__ || defined SC_LIGHT |
|
ds@1952
|
225 |
return fgets(string,maxchars,(FILE*)handle); |
|
ds@1952
|
226 |
#else |
|
ds@1952
|
227 |
return mfgets((MEMFILE*)handle,string,maxchars); |
|
ds@1952
|
228 |
#endif |
|
ds@1952
|
229 |
} |
|
ds@1952
|
230 |
|
|
ds@1952
|
231 |
extern memfile_t *bin_file; |
|
ds@1952
|
232 |
|
|
ds@1952
|
233 |
/* Should return a pointer, which is used as a "magic cookie" to all I/O |
|
ds@1952
|
234 |
* functions; return NULL for failure. |
|
ds@1952
|
235 |
*/ |
|
ds@1952
|
236 |
void *pc_openbin(char *filename) |
|
ds@1952
|
237 |
{ |
|
ds@1952
|
238 |
return memfile_creat(filename, 1); |
|
ds@1952
|
239 |
} |
|
ds@1952
|
240 |
|
|
ds@1952
|
241 |
void pc_closebin(void *handle,int deletefile) |
|
ds@1952
|
242 |
{ |
|
ds@1952
|
243 |
if (deletefile) |
|
ds@1952
|
244 |
{ |
|
ds@1952
|
245 |
memfile_destroy((memfile_t *)handle); |
|
ds@1952
|
246 |
bin_file = NULL; |
|
ds@1952
|
247 |
} else { |
|
ds@1952
|
248 |
bin_file = (memfile_t *)handle; |
|
ds@1952
|
249 |
} |
|
ds@1952
|
250 |
} |
|
ds@1952
|
251 |
|
|
ds@1952
|
252 |
/* pc_resetbin() |
|
ds@1952
|
253 |
* Can seek to any location in the file. |
|
ds@1952
|
254 |
* The offset is always from the start of the file. |
|
ds@1952
|
255 |
*/ |
|
ds@1952
|
256 |
void pc_resetbin(void *handle,long offset) |
|
ds@1952
|
257 |
{ |
|
ds@1952
|
258 |
memfile_seek((memfile_t *)handle, offset); |
|
ds@1952
|
259 |
} |
|
ds@1952
|
260 |
|
|
ds@1952
|
261 |
int pc_writebin(void *handle,void *buffer,int size) |
|
ds@1952
|
262 |
{ |
|
ds@1952
|
263 |
return memfile_write((memfile_t *)handle, buffer, size); |
|
ds@1952
|
264 |
} |
|
ds@1952
|
265 |
|
|
ds@1952
|
266 |
long pc_lengthbin(void *handle) |
|
ds@1952
|
267 |
{ |
|
ds@1952
|
268 |
return memfile_tell((memfile_t *)handle); |
|
ds@1952
|
269 |
} |