History

Lua 5.1-work6

Lua Team committed 21 years ago

Commit b5301731 parent e684a4d66 files · +1742 −1346Browse files

Showing the changes to src/lua.c (1 of 66 files). Show all

Unified · Split

src/lua.c+55−82
@@ -1,5 +1,5 @@
1/*1/*
2** $Id: lua.c,v 1.135 2005/01/10 17:21:10 roberto Exp $2** $Id: lua.c,v 1.144 2005/05/17 19:49:15 roberto Exp $
3** Lua stand-alone interpreter3** Lua stand-alone interpreter
4** See Copyright Notice in lua.h4** See Copyright Notice in lua.h
5*/5*/
@@ -18,19 +18,10 @@ · 12 unmodified lines
18#include "lualib.h"18#include "lualib.h"
19 19
20 20
21/*
22** generic extra include file
23*/
24#ifdef LUA_USERCONFIG
25#include LUA_USERCONFIG
26#endif
27
28
29
30 21
31static lua_State *globalL = NULL;22static lua_State *globalL = NULL;
32 23
33static const char *progname = PROGNAME;24static const char *progname = LUA_PROGNAME;
34 25
35 26
36 27
@@ -53,10 +44,11 @@ static void print_usage (void) { · 16 unmodified lines
53 "usage: %s [options] [script [args]].\n"44 "usage: %s [options] [script [args]].\n"
54 "Available options are:\n"45 "Available options are:\n"
55 " - execute stdin as a file\n"46 " - execute stdin as a file\n"
56 " -e stat execute string `stat'\n"47 " -e stat execute string " LUA_QL("stat") "\n"
57 " -i enter interactive mode after executing `script'\n"48 " -i enter interactive mode after executing " LUA_QL("script") "\n"
58 " -l name load and run library `name'\n"49 " -l name require library " LUA_QL("name") "\n"
59 " -v show version information\n"50 " -v show version information\n"
51 " -w trap access to undefined globals\n"
60 " -- stop handling options\n" ,52 " -- stop handling options\n" ,
61 progname);53 progname);
62}54}
@@ -110,15 +102,14 @@ static void print_version (void) { · 47 unmodified lines
110}102}
111 103
112 104
113static int getargs (lua_State *L, char *argv[], int n) {105static int getargs (lua_State *L, int argc, char **argv, int n) {
114 int i, narg;106 int narg = argc - (n + 1); /* number of arguments to the script */
115 for (i=n+1; argv[i]; i++) {107 int i;
116 luaL_checkstack(L, 1, "too many arguments to script");108 luaL_checkstack(L, narg + 3, "too many arguments to script");
109 for (i=n+1; i < argc; i++)
117 lua_pushstring(L, argv[i]);110 lua_pushstring(L, argv[i]);
118 }
119 narg = i-(n+1); /* number of arguments to the script (not to `lua.c') */
120 lua_newtable(L);111 lua_newtable(L);
121 for (i=0; argv[i]; i++) {112 for (i=0; i < argc; i++) {
122 lua_pushstring(L, argv[i]);113 lua_pushstring(L, argv[i]);
123 lua_rawseti(L, -2, i - n);114 lua_rawseti(L, -2, i - n);
124 }115 }
@@ -139,54 +130,18 @@ static int dostring (lua_State *L, const char *s, const char *name) { · 14 unmodified lines
139 130
140 131
141static int dolibrary (lua_State *L, const char *name) {132static int dolibrary (lua_State *L, const char *name) {
142 luaL_getfield(L, LUA_GLOBALSINDEX, "package.path");133 lua_getglobal(L, "require");
143 if (!lua_isstring(L, -1)) {134 lua_pushstring(L, name);
144 l_message(progname, "`package.path' must be a string");135 return report(L, lua_pcall(L, 1, 0, 0));
145 return 1;
146 }
147 name = luaL_searchpath(L, name, lua_tostring(L, -1));
148 if (name == NULL) return report(L, 1);
149 else return dofile(L, name);
150}
151
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}136}
180 137
181#endif
182
183 138
184static const char *get_prompt (lua_State *L, int firstline) {139static const char *get_prompt (lua_State *L, int firstline) {
185 const char *p = NULL;140 const char *p;
186 lua_pushstring(L, firstline ? "_PROMPT" : "_PROMPT2");141 lua_pushstring(L, firstline ? "_PROMPT" : "_PROMPT2");
187 lua_rawget(L, LUA_GLOBALSINDEX);142 lua_rawget(L, LUA_GLOBALSINDEX);
188 p = lua_tostring(L, -1);143 p = lua_tostring(L, -1);
189 if (p == NULL) p = (firstline ? PROMPT : PROMPT2);144 if (p == NULL) p = (firstline ? LUA_PROMPT : LUA_PROMPT2);
190 lua_pop(L, 1); /* remove global */145 lua_pop(L, 1); /* remove global */
191 return p;146 return p;
192}147}
@@ -194,7 +149,7 @@ static const char *get_prompt (lua_State *L, int firstline) { · 1 unmodified line
194 149
195static int incomplete (lua_State *L, int status) {150static int incomplete (lua_State *L, int status) {
196 if (status == LUA_ERRSYNTAX &&151 if (status == LUA_ERRSYNTAX &&
197 strstr(lua_tostring(L, -1), "near `<eof>'") != NULL) {152 strstr(lua_tostring(L, -1), "<eof>") != NULL) {
198 lua_pop(L, 1);153 lua_pop(L, 1);
199 return 1;154 return 1;
200 }155 }
@@ -203,23 +158,40 @@ static int incomplete (lua_State *L, int status) { · 2 unmodified lines
203}158}
204 159
205 160
161static int pushline (lua_State *L, int firstline) {
162 char buffer[LUA_MAXINPUT];
163 char *b = buffer;
164 size_t l;
165 const char *prmt = get_prompt(L, firstline);
166 if (lua_readline(L, b, prmt) == 0)
167 return 0; /* no input */
168 l = strlen(b);
169 if (l > 0 && b[l-1] == '\n') /* line ends with newline? */
170 b[l-1] = '\0'; /* remove it */
171 if (firstline && b[0] == '=') /* first line starts with `=' ? */
172 lua_pushfstring(L, "return %s", b+1); /* change it to `return' */
173 else
174 lua_pushstring(L, b);
175 lua_freeline(L, b);
176 return 1;
177}
178
179
206static int loadline (lua_State *L) {180static int loadline (lua_State *L) {
207 int status;181 int status;
208 lua_settop(L, 0);182 lua_settop(L, 0);
209 if (lua_readline(L, get_prompt(L, 1)) == 0) /* no input? */183 if (!pushline(L, 1))
210 return -1;184 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 */185 for (;;) { /* repeat until gets a complete line */
216 status = luaL_loadbuffer(L, lua_tostring(L, 1), lua_strlen(L, 1), "=stdin");186 status = luaL_loadbuffer(L, lua_tostring(L, 1), lua_strlen(L, 1), "=stdin");
217 if (!incomplete(L, status)) break; /* cannot try to add lines? */187 if (!incomplete(L, status)) break; /* cannot try to add lines? */
218 if (lua_readline(L, get_prompt(L, 0)) == 0) /* no more input? */188 if (!pushline(L, 0)) /* no more input? */
219 return -1;189 return -1;
220 lua_concat(L, lua_gettop(L)); /* join lines */190 lua_pushliteral(L, "\n"); /* add a new line... */
191 lua_insert(L, -2); /* ...between the two lines */
192 lua_concat(L, 3); /* join them */
221 }193 }
222 lua_saveline(L, lua_tostring(L, 1));194 lua_saveline(L, 1);
223 lua_remove(L, 1); /* remove line */195 lua_remove(L, 1); /* remove line */
224 return status;196 return status;
225}197}
@@ -237,8 +209,9 @@ static void dotty (lua_State *L) { · 11 unmodified lines
237 lua_getglobal(L, "print");209 lua_getglobal(L, "print");
238 lua_insert(L, 1);210 lua_insert(L, 1);
239 if (lua_pcall(L, lua_gettop(L)-1, 0, 0) != 0)211 if (lua_pcall(L, lua_gettop(L)-1, 0, 0) != 0)
240 l_message(progname, lua_pushfstring(L, "error calling `print' (%s)",212 l_message(progname, lua_pushfstring(L,
241 lua_tostring(L, -1)));213 "error calling " LUA_QL("print") " (%s)",
214 lua_tostring(L, -1)));
242 }215 }
243 }216 }
244 lua_settop(L, 0); /* clear stack */217 lua_settop(L, 0); /* clear stack */
@@ -250,17 +223,17 @@ static void dotty (lua_State *L) { · 5 unmodified lines
250static int checkvar (lua_State *L) {223static int checkvar (lua_State *L) {
251 const char *name = lua_tostring(L, 2);224 const char *name = lua_tostring(L, 2);
252 if (name)225 if (name)
253 luaL_error(L, "attempt to access undefined variable `%s'", name);226 luaL_error(L, "attempt to access undefined variable " LUA_QS, name);
254 return 0;227 return 0;
255}228}
256 229
257 230
258#define clearinteractive(i) (*i &= 2)231#define clearinteractive(i) (*i &= 2)
259 232
260static int handle_argv (lua_State *L, char *argv[], int *interactive) {233static int handle_argv (lua_State *L, int argc, char **argv, int *interactive) {
261 if (argv[1] == NULL) { /* no arguments? */234 if (argv[1] == NULL) { /* no arguments? */
262 *interactive = 0;235 *interactive = 0;
263 if (stdin_is_tty())236 if (lua_stdin_is_tty())
264 dotty(L);237 dotty(L);
265 else238 else
266 dofile(L, NULL); /* executes stdin as a file */239 dofile(L, NULL); /* executes stdin as a file */
@@ -330,9 +303,9 @@ static int handle_argv (lua_State *L, char *argv[], int *interactive) { · 63 unmodified lines
330 }303 }
331 } endloop:304 } endloop:
332 if (argv[i] != NULL) {305 if (argv[i] != NULL) {
333 const char *filename = argv[i];
334 int narg = getargs(L, argv, i); /* collect arguments */
335 int status;306 int status;
307 const char *filename = argv[i];
308 int narg = getargs(L, argc, argv, i); /* collect arguments */
336 lua_setglobal(L, "arg");309 lua_setglobal(L, "arg");
337 clearinteractive(interactive);310 clearinteractive(interactive);
338 status = luaL_loadfile(L, filename);311 status = luaL_loadfile(L, filename);
@@ -371,10 +344,10 @@ static int pmain (lua_State *L) { · 32 unmodified lines
371 int interactive = 1;344 int interactive = 1;
372 if (s->argv[0] && s->argv[0][0]) progname = s->argv[0];345 if (s->argv[0] && s->argv[0][0]) progname = s->argv[0];
373 globalL = L;346 globalL = L;
374 luaopen_stdlibs(L); /* open libraries */347 luaL_openlibs(L); /* open libraries */
375 status = handle_luainit(L);348 status = handle_luainit(L);
376 if (status == 0) {349 if (status == 0) {
377 status = handle_argv(L, s->argv, &interactive);350 status = handle_argv(L, s->argc, s->argv, &interactive);
378 if (status == 0 && interactive) dotty(L);351 if (status == 0 && interactive) dotty(L);
379 }352 }
380 s->status = status;353 s->status = status;
@@ -382,7 +355,7 @@ static int pmain (lua_State *L) { · 1 unmodified line
382}355}
383 356
384 357
385int main (int argc, char *argv[]) {358int main (int argc, char **argv) {
386 int status;359 int status;
387 struct Smain s;360 struct Smain s;
388 lua_State *L = lua_open(); /* create state */361 lua_State *L = lua_open(); /* create state */

Keyboard shortcuts

General

Show keyboard shortcuts
?
Focus the current search
/

Issues and pull requests

Open filters
f

Issues, pull requests and Inbox

Select next item
j↓
Select previous item
k↑
Open selected item
Enter
Dismiss filters or clear selection
Esc

Repository code

Go to file
t