diff options
author | Roberto Ierusalimschy <roberto@inf.puc-rio.br> | 2001-02-23 14:17:25 -0300 |
---|---|---|
committer | Roberto Ierusalimschy <roberto@inf.puc-rio.br> | 2001-02-23 14:17:25 -0300 |
commit | 39b79783297bee79db9853b63d199e120a009a8f (patch) | |
tree | c738c621c4c28d8822c2f785400786301985273b /lua.c | |
parent | d164e2294f73d8e69f00d95a66014514b2dd0ec0 (diff) | |
download | lua-39b79783297bee79db9853b63d199e120a009a8f.tar.gz lua-39b79783297bee79db9853b63d199e120a009a8f.tar.bz2 lua-39b79783297bee79db9853b63d199e120a009a8f.zip |
first (big) step to support wide chars
Diffstat (limited to 'lua.c')
-rw-r--r-- | lua.c | 106 |
1 files changed, 53 insertions, 53 deletions
@@ -1,5 +1,5 @@ | |||
1 | /* | 1 | /* |
2 | ** $Id: lua.c,v 1.60 2001/02/14 17:19:01 roberto Exp roberto $ | 2 | ** $Id: lua.c,v 1.61 2001/02/20 18:15:33 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,7 +25,7 @@ static int isatty (int x) { return x==0; } /* assume stdin is a tty */ | |||
25 | 25 | ||
26 | 26 | ||
27 | #ifndef PROMPT | 27 | #ifndef PROMPT |
28 | #define PROMPT "> " | 28 | #define PROMPT l_s("> ") |
29 | #endif | 29 | #endif |
30 | 30 | ||
31 | 31 | ||
@@ -70,7 +70,7 @@ static void lstop (void) { | |||
70 | lua_setlinehook(L, old_linehook); | 70 | lua_setlinehook(L, old_linehook); |
71 | lua_setcallhook(L, old_callhook); | 71 | lua_setcallhook(L, old_callhook); |
72 | lreset(); | 72 | lreset(); |
73 | lua_error(L, "interrupted!"); | 73 | lua_error(L, l_s("interrupted!")); |
74 | } | 74 | } |
75 | 75 | ||
76 | 76 | ||
@@ -83,7 +83,7 @@ static void laction (int i) { | |||
83 | } | 83 | } |
84 | 84 | ||
85 | 85 | ||
86 | static int ldo (int (*f)(lua_State *l, const char *), const char *name) { | 86 | static int ldo (int (*f)(lua_State *l, const l_char *), const l_char *name) { |
87 | int res; | 87 | int res; |
88 | handler h = lreset(); | 88 | handler h = lreset(); |
89 | int top = lua_gettop(L); | 89 | int top = lua_gettop(L); |
@@ -92,45 +92,45 @@ static int ldo (int (*f)(lua_State *l, const char *), const char *name) { | |||
92 | signal(SIGINT, h); /* restore old action */ | 92 | signal(SIGINT, h); /* restore old action */ |
93 | /* Lua gives no message in such cases, so lua.c provides one */ | 93 | /* Lua gives no message in such cases, so lua.c provides one */ |
94 | if (res == LUA_ERRMEM) { | 94 | if (res == LUA_ERRMEM) { |
95 | fprintf(stderr, "lua: memory allocation error\n"); | 95 | fprintf(stderr, l_s("lua: memory allocation error\n")); |
96 | } | 96 | } |
97 | else if (res == LUA_ERRERR) | 97 | else if (res == LUA_ERRERR) |
98 | fprintf(stderr, "lua: error in error message\n"); | 98 | fprintf(stderr, l_s("lua: error in error message\n")); |
99 | return res; | 99 | return res; |
100 | } | 100 | } |
101 | 101 | ||
102 | 102 | ||
103 | static void print_message (void) { | 103 | static void print_message (void) { |
104 | fprintf(stderr, | 104 | fprintf(stderr, |
105 | "usage: lua [options]. Available options are:\n" | 105 | l_s("usage: lua [options]. Available options are:\n") |
106 | " - execute stdin as a file\n" | 106 | l_s(" - execute stdin as a file\n") |
107 | " -c close Lua when exiting\n" | 107 | l_s(" -c close Lua when exiting\n") |
108 | " -e stat execute string `stat'\n" | 108 | l_s(" -e stat execute string `stat'\n") |
109 | " -f name execute file `name' with remaining arguments in table `arg'\n" | 109 | l_s(" -f name execute file `name' with remaining arguments in table `arg'\n") |
110 | " -i enter interactive mode with prompt\n" | 110 | l_s(" -i enter interactive mode with prompt\n") |
111 | " -q enter interactive mode without prompt\n" | 111 | l_s(" -q enter interactive mode without prompt\n") |
112 | " -sNUM set stack size to NUM (must be the first option)\n" | 112 | l_s(" -sNUM set stack size to NUM (must be the first option)\n") |
113 | " -v print version information\n" | 113 | l_s(" -v print version information\n") |
114 | " a=b set global `a' to string `b'\n" | 114 | l_s(" a=b set global `a' to string `b'\n") |
115 | " name execute file `name'\n" | 115 | l_s(" name execute file `name'\n") |
116 | ); | 116 | ); |
117 | } | 117 | } |
118 | 118 | ||
119 | 119 | ||
120 | static void print_version (void) { | 120 | static void print_version (void) { |
121 | printf("%.80s %.80s\n", LUA_VERSION, LUA_COPYRIGHT); | 121 | printf(l_s("%.80s %.80s\n"), l_s(LUA_VERSION), l_s(LUA_COPYRIGHT)); |
122 | } | 122 | } |
123 | 123 | ||
124 | 124 | ||
125 | static void assign (char *arg) { | 125 | static void assign (l_char *arg) { |
126 | char *eq = strchr(arg, '='); | 126 | l_char *eq = strchr(arg, l_c('=')); |
127 | *eq = '\0'; /* spilt `arg' in two strings (name & value) */ | 127 | *eq = l_c('\0'); /* spilt `arg' in two strings (name & value) */ |
128 | lua_pushstring(L, eq+1); | 128 | lua_pushstring(L, eq+1); |
129 | lua_setglobal(L, arg); | 129 | lua_setglobal(L, arg); |
130 | } | 130 | } |
131 | 131 | ||
132 | 132 | ||
133 | static void getargs (char *argv[]) { | 133 | static void getargs (l_char *argv[]) { |
134 | int i; | 134 | int i; |
135 | lua_newtable(L); | 135 | lua_newtable(L); |
136 | for (i=0; argv[i]; i++) { | 136 | for (i=0; argv[i]; i++) { |
@@ -140,24 +140,24 @@ static void getargs (char *argv[]) { | |||
140 | lua_settable(L, -3); | 140 | lua_settable(L, -3); |
141 | } | 141 | } |
142 | /* arg.n = maximum index in table `arg' */ | 142 | /* arg.n = maximum index in table `arg' */ |
143 | lua_pushliteral(L, "n"); | 143 | lua_pushliteral(L, l_s("n")); |
144 | lua_pushnumber(L, i-1); | 144 | lua_pushnumber(L, i-1); |
145 | lua_settable(L, -3); | 145 | lua_settable(L, -3); |
146 | } | 146 | } |
147 | 147 | ||
148 | 148 | ||
149 | static int l_getargs (lua_State *l) { | 149 | static int l_getargs (lua_State *l) { |
150 | char **argv = (char **)lua_touserdata(l, -1); | 150 | l_char **argv = (l_char **)lua_touserdata(l, -1); |
151 | getargs(argv); | 151 | getargs(argv); |
152 | return 1; | 152 | return 1; |
153 | } | 153 | } |
154 | 154 | ||
155 | 155 | ||
156 | static int file_input (const char *argv) { | 156 | static int file_input (const l_char *argv) { |
157 | int result = ldo(lua_dofile, argv); | 157 | int result = ldo(lua_dofile, argv); |
158 | if (result) { | 158 | if (result) { |
159 | if (result == LUA_ERRFILE) { | 159 | if (result == LUA_ERRFILE) { |
160 | fprintf(stderr, "lua: cannot execute file "); | 160 | fprintf(stderr, l_s("lua: cannot execute file ")); |
161 | perror(argv); | 161 | perror(argv); |
162 | } | 162 | } |
163 | return EXIT_FAILURE; | 163 | return EXIT_FAILURE; |
@@ -173,12 +173,12 @@ static int file_input (const char *argv) { | |||
173 | #endif | 173 | #endif |
174 | 174 | ||
175 | 175 | ||
176 | static const char *get_prompt (int prompt) { | 176 | static const l_char *get_prompt (int prompt) { |
177 | if (!prompt) | 177 | if (!prompt) |
178 | return ""; | 178 | return l_s(""); |
179 | else { | 179 | else { |
180 | const char *s; | 180 | const l_char *s; |
181 | lua_getglobal(L, "_PROMPT"); | 181 | lua_getglobal(L, l_s("_PROMPT")); |
182 | s = lua_tostring(L, -1); | 182 | s = lua_tostring(L, -1); |
183 | if (!s) s = PROMPT; | 183 | if (!s) s = PROMPT; |
184 | lua_pop(L, 1); /* remove global */ | 184 | lua_pop(L, 1); /* remove global */ |
@@ -192,15 +192,15 @@ static void manual_input (int version, int prompt) { | |||
192 | for (;;) { | 192 | for (;;) { |
193 | fputs(get_prompt(prompt), stdout); /* show prompt */ | 193 | fputs(get_prompt(prompt), stdout); /* show prompt */ |
194 | for(;;) { | 194 | for(;;) { |
195 | char buffer[MAXINPUT]; | 195 | l_char buffer[MAXINPUT]; |
196 | size_t l; | 196 | size_t l; |
197 | if (fgets(buffer, sizeof(buffer), stdin) == NULL) { | 197 | if (fgets(buffer, sizeof(buffer), stdin) == NULL) { |
198 | printf("\n"); | 198 | printf(l_s("\n")); |
199 | return; | 199 | return; |
200 | } | 200 | } |
201 | l = strlen(buffer); | 201 | l = strlen(buffer); |
202 | if (buffer[l-1] == '\n' && buffer[l-2] == '\\') { | 202 | if (buffer[l-1] == l_c('\n') && buffer[l-2] == l_c('\\')) { |
203 | buffer[l-2] = '\n'; | 203 | buffer[l-2] = l_c('\n'); |
204 | lua_pushlstring(L, buffer, l-1); | 204 | lua_pushlstring(L, buffer, l-1); |
205 | } | 205 | } |
206 | else { | 206 | else { |
@@ -215,7 +215,7 @@ static void manual_input (int version, int prompt) { | |||
215 | } | 215 | } |
216 | 216 | ||
217 | 217 | ||
218 | static int handle_argv (char *argv[], struct Options *opt) { | 218 | static int handle_argv (l_char *argv[], struct Options *opt) { |
219 | if (opt->stacksize > 0) argv++; /* skip option `-s' (if present) */ | 219 | if (opt->stacksize > 0) argv++; /* skip option `-s' (if present) */ |
220 | if (*argv == NULL) { /* no more arguments? */ | 220 | if (*argv == NULL) { /* no more arguments? */ |
221 | if (isatty(0)) { | 221 | if (isatty(0)) { |
@@ -227,8 +227,8 @@ static int handle_argv (char *argv[], struct Options *opt) { | |||
227 | else { /* other arguments; loop over them */ | 227 | else { /* other arguments; loop over them */ |
228 | int i; | 228 | int i; |
229 | for (i = 0; argv[i] != NULL; i++) { | 229 | for (i = 0; argv[i] != NULL; i++) { |
230 | if (argv[i][0] != '-') { /* not an option? */ | 230 | if (argv[i][0] != l_c('-')) { /* not an option? */ |
231 | if (strchr(argv[i], '=')) | 231 | if (strchr(argv[i], l_c('='))) |
232 | assign(argv[i]); | 232 | assign(argv[i]); |
233 | else | 233 | else |
234 | if (file_input(argv[i]) != EXIT_SUCCESS) | 234 | if (file_input(argv[i]) != EXIT_SUCCESS) |
@@ -239,46 +239,46 @@ static int handle_argv (char *argv[], struct Options *opt) { | |||
239 | ldo(lua_dofile, NULL); /* executes stdin as a file */ | 239 | ldo(lua_dofile, NULL); /* executes stdin as a file */ |
240 | break; | 240 | break; |
241 | } | 241 | } |
242 | case 'i': { | 242 | case l_c('i'): { |
243 | manual_input(0, 1); | 243 | manual_input(0, 1); |
244 | break; | 244 | break; |
245 | } | 245 | } |
246 | case 'q': { | 246 | case l_c('q'): { |
247 | manual_input(0, 0); | 247 | manual_input(0, 0); |
248 | break; | 248 | break; |
249 | } | 249 | } |
250 | case 'c': { | 250 | case l_c('c'): { |
251 | opt->toclose = 1; | 251 | opt->toclose = 1; |
252 | break; | 252 | break; |
253 | } | 253 | } |
254 | case 'v': { | 254 | case l_c('v'): { |
255 | print_version(); | 255 | print_version(); |
256 | break; | 256 | break; |
257 | } | 257 | } |
258 | case 'e': { | 258 | case l_c('e'): { |
259 | i++; | 259 | i++; |
260 | if (argv[i] == NULL) { | 260 | if (argv[i] == NULL) { |
261 | print_message(); | 261 | print_message(); |
262 | return EXIT_FAILURE; | 262 | return EXIT_FAILURE; |
263 | } | 263 | } |
264 | if (ldo(lua_dostring, argv[i]) != 0) { | 264 | if (ldo(lua_dostring, argv[i]) != 0) { |
265 | fprintf(stderr, "lua: error running argument `%.99s'\n", argv[i]); | 265 | fprintf(stderr, l_s("lua: error running argument `%.99s'\n"), argv[i]); |
266 | return EXIT_FAILURE; | 266 | return EXIT_FAILURE; |
267 | } | 267 | } |
268 | break; | 268 | break; |
269 | } | 269 | } |
270 | case 'f': { | 270 | case l_c('f'): { |
271 | i++; | 271 | i++; |
272 | if (argv[i] == NULL) { | 272 | if (argv[i] == NULL) { |
273 | print_message(); | 273 | print_message(); |
274 | return EXIT_FAILURE; | 274 | return EXIT_FAILURE; |
275 | } | 275 | } |
276 | getargs(argv+i); /* collect remaining arguments */ | 276 | getargs(argv+i); /* collect remaining arguments */ |
277 | lua_setglobal(L, "arg"); | 277 | lua_setglobal(L, l_s("arg")); |
278 | return file_input(argv[i]); /* stop scanning arguments */ | 278 | return file_input(argv[i]); /* stop scanning arguments */ |
279 | } | 279 | } |
280 | case 's': { | 280 | case l_c('s'): { |
281 | fprintf(stderr, "lua: stack size (`-s') must be the first option\n"); | 281 | fprintf(stderr, l_s("lua: stack size (`-s') must be the first option\n")); |
282 | return EXIT_FAILURE; | 282 | return EXIT_FAILURE; |
283 | } | 283 | } |
284 | default: { | 284 | default: { |
@@ -292,11 +292,11 @@ static int handle_argv (char *argv[], struct Options *opt) { | |||
292 | } | 292 | } |
293 | 293 | ||
294 | 294 | ||
295 | static void getstacksize (int argc, char *argv[], struct Options *opt) { | 295 | static void getstacksize (int argc, l_char *argv[], struct Options *opt) { |
296 | if (argc >= 2 && argv[1][0] == '-' && argv[1][1] == 's') { | 296 | if (argc >= 2 && argv[1][0] == l_c('-') && argv[1][1] == l_c('s')) { |
297 | int stacksize = atoi(&argv[1][2]); | 297 | int stacksize = atoi(&argv[1][2]); |
298 | if (stacksize <= 0) { | 298 | if (stacksize <= 0) { |
299 | fprintf(stderr, "lua: invalid stack size ('%.20s')\n", &argv[1][2]); | 299 | fprintf(stderr, l_s("lua: invalid stack size ('%.20s')\n"), &argv[1][2]); |
300 | exit(EXIT_FAILURE); | 300 | exit(EXIT_FAILURE); |
301 | } | 301 | } |
302 | opt->stacksize = stacksize; | 302 | opt->stacksize = stacksize; |
@@ -306,10 +306,10 @@ static void getstacksize (int argc, char *argv[], struct Options *opt) { | |||
306 | } | 306 | } |
307 | 307 | ||
308 | 308 | ||
309 | static void register_getargs (char *argv[]) { | 309 | static void register_getargs (l_char *argv[]) { |
310 | lua_pushuserdata(L, argv); | 310 | lua_pushuserdata(L, argv); |
311 | lua_pushcclosure(L, l_getargs, 1); | 311 | lua_pushcclosure(L, l_getargs, 1); |
312 | lua_setglobal(L, "getargs"); | 312 | lua_setglobal(L, l_s("getargs")); |
313 | } | 313 | } |
314 | 314 | ||
315 | 315 | ||
@@ -322,7 +322,7 @@ static void openstdlibs (lua_State *l) { | |||
322 | } | 322 | } |
323 | 323 | ||
324 | 324 | ||
325 | int main (int argc, char *argv[]) { | 325 | int main (int argc, l_char *argv[]) { |
326 | struct Options opt; | 326 | struct Options opt; |
327 | int status; | 327 | int status; |
328 | opt.toclose = 0; | 328 | opt.toclose = 0; |