This commit is contained in:
romkazvo
2023-08-07 19:29:24 +08:00
commit 34d6c5d489
4832 changed files with 1389451 additions and 0 deletions

View File

@@ -0,0 +1,229 @@
/*
** $Id: lauxlib.c,v 1.51 2001/07/12 18:11:58 roberto Exp $
** Auxiliary functions for building Lua libraries
** See Copyright Notice in lua.h
*/
#ifndef PS2
#include <stdarg.h>
#include <stdio.h>
#include <string.h>
#endif
/* This file uses only the official API of Lua.
** Any function declared here could be written as an application function.
** With care, these functions can be used by other libraries.
*/
#define LUA_PRIVATE
#include "lua.h"
#include "lauxlib.h"
#include "luadebug.h"
#include "lualib.h"
LUALIB_API int luaL_findstring (const l_char *name, const l_char *const list[]) {
int i;
for (i=0; list[i]; i++)
#if defined(LINUX)
if (strcasecmp(list[i], name) == 0)
#else
if (strcmp(list[i], name) == 0)
#endif
return i;
return -1; /* name not found */
}
LUALIB_API void luaL_argerror (lua_State *L, int narg, const l_char *extramsg) {
lua_Debug ar;
lua_getstack(L, 0, &ar);
lua_getinfo(L, l_s("n"), &ar);
if (ar.name == NULL)
ar.name = l_s("?");
luaL_verror(L, l_s("bad argument #%d to `%.50s' (%.100s)"),
narg, ar.name, extramsg);
}
static void type_error (lua_State *L, int narg, const l_char *tname) {
l_char buff[80];
sprintf(buff, l_s("%.25s expected, got %.25s"), tname, lua_type(L,narg));
luaL_argerror(L, narg, buff);
}
static void tag_error (lua_State *L, int narg, int tag) {
type_error(L, narg, lua_typename(L, tag));
}
LUALIB_API void luaL_checkstack (lua_State *L, int space, const l_char *mes) {
if (space > lua_stackspace(L))
luaL_verror(L, l_s("stack overflow (%.30s)"), mes);
}
LUALIB_API void luaL_checktype(lua_State *L, int narg, int t) {
if (lua_rawtag(L, narg) != t)
tag_error(L, narg, t);
}
LUALIB_API void luaL_checkany (lua_State *L, int narg) {
if (lua_rawtag(L, narg) == LUA_TNONE)
luaL_argerror(L, narg, l_s("value expected"));
}
LUALIB_API void *luaL_check_userdata (lua_State *L, int narg,
const l_char *name) {
if (strcmp(lua_type(L, narg), name) != 0)
type_error(L, narg, name);
return lua_touserdata(L, narg);
}
LUALIB_API const l_char *luaL_check_lstr (lua_State *L, int narg, size_t *len) {
const l_char *s = lua_tostring(L, narg);
if (!s) tag_error(L, narg, LUA_TSTRING);
if (len) *len = lua_strlen(L, narg);
return s;
}
LUALIB_API const l_char *luaL_opt_lstr (lua_State *L, int narg, const l_char *def, size_t *len) {
if (lua_isnull(L, narg)) {
if (len)
*len = (def ? strlen(def) : 0);
return def;
}
else return luaL_check_lstr(L, narg, len);
}
LUALIB_API lua_Number luaL_check_number (lua_State *L, int narg) {
lua_Number d = lua_tonumber(L, narg);
if (d == 0 && !lua_isnumber(L, narg)) /* avoid extra test when d is not 0 */
tag_error(L, narg, LUA_TNUMBER);
return d;
}
LUALIB_API lua_Number luaL_opt_number (lua_State *L, int narg, lua_Number def) {
if (lua_isnull(L, narg)) return def;
else return luaL_check_number(L, narg);
}
LUALIB_API void luaL_openlib (lua_State *L, const luaL_reg *l, int n) {
int i;
for (i=0; i<n; i++)
lua_register(L, l[i].name, l[i].func);
}
LUALIB_API void luaL_verror (lua_State *L, const l_char *fmt, ...) {
l_char buff[500];
va_list argp;
va_start(argp, fmt);
vsprintf(buff, fmt, argp);
va_end(argp);
lua_error(L, buff);
}
/*
** {======================================================
** Generic Buffer manipulation
** =======================================================
*/
#define buffempty(B) ((B)->p == (B)->buffer)
#define bufflen(B) ((B)->p - (B)->buffer)
#define bufffree(B) ((size_t)(LUAL_BUFFERSIZE - bufflen(B)))
#define LIMIT (LUA_MINSTACK/2)
static int emptybuffer (luaL_Buffer *B) {
size_t l = bufflen(B);
if (l == 0) return 0; /* put nothing on stack */
else {
lua_pushlstring(B->L, B->buffer, l);
B->p = B->buffer;
B->level++;
return 1;
}
}
static void adjuststack (luaL_Buffer *B) {
if (B->level > 1) {
lua_State *L = B->L;
int toget = 1; /* number of levels to concat */
size_t toplen = lua_strlen(L, -1);
do {
size_t l = lua_strlen(L, -(toget+1));
if (B->level - toget + 1 >= LIMIT || toplen > l) {
toplen += l;
toget++;
}
else break;
} while (toget < B->level);
lua_concat(L, toget);
B->level = B->level - toget + 1;
}
}
LUALIB_API l_char *luaL_prepbuffer (luaL_Buffer *B) {
if (emptybuffer(B))
adjuststack(B);
return B->buffer;
}
LUALIB_API void luaL_addlstring (luaL_Buffer *B, const l_char *s, size_t l) {
while (l--)
luaL_putchar(B, *s++);
}
LUALIB_API void luaL_addstring (luaL_Buffer *B, const l_char *s) {
luaL_addlstring(B, s, strlen(s));
}
LUALIB_API void luaL_pushresult (luaL_Buffer *B) {
emptybuffer(B);
lua_concat(B->L, B->level);
B->level = 1;
}
LUALIB_API void luaL_addvalue (luaL_Buffer *B) {
lua_State *L = B->L;
size_t vl = lua_strlen(L, -1);
if (vl <= bufffree(B)) { /* fit into buffer? */
memcpy(B->p, lua_tostring(L, -1), vl); /* put it there */
B->p += vl;
lua_pop(L, 1); /* remove from stack */
}
else {
if (emptybuffer(B))
lua_insert(L, -2); /* put buffer before new value */
B->level++; /* add new value into B stack */
adjuststack(B);
}
}
LUALIB_API void luaL_buffinit (lua_State *L, luaL_Buffer *B) {
B->L = L;
B->p = B->buffer;
B->level = 0;
}
/* }====================================================== */

View File

