1997-09-16 23:25:59 +04:00
|
|
|
/*
|
1997-11-28 15:40:37 +03:00
|
|
|
** $Id: liolib.c,v 1.7 1997/11/27 15:59:44 roberto Exp roberto $
|
1997-09-16 23:25:59 +04:00
|
|
|
** Standard I/O (and system) library
|
|
|
|
** See Copyright Notice in lua.h
|
|
|
|
*/
|
|
|
|
|
|
|
|
|
1993-07-28 17:18:00 +04:00
|
|
|
#include <stdio.h>
|
1994-11-16 20:39:16 +03:00
|
|
|
#include <string.h>
|
|
|
|
#include <time.h>
|
|
|
|
#include <stdlib.h>
|
1996-02-09 22:02:30 +03:00
|
|
|
#include <errno.h>
|
1993-07-28 17:18:00 +04:00
|
|
|
|
1997-09-16 23:25:59 +04:00
|
|
|
#include "lauxlib.h"
|
1993-07-28 17:18:00 +04:00
|
|
|
#include "lua.h"
|
1995-10-17 17:12:45 +03:00
|
|
|
#include "luadebug.h"
|
1994-08-17 19:10:04 +04:00
|
|
|
#include "lualib.h"
|
1993-07-28 17:18:00 +04:00
|
|
|
|
1996-11-01 20:03:36 +03:00
|
|
|
|
1997-09-16 23:25:59 +04:00
|
|
|
#ifndef OLD_ANSI
|
|
|
|
#include <locale.h>
|
|
|
|
#else
|
|
|
|
#define strcoll(a,b) strcmp(a,b)
|
|
|
|
#define setlocale(a,b) 0
|
|
|
|
#define LC_ALL 0
|
|
|
|
#define LC_COLLATE 0
|
|
|
|
#define LC_CTYPE 0
|
|
|
|
#define LC_MONETARY 0
|
|
|
|
#define LC_NUMERIC 0
|
|
|
|
#define LC_TIME 0
|
|
|
|
#define strerror(e) "O.S. is unable to define the error"
|
|
|
|
#endif
|
1997-03-20 23:36:58 +03:00
|
|
|
|
1995-10-04 16:53:10 +03:00
|
|
|
|
1997-11-28 15:40:37 +03:00
|
|
|
#define CLOSEDTAG 2
|
|
|
|
#define IOTAG 1
|
1997-11-27 18:59:44 +03:00
|
|
|
|
|
|
|
#define FINPUT "_INPUT"
|
|
|
|
#define FOUTPUT "_OUTPUT"
|
|
|
|
|
|
|
|
|
1997-11-28 15:40:37 +03:00
|
|
|
#define FIRSTARG 3 /* 1st and 2nd are upvalues */
|
|
|
|
|
|
|
|
|
1995-11-10 20:55:48 +03:00
|
|
|
#ifdef POPEN
|
|
|
|
FILE *popen();
|
|
|
|
int pclose();
|
|
|
|
#else
|
1995-10-04 16:53:10 +03:00
|
|
|
#define popen(x,y) NULL /* that is, popen always fails */
|
|
|
|
#define pclose(x) (-1)
|
|
|
|
#endif
|
|
|
|
|
1995-11-03 18:43:50 +03:00
|
|
|
|
1997-11-28 15:40:37 +03:00
|
|
|
static int gettag (int i)
|
1997-11-19 20:29:23 +03:00
|
|
|
{
|
1997-11-28 15:40:37 +03:00
|
|
|
return lua_getnumber(lua_getparam(i));
|
1997-11-19 20:29:23 +03:00
|
|
|
}
|
1997-09-16 23:25:59 +04:00
|
|
|
|
|
|
|
|
1996-03-14 18:55:18 +03:00
|
|
|
static void pushresult (int i)
|
|
|
|
{
|
|
|
|
if (i)
|
1996-11-01 20:03:36 +03:00
|
|
|
lua_pushuserdata(NULL);
|
|
|
|
else {
|
|
|
|
lua_pushnil();
|
|
|
|
lua_pushstring(strerror(errno));
|
1995-10-04 16:53:10 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
1996-11-01 20:03:36 +03:00
|
|
|
|
1997-10-30 23:29:09 +03:00
|
|
|
static int ishandler (lua_Object f)
|
|
|
|
{
|
|
|
|
if (lua_isuserdata(f)) {
|
1997-11-27 18:59:44 +03:00
|
|
|
if (lua_tag(f) == gettag(CLOSEDTAG))
|
1997-10-30 23:29:09 +03:00
|
|
|
lua_error("trying to access a closed file");
|
1997-11-27 18:59:44 +03:00
|
|
|
return lua_tag(f) == gettag(IOTAG);
|
1997-10-30 23:29:09 +03:00
|
|
|
}
|
|
|
|
else return 0;
|
|
|
|
}
|
1997-06-27 00:39:10 +04:00
|
|
|
|
|
|
|
static FILE *getfile (char *name)
|
1995-10-04 16:53:10 +03:00
|
|
|
{
|
1997-06-27 00:39:10 +04:00
|
|
|
lua_Object f = lua_getglobal(name);
|
1997-10-30 23:29:09 +03:00
|
|
|
if (!ishandler(f))
|
1997-11-19 21:16:33 +03:00
|
|
|
luaL_verror("global variable %.50s is not a file handle", name);
|
1997-06-27 00:39:10 +04:00
|
|
|
return lua_getuserdata(f);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
1997-10-30 23:29:09 +03:00
|
|
|
static FILE *getfileparam (char *name, int *arg)
|
|
|
|
{
|
|
|
|
lua_Object f = lua_getparam(*arg);
|
|
|
|
if (ishandler(f)) {
|
|
|
|
(*arg)++;
|
|
|
|
return lua_getuserdata(f);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
return getfile(name);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
1997-06-27 00:39:10 +04:00
|
|
|
static void closefile (char *name)
|
|
|
|
{
|
|
|
|
FILE *f = getfile(name);
|
|
|
|
if (f == stdin || f == stdout) return;
|
1996-11-01 20:03:36 +03:00
|
|
|
if (pclose(f) == -1)
|
|
|
|
fclose(f);
|
1997-09-23 18:12:44 +04:00
|
|
|
lua_pushobject(lua_getglobal(name));
|
1997-11-27 18:59:44 +03:00
|
|
|
lua_settag(gettag(CLOSEDTAG));
|
1995-10-04 16:53:10 +03:00
|
|
|
}
|
|
|
|
|
1996-11-01 20:03:36 +03:00
|
|
|
|
1997-11-28 15:40:37 +03:00
|
|
|
static void setfile (FILE *f, char *name, int tag)
|
1997-06-27 00:39:10 +04:00
|
|
|
{
|
1997-11-28 15:40:37 +03:00
|
|
|
lua_pushusertag(f, tag);
|
1997-06-27 00:39:10 +04:00
|
|
|
lua_setglobal(name);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
static void setreturn (FILE *f, char *name)
|
|
|
|
{
|
1997-11-28 15:40:37 +03:00
|
|
|
int tag = gettag(IOTAG);
|
|
|
|
setfile(f, name, tag);
|
|
|
|
lua_pushusertag(f, tag);
|
1997-06-27 00:39:10 +04:00
|
|
|
}
|
|
|
|
|
1996-11-01 20:03:36 +03:00
|
|
|
|
1993-07-28 17:18:00 +04:00
|
|
|
static void io_readfrom (void)
|
|
|
|
{
|
1997-06-27 00:39:10 +04:00
|
|
|
FILE *current;
|
1997-11-28 15:40:37 +03:00
|
|
|
lua_Object f = lua_getparam(FIRSTARG);
|
1997-06-27 00:39:10 +04:00
|
|
|
if (f == LUA_NOOBJECT) {
|
1997-11-27 18:59:44 +03:00
|
|
|
closefile(FINPUT);
|
1997-06-27 00:39:10 +04:00
|
|
|
current = stdin;
|
|
|
|
}
|
1997-11-27 18:59:44 +03:00
|
|
|
else if (lua_tag(f) == gettag(IOTAG))
|
1997-06-27 00:39:10 +04:00
|
|
|
current = lua_getuserdata(f);
|
1996-11-01 20:03:36 +03:00
|
|
|
else {
|
1997-11-28 15:40:37 +03:00
|
|
|
char *s = luaL_check_string(FIRSTARG);
|
1997-06-27 00:39:10 +04:00
|
|
|
current = (*s == '|') ? popen(s+1, "r") : fopen(s, "r");
|
|
|
|
if (current == NULL) {
|
1996-11-01 20:03:36 +03:00
|
|
|
pushresult(0);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
1997-11-27 18:59:44 +03:00
|
|
|
setreturn(current, FINPUT);
|
1993-07-28 17:18:00 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
static void io_writeto (void)
|
|
|
|
{
|
1997-06-27 00:39:10 +04:00
|
|
|
FILE *current;
|
1997-11-28 15:40:37 +03:00
|
|
|
lua_Object f = lua_getparam(FIRSTARG);
|
1997-06-27 00:39:10 +04:00
|
|
|
if (f == LUA_NOOBJECT) {
|
1997-11-27 18:59:44 +03:00
|
|
|
closefile(FOUTPUT);
|
1997-06-27 00:39:10 +04:00
|
|
|
current = stdout;
|
|
|
|
}
|
1997-11-27 18:59:44 +03:00
|
|
|
else if (lua_tag(f) == gettag(IOTAG))
|
1997-06-27 00:39:10 +04:00
|
|
|
current = lua_getuserdata(f);
|
1996-11-01 20:03:36 +03:00
|
|
|
else {
|
1997-11-28 15:40:37 +03:00
|
|
|
char *s = luaL_check_string(FIRSTARG);
|
1997-06-27 00:39:10 +04:00
|
|
|
current = (*s == '|') ? popen(s+1,"w") : fopen(s,"w");
|
|
|
|
if (current == NULL) {
|
1996-11-01 20:03:36 +03:00
|
|
|
pushresult(0);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
1997-11-27 18:59:44 +03:00
|
|
|
setreturn(current, FOUTPUT);
|
1993-07-28 17:18:00 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
|
1993-12-17 21:41:19 +03:00
|
|
|
static void io_appendto (void)
|
|
|
|
{
|
1997-11-28 15:40:37 +03:00
|
|
|
char *s = luaL_check_string(FIRSTARG);
|
1996-11-01 20:03:36 +03:00
|
|
|
FILE *fp = fopen (s, "a");
|
1997-06-27 00:39:10 +04:00
|
|
|
if (fp != NULL)
|
1997-11-27 18:59:44 +03:00
|
|
|
setreturn(fp, FOUTPUT);
|
1995-11-10 20:55:48 +03:00
|
|
|
else
|
1996-11-01 20:03:36 +03:00
|
|
|
pushresult(0);
|
1995-11-10 20:55:48 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
|
1996-11-01 20:03:36 +03:00
|
|
|
#define NEED_OTHER (EOF-1) /* just some flag different from EOF */
|
1995-11-10 20:55:48 +03:00
|
|
|
|
|
|
|
static void io_read (void)
|
|
|
|
{
|
1997-11-28 15:40:37 +03:00
|
|
|
int arg = FIRSTARG;
|
1997-11-27 18:59:44 +03:00
|
|
|
FILE *f = getfileparam(FINPUT, &arg);
|
1996-11-01 20:03:36 +03:00
|
|
|
char *buff;
|
1997-10-30 23:29:09 +03:00
|
|
|
char *p = luaL_opt_string(arg, "[^\n]*{\n}");
|
1996-11-01 20:03:36 +03:00
|
|
|
int inskip = 0; /* to control {skips} */
|
|
|
|
int c = NEED_OTHER;
|
1997-03-27 01:23:15 +03:00
|
|
|
luaI_emptybuff();
|
1996-11-01 20:03:36 +03:00
|
|
|
while (*p) {
|
1997-03-05 16:32:41 +03:00
|
|
|
if (*p == '{') {
|
|
|
|
inskip++;
|
|
|
|
p++;
|
|
|
|
}
|
|
|
|
else if (*p == '}') {
|
|
|
|
if (inskip == 0)
|
1997-04-05 02:24:51 +04:00
|
|
|
lua_error("unbalanced braces in read pattern");
|
1997-03-05 16:32:41 +03:00
|
|
|
inskip--;
|
1996-11-01 20:03:36 +03:00
|
|
|
p++;
|
|
|
|
}
|
|
|
|
else {
|
1997-09-16 23:25:59 +04:00
|
|
|
char *ep; /* get what is next */
|
1996-11-20 16:47:59 +03:00
|
|
|
int m; /* match result */
|
1997-06-27 00:39:10 +04:00
|
|
|
if (c == NEED_OTHER) c = getc(f);
|
1997-09-16 23:25:59 +04:00
|
|
|
m = luaI_singlematch((c == EOF) ? 0 : (char)c, p, &ep);
|
1996-11-20 16:47:59 +03:00
|
|
|
if (m) {
|
1997-03-05 16:32:41 +03:00
|
|
|
if (inskip == 0) luaI_addchar(c);
|
1996-11-01 20:03:36 +03:00
|
|
|
c = NEED_OTHER;
|
1996-01-12 20:00:30 +03:00
|
|
|
}
|
1996-11-01 20:03:36 +03:00
|
|
|
switch (*ep) {
|
|
|
|
case '*': /* repetition */
|
|
|
|
if (!m) p = ep+1; /* else stay in (repeat) the same item */
|
|
|
|
break;
|
|
|
|
case '?': /* optional */
|
|
|
|
p = ep+1; /* continues reading the pattern */
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
if (m) p = ep; /* continues reading the pattern */
|
|
|
|
else
|
|
|
|
goto break_while; /* pattern fails */
|
1995-11-10 20:55:48 +03:00
|
|
|
}
|
1993-07-28 17:18:00 +04:00
|
|
|
}
|
1996-11-01 20:03:36 +03:00
|
|
|
} break_while:
|
|
|
|
if (c >= 0) /* not EOF nor NEED_OTHER? */
|
1997-06-27 00:39:10 +04:00
|
|
|
ungetc(c, f);
|
1996-11-01 20:03:36 +03:00
|
|
|
buff = luaI_addchar(0);
|
|
|
|
if (*buff != 0 || *p == 0) /* read something or did not fail? */
|
|
|
|
lua_pushstring(buff);
|
1995-11-10 20:55:48 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
|
1993-07-28 17:18:00 +04:00
|
|
|
static void io_write (void)
|
|
|
|
{
|
1997-11-28 15:40:37 +03:00
|
|
|
int arg = FIRSTARG;
|
1997-11-27 18:59:44 +03:00
|
|
|
FILE *f = getfileparam(FOUTPUT, &arg);
|
1996-11-01 20:03:36 +03:00
|
|
|
int status = 1;
|
|
|
|
char *s;
|
1997-04-06 18:08:08 +04:00
|
|
|
while ((s = luaL_opt_string(arg++, NULL)) != NULL)
|
1997-06-27 00:39:10 +04:00
|
|
|
status = status && (fputs(s, f) != EOF);
|
1996-11-01 20:03:36 +03:00
|
|
|
pushresult(status);
|
1993-07-28 17:18:00 +04:00
|
|
|
}
|
|
|
|
|
1996-11-01 20:03:36 +03:00
|
|
|
|
1994-08-12 03:11:57 +04:00
|
|
|
static void io_execute (void)
|
1993-07-28 17:18:00 +04:00
|
|
|
{
|
1997-04-06 18:08:08 +04:00
|
|
|
lua_pushnumber(system(luaL_check_string(1)));
|
1993-07-28 17:18:00 +04:00
|
|
|
}
|
|
|
|
|
1996-11-01 20:03:36 +03:00
|
|
|
|
1994-08-12 03:11:57 +04:00
|
|
|
static void io_remove (void)
|
1993-07-28 17:18:00 +04:00
|
|
|
{
|
1997-04-06 18:08:08 +04:00
|
|
|
pushresult(remove(luaL_check_string(1)) == 0);
|
1996-03-14 18:55:18 +03:00
|
|
|
}
|
|
|
|
|
1996-11-01 20:03:36 +03:00
|
|
|
|
1996-03-14 18:55:18 +03:00
|
|
|
static void io_rename (void)
|
|
|
|
{
|
1997-04-06 18:08:08 +04:00
|
|
|
pushresult(rename(luaL_check_string(1),
|
|
|
|
luaL_check_string(2)) == 0);
|
1996-03-14 18:55:18 +03:00
|
|
|
}
|
|
|
|
|
1996-11-01 20:03:36 +03:00
|
|
|
|
1996-03-14 18:55:18 +03:00
|
|
|
static void io_tmpname (void)
|
|
|
|
{
|
|
|
|
lua_pushstring(tmpnam(NULL));
|
1993-07-28 17:18:00 +04:00
|
|
|
}
|
|
|
|
|
1996-02-09 22:02:30 +03:00
|
|
|
|
1994-08-12 03:11:57 +04:00
|
|
|
|
|
|
|
static void io_getenv (void)
|
|
|
|
{
|
1997-09-16 23:25:59 +04:00
|
|
|
lua_pushstring(getenv(luaL_check_string(1))); /* if NULL push nil */
|
1994-08-12 03:11:57 +04:00
|
|
|
}
|
|
|
|
|
1996-11-01 20:03:36 +03:00
|
|
|
|
1994-10-19 20:02:20 +03:00
|
|
|
static void io_date (void)
|
|
|
|
{
|
1996-11-01 20:03:36 +03:00
|
|
|
time_t t;
|
|
|
|
struct tm *tm;
|
1997-04-06 18:08:08 +04:00
|
|
|
char *s = luaL_opt_string(1, "%c");
|
1996-11-01 20:03:36 +03:00
|
|
|
char b[BUFSIZ];
|
|
|
|
time(&t); tm = localtime(&t);
|
|
|
|
if (strftime(b,sizeof(b),s,tm))
|
|
|
|
lua_pushstring(b);
|
|
|
|
else
|
|
|
|
lua_error("invalid `date' format");
|
1994-10-19 20:02:20 +03:00
|
|
|
}
|
1997-07-01 23:32:41 +04:00
|
|
|
|
|
|
|
|
|
|
|
static void setloc (void)
|
|
|
|
{
|
|
|
|
static int cat[] = {LC_ALL, LC_COLLATE, LC_CTYPE, LC_MONETARY, LC_NUMERIC,
|
|
|
|
LC_TIME};
|
|
|
|
int op = (int)luaL_opt_number(2, 0);
|
|
|
|
luaL_arg_check(0 <= op && op <= 5, 2, "invalid option");
|
|
|
|
lua_pushstring(setlocale(cat[op], luaL_check_string(1)));
|
|
|
|
}
|
1997-09-16 23:25:59 +04:00
|
|
|
|
1996-11-01 20:03:36 +03:00
|
|
|
|
1994-10-19 20:02:20 +03:00
|
|
|
static void io_exit (void)
|
1994-08-12 03:11:57 +04:00
|
|
|
{
|
1996-11-01 20:03:36 +03:00
|
|
|
lua_Object o = lua_getparam(1);
|
|
|
|
exit(lua_isnumber(o) ? (int)lua_getnumber(o) : 1);
|
1994-08-12 03:11:57 +04:00
|
|
|
}
|
|
|
|
|
1996-11-01 20:03:36 +03:00
|
|
|
|
1994-08-18 02:34:20 +04:00
|
|
|
static void io_debug (void)
|
|
|
|
{
|
1996-11-01 20:03:36 +03:00
|
|
|
while (1) {
|
1994-08-18 02:34:20 +04:00
|
|
|
char buffer[250];
|
1994-12-13 18:55:41 +03:00
|
|
|
fprintf(stderr, "lua_debug> ");
|
1996-03-12 18:56:03 +03:00
|
|
|
if (fgets(buffer, sizeof(buffer), stdin) == 0) return;
|
1996-05-27 18:06:58 +04:00
|
|
|
if (strcmp(buffer, "cont\n") == 0) return;
|
1994-08-18 02:34:20 +04:00
|
|
|
lua_dostring(buffer);
|
|
|
|
}
|
|
|
|
}
|
1994-08-12 03:11:57 +04:00
|
|
|
|
1995-10-17 17:12:45 +03:00
|
|
|
|
1996-05-04 00:10:59 +04:00
|
|
|
static void lua_printstack (FILE *f)
|
1995-10-17 17:12:45 +03:00
|
|
|
{
|
1997-03-18 18:30:50 +03:00
|
|
|
int level = 1; /* skip level 0 (it's this function) */
|
1995-10-17 17:12:45 +03:00
|
|
|
lua_Object func;
|
1996-11-01 20:03:36 +03:00
|
|
|
while ((func = lua_stackedfunction(level++)) != LUA_NOOBJECT) {
|
1995-10-26 17:21:56 +03:00
|
|
|
char *name;
|
|
|
|
int currentline;
|
1997-06-18 21:33:30 +04:00
|
|
|
char *filename;
|
|
|
|
int linedefined;
|
|
|
|
lua_funcinfo(func, &filename, &linedefined);
|
1997-03-18 18:30:50 +03:00
|
|
|
fprintf(f, (level==2) ? "Active Stack:\n\t" : "\t");
|
1996-11-01 20:03:36 +03:00
|
|
|
switch (*lua_getobjname(func, &name)) {
|
1995-10-26 17:21:56 +03:00
|
|
|
case 'g':
|
1995-11-03 18:43:50 +03:00
|
|
|
fprintf(f, "function %s", name);
|
1995-10-26 17:21:56 +03:00
|
|
|
break;
|
1997-06-17 22:09:31 +04:00
|
|
|
case 't':
|
1997-04-30 23:55:47 +04:00
|
|
|
fprintf(f, "`%s' tag method", name);
|
1995-10-26 17:21:56 +03:00
|
|
|
break;
|
1996-11-01 20:03:36 +03:00
|
|
|
default: {
|
1995-10-26 17:21:56 +03:00
|
|
|
if (linedefined == 0)
|
1995-11-03 18:43:50 +03:00
|
|
|
fprintf(f, "main of %s", filename);
|
1995-10-26 17:21:56 +03:00
|
|
|
else if (linedefined < 0)
|
1995-11-03 18:43:50 +03:00
|
|
|
fprintf(f, "%s", filename);
|
1995-10-26 17:21:56 +03:00
|
|
|
else
|
1995-11-03 18:43:50 +03:00
|
|
|
fprintf(f, "function (%s:%d)", filename, linedefined);
|
1997-06-18 21:33:30 +04:00
|
|
|
filename = NULL;
|
1995-10-17 17:12:45 +03:00
|
|
|
}
|
1995-10-26 17:21:56 +03:00
|
|
|
}
|
|
|
|
if ((currentline = lua_currentline(func)) > 0)
|
1996-03-12 18:56:03 +03:00
|
|
|
fprintf(f, " at line %d", currentline);
|
1997-06-18 21:33:30 +04:00
|
|
|
if (filename)
|
|
|
|
fprintf(f, " [in file %s]", filename);
|
1995-11-03 18:43:50 +03:00
|
|
|
fprintf(f, "\n");
|
1995-10-17 17:12:45 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
1995-11-03 18:43:50 +03:00
|
|
|
|
|
|
|
static void errorfb (void)
|
|
|
|
{
|
1997-04-02 21:44:18 +04:00
|
|
|
fprintf(stderr, "lua: %s\n", lua_getstring(lua_getparam(1)));
|
1995-11-03 18:43:50 +03:00
|
|
|
lua_printstack(stderr);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
1997-03-27 01:23:15 +03:00
|
|
|
|
1997-03-18 18:30:50 +03:00
|
|
|
static struct luaL_reg iolib[] = {
|
1997-07-01 23:32:41 +04:00
|
|
|
{"setlocale", setloc},
|
1996-05-01 01:13:55 +04:00
|
|
|
{"execute", io_execute},
|
|
|
|
{"remove", io_remove},
|
|
|
|
{"rename", io_rename},
|
|
|
|
{"tmpname", io_tmpname},
|
|
|
|
{"getenv", io_getenv},
|
|
|
|
{"date", io_date},
|
|
|
|
{"exit", io_exit},
|
|
|
|
{"debug", io_debug},
|
|
|
|
{"print_stack", errorfb}
|
|
|
|
};
|
|
|
|
|
1997-11-28 15:40:37 +03:00
|
|
|
static struct luaL_reg iolibtag[] = {
|
|
|
|
{"readfrom", io_readfrom},
|
|
|
|
{"writeto", io_writeto},
|
|
|
|
{"appendto", io_appendto},
|
|
|
|
{"read", io_read},
|
|
|
|
{"write", io_write}
|
|
|
|
};
|
|
|
|
|
|
|
|
static void openwithtags (void)
|
|
|
|
{
|
|
|
|
int iotag = lua_newtag();
|
|
|
|
int closedtag = lua_newtag();
|
|
|
|
int i;
|
|
|
|
for (i=0; i<sizeof(iolibtag)/sizeof(iolibtag[0]); i++) {
|
|
|
|
/* put both tags as upvalues for these functions */
|
|
|
|
lua_pushnumber(iotag);
|
|
|
|
lua_pushnumber(closedtag);
|
|
|
|
lua_pushCclosure(iolibtag[i].func, 2);
|
|
|
|
lua_setglobal(iolibtag[i].name);
|
|
|
|
}
|
|
|
|
setfile(stdin, FINPUT, iotag);
|
|
|
|
setfile(stdout, FOUTPUT, iotag);
|
|
|
|
setfile(stdin, "_STDIN", iotag);
|
|
|
|
setfile(stdout, "_STDOUT", iotag);
|
|
|
|
setfile(stderr, "_STDERR", iotag);
|
|
|
|
}
|
|
|
|
|
1997-09-16 23:25:59 +04:00
|
|
|
void lua_iolibopen (void)
|
1993-07-28 17:18:00 +04:00
|
|
|
{
|
1997-11-04 18:27:53 +03:00
|
|
|
luaL_openlib(iolib, (sizeof(iolib)/sizeof(iolib[0])));
|
1997-11-28 15:40:37 +03:00
|
|
|
openwithtags();
|
1997-06-19 22:03:04 +04:00
|
|
|
lua_pushcfunction(errorfb);
|
|
|
|
lua_seterrormethod();
|
1993-07-28 17:18:00 +04:00
|
|
|
}
|