summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--lapi.c122
-rw-r--r--ldo.c4
-rw-r--r--lua.h20
-rw-r--r--lundump.h3
-rw-r--r--lzio.c56
-rw-r--r--lzio.h34
6 files changed, 55 insertions, 184 deletions
diff --git a/lapi.c b/lapi.c
index 9ad290f7..7e88dc03 100644
--- a/lapi.c
+++ b/lapi.c
@@ -1,11 +1,10 @@
1/* 1/*
2** $Id: lapi.c,v 1.192 2002/05/16 18:39:46 roberto Exp roberto $ 2** $Id: lapi.c,v 1.193 2002/05/27 20:35:40 roberto Exp roberto $
3** Lua API 3** Lua API
4** See Copyright Notice in lua.h 4** See Copyright Notice in lua.h
5*/ 5*/
6 6
7 7
8#include <stdio.h>
9#include <string.h> 8#include <string.h>
10 9
11#include "lua.h" 10#include "lua.h"
@@ -31,11 +30,6 @@ const char lua_ident[] =
31 "$URL: www.lua.org $\n"; 30 "$URL: www.lua.org $\n";
32 31
33 32
34#ifndef lua_filerror
35#include <errno.h>
36#define lua_fileerror (strerror(errno))
37#endif
38
39 33
40#ifndef api_check 34#ifndef api_check
41#define api_check(L, o) ((void)1) 35#define api_check(L, o) ((void)1)
@@ -368,9 +362,11 @@ LUA_API const char *lua_pushvfstring (lua_State *L, const char *fmt,
368LUA_API const char *lua_pushfstring (lua_State *L, const char *fmt, ...) { 362LUA_API const char *lua_pushfstring (lua_State *L, const char *fmt, ...) {
369 const char *ret; 363 const char *ret;
370 va_list argp; 364 va_list argp;
365 lua_lock(L);
371 va_start(argp, fmt); 366 va_start(argp, fmt);
372 ret = lua_pushvfstring(L, fmt, argp); 367 ret = luaO_pushvfstring(L, fmt, argp);
373 va_end(argp); 368 va_end(argp);
369 lua_unlock(L);
374 return ret; 370 return ret;
375} 371}
376 372
@@ -567,54 +563,19 @@ LUA_API int lua_pcall (lua_State *L, int nargs, int nresults, int errf) {
567} 563}
568 564
569 565
570static int errfile (lua_State *L, const char *filename) { 566LUA_API int lua_load (lua_State *L, lua_Getblock getblock, void *ud,
571 if (filename == NULL) filename = "stdin"; 567 int binary, const char *chunkname) {
572 lua_pushfstring(L, "cannot read %s: %s", filename, lua_fileerror);
573 return LUA_ERRFILE;
574}
575
576
577LUA_API int lua_loadfile (lua_State *L, const char *filename) {
578 ZIO z; 568 ZIO z;
579 int fnindex;
580 int status; 569 int status;
581 int bin; /* flag for file mode */ 570 lua_lock(L);
582 FILE *f = (filename == NULL) ? stdin : fopen(filename, "r"); 571 if (!chunkname) chunkname = "?";
583 if (f == NULL) return errfile(L, filename); /* unable to open file */ 572 luaZ_init(&z, getblock, ud, chunkname);
584 bin = (ungetc(getc(f), f) == LUA_SIGNATURE[0]); 573 status = luaD_protectedparser(L, &z, binary);
585 if (bin && f != stdin) { 574 lua_unlock(L);
586 fclose(f);
587 f = fopen(filename, "rb"); /* reopen in binary mode */
588 if (f == NULL) return errfile(L, filename); /* unable to reopen file */
589 }
590 if (filename == NULL)
591 lua_pushliteral(L, "=stdin");
592 else
593 lua_pushfstring(L, "@%s", filename);
594 fnindex = lua_gettop(L); /* stack index of file name */
595 luaZ_Fopen(&z, f, lua_tostring(L, fnindex));
596 status = luaD_protectedparser(L, &z, bin);
597 lua_remove(L, fnindex);
598 if (ferror(f)) {
599 if (status == 0) lua_pop(L, 1); /* remove chunk */
600 return errfile(L, filename);
601 }
602 if (f != stdin)
603 fclose(f);
604 return status; 575 return status;
605} 576}
606 577
607 578
608LUA_API int lua_loadbuffer (lua_State *L, const char *buff, size_t size,
609 const char *name) {
610 ZIO z;
611 if (!name) name = "?";
612 luaZ_mopen(&z, buff, size, name);
613 return luaD_protectedparser(L, &z, buff[0]==LUA_SIGNATURE[0]);
614}
615
616
617
618/* 579/*
619** Garbage-collection functions 580** Garbage-collection functions
620*/ 581*/
@@ -722,7 +683,7 @@ LUA_API void lua_concat (lua_State *L, int n) {
722 lua_lock(L); 683 lua_lock(L);
723 api_checknelems(L, n); 684 api_checknelems(L, n);
724 if (n >= 2) { 685 if (n >= 2) {
725 luaV_strconc(L, n, L->top - L->ci->base - 1); 686 luaV_concat(L, n, L->top - L->ci->base - 1);
726 L->top -= (n-1); 687 L->top -= (n-1);
727 luaC_checkGC(L); 688 luaC_checkGC(L);
728 } 689 }
@@ -763,62 +724,3 @@ LUA_API int lua_pushupvalues (lua_State *L) {
763} 724}
764 725
765 726
766
767/*
768** {======================================================
769** compatibility code
770** =======================================================
771*/
772
773
774static void callalert (lua_State *L, int status) {
775 if (status != 0) {
776 int top = lua_gettop(L);
777 lua_getglobal(L, "_ALERT");
778 lua_insert(L, -2);
779 lua_pcall(L, 1, 0, 0);
780 lua_settop(L, top-1);
781 }
782}
783
784
785LUA_API int lua_call (lua_State *L, int nargs, int nresults) {
786 int status;
787 int errpos = lua_gettop(L) - nargs;
788 lua_getglobal(L, "_ERRORMESSAGE");
789 lua_insert(L, errpos); /* put below function and args */
790 status = lua_pcall(L, nargs, nresults, errpos);
791 lua_remove(L, errpos);
792 callalert(L, status);
793 return status;
794}
795
796static int aux_do (lua_State *L, int status) {
797 if (status == 0) { /* parse OK? */
798 int err = lua_gettop(L);
799 lua_getglobal(L, "_ERRORMESSAGE");
800 lua_insert(L, err);
801 status = lua_pcall(L, 0, LUA_MULTRET, err); /* call main */
802 lua_remove(L, err); /* remove error function */
803 }
804 callalert(L, status);
805 return status;
806}
807
808
809LUA_API int lua_dofile (lua_State *L, const char *filename) {
810 return aux_do(L, lua_loadfile(L, filename));
811}
812
813
814LUA_API int lua_dobuffer (lua_State *L, const char *buff, size_t size,
815 const char *name) {
816 return aux_do(L, lua_loadbuffer(L, buff, size, name));
817}
818
819
820LUA_API int lua_dostring (lua_State *L, const char *str) {
821 return lua_dobuffer(L, str, strlen(str), str);
822}
823
824/* }====================================================== */
diff --git a/ldo.c b/ldo.c
index c11c5639..ee9dd249 100644
--- a/ldo.c
+++ b/ldo.c
@@ -1,5 +1,5 @@
1/* 1/*
2** $Id: ldo.c,v 1.176 2002/05/16 18:39:46 roberto Exp roberto $ 2** $Id: ldo.c,v 1.177 2002/05/27 20:35:40 roberto Exp roberto $
3** Stack and Call structure of Lua 3** Stack and Call structure of Lua
4** See Copyright Notice in lua.h 4** See Copyright Notice in lua.h
5*/ 5*/
@@ -420,7 +420,6 @@ int luaD_protectedparser (lua_State *L, ZIO *z, int bin) {
420 struct SParser p; 420 struct SParser p;
421 lu_mem old_blocks; 421 lu_mem old_blocks;
422 int status; 422 int status;
423 lua_lock(L);
424 p.z = z; p.bin = bin; 423 p.z = z; p.bin = bin;
425 /* before parsing, give a (good) chance to GC */ 424 /* before parsing, give a (good) chance to GC */
426 if (G(L)->nblocks + G(L)->nblocks/4 >= G(L)->GCthreshold) 425 if (G(L)->nblocks + G(L)->nblocks/4 >= G(L)->GCthreshold)
@@ -437,7 +436,6 @@ int luaD_protectedparser (lua_State *L, ZIO *z, int bin) {
437 setobj(L->top++, &p.err); 436 setobj(L->top++, &p.err);
438 lua_assert(status != LUA_ERRRUN); 437 lua_assert(status != LUA_ERRRUN);
439 } 438 }
440 lua_unlock(L);
441 return status; 439 return status;
442} 440}
443 441
diff --git a/lua.h b/lua.h
index 710e0c4d..6277b98c 100644
--- a/lua.h
+++ b/lua.h
@@ -1,5 +1,5 @@
1/* 1/*
2** $Id: lua.h,v 1.133 2002/05/16 18:39:46 roberto Exp roberto $ 2** $Id: lua.h,v 1.134 2002/05/23 20:29:05 roberto Exp roberto $
3** Lua - An Extensible Extension Language 3** Lua - An Extensible Extension Language
4** Tecgraf: Grupo de Tecnologia em Computacao Grafica, PUC-Rio, Brazil 4** Tecgraf: Grupo de Tecnologia em Computacao Grafica, PUC-Rio, Brazil
5** e-mail: info@lua.org 5** e-mail: info@lua.org
@@ -51,6 +51,11 @@ typedef struct lua_State lua_State;
51typedef int (*lua_CFunction) (lua_State *L); 51typedef int (*lua_CFunction) (lua_State *L);
52 52
53 53
54/*
55** function for loading Lua code
56*/
57typedef const char * (*lua_Getblock) (void *ud, size_t *size);
58
54 59
55/* 60/*
56** basic types 61** basic types
@@ -176,9 +181,8 @@ LUA_API void lua_setmetatable (lua_State *L, int objindex);
176*/ 181*/
177LUA_API void lua_rawcall (lua_State *L, int nargs, int nresults); 182LUA_API void lua_rawcall (lua_State *L, int nargs, int nresults);
178LUA_API int lua_pcall (lua_State *L, int nargs, int nresults, int errf); 183LUA_API int lua_pcall (lua_State *L, int nargs, int nresults, int errf);
179LUA_API int lua_loadfile (lua_State *L, const char *filename); 184LUA_API int lua_load (lua_State *L, lua_Getblock getblock, void *ud,
180LUA_API int lua_loadbuffer (lua_State *L, const char *buff, size_t size, 185 int binary, const char *chunkname);
181 const char *name);
182 186
183 187
184/* 188/*
@@ -249,11 +253,6 @@ LUA_API void *lua_newuserdata (lua_State *L, size_t size);
249** compatibility macros and functions 253** compatibility macros and functions
250*/ 254*/
251 255
252LUA_API int lua_call (lua_State *L, int nargs, int nresults);
253LUA_API int lua_dofile (lua_State *L, const char *filename);
254LUA_API int lua_dostring (lua_State *L, const char *str);
255LUA_API int lua_dobuffer (lua_State *L, const char *buff, size_t size,
256 const char *name);
257 256
258LUA_API int lua_pushupvalues (lua_State *L); 257LUA_API int lua_pushupvalues (lua_State *L);
259 258
@@ -292,6 +291,9 @@ LUA_API int lua_pushupvalues (lua_State *L);
292** ======================================================================= 291** =======================================================================
293*/ 292*/
294 293
294/* binary files start with <esc>Lua */
295#define LUA_SIGNATURE "\033Lua"
296
295/* formats for Lua numbers */ 297/* formats for Lua numbers */
296#ifndef LUA_NUMBER_SCAN 298#ifndef LUA_NUMBER_SCAN
297#define LUA_NUMBER_SCAN "%lf" 299#define LUA_NUMBER_SCAN "%lf"
diff --git a/lundump.h b/lundump.h
index b82f0b8c..6c647acf 100644
--- a/lundump.h
+++ b/lundump.h
@@ -1,5 +1,5 @@
1/* 1/*
2** $Id: lundump.h,v 1.23 2001/06/28 13:55:17 lhf Exp lhf $ 2** $Id: lundump.h,v 1.23 2001/07/12 19:34:03 roberto Exp roberto $
3** load pre-compiled Lua chunks 3** load pre-compiled Lua chunks
4** See Copyright Notice in lua.h 4** See Copyright Notice in lua.h
5*/ 5*/
@@ -19,7 +19,6 @@ int luaU_endianness (void);
19/* definitions for headers of binary files */ 19/* definitions for headers of binary files */
20#define VERSION 0x41 /* last format change was in 4.1 */ 20#define VERSION 0x41 /* last format change was in 4.1 */
21#define VERSION0 0x41 /* last major change was in 4.1 */ 21#define VERSION0 0x41 /* last major change was in 4.1 */
22#define LUA_SIGNATURE "\033Lua" /* binary files start with <esc>Lua */
23 22
24/* a multiple of PI for testing native format */ 23/* a multiple of PI for testing native format */
25/* multiplying by 1E8 gives non-trivial integer values */ 24/* multiplying by 1E8 gives non-trivial integer values */
diff --git a/lzio.c b/lzio.c
index 6bbe6982..3e3610c9 100644
--- a/lzio.c
+++ b/lzio.c
@@ -1,69 +1,44 @@
1/* 1/*
2** $Id: lzio.c,v 1.15 2001/11/28 20:13:13 roberto Exp roberto $ 2** $Id: lzio.c,v 1.16 2002/04/29 12:37:41 roberto Exp roberto $
3** a generic input stream interface 3** a generic input stream interface
4** See Copyright Notice in lua.h 4** See Copyright Notice in lua.h
5*/ 5*/
6 6
7 7
8
9#include <stdio.h>
10#include <string.h> 8#include <string.h>
11 9
12#include "lua.h" 10#include "lua.h"
13 11
12#include "llimits.h"
14#include "lzio.h" 13#include "lzio.h"
15 14
16 15
17 16
18/* ----------------------------------------------------- memory buffers --- */ 17int luaZ_fill (ZIO *z) {
19 18 size_t size;
20static int zmfilbuf (ZIO* z) { 19 const char *buff = z->getblock(z->ud, &size);
21 (void)z; /* to avoid warnings */ 20 if (buff == NULL || size == 0) return EOZ;
22 return EOZ; 21 z->n = size - 1;
23} 22 z->p = buff;
24
25
26ZIO* zmopen (ZIO* z, const char* b, size_t size, const char *name) {
27 if (b==NULL) return NULL;
28 z->n = size;
29 z->p = (const unsigned char *)b;
30 z->filbuf = zmfilbuf;
31 z->u = NULL;
32 z->name = name;
33 return z;
34}
35
36
37/* -------------------------------------------------------------- FILEs --- */
38
39static int zffilbuf (ZIO* z) {
40 size_t n;
41 if (feof((FILE *)z->u)) return EOZ;
42 n = fread(z->buffer, 1, ZBSIZE, (FILE *)z->u);
43 if (n==0) return EOZ;
44 z->n = n-1;
45 z->p = z->buffer;
46 return *(z->p++); 23 return *(z->p++);
47} 24}
48 25
49 26
50ZIO* zFopen (ZIO* z, FILE* f, const char *name) { 27void luaZ_init (ZIO *z, lua_Getblock getblock, void *ud, const char *name) {
51 if (f==NULL) return NULL; 28 z->getblock = getblock;
52 z->n = 0; 29 z->ud = ud;
53 z->p = z->buffer;
54 z->filbuf = zffilbuf;
55 z->u = f;
56 z->name = name; 30 z->name = name;
57 return z; 31 z->n = 0;
32 z->p = NULL;
58} 33}
59 34
60 35
61/* --------------------------------------------------------------- read --- */ 36/* --------------------------------------------------------------- read --- */
62size_t zread (ZIO *z, void *b, size_t n) { 37size_t luaZ_zread (ZIO *z, void *b, size_t n) {
63 while (n) { 38 while (n) {
64 size_t m; 39 size_t m;
65 if (z->n == 0) { 40 if (z->n == 0) {
66 if (z->filbuf(z) == EOZ) 41 if (luaZ_fill(z) == EOZ)
67 return n; /* return number of missing bytes */ 42 return n; /* return number of missing bytes */
68 else { 43 else {
69 ++z->n; /* filbuf removed first byte; put back it */ 44 ++z->n; /* filbuf removed first byte; put back it */
@@ -79,3 +54,4 @@ size_t zread (ZIO *z, void *b, size_t n) {
79 } 54 }
80 return 0; 55 return 0;
81} 56}
57
diff --git a/lzio.h b/lzio.h
index 283fdc38..9ffbbf8a 100644
--- a/lzio.h
+++ b/lzio.h
@@ -1,5 +1,5 @@
1/* 1/*
2** $Id: lzio.h,v 1.8 2001/03/26 14:31:49 roberto Exp roberto $ 2** $Id: lzio.h,v 1.9 2002/04/29 12:37:41 roberto Exp roberto $
3** Buffered streams 3** Buffered streams
4** See Copyright Notice in lua.h 4** See Copyright Notice in lua.h
5*/ 5*/
@@ -8,43 +8,37 @@
8#ifndef lzio_h 8#ifndef lzio_h
9#define lzio_h 9#define lzio_h
10 10
11#include <stdio.h> 11#include "lua.h"
12
13 12
14 13
15/* For Lua only */ 14/* For Lua only */
16#define zFopen luaZ_Fopen 15#define zread luaZ_zread
17#define zmopen luaZ_mopen
18#define zread luaZ_read
19 16
20#define EOZ (-1) /* end of stream */ 17#define EOZ (-1) /* end of stream */
21 18
22typedef struct zio ZIO; 19typedef struct zio ZIO;
23 20
24ZIO* zFopen (ZIO* z, FILE* f, const char *name); /* open FILEs */ 21#define zgetc(z) (((z)->n--)>0 ? \
25ZIO* zmopen (ZIO* z, const char* b, size_t size, const char *name); /* memory */ 22 cast(int, cast(unsigned char, *(z)->p++)) : \
26 23 luaZ_fill(z))
27size_t zread (ZIO* z, void* b, size_t n); /* read next n bytes */
28 24
29#define zgetc(z) (((z)->n--)>0 ? ((int)*(z)->p++): (z)->filbuf(z))
30#define zname(z) ((z)->name) 25#define zname(z) ((z)->name)
31 26
27void luaZ_init (ZIO *z, lua_Getblock getblock, void *ud, const char *name);
28size_t luaZ_zread (ZIO* z, void* b, size_t n); /* read next n bytes */
32 29
33 30
34/* --------- Private Part ------------------ */ 31/* --------- Private Part ------------------ */
35 32
36#ifndef ZBSIZE
37#define ZBSIZE 256 /* buffer size */
38#endif
39
40struct zio { 33struct zio {
41 size_t n; /* bytes still unread */ 34 size_t n; /* bytes still unread */
42 const unsigned char* p; /* current position in buffer */ 35 const char *p; /* current position in buffer */
43 int (*filbuf)(ZIO* z); 36 lua_Getblock getblock;
44 void* u; /* additional data */ 37 void* ud; /* additional data */
45 const char *name; 38 const char *name;
46 unsigned char buffer[ZBSIZE]; /* buffer */
47}; 39};
48 40
49 41
42int luaZ_fill (ZIO *z);
43
50#endif 44#endif