@@ -0,0 +1,773 @@
/*
** $Id: lbaselib.c,v 1.39 2001/07/12 18:11:58 roberto Exp $
** Basic library
** See Copyright Notice in lua.h
*/
#include <ctype.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define LUA_PRIVATE
#include "lua.h"
#include "lauxlib.h"
#include "luadebug.h"
#include "lualib.h"
#ifdef PS2
#include "File.h"
#endif
#if defined(LINUX)
LUA_API void lua_getxregistry (lua_State *L);
#endif
static void aux_setn (lua_State *L, int t, int n) {
lua_pushliteral(L, l_s("n"));
lua_pushnumber(L, n);
lua_settable(L, t);
}
/*
** If your system does not support `stderr', redefine this function, or
** redefine _ERRORMESSAGE so that it won't need _ALERT.
*/
static int luaB__ALERT (lua_State *L) {
#ifdef PS2
OutputDebugString(luaL_check_string(L, 1));
#else
fputs(luaL_check_string(L, 1), stderr);
#endif
return 0;
}
/*
** Basic implementation of _ERRORMESSAGE.
** The library `liolib' redefines _ERRORMESSAGE for better error information.
*/
static int luaB__ERRORMESSAGE (lua_State *L) {
luaL_checktype(L, 1, LUA_TSTRING);
lua_getglobal(L, l_s(LUA_ALERT));
if (lua_isfunction(L, -1)) { /* avoid error loop if _ALERT is not defined */
lua_Debug ar;
lua_pushliteral(L, l_s("error: "));
lua_pushvalue(L, 1);
if (lua_getstack(L, 1, &ar)) {
lua_getinfo(L, l_s("Sl"), &ar);
if (ar.source && ar.currentline > 0) {
l_char buff[100];
sprintf(buff, l_s("\n <%.70s: line %d>"), ar.short_src, ar.currentline);
lua_pushstring(L, buff);
lua_concat(L, 2);
}
}
lua_pushliteral(L, l_s("\n"));
lua_concat(L, 3);
lua_rawcall(L, 1, 0);
}
return 0;
}
/*
** If your system does not support `stdout', you can just remove this function.
** If you need, you can define your own `print' function, following this
** model but changing `fputs' to put the strings at a proper place
** (a console window or a log file, for instance).
*/
static int luaB_print (lua_State *L) {
int n = lua_gettop(L); /* number of arguments */
int i;
lua_getglobal(L, l_s("tostring"));
for (i=1; i<=n; i++) {
const l_char *s;
lua_pushvalue(L, -1); /* function to be called */
lua_pushvalue(L, i); /* value to print */
lua_rawcall(L, 1, 1);
s = lua_tostring(L, -1); /* get result */
if (s == NULL)
lua_error(L, l_s("`tostring' must return a string to `print'"));
#ifdef PS2
if (i>1) OutputDebugString(l_s("\t"));
OutputDebugString(s);
#else
if (i>1) fputs(l_s("\t"), stdout);
fputs(s, stdout);
#endif
lua_pop(L, 1); /* pop result */
}
#ifndef PS2
fputs(l_s("\n"), stdout);
#endif
return 0;
}
static int luaB_tonumber (lua_State *L) {
int base = luaL_opt_int(L, 2, 10);
if (base == 10) { /* standard conversion */
luaL_checkany(L, 1);
if (lua_isnumber(L, 1)) {
lua_pushnumber(L, lua_tonumber(L, 1));
return 1;
}
}
else {
const l_char *s1 = luaL_check_string(L, 1);
l_char *s2;
unsigned int n;
luaL_arg_check(L, 2 <= base && base <= 36, 2, l_s("base out of range"));
n = strtoul(s1, &s2, base);
if (s1 != s2) { /* at least one valid digit? */
while (isspace(uchar(*s2))) s2++; /* skip trailing spaces */
if (*s2 == l_c('\0')) { /* no invalid trailing characters? */
lua_pushnumber(L, n);
return 1;
}
}
}
lua_pushnil(L); /* else not a number */
return 1;
}
static int luaB_error (lua_State *L) {
lua_error(L, luaL_opt_string(L, 1, NULL));
return 0; /* to avoid warnings */
}
static int luaB_setglobal (lua_State *L) {
luaL_checkany(L, 2);
lua_setglobal(L, luaL_check_string(L, 1));
return 0;
}
static int luaB_getglobal (lua_State *L) {
lua_getglobal(L, luaL_check_string(L, 1));
return 1;
}
/* auxiliary function to get `tags' */
static int gettag (lua_State *L, int narg) {
switch (lua_rawtag(L, narg)) {
case LUA_TNUMBER:
return (int)lua_tonumber(L, narg);
case LUA_TSTRING: {
const l_char *name = lua_tostring(L, narg);
int tag = lua_name2tag(L, name);
if (tag == LUA_TNONE)
luaL_verror(L, l_s("'%.30s' is not a valid type name"), name);
return tag;
}
default:
luaL_argerror(L, narg, l_s("tag or type name expected"));
return 0; /* to avoid warnings */
}
}
static int luaB_tag (lua_State *L) {
luaL_checkany(L, 1);
lua_pushnumber(L, lua_tag(L, 1));
return 1;
}
static int luaB_settype (lua_State *L) {
luaL_checktype(L, 1, LUA_TTABLE);
lua_pushvalue(L, 1); /* push table */
lua_settag(L, gettag(L, 2));
return 1; /* return table */
}
static int luaB_weakmode (lua_State *L) {
const l_char *mode = luaL_check_string(L, 2);
luaL_checktype(L, 1, LUA_TTABLE);
if (*mode == l_c('?')) {
l_char buff[3];
l_char *s = buff;
int imode = lua_getweakmode(L, 1);
if (imode & LUA_WEAK_KEY) *s++ = 'k';
if (imode & LUA_WEAK_VALUE) *s++ = 'v';
*s = '\0';
lua_pushstring(L, buff);
return 1;
}
else {
int imode = 0;
if (strchr(mode, l_c('k'))) imode |= LUA_WEAK_KEY;
if (strchr(mode, l_c('v'))) imode |= LUA_WEAK_VALUE;
lua_pushvalue(L, 1); /* push table */
lua_setweakmode(L, imode);
return 1; /* return the table */
}
}
static int luaB_newtype (lua_State *L) {
const l_char *name = luaL_opt_string(L, 1, NULL);
lua_pushnumber(L, lua_newtype(L, name, LUA_TTABLE));
return 1;
}
static int luaB_copytagmethods (lua_State *L) {
lua_pushnumber(L, lua_copytagmethods(L, gettag(L, 1), gettag(L, 2)));
return 1;
}
static int luaB_globals (lua_State *L) {
lua_getglobals(L); /* value to be returned */
if (!lua_isnull(L, 1)) {
luaL_checktype(L, 1, LUA_TTABLE);
lua_pushvalue(L, 1); /* new table of globals */
lua_setglobals(L);
}
return 1;
}
static int luaB_rawget (lua_State *L) {
luaL_checktype(L, 1, LUA_TTABLE);
luaL_checkany(L, 2);
lua_rawget(L, -2);
return 1;
}
static int luaB_rawset (lua_State *L) {
luaL_checktype(L, 1, LUA_TTABLE);
luaL_checkany(L, 2);
luaL_checkany(L, 3);
lua_rawset(L, -3);
return 1;
}
static int luaB_settagmethod (lua_State *L) {
int tag = gettag(L, 1);
const l_char *event = luaL_check_string(L, 2);
luaL_arg_check(L, lua_isfunction(L, 3) || lua_isnil(L, 3), 3,
l_s("function or nil expected"));
if (strcmp(event, l_s("gc")) == 0)
lua_error(L, l_s("deprecated use: cannot set the `gc' tag method from Lua"));
lua_gettagmethod(L, tag, event);
lua_pushvalue(L, 3);
lua_settagmethod(L, tag, event);
return 1;
}
static int luaB_gettagmethod (lua_State *L) {
int tag = gettag(L, 1);
const l_char *event = luaL_check_string(L, 2);
if (strcmp(event, l_s("gc")) == 0)
lua_error(L, l_s("deprecated use: cannot get the `gc' tag method from Lua"));
lua_gettagmethod(L, tag, event);
return 1;
}
static int luaB_gcinfo (lua_State *L) {
lua_pushnumber(L, lua_getgccount(L));
lua_pushnumber(L, lua_getgcthreshold(L));
return 2;
}
static int luaB_collectgarbage (lua_State *L) {
lua_setgcthreshold(L, luaL_opt_int(L, 1, 0));
return 0;
}
static int luaB_type (lua_State *L) {
luaL_checkany(L, 1);
lua_pushstring(L, lua_type(L, 1));
return 1;
}
static int luaB_rawtype (lua_State *L) {
luaL_checkany(L, 1);
lua_pushstring(L, lua_tag2name(L, lua_rawtag(L, 1)));
return 1;
}
static int luaB_next (lua_State *L) {
luaL_checktype(L, 1, LUA_TTABLE);
lua_settop(L, 2); /* create a 2nd argument if there isn't one */
if (lua_next(L, 1))
return 2;
else {
lua_pushnil(L);
return 1;
}
}
static int passresults (lua_State *L, int status, int oldtop) {
static const l_char *const errornames[] =
{l_s("ok"), l_s("run-time error"), l_s("file error"), l_s("syntax error"),
l_s("memory error"), l_s("error in error handling")};
if (status == 0) {
int nresults = lua_gettop(L) - oldtop;
if (nresults > 0)
return nresults; /* results are already on the stack */
else {
lua_newuserdatabox(L, NULL); /* at least one result to signal no errors */
return 1;
}
}
else { /* error */
lua_pushnil(L);
lua_pushstring(L, errornames[status]); /* error code */
return 2;
}
}
static int luaB_dostring (lua_State *L) {
int oldtop = lua_gettop(L);
size_t l;
const l_char *s = luaL_check_lstr(L, 1, &l);
const l_char *chunkname = luaL_opt_string(L, 2, s);
return passresults(L, lua_dobuffer(L, s, l, chunkname), oldtop);
}
static int luaB_loadstring (lua_State *L) {
int oldtop = lua_gettop(L);
size_t l;
const l_char *s = luaL_check_lstr(L, 1, &l);
const l_char *chunkname = luaL_opt_string(L, 2, s);
return passresults(L, lua_loadbuffer(L, s, l, chunkname), oldtop);
}
static int luaB_dofile (lua_State *L) {
int oldtop = lua_gettop(L);
const l_char *fname = luaL_opt_string(L, 1, NULL);
return passresults(L, lua_dofile(L, fname), oldtop);
}
static int luaB_loadfile (lua_State *L) {
int oldtop = lua_gettop(L);
const l_char *fname = luaL_opt_string(L, 1, NULL);
return passresults(L, lua_loadfile(L, fname), oldtop);
}
//ALBERTO this function is super dangerous be careful
static int luaB_getregistry(lua_State *L)
{
lua_getregistry(L);
lua_getweakregistry(L);
lua_getxregistry(L);
return 3;
}
#define LUA_PATH l_s("LUA_PATH")
#define LUA_PATH_SEP l_s(";")
#ifndef LUA_PATH_DEFAULT
#define LUA_PATH_DEFAULT l_s("./")
#endif
static int luaB_require (lua_State *L) {
const l_char *path;
luaL_check_string(L, 1);
lua_settop(L, 1);
lua_getglobal(L, LUA_PATH); /* get path */
if (lua_isstring(L, 2)) /* is LUA_PATH defined? */
path = lua_tostring(L, 2);
else { /* LUA_PATH not defined */
lua_pop(L, 1); /* pop old global value */
path = getenv(LUA_PATH); /* try environment variable */
if (path == NULL) path = LUA_PATH_DEFAULT; /* else use default */
lua_pushstring(L, path);
lua_pushvalue(L, -1); /* duplicate to leave a copy on stack */
lua_setglobal(L, LUA_PATH);
}
lua_getregistry(L);
lua_pushliteral(L, LUA_PATH);
lua_gettable(L, 3); /* get book-keeping table */
if (lua_isnil(L, 4)) { /* no book-keeping table? */
lua_pop(L, 1); /* pop the `nil' */
lua_newtable(L); /* create book-keeping table */
lua_pushliteral(L, LUA_PATH);
lua_pushvalue(L, -2); /* duplicate table to leave a copy on stack */
lua_settable(L, 3); /* store book-keeping table in registry */
}
lua_pushvalue(L, 1);
lua_gettable(L, 4); /* check package's name in book-keeping table */
if (!lua_isnil(L, -1)) /* is it there? */
return 0; /* package is already loaded */
else { /* must load it */
for (;;) { /* traverse path */
int res;
int l = strcspn(path, LUA_PATH_SEP); /* find separator */
lua_pushlstring(L, path, l); /* directory name */
lua_pushvalue(L, 1); /* package name */
lua_concat(L, 2); /* concat directory with package name */
res = lua_dofile(L, lua_tostring(L, -1)); /* try to load it */
lua_settop(L, 4); /* pop string and eventual results from dofile */
if (res == 0) break; /* ok; file done */
else if (res != LUA_ERRFILE)
lua_error(L, NULL); /* error running package; propagate it */
if (*(path+l) == l_c('\0')) /* no more directories? */
luaL_verror(L, l_s("could not load package `%.20s' from path `%.200s'"),
lua_tostring(L, 1), lua_tostring(L, 2));
path += l+1; /* try next directory */
}
}
lua_pushvalue(L, 1);
lua_pushnumber(L, 1);
lua_settable(L, 4); /* mark it as loaded */
return 0;
}
static int aux_unpack (lua_State *L, int arg) {
int n, i;
luaL_checktype(L, arg, LUA_TTABLE);
n = lua_getn(L, arg);
luaL_checkstack(L, n, l_s("table too big to unpack"));
for (i=1; i<=n; i++) /* push arg[1...n] */
lua_rawgeti(L, arg, i);
return n;
}
static int luaB_unpack (lua_State *L) {
return aux_unpack(L, 1);
}
static int luaB_call (lua_State *L) {
int oldtop;
const l_char *options = luaL_opt_string(L, 3, l_s(""));
int err = 0; /* index of old error method */
int status;
int n;
if (!lua_isnull(L, 4)) { /* set new error method */
lua_getglobal(L, l_s(LUA_ERRORMESSAGE));
err = lua_gettop(L); /* get index */
lua_pushvalue(L, 4);
lua_setglobal(L, l_s(LUA_ERRORMESSAGE));
}
oldtop = lua_gettop(L); /* top before function-call preparation */
/* push function */
lua_pushvalue(L, 1);
n = aux_unpack(L, 2); /* push arg[1...n] */
status = lua_call(L, n, LUA_MULTRET);
if (err != 0) { /* restore old error method */
lua_pushvalue(L, err);
lua_setglobal(L, l_s(LUA_ERRORMESSAGE));
}
if (status != 0) { /* error in call? */
if (strchr(options, l_c('x')))
lua_pushnil(L); /* return nil to signal the error */
else
lua_error(L, NULL); /* propagate error without additional messages */
return 1;
}
if (strchr(options, l_c('p'))) /* pack results? */
lua_error(L, l_s("deprecated option `p' in `call'"));
return lua_gettop(L) - oldtop; /* results are already on the stack */
}
static int luaB_tostring (lua_State *L) {
l_char buff[64];
switch (lua_rawtag(L, 1)) {
case LUA_TNUMBER:
lua_pushstring(L, lua_tostring(L, 1));
return 1;
case LUA_TSTRING:
lua_pushvalue(L, 1);
return 1;
case LUA_TTABLE:
sprintf(buff, l_s("%.40s: %p"), lua_type(L, 1), lua_topointer(L, 1));
break;
case LUA_TFUNCTION:
sprintf(buff, l_s("function: %p"), lua_topointer(L, 1));
break;
case LUA_TUSERDATA: {
const l_char *t = lua_type(L, 1);
if (strcmp(t, l_s("userdata")) == 0)
sprintf(buff, l_s("userdata(%d): %p"), lua_tag(L, 1),
lua_touserdata(L, 1));
else
sprintf(buff, l_s("%.40s: %p"), t, lua_touserdata(L, 1));
break;
}
case LUA_TNIL:
lua_pushliteral(L, l_s("nil"));
return 1;
default:
luaL_argerror(L, 1, l_s("value expected"));
}
lua_pushstring(L, buff);
return 1;
}
static int luaB_foreachi (lua_State *L) {
int n, i;
luaL_checktype(L, 1, LUA_TTABLE);
luaL_checktype(L, 2, LUA_TFUNCTION);
n = lua_getn(L, 1);
for (i=1; i<=n; i++) {
lua_pushvalue(L, 2); /* function */
lua_pushnumber(L, i); /* 1st argument */
lua_rawgeti(L, 1, i); /* 2nd argument */
lua_rawcall(L, 2, 1);
if (!lua_isnil(L, -1))
return 1;
lua_pop(L, 1); /* remove nil result */
}
return 0;
}
static int luaB_foreach (lua_State *L) {
luaL_checktype(L, 1, LUA_TTABLE);
luaL_checktype(L, 2, LUA_TFUNCTION);
lua_pushnil(L); /* first index */
for (;;) {
if (lua_next(L, 1) == 0)
return 0;
lua_pushvalue(L, 2); /* function */
lua_pushvalue(L, -3); /* key */
lua_pushvalue(L, -3); /* value */
lua_rawcall(L, 2, 1);
if (!lua_isnil(L, -1))
return 1;
lua_pop(L, 2); /* remove value and result */
}
}
static int luaB_assert (lua_State *L) {
luaL_checkany(L, 1);
if (lua_isnil(L, 1))
luaL_verror(L, l_s("assertion failed! %.90s"), luaL_opt_string(L, 2, l_s("")));
lua_settop(L, 1);
return 1;
}
static int luaB_getn (lua_State *L) {
luaL_checktype(L, 1, LUA_TTABLE);
lua_pushnumber(L, lua_getn(L, 1));
return 1;
}
static int luaB_tinsert (lua_State *L) {
int v = lua_gettop(L); /* last argument: to be inserted */
int n, pos;
luaL_checktype(L, 1, LUA_TTABLE);
n = lua_getn(L, 1);
if (v == 2) /* called with only 2 arguments */
pos = n+1;
else
pos = luaL_check_int(L, 2); /* 2nd argument is the position */
aux_setn(L, 1, n+1); /* t.n = n+1 */
for (; n>=pos; n--) {
lua_rawgeti(L, 1, n);
lua_rawseti(L, 1, n+1); /* t[n+1] = t[n] */
}
lua_pushvalue(L, v);
lua_rawseti(L, 1, pos); /* t[pos] = v */
return 0;
}
static int luaB_tremove (lua_State *L) {
int pos, n;
luaL_checktype(L, 1, LUA_TTABLE);
n = lua_getn(L, 1);
pos = luaL_opt_int(L, 2, n);
if (n <= 0) return 0; /* table is `empty' */
aux_setn(L, 1, n-1); /* t.n = n-1 */
lua_rawgeti(L, 1, pos); /* result = t[pos] */
for ( ;pos<n; pos++) {
lua_rawgeti(L, 1, pos+1);
lua_rawseti(L, 1, pos); /* a[pos] = a[pos+1] */
}
lua_pushnil(L);
lua_rawseti(L, 1, n); /* t[n] = nil */
return 1;
}
/*
** {======================================================
** Quicksort
** (based on `Algorithms in MODULA-3', Robert Sedgewick;
** Addison-Wesley, 1993.)
*/
static void set2 (lua_State *L, int i, int j) {
lua_rawseti(L, 1, i);
lua_rawseti(L, 1, j);
}
static int sort_comp (lua_State *L, int a, int b) {
/* WARNING: the caller (auxsort) must ensure stack space */
if (!lua_isnil(L, 2)) { /* function? */
int res;
lua_pushvalue(L, 2);
lua_pushvalue(L, a-1); /* -1 to compensate function */
lua_pushvalue(L, b-2); /* -2 to compensate function and `a' */
lua_rawcall(L, 2, 1);
res = !lua_isnil(L, -1);
lua_pop(L, 1);
return res;
}
else /* a < b? */
return lua_lessthan(L, a, b);
}
static void auxsort (lua_State *L, int l, int u) {
while (l < u) { /* for tail recursion */
int i, j;
/* sort elements a[l], a[(l+u)/2] and a[u] */
lua_rawgeti(L, 1, l);
lua_rawgeti(L, 1, u);
if (sort_comp(L, -1, -2)) /* a[u] < a[l]? */
set2(L, l, u); /* swap a[l] - a[u] */
else
lua_pop(L, 2);
if (u-l == 1) break; /* only 2 elements */
i = (l+u)/2;
lua_rawgeti(L, 1, i);
lua_rawgeti(L, 1, l);
if (sort_comp(L, -2, -1)) /* a[i]<a[l]? */
set2(L, i, l);
else {
lua_pop(L, 1); /* remove a[l] */
lua_rawgeti(L, 1, u);
if (sort_comp(L, -1, -2)) /* a[u]<a[i]? */
set2(L, i, u);
else
lua_pop(L, 2);
}
if (u-l == 2) break; /* only 3 elements */
lua_rawgeti(L, 1, i); /* Pivot */
lua_pushvalue(L, -1);
lua_rawgeti(L, 1, u-1);
set2(L, i, u-1);
/* a[l] <= P == a[u-1] <= a[u], only need to sort from l+1 to u-2 */
i = l; j = u-1;
for (;;) { /* invariant: a[l..i] <= P <= a[j..u] */
/* repeat ++i until a[i] >= P */
while (lua_rawgeti(L, 1, ++i), sort_comp(L, -1, -2)) {
if (i>u) lua_error(L, l_s("invalid order function for sorting"));
lua_pop(L, 1); /* remove a[i] */
}
/* repeat --j until a[j] <= P */
while (lua_rawgeti(L, 1, --j), sort_comp(L, -3, -1)) {
if (j<l) lua_error(L, l_s("invalid order function for sorting"));
lua_pop(L, 1); /* remove a[j] */
}
if (j<i) {
lua_pop(L, 3); /* pop pivot, a[i], a[j] */
break;
}
set2(L, i, j);
}
lua_rawgeti(L, 1, u-1);
lua_rawgeti(L, 1, i);
set2(L, u-1, i); /* swap pivot (a[u-1]) with a[i] */
/* a[l..i-1] <= a[i] == P <= a[i+1..u] */
/* adjust so that smaller half is in [j..i] and larger one in [l..u] */
if (i-l < u-i) {
j=l; i=i-1; l=i+2;
}
else {
j=i+1; i=u; u=j-2;
}
auxsort(L, j, i); /* call recursively the smaller one */
} /* repeat the routine for the larger one */
}
static int luaB_sort (lua_State *L) {
int n;
luaL_checktype(L, 1, LUA_TTABLE);
n = lua_getn(L, 1);
if (!lua_isnull(L, 2)) /* is there a 2nd argument? */
luaL_checktype(L, 2, LUA_TFUNCTION);
lua_settop(L, 2); /* make sure there is two arguments */
auxsort(L, 1, n);
return 0;
}
/* }====================================================== */
static const luaL_reg base_funcs[] = {
{l_s(LUA_ALERT), luaB__ALERT},
{l_s(LUA_ERRORMESSAGE), luaB__ERRORMESSAGE},
{l_s("call"), luaB_call},
{l_s("collectgarbage"), luaB_collectgarbage},
{l_s("copytagmethods"), luaB_copytagmethods},
{l_s("dofile"), luaB_dofile},
{l_s("dostring"), luaB_dostring},
{l_s("error"), luaB_error},
{l_s("foreach"), luaB_foreach},
{l_s("foreachi"), luaB_foreachi},
{l_s("gcinfo"), luaB_gcinfo},
{l_s("getglobal"), luaB_getglobal},
{l_s("gettagmethod"), luaB_gettagmethod},
{l_s("globals"), luaB_globals},
{l_s("loadfile"), luaB_loadfile},
{l_s("loadstring"), luaB_loadstring},
{l_s("newtype"), luaB_newtype},
{l_s("newtag"), luaB_newtype}, /* for compatibility 4.0 */
{l_s("next"), luaB_next},
{l_s("print"), luaB_print},
{l_s("rawget"), luaB_rawget},
{l_s("rawset"), luaB_rawset},
{l_s("rawgettable"), luaB_rawget}, /* for compatibility 3.2 */
{l_s("rawsettable"), luaB_rawset}, /* for compatibility 3.2 */
{l_s("rawtype"), luaB_rawtype},
{l_s("require"), luaB_require},
{l_s("setglobal"), luaB_setglobal},
{l_s("settag"), luaB_settype}, /* for compatibility 4.0 */
{l_s("settype"), luaB_settype},
{l_s("settagmethod"), luaB_settagmethod},
{l_s("tag"), luaB_tag},
{l_s("tonumber"), luaB_tonumber},
{l_s("tostring"), luaB_tostring},
{l_s("type"), luaB_type},
{l_s("assert"), luaB_assert},
{l_s("getn"), luaB_getn},
{l_s("sort"), luaB_sort},
{l_s("tinsert"), luaB_tinsert},
{l_s("tremove"), luaB_tremove},
{l_s("unpack"), luaB_unpack},
{l_s("weakmode"), luaB_weakmode},
{l_s("__getregistry"), luaB_getregistry}
};
LUALIB_API int lua_baselibopen (lua_State *L) {
luaL_openl(L, base_funcs);
lua_pushliteral(L, l_s(LUA_VERSION));
lua_setglobal(L, l_s("_VERSION"));
return 0;
}

