sourcepawn/compiler/libpawnc.c
author David Anderson <dvander@alliedmods.net>
Thu Mar 15 20:44:23 2007 +0000 (2007-03-15)
changeset 619 6934bf39171e
parent 365 d830b2b0414f
child 844 cca8444ebd11
permissions -rw-r--r--
random touchups that might not be so great
     1 /*	LIBPAWNC.C
     2  *
     3  *	A "glue file" for building the Pawn compiler as a DLL or shared library.
     4  *
     5  *	Copyright (c) ITB CompuPhase, 2000-2006
     6  *
     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.
    10  *
    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:
    14  *
    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.
    22  *
    23  *  Version: $Id$
    24  */
    25 #include <assert.h>
    26 #include <stdio.h>
    27 #include <stdlib.h>
    28 #include <string.h>
    29 #include <malloc.h>
    30 #include "sc.h"
    31 #include "memfile.h"
    32 
    33 /* pc_printf()
    34  * Called for general purpose "console" output. This function prints general
    35  * purpose messages; errors go through pc_error(). The function is modelled
    36  * after printf().
    37  */
    38 int pc_printf(const char *message,...)
    39 {
    40 	int ret;
    41 	va_list argptr;
    42 
    43 	va_start(argptr,message);
    44 	ret=vprintf(message,argptr);
    45 	va_end(argptr);
    46 
    47 	return ret;
    48 }
    49 
    50 /* pc_error()
    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
    59  *								"va_arg")
    60  * Return:
    61  *		If the function returns 0, the parser attempts to continue compilation.
    62  *		On a non-zero return value, the parser aborts.
    63  */
    64 int pc_error(int number,char *message,char *filename,int firstline,int lastline,va_list argptr)
    65 {
    66 static char *prefix[3]={ "error", "fatal error", "warning" };
    67 
    68 	if (number!=0) {
    69 		char *pre;
    70 
    71 		pre=prefix[number/100];
    72 		if (firstline>=0)
    73 			fprintf(stderr,"%s(%d -- %d) : %s %03d: ",filename,firstline,lastline,pre,number);
    74 		else
    75 			fprintf(stderr,"%s(%d) : %s %03d: ",filename,lastline,pre,number);
    76 	} /* if */
    77 	vfprintf(stderr,message,argptr);
    78 	fflush(stderr);
    79 	return 0;
    80 }
    81 
    82 /* pc_opensrc()
    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
    86  * Return:
    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.
    90  * Note:
    91  *		Several "source files" may be open at the same time. Specifically, one
    92  *		file can be open for reading and another for writing.
    93  */
    94 void *pc_opensrc(char *filename)
    95 {
    96 	return fopen(filename,"rt");
    97 }
    98 
    99 /* pc_createsrc()
   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
   103  * Return:
   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.
   107  * Note:
   108  *		Several "source files" may be open at the same time. Specifically, one
   109  *		file can be open for reading and another for writing.
   110  */
   111 void *pc_createsrc(char *filename)
   112 {
   113 	return fopen(filename,"wt");
   114 }
   115 
   116 /* pc_closesrc()
   117  * Closes a source file (or include file). The "handle" parameter has the
   118  * value that pc_opensrc() returned in an earlier call.
   119  */
   120 void pc_closesrc(void *handle)
   121 {
   122 	assert(handle!=NULL);
   123 	fclose((FILE*)handle);
   124 }
   125 
   126 /* pc_resetsrc()
   127  * "position" may only hold a pointer that was previously obtained from
   128  * pc_getpossrc()
   129  */
   130 void pc_resetsrc(void *handle,void *position)
   131 {
   132 	assert(handle!=NULL);
   133 	fsetpos((FILE*)handle,(fpos_t *)position);
   134 }
   135 
   136 /* pc_readsrc()
   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).
   139  */
   140 char *pc_readsrc(void *handle,unsigned char *target,int maxchars)
   141 {
   142 	return fgets((char*)target,maxchars,(FILE*)handle);
   143 }
   144 
   145 /* pc_writesrc()
   146  * Writes to to the source file. There is no automatic line ending; to end a
   147  * line, write a "\n".
   148  */
   149 int pc_writesrc(void *handle,unsigned char *source)
   150 {
   151 	return fputs((char*)source,(FILE*)handle) >= 0;
   152 }
   153 
   154 void *pc_getpossrc(void *handle)
   155 {
   156 	static fpos_t lastpos;	/* may need to have a LIFO stack of such positions */
   157 
   158 	fgetpos((FILE*)handle,&lastpos);
   159 	return &lastpos;
   160 }
   161 
   162 int pc_eofsrc(void *handle)
   163 {
   164 	return feof((FILE*)handle);
   165 }
   166 
   167 /* should return a pointer, which is used as a "magic cookie" to all I/O
   168  * functions; return NULL for failure
   169  */
   170 void *pc_openasm(char *filename)
   171 {
   172 	#if defined __MSDOS__ || defined SC_LIGHT
   173 		return fopen(filename,"w+t");
   174 	#else
   175 		return mfcreate(filename);
   176 	#endif
   177 }
   178 
   179 void pc_closeasm(void *handle, int deletefile)
   180 {
   181 	#if defined __MSDOS__ || defined SC_LIGHT
   182 		if (handle!=NULL)
   183 			fclose((FILE*)handle);
   184 		if (deletefile)
   185 			remove(outfname);
   186 	#else
   187 		if (handle!=NULL) {
   188 			if (!deletefile)
   189 				mfdump((MEMFILE*)handle);
   190 			mfclose((MEMFILE*)handle);
   191 		} /* if */
   192 	#endif
   193 }
   194 
   195 void pc_resetasm(void *handle)
   196 {
   197 	assert(handle!=NULL);
   198 	#if defined __MSDOS__ || defined SC_LIGHT
   199 		fflush((FILE*)handle);
   200 		fseek((FILE*)handle,0,SEEK_SET);
   201 	#else
   202 		mfseek((MEMFILE*)handle,0,SEEK_SET);
   203 	#endif
   204 }
   205 
   206 int pc_writeasm(void *handle,char *string)
   207 {
   208 	#if defined __MSDOS__ || defined SC_LIGHT
   209 		return fputs(string,(FILE*)handle) >= 0;
   210 	#else
   211 		return mfputs((MEMFILE*)handle,string);
   212 	#endif
   213 }
   214 
   215 char *pc_readasm(void *handle, char *string, int maxchars)
   216 {
   217 	#if defined __MSDOS__ || defined SC_LIGHT
   218 		return fgets(string,maxchars,(FILE*)handle);
   219 	#else
   220 		return mfgets((MEMFILE*)handle,string,maxchars);
   221 	#endif
   222 }
   223 
   224 extern memfile_t *bin_file;
   225 
   226 /* Should return a pointer, which is used as a "magic cookie" to all I/O
   227  * functions; return NULL for failure.
   228  */
   229 void *pc_openbin(char *filename)
   230 {
   231 	return memfile_creat(filename, 1);
   232 }
   233 
   234 void pc_closebin(void *handle,int deletefile)
   235 {
   236 	if (deletefile)
   237 	{
   238 		memfile_destroy((memfile_t *)handle);
   239 		bin_file = NULL;
   240 	} else {
   241 		bin_file = (memfile_t *)handle;
   242 	}
   243 }
   244 
   245 /* pc_resetbin()
   246  * Can seek to any location in the file.
   247  * The offset is always from the start of the file.
   248  */
   249 void pc_resetbin(void *handle,long offset)
   250 {
   251 	memfile_seek((memfile_t *)handle, offset);
   252 }
   253 
   254 int pc_writebin(void *handle,void *buffer,int size)
   255 {
   256 	return memfile_write((memfile_t *)handle, buffer, size);
   257 }
   258 
   259 long pc_lengthbin(void *handle)
   260 {
   261 	return memfile_tell((memfile_t *)handle);
   262 }