diff options
| author | Roberto Ierusalimschy <roberto@inf.puc-rio.br> | 1997-12-19 16:34:23 -0200 |
|---|---|---|
| committer | Roberto Ierusalimschy <roberto@inf.puc-rio.br> | 1997-12-19 16:34:23 -0200 |
| commit | 7ecc3ce82700b931be0a2cc4a2feacb674c9854f (patch) | |
| tree | f5c366bb8dc66894cb5a9fd4fb181dcde552f224 | |
| parent | 4e91384e14bd8d28c7871ea0c2fb339520290f99 (diff) | |
| download | lua-7ecc3ce82700b931be0a2cc4a2feacb674c9854f.tar.gz lua-7ecc3ce82700b931be0a2cc4a2feacb674c9854f.tar.bz2 lua-7ecc3ce82700b931be0a2cc4a2feacb674c9854f.zip | |
new command line options
| -rw-r--r-- | lua.c | 81 |
1 files changed, 47 insertions, 34 deletions
| @@ -1,5 +1,5 @@ | |||
| 1 | /* | 1 | /* |
| 2 | ** $Id: lua.c,v 1.8 1997/12/11 14:48:46 roberto Exp roberto $ | 2 | ** $Id: lua.c,v 1.9 1997/12/11 17:00:21 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 | */ |
| @@ -25,6 +25,17 @@ | |||
| 25 | #define isatty(x) (x==0) /* assume stdin is a tty */ | 25 | #define isatty(x) (x==0) /* assume stdin is a tty */ |
| 26 | #endif | 26 | #endif |
| 27 | 27 | ||
| 28 | /* command line options: | ||
| 29 | ** -v print version information (banner). | ||
| 30 | ** -d debug on | ||
| 31 | ** -e dostring on next arg | ||
| 32 | ** a=b sets global 'a' with string 'b' | ||
| 33 | ** -q interactive mode without prompt | ||
| 34 | ** -i interactive mode with prompt | ||
| 35 | ** - executes stdin as a file | ||
| 36 | ** name dofile "name" | ||
| 37 | */ | ||
| 38 | |||
| 28 | 39 | ||
| 29 | static void assign (char *arg) | 40 | static void assign (char *arg) |
| 30 | { | 41 | { |
| @@ -44,39 +55,35 @@ static void assign (char *arg) | |||
| 44 | 55 | ||
| 45 | static void manual_input (int prompt) | 56 | static void manual_input (int prompt) |
| 46 | { | 57 | { |
| 47 | if (isatty(0)) { | 58 | int cont = 1; |
| 48 | int cont = 1; | 59 | while (cont) { |
| 49 | while (cont) { | 60 | char buffer[BUF_SIZE]; |
| 50 | char buffer[BUF_SIZE]; | 61 | int i = 0; |
| 51 | int i = 0; | 62 | lua_beginblock(); |
| 52 | lua_beginblock(); | 63 | if (prompt) |
| 53 | if (prompt) | 64 | printf("%s", lua_getstring(lua_getglobal("_PROMPT"))); |
| 54 | printf("%s", lua_getstring(lua_getglobal("_PROMPT"))); | 65 | for(;;) { |
| 55 | for(;;) { | 66 | int c = getchar(); |
| 56 | int c = getchar(); | 67 | if (c == EOF) { |
| 57 | if (c == EOF) { | 68 | cont = 0; |
| 58 | cont = 0; | 69 | break; |
| 59 | break; | ||
| 60 | } | ||
| 61 | else if (c == '\n') { | ||
| 62 | if (i>0 && buffer[i-1] == '\\') | ||
| 63 | buffer[i-1] = '\n'; | ||
| 64 | else break; | ||
| 65 | } | ||
| 66 | else if (i >= BUF_SIZE-1) { | ||
| 67 | fprintf(stderr, "lua: argument line too long\n"); | ||
| 68 | break; | ||
| 69 | } | ||
| 70 | else buffer[i++] = c; | ||
| 71 | } | 70 | } |
| 72 | buffer[i] = 0; | 71 | else if (c == '\n') { |
| 73 | lua_dostring(buffer); | 72 | if (i>0 && buffer[i-1] == '\\') |
| 74 | lua_endblock(); | 73 | buffer[i-1] = '\n'; |
| 74 | else break; | ||
| 75 | } | ||
| 76 | else if (i >= BUF_SIZE-1) { | ||
| 77 | fprintf(stderr, "lua: argument line too long\n"); | ||
| 78 | break; | ||
| 79 | } | ||
| 80 | else buffer[i++] = c; | ||
| 75 | } | 81 | } |
| 76 | printf("\n"); | 82 | buffer[i] = 0; |
| 83 | lua_dostring(buffer); | ||
| 84 | lua_endblock(); | ||
| 77 | } | 85 | } |
| 78 | else | 86 | printf("\n"); |
| 79 | lua_dofile(NULL); /* executes stdin as a file */ | ||
| 80 | } | 87 | } |
| 81 | 88 | ||
| 82 | 89 | ||
| @@ -88,12 +95,18 @@ int main (int argc, char *argv[]) | |||
| 88 | lua_strlibopen(); | 95 | lua_strlibopen(); |
| 89 | lua_mathlibopen(); | 96 | lua_mathlibopen(); |
| 90 | lua_pushstring("> "); lua_setglobal("_PROMPT"); | 97 | lua_pushstring("> "); lua_setglobal("_PROMPT"); |
| 91 | if (argc < 2) { | 98 | if (argc < 2) { /* no arguments? */ |
| 92 | printf("%s %s\n", LUA_VERSION, LUA_COPYRIGHT); | 99 | if (isatty(0)) { |
| 93 | manual_input(1); | 100 | printf("%s %s\n", LUA_VERSION, LUA_COPYRIGHT); |
| 101 | manual_input(1); | ||
| 102 | } | ||
| 103 | else | ||
| 104 | lua_dofile(NULL); /* executes stdin as a file */ | ||
| 94 | } | 105 | } |
| 95 | else for (i=1; i<argc; i++) { | 106 | else for (i=1; i<argc; i++) { |
| 96 | if (strcmp(argv[i], "-") == 0) | 107 | if (strcmp(argv[i], "-") == 0) |
| 108 | lua_dofile(NULL); /* executes stdin as a file */ | ||
| 109 | else if (strcmp(argv[i], "-i") == 0) | ||
| 97 | manual_input(1); | 110 | manual_input(1); |
| 98 | else if (strcmp(argv[i], "-q") == 0) | 111 | else if (strcmp(argv[i], "-q") == 0) |
| 99 | manual_input(0); | 112 | manual_input(0); |