View File

@@ -0,0 +1,63 @@
/* Bitwise operations library */
/* Reuben Thomas Nov00-26Nov01 */
#include "lauxlib.h"
#include "lua.h"
#include "platform.h"
typedef int64 Integer;
typedef uint64 UInteger;
#define luaL_check_bit(L, n) ((Integer)luaL_check_number(L, n))
#define luaL_check_ubit(L, n) ((UInteger)luaL_check_bit(L, n))
#define TDYADIC(name, op, t1, t2) \
static int int_ ## name(lua_State* L) { \
lua_pushnumber(L, \
luaL_check_ ## t1 ## bit(L, 1) op luaL_check_ ## t2 ## bit(L, 2)); \
return 1; \
}
#define DYADIC(name, op) \
TDYADIC(name, op, , )
#define MONADIC(name, op) \
static int int_ ## name(lua_State* L) { \
lua_pushnumber(L, op luaL_check_bit(L, 1)); \
return 1; \
}
#define VARIADIC(name, op) \
static int int_ ## name(lua_State *L) { \
int n = lua_gettop(L), i; \
Integer w = luaL_check_bit(L, 1); \
for (i = 2; i <= n; i++) \
w op ## = luaL_check_bit(L, i); \
lua_pushnumber(L, w); \
return 1; \
}
#pragma pack(push)
#pragma warning (disable : 4003)
MONADIC(not, ~)
DYADIC(mod, %)
VARIADIC(and, &)
VARIADIC(or, |)
VARIADIC(xor, ^)
TDYADIC(lshift, <<,, u)
TDYADIC(rshift, >>, u, u)
TDYADIC(arshift, >>,, u)
#pragma pack(pop)
static const struct luaL_reg bitlib[] = {
{"bnot", int_not},
{"imod", int_mod}, /* "mod" already in Lua math library */
{"band", int_and},
{"bor", int_or},
{"bxor", int_xor},
{"lshift", int_lshift},
{"rshift", int_rshift},
{"arshift", int_arshift},
};
LUALIB_API void lua_bitlibopen (lua_State *L) {
luaL_openl(L, bitlib);
}

