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
|
|
|
|
|
|
|
#include "lua.h"
|
1997-03-18 18:30:50 +03:00
|
|
|
#include "auxlib.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
|
|
|
|
|
|
|
FILE *lua_infile, *lua_outfile;
|
1993-07-28 17:18:00 +04:00
|
|
|
|
1997-03-20 23:36:58 +03:00
|
|
|
int lua_tagio;
|
|
|
|
|
1995-10-04 16:53:10 +03:00
|
|
|
|
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
|
|
|
|
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();
|
1996-11-01 20:54:41 +03:00
|
|
|
#ifndef NOSTRERROR
|
1996-11-01 20:03:36 +03:00
|
|
|
lua_pushstring(strerror(errno));
|
1996-11-01 20:54:41 +03:00
|
|
|
#else
|
|
|
|
lua_pushstring("system unable to define the error");
|
|
|
|
#endif
|
1995-10-04 16:53:10 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
1996-11-01 20:03:36 +03:00
|
|
|
|
|
|
|
static void closefile (FILE *f)
|
1995-10-04 16:53:10 +03:00
|
|
|
{
|
1996-11-01 20:03:36 +03:00
|
|
|
if (f == stdin || f == stdout)
|
|
|
|
return;
|
|
|
|
if (f == lua_infile)
|
|
|
|
lua_infile = stdin;
|
|
|
|
if (f == lua_outfile)
|
|
|
|
lua_outfile = stdout;
|
|
|
|
if (pclose(f) == -1)
|
|
|
|
fclose(f);
|
1995-10-04 16:53:10 +03:00
|
|
|
}
|
|
|
|
|
1996-11-01 20:03:36 +03:00
|
|
|
|
|
|
|
|
1993-07-28 17:18:00 +04:00
|
|
|
static void io_readfrom (void)
|
|
|
|
{
|
1996-11-01 20:03:36 +03:00
|
|
|
lua_Object f = lua_getparam(1);
|
|
|
|
if (f == LUA_NOOBJECT)
|
|
|
|
closefile(lua_infile); /* restore standart input */
|
1997-03-20 23:36:58 +03:00
|
|
|
else if (lua_tag(f) == lua_tagio)
|
1996-11-01 20:03:36 +03:00
|
|
|
lua_infile = lua_getuserdata(f);
|
|
|
|
else {
|
1997-03-17 20:02:29 +03:00
|
|
|
char *s = luaL_check_string(1, "readfrom");
|
1996-11-01 20:03:36 +03:00
|
|
|
FILE *fp = (*s == '|') ? popen(s+1, "r") : fopen(s, "r");
|
|
|
|
if (fp)
|
|
|
|
lua_infile = fp;
|
|
|
|
else {
|
|
|
|
pushresult(0);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
1997-03-20 23:36:58 +03:00
|
|
|
lua_pushusertag(lua_infile, lua_tagio);
|
1993-07-28 17:18:00 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
static void io_writeto (void)
|
|
|
|
{
|
1996-11-01 20:03:36 +03:00
|
|
|
lua_Object f = lua_getparam(1);
|
|
|
|
if (f == LUA_NOOBJECT)
|
|
|
|
closefile(lua_outfile); /* restore standart output */
|
1997-03-20 23:36:58 +03:00
|
|
|
else if (lua_tag(f) == lua_tagio)
|
1996-11-01 20:03:36 +03:00
|
|
|
lua_outfile = lua_getuserdata(f);
|
|
|
|
else {
|
1997-03-17 20:02:29 +03:00
|
|
|
char *s = luaL_check_string(1, "writeto");
|
1996-11-01 20:03:36 +03:00
|
|
|
FILE *fp = (*s == '|') ? popen(s+1,"w") : fopen(s,"w");
|
|
|
|
if (fp)
|
|
|
|
lua_outfile = fp;
|
|
|
|
else {
|
|
|
|
pushresult(0);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
1997-03-20 23:36:58 +03:00
|
|
|
lua_pushusertag(lua_outfile, lua_tagio);
|
1993-07-28 17:18:00 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
|
1993-12-17 21:41:19 +03:00
|
|
|
static void io_appendto (void)
|
|
|
|
{
|
1997-03-17 20:02:29 +03:00
|
|
|
char *s = luaL_check_string(1, "appendto");
|
1996-11-01 20:03:36 +03:00
|
|
|
FILE *fp = fopen (s, "a");
|
|
|
|
if (fp != NULL) {
|
|
|
|
lua_outfile = fp;
|
1997-03-20 23:36:58 +03:00
|
|
|
lua_pushusertag(lua_outfile, lua_tagio);
|
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)
|
|
|
|
{
|
1996-11-01 20:03:36 +03:00
|
|
|
char *buff;
|
1997-03-17 20:02:29 +03:00
|
|
|
char *p = luaL_opt_string(1, "[^\n]*{\n}", "read");
|
1996-11-01 20:03:36 +03:00
|
|
|
int inskip = 0; /* to control {skips} */
|
|
|
|
int c = NEED_OTHER;
|
|
|
|
luaI_addchar(0);
|
|
|
|
while (*p) {
|
1997-03-05 16:32:41 +03:00
|
|
|
if (*p == '{') {
|
|
|
|
inskip++;
|
|
|
|
p++;
|
|
|
|
}
|
|
|
|
else if (*p == '}') {
|
|
|
|
if (inskip == 0)
|
|
|
|
lua_error("unbalanced `{...}' in read pattern");
|
|
|
|
inskip--;
|
1996-11-01 20:03:36 +03:00
|
|
|
p++;
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
char *ep = item_end(p); /* get what is next */
|
1996-11-20 16:47:59 +03:00
|
|
|
int m; /* match result */
|
1996-11-01 20:03:36 +03:00
|
|
|
if (c == NEED_OTHER) c = getc(lua_infile);
|
1996-11-20 16:47:59 +03:00
|
|
|
m = (c == EOF) ? 0 : singlematch((char)c, p);
|
|
|
|
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? */
|
|
|
|
ungetc(c, lua_infile);
|
|
|
|
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)
|
|
|
|
{
|
1996-11-01 20:03:36 +03:00
|
|
|
int arg = 1;
|
|
|
|
int status = 1;
|
|
|
|
char *s;
|
1997-03-17 20:02:29 +03:00
|
|
|
while ((s = luaL_opt_string(arg++, NULL, "write")) != NULL)
|
1996-11-01 20:03:36 +03:00
|
|
|
status = status && (fputs(s, lua_outfile) != EOF);
|
|
|
|
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-03-17 20:02:29 +03:00
|
|
|
lua_pushnumber(system(luaL_check_string(1, "execute")));
|
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-03-17 20:02:29 +03:00
|
|
|
pushresult(remove(luaL_check_string(1, "remove")) == 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-03-17 20:02:29 +03:00
|
|
|
pushresult(rename(luaL_check_string(1, "rename"),
|
|
|
|
luaL_check_string(2, "rename")) == 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-03-17 20:02:29 +03:00
|
|
|
lua_pushstring(getenv(luaL_check_string(1, "getenv"))); /* 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-03-17 20:02:29 +03:00
|
|
|
char *s = luaL_opt_string(1, "%c", "date");
|
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
|
|
|
}
|
|
|
|
|
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-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;
|
|
|
|
case 'f':
|
1996-03-12 18:56:03 +03:00
|
|
|
fprintf(f, "`%s' fallback", 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
|
|
|
char *filename;
|
|
|
|
int linedefined;
|
|
|
|
lua_funcinfo(func, &filename, &linedefined);
|
|
|
|
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);
|
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);
|
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-03-17 20:02:29 +03:00
|
|
|
char *s = luaL_opt_string(1, "(no messsage)", NULL);
|
1995-11-03 18:43:50 +03:00
|
|
|
fprintf(stderr, "lua: %s\n", s);
|
|
|
|
lua_printstack(stderr);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
1997-03-18 18:30:50 +03:00
|
|
|
static struct luaL_reg iolib[] = {
|
1996-05-01 01:13:55 +04:00
|
|
|
{"readfrom", io_readfrom},
|
|
|
|
{"writeto", io_writeto},
|
|
|
|
{"appendto", io_appendto},
|
|
|
|
{"read", io_read},
|
|
|
|
{"write", io_write},
|
|
|
|
{"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}
|
|
|
|
};
|
|
|
|
|
1993-07-28 17:18:00 +04:00
|
|
|
void iolib_open (void)
|
|
|
|
{
|
1997-03-20 23:36:58 +03:00
|
|
|
lua_tagio = lua_newtag("userdata");
|
1996-11-01 20:03:36 +03:00
|
|
|
lua_infile=stdin; lua_outfile=stdout;
|
1997-03-18 18:30:50 +03:00
|
|
|
luaL_openlib(iolib, (sizeof(iolib)/sizeof(iolib[0])));
|
|
|
|
lua_setglobalmethod("error", errorfb);
|
1993-07-28 17:18:00 +04:00
|
|
|
}
|