1993-07-28 21:18:00 +08:00
|
|
|
/*
|
|
|
|
** inout.c
|
|
|
|
** Provide function to realise the input/output function and debugger
|
|
|
|
** facilities.
|
1994-11-03 04:29:39 +08:00
|
|
|
** Also provides some predefined lua functions.
|
1993-07-28 21:18:00 +08:00
|
|
|
*/
|
|
|
|
|
1995-10-17 19:58:41 +08:00
|
|
|
char *rcs_inout="$Id: inout.c,v 2.22 1995/10/09 13:06:20 roberto Exp roberto $";
|
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>
|
|
|
|
|
1994-11-22 05:41:09 +08:00
|
|
|
#include "mem.h"
|
1993-07-28 21:18:00 +08:00
|
|
|
#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
|
|
|
|
1995-05-17 01:23:58 +08:00
|
|
|
|
|
|
|
#ifndef MAXFUNCSTACK
|
|
|
|
#define MAXFUNCSTACK 100
|
|
|
|
#endif
|
|
|
|
|
|
|
|
#define MAXMESSAGE MAXFUNCSTACK*80
|
|
|
|
|
|
|
|
|
1993-07-28 21:18:00 +08:00
|
|
|
/* Exported variables */
|
1994-12-21 05:20:36 +08:00
|
|
|
Word lua_linenumber;
|
|
|
|
Bool lua_debug;
|
|
|
|
Word lua_debugline = 0;
|
1995-10-17 19:58:41 +08:00
|
|
|
char *lua_parsedfile;
|
1993-07-28 21:18:00 +08:00
|
|
|
|
1994-11-22 05:41:09 +08:00
|
|
|
|
1993-07-28 21:18:00 +08:00
|
|
|
static FILE *fp;
|
|
|
|
static char *st;
|
|
|
|
|
|
|
|
/*
|
|
|
|
** Function to get the next character from the input file
|
|
|
|
*/
|
|
|
|
static int fileinput (void)
|
|
|
|
{
|
1994-10-18 03:04:19 +08:00
|
|
|
return fgetc (fp);
|
1993-07-28 21:18:00 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
** Function to get the next character from the input string
|
|
|
|
*/
|
|
|
|
static int stringinput (void)
|
|
|
|
{
|
1994-10-18 03:04:19 +08:00
|
|
|
return *st++;
|
1993-07-28 21:18:00 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
** Function to open a file to be input unit.
|
1994-11-04 06:34:29 +08:00
|
|
|
** Return 0 on success or error message on error.
|
1993-07-28 21:18:00 +08:00
|
|
|
*/
|
1994-11-04 06:34:29 +08:00
|
|
|
char *lua_openfile (char *fn)
|
1993-07-28 21:18:00 +08:00
|
|
|
{
|
|
|
|
lua_setinput (fileinput);
|
|
|
|
fp = fopen (fn, "r");
|
1994-11-04 06:34:29 +08:00
|
|
|
if (fp == NULL)
|
|
|
|
{
|
1995-03-18 04:27:11 +08:00
|
|
|
static char buff[255];
|
1995-10-17 19:58:41 +08:00
|
|
|
sprintf(buff, "unable to open file `%.200s'", fn);
|
1994-11-04 06:34:29 +08:00
|
|
|
return buff;
|
|
|
|
}
|
1995-10-17 19:58:41 +08:00
|
|
|
lua_linenumber = 1;
|
|
|
|
lua_parsedfile = lua_constcreate(fn)->ts.str;
|
|
|
|
return NULL;
|
1993-07-28 21:18:00 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
** Function to close an opened file
|
|
|
|
*/
|
|
|
|
void lua_closefile (void)
|
|
|
|
{
|
|
|
|
if (fp != NULL)
|
|
|
|
{
|
|
|
|
fclose (fp);
|
|
|
|
fp = NULL;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
** Function to open a string to be input unit
|
|
|
|
*/
|
1995-10-17 19:58:41 +08:00
|
|
|
void lua_openstring (char *s)
|
1993-07-28 21:18:00 +08:00
|
|
|
{
|
|
|
|
lua_setinput (stringinput);
|
|
|
|
st = s;
|
1995-10-17 19:58:41 +08:00
|
|
|
lua_linenumber = 1;
|
|
|
|
lua_parsedfile = lua_constcreate("(string)")->ts.str;
|
1993-07-28 21:18:00 +08:00
|
|
|
}
|
|
|
|
|
1993-12-18 02:41:19 +08:00
|
|
|
/*
|
|
|
|
** Function to close an opened string
|
|
|
|
*/
|
|
|
|
void lua_closestring (void)
|
|
|
|
{
|
1993-07-28 21:18:00 +08:00
|
|
|
}
|
|
|
|
|
1994-11-03 04:29:39 +08:00
|
|
|
|
|
|
|
/*
|
|
|
|
** Internal function: do a string
|
|
|
|
*/
|
|
|
|
void lua_internaldostring (void)
|
|
|
|
{
|
|
|
|
lua_Object obj = lua_getparam (1);
|
|
|
|
if (lua_isstring(obj) && !lua_dostring(lua_getstring(obj)))
|
|
|
|
lua_pushnumber(1);
|
|
|
|
else
|
|
|
|
lua_pushnil();
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
** Internal function: do a file
|
|
|
|
*/
|
|
|
|
void lua_internaldofile (void)
|
|
|
|
{
|
|
|
|
lua_Object obj = lua_getparam (1);
|
|
|
|
if (lua_isstring(obj) && !lua_dofile(lua_getstring(obj)))
|
|
|
|
lua_pushnumber(1);
|
|
|
|
else
|
|
|
|
lua_pushnil();
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
** Internal function: print object values
|
|
|
|
*/
|
|
|
|
void lua_print (void)
|
|
|
|
{
|
|
|
|
int i=1;
|
1994-11-08 00:34:44 +08:00
|
|
|
lua_Object obj;
|
1994-12-16 23:55:04 +08:00
|
|
|
while ((obj=lua_getparam (i++)) != LUA_NOOBJECT)
|
1994-11-03 04:29:39 +08:00
|
|
|
{
|
1994-11-08 00:34:44 +08:00
|
|
|
if (lua_isnumber(obj)) printf("%g\n",lua_getnumber(obj));
|
|
|
|
else if (lua_isstring(obj)) printf("%s\n",lua_getstring(obj));
|
1995-10-05 01:13:02 +08:00
|
|
|
else if (lua_isfunction(obj)) printf("function: %p\n",(luaI_Address(obj))->value.tf);
|
1994-11-08 00:34:44 +08:00
|
|
|
else if (lua_iscfunction(obj)) printf("cfunction: %p\n",lua_getcfunction(obj)
|
1994-11-03 04:29:39 +08:00
|
|
|
);
|
1994-11-08 00:34:44 +08:00
|
|
|
else if (lua_isuserdata(obj)) printf("userdata: %p\n",lua_getuserdata(obj));
|
|
|
|
else if (lua_istable(obj)) printf("table: %p\n",avalue(luaI_Address(obj)));
|
1994-11-03 04:29:39 +08:00
|
|
|
else if (lua_isnil(obj)) printf("nil\n");
|
|
|
|
else printf("invalid value to print\n");
|
|
|
|
}
|
|
|
|
}
|
1994-11-04 06:34:29 +08:00
|
|
|
|
|
|
|
|
1994-11-03 04:29:39 +08:00
|
|
|
/*
|
|
|
|
** Internal function: return an object type.
|
|
|
|
*/
|
1994-11-04 06:34:29 +08:00
|
|
|
void luaI_type (void)
|
1994-11-03 04:29:39 +08:00
|
|
|
{
|
1994-11-08 00:34:44 +08:00
|
|
|
lua_Object o = lua_getparam(1);
|
1995-03-18 04:42:20 +08:00
|
|
|
int t;
|
1994-12-16 23:55:04 +08:00
|
|
|
if (o == LUA_NOOBJECT)
|
1994-11-04 06:34:29 +08:00
|
|
|
lua_error("no parameter to function 'type'");
|
1995-03-18 04:42:20 +08:00
|
|
|
t = lua_type(o);
|
|
|
|
switch (t)
|
1994-11-03 04:29:39 +08:00
|
|
|
{
|
|
|
|
case LUA_T_NIL :
|
1994-12-13 23:54:21 +08:00
|
|
|
lua_pushliteral("nil");
|
1994-11-03 04:29:39 +08:00
|
|
|
break;
|
|
|
|
case LUA_T_NUMBER :
|
1994-12-13 23:54:21 +08:00
|
|
|
lua_pushliteral("number");
|
1994-11-03 04:29:39 +08:00
|
|
|
break;
|
|
|
|
case LUA_T_STRING :
|
1994-12-13 23:54:21 +08:00
|
|
|
lua_pushliteral("string");
|
1994-11-03 04:29:39 +08:00
|
|
|
break;
|
|
|
|
case LUA_T_ARRAY :
|
1994-12-13 23:54:21 +08:00
|
|
|
lua_pushliteral("table");
|
1994-11-03 04:29:39 +08:00
|
|
|
break;
|
|
|
|
case LUA_T_FUNCTION :
|
|
|
|
case LUA_T_CFUNCTION :
|
1995-10-09 21:06:20 +08:00
|
|
|
lua_pushliteral("function");
|
1994-11-03 04:29:39 +08:00
|
|
|
break;
|
|
|
|
default :
|
1994-12-13 23:54:21 +08:00
|
|
|
lua_pushliteral("userdata");
|
1994-11-03 04:29:39 +08:00
|
|
|
break;
|
|
|
|
}
|
1995-03-18 04:42:20 +08:00
|
|
|
lua_pushnumber(t);
|
1994-11-03 04:29:39 +08:00
|
|
|
}
|
|
|
|
|
1994-11-04 06:34:29 +08:00
|
|
|
/*
|
|
|
|
** Internal function: convert an object to a number
|
|
|
|
*/
|
|
|
|
void lua_obj2number (void)
|
|
|
|
{
|
|
|
|
lua_Object o = lua_getparam(1);
|
|
|
|
if (lua_isnumber(o))
|
|
|
|
lua_pushobject(o);
|
|
|
|
else if (lua_isstring(o))
|
|
|
|
{
|
|
|
|
char c;
|
|
|
|
float f;
|
|
|
|
if (sscanf(lua_getstring(o),"%f %c",&f,&c) == 1)
|
|
|
|
lua_pushnumber(f);
|
|
|
|
else
|
|
|
|
lua_pushnil();
|
|
|
|
}
|
|
|
|
else
|
|
|
|
lua_pushnil();
|
|
|
|
}
|
|
|
|
|
1994-11-09 04:06:15 +08:00
|
|
|
|
|
|
|
void luaI_error (void)
|
|
|
|
{
|
|
|
|
char *s = lua_getstring(lua_getparam(1));
|
|
|
|
if (s == NULL) s = "(no message)";
|
1995-05-03 02:43:03 +08:00
|
|
|
lua_error(s);
|
1994-11-09 04:06:15 +08:00
|
|
|
}
|
|
|
|
|