diff options
| author | Roberto Ierusalimschy <roberto@inf.puc-rio.br> | 2000-03-20 17:27:32 -0300 |
|---|---|---|
| committer | Roberto Ierusalimschy <roberto@inf.puc-rio.br> | 2000-03-20 17:27:32 -0300 |
| commit | 441469f8031adb24322332e9d9653b794eefff5c (patch) | |
| tree | 211756522f0db476b3313cb5b78a11df0af23354 | |
| parent | 1157c2a7e43f690ec470b70a07dd142a93b7a412 (diff) | |
| download | lua-441469f8031adb24322332e9d9653b794eefff5c.tar.gz lua-441469f8031adb24322332e9d9653b794eefff5c.tar.bz2 lua-441469f8031adb24322332e9d9653b794eefff5c.zip | |
new option `-s' + argv ends with NULL (so we don't need argc).
| -rw-r--r-- | lua.c | 81 |
1 files changed, 48 insertions, 33 deletions
| @@ -1,5 +1,5 @@ | |||
| 1 | /* | 1 | /* |
| 2 | ** $Id: lua.c,v 1.33 2000/02/21 18:30:42 roberto Exp roberto $ | 2 | ** $Id: lua.c,v 1.34 2000/03/03 14:58:26 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 | */ |
| @@ -74,6 +74,7 @@ static void print_message (void) { | |||
| 74 | " -f name execute file `name' with remaining arguments in table `arg'\n" | 74 | " -f name execute file `name' with remaining arguments in table `arg'\n" |
| 75 | " -i enter interactive mode with prompt\n" | 75 | " -i enter interactive mode with prompt\n" |
| 76 | " -q enter interactive mode without prompt\n" | 76 | " -q enter interactive mode without prompt\n" |
| 77 | " -sNUM set stack size to NUM (must be first option)\n" | ||
| 77 | " -v print version information\n" | 78 | " -v print version information\n" |
| 78 | " a=b set global `a' to string `b'\n" | 79 | " a=b set global `a' to string `b'\n" |
| 79 | " name execute file `name'\n" | 80 | " name execute file `name'\n" |
| @@ -94,38 +95,39 @@ static void assign (char *arg) { | |||
| 94 | } | 95 | } |
| 95 | 96 | ||
| 96 | 97 | ||
| 97 | static void getargs (int argc, char *argv[]) { | 98 | static void getargs (char *argv[]) { |
| 98 | lua_beginblock(); { | 99 | lua_beginblock(); { |
| 99 | int i; | 100 | int i; |
| 100 | lua_Object args = lua_createtable(); | 101 | lua_Object args = lua_createtable(); |
| 101 | lua_pushobject(args); | 102 | lua_pushobject(args); |
| 102 | lua_setglobal("arg"); | 103 | lua_setglobal("arg"); |
| 103 | for (i=0; i<argc; i++) { | 104 | for (i=0; argv[i]; i++) { |
| 104 | /* arg[i] = argv[i] */ | 105 | /* arg[i] = argv[i] */ |
| 105 | lua_pushobject(args); lua_pushnumber(i); | 106 | lua_pushobject(args); lua_pushnumber(i); |
| 106 | lua_pushstring(argv[i]); lua_settable(); | 107 | lua_pushstring(argv[i]); lua_settable(); |
| 107 | } | 108 | } |
| 108 | /* arg.n = maximum index in table `arg' */ | 109 | /* arg.n = maximum index in table `arg' */ |
| 109 | lua_pushobject(args); lua_pushstring("n"); | 110 | lua_pushobject(args); lua_pushstring("n"); |
| 110 | lua_pushnumber(argc-1); lua_settable(); | 111 | lua_pushnumber(i-1); lua_settable(); |
| 111 | } lua_endblock(); | 112 | } lua_endblock(); |
| 112 | } | 113 | } |
| 113 | 114 | ||
| 114 | 115 | ||
| 115 | static void file_input (char **argv, int arg) { | 116 | static void file_input (const char *argv) { |
| 116 | int result = ldo(lua_dofile, argv[arg]); | 117 | int result = ldo(lua_dofile, argv); |
| 117 | if (result) { | 118 | if (result) { |
| 118 | if (result == 2) { | 119 | if (result == 2) { |
| 119 | fprintf(stderr, "lua: cannot execute file "); | 120 | fprintf(stderr, "lua: cannot execute file "); |
| 120 | perror(argv[arg]); | 121 | perror(argv); |
| 121 | } | 122 | } |
| 122 | exit(1); | 123 | exit(1); |
| 123 | } | 124 | } |
| 124 | } | 125 | } |
| 125 | 126 | ||
| 126 | 127 | ||
| 127 | static void manual_input (int prompt) { | 128 | static void manual_input (int version, int prompt) { |
| 128 | int cont = 1; | 129 | int cont = 1; |
| 130 | if (version) print_version(); | ||
| 129 | while (cont) { | 131 | while (cont) { |
| 130 | char buffer[BUFSIZ]; | 132 | char buffer[BUFSIZ]; |
| 131 | int i = 0; | 133 | int i = 0; |
| @@ -161,70 +163,83 @@ static void manual_input (int prompt) { | |||
| 161 | 163 | ||
| 162 | 164 | ||
| 163 | int main (int argc, char *argv[]) { | 165 | int main (int argc, char *argv[]) { |
| 164 | int i; | 166 | (void)argc; /* unused */ |
| 165 | lua_state = lua_newstate("stack", 1024, "builtin", 1, NULL); | 167 | argv++; /* skip program name */ |
| 168 | if (*argv && (*argv)[0] == '-' && (*argv)[1] == 's') { | ||
| 169 | int stacksize = atoi((*argv)+2); | ||
| 170 | if (stacksize == 0) { | ||
| 171 | fprintf(stderr, "lua: invalid stack size\n"); | ||
| 172 | exit(1); | ||
| 173 | } | ||
| 174 | argv++; | ||
| 175 | lua_state = lua_newstate("stack", stacksize, NULL); | ||
| 176 | } | ||
| 177 | else | ||
| 178 | lua_state = lua_newstate(NULL); | ||
| 166 | lua_userinit(); | 179 | lua_userinit(); |
| 167 | if (argc < 2) { /* no arguments? */ | 180 | if (*argv == NULL) { /* no other arguments? */ |
| 168 | if (isatty(0)) { | 181 | if (isatty(0)) { |
| 169 | print_version(); | 182 | manual_input(1, 1); |
| 170 | manual_input(1); | ||
| 171 | } | 183 | } |
| 172 | else | 184 | else |
| 173 | ldo(lua_dofile, NULL); /* executes stdin as a file */ | 185 | ldo(lua_dofile, NULL); /* executes stdin as a file */ |
| 174 | } | 186 | } |
| 175 | else for (i=1; i<argc; i++) { | 187 | else for (; *argv; argv++) { |
| 176 | if (argv[i][0] == '-') { /* option? */ | 188 | if ((*argv)[0] == '-') { /* option? */ |
| 177 | switch (argv[i][1]) { | 189 | switch ((*argv)[1]) { |
| 178 | case 0: | 190 | case 0: |
| 179 | ldo(lua_dofile, NULL); /* executes stdin as a file */ | 191 | ldo(lua_dofile, NULL); /* executes stdin as a file */ |
| 180 | break; | 192 | break; |
| 181 | case 'i': | 193 | case 'i': |
| 182 | manual_input(1); | 194 | manual_input(0, 1); |
| 183 | break; | 195 | break; |
| 184 | case 'q': | 196 | case 'q': |
| 185 | manual_input(0); | 197 | manual_input(0, 0); |
| 186 | break; | 198 | break; |
| 187 | case 'd': | 199 | case 'd': |
| 188 | lua_setdebug(lua_state, 1); | 200 | lua_setdebug(lua_state, 1); |
| 189 | if (i==argc-1) { /* last argument? */ | 201 | if (*(argv+1) == NULL) { /* last argument? */ |
| 190 | print_version(); | 202 | manual_input(1, 1); |
| 191 | manual_input(1); | ||
| 192 | } | 203 | } |
| 193 | break; | 204 | break; |
| 194 | case 'v': | 205 | case 'v': |
| 195 | print_version(); | 206 | print_version(); |
| 196 | break; | 207 | break; |
| 197 | case 'e': | 208 | case 'e': |
| 198 | i++; | 209 | argv++; |
| 199 | if (i>=argc) { | 210 | if (*argv == NULL) { |
| 200 | print_message(); | 211 | print_message(); |
| 201 | exit(1); | 212 | exit(1); |
| 202 | } | 213 | } |
| 203 | if (ldo(lua_dostring, argv[i]) != 0) { | 214 | if (ldo(lua_dostring, *argv) != 0) { |
| 204 | fprintf(stderr, "lua: error running argument `%s'\n", argv[i]); | 215 | fprintf(stderr, "lua: error running argument `%s'\n", *argv); |
| 205 | exit(1); | 216 | exit(1); |
| 206 | } | 217 | } |
| 207 | break; | 218 | break; |
| 208 | case 'f': | 219 | case 'f': |
| 209 | i++; | 220 | argv++; |
| 210 | if (i>=argc) { | 221 | if (*argv == NULL) { |
| 211 | print_message(); | 222 | print_message(); |
| 212 | exit(1); | 223 | exit(1); |
| 213 | } | 224 | } |
| 214 | getargs(argc-i, argv+i); /* collect remaining arguments */ | 225 | getargs(argv); /* collect remaining arguments */ |
| 215 | file_input(argv, i); | 226 | file_input(*argv); |
| 216 | i = argc; /* stop scanning arguments */ | 227 | goto endloop; /* stop scanning arguments */ |
| 217 | break; | 228 | break; |
| 229 | case 's': | ||
| 230 | fprintf(stderr, "lua: stack size (`-s') must be the first option\n"); | ||
| 231 | exit(1); | ||
| 218 | default: | 232 | default: |
| 219 | print_message(); | 233 | print_message(); |
| 220 | exit(1); | 234 | exit(1); |
| 221 | } | 235 | } |
| 222 | } | 236 | } |
| 223 | else if (strchr(argv[i], '=')) | 237 | else if (strchr(*argv, '=')) |
| 224 | assign(argv[i]); | 238 | assign(*argv); |
| 225 | else | 239 | else |
| 226 | file_input(argv, i); | 240 | file_input(*argv); |
| 227 | } | 241 | } |
| 242 | endloop: | ||
| 228 | #ifdef DEBUG | 243 | #ifdef DEBUG |
| 229 | lua_close(); | 244 | lua_close(); |
| 230 | #endif | 245 | #endif |
