1.1 --- a/compiler-init/libpawnc.c Sat Jul 15 01:36:48 2006 +0000
1.2 +++ /dev/null Thu Jan 01 00:00:00 1970 +0000
1.3 @@ -1,257 +0,0 @@
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: libsc.c 3114 2005-03-17 14:48:29Z thiadmer $
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 -
1.74 - pre=prefix[number/100];
1.75 - if (firstline>=0)
1.76 - fprintf(stderr,"%s(%d -- %d) : %s %03d: ",filename,firstline,lastline,pre,number);
1.77 - else
1.78 - fprintf(stderr,"%s(%d) : %s %03d: ",filename,lastline,pre,number);
1.79 - } /* if */
1.80 - vfprintf(stderr,message,argptr);
1.81 - fflush(stderr);
1.82 - return 0;
1.83 -}
1.84 -
1.85 -/* pc_opensrc()
1.86 - * Opens a source file (or include file) for reading. The "file" does not have
1.87 - * to be a physical file, one might compile from memory.
1.88 - * filename the name of the "file" to read from
1.89 - * Return:
1.90 - * The function must return a pointer, which is used as a "magic cookie" to
1.91 - * all I/O functions. When failing to open the file for reading, the
1.92 - * function must return NULL.
1.93 - * Note:
1.94 - * Several "source files" may be open at the same time. Specifically, one
1.95 - * file can be open for reading and another for writing.
1.96 - */
1.97 -void *pc_opensrc(char *filename)
1.98 -{
1.99 - return fopen(filename,"rt");
1.100 -}
1.101 -
1.102 -/* pc_createsrc()
1.103 - * Creates/overwrites a source file for writing. The "file" does not have
1.104 - * to be a physical file, one might compile from memory.
1.105 - * filename the name of the "file" to create
1.106 - * Return:
1.107 - * The function must return a pointer which is used as a "magic cookie" to
1.108 - * all I/O functions. When failing to open the file for reading, the
1.109 - * function must return NULL.
1.110 - * Note:
1.111 - * Several "source files" may be open at the same time. Specifically, one
1.112 - * file can be open for reading and another for writing.
1.113 - */
1.114 -void *pc_createsrc(char *filename)
1.115 -{
1.116 - return fopen(filename,"wt");
1.117 -}
1.118 -
1.119 -/* pc_closesrc()
1.120 - * Closes a source file (or include file). The "handle" parameter has the
1.121 - * value that pc_opensrc() returned in an earlier call.
1.122 - */
1.123 -void pc_closesrc(void *handle)
1.124 -{
1.125 - assert(handle!=NULL);
1.126 - fclose((FILE*)handle);
1.127 -}
1.128 -
1.129 -/* pc_resetsrc()
1.130 - * "position" may only hold a pointer that was previously obtained from
1.131 - * pc_getpossrc()
1.132 - */
1.133 -void pc_resetsrc(void *handle,void *position)
1.134 -{
1.135 - assert(handle!=NULL);
1.136 - fsetpos((FILE*)handle,(fpos_t *)position);
1.137 -}
1.138 -
1.139 -/* pc_readsrc()
1.140 - * Reads a single line from the source file (or up to a maximum number of
1.141 - * characters if the line in the input file is too long).
1.142 - */
1.143 -char *pc_readsrc(void *handle,unsigned char *target,int maxchars)
1.144 -{
1.145 - return fgets((char*)target,maxchars,(FILE*)handle);
1.146 -}
1.147 -
1.148 -/* pc_writesrc()
1.149 - * Writes to to the source file. There is no automatic line ending; to end a
1.150 - * line, write a "\n".
1.151 - */
1.152 -int pc_writesrc(void *handle,unsigned char *source)
1.153 -{
1.154 - return fputs((char*)source,(FILE*)handle) >= 0;
1.155 -}
1.156 -
1.157 -void *pc_getpossrc(void *handle)
1.158 -{
1.159 - static fpos_t lastpos; /* may need to have a LIFO stack of such positions */
1.160 -
1.161 - fgetpos((FILE*)handle,&lastpos);
1.162 - return &lastpos;
1.163 -}
1.164 -
1.165 -int pc_eofsrc(void *handle)
1.166 -{
1.167 - return feof((FILE*)handle);
1.168 -}
1.169 -
1.170 -/* should return a pointer, which is used as a "magic cookie" to all I/O
1.171 - * functions; return NULL for failure
1.172 - */
1.173 -void *pc_openasm(char *filename)
1.174 -{
1.175 - #if defined __MSDOS__ || defined SC_LIGHT
1.176 - return fopen(filename,"w+t");
1.177 - #else
1.178 - return mfcreate(filename);
1.179 - #endif
1.180 -}
1.181 -
1.182 -void pc_closeasm(void *handle, int deletefile)
1.183 -{
1.184 - #if defined __MSDOS__ || defined SC_LIGHT
1.185 - if (handle!=NULL)
1.186 - fclose((FILE*)handle);
1.187 - if (deletefile)
1.188 - remove(outfname);
1.189 - #else
1.190 - if (handle!=NULL) {
1.191 - if (!deletefile)
1.192 - mfdump((MEMFILE*)handle);
1.193 - mfclose((MEMFILE*)handle);
1.194 - } /* if */
1.195 - #endif
1.196 -}
1.197 -
1.198 -void pc_resetasm(void *handle)
1.199 -{
1.200 - assert(handle!=NULL);
1.201 - #if defined __MSDOS__ || defined SC_LIGHT
1.202 - fflush((FILE*)handle);
1.203 - fseek((FILE*)handle,0,SEEK_SET);
1.204 - #else
1.205 - mfseek((MEMFILE*)handle,0,SEEK_SET);
1.206 - #endif
1.207 -}
1.208 -
1.209 -int pc_writeasm(void *handle,char *string)
1.210 -{
1.211 - #if defined __MSDOS__ || defined SC_LIGHT
1.212 - return fputs(string,(FILE*)handle) >= 0;
1.213 - #else
1.214 - return mfputs((MEMFILE*)handle,string);
1.215 - #endif
1.216 -}
1.217 -
1.218 -char *pc_readasm(void *handle, char *string, int maxchars)
1.219 -{
1.220 - #if defined __MSDOS__ || defined SC_LIGHT
1.221 - return fgets(string,maxchars,(FILE*)handle);
1.222 - #else
1.223 - return mfgets((MEMFILE*)handle,string,maxchars);
1.224 - #endif
1.225 -}
1.226 -
1.227 -/* Should return a pointer, which is used as a "magic cookie" to all I/O
1.228 - * functions; return NULL for failure.
1.229 - */
1.230 -void *pc_openbin(char *filename)
1.231 -{
1.232 - return memfile_creat(filename, 1);
1.233 -}
1.234 -
1.235 -void pc_closebin(void *handle,int deletefile)
1.236 -{
1.237 - if (deletefile)
1.238 - {
1.239 - memfile_destroy((memfile_t *)handle);
1.240 - }
1.241 -}
1.242 -
1.243 -/* pc_resetbin()
1.244 - * Can seek to any location in the file.
1.245 - * The offset is always from the start of the file.
1.246 - */
1.247 -void pc_resetbin(void *handle,long offset)
1.248 -{
1.249 - memfile_seek((memfile_t *)handle, offset);
1.250 -}
1.251 -
1.252 -int pc_writebin(void *handle,void *buffer,int size)
1.253 -{
1.254 - return memfile_write((memfile_t *)handle, buffer, size);
1.255 -}
1.256 -
1.257 -long pc_lengthbin(void *handle)
1.258 -{
1.259 - return memfile_tell((memfile_t *)handle);
1.260 -}