diff options
| author | Roberto Ierusalimschy <roberto@inf.puc-rio.br> | 2000-04-14 14:46:29 -0300 |
|---|---|---|
| committer | Roberto Ierusalimschy <roberto@inf.puc-rio.br> | 2000-04-14 14:46:29 -0300 |
| commit | 0b56646bafa5e83a689c42313d9d1e598ad7e591 (patch) | |
| tree | 0e74b9400a4d710e400b47756b45496c00033974 /lua.c | |
| parent | 634c3d57e924f36812ccc5798d91236ae819c6d1 (diff) | |
| download | lua-0b56646bafa5e83a689c42313d9d1e598ad7e591.tar.gz lua-0b56646bafa5e83a689c42313d9d1e598ad7e591.tar.bz2 lua-0b56646bafa5e83a689c42313d9d1e598ad7e591.zip | |
new function `getargs'
Diffstat (limited to '')
| -rw-r--r-- | lua.c | 64 |
1 files changed, 35 insertions, 29 deletions
| @@ -1,5 +1,5 @@ | |||
| 1 | /* | 1 | /* |
| 2 | ** $Id: lua.c,v 1.35 2000/03/20 20:27:32 roberto Exp roberto $ | 2 | ** $Id: lua.c,v 1.36 2000/03/30 17:19:48 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 | */ |
| @@ -95,12 +95,9 @@ static void assign (char *arg) { | |||
| 95 | } | 95 | } |
| 96 | 96 | ||
| 97 | 97 | ||
| 98 | static void getargs (char *argv[]) { | 98 | static lua_Object getargs (char *argv[]) { |
| 99 | lua_beginblock(); { | ||
| 100 | int i; | ||
| 101 | lua_Object args = lua_createtable(); | 99 | lua_Object args = lua_createtable(); |
| 102 | lua_pushobject(args); | 100 | int i; |
| 103 | lua_setglobal("arg"); | ||
| 104 | for (i=0; argv[i]; i++) { | 101 | for (i=0; argv[i]; i++) { |
| 105 | /* arg[i] = argv[i] */ | 102 | /* arg[i] = argv[i] */ |
| 106 | lua_pushobject(args); lua_pushnumber(i); | 103 | lua_pushobject(args); lua_pushnumber(i); |
| @@ -109,7 +106,13 @@ static void getargs (char *argv[]) { | |||
| 109 | /* arg.n = maximum index in table `arg' */ | 106 | /* arg.n = maximum index in table `arg' */ |
| 110 | lua_pushobject(args); lua_pushstring("n"); | 107 | lua_pushobject(args); lua_pushstring("n"); |
| 111 | lua_pushnumber(i-1); lua_settable(); | 108 | lua_pushnumber(i-1); lua_settable(); |
| 112 | } lua_endblock(); | 109 | return args; |
| 110 | } | ||
| 111 | |||
| 112 | |||
| 113 | static void l_getargs (void) { | ||
| 114 | char **argv = (char **)lua_getuserdata(lua_getparam(1)); | ||
| 115 | lua_pushobject(getargs(argv)); | ||
| 113 | } | 116 | } |
| 114 | 117 | ||
| 115 | 118 | ||
| @@ -163,30 +166,32 @@ static void manual_input (int version, int prompt) { | |||
| 163 | 166 | ||
| 164 | 167 | ||
| 165 | int main (int argc, char *argv[]) { | 168 | int main (int argc, char *argv[]) { |
| 166 | (void)argc; /* unused */ | 169 | int i = 1; |
| 167 | argv++; /* skip program name */ | 170 | if (i < argc && argv[1][0] == '-' && argv[1][1] == 's') { |
| 168 | if (*argv && (*argv)[0] == '-' && (*argv)[1] == 's') { | 171 | int stacksize = atoi(&argv[1][2]); |
| 169 | int stacksize = atoi((*argv)+2); | ||
| 170 | if (stacksize == 0) { | 172 | if (stacksize == 0) { |
| 171 | fprintf(stderr, "lua: invalid stack size\n"); | 173 | fprintf(stderr, "lua: invalid stack size ('%s')\n", &argv[1][2]); |
| 172 | exit(1); | 174 | exit(1); |
| 173 | } | 175 | } |
| 174 | argv++; | 176 | i++; |
| 175 | lua_state = lua_newstate("stack", stacksize, NULL); | 177 | lua_state = lua_newstate("stack", stacksize, NULL); |
| 176 | } | 178 | } |
| 177 | else | 179 | else |
| 178 | lua_state = lua_newstate(NULL); | 180 | lua_state = lua_newstate(NULL); |
| 179 | lua_userinit(); | 181 | lua_userinit(); |
| 180 | if (*argv == NULL) { /* no other arguments? */ | 182 | lua_pushuserdata(argv); |
| 183 | lua_pushcclosure(l_getargs, 1); | ||
| 184 | lua_setglobal("getargs"); | ||
| 185 | if (i >= argc) { /* no other arguments? */ | ||
| 181 | if (isatty(0)) { | 186 | if (isatty(0)) { |
| 182 | manual_input(1, 1); | 187 | manual_input(1, 1); |
| 183 | } | 188 | } |
| 184 | else | 189 | else |
| 185 | ldo(lua_dofile, NULL); /* executes stdin as a file */ | 190 | ldo(lua_dofile, NULL); /* executes stdin as a file */ |
| 186 | } | 191 | } |
| 187 | else for (; *argv; argv++) { | 192 | else for (; i<argc; i++) { |
| 188 | if ((*argv)[0] == '-') { /* option? */ | 193 | if (argv[i][0] == '-') { /* option? */ |
| 189 | switch ((*argv)[1]) { | 194 | switch (argv[i][1]) { |
| 190 | case 0: | 195 | case 0: |
| 191 | ldo(lua_dofile, NULL); /* executes stdin as a file */ | 196 | ldo(lua_dofile, NULL); /* executes stdin as a file */ |
| 192 | break; | 197 | break; |
| @@ -198,7 +203,7 @@ int main (int argc, char *argv[]) { | |||
| 198 | break; | 203 | break; |
| 199 | case 'd': | 204 | case 'd': |
| 200 | lua_setdebug(lua_state, 1); | 205 | lua_setdebug(lua_state, 1); |
| 201 | if (*(argv+1) == NULL) { /* last argument? */ | 206 | if (i+1 >= argc) { /* last argument? */ |
| 202 | manual_input(1, 1); | 207 | manual_input(1, 1); |
| 203 | } | 208 | } |
| 204 | break; | 209 | break; |
| @@ -206,24 +211,25 @@ int main (int argc, char *argv[]) { | |||
| 206 | print_version(); | 211 | print_version(); |
| 207 | break; | 212 | break; |
| 208 | case 'e': | 213 | case 'e': |
| 209 | argv++; | 214 | i++; |
| 210 | if (*argv == NULL) { | 215 | if (i >= argc) { |
| 211 | print_message(); | 216 | print_message(); |
| 212 | exit(1); | 217 | exit(1); |
| 213 | } | 218 | } |
| 214 | if (ldo(lua_dostring, *argv) != 0) { | 219 | if (ldo(lua_dostring, argv[i]) != 0) { |
| 215 | fprintf(stderr, "lua: error running argument `%s'\n", *argv); | 220 | fprintf(stderr, "lua: error running argument `%s'\n", argv[i]); |
| 216 | exit(1); | 221 | exit(1); |
| 217 | } | 222 | } |
| 218 | break; | 223 | break; |
| 219 | case 'f': | 224 | case 'f': |
| 220 | argv++; | 225 | i++; |
| 221 | if (*argv == NULL) { | 226 | if (i >= argc) { |
| 222 | print_message(); | 227 | print_message(); |
| 223 | exit(1); | 228 | exit(1); |
| 224 | } | 229 | } |
| 225 | getargs(argv); /* collect remaining arguments */ | 230 | lua_pushobject(getargs(argv+i)); /* collect remaining arguments */ |
| 226 | file_input(*argv); | 231 | lua_setglobal("arg"); |
| 232 | file_input(argv[i]); | ||
| 227 | goto endloop; /* stop scanning arguments */ | 233 | goto endloop; /* stop scanning arguments */ |
| 228 | break; | 234 | break; |
| 229 | case 's': | 235 | case 's': |
| @@ -234,10 +240,10 @@ int main (int argc, char *argv[]) { | |||
| 234 | exit(1); | 240 | exit(1); |
| 235 | } | 241 | } |
| 236 | } | 242 | } |
| 237 | else if (strchr(*argv, '=')) | 243 | else if (strchr(argv[i], '=')) |
| 238 | assign(*argv); | 244 | assign(argv[i]); |
| 239 | else | 245 | else |
| 240 | file_input(*argv); | 246 | file_input(argv[i]); |
| 241 | } | 247 | } |
| 242 | endloop: | 248 | endloop: |
| 243 | #ifdef DEBUG | 249 | #ifdef DEBUG |
