lua/iolib.c

304 lines
6.3 KiB
C
Raw Normal View History

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>
#include <errno.h>
1993-07-28 17:18:00 +04:00
#include "lua.h"
#include "auxlib.h"
1995-10-17 17:12:45 +03:00
#include "luadebug.h"
#include "lualib.h"
1993-07-28 17:18:00 +04: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;
#ifdef POPEN
FILE *popen();
int pclose();
#else
#define popen(x,y) NULL /* that is, popen always fails */
#define pclose(x) (-1)
#endif
1996-03-14 18:55:18 +03:00
static void pushresult (int i)
{
if (i)
lua_pushuserdata(NULL);
else {
lua_pushnil();
#ifndef NOSTRERROR
lua_pushstring(strerror(errno));
#else
lua_pushstring("system unable to define the error");
#endif
}
}
static void closefile (FILE *f)
{
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);
}
1993-07-28 17:18:00 +04:00
static void io_readfrom (void)
{
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)
lua_infile = lua_getuserdata(f);
else {
char *s = luaL_check_string(1, "readfrom");
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)
{
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)
lua_outfile = lua_getuserdata(f);
else {
char *s = luaL_check_string(1, "writeto");
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)
{
char *s = luaL_check_string(1, "appendto");
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);
}
else
pushresult(0);
}
#define NEED_OTHER (EOF-1) /* just some flag different from EOF */
static void io_read (void)
{
char *buff;
char *p = luaL_opt_string(1, "[^\n]*{\n}", "read");
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--;
p++;
}
else {
char *ep = item_end(p); /* get what is next */
int m; /* match result */
if (c == NEED_OTHER) c = getc(lua_infile);
m = (c == EOF) ? 0 : singlematch((char)c, p);
if (m) {
1997-03-05 16:32:41 +03:00
if (inskip == 0) luaI_addchar(c);
c = NEED_OTHER;
}
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 */
}
1993-07-28 17:18:00 +04: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);
}
1993-07-28 17:18:00 +04:00
static void io_write (void)
{
int arg = 1;
int status = 1;
char *s;
while ((s = luaL_opt_string(arg++, NULL, "write")) != NULL)
status = status && (fputs(s, lua_outfile) != EOF);
pushresult(status);
1993-07-28 17:18:00 +04:00
}
static void io_execute (void)
1993-07-28 17:18:00 +04:00
{
lua_pushnumber(system(luaL_check_string(1, "execute")));
1993-07-28 17:18:00 +04:00
}
static void io_remove (void)
1993-07-28 17:18:00 +04:00
{
pushresult(remove(luaL_check_string(1, "remove")) == 0);
1996-03-14 18:55:18 +03:00
}
1996-03-14 18:55:18 +03:00
static void io_rename (void)
{
pushresult(rename(luaL_check_string(1, "rename"),
luaL_check_string(2, "rename")) == 0);
1996-03-14 18:55:18 +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
}
static void io_getenv (void)
{
lua_pushstring(getenv(luaL_check_string(1, "getenv"))); /* if NULL push nil */
}
static void io_date (void)
{
time_t t;
struct tm *tm;
char *s = luaL_opt_string(1, "%c", "date");
char b[BUFSIZ];
time(&t); tm = localtime(&t);
if (strftime(b,sizeof(b),s,tm))
lua_pushstring(b);
else
lua_error("invalid `date' format");
}
static void io_exit (void)
{
lua_Object o = lua_getparam(1);
exit(lua_isnumber(o) ? (int)lua_getnumber(o) : 1);
}
static void io_debug (void)
{
while (1) {
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;
lua_dostring(buffer);
}
}
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
{
int level = 1; /* skip level 0 (it's this function) */
1995-10-17 17:12:45 +03:00
lua_Object func;
while ((func = lua_stackedfunction(level++)) != LUA_NOOBJECT) {
char *name;
int currentline;
fprintf(f, (level==2) ? "Active Stack:\n\t" : "\t");
switch (*lua_getobjname(func, &name)) {
case 'g':
fprintf(f, "function %s", name);
break;
case 'f':
1996-03-12 18:56:03 +03:00
fprintf(f, "`%s' fallback", name);
break;
default: {
char *filename;
int linedefined;
lua_funcinfo(func, &filename, &linedefined);
if (linedefined == 0)
fprintf(f, "main of %s", filename);
else if (linedefined < 0)
fprintf(f, "%s", filename);
else
fprintf(f, "function (%s:%d)", filename, linedefined);
1995-10-17 17:12:45 +03:00
}
}
if ((currentline = lua_currentline(func)) > 0)
1996-03-12 18:56:03 +03:00
fprintf(f, " at line %d", currentline);
fprintf(f, "\n");
1995-10-17 17:12:45 +03:00
}
}
static void errorfb (void)
{
char *s = luaL_opt_string(1, "(no messsage)", NULL);
fprintf(stderr, "lua: %s\n", s);
lua_printstack(stderr);
}
static struct luaL_reg iolib[] = {
{"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");
lua_infile=stdin; lua_outfile=stdout;
luaL_openlib(iolib, (sizeof(iolib)/sizeof(iolib[0])));
lua_setglobalmethod("error", errorfb);
1993-07-28 17:18:00 +04:00
}