View File

@@ -0,0 +1,190 @@
/*
** $Id: ldblib.c,v 1.37 2001/06/06 18:00:19 roberto Exp $
** Interface from Lua to its debug API
** See Copyright Notice in lua.h
*/
#include <stdio.h>
#include <stdlib.h>
#ifndef PS2
#include <string.h>
#endif
#define LUA_PRIVATE
#include "lua.h"
#include "lauxlib.h"
#include "luadebug.h"
#include "lualib.h"
static void settabss (lua_State *L, const l_char *i, const l_char *v) {
lua_pushstring(L, i);
lua_pushstring(L, v);
lua_settable(L, -3);
}
static void settabsi (lua_State *L, const l_char *i, int v) {
lua_pushstring(L, i);
lua_pushnumber(L, v);
lua_settable(L, -3);
}
static int getinfo (lua_State *L) {
lua_Debug ar;
const l_char *options = luaL_opt_string(L, 2, l_s("flnSu"));
l_char buff[20];
if (lua_isnumber(L, 1)) {
if (!lua_getstack(L, (int)lua_tonumber(L, 1), &ar)) {
lua_pushnil(L); /* level out of range */
return 1;
}
}
else if (lua_isfunction(L, 1)) {
lua_pushvalue(L, 1);
sprintf(buff, l_s(">%.10s"), options);
options = buff;
}
else
luaL_argerror(L, 1, l_s("function or level expected"));
if (!lua_getinfo(L, options, &ar))
luaL_argerror(L, 2, l_s("invalid option"));
lua_newtable(L);
for (; *options; options++) {
switch (*options) {
case l_c('S'):
settabss(L, l_s("source"), ar.source);
if (ar.source)
settabss(L, l_s("short_src"), ar.short_src);
settabsi(L, l_s("linedefined"), ar.linedefined);
settabss(L, l_s("what"), ar.what);
break;
case l_c('l'):
settabsi(L, l_s("currentline"), ar.currentline);
break;
case l_c('u'):
settabsi(L, l_s("nups"), ar.nups);
break;
case l_c('n'):
settabss(L, l_s("name"), ar.name);
settabss(L, l_s("namewhat"), ar.namewhat);
break;
case l_c('f'):
lua_pushliteral(L, l_s("func"));
lua_pushvalue(L, -3);
lua_settable(L, -3);
break;
}
}
return 1; /* return table */
}
static int getlocal (lua_State *L) {
lua_Debug ar;
const l_char *name;
if (!lua_getstack(L, luaL_check_int(L, 1), &ar)) /* level out of range? */
luaL_argerror(L, 1, l_s("level out of range"));
name = lua_getlocal(L, &ar, luaL_check_int(L, 2));
if (name) {
lua_pushstring(L, name);
lua_pushvalue(L, -2);
return 2;
}
else {
lua_pushnil(L);
return 1;
}
}
static int setlocal (lua_State *L) {
lua_Debug ar;
if (!lua_getstack(L, luaL_check_int(L, 1), &ar)) /* level out of range? */
luaL_argerror(L, 1, l_s("level out of range"));
luaL_checkany(L, 3);
lua_pushstring(L, lua_setlocal(L, &ar, luaL_check_int(L, 2)));
return 1;
}
#define KEY_CALLHOOK l_s("luadblibCallhook")
#define KEY_LINEHOOK l_s("luadblibLinehook")
static void hookf (lua_State *L, const l_char *key) {
lua_getregistry(L);
lua_pushstring(L, key);
lua_gettable(L, -2);
if (lua_isfunction(L, -1)) {
lua_pushvalue(L, -3); /* original argument (below table and function) */
lua_rawcall(L, 1, 0);
}
else
lua_pop(L, 1); /* pop result from gettable */
lua_pop(L, 1); /* pop table */
}
static void callf (lua_State *L, lua_Debug *ar) {
lua_pushstring(L, ar->event);
hookf(L, KEY_CALLHOOK);
}
static void linef (lua_State *L, lua_Debug *ar) {
lua_pushnumber(L, ar->currentline);
hookf(L, KEY_LINEHOOK);
}
static void sethook (lua_State *L, const l_char *key, lua_Hook hook,
lua_Hook (*sethookf)(lua_State * L, lua_Hook h)) {
lua_settop(L, 1);
if (lua_isnil(L, 1))
(*sethookf)(L, NULL);
else if (lua_isfunction(L, 1))
(*sethookf)(L, hook);
else
luaL_argerror(L, 1, l_s("function expected"));
lua_getregistry(L);
lua_pushstring(L, key);
lua_pushvalue(L, -1); /* dup key */
lua_gettable(L, -3); /* get old value */
lua_pushvalue(L, -2); /* key (again) */
lua_pushvalue(L, 1);
lua_settable(L, -5); /* set new value */
}
static int setcallhook (lua_State *L) {
sethook(L, KEY_CALLHOOK, callf, lua_setcallhook);
return 1;
}
static int setlinehook (lua_State *L) {
sethook(L, KEY_LINEHOOK, linef, lua_setlinehook);
return 1;
}
static const luaL_reg dblib[] = {
{l_s("getlocal"), getlocal},
{l_s("getinfo"), getinfo},
{l_s("setcallhook"), setcallhook},
{l_s("setlinehook"), setlinehook},
{l_s("setlocal"), setlocal}
};
LUALIB_API int lua_dblibopen (lua_State *L) {
luaL_openl(L, dblib);
return 0;
}

View File

