Showing the changes to src/lua.c (1 of 66 files). Show all
Unified · Split
| @@ -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 | 3 | ** Lua stand-alone interpreter |
| 4 | 4 | ** See Copyright Notice in lua.h |
| 5 | 5 | */ |
| @@ -18,19 +18,10 @@ · 12 unmodified lines | ||
| 18 | 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 | |
| 31 | 22 | static lua_State *globalL = NULL; |
| 32 | 23 | |
| 33 | −static const char *progname = PROGNAME; | |
| 24 | +static const char *progname = LUA_PROGNAME; | |
| 34 | 25 | |
| 35 | 26 | |
| 36 | 27 | |
| @@ -53,10 +44,11 @@ static void print_usage (void) { · 16 unmodified lines | ||
| 53 | 44 | "usage: %s [options] [script [args]].\n" |
| 54 | 45 | "Available options are:\n" |
| 55 | 46 | " - 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" | |
| 59 | 50 | " -v show version information\n" |
| 51 | + " -w trap access to undefined globals\n" | |
| 60 | 52 | " -- stop handling options\n" , |
| 61 | 53 | progname); |
| 62 | 54 | } |
| @@ -110,15 +102,14 @@ static void print_version (void) { · 47 unmodified lines | ||
| 110 | 102 | } |
| 111 | 103 | |
| 112 | 104 | |
| 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++) | |
| 117 | 110 | lua_pushstring(L, argv[i]); |
| 118 | − } | |
| 119 | − narg = i-(n+1); /* number of arguments to the script (not to `lua.c') */ | |
| 120 | 111 | lua_newtable(L); |
| 121 | − for (i=0; argv[i]; i++) { | |
| 112 | + for (i=0; i < argc; i++) { | |
| 122 | 113 | lua_pushstring(L, argv[i]); |
| 123 | 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 | |
| 141 | 132 | 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)); | |
| 179 | 136 | } |
| 180 | 137 | |
| 181 | −#endif | |
| 182 | − | |
| 183 | 138 | |
| 184 | 139 | static const char *get_prompt (lua_State *L, int firstline) { |
| 185 | − const char *p = NULL; | |
| 140 | + const char *p; | |
| 186 | 141 | lua_pushstring(L, firstline ? "_PROMPT" : "_PROMPT2"); |
| 187 | 142 | lua_rawget(L, LUA_GLOBALSINDEX); |
| 188 | 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 | 145 | lua_pop(L, 1); /* remove global */ |
| 191 | 146 | return p; |
| 192 | 147 | } |
| @@ -194,7 +149,7 @@ static const char *get_prompt (lua_State *L, int firstline) { · 1 unmodified line | ||
| 194 | 149 | |
| 195 | 150 | static int incomplete (lua_State *L, int status) { |
| 196 | 151 | if (status == LUA_ERRSYNTAX && |
| 197 | − strstr(lua_tostring(L, -1), "near `<eof>'") != NULL) { | |
| 152 | + strstr(lua_tostring(L, -1), "<eof>") != NULL) { | |
| 198 | 153 | lua_pop(L, 1); |
| 199 | 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 | |
| 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 | + | |
| 206 | 180 | static int loadline (lua_State *L) { |
| 207 | 181 | int status; |
| 208 | 182 | 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 */ | |
| 215 | 185 | for (;;) { /* repeat until gets a complete line */ |
| 216 | 186 | status = luaL_loadbuffer(L, lua_tostring(L, 1), lua_strlen(L, 1), "=stdin"); |
| 217 | 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 | 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 | 195 | lua_remove(L, 1); /* remove line */ |
| 224 | 196 | return status; |
| 225 | 197 | } |
| @@ -237,8 +209,9 @@ static void dotty (lua_State *L) { · 11 unmodified lines | ||
| 237 | 209 | lua_getglobal(L, "print"); |
| 238 | 210 | lua_insert(L, 1); |
| 239 | 211 | 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))); | |
| 242 | 215 | } |
| 243 | 216 | } |
| 244 | 217 | lua_settop(L, 0); /* clear stack */ |
| @@ -250,17 +223,17 @@ static void dotty (lua_State *L) { · 5 unmodified lines | ||
| 250 | 223 | static int checkvar (lua_State *L) { |
| 251 | 224 | const char *name = lua_tostring(L, 2); |
| 252 | 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 | 227 | return 0; |
| 255 | 228 | } |
| 256 | 229 | |
| 257 | 230 | |
| 258 | 231 | #define clearinteractive(i) (*i &= 2) |
| 259 | 232 | |
| 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) { | |
| 261 | 234 | if (argv[1] == NULL) { /* no arguments? */ |
| 262 | 235 | *interactive = 0; |
| 263 | − if (stdin_is_tty()) | |
| 236 | + if (lua_stdin_is_tty()) | |
| 264 | 237 | dotty(L); |
| 265 | 238 | else |
| 266 | 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 | 304 | } endloop: |
| 332 | 305 | if (argv[i] != NULL) { |
| 333 | − const char *filename = argv[i]; | |
| 334 | − int narg = getargs(L, argv, i); /* collect arguments */ | |
| 335 | 306 | int status; |
| 307 | + const char *filename = argv[i]; | |
| 308 | + int narg = getargs(L, argc, argv, i); /* collect arguments */ | |
| 336 | 309 | lua_setglobal(L, "arg"); |
| 337 | 310 | clearinteractive(interactive); |
| 338 | 311 | status = luaL_loadfile(L, filename); |
| @@ -371,10 +344,10 @@ static int pmain (lua_State *L) { · 32 unmodified lines | ||
| 371 | 344 | int interactive = 1; |
| 372 | 345 | if (s->argv[0] && s->argv[0][0]) progname = s->argv[0]; |
| 373 | 346 | globalL = L; |
| 374 | − luaopen_stdlibs(L); /* open libraries */ | |
| 347 | + luaL_openlibs(L); /* open libraries */ | |
| 375 | 348 | status = handle_luainit(L); |
| 376 | 349 | if (status == 0) { |
| 377 | − status = handle_argv(L, s->argv, &interactive); | |
| 350 | + status = handle_argv(L, s->argc, s->argv, &interactive); | |
| 378 | 351 | if (status == 0 && interactive) dotty(L); |
| 379 | 352 | } |
| 380 | 353 | s->status = status; |
| @@ -382,7 +355,7 @@ static int pmain (lua_State *L) { · 1 unmodified line | ||
| 382 | 355 | } |
| 383 | 356 | |
| 384 | 357 | |
| 385 | −int main (int argc, char *argv[]) { | |
| 358 | +int main (int argc, char **argv) { | |
| 386 | 359 | int status; |
| 387 | 360 | struct Smain s; |
| 388 | 361 | lua_State *L = lua_open(); /* create state */ |