3 * A "glue file" for building the Pawn compiler as a DLL or shared library.
5 * Copyright (c) ITB CompuPhase, 2000-2006
7 * This software is provided "as-is", without any express or implied warranty.
8 * In no event will the authors be held liable for any damages arising from
9 * the use of this software.
11 * Permission is granted to anyone to use this software for any purpose,
12 * including commercial applications, and to alter it and redistribute it
13 * freely, subject to the following restrictions:
15 * 1. The origin of this software must not be misrepresented; you must not
16 * claim that you wrote the original software. If you use this software in
17 * a product, an acknowledgment in the product documentation would be
18 * appreciated but is not required.
19 * 2. Altered source versions must be plainly marked as such, and must not be
20 * misrepresented as being the original software.
21 * 3. This notice may not be removed or altered from any source distribution.
34 * Called for general purpose "console" output. This function prints general
35 * purpose messages; errors go through pc_error(). The function is modelled
38 int pc_printf(const char *message,...)
43 va_start(argptr,message);
44 ret=vprintf(message,argptr);
51 * Called for producing error output.
52 * number the error number (as documented in the manual)
53 * message a string describing the error with embedded %d and %s tokens
54 * filename the name of the file currently being parsed
55 * firstline the line number at which the expression started on which
56 * the error was found, or -1 if there is no "starting line"
57 * lastline the line number at which the error was detected
58 * argptr a pointer to the first of a series of arguments (for macro
61 * If the function returns 0, the parser attempts to continue compilation.
62 * On a non-zero return value, the parser aborts.
64 int pc_error(int number,char *message,char *filename,int firstline,int lastline,va_list argptr)
66 static char *prefix[3]={ "error", "fatal error", "warning" };
71 pre=prefix[number/100];
73 fprintf(stderr,"%s(%d -- %d) : %s %03d: ",filename,firstline,lastline,pre,number);
75 fprintf(stderr,"%s(%d) : %s %03d: ",filename,lastline,pre,number);
77 vfprintf(stderr,message,argptr);
83 * Opens a source file (or include file) for reading. The "file" does not have
84 * to be a physical file, one might compile from memory.
85 * filename the name of the "file" to read from
87 * The function must return a pointer, which is used as a "magic cookie" to
88 * all I/O functions. When failing to open the file for reading, the
89 * function must return NULL.
91 * Several "source files" may be open at the same time. Specifically, one
92 * file can be open for reading and another for writing.
94 void *pc_opensrc(char *filename)
96 return fopen(filename,"rt");
100 * Creates/overwrites a source file for writing. The "file" does not have
101 * to be a physical file, one might compile from memory.
102 * filename the name of the "file" to create
104 * The function must return a pointer which is used as a "magic cookie" to
105 * all I/O functions. When failing to open the file for reading, the
106 * function must return NULL.
108 * Several "source files" may be open at the same time. Specifically, one
109 * file can be open for reading and another for writing.
111 void *pc_createsrc(char *filename)
113 return fopen(filename,"wt");
117 * Closes a source file (or include file). The "handle" parameter has the
118 * value that pc_opensrc() returned in an earlier call.
120 void pc_closesrc(void *handle)
122 assert(handle!=NULL);
123 fclose((FILE*)handle);
127 * "position" may only hold a pointer that was previously obtained from
130 void pc_resetsrc(void *handle,void *position)
132 assert(handle!=NULL);
133 fsetpos((FILE*)handle,(fpos_t *)position);
137 * Reads a single line from the source file (or up to a maximum number of
138 * characters if the line in the input file is too long).
140 char *pc_readsrc(void *handle,unsigned char *target,int maxchars)
142 return fgets((char*)target,maxchars,(FILE*)handle);
146 * Writes to to the source file. There is no automatic line ending; to end a
147 * line, write a "\n".
149 int pc_writesrc(void *handle,unsigned char *source)
151 return fputs((char*)source,(FILE*)handle) >= 0;
154 void *pc_getpossrc(void *handle)
156 static fpos_t lastpos; /* may need to have a LIFO stack of such positions */
158 fgetpos((FILE*)handle,&lastpos);
162 int pc_eofsrc(void *handle)
164 return feof((FILE*)handle);
167 /* should return a pointer, which is used as a "magic cookie" to all I/O
168 * functions; return NULL for failure
170 void *pc_openasm(char *filename)
172 #if defined __MSDOS__ || defined SC_LIGHT
173 return fopen(filename,"w+t");
175 return mfcreate(filename);
179 void pc_closeasm(void *handle, int deletefile)
181 #if defined __MSDOS__ || defined SC_LIGHT
183 fclose((FILE*)handle);
189 mfdump((MEMFILE*)handle);
190 mfclose((MEMFILE*)handle);
195 void pc_resetasm(void *handle)
197 assert(handle!=NULL);
198 #if defined __MSDOS__ || defined SC_LIGHT
199 fflush((FILE*)handle);
200 fseek((FILE*)handle,0,SEEK_SET);
202 mfseek((MEMFILE*)handle,0,SEEK_SET);
206 int pc_writeasm(void *handle,char *string)
208 #if defined __MSDOS__ || defined SC_LIGHT
209 return fputs(string,(FILE*)handle) >= 0;
211 return mfputs((MEMFILE*)handle,string);
215 char *pc_readasm(void *handle, char *string, int maxchars)
217 #if defined __MSDOS__ || defined SC_LIGHT
218 return fgets(string,maxchars,(FILE*)handle);
220 return mfgets((MEMFILE*)handle,string,maxchars);
224 extern memfile_t *bin_file;
226 /* Should return a pointer, which is used as a "magic cookie" to all I/O
227 * functions; return NULL for failure.
229 void *pc_openbin(char *filename)
231 return memfile_creat(filename, 1);
234 void pc_closebin(void *handle,int deletefile)
238 memfile_destroy((memfile_t *)handle);
241 bin_file = (memfile_t *)handle;
246 * Can seek to any location in the file.
247 * The offset is always from the start of the file.
249 void pc_resetbin(void *handle,long offset)
251 memfile_seek((memfile_t *)handle, offset);
254 int pc_writebin(void *handle,void *buffer,int size)
256 return memfile_write((memfile_t *)handle, buffer, size);
259 long pc_lengthbin(void *handle)
261 return memfile_tell((memfile_t *)handle);