@@ -0,0 +1,748 @@
/*
** $Id: liolib.c,v 1.121 2001/07/22 00:59:36 roberto Exp $
** Standard I/O (and system) library
** See Copyright Notice in lua.h
*/
#include <platform.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <time.h>
#include "../LuaCryPakIO.h"
#define LUA_PRIVATE
#include "lua.h"
#include "ldo.h"
#include "lauxlib.h"
#include "luadebug.h"
#include "lualib.h"
#ifndef OLD_ANSI
#include <errno.h>
#include <locale.h>
#endif
#ifdef PS2
//#include "File.h"
/*
#undef stdin
#define stdin NULL
#undef stdout
#define stdout NULL
#undef stderr
#define stderr NULL
*/
//extern FILE * fxopen(const char *file, const char *mode);
//extern fxclose(FILE *f); // crypak support
#endif
#ifdef POPEN
/* FILE *popen();
int pclose(); */
//#define CLOSEFILE(L, f) ((pclose(f) == -1) ? fclose(f) : 0)
#define CLOSEFILE(L, f) ((pclose(f) == -1) ? CryPakClose(f) : 0)
#else
/* no support for popen */
#define popen(x,y) NULL /* that is, popen always fails */
//#define CLOSEFILE(L, f) (fclose(f))
#define CLOSEFILE(L, f) (CryPakClose(f))
#endif
#define INFILE 0
#define OUTFILE 1
#define NOFILE 2
#define FILEHANDLE l_s("FileHandle")
#define CLOSEDFILEHANDLE l_s("ClosedFileHandle")
static const l_char *const filenames[] = {l_s("_INPUT"), l_s("_OUTPUT")};
static const l_char *const basicfiles[] = {l_s("_STDIN"), l_s("_STDOUT")};
static int pushresult (lua_State *L, int i) {
if (i) {
lua_pushnumber(L, 1);
return 1;
}
else {
lua_pushnil(L);
lua_pushstring(L, strerror(errno));
lua_pushnumber(L, errno);
return 3;
}
}
/*
** {======================================================
** FILE Operations
** =======================================================
*/
#if defined(LINUX)
#define checkfile(L,f) (strcasecmp(lua_type(L,(f)), FILEHANDLE) == 0)
#else
#define checkfile(L,f) (strcmp(lua_type(L,(f)), FILEHANDLE) == 0)
#endif
static FILE *getopthandle (lua_State *L, int inout) {
FILE *p = (FILE *)lua_touserdata(L, 1);
if (p != NULL) { /* is it a userdata ? */
if (!checkfile(L, 1)) { /* not a valid file handle? */
if (strcmp(lua_type(L, 1), CLOSEDFILEHANDLE) == 0)
luaL_argerror(L, 1, l_s("file is closed"));
else
luaL_argerror(L, 1, l_s("(invalid value)"));
}
lua_pushvalue(L, 1); lua_remove(L, 1); /* move it to stack top */
}
else { /* try global value */
lua_getglobal(L, filenames[inout]);
if (!checkfile(L,-1))
luaL_verror(L, l_s("global variable `%.10s' is not a valid file handle"),
filenames[inout]);
p = (FILE *)lua_touserdata(L, -1);
}
return p; /* leave handle at stack top to avoid GC */
}
static void newfile (lua_State *L, FILE *f) {
lua_newuserdatabox(L, f);
lua_settag(L, lua_name2tag(L, FILEHANDLE));
}
static void newfilewithname (lua_State *L, FILE *f, const l_char *name) {
newfile(L, f);
lua_setglobal(L, name);
}
static int setnewfile (lua_State *L, FILE *f, int inout) {
if (f == NULL)
return pushresult(L, 0);
else {
newfile(L, f);
if (inout != NOFILE) {
lua_pushvalue(L, -1);
lua_setglobal(L, filenames[inout]);
}
return 1;
}
}
static void resetfile (lua_State *L, int inout) {
lua_getglobal(L, basicfiles[inout]);
lua_setglobal(L, filenames[inout]);
}
static int io_close (lua_State *L) {
FILE *f = (FILE *)luaL_check_userdata(L, 1, FILEHANDLE);
int status = 1;
#ifndef PS2
if (f != stdin && f != stdout && f != stderr) {
lua_settop(L, 1); /* make sure file is on top */
lua_settag(L, lua_name2tag(L, CLOSEDFILEHANDLE));
status = (CLOSEFILE(L, f) == 0);
}
#else
lua_settop(L, 1); /* make sure file is on top */
lua_settag(L, lua_name2tag(L, CLOSEDFILEHANDLE));
status = (CLOSEFILE(L, f) == 0);
#endif
return pushresult(L, status);
}
static int file_collect (lua_State *L) {
FILE *f = (FILE *)luaL_check_userdata(L, 1, FILEHANDLE);
#ifndef PS2
if (f != stdin && f != stdout && f != stderr)
#endif
CLOSEFILE(L, f);
return 0;
}
static int io_open (lua_State *L) {
FILE *f = CryPakOpen(luaL_check_string(L, 1), luaL_check_string(L, 2));
return setnewfile(L, f, NOFILE);
}
static int io_tmpfile (lua_State *L) {
#ifdef PS2
static FILE *tmpfile__;
#define tmpfile() tmpfile__
#endif
return setnewfile(L, tmpfile(), NOFILE);
}
static int io_fromto (lua_State *L, int inout, const l_char *mode) {
FILE *current;
if (lua_isnull(L, 1)) {
getopthandle(L, inout);
resetfile(L, inout);
return io_close(L);
}
else {
const l_char *s = luaL_check_string(L, 1);
current = (*s == l_c('|')) ? popen(s+1, mode) : CryPakOpen(s, mode);
return setnewfile(L, current, inout);
}
}
static int io_readfrom (lua_State *L) {
return io_fromto(L, INFILE, l_s("r"));
}
static int io_writeto (lua_State *L) {
return io_fromto(L, OUTFILE, l_s("w"));
}
static int io_appendto (lua_State *L) {
FILE *current = fxopen(luaL_check_string(L, 1), l_s("a"));
return setnewfile(L, current, OUTFILE);
}
/*
** {======================================================
** READ
** =======================================================
*/
#ifndef LUA_MAXUNTIL
#define LUA_MAXUNTIL 100
#endif
/*
** Knuth-Morris-Pratt algorithm for string searching
** (based on `Algorithms in MODULA-3', Robert Sedgewick;
** Addison-Wesley, 1993.)
*/
static void prep_read_until (int next[], const l_char *p, int pl) {
int i = 0;
int j = -1;
next[0] = -1;
while (i < pl) {
if (j == -1 || p[i] == p[j]) {
i++; j++; next[i] = j;
}
else j = next[j];
}
}
static int read_until (lua_State *L, FILE *f, const l_char *p, int pl) {
l_charint c;
int j;
int next[LUA_MAXUNTIL+1];
luaL_Buffer b;
luaL_buffinit(L, &b);
prep_read_until(next, p, pl);
j = 0;
while ((c = CryPakGetc(f)) != EOF) {
NoRead:
if (c == p[j]) {
j++; /* go to next char in pattern */
if (j == pl) { /* complete match? */
luaL_pushresult(&b); /* close buffer */
return 1; /* always success */
}
}
else if (j == 0)
luaL_putchar(&b, c);
else { /* match fail */
luaL_addlstring(&b, p, j - next[j]); /* put failed part on result */
j = next[j]; /* backtrack pattern index */
goto NoRead; /* repeat without reading next char */
}
}
/* end of file without a match */
luaL_addlstring(&b, p, j); /* put failed part on result */
luaL_pushresult(&b); /* close buffer */
return (lua_strlen(L, -1) > 0);
}
static int read_number (lua_State *L, FILE *f) {
double d;
if (CryPakFScanf(f, l_s(LUA_NUMBER_SCAN), &d) == 1) {
lua_pushnumber(L, d);
return 1;
}
else return 0; /* read fails */
}
static int test_eof (lua_State *L, FILE *f) {
l_charint c = CryPakGetc(f);
CryPakUngetc(c, f);
lua_pushlstring(L, NULL, 0);
return (c != EOF);
}
static int read_chars (lua_State *L, FILE *f, size_t n) {
size_t rlen; /* how much to read */
size_t nr; /* number of chars actually read */
luaL_Buffer b;
luaL_buffinit(L, &b);
rlen = LUAL_BUFFERSIZE; /* try to read that much each time */
do {
l_char *p = luaL_prepbuffer(&b);
if (rlen > n) rlen = n; /* cannot read more than asked */
nr = CryPakFRead(p, sizeof(l_char), rlen, f);
luaL_addsize(&b, nr);
n -= nr; /* still have to read `n' chars */
} while (n > 0 && nr == rlen); /* until end of count or eof */
luaL_pushresult(&b); /* close buffer */
return (n == 0 || lua_strlen(L, -1) > 0);
}
static int io_read (lua_State *L) {
FILE *f = getopthandle(L, INFILE);
int nargs = lua_gettop(L) - 1;
int success;
int n;
if (nargs == 0) { /* no arguments? */
success = read_until(L, f, l_s("\n"), 1); /* read until \n (a line) */
n = 2; /* will return n-1 results */
}
else { /* ensure stack space for all results and for auxlib's buffer */
luaL_checkstack(L, nargs+LUA_MINSTACK, l_s("too many arguments"));
success = 1;
for (n = 1; n<=nargs && success; n++) {
if (lua_rawtag(L, n) == LUA_TNUMBER) {
size_t l = (size_t)lua_tonumber(L, n);
success = (l == 0) ? test_eof(L, f) : read_chars(L, f, l);
}
else {
const l_char *p = lua_tostring(L, n);
if (!p || p[0] != l_c('*'))
lua_error(L, l_s("invalid `read' option"));
switch (p[1]) {
case l_c('n'): /* number */
success = read_number(L, f);
break;
case l_c('l'): /* line */
success = read_until(L, f, l_s("\n"), 1); /* read until \n */
break;
case l_c('a'): /* file */
read_chars(L, f, ~((size_t)0)); /* read MAX_SIZE_T chars */
success = 1; /* always success */
break;
case l_c('w'): /* word */
lua_error(L, l_s("option `*w' is deprecated"));
break;
case l_c('u'): { /* read until */
size_t pl = lua_strlen(L, n) - 2;
luaL_arg_check(L, 0 < pl && pl <= LUA_MAXUNTIL, n,
l_s("invalid read-until length"));
success = read_until(L, f, p+2, (int)pl);
break;
}
default:
luaL_argerror(L, n, l_s("invalid format"));
success = 0; /* to avoid warnings */
}
}
}
}
if (!success) {
lua_pop(L, 1); /* remove last result */
lua_pushnil(L); /* push nil instead */
}
return n - 1;
}
/* }====================================================== */
static int io_write (lua_State *L) {
FILE *f = getopthandle(L, OUTFILE);
int nargs = lua_gettop(L)-1;
int arg;
int status = 1;
for (arg=1; arg<=nargs; arg++) {
if (lua_rawtag(L, arg) == LUA_TNUMBER) {
/* optimization: could be done exactly as for strings */
status = status &&
CryPakFPrintf(f, l_s(LUA_NUMBER_FMT), lua_tonumber(L, arg)) > 0;
}
else {
size_t l;
const l_char *s = luaL_check_lstr(L, arg, &l);
status = status && (CryPakFWrite((void*)s, sizeof(l_char), l, f) == l);
}
}
pushresult(L, status);
return 1;
}
static int io_seek (lua_State *L) {
static const int mode[] = {SEEK_SET, SEEK_CUR, SEEK_END};
static const l_char *const modenames[] = {l_s("set"), l_s("cur"), l_s("end"), NULL};
FILE *f = (FILE *)luaL_check_userdata(L, 1, FILEHANDLE);
int op = luaL_findstring(luaL_opt_string(L, 2, l_s("cur")), modenames);
long offset = luaL_opt_long(L, 3, 0);
luaL_arg_check(L, op != -1, 2, l_s("invalid mode"));
op = CryPakFSeek(f, offset, mode[op]);
if (op)
return pushresult(L, 0); /* error */
else {
lua_pushnumber(L, ftell(f));
return 1;
}
}
static int io_flush (lua_State *L) {
FILE *f = (lua_isnull(L, 1)) ? (FILE *)NULL :
(FILE *)luaL_check_userdata(L, 1, FILEHANDLE);
return pushresult(L, CryPakFFlush(f) == 0);
}
/* }====================================================== */
/*
** {======================================================
** Other O.S. Operations
** =======================================================
*/
static int io_execute (lua_State *L) {
lua_pushnumber(L, system(luaL_check_string(L, 1)));
return 1;
}
static int io_remove (lua_State *L) {
return pushresult(L, remove(luaL_check_string(L, 1)) == 0);
}
static int io_rename (lua_State *L) {
return pushresult(L, rename(luaL_check_string(L, 1),
luaL_check_string(L, 2)) == 0);
}
static int io_tmpname (lua_State *L) {
l_char buff[L_tmpnam];
if (tmpnam(buff) != buff)
lua_error(L, l_s("unable to generate a unique filename"));
lua_pushstring(L, buff);
return 1;
}
static int io_getenv (lua_State *L) {
lua_pushstring(L, getenv(luaL_check_string(L, 1))); /* if NULL push nil */
return 1;
}
static int io_clock (lua_State *L) {
lua_pushnumber(L, ((lua_Number)clock())/(lua_Number)CLOCKS_PER_SEC);
return 1;
}
/*
** {======================================================
** Time/Date operations
** { year=%Y, month=%m, day=%d, hour=%H, min=%M, sec=%S,
** wday=%w+1, yday=%j, isdst=? }
** =======================================================
*/
static void setfield (lua_State *L, const l_char *key, int value) {
lua_pushstring(L, key);
lua_pushnumber(L, value);
lua_rawset(L, -3);
}
static int getfield (lua_State *L, const l_char *key, int d) {
int res;
lua_pushstring(L, key);
lua_rawget(L, -2);
if (lua_isnumber(L, -1))
res = (int)lua_tonumber(L, -1);
else {
if (d == -2)
luaL_verror(L, l_s("field `%.20s' missing in date table"), key);
res = d;
}
lua_pop(L, 1);
return res;
}
static int io_date (lua_State *L) {
#ifdef PS2
OutputDebugString("Not implemented");
#else
const l_char *s = luaL_opt_string(L, 1, l_s("%c"));
time_t t = (time_t)luaL_opt_number(L, 2, -1);
struct tm *stm;
if (t == (time_t)-1) /* no time given? */
t = time(NULL); /* use current time */
if (*s == l_c('!')) { /* UTC? */
stm = gmtime(&t);
s++; /* skip `!' */
}
else
stm = localtime(&t);
if (stm == NULL) /* invalid date? */
lua_pushnil(L);
else if (strcmp(s, l_s("*t")) == 0) {
lua_newtable(L);
setfield(L, l_s("sec"), stm->tm_sec);
setfield(L, l_s("min"), stm->tm_min);
setfield(L, l_s("hour"), stm->tm_hour);
setfield(L, l_s("day"), stm->tm_mday);
setfield(L, l_s("month"), stm->tm_mon+1);
setfield(L, l_s("year"), stm->tm_year+1900);
setfield(L, l_s("wday"), stm->tm_wday+1);
setfield(L, l_s("yday"), stm->tm_yday+1);
setfield(L, l_s("isdst"), stm->tm_isdst);
}
else {
l_char b[256];
if (strftime(b, sizeof(b), s, stm))
lua_pushstring(L, b);
else
lua_error(L, l_s("invalid `date' format"));
}
#endif
return 1;
}
static int io_time (lua_State *L) {
#ifdef PS2
OutputDebugString("Not Implemented");
#else
if (lua_isnull(L, 1)) //* called without args?
lua_pushnumber(L, time(NULL)); //* return current time
if (lua_isnull(L, 1)) /* called without args? */
lua_pushnumber(L, time(NULL)); /* return current time */
else {
time_t t;
struct tm ts;
luaL_checktype(L, 1, LUA_TTABLE);
lua_settop(L, 1); /* make sure table is at the top */
ts.tm_sec = getfield(L, l_s("sec"), 0);
ts.tm_min = getfield(L, l_s("min"), 0);
ts.tm_hour = getfield(L, l_s("hour"), 12);
ts.tm_mday = getfield(L, l_s("day"), -2);
ts.tm_mon = getfield(L, l_s("month"), -2)-1;
ts.tm_year = getfield(L, l_s("year"), -2)-1900;
ts.tm_isdst = getfield(L, l_s("isdst"), -1);
t = mktime(&ts);
if (t == (time_t)-1)
lua_pushnil(L);
else
lua_pushnumber(L, t);
}
#endif
return 1;
}
static int io_difftime (lua_State *L) {
lua_pushnumber(L, difftime((time_t)luaL_check_number(L, 1),
(time_t)luaL_opt_number(L, 2, 0)));
return 1;
}
/* }====================================================== */
static int io_setloc (lua_State *L) {
static const int cat[] = {LC_ALL, LC_COLLATE, LC_CTYPE, LC_MONETARY,
LC_NUMERIC, LC_TIME};
static const l_char *const catnames[] = {l_s("all"), l_s("collate"), l_s("ctype"), l_s("monetary"),
l_s("numeric"), l_s("time"), NULL};
int op = luaL_findstring(luaL_opt_string(L, 2, l_s("all")), catnames);
luaL_arg_check(L, op != -1, 2, l_s("invalid option"));
lua_pushstring(L, setlocale(cat[op], luaL_check_string(L, 1)));
return 1;
}
static int io_exit (lua_State *L) {
//FORCE_EXIT();
#if defined(WIN32) && !defined(WIN64)
DEBUG_BREAK;
#endif
//it(luaL_opt_int(L, 1, EXIT_SUCCESS));
return 0; /* to avoid warnings */
}
/* }====================================================== */
static int io_debug (lua_State *L) {
#ifdef PS2
OutputDebugString("Not Implemented");
#else
for (;;) {
l_char buffer[250];
CryPakFPrintf(stderr, l_s("lua_debug> "));
if (CryPakFGets(buffer, sizeof(buffer), stdin) == 0 ||
strcmp(buffer, l_s("cont\n")) == 0)
return 0;
lua_dostring(L, buffer);
lua_settop(L, 0); /* remove eventual returns */
}
#endif
}
#define LEVELS1 12 /* size of the first part of the stack */
#define LEVELS2 10 /* size of the second part of the stack */
int errorfb (lua_State *L) {
#ifdef PS2
OutputDebugString("Not implemented");
#else
int level = 1; /* skip level 0 (it's this function) */
int firstpart = 1; /* still before eventual `...' */
lua_Debug ar;
luaL_Buffer b;
luaL_buffinit(L, &b);
luaL_addstring(&b, l_s("error: "));
luaL_addstring(&b, luaL_check_string(L, 1));
luaL_addstring(&b, l_s("\n"));
while (lua_getstack(L, level++, &ar)) {
l_char buff[120]; /* enough to fit following `sprintf's */
if (level == 2)
luaL_addstring(&b, l_s("stack traceback:\n"));
else if (level > LEVELS1 && firstpart) {
/* no more than `LEVELS2' more levels? */
if (!lua_getstack(L, level+LEVELS2, &ar))
level--; /* keep going */
else {
luaL_addstring(&b, l_s(" ...\n")); /* too many levels */
while (lua_getstack(L, level+LEVELS2, &ar)) /* find last levels */
level++;
}
firstpart = 0;
continue;
}
sprintf(buff, l_s("%4d: "), level-1);
luaL_addstring(&b, buff);
lua_getinfo(L, l_s("Snl"), &ar);
switch (*ar.namewhat) {
case l_c('g'): case l_c('l'): /* global, local */
sprintf(buff, l_s("function `%.50s'"), ar.name);
break;
case l_c('f'): /* field */
sprintf(buff, l_s("method `%.50s'"), ar.name);
break;
case l_c('t'): /* tag method */
sprintf(buff, l_s("`%.50s' tag method"), ar.name);
break;
default: {
if (*ar.what == l_c('m')) /* main? */
sprintf(buff, l_s("main of %.70s"), ar.short_src);
else if (*ar.what == l_c('C')) /* C function? */
sprintf(buff, l_s("%.70s"), ar.short_src);
else
sprintf(buff, l_s("function <%d:%.70s>"), ar.linedefined, ar.short_src);
ar.source = NULL; /* do not print source again */
}
}
luaL_addstring(&b, buff);
if (ar.currentline > 0) {
sprintf(buff, l_s(" at line %d"), ar.currentline);
luaL_addstring(&b, buff);
}
if (ar.source) {
sprintf(buff, l_s(" [%.70s]"), ar.short_src);
luaL_addstring(&b, buff);
}
luaL_addstring(&b, l_s("\n"));
}
luaL_pushresult(&b);
lua_getglobal(L, l_s(LUA_ALERT));
if (lua_isfunction(L, -1)) { /* avoid loop if _ALERT is not defined */
lua_pushvalue(L, -2); /* error message */
lua_rawcall(L, 1, 0);
}
#endif
return 0;
}
static const luaL_reg iolib[] = {
{l_s("appendto"), io_appendto},
{l_s("clock"), io_clock},
{l_s("closefile"), io_close},
{l_s("date"), io_date},
{l_s("debug"), io_debug},
{l_s("difftime"), io_difftime},
{l_s("execute"), io_execute},
{l_s("exit"), io_exit},
{l_s("flush"), io_flush},
{l_s("getenv"), io_getenv},
{l_s("openfile"), io_open},
{l_s("read"), io_read},
{l_s("readfrom"), io_readfrom},
{l_s("remove"), io_remove},
{l_s("rename"), io_rename},
{l_s("seek"), io_seek},
{l_s("setlocale"), io_setloc},
{l_s("time"), io_time},
{l_s("tmpfile"), io_tmpfile},
{l_s("tmpname"), io_tmpname},
{l_s("write"), io_write},
{l_s("writeto"), io_writeto},
{l_s(LUA_ERRORMESSAGE), errorfb}
};
LUALIB_API int lua_iolibopen (lua_State *L) {
int iotag = lua_newtype(L, FILEHANDLE, LUA_TUSERDATA);
lua_newtype(L, CLOSEDFILEHANDLE, LUA_TUSERDATA);
luaL_openl(L, iolib);
/* predefined file handles */
newfilewithname(L, stdin, basicfiles[INFILE]);
newfilewithname(L, stdout, basicfiles[OUTFILE]);
newfilewithname(L, stderr, l_s("_STDERR"));
resetfile(L, INFILE);
resetfile(L, OUTFILE);
/* close files when collected */
lua_pushcfunction(L, file_collect);
lua_settagmethod(L, iotag, l_s("gc"));
return 0;
}

