diff options
author | Roberto Ierusalimschy <roberto@inf.puc-rio.br> | 2005-03-21 15:12:07 -0300 |
---|---|---|
committer | Roberto Ierusalimschy <roberto@inf.puc-rio.br> | 2005-03-21 15:12:07 -0300 |
commit | 6d475731cae37e8e46abf217697198bcce316b42 (patch) | |
tree | 12ac3d08eac911d83a4b09761ac9f578fb279616 | |
parent | f41fc0eb0e7e92d04c9df206b8f185792804bf6a (diff) | |
download | lua-6d475731cae37e8e46abf217697198bcce316b42.tar.gz lua-6d475731cae37e8e46abf217697198bcce316b42.tar.bz2 lua-6d475731cae37e8e46abf217697198bcce316b42.zip |
cleaner configuration for lua.c
-rw-r--r-- | lua.c | 75 | ||||
-rw-r--r-- | luaconf.h | 42 |
2 files changed, 61 insertions, 56 deletions
@@ -1,5 +1,5 @@ | |||
1 | /* | 1 | /* |
2 | ** $Id: lua.c,v 1.134 2005/01/10 16:30:59 roberto Exp roberto $ | 2 | ** $Id: lua.c,v 1.135 2005/01/10 17:21:10 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 | */ |
@@ -30,7 +30,7 @@ | |||
30 | 30 | ||
31 | static lua_State *globalL = NULL; | 31 | static lua_State *globalL = NULL; |
32 | 32 | ||
33 | static const char *progname = PROGNAME; | 33 | static const char *progname = LUA_PROGNAME; |
34 | 34 | ||
35 | 35 | ||
36 | 36 | ||
@@ -150,43 +150,12 @@ static int dolibrary (lua_State *L, const char *name) { | |||
150 | } | 150 | } |
151 | 151 | ||
152 | 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 | } | ||
179 | } | ||
180 | |||
181 | #endif | ||
182 | |||
183 | |||
184 | static const char *get_prompt (lua_State *L, int firstline) { | 153 | static const char *get_prompt (lua_State *L, int firstline) { |
185 | const char *p = NULL; | 154 | const char *p; |
186 | lua_pushstring(L, firstline ? "_PROMPT" : "_PROMPT2"); | 155 | lua_pushstring(L, firstline ? "_PROMPT" : "_PROMPT2"); |
187 | lua_rawget(L, LUA_GLOBALSINDEX); | 156 | lua_rawget(L, LUA_GLOBALSINDEX); |
188 | p = lua_tostring(L, -1); | 157 | p = lua_tostring(L, -1); |
189 | if (p == NULL) p = (firstline ? PROMPT : PROMPT2); | 158 | if (p == NULL) p = (firstline ? LUA_PROMPT : LUA_PROMPT2); |
190 | lua_pop(L, 1); /* remove global */ | 159 | lua_pop(L, 1); /* remove global */ |
191 | return p; | 160 | return p; |
192 | } | 161 | } |
@@ -203,23 +172,39 @@ static int incomplete (lua_State *L, int status) { | |||
203 | } | 172 | } |
204 | 173 | ||
205 | 174 | ||
175 | static int pushline (lua_State *L, int firstline) { | ||
176 | char buffer[LUA_MAXINPUT]; | ||
177 | char *b = buffer; | ||
178 | size_t l; | ||
179 | const char *prmt = get_prompt(L, firstline); | ||
180 | if (lua_readline(L, b, prmt) == 0) | ||
181 | return 0; /* no input */ | ||
182 | l = strlen(b); | ||
183 | if (l > 0 && b[l-1] == '\n') /* line ends with newline? */ | ||
184 | b[l-1] = '\0'; /* remove it */ | ||
185 | if (firstline && b[0] == '=') /* first line starts with `=' ? */ | ||
186 | lua_pushfstring(L, "return %s", b+1); /* change it to `return' */ | ||
187 | else | ||
188 | lua_pushstring(L, b); | ||
189 | return 1; | ||
190 | } | ||
191 | |||
192 | |||
206 | static int loadline (lua_State *L) { | 193 | static int loadline (lua_State *L) { |
207 | int status; | 194 | int status; |
208 | lua_settop(L, 0); | 195 | lua_settop(L, 0); |
209 | if (lua_readline(L, get_prompt(L, 1)) == 0) /* no input? */ | 196 | if (!pushline(L, 1)) |
210 | return -1; | 197 | return -1; /* no input */ |
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 | } | ||
215 | for (;;) { /* repeat until gets a complete line */ | 198 | for (;;) { /* repeat until gets a complete line */ |
216 | status = luaL_loadbuffer(L, lua_tostring(L, 1), lua_strlen(L, 1), "=stdin"); | 199 | status = luaL_loadbuffer(L, lua_tostring(L, 1), lua_strlen(L, 1), "=stdin"); |
217 | if (!incomplete(L, status)) break; /* cannot try to add lines? */ | 200 | if (!incomplete(L, status)) break; /* cannot try to add lines? */ |
218 | if (lua_readline(L, get_prompt(L, 0)) == 0) /* no more input? */ | 201 | if (!pushline(L, 0)) /* no more input? */ |
219 | return -1; | 202 | return -1; |
220 | lua_concat(L, lua_gettop(L)); /* join lines */ | 203 | lua_pushliteral(L, "\n"); /* add a new line... */ |
204 | lua_insert(L, -2); /* ...between the two lines */ | ||
205 | lua_concat(L, 3); /* join them */ | ||
221 | } | 206 | } |
222 | lua_saveline(L, lua_tostring(L, 1)); | 207 | lua_saveline(L, 1); |
223 | lua_remove(L, 1); /* remove line */ | 208 | lua_remove(L, 1); /* remove line */ |
224 | return status; | 209 | return status; |
225 | } | 210 | } |
@@ -260,7 +245,7 @@ static int checkvar (lua_State *L) { | |||
260 | static int handle_argv (lua_State *L, char *argv[], int *interactive) { | 245 | static int handle_argv (lua_State *L, char *argv[], int *interactive) { |
261 | if (argv[1] == NULL) { /* no arguments? */ | 246 | if (argv[1] == NULL) { /* no arguments? */ |
262 | *interactive = 0; | 247 | *interactive = 0; |
263 | if (stdin_is_tty()) | 248 | if (lua_stdin_is_tty()) |
264 | dotty(L); | 249 | dotty(L); |
265 | else | 250 | else |
266 | dofile(L, NULL); /* executes stdin as a file */ | 251 | dofile(L, NULL); /* executes stdin as a file */ |
@@ -1,5 +1,5 @@ | |||
1 | /* | 1 | /* |
2 | ** $Id: luaconf.h,v 1.36 2005/03/18 18:02:04 roberto Exp roberto $ | 2 | ** $Id: luaconf.h,v 1.37 2005/03/18 18:55:45 roberto Exp roberto $ |
3 | ** Configuration file for Lua | 3 | ** Configuration file for Lua |
4 | ** See Copyright Notice in lua.h | 4 | ** See Copyright Notice in lua.h |
5 | */ | 5 | */ |
@@ -133,28 +133,48 @@ | |||
133 | /* CONFIG: definition of isatty */ | 133 | /* CONFIG: definition of isatty */ |
134 | #ifdef _POSIX_C_SOURCE | 134 | #ifdef _POSIX_C_SOURCE |
135 | #include <unistd.h> | 135 | #include <unistd.h> |
136 | #define stdin_is_tty() isatty(0) | 136 | #define lua_stdin_is_tty() isatty(0) |
137 | #elif defined(_WIN32) | 137 | #elif defined(_WIN32) |
138 | #include <io.h> | 138 | #include <io.h> |
139 | #include <stdio.h> | 139 | #include <stdio.h> |
140 | #define stdin_is_tty() _isatty(_fileno(stdin)) | 140 | #define lua_stdin_is_tty() _isatty(_fileno(stdin)) |
141 | #else | 141 | #else |
142 | #define stdin_is_tty() 1 /* assume stdin is a tty */ | 142 | #define lua_stdin_is_tty() 1 /* assume stdin is a tty */ |
143 | #endif | 143 | #endif |
144 | 144 | ||
145 | 145 | ||
146 | #define PROMPT "> " | 146 | #define LUA_PROMPT "> " |
147 | #define PROMPT2 ">> " | 147 | #define LUA_PROMPT2 ">> " |
148 | #define PROGNAME "lua" | 148 | #define LUA_PROGNAME "lua" |
149 | |||
150 | 149 | ||
150 | /* | ||
151 | *@ LUA_MAXINPUT is the maximum length for an input line | ||
152 | ** CHANGE it if you need longer lines. | ||
153 | */ | ||
154 | #define LUA_MAXINPUT 512 | ||
151 | 155 | ||
152 | 156 | ||
153 | /* | 157 | /* |
154 | ** CONFIG: this macro can be used by some history system to save lines | 158 | *@ lua_readline defines how to show a prompt and then read a line from |
155 | ** read in manual input | 159 | ** the standard input. |
160 | *@ lua_saveline defines how to "save" a read line. | ||
161 | ** CHANGE them if you want to improve this functionality (e.g., using GNU | ||
162 | ** readline and history facilities). (Lua already tries to use those | ||
163 | ** facilities when it detects a GNU compiler.) | ||
156 | */ | 164 | */ |
157 | #define lua_saveline(L,line) /* empty */ | 165 | #ifdef __GNUC__ |
166 | #include <readline/readline.h> | ||
167 | #include <readline/history.h> | ||
168 | #define lua_readline(L,b,p) (((b)=readline(p)) != NULL) | ||
169 | #define lua_saveline(L,idx) \ | ||
170 | if (lua_strlen(L,idx) > 0) /* non-empty line? */ \ | ||
171 | add_history(lua_tostring(L, idx)); /* add it to history */ | ||
172 | #else | ||
173 | #define lua_readline(L,b,p) \ | ||
174 | (fputs(p, stdout), fflush(stdout), /* show prompt */ \ | ||
175 | fgets(b, LUA_MAXINPUT, stdin) != NULL) /* get line */ | ||
176 | #define lua_saveline(L,idx) ((void)0) | ||
177 | #endif | ||
158 | 178 | ||
159 | 179 | ||
160 | 180 | ||