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 @@
11 /*
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 $
33 ** Lua stand-alone interpreter
44 ** See Copyright Notice in lua.h
55 */
@@ -18,19 +18,10 @@ · 12 unmodified lines
1818 #include "lualib.h"
1919
2020
21−/*
22−** generic extra include file
23−*/
24−#ifdef LUA_USERCONFIG
25−#include LUA_USERCONFIG
26−#endif
27−
28−
29−
3021
3122 static lua_State *globalL = NULL;
3223
33−static const char *progname = PROGNAME;
24+static const char *progname = LUA_PROGNAME;
3425
3526
3627
@@ -53,10 +44,11 @@ static void print_usage (void) { · 16 unmodified lines
5344 "usage: %s [options] [script [args]].\n"
5445 "Available options are:\n"
5546 " - execute stdin as a file\n"
56− " -e stat execute string `stat'\n"
57− " -i enter interactive mode after executing `script'\n"
58− " -l name load and run library `name'\n"
47+ " -e stat execute string " LUA_QL("stat") "\n"
48+ " -i enter interactive mode after executing " LUA_QL("script") "\n"
49+ " -l name require library " LUA_QL("name") "\n"
5950 " -v show version information\n"
51+ " -w trap access to undefined globals\n"
6052 " -- stop handling options\n" ,
6153 progname);
6254 }
@@ -110,15 +102,14 @@ static void print_version (void) { · 47 unmodified lines
110102 }
111103
112104
113−static int getargs (lua_State *L, char *argv[], int n) {
114− int i, narg;
115− for (i=n+1; argv[i]; i++) {
116− luaL_checkstack(L, 1, "too many arguments to script");
105+static int getargs (lua_State *L, int argc, char **argv, int n) {
106+ int narg = argc - (n + 1); /* number of arguments to the script */
107+ int i;
108+ luaL_checkstack(L, narg + 3, "too many arguments to script");
109+ for (i=n+1; i < argc; i++)
117110 lua_pushstring(L, argv[i]);
118− }
119− narg = i-(n+1); /* number of arguments to the script (not to `lua.c') */
120111 lua_newtable(L);
121− for (i=0; argv[i]; i++) {
112+ for (i=0; i < argc; i++) {
122113 lua_pushstring(L, argv[i]);
123114 lua_rawseti(L, -2, i - n);
124115 }
@@ -139,54 +130,18 @@ static int dostring (lua_State *L, const char *s, const char *name) { · 14 unmodified lines
139130
140131
141132 static int dolibrary (lua_State *L, const char *name) {
142− luaL_getfield(L, LUA_GLOBALSINDEX, "package.path");
143− if (!lua_isstring(L, -1)) {
144− l_message(progname, "`package.path' must be a string");
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−
167−static 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− }
133+ lua_getglobal(L, "require");
134+ lua_pushstring(L, name);
135+ return report(L, lua_pcall(L, 1, 0, 0));
179136 }
180137
181−#endif
182−
183138
184139 static const char *get_prompt (lua_State *L, int firstline) {
185− const char *p = NULL;
140+ const char *p;
186141 lua_pushstring(L, firstline ? "_PROMPT" : "_PROMPT2");
187142 lua_rawget(L, LUA_GLOBALSINDEX);
188143 p = lua_tostring(L, -1);
189− if (p == NULL) p = (firstline ? PROMPT : PROMPT2);
144+ if (p == NULL) p = (firstline ? LUA_PROMPT : LUA_PROMPT2);
190145 lua_pop(L, 1); /* remove global */
191146 return p;
192147 }
@@ -194,7 +149,7 @@ static const char *get_prompt (lua_State *L, int firstline) { · 1 unmodified line
194149
195150 static int incomplete (lua_State *L, int status) {
196151 if (status == LUA_ERRSYNTAX &&
197− strstr(lua_tostring(L, -1), "near `<eof>'") != NULL) {
152+ strstr(lua_tostring(L, -1), "<eof>") != NULL) {
198153 lua_pop(L, 1);
199154 return 1;
200155 }
@@ -203,23 +158,40 @@ static int incomplete (lua_State *L, int status) { · 2 unmodified lines
203158 }
204159
205160
161+static 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+
206180 static int loadline (lua_State *L) {
207181 int status;
208182 lua_settop(L, 0);
209− if (lua_readline(L, get_prompt(L, 1)) == 0) /* no input? */
210− return -1;
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− }
183+ if (!pushline(L, 1))
184+ return -1; /* no input */
215185 for (;;) { /* repeat until gets a complete line */
216186 status = luaL_loadbuffer(L, lua_tostring(L, 1), lua_strlen(L, 1), "=stdin");
217187 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? */
219189 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 */
221193 }
222− lua_saveline(L, lua_tostring(L, 1));
194+ lua_saveline(L, 1);
223195 lua_remove(L, 1); /* remove line */
224196 return status;
225197 }
@@ -237,8 +209,9 @@ static void dotty (lua_State *L) { · 11 unmodified lines
237209 lua_getglobal(L, "print");
238210 lua_insert(L, 1);
239211 if (lua_pcall(L, lua_gettop(L)-1, 0, 0) != 0)
240− l_message(progname, lua_pushfstring(L, "error calling `print' (%s)",
241− lua_tostring(L, -1)));
212+ l_message(progname, lua_pushfstring(L,
213+ "error calling " LUA_QL("print") " (%s)",
214+ lua_tostring(L, -1)));
242215 }
243216 }
244217 lua_settop(L, 0); /* clear stack */
@@ -250,17 +223,17 @@ static void dotty (lua_State *L) { · 5 unmodified lines
250223 static int checkvar (lua_State *L) {
251224 const char *name = lua_tostring(L, 2);
252225 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);
254227 return 0;
255228 }
256229
257230
258231 #define clearinteractive(i) (*i &= 2)
259232
260−static int handle_argv (lua_State *L, char *argv[], int *interactive) {
233+static int handle_argv (lua_State *L, int argc, char **argv, int *interactive) {
261234 if (argv[1] == NULL) { /* no arguments? */
262235 *interactive = 0;
263− if (stdin_is_tty())
236+ if (lua_stdin_is_tty())
264237 dotty(L);
265238 else
266239 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
330303 }
331304 } endloop:
332305 if (argv[i] != NULL) {
333− const char *filename = argv[i];
334− int narg = getargs(L, argv, i); /* collect arguments */
335306 int status;
307+ const char *filename = argv[i];
308+ int narg = getargs(L, argc, argv, i); /* collect arguments */
336309 lua_setglobal(L, "arg");
337310 clearinteractive(interactive);
338311 status = luaL_loadfile(L, filename);
@@ -371,10 +344,10 @@ static int pmain (lua_State *L) { · 32 unmodified lines
371344 int interactive = 1;
372345 if (s->argv[0] && s->argv[0][0]) progname = s->argv[0];
373346 globalL = L;
374− luaopen_stdlibs(L); /* open libraries */
347+ luaL_openlibs(L); /* open libraries */
375348 status = handle_luainit(L);
376349 if (status == 0) {
377− status = handle_argv(L, s->argv, &interactive);
350+ status = handle_argv(L, s->argc, s->argv, &interactive);
378351 if (status == 0 && interactive) dotty(L);
379352 }
380353 s->status = status;
@@ -382,7 +355,7 @@ static int pmain (lua_State *L) { · 1 unmodified line
382355 }
383356
384357
385−int main (int argc, char *argv[]) {
358+int main (int argc, char **argv) {
386359 int status;
387360 struct Smain s;
388361 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