View File

@@ -0,0 +1,241 @@
/*
** $Id: lmathlib.c,v 1.38 2001/03/26 14:31:49 roberto Exp $
** Standard mathematical library
** See Copyright Notice in lua.h
*/
#include <stdlib.h>
#include <math.h>
#define LUA_PRIVATE
#include "lua.h"
#include "lauxlib.h"
#include "lualib.h"
#undef PI
#define PI (3.14159265358979323846)
#define RADIANS_PER_DEGREE (PI/180.0)
/*
** If you want Lua to operate in radians (instead of degrees),
** define RADIANS
*/
#define RADIANS
#ifdef RADIANS
#define FROMRAD(a) (a)
#define TORAD(a) (a)
#else
#define FROMRAD(a) ((a)/RADIANS_PER_DEGREE)
#define TORAD(a) ((a)*RADIANS_PER_DEGREE)
#endif
static int math_abs (lua_State *L) {
lua_pushnumber(L, fabs(luaL_check_number(L, 1)));
return 1;
}
static int math_sin (lua_State *L) {
lua_pushnumber(L, sin(TORAD(luaL_check_number(L, 1))));
return 1;
}
static int math_cos (lua_State *L) {
lua_pushnumber(L, cos(TORAD(luaL_check_number(L, 1))));
return 1;
}
static int math_tan (lua_State *L) {
lua_pushnumber(L, tan(TORAD(luaL_check_number(L, 1))));
return 1;
}
static int math_asin (lua_State *L) {
lua_pushnumber(L, FROMRAD(asin(luaL_check_number(L, 1))));
return 1;
}
static int math_acos (lua_State *L) {
lua_pushnumber(L, FROMRAD(acos(luaL_check_number(L, 1))));
return 1;
}
static int math_atan (lua_State *L) {
lua_pushnumber(L, FROMRAD(atan(luaL_check_number(L, 1))));
return 1;
}
static int math_atan2 (lua_State *L) {
lua_pushnumber(L, FROMRAD(atan2(luaL_check_number(L, 1), luaL_check_number(L, 2))));
return 1;
}
static int math_ceil (lua_State *L) {
lua_pushnumber(L, ceil(luaL_check_number(L, 1)));
return 1;
}
static int math_floor (lua_State *L) {
lua_pushnumber(L, floor(luaL_check_number(L, 1)));
return 1;
}
static int math_mod (lua_State *L) {
lua_pushnumber(L, fmod(luaL_check_number(L, 1), luaL_check_number(L, 2)));
return 1;
}
static int math_sqrt (lua_State *L) {
lua_pushnumber(L, sqrt(luaL_check_number(L, 1)));
return 1;
}
static int math_pow (lua_State *L) {
lua_pushnumber(L, pow(luaL_check_number(L, 1), luaL_check_number(L, 2)));
return 1;
}
static int math_log (lua_State *L) {
lua_pushnumber(L, log(luaL_check_number(L, 1)));
return 1;
}
static int math_log10 (lua_State *L) {
lua_pushnumber(L, log10(luaL_check_number(L, 1)));
return 1;
}
static int math_exp (lua_State *L) {
lua_pushnumber(L, exp(luaL_check_number(L, 1)));
return 1;
}
static int math_deg (lua_State *L) {
lua_pushnumber(L, luaL_check_number(L, 1)/RADIANS_PER_DEGREE);
return 1;
}
static int math_rad (lua_State *L) {
lua_pushnumber(L, luaL_check_number(L, 1)*RADIANS_PER_DEGREE);
return 1;
}
static int math_frexp (lua_State *L) {
int e;
lua_pushnumber(L, frexp(luaL_check_number(L, 1), &e));
lua_pushnumber(L, e);
return 2;
}
static int math_ldexp (lua_State *L) {
lua_pushnumber(L, ldexp(luaL_check_number(L, 1), luaL_check_int(L, 2)));
return 1;
}
static int math_min (lua_State *L) {
int n = lua_gettop(L); /* number of arguments */
lua_Number dmin = luaL_check_number(L, 1);
int i;
for (i=2; i<=n; i++) {
lua_Number d = luaL_check_number(L, i);
if (d < dmin)
dmin = d;
}
lua_pushnumber(L, dmin);
return 1;
}
static int math_max (lua_State *L) {
int n = lua_gettop(L); /* number of arguments */
lua_Number dmax = luaL_check_number(L, 1);
int i;
for (i=2; i<=n; i++) {
lua_Number d = luaL_check_number(L, i);
if (d > dmax)
dmax = d;
}
lua_pushnumber(L, dmax);
return 1;
}
static int math_random (lua_State *L) {
/* the `%' avoids the (rare) case of r==1, and is needed also because on
some systems (SunOS!) `rand()' may return a value larger than RAND_MAX */
lua_Number r = (lua_Number)(rand()%RAND_MAX) / (lua_Number)RAND_MAX;
switch (lua_gettop(L)) { /* check number of arguments */
case 0: { /* no arguments */
lua_pushnumber(L, r); /* Number between 0 and 1 */
break;
}
case 1: { /* only upper limit */
int u = luaL_check_int(L, 1);
luaL_arg_check(L, 1<=u, 1, l_s("interval is empty"));
lua_pushnumber(L, (int)(r*u)+1); /* integer between 1 and `u' */
break;
}
case 2: { /* lower and upper limits */
int l = luaL_check_int(L, 1);
int u = luaL_check_int(L, 2);
luaL_arg_check(L, l<=u, 2, l_s("interval is empty"));
lua_pushnumber(L, (int)(r*(u-l+1))+l); /* integer between `l' and `u' */
break;
}
default: lua_error(L, l_s("wrong number of arguments"));
}
return 1;
}
static int math_randomseed (lua_State *L) {
srand(luaL_check_int(L, 1));
return 0;
}
static const luaL_reg mathlib[] = {
{l_s("abs"), math_abs},
{l_s("sin"), math_sin},
{l_s("cos"), math_cos},
{l_s("tan"), math_tan},
{l_s("asin"), math_asin},
{l_s("acos"), math_acos},
{l_s("atan"), math_atan},
{l_s("atan2"), math_atan2},
{l_s("ceil"), math_ceil},
{l_s("floor"), math_floor},
{l_s("mod"), math_mod},
{l_s("frexp"), math_frexp},
{l_s("ldexp"), math_ldexp},
{l_s("sqrt"), math_sqrt},
{l_s("min"), math_min},
{l_s("max"), math_max},
{l_s("log"), math_log},
{l_s("log10"), math_log10},
{l_s("exp"), math_exp},
{l_s("deg"), math_deg},
{l_s("rad"), math_rad},
{l_s("random"), math_random},
{l_s("randomseed"), math_randomseed}
};
/*
** Open math library
*/
LUALIB_API int lua_mathlibopen (lua_State *L) {
luaL_openl(L, mathlib);
lua_pushcfunction(L, math_pow);
lua_settagmethod(L, LUA_TNUMBER, l_s("pow"));
lua_pushnumber(L, PI);
lua_setglobal(L, l_s("PI"));
return 0;
}

