new implementation of function "read", with predifined options.

This commit is contained in:
Roberto Ierusalimschy 1998-12-27 18:21:28 -02:00
parent a881abfd1e
commit 63166c0ca0
1 changed files with 149 additions and 121 deletions

270
liolib.c
View File

@ -1,5 +1,5 @@
/* /*
** $Id: liolib.c,v 1.25 1998/09/07 18:59:59 roberto Exp roberto $ ** $Id: liolib.c,v 1.26 1998/11/20 15:41:43 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
*/ */
@ -20,6 +20,7 @@
#ifndef OLD_ANSI #ifndef OLD_ANSI
#include <locale.h> #include <locale.h>
#else #else
/* no support for locale and for strerror: fake them */
#define setlocale(a,b) 0 #define setlocale(a,b) 0
#define LC_ALL 0 #define LC_ALL 0
#define LC_COLLATE 0 #define LC_COLLATE 0
@ -44,19 +45,19 @@
FILE *popen(); FILE *popen();
int pclose(); int pclose();
#else #else
/* no support for popen */
#define popen(x,y) NULL /* that is, popen always fails */ #define popen(x,y) NULL /* that is, popen always fails */
#define pclose(x) (-1) #define pclose(x) (-1)
#endif #endif
static int gettag (int i)
{ static int gettag (int i) {
return lua_getnumber(lua_getparam(i)); return (int)lua_getnumber(lua_getparam(i));
} }
static void pushresult (int i) static void pushresult (int i) {
{
if (i) if (i)
lua_pushuserdata(NULL); lua_pushuserdata(NULL);
else { else {
@ -67,8 +68,7 @@ static void pushresult (int i)
} }
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)) if (lua_tag(f) == gettag(CLOSEDTAG))
lua_error("cannot access a closed file"); lua_error("cannot access a closed file");
@ -77,8 +77,7 @@ static int ishandler (lua_Object f)
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))
luaL_verror("global variable `%.50s' is not a file handle", name); luaL_verror("global variable `%.50s' is not a file handle", name);
@ -112,8 +111,7 @@ static char *getmode (char mode) {
} }
static void closefile (char *name) static void closefile (char *name) {
{
FILE *f = getfilebyname(name); FILE *f = getfilebyname(name);
if (f == stdin || f == stdout) return; if (f == stdin || f == stdout) return;
if (pclose(f) == -1) if (pclose(f) == -1)
@ -123,23 +121,20 @@ static void closefile (char *name)
} }
static void setfile (FILE *f, char *name, int tag) static void setfile (FILE *f, char *name, int tag) {
{
lua_pushusertag(f, tag); lua_pushusertag(f, tag);
lua_setglobal(name); lua_setglobal(name);
} }
static void setreturn (FILE *f, char *name) static void setreturn (FILE *f, char *name) {
{
int tag = gettag(IOTAG); int tag = gettag(IOTAG);
setfile(f, name, tag); setfile(f, name, tag);
lua_pushusertag(f, tag); lua_pushusertag(f, tag);
} }
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(FIRSTARG);
if (f == LUA_NOOBJECT) { if (f == LUA_NOOBJECT) {
@ -160,8 +155,7 @@ 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(FIRSTARG);
if (f == LUA_NOOBJECT) { if (f == LUA_NOOBJECT) {
@ -182,8 +176,7 @@ static void io_writeto (void)
} }
static void io_appendto (void) static void io_appendto (void) {
{
char *s = luaL_check_string(FIRSTARG); char *s = luaL_check_string(FIRSTARG);
FILE *fp = fopen (s, getmode('a')); FILE *fp = fopen (s, getmode('a'));
if (fp != NULL) if (fp != NULL)
@ -193,90 +186,133 @@ static void io_appendto (void)
} }
#define NEED_OTHER (EOF-1) /* just some flag different from EOF */
/*====================================================
** READ
**===================================================*/
static void read_until (FILE *f, int lim) { static int read_pattern (FILE *f, char *p) {
int l = 0; int inskip = 0; /* {skip} level */
int c; int c = getc(f);
for (c = getc(f); c != EOF && c != lim; c = getc(f)) { while (*p != '\0') {
luaL_addchar(c); switch (*p) {
l++; case '{':
} inskip++;
if (l > 0 || c == lim) /* read anything? */ p++;
lua_pushlstring(luaL_buffer(), l); continue;
else case '}':
lua_pushnil(); if (!inskip) lua_error("unbalanced braces in read pattern");
} inskip--;
p++;
static void io_read (void) { continue;
int arg = FIRSTARG; default: {
FILE *f = getfileparam(FINPUT, &arg); char *ep; /* get what is next */
char *p = luaL_opt_string(arg, NULL); int m; /* match result */
luaL_resetbuffer(); if (c != EOF)
if (p == NULL) /* default: read a line */ m = luaI_singlematch(c, p, &ep);
read_until(f, '\n'); else {
else if (strcmp(p, ".*") == 0) luaI_singlematch(0, p, &ep); /* to set "ep" */
read_until(f, EOF); m = 0; /* EOF matches no pattern */
else { }
int l = 0; /* number of chars read in buffer */ if (m) {
int inskip = 0; /* to control {skips} */ if (!inskip) luaL_addchar(c);
int c = NEED_OTHER; c = getc(f);
while (*p) { }
switch (*p) { switch (*ep) {
case '{': case '*': /* repetition */
inskip++; if (!m) p = ep+1; /* else stay in (repeat) the same item */
p++; continue;
continue; case '?': /* optional */
case '}': p = ep+1; /* continues reading the pattern */
if (inskip == 0) continue;
lua_error("unbalanced braces in read pattern"); default:
inskip--; if (!m) { /* pattern fails? */
p++; ungetc(c, f);
continue; return 0;
default: {
char *ep; /* get what is next */
int m; /* match result */
if (c == NEED_OTHER) c = getc(f);
if (c == EOF) {
luaI_singlematch(0, p, &ep); /* to set "ep" */
m = 0;
}
else {
m = luaI_singlematch(c, p, &ep);
if (m) {
if (inskip == 0) {
luaL_addchar(c);
l++;
}
c = NEED_OTHER;
} }
} p = ep; /* continues reading the pattern */
switch (*ep) {
case '*': /* repetition */
if (!m) p = ep+1; /* else stay in (repeat) the same item */
continue;
case '?': /* optional */
p = ep+1; /* continues reading the pattern */
continue;
default:
if (m) p = ep; /* continues reading the pattern */
else
goto break_while; /* pattern fails */
}
} }
} }
} break_while: }
if (c >= 0) /* not EOF nor NEED_OTHER? */
ungetc(c, f);
if (l > 0 || *p == 0) /* read something or did not fail? */
lua_pushlstring(luaL_buffer(), l);
} }
ungetc(c, f);
return 1;
} }
static void io_write (void) static int read_number (FILE *f) {
{ double d;
if (fscanf(f, "%lf", &d) == 1) {
lua_pushnumber(d);
return 1;
}
else return 0; /* read fails */
}
#define HUNK_LINE 1024
#define HUNK_FILE BUFSIZ
static int read_line (FILE *f) {
/* equivalent to: return read_pattern(f, "[^\n]*{\n}"); */
int n;
char *b;
do {
b = luaL_openspace(HUNK_LINE);
if (!fgets(b, HUNK_LINE, f)) return 0; /* read fails */
n = strlen(b);
luaL_addsize(n);
} while (b[n-1] != '\n');
luaL_addsize(-1); /* remove '\n' */
return 1;
}
static void read_file (FILE *f) {
/* equivalent to: return read_pattern(f, ".*"); */
int n;
do {
char *b = luaL_openspace(HUNK_FILE);
n = fread(b, sizeof(char), HUNK_FILE, f);
luaL_addsize(n);
} while (n==HUNK_FILE);
}
static void io_read (void) {
static char *options[] = {"*n", "*l", "*a", ".*", "*w", NULL};
int arg = FIRSTARG;
FILE *f = getfileparam(FINPUT, &arg);
char *p = luaL_opt_string(arg++, "*l");
do { /* repeat for each part */
long l;
int success;
luaL_resetbuffer();
switch (luaL_findstring(p, options)) {
case 0: /* number */
if (!read_number(f)) return; /* read fails */
continue; /* number is already pushed; avoid the "pushstring" */
case 1: /* line */
success = read_line(f);
break;
case 2: case 3: /* file */
read_file(f);
success = 1; /* always success */
break;
case 4: /* word */
success = read_pattern(f, "{%s*}%S%S*");
break;
default:
success = read_pattern(f, p);
}
l = luaL_getsize();
if (!success && l==0) return; /* read fails */
lua_pushlstring(luaL_buffer(), l);
} while ((p = luaL_opt_string(arg++, NULL)));
}
static void io_write (void) {
int arg = FIRSTARG; int arg = FIRSTARG;
FILE *f = getfileparam(FOUTPUT, &arg); FILE *f = getfileparam(FOUTPUT, &arg);
int status = 1; int status = 1;
@ -312,34 +348,29 @@ static void io_flush (void) {
} }
static void io_execute (void) static void io_execute (void) {
{
lua_pushnumber(system(luaL_check_string(1))); lua_pushnumber(system(luaL_check_string(1)));
} }
static void io_remove (void) static void io_remove (void) {
{
pushresult(remove(luaL_check_string(1)) == 0); pushresult(remove(luaL_check_string(1)) == 0);
} }
static void io_rename (void) static void io_rename (void) {
{
pushresult(rename(luaL_check_string(1), pushresult(rename(luaL_check_string(1),
luaL_check_string(2)) == 0); luaL_check_string(2)) == 0);
} }
static void io_tmpname (void) static void io_tmpname (void) {
{
lua_pushstring(tmpnam(NULL)); lua_pushstring(tmpnam(NULL));
} }
static void io_getenv (void) static void io_getenv (void) {
{
lua_pushstring(getenv(luaL_check_string(1))); /* if NULL push nil */ lua_pushstring(getenv(luaL_check_string(1))); /* if NULL push nil */
} }
@ -349,12 +380,11 @@ static void io_clock (void) {
} }
static void io_date (void) static void io_date (void) {
{ char b[256];
time_t t;
struct tm *tm;
char *s = luaL_opt_string(1, "%c"); char *s = luaL_opt_string(1, "%c");
char b[BUFSIZ]; struct tm *tm;
time_t t;
time(&t); tm = localtime(&t); time(&t); tm = localtime(&t);
if (strftime(b,sizeof(b),s,tm)) if (strftime(b,sizeof(b),s,tm))
lua_pushstring(b); lua_pushstring(b);
@ -363,8 +393,7 @@ static void io_date (void)
} }
static void setloc (void) static void setloc (void) {
{
static int cat[] = {LC_ALL, LC_COLLATE, LC_CTYPE, LC_MONETARY, LC_NUMERIC, static int cat[] = {LC_ALL, LC_COLLATE, LC_CTYPE, LC_MONETARY, LC_NUMERIC,
LC_TIME}; LC_TIME};
static char *catnames[] = {"all", "collate", "ctype", "monetary", static char *catnames[] = {"all", "collate", "ctype", "monetary",
@ -375,16 +404,14 @@ static void setloc (void)
} }
static void io_exit (void) static void io_exit (void) {
{
lua_Object o = lua_getparam(1); lua_Object o = lua_getparam(1);
exit(lua_isnumber(o) ? (int)lua_getnumber(o) : 1); exit(lua_isnumber(o) ? (int)lua_getnumber(o) : 1);
} }
static void io_debug (void) static void io_debug (void) {
{ for (;;) {
while (1) {
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) return;
@ -394,6 +421,7 @@ static void io_debug (void)
} }
#define MESSAGESIZE 150 #define MESSAGESIZE 150
#define MAXMESSAGE (MESSAGESIZE*10) #define MAXMESSAGE (MESSAGESIZE*10)
@ -467,8 +495,7 @@ static struct luaL_reg iolibtag[] = {
{"write", io_write} {"write", io_write}
}; };
static void openwithtags (void) static void openwithtags (void) {
{
int iotag = lua_newtag(); int iotag = lua_newtag();
int closedtag = lua_newtag(); int closedtag = lua_newtag();
int i; int i;
@ -490,3 +517,4 @@ void lua_iolibopen (void) {
luaL_openlib(iolib, (sizeof(iolib)/sizeof(iolib[0]))); luaL_openlib(iolib, (sizeof(iolib)/sizeof(iolib[0])));
openwithtags(); openwithtags();
} }