new macro 'luai_writestringerror'

This commit is contained in:
Roberto Ierusalimschy 2010-02-18 17:18:41 -02:00
parent 0d7d559dcc
commit 4274738e81
4 changed files with 25 additions and 18 deletions

View File

@ -1,5 +1,5 @@
/* /*
** $Id: lauxlib.c,v 1.197 2010/01/21 16:49:21 roberto Exp roberto $ ** $Id: lauxlib.c,v 1.198 2010/02/11 15:52:50 roberto Exp roberto $
** Auxiliary functions for building Lua libraries ** Auxiliary functions for building Lua libraries
** See Copyright Notice in lua.h ** See Copyright Notice in lua.h
*/ */
@ -739,7 +739,7 @@ static void *l_alloc (void *ud, void *ptr, size_t osize, size_t nsize) {
static int panic (lua_State *L) { static int panic (lua_State *L) {
fprintf(stderr, "PANIC: unprotected error in call to Lua API (%s)\n", luai_writestringerror("PANIC: unprotected error in call to Lua API (%s)\n",
lua_tostring(L, -1)); lua_tostring(L, -1));
return 0; /* return to Lua to abort */ return 0; /* return to Lua to abort */
} }

View File

@ -1,5 +1,5 @@
/* /*
** $Id: ldblib.c,v 1.118 2009/11/25 15:27:51 roberto Exp roberto $ ** $Id: ldblib.c,v 1.119 2010/01/06 14:42:35 roberto Exp roberto $
** Interface from Lua to its debug API ** Interface from Lua to its debug API
** See Copyright Notice in lua.h ** See Copyright Notice in lua.h
*/ */
@ -339,15 +339,13 @@ static int db_gethook (lua_State *L) {
static int db_debug (lua_State *L) { static int db_debug (lua_State *L) {
for (;;) { for (;;) {
char buffer[250]; char buffer[250];
fputs("lua_debug> ", stderr); luai_writestringerror("%s", "lua_debug> ");
if (fgets(buffer, sizeof(buffer), stdin) == 0 || if (fgets(buffer, sizeof(buffer), stdin) == 0 ||
strcmp(buffer, "cont\n") == 0) strcmp(buffer, "cont\n") == 0)
return 0; return 0;
if (luaL_loadbuffer(L, buffer, strlen(buffer), "=(debug command)") || if (luaL_loadbuffer(L, buffer, strlen(buffer), "=(debug command)") ||
lua_pcall(L, 0, 0, 0)) { lua_pcall(L, 0, 0, 0))
fputs(lua_tostring(L, -1), stderr); luai_writestringerror("%s\n", lua_tostring(L, -1));
fputs("\n", stderr);
}
lua_settop(L, 0); /* remove eventual returns */ lua_settop(L, 0); /* remove eventual returns */
} }
} }

19
lua.c
View File

@ -1,5 +1,5 @@
/* /*
** $Id: lua.c,v 1.185 2010/02/09 11:58:57 roberto Exp roberto $ ** $Id: lua.c,v 1.186 2010/02/11 17:12:27 roberto Exp roberto $
** Lua stand-alone interpreter ** Lua stand-alone interpreter
** See Copyright Notice in lua.h ** See Copyright Notice in lua.h
*/ */
@ -103,11 +103,14 @@ static void laction (int i) {
static void print_usage (const char *badoption) { static void print_usage (const char *badoption) {
if (badoption[1] == 'e' || badoption[1] == 'l') if (badoption[1] == 'e' || badoption[1] == 'l') {
fprintf(stderr, "%s: '%s' needs argument\n", progname, badoption); luai_writestringerror("%s: ", progname);
else luai_writestringerror("'%s' needs argument\n", badoption);
fprintf(stderr, "%s: unrecognized option '%s'\n", progname, badoption); } else {
fprintf(stderr, luai_writestringerror("%s: ", progname);
luai_writestringerror("unrecognized option '%s'\n", badoption);
}
luai_writestringerror(
"usage: %s [options] [script [args]]\n" "usage: %s [options] [script [args]]\n"
"Available options are:\n" "Available options are:\n"
" -e stat execute string " LUA_QL("stat") "\n" " -e stat execute string " LUA_QL("stat") "\n"
@ -122,8 +125,8 @@ static void print_usage (const char *badoption) {
static void l_message (const char *pname, const char *msg) { static void l_message (const char *pname, const char *msg) {
if (pname) fprintf(stderr, "%s: ", pname); if (pname) luai_writestringerror("%s: ", pname);
fprintf(stderr, "%s\n", msg); luai_writestringerror("%s\n", msg);
} }

View File

@ -1,5 +1,5 @@
/* /*
** $Id: luaconf.h,v 1.131 2010/01/21 16:31:24 roberto Exp roberto $ ** $Id: luaconf.h,v 1.132 2010/01/21 16:49:21 roberto Exp roberto $
** Configuration file for Lua ** Configuration file for Lua
** See Copyright Notice in lua.h ** See Copyright Notice in lua.h
*/ */
@ -192,10 +192,16 @@
/* /*
@@ luai_writestring defines how 'print' prints its results. @@ luai_writestring defines how 'print' prints its results.
** CHANGE it if your system does not have a useful stdout.
*/ */
#include <stdio.h>
#define luai_writestring(s,l) fwrite((s), sizeof(char), (l), stdout) #define luai_writestring(s,l) fwrite((s), sizeof(char), (l), stdout)
/*
@@ luai_writestringerror defines how to print error messages.
** (A format string with one argument is enough for Lua...)
*/
#define luai_writestringerror(s,p) fprintf(stderr, (s), (p))