1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000
1.2 +++ b/sourcepawn/compiler/libpawnc.c Sun Mar 30 07:00:22 2008 +0000
1.3 @@ -0,0 +1,270 @@
1.4 +/* LIBPAWNC.C
1.5 + *
1.6 + * A "glue file" for building the Pawn compiler as a DLL or shared library.
1.7 + *
1.8 + * Copyright (c) ITB CompuPhase, 2000-2006
1.9 + *
1.10 + * This software is provided "as-is", without any express or implied warranty.
1.11 + * In no event will the authors be held liable for any damages arising from
1.12 + * the use of this software.
1.13 + *
1.14 + * Permission is granted to anyone to use this software for any purpose,
1.15 + * including commercial applications, and to alter it and redistribute it
1.16 + * freely, subject to the following restrictions:
1.17 + *
1.18 + * 1. The origin of this software must not be misrepresented; you must not
1.19 + * claim that you wrote the original software. If you use this software in
1.20 + * a product, an acknowledgment in the product documentation would be
1.21 + * appreciated but is not required.
1.22 + * 2. Altered source versions must be plainly marked as such, and must not be
1.23 + * misrepresented as being the original software.
1.24 + * 3. This notice may not be removed or altered from any source distribution.
1.25 + *
1.26 + * Version: $Id$
1.27 + */
1.28 +#include <assert.h>
1.29 +#include <stdio.h>
1.30 +#include <stdlib.h>
1.31 +#include <string.h>
1.32 +#include <malloc.h>
1.33 +#include "sc.h"
1.34 +#include "memfile.h"
1.35 +
1.36 +/* pc_printf()
1.37 + * Called for general purpose "console" output. This function prints general
1.38 + * purpose messages; errors go through pc_error(). The function is modelled
1.39 + * after printf().
1.40 + */
1.41 +int pc_printf(const char *message,...)
1.42 +{
1.43 + int ret;
1.44 + va_list argptr;
1.45 +
1.46 + va_start(argptr,message);
1.47 + ret=vprintf(message,argptr);
1.48 + va_end(argptr);
1.49 +
1.50 + return ret;
1.51 +}
1.52 +
1.53 +/* pc_error()
1.54 + * Called for producing error output.
1.55 + * number the error number (as documented in the manual)
1.56 + * message a string describing the error with embedded %d and %s tokens
1.57 + * filename the name of the file currently being parsed
1.58 + * firstline the line number at which the expression started on which
1.59 + * the error was found, or -1 if there is no "starting line"
1.60 + * lastline the line number at which the error was detected
1.61 + * argptr a pointer to the first of a series of arguments (for macro
1.62 + * "va_arg")
1.63 + * Return:
1.64 + * If the function returns 0, the parser attempts to continue compilation.
1.65 + * On a non-zero return value, the parser aborts.
1.66 + */
1.67 +int pc_error(int number,char *message,char *filename,int firstline,int lastline,va_list argptr)
1.68 +{
1.69 +static char *prefix[3]={ "error", "fatal error", "warning" };
1.70 +
1.71 + if (number!=0) {
1.72 + char *pre;
1.73 + int idx;
1.74 +
1.75 + if (number < 120)
1.76 + idx = 0;
1.77 + else if (number < 200)
1.78 + idx = 1;
1.79 + else
1.80 + idx = 2;
1.81 +
1.82 + pre=prefix[idx];
1.83 + if (firstline>=0)
1.84 + fprintf(stdout,"%s(%d -- %d) : %s %03d: ",filename,firstline,lastline,pre,number);
1.85 + else
1.86 + fprintf(stdout,"%s(%d) : %s %03d: ",filename,lastline,pre,number);
1.87 + } /* if */
1.88 + vfprintf(stdout,message,argptr);
1.89 + fflush(stdout);
1.90 + return 0;
1.91 +}
1.92 +
1.93 +/* pc_opensrc()
1.94 + * Opens a source file (or include file) for reading. The "file" does not have
1.95 + * to be a physical file, one might compile from memory.
1.96 + * filename the name of the "file" to read from
1.97 + * Return:
1.98 + * The function must return a pointer, which is used as a "magic cookie" to
1.99 + * all I/O functions. When failing to open the file for reading, the
1.100 + * function must return NULL.
1.101 + * Note:
1.102 + * Several "source files" may be open at the same time. Specifically, one
1.103 + * file can be open for reading and another for writing.
1.104 + */
1.105 +void *pc_opensrc(char *filename)
1.106 +{
1.107 + return fopen(filename,"rt");
1.108 +}
1.109 +
1.110 +/* pc_createsrc()
1.111 + * Creates/overwrites a source file for writing. The "file" does not have
1.112 + * to be a physical file, one might compile from memory.
1.113 + * filename the name of the "file" to create
1.114 + * Return:
1.115 + * The function must return a pointer which is used as a "magic cookie" to
1.116 + * all I/O functions. When failing to open the file for reading, the
1.117 + * function must return NULL.
1.118 + * Note:
1.119 + * Several "source files" may be open at the same time. Specifically, one
1.120 + * file can be open for reading and another for writing.
1.121 + */
1.122 +void *pc_createsrc(char *filename)
1.123 +{
1.124 + return fopen(filename,"wt");
1.125 +}
1.126 +
1.127 +/* pc_closesrc()
1.128 + * Closes a source file (or include file). The "handle" parameter has the
1.129 + * value that pc_opensrc() returned in an earlier call.
1.130 + */
1.131 +void pc_closesrc(void *handle)
1.132 +{
1.133 + assert(handle!=NULL);
1.134 + fclose((FILE*)handle);
1.135 +}
1.136 +
1.137 +/* pc_resetsrc()
1.138 + * "position" may only hold a pointer that was previously obtained from
1.139 + * pc_getpossrc()
1.140 + */
1.141 +void pc_resetsrc(void *handle,void *position)
1.142 +{
1.143 + assert(handle!=NULL);
1.144 + fsetpos((FILE*)handle,(fpos_t *)position);
1.145 +}
1.146 +
1.147 +/* pc_readsrc()
1.148 + * Reads a single line from the source file (or up to a maximum number of
1.149 + * characters if the line in the input file is too long).
1.150 + */
1.151 +char *pc_readsrc(void *handle,unsigned char *target,int maxchars)
1.152 +{
1.153 + return fgets((char*)target,maxchars,(FILE*)handle);
1.154 +}
1.155 +
1.156 +/* pc_writesrc()
1.157 + * Writes to to the source file. There is no automatic line ending; to end a
1.158 + * line, write a "\n".
1.159 + */
1.160 +int pc_writesrc(void *handle,unsigned char *source)
1.161 +{
1.162 + return fputs((char*)source,(FILE*)handle) >= 0;
1.163 +}
1.164 +
1.165 +void *pc_getpossrc(void *handle)
1.166 +{
1.167 + static fpos_t lastpos; /* may need to have a LIFO stack of such positions */
1.168 +
1.169 + fgetpos((FILE*)handle,&lastpos);
1.170 + return &lastpos;
1.171 +}
1.172 +
1.173 +int pc_eofsrc(void *handle)
1.174 +{
1.175 + return feof((FILE*)handle);
1.176 +}
1.177 +
1.178 +/* should return a pointer, which is used as a "magic cookie" to all I/O
1.179 + * functions; return NULL for failure
1.180 + */
1.181 +void *pc_openasm(char *filename)
1.182 +{
1.183 + #if defined __MSDOS__ || defined SC_LIGHT
1.184 + return fopen(filename,"w+t");
1.185 + #else
1.186 + return mfcreate(filename);
1.187 + #endif
1.188 +}
1.189 +
1.190 +void pc_closeasm(void *handle, int deletefile)
1.191 +{
1.192 + #if defined __MSDOS__ || defined SC_LIGHT
1.193 + if (handle!=NULL)
1.194 + fclose((FILE*)handle);
1.195 + if (deletefile)
1.196 + remove(outfname);
1.197 + #else
1.198 + if (handle!=NULL) {
1.199 + if (!deletefile)
1.200 + mfdump((MEMFILE*)handle);
1.201 + mfclose((MEMFILE*)handle);
1.202 + } /* if */
1.203 + #endif
1.204 +}
1.205 +
1.206 +void pc_resetasm(void *handle)
1.207 +{
1.208 + assert(handle!=NULL);
1.209 + #if defined __MSDOS__ || defined SC_LIGHT
1.210 + fflush((FILE*)handle);
1.211 + fseek((FILE*)handle,0,SEEK_SET);
1.212 + #else
1.213 + mfseek((MEMFILE*)handle,0,SEEK_SET);
1.214 + #endif
1.215 +}
1.216 +
1.217 +int pc_writeasm(void *handle,char *string)
1.218 +{
1.219 + #if defined __MSDOS__ || defined SC_LIGHT
1.220 + return fputs(string,(FILE*)handle) >= 0;
1.221 + #else
1.222 + return mfputs((MEMFILE*)handle,string);
1.223 + #endif
1.224 +}
1.225 +
1.226 +char *pc_readasm(void *handle, char *string, int maxchars)
1.227 +{
1.228 + #if defined __MSDOS__ || defined SC_LIGHT
1.229 + return fgets(string,maxchars,(FILE*)handle);
1.230 + #else
1.231 + return mfgets((MEMFILE*)handle,string,maxchars);
1.232 + #endif
1.233 +}
1.234 +
1.235 +extern memfile_t *bin_file;
1.236 +
1.237 +/* Should return a pointer, which is used as a "magic cookie" to all I/O
1.238 + * functions; return NULL for failure.
1.239 + */
1.240 +void *pc_openbin(char *filename)
1.241 +{
1.242 + return memfile_creat(filename, 1);
1.243 +}
1.244 +
1.245 +void pc_closebin(void *handle,int deletefile)
1.246 +{
1.247 + if (deletefile)
1.248 + {
1.249 + memfile_destroy((memfile_t *)handle);
1.250 + bin_file = NULL;
1.251 + } else {
1.252 + bin_file = (memfile_t *)handle;
1.253 + }
1.254 +}
1.255 +
1.256 +/* pc_resetbin()
1.257 + * Can seek to any location in the file.
1.258 + * The offset is always from the start of the file.
1.259 + */
1.260 +void pc_resetbin(void *handle,long offset)
1.261 +{
1.262 + memfile_seek((memfile_t *)handle, offset);
1.263 +}
1.264 +
1.265 +int pc_writebin(void *handle,void *buffer,int size)
1.266 +{
1.267 + return memfile_write((memfile_t *)handle, buffer, size);
1.268 +}
1.269 +
1.270 +long pc_lengthbin(void *handle)
1.271 +{
1.272 + return memfile_tell((memfile_t *)handle);
1.273 +}