View File

@@ -0,0 +1,661 @@
/*
** $Id: lstrlib.c,v 1.69 2001/07/17 18:46:49 roberto Exp $
** Standard library for string operations and pattern-matching
** See Copyright Notice in lua.h
*/
#include <ctype.h>
#include <stddef.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define LUA_PRIVATE
#include "lua.h"
#include "lauxlib.h"
#include "lualib.h"
typedef int sint32; /* a signed version for size_t */
static int str_len (lua_State *L) {
size_t l;
luaL_check_lstr(L, 1, &l);
lua_pushnumber(L, l);
return 1;
}
static sint32 posrelat (sint32 pos, size_t len) {
/* relative string position: negative means back from end */
return (pos>=0) ? pos : (sint32)len+pos+1;
}
static int str_sub (lua_State *L) {
size_t l;
const l_char *s = luaL_check_lstr(L, 1, &l);
sint32 start = posrelat(luaL_check_long(L, 2), l);
sint32 end = posrelat(luaL_opt_long(L, 3, -1), l);
if (start < 1) start = 1;
if (end > (sint32)l) end = l;
if (start <= end)
lua_pushlstring(L, s+start-1, end-start+1);
else lua_pushliteral(L, l_s(""));
return 1;
}
static int str_lower (lua_State *L) {
size_t l;
size_t i;
luaL_Buffer b;
const l_char *s = luaL_check_lstr(L, 1, &l);
luaL_buffinit(L, &b);
for (i=0; i<l; i++)
luaL_putchar(&b, tolower(uchar(s[i])));
luaL_pushresult(&b);
return 1;
}
static int str_upper (lua_State *L) {
size_t l;
size_t i;
luaL_Buffer b;
const l_char *s = luaL_check_lstr(L, 1, &l);
luaL_buffinit(L, &b);
for (i=0; i<l; i++)
luaL_putchar(&b, toupper(uchar(s[i])));
luaL_pushresult(&b);
return 1;
}
static int str_rep (lua_State *L) {
size_t l;
luaL_Buffer b;
const l_char *s = luaL_check_lstr(L, 1, &l);
int n = luaL_check_int(L, 2);
luaL_buffinit(L, &b);
while (n-- > 0)
luaL_addlstring(&b, s, l);
luaL_pushresult(&b);
return 1;
}
static int str_byte (lua_State *L) {
size_t l;
const l_char *s = luaL_check_lstr(L, 1, &l);
sint32 pos = posrelat(luaL_opt_long(L, 2, 1), l);
luaL_arg_check(L, 0<pos && (size_t)pos<=l, 2, l_s("out of range"));
lua_pushnumber(L, uchar(s[pos-1]));
return 1;
}
static int str_char (lua_State *L) {
int n = lua_gettop(L); /* number of arguments */
int i;
luaL_Buffer b;
luaL_buffinit(L, &b);
for (i=1; i<=n; i++) {
int c = luaL_check_int(L, i);
luaL_arg_check(L, uchar(c) == c, i, l_s("invalid value"));
luaL_putchar(&b, uchar(c));
}
luaL_pushresult(&b);
return 1;
}
/*
** {======================================================
** PATTERN MATCHING
** =======================================================
*/
#ifndef MAX_CAPTURES
#define MAX_CAPTURES 32 /* arbitrary limit */
#endif
#define CAP_UNFINISHED (-1)
#define CAP_POSITION (-2)
typedef struct MatchState {
const l_char *src_init; /* init of source string */
const l_char *src_end; /* end (`\0') of source string */
int level; /* total number of captures (finished or unfinished) */
struct {
const l_char *init;
sint32 len;
} capture[MAX_CAPTURES];
lua_State *L;
} MatchState;
#define ESC l_c('%')
#define SPECIALS l_s("^$*+?.([%-")
static int check_capture (MatchState *ms, int l) {
l -= l_c('1');
if (l < 0 || l >= ms->level || ms->capture[l].len == CAP_UNFINISHED)
lua_error(ms->L, l_s("invalid capture index"));
return l;
}
static int capture_to_close (MatchState *ms) {
int level = ms->level;
for (level--; level>=0; level--)
if (ms->capture[level].len == CAP_UNFINISHED) return level;
lua_error(ms->L, l_s("invalid pattern capture"));
return 0; /* to avoid warnings */
}
static const l_char *luaI_classend (MatchState *ms, const l_char *p) {
switch (*p++) {
case ESC:
if (*p == l_c('\0'))
lua_error(ms->L, l_s("malformed pattern (ends with `%')"));
return p+1;
case l_c('['):
if (*p == l_c('^')) p++;
do { /* look for a `]' */
if (*p == l_c('\0'))
lua_error(ms->L, l_s("malformed pattern (missing `]')"));
if (*(p++) == ESC && *p != l_c('\0'))
p++; /* skip escapes (e.g. `%]') */
} while (*p != l_c(']'));
return p+1;
default:
return p;
}
}
static int match_class (l_char c, l_char cl) {
int res;
switch (tolower(uchar(cl))) {
case l_c('a') : res = isalpha(uchar(c)); break;
case l_c('c') : res = iscntrl(uchar(c)); break;
case l_c('d') : res = isdigit(uchar(c)); break;
case l_c('l') : res = islower(uchar(c)); break;
case l_c('p') : res = ispunct(uchar(c)); break;
case l_c('s') : res = isspace(uchar(c)); break;
case l_c('u') : res = isupper(uchar(c)); break;
case l_c('w') : res = isalnum(uchar(c)); break;
case l_c('x') : res = isxdigit(uchar(c)); break;
case l_c('z') : res = (c == l_c('\0')); break;
default: return (cl == c);
}
return (islower(uchar(cl)) ? res : !res);
}
static int matchbracketclass (l_char c, const l_char *p, const l_char *ec) {
int sig = 1;
if (*(p+1) == l_c('^')) {
sig = 0;
p++; /* skip the `^' */
}
while (++p < ec) {
if (*p == ESC) {
p++;
if (match_class(c, *p))
return sig;
}
else if ((*(p+1) == l_c('-')) && (p+2 < ec)) {
p+=2;
if (uchar(*(p-2)) <= uchar(c) && uchar(c) <= uchar(*p))
return sig;
}
else if (*p == c) return sig;
}
return !sig;
}
static int luaI_singlematch (l_char c, const l_char *p, const l_char *ep) {
switch (*p) {
case l_c('.'): /* matches any char */
return 1;
case ESC:
return match_class(c, *(p+1));
case l_c('['):
return matchbracketclass(c, p, ep-1);
default:
return (*p == c);
}
}
static const l_char *match (MatchState *ms, const l_char *s, const l_char *p);
static const l_char *matchbalance (MatchState *ms, const l_char *s,
const l_char *p) {
if (*p == 0 || *(p+1) == 0)
lua_error(ms->L, l_s("unbalanced pattern"));
if (*s != *p) return NULL;
else {
int b = *p;
int e = *(p+1);
int cont = 1;
while (++s < ms->src_end) {
if (*s == e) {
if (--cont == 0) return s+1;
}
else if (*s == b) cont++;
}
}
return NULL; /* string ends out of balance */
}
static const l_char *max_expand (MatchState *ms, const l_char *s,
const l_char *p, const l_char *ep) {
sint32 i = 0; /* counts maximum expand for item */
while ((s+i)<ms->src_end && luaI_singlematch(*(s+i), p, ep))
i++;
/* keeps trying to match with the maximum repetitions */
while (i>=0) {
const l_char *res = match(ms, (s+i), ep+1);
if (res) return res;
i--; /* else didn't match; reduce 1 repetition to try again */
}
return NULL;
}
static const l_char *min_expand (MatchState *ms, const l_char *s,
const l_char *p, const l_char *ep) {
for (;;) {
const l_char *res = match(ms, s, ep+1);
if (res != NULL)
return res;
else if (s<ms->src_end && luaI_singlematch(*s, p, ep))
s++; /* try with one more repetition */
else return NULL;
}
}
static const l_char *start_capture (MatchState *ms, const l_char *s,
const l_char *p, int what) {
const l_char *res;
int level = ms->level;
if (level >= MAX_CAPTURES) lua_error(ms->L, l_s("too many captures"));
ms->capture[level].init = s;
ms->capture[level].len = what;
ms->level = level+1;
if ((res=match(ms, s, p)) == NULL) /* match failed? */
ms->level--; /* undo capture */
return res;
}
static const l_char *end_capture (MatchState *ms, const l_char *s,
const l_char *p) {
int l = capture_to_close(ms);
const l_char *res;
ms->capture[l].len = s - ms->capture[l].init; /* close capture */
if ((res = match(ms, s, p)) == NULL) /* match failed? */
ms->capture[l].len = CAP_UNFINISHED; /* undo capture */
return res;
}
static const l_char *match_capture (MatchState *ms, const l_char *s, int l) {
size_t len;
l = check_capture(ms, l);
len = ms->capture[l].len;
if ((size_t)(ms->src_end-s) >= len &&
memcmp(ms->capture[l].init, s, len) == 0)
return s+len;
else return NULL;
}
static const l_char *match (MatchState *ms, const l_char *s, const l_char *p) {
init: /* using goto's to optimize tail recursion */
switch (*p) {
case l_c('('): /* start capture */
if (*(p+1) == l_c(')')) /* position capture? */
return start_capture(ms, s, p+2, CAP_POSITION);
else
return start_capture(ms, s, p+1, CAP_UNFINISHED);
case l_c(')'): /* end capture */
return end_capture(ms, s, p+1);
case ESC: /* may be %[0-9] or %b */
if (isdigit(uchar(*(p+1)))) { /* capture? */
s = match_capture(ms, s, *(p+1));
if (s == NULL) return NULL;
p+=2; goto init; /* else return match(ms, s, p+2) */
}
else if (*(p+1) == l_c('b')) { /* balanced string? */
s = matchbalance(ms, s, p+2);
if (s == NULL) return NULL;
p+=4; goto init; /* else return match(ms, s, p+4); */
}
else goto dflt; /* case default */
case l_c('\0'): /* end of pattern */
return s; /* match succeeded */
case l_c('$'):
if (*(p+1) == l_c('\0')) /* is the `$' the last char in pattern? */
return (s == ms->src_end) ? s : NULL; /* check end of string */
else goto dflt;
default: dflt: { /* it is a pattern item */
const l_char *ep = luaI_classend(ms, p); /* points to what is next */
int m = s<ms->src_end && luaI_singlematch(*s, p, ep);
switch (*ep) {
case l_c('?'): { /* optional */
const l_char *res;
if (m && ((res=match(ms, s+1, ep+1)) != NULL))
return res;
p=ep+1; goto init; /* else return match(ms, s, ep+1); */
}
case l_c('*'): /* 0 or more repetitions */
return max_expand(ms, s, p, ep);
case l_c('+'): /* 1 or more repetitions */
return (m ? max_expand(ms, s+1, p, ep) : NULL);
case l_c('-'): /* 0 or more repetitions (minimum) */
return min_expand(ms, s, p, ep);
default:
if (!m) return NULL;
s++; p=ep; goto init; /* else return match(ms, s+1, ep); */
}
}
}
}
static const l_char *lmemfind (const l_char *s1, size_t l1,
const l_char *s2, size_t l2) {
if (l2 == 0) return s1; /* empty strings are everywhere */
else if (l2 > l1) return NULL; /* avoids a negative `l1' */
else {
const l_char *init; /* to search for a `*s2' inside `s1' */
l2--; /* 1st char will be checked by `memchr' */
l1 = l1-l2; /* `s2' cannot be found after that */
while (l1 > 0 && (init = (const l_char *)memchr(s1, *s2, l1)) != NULL) {
init++; /* 1st char is already checked */
if (memcmp(init, s2+1, l2) == 0)
return init-1;
else { /* correct `l1' and `s1' to try again */
l1 -= init-s1;
s1 = init;
}
}
return NULL; /* not found */
}
}
static void push_onecapture (MatchState *ms, int i) {
int l = ms->capture[i].len;
if (l == CAP_UNFINISHED) lua_error(ms->L, l_s("unfinished capture"));
if (l == CAP_POSITION)
lua_pushnumber(ms->L, ms->capture[i].init - ms->src_init + 1);
else
lua_pushlstring(ms->L, ms->capture[i].init, l);
}
static int push_captures (MatchState *ms) {
int i;
luaL_checkstack(ms->L, ms->level, l_s("too many captures"));
for (i=0; i<ms->level; i++)
push_onecapture(ms, i);
return ms->level; /* number of strings pushed */
}
static int str_find (lua_State *L) {
size_t l1, l2;
const l_char *s = luaL_check_lstr(L, 1, &l1);
const l_char *p = luaL_check_lstr(L, 2, &l2);
sint32 init = posrelat(luaL_opt_long(L, 3, 1), l1) - 1;
luaL_arg_check(L, 0 <= init && (size_t)init <= l1, 3, l_s("out of range"));
if (lua_gettop(L) > 3 || /* extra argument? */
strpbrk(p, SPECIALS) == NULL) { /* or no special characters? */
const l_char *s2 = lmemfind(s+init, l1-init, p, l2);
if (s2) {
lua_pushnumber(L, s2-s+1);
lua_pushnumber(L, s2-s+l2);
return 2;
}
}
else {
MatchState ms;
int anchor = (*p == l_c('^')) ? (p++, 1) : 0;
const l_char *s1=s+init;
ms.L = L;
ms.src_init = s;
ms.src_end = s+l1;
do {
const l_char *res;
ms.level = 0;
if ((res=match(&ms, s1, p)) != NULL) {
lua_pushnumber(L, s1-s+1); /* start */
lua_pushnumber(L, res-s); /* end */
return push_captures(&ms) + 2;
}
} while (s1++<ms.src_end && !anchor);
}
lua_pushnil(L); /* not found */
return 1;
}
static void add_s (MatchState *ms, luaL_Buffer *b) {
lua_State *L = ms->L;
if (lua_isstring(L, 3)) {
const l_char *news = lua_tostring(L, 3);
size_t l = lua_strlen(L, 3);
size_t i;
for (i=0; i<l; i++) {
if (news[i] != ESC)
luaL_putchar(b, news[i]);
else {
i++; /* skip ESC */
if (!isdigit(uchar(news[i])))
luaL_putchar(b, news[i]);
else {
int level = check_capture(ms, news[i]);
push_onecapture(ms, level);
luaL_addvalue(b); /* add capture to accumulated result */
}
}
}
}
else { /* is a function */
int n;
lua_pushvalue(L, 3);
n = push_captures(ms);
lua_rawcall(L, n, 1);
if (lua_isstring(L, -1))
luaL_addvalue(b); /* add return to accumulated result */
else
lua_pop(L, 1); /* function result is not a string: pop it */
}
}
static int str_gsub (lua_State *L) {
size_t srcl;
const l_char *src = luaL_check_lstr(L, 1, &srcl);
const l_char *p = luaL_check_string(L, 2);
int max_s = luaL_opt_int(L, 4, srcl+1);
int anchor = (*p == l_c('^')) ? (p++, 1) : 0;
int n = 0;
MatchState ms;
luaL_Buffer b;
luaL_arg_check(L,
lua_gettop(L) >= 3 && (lua_isstring(L, 3) || lua_isfunction(L, 3)),
3, l_s("string or function expected"));
luaL_buffinit(L, &b);
ms.L = L;
ms.src_init = src;
ms.src_end = src+srcl;
while (n < max_s) {
const l_char *e;
ms.level = 0;
e = match(&ms, src, p);
if (e) {
n++;
add_s(&ms, &b);
}
if (e && e>src) /* non empty match? */
src = e; /* skip it */
else if (src < ms.src_end)
luaL_putchar(&b, *src++);
else break;
if (anchor) break;
}
luaL_addlstring(&b, src, ms.src_end-src);
luaL_pushresult(&b);
lua_pushnumber(L, n); /* number of substitutions */
return 2;
}
/* }====================================================== */
/* maximum size of each formatted item (> len(format('%99.99f', -1e308))) */
#define MAX_ITEM 512
/* maximum size of each format specification (such as '%-099.99d') */
#define MAX_FORMAT 20
static void luaI_addquoted (lua_State *L, luaL_Buffer *b, int arg) {
size_t l;
const l_char *s = luaL_check_lstr(L, arg, &l);
luaL_putchar(b, l_c('"'));
while (l--) {
switch (*s) {
case l_c('"'): case l_c('\\'): case l_c('\n'):
luaL_putchar(b, l_c('\\'));
luaL_putchar(b, *s);
break;
case l_c('\0'): luaL_addlstring(b, l_s("\\000"), 4); break;
default: luaL_putchar(b, *s);
}
s++;
}
luaL_putchar(b, l_c('"'));
}
static const l_char *scanformat (lua_State *L, const l_char *strfrmt,
l_char *form, int *hasprecision) {
const l_char *p = strfrmt;
while (strchr(l_s("-+ #0"), *p)) p++; /* skip flags */
if (isdigit(uchar(*p))) p++; /* skip width */
if (isdigit(uchar(*p))) p++; /* (2 digits at most) */
if (*p == l_c('.')) {
p++;
*hasprecision = 1;
if (isdigit(uchar(*p))) p++; /* skip precision */
if (isdigit(uchar(*p))) p++; /* (2 digits at most) */
}
if (isdigit(uchar(*p)))
lua_error(L, l_s("invalid format (width or precision too long)"));
if (p-strfrmt+2 > MAX_FORMAT) /* +2 to include `%' and the specifier */
lua_error(L, l_s("invalid format (too long)"));
form[0] = l_c('%');
strncpy(form+1, strfrmt, p-strfrmt+1);
form[p-strfrmt+2] = 0;
return p;
}
static int str_format (lua_State *L) {
int arg = 1;
size_t sfl;
const l_char *strfrmt = luaL_check_lstr(L, arg, &sfl);
const l_char *strfrmt_end = strfrmt+sfl;
luaL_Buffer b;
luaL_buffinit(L, &b);
while (strfrmt < strfrmt_end) {
if (*strfrmt != l_c('%'))
luaL_putchar(&b, *strfrmt++);
else if (*++strfrmt == l_c('%'))
luaL_putchar(&b, *strfrmt++); /* %% */
else { /* format item */
l_char form[MAX_FORMAT]; /* to store the format (`%...') */
l_char buff[MAX_ITEM]; /* to store the formatted item */
int hasprecision = 0;
if (isdigit(uchar(*strfrmt)) && *(strfrmt+1) == l_c('$'))
lua_error(L, l_s("obsolete `format' option (d$)"));
arg++;
strfrmt = scanformat(L, strfrmt, form, &hasprecision);
switch (*strfrmt++) {
case l_c('c'): case l_c('d'): case l_c('i'):
sprintf(buff, form, luaL_check_int(L, arg));
break;
case l_c('o'): case l_c('u'): case l_c('x'): case l_c('X'):
sprintf(buff, form, (unsigned int)luaL_check_number(L, arg));
break;
case l_c('e'): case l_c('E'): case l_c('f'):
case l_c('g'): case l_c('G'):
sprintf(buff, form, luaL_check_number(L, arg));
break;
case l_c('q'):
luaI_addquoted(L, &b, arg);
continue; /* skip the `addsize' at the end */
case l_c('s'): {
size_t l;
const l_char *s = luaL_check_lstr(L, arg, &l);
if (!hasprecision && l >= 100) {
/* no precision and string is too long to be formatted;
keep original string */
lua_pushvalue(L, arg);
luaL_addvalue(&b);
continue; /* skip the `addsize' at the end */
}
else {
sprintf(buff, form, s);
break;
}
}
default: /* also treat cases `pnLlh' */
lua_error(L, l_s("invalid option in `format'"));
}
luaL_addlstring(&b, buff, strlen(buff));
}
}
luaL_pushresult(&b);
return 1;
}
static const luaL_reg strlib[] = {
{l_s("strlen"), str_len},
{l_s("strsub"), str_sub},
{l_s("strlower"), str_lower},
{l_s("strupper"), str_upper},
{l_s("strchar"), str_char},
{l_s("strrep"), str_rep},
{l_s("strbyte"), str_byte},
{l_s("format"), str_format},
{l_s("strfind"), str_find},
{l_s("gsub"), str_gsub}
};
/*
** Open string library
*/
LUALIB_API int lua_strlibopen (lua_State *L) {
luaL_openl(L, strlib);
return 0;
}