aboutsummaryrefslogtreecommitdiff
path: root/lua.c
diff options
context:
space:
mode:
authorRoberto Ierusalimschy <roberto@inf.puc-rio.br>2005-03-21 15:12:07 -0300
committerRoberto Ierusalimschy <roberto@inf.puc-rio.br>2005-03-21 15:12:07 -0300
commit6d475731cae37e8e46abf217697198bcce316b42 (patch)
tree12ac3d08eac911d83a4b09761ac9f578fb279616 /lua.c
parentf41fc0eb0e7e92d04c9df206b8f185792804bf6a (diff)
downloadlua-6d475731cae37e8e46abf217697198bcce316b42.tar.gz
lua-6d475731cae37e8e46abf217697198bcce316b42.tar.bz2
lua-6d475731cae37e8e46abf217697198bcce316b42.zip
cleaner configuration for lua.c
Diffstat (limited to 'lua.c')
-rw-r--r--lua.c75
1 files changed, 30 insertions, 45 deletions
diff --git a/lua.c b/lua.c
index 2f88abfc..42d94cfe 100644
--- a/lua.c
+++ b/lua.c
@@ -1,5 +1,5 @@
1/* 1/*
2** $Id: lua.c,v 1.134 2005/01/10 16:30:59 roberto Exp roberto $ 2** $Id: lua.c,v 1.135 2005/01/10 17:21:10 roberto Exp roberto $
3** Lua stand-alone interpreter 3** Lua stand-alone interpreter
4** See Copyright Notice in lua.h 4** See Copyright Notice in lua.h
5*/ 5*/
@@ -30,7 +30,7 @@
30 30
31static lua_State *globalL = NULL; 31static lua_State *globalL = NULL;
32 32
33static const char *progname = PROGNAME; 33static const char *progname = LUA_PROGNAME;
34 34
35 35
36 36
@@ -150,43 +150,12 @@ static int dolibrary (lua_State *L, const char *name) {
150} 150}
151 151
152 152
153
154/*
155** this macro defines a function to show the prompt and reads the
156** next line for manual input
157*/
158#ifndef lua_readline
159#define lua_readline(L,prompt) readline(L,prompt)
160
161/* maximum length of an input line */
162#ifndef MAXINPUT
163#define MAXINPUT 512
164#endif
165
166
167static int readline (lua_State *L, const char *prompt) {
168 static char buffer[MAXINPUT];
169 if (prompt) {
170 fputs(prompt, stdout);
171 fflush(stdout);
172 }
173 if (fgets(buffer, sizeof(buffer), stdin) == NULL)
174 return 0; /* read fails */
175 else {
176 lua_pushstring(L, buffer);
177 return 1;
178 }
179}
180
181#endif
182
183
184static const char *get_prompt (lua_State *L, int firstline) { 153static const char *get_prompt (lua_State *L, int firstline) {
185 const char *p = NULL; 154 const char *p;
186 lua_pushstring(L, firstline ? "_PROMPT" : "_PROMPT2"); 155 lua_pushstring(L, firstline ? "_PROMPT" : "_PROMPT2");
187 lua_rawget(L, LUA_GLOBALSINDEX); 156 lua_rawget(L, LUA_GLOBALSINDEX);
188 p = lua_tostring(L, -1); 157 p = lua_tostring(L, -1);
189 if (p == NULL) p = (firstline ? PROMPT : PROMPT2); 158 if (p == NULL) p = (firstline ? LUA_PROMPT : LUA_PROMPT2);
190 lua_pop(L, 1); /* remove global */ 159 lua_pop(L, 1); /* remove global */
191 return p; 160 return p;
192} 161}
@@ -203,23 +172,39 @@ static int incomplete (lua_State *L, int status) {
203} 172}
204 173
205 174
175static int pushline (lua_State *L, int firstline) {
176 char buffer[LUA_MAXINPUT];
177 char *b = buffer;
178 size_t l;
179 const char *prmt = get_prompt(L, firstline);
180 if (lua_readline(L, b, prmt) == 0)
181 return 0; /* no input */
182 l = strlen(b);
183 if (l > 0 && b[l-1] == '\n') /* line ends with newline? */
184 b[l-1] = '\0'; /* remove it */
185 if (firstline && b[0] == '=') /* first line starts with `=' ? */
186 lua_pushfstring(L, "return %s", b+1); /* change it to `return' */
187 else
188 lua_pushstring(L, b);
189 return 1;
190}
191
192
206static int loadline (lua_State *L) { 193static int loadline (lua_State *L) {
207 int status; 194 int status;
208 lua_settop(L, 0); 195 lua_settop(L, 0);
209 if (lua_readline(L, get_prompt(L, 1)) == 0) /* no input? */ 196 if (!pushline(L, 1))
210 return -1; 197 return -1; /* no input */
211 if (lua_tostring(L, -1)[0] == '=') { /* line starts with `=' ? */
212 lua_pushfstring(L, "return %s", lua_tostring(L, -1)+1);/* `=' -> `return' */
213 lua_remove(L, -2); /* remove original line */
214 }
215 for (;;) { /* repeat until gets a complete line */ 198 for (;;) { /* repeat until gets a complete line */
216 status = luaL_loadbuffer(L, lua_tostring(L, 1), lua_strlen(L, 1), "=stdin"); 199 status = luaL_loadbuffer(L, lua_tostring(L, 1), lua_strlen(L, 1), "=stdin");
217 if (!incomplete(L, status)) break; /* cannot try to add lines? */ 200 if (!incomplete(L, status)) break; /* cannot try to add lines? */
218 if (lua_readline(L, get_prompt(L, 0)) == 0) /* no more input? */ 201 if (!pushline(L, 0)) /* no more input? */
219 return -1; 202 return -1;
220 lua_concat(L, lua_gettop(L)); /* join lines */ 203 lua_pushliteral(L, "\n"); /* add a new line... */
204 lua_insert(L, -2); /* ...between the two lines */
205 lua_concat(L, 3); /* join them */
221 } 206 }
222 lua_saveline(L, lua_tostring(L, 1)); 207 lua_saveline(L, 1);
223 lua_remove(L, 1); /* remove line */ 208 lua_remove(L, 1); /* remove line */
224 return status; 209 return status;
225} 210}
@@ -260,7 +245,7 @@ static int checkvar (lua_State *L) {
260static int handle_argv (lua_State *L, char *argv[], int *interactive) { 245static int handle_argv (lua_State *L, char *argv[], int *interactive) {
261 if (argv[1] == NULL) { /* no arguments? */ 246 if (argv[1] == NULL) { /* no arguments? */
262 *interactive = 0; 247 *interactive = 0;
263 if (stdin_is_tty()) 248 if (lua_stdin_is_tty())
264 dotty(L); 249 dotty(L);
265 else 250 else
266 dofile(L, NULL); /* executes stdin as a file */ 251 dofile(L, NULL); /* executes stdin as a file */