mirror of https://github.com/rusefi/lua.git
new functions openfile and closefile;
new way to keep file handle tags.
This commit is contained in:
parent
7a11c7f8e4
commit
d6ff06751a
163
liolib.c
163
liolib.c
|
@ -1,5 +1,5 @@
|
||||||
/*
|
/*
|
||||||
** $Id: liolib.c,v 1.33 1999/03/05 20:45:01 roberto Exp roberto $
|
** $Id: liolib.c,v 1.34 1999/03/11 18:59:19 roberto Exp roberto $
|
||||||
** Standard I/O (and system) library
|
** Standard I/O (and system) library
|
||||||
** See Copyright Notice in lua.h
|
** See Copyright Notice in lua.h
|
||||||
*/
|
*/
|
||||||
|
@ -32,10 +32,8 @@
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
|
|
||||||
#define CLOSEDTAG 2
|
#define CLOSEDTAG(tag) ((tag)-1)
|
||||||
#define IOTAG 1
|
|
||||||
|
|
||||||
#define FIRSTARG 3 /* 1st and 2nd are upvalues */
|
|
||||||
|
|
||||||
#define FINPUT "_INPUT"
|
#define FINPUT "_INPUT"
|
||||||
#define FOUTPUT "_OUTPUT"
|
#define FOUTPUT "_OUTPUT"
|
||||||
|
@ -70,20 +68,23 @@ static void pushresult (int i) {
|
||||||
** =======================================================
|
** =======================================================
|
||||||
*/
|
*/
|
||||||
|
|
||||||
static int gettag (int i) {
|
static int gettag (void) {
|
||||||
return (int)lua_getnumber(lua_getparam(i));
|
lua_pushusertag(stdin, LUA_ANYTAG);
|
||||||
|
return lua_tag(lua_pop()); /* get the tag of stdin */
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
static int ishandler (lua_Object f) {
|
static int ishandler (lua_Object f) {
|
||||||
if (lua_isuserdata(f)) {
|
if (lua_isuserdata(f)) {
|
||||||
if (lua_tag(f) == gettag(CLOSEDTAG))
|
int tag = gettag();
|
||||||
|
if (lua_tag(f) == CLOSEDTAG(tag))
|
||||||
lua_error("cannot access a closed file");
|
lua_error("cannot access a closed file");
|
||||||
return lua_tag(f) == gettag(IOTAG);
|
return lua_tag(f) == tag;
|
||||||
}
|
}
|
||||||
else return 0;
|
else return 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
static FILE *getfilebyname (char *name) {
|
static FILE *getfilebyname (char *name) {
|
||||||
lua_Object f = lua_getglobal(name);
|
lua_Object f = lua_getglobal(name);
|
||||||
if (!ishandler(f))
|
if (!ishandler(f))
|
||||||
|
@ -98,6 +99,13 @@ static FILE *getfile (int arg) {
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
static FILE *getnonullfile (int arg) {
|
||||||
|
FILE *f = getfile(arg);
|
||||||
|
luaL_arg_check(f, arg, "invalid file handler");
|
||||||
|
return f;
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
static FILE *getfileparam (char *name, int *arg) {
|
static FILE *getfileparam (char *name, int *arg) {
|
||||||
FILE *f = getfile(*arg);
|
FILE *f = getfile(*arg);
|
||||||
if (f) {
|
if (f) {
|
||||||
|
@ -109,23 +117,26 @@ static FILE *getfileparam (char *name, int *arg) {
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
static void getmode (char mode, char *m) {
|
static void closefile (FILE *f) {
|
||||||
m[0] = mode;
|
if (f != stdin && f != stdout) {
|
||||||
if (*luaL_opt_string(FIRSTARG+1, "text") == 'b') {
|
int tag = gettag();
|
||||||
m[1] = 'b';
|
if (pclose(f) == -1)
|
||||||
m[2] = '\0';
|
fclose(f);
|
||||||
|
lua_pushusertag(f, tag);
|
||||||
|
lua_settag(CLOSEDTAG(tag));
|
||||||
}
|
}
|
||||||
else m[1] = '\0';
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
static void closefile (char *name) {
|
static void io_close (void) {
|
||||||
FILE *f = getfilebyname(name);
|
closefile(getnonullfile(1));
|
||||||
if (f == stdin || f == stdout) return;
|
}
|
||||||
if (pclose(f) == -1)
|
|
||||||
fclose(f);
|
|
||||||
lua_pushobject(lua_getglobal(name));
|
static void io_open (void) {
|
||||||
lua_settag(gettag(CLOSEDTAG));
|
FILE *f = fopen(luaL_check_string(1), luaL_check_string(2));
|
||||||
|
if (f) lua_pushusertag(f, gettag());
|
||||||
|
else pushresult(0);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
@ -136,7 +147,7 @@ static void setfile (FILE *f, char *name, int tag) {
|
||||||
|
|
||||||
|
|
||||||
static void setreturn (FILE *f, char *name) {
|
static void setreturn (FILE *f, char *name) {
|
||||||
int tag = gettag(IOTAG);
|
int tag = gettag();
|
||||||
setfile(f, name, tag);
|
setfile(f, name, tag);
|
||||||
lua_pushusertag(f, tag);
|
lua_pushusertag(f, tag);
|
||||||
}
|
}
|
||||||
|
@ -144,18 +155,16 @@ static void setreturn (FILE *f, char *name) {
|
||||||
|
|
||||||
static void io_readfrom (void) {
|
static void io_readfrom (void) {
|
||||||
FILE *current;
|
FILE *current;
|
||||||
lua_Object f = lua_getparam(FIRSTARG);
|
lua_Object f = lua_getparam(1);
|
||||||
if (f == LUA_NOOBJECT) {
|
if (f == LUA_NOOBJECT) {
|
||||||
closefile(FINPUT);
|
closefile(getfilebyname(FINPUT));
|
||||||
current = stdin;
|
current = stdin;
|
||||||
}
|
}
|
||||||
else if (lua_tag(f) == gettag(IOTAG))
|
else if (lua_tag(f) == gettag()) /* deprecated option */
|
||||||
current = lua_getuserdata(f);
|
current = lua_getuserdata(f);
|
||||||
else {
|
else {
|
||||||
char *s = luaL_check_string(FIRSTARG);
|
char *s = luaL_check_string(1);
|
||||||
char m[MODESIZE];
|
current = (*s == '|') ? popen(s+1, "r") : fopen(s, "r");
|
||||||
getmode('r', m);
|
|
||||||
current = (*s == '|') ? popen(s+1, "r") : fopen(s, m);
|
|
||||||
if (current == NULL) {
|
if (current == NULL) {
|
||||||
pushresult(0);
|
pushresult(0);
|
||||||
return;
|
return;
|
||||||
|
@ -167,18 +176,16 @@ static void io_readfrom (void) {
|
||||||
|
|
||||||
static void io_writeto (void) {
|
static void io_writeto (void) {
|
||||||
FILE *current;
|
FILE *current;
|
||||||
lua_Object f = lua_getparam(FIRSTARG);
|
lua_Object f = lua_getparam(1);
|
||||||
if (f == LUA_NOOBJECT) {
|
if (f == LUA_NOOBJECT) {
|
||||||
closefile(FOUTPUT);
|
closefile(getfilebyname(FOUTPUT));
|
||||||
current = stdout;
|
current = stdout;
|
||||||
}
|
}
|
||||||
else if (lua_tag(f) == gettag(IOTAG))
|
else if (lua_tag(f) == gettag()) /* deprecated option */
|
||||||
current = lua_getuserdata(f);
|
current = lua_getuserdata(f);
|
||||||
else {
|
else {
|
||||||
char *s = luaL_check_string(FIRSTARG);
|
char *s = luaL_check_string(1);
|
||||||
char m[MODESIZE];
|
current = (*s == '|') ? popen(s+1,"w") : fopen(s, "w");
|
||||||
getmode('w', m);
|
|
||||||
current = (*s == '|') ? popen(s+1,"w") : fopen(s, m);
|
|
||||||
if (current == NULL) {
|
if (current == NULL) {
|
||||||
pushresult(0);
|
pushresult(0);
|
||||||
return;
|
return;
|
||||||
|
@ -189,11 +196,7 @@ static void io_writeto (void) {
|
||||||
|
|
||||||
|
|
||||||
static void io_appendto (void) {
|
static void io_appendto (void) {
|
||||||
char *s = luaL_check_string(FIRSTARG);
|
FILE *fp = fopen(luaL_check_string(1), "a");
|
||||||
char m[MODESIZE];
|
|
||||||
FILE *fp;
|
|
||||||
getmode('a', m);
|
|
||||||
fp = fopen (s, m);
|
|
||||||
if (fp != NULL)
|
if (fp != NULL)
|
||||||
setreturn(fp, FOUTPUT);
|
setreturn(fp, FOUTPUT);
|
||||||
else
|
else
|
||||||
|
@ -298,7 +301,7 @@ static void read_file (FILE *f) {
|
||||||
|
|
||||||
static void io_read (void) {
|
static void io_read (void) {
|
||||||
static char *options[] = {"*n", "*l", "*a", ".*", "*w", NULL};
|
static char *options[] = {"*n", "*l", "*a", ".*", "*w", NULL};
|
||||||
int arg = FIRSTARG;
|
int arg = 1;
|
||||||
FILE *f = getfileparam(FINPUT, &arg);
|
FILE *f = getfileparam(FINPUT, &arg);
|
||||||
char *p = luaL_opt_string(arg++, "*l");
|
char *p = luaL_opt_string(arg++, "*l");
|
||||||
do { /* repeat for each part */
|
do { /* repeat for each part */
|
||||||
|
@ -332,7 +335,7 @@ static void io_read (void) {
|
||||||
|
|
||||||
|
|
||||||
static void io_write (void) {
|
static void io_write (void) {
|
||||||
int arg = FIRSTARG;
|
int arg = 1;
|
||||||
FILE *f = getfileparam(FOUTPUT, &arg);
|
FILE *f = getfileparam(FOUTPUT, &arg);
|
||||||
int status = 1;
|
int status = 1;
|
||||||
char *s;
|
char *s;
|
||||||
|
@ -346,11 +349,10 @@ static void io_write (void) {
|
||||||
static void io_seek (void) {
|
static void io_seek (void) {
|
||||||
static int mode[] = {SEEK_SET, SEEK_CUR, SEEK_END};
|
static int mode[] = {SEEK_SET, SEEK_CUR, SEEK_END};
|
||||||
static char *modenames[] = {"set", "cur", "end", NULL};
|
static char *modenames[] = {"set", "cur", "end", NULL};
|
||||||
FILE *f = getfile(FIRSTARG-1+1);
|
FILE *f = getnonullfile(1);
|
||||||
int op = luaL_findstring(luaL_opt_string(FIRSTARG-1+2, "cur"), modenames);
|
int op = luaL_findstring(luaL_opt_string(2, "cur"), modenames);
|
||||||
long offset = luaL_opt_long(FIRSTARG-1+3, 0);
|
long offset = luaL_opt_long(3, 0);
|
||||||
luaL_arg_check(f, FIRSTARG-1+1, "invalid file handler");
|
luaL_arg_check(op != -1, 2, "invalid mode");
|
||||||
luaL_arg_check(op != -1, FIRSTARG-1+2, "invalid mode");
|
|
||||||
op = fseek(f, offset, mode[op]);
|
op = fseek(f, offset, mode[op]);
|
||||||
if (op)
|
if (op)
|
||||||
pushresult(0); /* error */
|
pushresult(0); /* error */
|
||||||
|
@ -360,8 +362,8 @@ static void io_seek (void) {
|
||||||
|
|
||||||
|
|
||||||
static void io_flush (void) {
|
static void io_flush (void) {
|
||||||
FILE *f = getfile(FIRSTARG);
|
FILE *f = getfile(1);
|
||||||
luaL_arg_check(f || lua_getparam(FIRSTARG) == LUA_NOOBJECT, FIRSTARG,
|
luaL_arg_check(f || lua_getparam(1) == LUA_NOOBJECT, 1,
|
||||||
"invalid file handler");
|
"invalid file handler");
|
||||||
pushresult(fflush(f) == 0);
|
pushresult(fflush(f) == 0);
|
||||||
}
|
}
|
||||||
|
@ -444,8 +446,9 @@ static void io_debug (void) {
|
||||||
for (;;) {
|
for (;;) {
|
||||||
char buffer[250];
|
char buffer[250];
|
||||||
fprintf(stderr, "lua_debug> ");
|
fprintf(stderr, "lua_debug> ");
|
||||||
if (fgets(buffer, sizeof(buffer), stdin) == 0) return;
|
if (fgets(buffer, sizeof(buffer), stdin) == 0 ||
|
||||||
if (strcmp(buffer, "cont\n") == 0) return;
|
strcmp(buffer, "cont\n") == 0)
|
||||||
|
return;
|
||||||
lua_dostring(buffer);
|
lua_dostring(buffer);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -512,49 +515,43 @@ static void errorfb (void) {
|
||||||
|
|
||||||
|
|
||||||
static struct luaL_reg iolib[] = {
|
static struct luaL_reg iolib[] = {
|
||||||
{"setlocale", setloc},
|
{"_ERRORMESSAGE", errorfb},
|
||||||
|
{"appendto", io_appendto},
|
||||||
|
{"clock", io_clock},
|
||||||
|
{"closefile", io_close},
|
||||||
|
{"date", io_date},
|
||||||
|
{"debug", io_debug},
|
||||||
{"execute", io_execute},
|
{"execute", io_execute},
|
||||||
|
{"exit", io_exit},
|
||||||
|
{"flush", io_flush},
|
||||||
|
{"getenv", io_getenv},
|
||||||
|
{"openfile", io_open},
|
||||||
|
{"read", io_read},
|
||||||
|
{"readfrom", io_readfrom},
|
||||||
{"remove", io_remove},
|
{"remove", io_remove},
|
||||||
{"rename", io_rename},
|
{"rename", io_rename},
|
||||||
{"tmpname", io_tmpname},
|
|
||||||
{"getenv", io_getenv},
|
|
||||||
{"date", io_date},
|
|
||||||
{"clock", io_clock},
|
|
||||||
{"exit", io_exit},
|
|
||||||
{"debug", io_debug},
|
|
||||||
{"_ERRORMESSAGE", errorfb}
|
|
||||||
};
|
|
||||||
|
|
||||||
static struct luaL_reg iolibtag[] = {
|
|
||||||
{"readfrom", io_readfrom},
|
|
||||||
{"writeto", io_writeto},
|
|
||||||
{"appendto", io_appendto},
|
|
||||||
{"flush", io_flush},
|
|
||||||
{"read", io_read},
|
|
||||||
{"seek", io_seek},
|
{"seek", io_seek},
|
||||||
{"write", io_write}
|
{"setlocale", setloc},
|
||||||
|
{"tmpname", io_tmpname},
|
||||||
|
{"write", io_write},
|
||||||
|
{"writeto", io_writeto}
|
||||||
};
|
};
|
||||||
|
|
||||||
static void openwithtags (void) {
|
|
||||||
|
void lua_iolibopen (void) {
|
||||||
int iotag = lua_newtag();
|
int iotag = lua_newtag();
|
||||||
int closedtag = lua_newtag();
|
lua_newtag(); /* alloc CLOSEDTAG: assume that CLOSEDTAG = iotag-1 */
|
||||||
int i;
|
/* predefined file handles */
|
||||||
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(stdin, FINPUT, iotag);
|
||||||
setfile(stdout, FOUTPUT, iotag);
|
setfile(stdout, FOUTPUT, iotag);
|
||||||
setfile(stdin, "_STDIN", iotag);
|
setfile(stdin, "_STDIN", iotag);
|
||||||
setfile(stdout, "_STDOUT", iotag);
|
setfile(stdout, "_STDOUT", iotag);
|
||||||
setfile(stderr, "_STDERR", iotag);
|
setfile(stderr, "_STDERR", iotag);
|
||||||
}
|
/* make sure stdin (with its tag) won't be collected */
|
||||||
|
lua_pushusertag(stdin, iotag); lua_ref(1);
|
||||||
void lua_iolibopen (void) {
|
/* close file when collected */
|
||||||
|
lua_pushcfunction(io_close); lua_settagmethod(iotag, "gc");
|
||||||
|
/* register lib functions */
|
||||||
luaL_openlib(iolib, (sizeof(iolib)/sizeof(iolib[0])));
|
luaL_openlib(iolib, (sizeof(iolib)/sizeof(iolib[0])));
|
||||||
openwithtags();
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
Loading…
Reference in New Issue