** Main body of stand-alone interpreter (to be called in protected mode). ** Reads the options and handles them all. */
| 702 | ** Reads the options and handles them all. |
| 703 | */ |
| 704 | static int pmain (lua_State *L) { |
| 705 | int argc = (int)lua_tointeger(L, 1); |
| 706 | char **argv = (char **)lua_touserdata(L, 2); |
| 707 | int script; |
| 708 | int args = collectargs(argv, &script); |
| 709 | int optlim = (script > 0) ? script : argc; /* first argv not an option */ |
| 710 | luaL_checkversion(L); /* check that interpreter has correct version */ |
| 711 | if (args == has_error) { /* bad arg? */ |
| 712 | print_usage(argv[script]); /* 'script' has index of bad arg. */ |
| 713 | return 0; |
| 714 | } |
| 715 | if (args & has_v) /* option '-v'? */ |
| 716 | print_version(); |
| 717 | if (args & has_E) { /* option '-E'? */ |
| 718 | lua_pushboolean(L, 1); /* signal for libraries to ignore env. vars. */ |
| 719 | lua_setfield(L, LUA_REGISTRYINDEX, "LUA_NOENV"); |
| 720 | } |
| 721 | luai_openlibs(L); /* open standard libraries */ |
| 722 | createargtable(L, argv, argc, script); /* create table 'arg' */ |
| 723 | lua_gc(L, LUA_GCRESTART); /* start GC... */ |
| 724 | lua_gc(L, LUA_GCGEN); /* ...in generational mode */ |
| 725 | if (!(args & has_E)) { /* no option '-E'? */ |
| 726 | if (handle_luainit(L) != LUA_OK) /* run LUA_INIT */ |
| 727 | return 0; /* error running LUA_INIT */ |
| 728 | } |
| 729 | if (!runargs(L, argv, optlim)) /* execute arguments -e, -l, and -W */ |
| 730 | return 0; /* something failed */ |
| 731 | if (script > 0) { /* execute main script (if there is one) */ |
| 732 | if (handle_script(L, argv + script) != LUA_OK) |
| 733 | return 0; /* interrupt in case of error */ |
| 734 | } |
| 735 | if (args & has_i) /* -i option? */ |
| 736 | doREPL(L); /* do read-eval-print loop */ |
| 737 | else if (script < 1 && !(args & (has_e | has_v))) { /* no active option? */ |
| 738 | if (lua_stdin_is_tty()) { /* running in interactive mode? */ |
| 739 | print_version(); |
| 740 | doREPL(L); /* do read-eval-print loop */ |
| 741 | } |
| 742 | else dofile(L, NULL); /* executes stdin as a file */ |
| 743 | } |
| 744 | lua_pushboolean(L, 1); /* signal no errors */ |
| 745 | return 1; |
| 746 | } |
| 747 | |
| 748 | |
| 749 | int main (int argc, char **argv) { |
nothing calls this directly
no test coverage detected