aboutsummaryrefslogtreecommitdiff
path: root/liolib.c
diff options
context:
space:
mode:
authorRoberto Ierusalimschy <roberto@inf.puc-rio.br>1998-08-21 14:43:44 -0300
committerRoberto Ierusalimschy <roberto@inf.puc-rio.br>1998-08-21 14:43:44 -0300
commitcc117253c83ec32d226a8f2226d5ca144804f873 (patch)
tree46551b01d3febf98d65d55276d5831a75e2e0062 /liolib.c
parent8e226e6a09390a80fde98f192833fc85a5537376 (diff)
downloadlua-cc117253c83ec32d226a8f2226d5ca144804f873.tar.gz
lua-cc117253c83ec32d226a8f2226d5ca144804f873.tar.bz2
lua-cc117253c83ec32d226a8f2226d5ca144804f873.zip
new implementation for error handling: on error, function _ERRORMESSAGE
is called, which in turn calls _ALERT to write a message to stderr.
Diffstat (limited to 'liolib.c')
-rw-r--r--liolib.c87
1 files changed, 44 insertions, 43 deletions
diff --git a/liolib.c b/liolib.c
index d252931c..0fbc425f 100644
--- a/liolib.c
+++ b/liolib.c
@@ -1,5 +1,5 @@
1/* 1/*
2** $Id: liolib.c,v 1.20 1998/06/05 22:17:44 roberto Exp roberto $ 2** $Id: liolib.c,v 1.21 1998/06/18 17:04:28 roberto Exp roberto $
3** Standard I/O (and system) library 3** Standard I/O (and system) library
4** See Copyright Notice in lua.h 4** See Copyright Notice in lua.h
5*/ 5*/
@@ -353,71 +353,75 @@ static void io_debug (void)
353} 353}
354 354
355 355
356static void lua_printstack (FILE *f) 356#define MESSAGESIZE 150
357{ 357#define MAXMESSAGE (MESSAGESIZE*10)
358
359static void errorfb (void) {
360 char buff[MAXMESSAGE];
358 int level = 1; /* skip level 0 (it's this function) */ 361 int level = 1; /* skip level 0 (it's this function) */
359 lua_Object func; 362 lua_Object func;
363 sprintf(buff, "lua: %.200s\n", lua_getstring(lua_getparam(1)));
360 while ((func = lua_stackedfunction(level++)) != LUA_NOOBJECT) { 364 while ((func = lua_stackedfunction(level++)) != LUA_NOOBJECT) {
361 char *name; 365 char *name;
362 int currentline; 366 int currentline;
363 char *filename; 367 char *chunkname;
364 int linedefined; 368 int linedefined;
365 lua_funcinfo(func, &filename, &linedefined); 369 lua_funcinfo(func, &chunkname, &linedefined);
366 fprintf(f, (level==2) ? "Active Stack:\n\t" : "\t"); 370 strcat(buff, (level==2) ? "Active Stack:\n\t" : "\t");
371 if (strlen(buff) > MAXMESSAGE-MESSAGESIZE) {
372 strcat(buff, "...\n");
373 break; /* buffer is full */
374 }
367 switch (*lua_getobjname(func, &name)) { 375 switch (*lua_getobjname(func, &name)) {
368 case 'g': 376 case 'g':
369 fprintf(f, "function %s", name); 377 sprintf(buff+strlen(buff), "function %.50s", name);
370 break; 378 break;
371 case 't': 379 case 't':
372 fprintf(f, "`%s' tag method", name); 380 sprintf(buff+strlen(buff), "`%.50s' tag method", name);
373 break; 381 break;
374 default: { 382 default: {
375 if (linedefined == 0) 383 if (linedefined == 0)
376 fprintf(f, "main of %s", filename); 384 sprintf(buff+strlen(buff), "main of %.50s", chunkname);
377 else if (linedefined < 0) 385 else if (linedefined < 0)
378 fprintf(f, "%s", filename); 386 sprintf(buff+strlen(buff), "%.50s", chunkname);
379 else 387 else
380 fprintf(f, "function (%s:%d)", filename, linedefined); 388 sprintf(buff+strlen(buff), "function (%.50s:%d)",
381 filename = NULL; 389 chunkname, linedefined);
390 chunkname = NULL;
382 } 391 }
383 } 392 }
384 if ((currentline = lua_currentline(func)) > 0) 393 if ((currentline = lua_currentline(func)) > 0)
385 fprintf(f, " at line %d", currentline); 394 sprintf(buff+strlen(buff), " at line %d", currentline);
386 if (filename) 395 if (chunkname)
387 fprintf(f, " [in file %s]", filename); 396 sprintf(buff+strlen(buff), " [in chunk %.50s]", chunkname);
388 fprintf(f, "\n"); 397 strcat(buff, "\n");
389 } 398 }
390} 399 lua_pushstring(buff);
391 400 lua_call("_ALERT");
392
393static void errorfb (void)
394{
395 fprintf(stderr, "lua: %s\n", lua_getstring(lua_getparam(1)));
396 lua_printstack(stderr);
397} 401}
398 402
399 403
400 404
401static struct luaL_reg iolib[] = { 405static struct luaL_reg iolib[] = {
402{"setlocale", setloc}, 406 {"setlocale", setloc},
403{"execute", io_execute}, 407 {"execute", io_execute},
404{"remove", io_remove}, 408 {"remove", io_remove},
405{"rename", io_rename}, 409 {"rename", io_rename},
406{"tmpname", io_tmpname}, 410 {"tmpname", io_tmpname},
407{"getenv", io_getenv}, 411 {"getenv", io_getenv},
408{"date", io_date}, 412 {"date", io_date},
409{"clock", io_clock}, 413 {"clock", io_clock},
410{"exit", io_exit}, 414 {"exit", io_exit},
411{"debug", io_debug}, 415 {"debug", io_debug},
412{"print_stack", errorfb} 416 {"_ERRORMESSAGE", errorfb}
413}; 417};
414 418
415static struct luaL_reg iolibtag[] = { 419static struct luaL_reg iolibtag[] = {
416{"readfrom", io_readfrom}, 420 {"readfrom", io_readfrom},
417{"writeto", io_writeto}, 421 {"writeto", io_writeto},
418{"appendto", io_appendto}, 422 {"appendto", io_appendto},
419{"read", io_read}, 423 {"read", io_read},
420{"write", io_write} 424 {"write", io_write}
421}; 425};
422 426
423static void openwithtags (void) 427static void openwithtags (void)
@@ -439,10 +443,7 @@ static void openwithtags (void)
439 setfile(stderr, "_STDERR", iotag); 443 setfile(stderr, "_STDERR", iotag);
440} 444}
441 445
442void lua_iolibopen (void) 446void lua_iolibopen (void) {
443{
444 luaL_openlib(iolib, (sizeof(iolib)/sizeof(iolib[0]))); 447 luaL_openlib(iolib, (sizeof(iolib)/sizeof(iolib[0])));
445 openwithtags(); 448 openwithtags();
446 lua_pushcfunction(errorfb);
447 lua_seterrormethod();
448} 449}