1993-07-28 21:18:00 +08:00
|
|
|
/*
|
|
|
|
** inout.c
|
|
|
|
** Provide function to realise the input/output function and debugger
|
|
|
|
** facilities.
|
|
|
|
*/
|
|
|
|
|
1994-10-11 22:38:17 +08:00
|
|
|
char *rcs_inout="$Id: inout.c,v 2.3 1994/09/05 21:22:43 celes Exp celes $";
|
1993-12-18 02:41:19 +08:00
|
|
|
|
1993-07-28 21:18:00 +08:00
|
|
|
#include <stdio.h>
|
1994-10-11 22:38:17 +08:00
|
|
|
#include <stdlib.h>
|
1993-07-28 21:18:00 +08:00
|
|
|
#include <string.h>
|
|
|
|
|
|
|
|
#include "opcode.h"
|
|
|
|
#include "hash.h"
|
|
|
|
#include "inout.h"
|
|
|
|
#include "table.h"
|
1994-07-20 05:27:18 +08:00
|
|
|
#include "tree.h"
|
1994-08-18 06:22:44 +08:00
|
|
|
#include "lua.h"
|
1993-07-28 21:18:00 +08:00
|
|
|
|
|
|
|
/* Exported variables */
|
|
|
|
int lua_linenumber;
|
|
|
|
int lua_debug;
|
|
|
|
int lua_debugline;
|
|
|
|
|
|
|
|
/* Internal variables */
|
|
|
|
#ifndef MAXFUNCSTACK
|
1994-09-06 05:22:43 +08:00
|
|
|
#define MAXFUNCSTACK 64
|
1993-07-28 21:18:00 +08:00
|
|
|
#endif
|
1994-10-11 22:38:17 +08:00
|
|
|
static struct { char *file; int function; } funcstack[MAXFUNCSTACK];
|
1993-07-28 21:18:00 +08:00
|
|
|
static int nfuncstack=0;
|
|
|
|
|
|
|
|
static FILE *fp;
|
|
|
|
static char *st;
|
|
|
|
static void (*usererror) (char *s);
|
|
|
|
|
|
|
|
/*
|
|
|
|
** Function to set user function to handle errors.
|
|
|
|
*/
|
|
|
|
void lua_errorfunction (void (*fn) (char *s))
|
|
|
|
{
|
|
|
|
usererror = fn;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
** Function to get the next character from the input file
|
|
|
|
*/
|
|
|
|
static int fileinput (void)
|
|
|
|
{
|
|
|
|
int c = fgetc (fp);
|
|
|
|
return (c == EOF ? 0 : c);
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
** Function to get the next character from the input string
|
|
|
|
*/
|
|
|
|
static int stringinput (void)
|
|
|
|
{
|
|
|
|
st++;
|
|
|
|
return (*(st-1));
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
** Function to open a file to be input unit.
|
|
|
|
** Return 0 on success or 1 on error.
|
|
|
|
*/
|
|
|
|
int lua_openfile (char *fn)
|
|
|
|
{
|
|
|
|
lua_linenumber = 1;
|
|
|
|
lua_setinput (fileinput);
|
|
|
|
fp = fopen (fn, "r");
|
|
|
|
if (fp == NULL) return 1;
|
|
|
|
if (lua_addfile (fn)) return 1;
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
** Function to close an opened file
|
|
|
|
*/
|
|
|
|
void lua_closefile (void)
|
|
|
|
{
|
|
|
|
if (fp != NULL)
|
|
|
|
{
|
1993-12-18 02:41:19 +08:00
|
|
|
lua_delfile();
|
1993-07-28 21:18:00 +08:00
|
|
|
fclose (fp);
|
|
|
|
fp = NULL;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
** Function to open a string to be input unit
|
|
|
|
*/
|
|
|
|
int lua_openstring (char *s)
|
|
|
|
{
|
|
|
|
lua_linenumber = 1;
|
|
|
|
lua_setinput (stringinput);
|
|
|
|
st = s;
|
|
|
|
{
|
|
|
|
char sn[64];
|
|
|
|
sprintf (sn, "String: %10.10s...", s);
|
|
|
|
if (lua_addfile (sn)) return 1;
|
|
|
|
}
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
1993-12-18 02:41:19 +08:00
|
|
|
/*
|
|
|
|
** Function to close an opened string
|
|
|
|
*/
|
|
|
|
void lua_closestring (void)
|
|
|
|
{
|
|
|
|
lua_delfile();
|
|
|
|
}
|
|
|
|
|
1993-07-28 21:18:00 +08:00
|
|
|
/*
|
|
|
|
** Call user function to handle error messages, if registred. Or report error
|
|
|
|
** using standard function (fprintf).
|
|
|
|
*/
|
|
|
|
void lua_error (char *s)
|
|
|
|
{
|
|
|
|
if (usererror != NULL) usererror (s);
|
|
|
|
else fprintf (stderr, "lua: %s\n", s);
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
** Called to execute SETFUNCTION opcode, this function pushs a function into
|
|
|
|
** function stack. Return 0 on success or 1 on error.
|
|
|
|
*/
|
1994-10-11 22:38:17 +08:00
|
|
|
int lua_pushfunction (char *file, int function)
|
1993-07-28 21:18:00 +08:00
|
|
|
{
|
|
|
|
if (nfuncstack >= MAXFUNCSTACK-1)
|
|
|
|
{
|
|
|
|
lua_error ("function stack overflow");
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
funcstack[nfuncstack].function = function;
|
1994-10-11 22:38:17 +08:00
|
|
|
funcstack[nfuncstack].file = file;
|
1993-07-28 21:18:00 +08:00
|
|
|
nfuncstack++;
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
** Called to execute RESET opcode, this function pops a function from
|
|
|
|
** function stack.
|
|
|
|
*/
|
|
|
|
void lua_popfunction (void)
|
|
|
|
{
|
|
|
|
nfuncstack--;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
** Report bug building a message and sending it to lua_error function.
|
|
|
|
*/
|
|
|
|
void lua_reportbug (char *s)
|
|
|
|
{
|
|
|
|
char msg[1024];
|
|
|
|
strcpy (msg, s);
|
|
|
|
if (lua_debugline != 0)
|
|
|
|
{
|
|
|
|
int i;
|
|
|
|
if (nfuncstack > 0)
|
|
|
|
{
|
|
|
|
sprintf (strchr(msg,0),
|
|
|
|
"\n\tin statement begining at line %d in function \"%s\" of file \"%s\"",
|
1994-07-20 05:27:18 +08:00
|
|
|
lua_debugline, lua_varname(funcstack[nfuncstack-1].function),
|
1994-10-11 22:38:17 +08:00
|
|
|
funcstack[nfuncstack-1].file);
|
1993-07-28 21:18:00 +08:00
|
|
|
sprintf (strchr(msg,0), "\n\tactive stack\n");
|
|
|
|
for (i=nfuncstack-1; i>=0; i--)
|
|
|
|
sprintf (strchr(msg,0), "\t-> function \"%s\" of file \"%s\"\n",
|
1994-07-20 05:27:18 +08:00
|
|
|
lua_varname(funcstack[i].function),
|
1994-10-11 22:38:17 +08:00
|
|
|
funcstack[i].file);
|
1993-07-28 21:18:00 +08:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
sprintf (strchr(msg,0),
|
|
|
|
"\n\tin statement begining at line %d of file \"%s\"",
|
|
|
|
lua_debugline, lua_filename());
|
|
|
|
}
|
|
|
|
}
|
|
|
|
lua_error (msg);
|
|
|
|
}
|
|
|
|
|