diff options
author | Roberto Ierusalimschy <roberto@inf.puc-rio.br> | 2001-02-14 15:19:01 -0200 |
---|---|---|
committer | Roberto Ierusalimschy <roberto@inf.puc-rio.br> | 2001-02-14 15:19:01 -0200 |
commit | 29371ecfe87d546b9ffab480808640bd2ef226a1 (patch) | |
tree | 5f4089aa1a9b9035b7a8ba248ebe687c8509b435 /lua.c | |
parent | 5d9b8b7cdc64777dd1ae071b52c1816fb1faa5e7 (diff) | |
download | lua-29371ecfe87d546b9ffab480808640bd2ef226a1.tar.gz lua-29371ecfe87d546b9ffab480808640bd2ef226a1.tar.bz2 lua-29371ecfe87d546b9ffab480808640bd2ef226a1.zip |
better way to handle multi-line input (with concat)
Diffstat (limited to 'lua.c')
-rw-r--r-- | lua.c | 58 |
1 files changed, 29 insertions, 29 deletions
@@ -1,5 +1,5 @@ | |||
1 | /* | 1 | /* |
2 | ** $Id: lua.c,v 1.58 2001/01/26 11:45:51 roberto Exp roberto $ | 2 | ** $Id: lua.c,v 1.59 2001/02/06 18:18:58 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 | */ |
@@ -167,47 +167,47 @@ static int file_input (const char *argv) { | |||
167 | } | 167 | } |
168 | 168 | ||
169 | 169 | ||
170 | /* maximum length of an input string */ | 170 | /* maximum length of an input line */ |
171 | #ifndef MAXINPUT | 171 | #ifndef MAXINPUT |
172 | #define MAXINPUT BUFSIZ | 172 | #define MAXINPUT 512 |
173 | #endif | 173 | #endif |
174 | 174 | ||
175 | |||
176 | static void show_prompt (void) { | ||
177 | const char *s; | ||
178 | lua_getglobal(L, "_PROMPT"); | ||
179 | s = lua_tostring(L, -1); | ||
180 | if (!s) s = PROMPT; | ||
181 | fputs(s, stdout); | ||
182 | lua_pop(L, 1); /* remove global */ | ||
183 | } | ||
184 | |||
185 | |||
175 | static void manual_input (int version, int prompt) { | 186 | static void manual_input (int version, int prompt) { |
176 | int cont = 1; | ||
177 | if (version) print_version(); | 187 | if (version) print_version(); |
178 | while (cont) { | 188 | for (;;) { |
179 | char buffer[MAXINPUT]; | 189 | if (prompt) show_prompt(); |
180 | int i = 0; | ||
181 | if (prompt) { | ||
182 | const char *s; | ||
183 | lua_getglobal(L, "_PROMPT"); | ||
184 | s = lua_tostring(L, -1); | ||
185 | if (!s) s = PROMPT; | ||
186 | fputs(s, stdout); | ||
187 | lua_pop(L, 1); /* remove global */ | ||
188 | } | ||
189 | for(;;) { | 190 | for(;;) { |
190 | int c = getchar(); | 191 | char buffer[MAXINPUT]; |
191 | if (c == EOF) { | 192 | size_t l; |
192 | cont = 0; | 193 | if (fgets(buffer, sizeof(buffer), stdin) == NULL) { |
193 | break; | 194 | printf("\n"); |
195 | return; | ||
194 | } | 196 | } |
195 | else if (c == '\n') { | 197 | l = strlen(buffer); |
196 | if (i>0 && buffer[i-1] == '\\') | 198 | if (buffer[l-1] == '\n' && buffer[l-2] == '\\') { |
197 | buffer[i-1] = '\n'; | 199 | buffer[l-2] = '\n'; |
198 | else break; | 200 | lua_pushlstring(L, buffer, l-1); |
199 | } | 201 | } |
200 | else if (i >= MAXINPUT-1) { | 202 | else { |
201 | fprintf(stderr, "lua: input line too long\n"); | 203 | lua_pushlstring(L, buffer, l); |
202 | break; | 204 | break; |
203 | } | 205 | } |
204 | else buffer[i++] = (char)c; | ||
205 | } | 206 | } |
206 | buffer[i] = '\0'; | 207 | lua_concat(L, lua_gettop(L)); |
207 | ldo(lua_dostring, buffer); | 208 | ldo(lua_dostring, lua_tostring(L, -1)); |
208 | lua_settop(L, 0); /* remove eventual results */ | 209 | lua_settop(L, 0); /* remove eventual results */ |
209 | } | 210 | } |
210 | printf("\n"); | ||
211 | } | 211 | } |
212 | 212 | ||
213 | 213 | ||