aboutsummaryrefslogtreecommitdiff
path: root/inout.c
diff options
context:
space:
mode:
authorRoberto Ierusalimschy <roberto@inf.puc-rio.br>1997-06-16 13:50:22 -0300
committerRoberto Ierusalimschy <roberto@inf.puc-rio.br>1997-06-16 13:50:22 -0300
commitc9a2dfeb2c71b1c6b8164a1e5aad4b178950e197 (patch)
tree6f099e983ecb968f625a8824ac2ca78e625882e3 /inout.c
parent9fe5be3acf340b90fe0c24d36a601adaf6f21c79 (diff)
downloadlua-c9a2dfeb2c71b1c6b8164a1e5aad4b178950e197.tar.gz
lua-c9a2dfeb2c71b1c6b8164a1e5aad4b178950e197.tar.bz2
lua-c9a2dfeb2c71b1c6b8164a1e5aad4b178950e197.zip
using "zio" for parsing Lua code.
Diffstat (limited to 'inout.c')
-rw-r--r--inout.c111
1 files changed, 49 insertions, 62 deletions
diff --git a/inout.c b/inout.c
index 50782703..feecf9bc 100644
--- a/inout.c
+++ b/inout.c
@@ -5,22 +5,24 @@
5** Also provides some predefined lua functions. 5** Also provides some predefined lua functions.
6*/ 6*/
7 7
8char *rcs_inout="$Id: inout.c,v 2.59 1997/05/26 14:42:51 roberto Exp roberto $"; 8char *rcs_inout="$Id: inout.c,v 2.60 1997/06/09 17:28:14 roberto Exp roberto $";
9 9
10#include <stdio.h> 10#include <stdio.h>
11#include <string.h> 11#include <string.h>
12 12
13#include "auxlib.h" 13#include "auxlib.h"
14#include "lex.h" 14#include "fallback.h"
15#include "opcode.h" 15#include "hash.h"
16#include "inout.h" 16#include "inout.h"
17#include "table.h" 17#include "lex.h"
18#include "tree.h"
19#include "lua.h" 18#include "lua.h"
20#include "hash.h"
21#include "luamem.h" 19#include "luamem.h"
22#include "fallback.h"
23#include "luamem.h" 20#include "luamem.h"
21#include "opcode.h"
22#include "table.h"
23#include "tree.h"
24#include "undump.h"
25#include "zio.h"
24 26
25 27
26/* Exported variables */ 28/* Exported variables */
@@ -34,73 +36,58 @@ char *luaI_typenames[] = { /* ORDER LUA_T */
34 NULL 36 NULL
35}; 37};
36 38
37static FILE *fp;
38static char *st;
39 39
40/*
41** Function to get the next character from the input file
42*/
43static int fileinput (void)
44{
45 int c = fgetc(fp);
46 return (c == EOF) ? 0 : c;
47}
48 40
49/* 41int lua_dofile (char *filename)
50** Function to get the next character from the input string
51*/
52static int stringinput (void)
53{ 42{
54 return *st++; 43 int status;
55} 44 int c;
45 FILE *f = (filename == NULL) ? stdin : fopen(filename, "r");
46 if (f == NULL)
47 return 2;
48 lua_parsedfile = luaI_createfixedstring(filename?filename:"(stdin)")->str;
49 c = fgetc(f);
50 ungetc(c, f);
51 if (c == ID_CHUNK) {
52 ZIO z;
53 f = freopen(filename, "rb", f); /* set binary mode */
54 zFopen(&z, f);
55 lua_setinput(&z);
56 status = luaI_undump(&z);
57 zclose(&z);
58 }
59 else {
60 ZIO z;
61 if (c == '#')
62 while ((c=fgetc(f)) != '\n') /* skip first line */;
63 zFopen(&z, f);
64 lua_setinput(&z);
65 status = lua_domain();
66 zclose(&z);
67 }
68 return status;
69}
56 70
57/*
58** Function to open a file to be input unit.
59** Return the file.
60*/
61FILE *lua_openfile (char *fn)
62{
63 lua_setinput (fileinput);
64 if (fn == NULL)
65 {
66 fp = stdin;
67 fn = "(stdin)";
68 }
69 else
70 fp = fopen (fn, "r");
71 lua_parsedfile = luaI_createfixedstring(fn)->str;
72 return fp;
73}
74 71
75/*
76** Function to close an opened file
77*/
78void lua_closefile (void)
79{
80 if (fp != stdin)
81 fclose(fp);
82}
83 72
84/*
85** Function to open a string to be input unit
86*/
87#define SIZE_PREF 20 /* size of string prefix to appear in error messages */ 73#define SIZE_PREF 20 /* size of string prefix to appear in error messages */
88void lua_openstring (char *s) 74
75int lua_dostring (char *str)
89{ 76{
77 int status;
90 char buff[SIZE_PREF+25]; 78 char buff[SIZE_PREF+25];
91 lua_setinput(stringinput); 79 ZIO z;
92 st = s; 80 if (str == NULL) return 1;
93 sprintf(buff, "(dostring) >> %.20s%s", s, 81 sprintf(buff, "(dostring) >> %.20s%s", str,
94 (strlen(s) > SIZE_PREF) ? "..." : ""); 82 (strlen(str) > SIZE_PREF) ? "..." : "");
95 lua_parsedfile = luaI_createfixedstring(buff)->str; 83 lua_parsedfile = luaI_createfixedstring(buff)->str;
84 zsopen(&z, str);
85 lua_setinput(&z);
86 status = lua_domain();
87 zclose(&z);
88 return status;
96} 89}
97 90
98/*
99** Function to close an opened string
100*/
101void lua_closestring (void)
102{
103}
104 91
105 92
106 93