From 71144e3ff0cb81bd9b8bb56d94dc76074c638c64 Mon Sep 17 00:00:00 2001 From: Roberto Ierusalimschy Date: Mon, 6 May 2002 16:05:10 -0300 Subject: [PATCH] errors `return' int, to avoid warnings + home-made `sprintf' (first version) --- lapi.c | 5 +++-- lauxlib.c | 63 +++++++++++++++++++++++++++++++++++++++++------------- lauxlib.h | 10 ++++----- lbaselib.c | 41 ++++++++++++++++------------------- ldblib.c | 57 ++++++++++++++++++++++-------------------------- liolib.c | 15 ++++++------- lmathlib.c | 4 ++-- lstrlib.c | 11 +++++----- ltests.c | 4 ++-- lua.h | 4 ++-- 10 files changed, 118 insertions(+), 96 deletions(-) diff --git a/lapi.c b/lapi.c index 54ec5f3b..fa05ca49 100644 --- a/lapi.c +++ b/lapi.c @@ -1,5 +1,5 @@ /* -** $Id: lapi.c,v 1.187 2002/05/02 16:55:55 roberto Exp roberto $ +** $Id: lapi.c,v 1.188 2002/05/06 15:51:41 roberto Exp roberto $ ** Lua API ** See Copyright Notice in lua.h */ @@ -641,11 +641,12 @@ LUA_API void lua_setgcthreshold (lua_State *L, int newthreshold) { */ -LUA_API void lua_errorobj (lua_State *L) { +LUA_API int lua_errorobj (lua_State *L) { lua_lock(L); api_checknelems(L, 1); luaD_errorobj(L, L->top - 1, LUA_ERRRUN); lua_unlock(L); + return 0; /* to avoid warnings */ } diff --git a/lauxlib.c b/lauxlib.c index 46454d83..7be33892 100644 --- a/lauxlib.c +++ b/lauxlib.c @@ -1,5 +1,5 @@ /* -** $Id: lauxlib.c,v 1.66 2002/04/16 12:00:02 roberto Exp roberto $ +** $Id: lauxlib.c,v 1.67 2002/05/01 20:40:42 roberto Exp roberto $ ** Auxiliary functions for building Lua libraries ** See Copyright Notice in lua.h */ @@ -30,23 +30,21 @@ LUALIB_API int luaL_findstring (const char *name, const char *const list[]) { } -LUALIB_API void luaL_argerror (lua_State *L, int narg, const char *extramsg) { +LUALIB_API int luaL_argerror (lua_State *L, int narg, const char *extramsg) { lua_Debug ar; lua_getstack(L, 0, &ar); lua_getinfo(L, "n", &ar); if (strcmp(ar.namewhat, "method") == 0) narg--; /* do not count `self' */ if (ar.name == NULL) ar.name = "?"; - luaL_verror(L, "bad argument #%d to `%.50s' (%.100s)", - narg, ar.name, extramsg); + return luaL_verror(L, "bad argument #%d to `%s' (%s)", + narg, ar.name, extramsg); } -LUALIB_API void luaL_typerror (lua_State *L, int narg, const char *tname) { - char buff[80]; - sprintf(buff, "%.25s expected, got %.25s", tname, - lua_typename(L, lua_type(L,narg))); - luaL_argerror(L, narg, buff); +LUALIB_API int luaL_typerror (lua_State *L, int narg, const char *tname) { + luaL_vstr(L, "%s expected, got %s", tname, lua_typename(L, lua_type(L,narg))); + return luaL_argerror(L, narg, lua_tostring(L, -1)); } @@ -57,11 +55,11 @@ static void tag_error (lua_State *L, int narg, int tag) { LUALIB_API void luaL_check_stack (lua_State *L, int space, const char *mes) { if (!lua_checkstack(L, space)) - luaL_verror(L, "stack overflow (%.30s)", mes); + luaL_verror(L, "stack overflow (%s)", mes); } -LUALIB_API void luaL_check_type(lua_State *L, int narg, int t) { +LUALIB_API void luaL_check_type (lua_State *L, int narg, int t) { if (lua_type(L, narg) != t) tag_error(L, narg, t); } @@ -144,13 +142,48 @@ LUALIB_API void luaL_opennamedlib (lua_State *L, const char *libname, } -LUALIB_API void luaL_verror (lua_State *L, const char *fmt, ...) { - char buff[500]; +static void vstr (lua_State *L, const char *fmt, va_list argp) { + luaL_Buffer b; + luaL_buffinit(L, &b); + for (;;) { + const char *e = strchr(fmt, '%'); + if (e == NULL) break; + luaL_addlstring(&b, fmt, e-fmt); + switch (*(e+1)) { + case 's': + luaL_addstring(&b, va_arg(argp, char *)); + break; + case 'd': + lua_pushnumber(L, va_arg(argp, int)); + luaL_addvalue (&b); + break; + case '%': + luaL_putchar(&b, '%'); + break; + default: + lua_error(L, "invalid format option"); + } + fmt = e+2; + } + luaL_addstring(&b, fmt); + luaL_pushresult(&b); +} + + +LUALIB_API void luaL_vstr (lua_State *L, const char *fmt, ...) { va_list argp; va_start(argp, fmt); - vsprintf(buff, fmt, argp); + vstr(L, fmt, argp); va_end(argp); - lua_error(L, buff); +} + + +LUALIB_API int luaL_verror (lua_State *L, const char *fmt, ...) { + va_list argp; + va_start(argp, fmt); + vstr(L, fmt, argp); + va_end(argp); + return lua_errorobj(L); } diff --git a/lauxlib.h b/lauxlib.h index 6affc407..93282517 100644 --- a/lauxlib.h +++ b/lauxlib.h @@ -1,5 +1,5 @@ /* -** $Id: lauxlib.h,v 1.44 2002/04/02 20:42:49 roberto Exp roberto $ +** $Id: lauxlib.h,v 1.45 2002/05/01 20:40:42 roberto Exp roberto $ ** Auxiliary functions for building Lua libraries ** See Copyright Notice in lua.h */ @@ -31,9 +31,8 @@ LUALIB_API void luaL_openlib (lua_State *L, const luaL_reg *l, int nup); LUALIB_API void luaL_opennamedlib (lua_State *L, const char *libname, const luaL_reg *l, int nup); LUALIB_API int luaL_callmeta (lua_State *L, int obj, const char *event); -LUALIB_API void luaL_typerror (lua_State *L, int narg, const char *tname); -LUALIB_API void luaL_argerror (lua_State *L, int numarg, - const char *extramsg); +LUALIB_API int luaL_typerror (lua_State *L, int narg, const char *tname); +LUALIB_API int luaL_argerror (lua_State *L, int numarg, const char *extramsg); LUALIB_API const char *luaL_check_lstr (lua_State *L, int numArg, size_t *len); LUALIB_API const char *luaL_opt_lstr (lua_State *L, int numArg, @@ -45,7 +44,8 @@ LUALIB_API void luaL_check_stack (lua_State *L, int space, const char *msg); LUALIB_API void luaL_check_type (lua_State *L, int narg, int t); LUALIB_API void luaL_check_any (lua_State *L, int narg); -LUALIB_API void luaL_verror (lua_State *L, const char *fmt, ...); +LUALIB_API int luaL_verror (lua_State *L, const char *fmt, ...); +LUALIB_API void luaL_vstr (lua_State *L, const char *fmt, ...); LUALIB_API int luaL_findstring (const char *name, const char *const list[]); diff --git a/lbaselib.c b/lbaselib.c index 50bdc7f7..a92f785d 100644 --- a/lbaselib.c +++ b/lbaselib.c @@ -1,5 +1,5 @@ /* -** $Id: lbaselib.c,v 1.70 2002/05/01 20:40:42 roberto Exp roberto $ +** $Id: lbaselib.c,v 1.71 2002/05/02 17:12:27 roberto Exp roberto $ ** Basic library ** See Copyright Notice in lua.h */ @@ -43,9 +43,7 @@ static int luaB__ERRORMESSAGE (lua_State *L) { if (lua_getstack(L, 1, &ar)) { lua_getinfo(L, "Sl", &ar); if (ar.source && ar.currentline > 0) { - char buff[100]; - sprintf(buff, "\n <%.70s: line %d>", ar.short_src, ar.currentline); - lua_pushstring(L, buff); + luaL_vstr(L, "\n <%s: line %d>", ar.short_src, ar.currentline); lua_concat(L, 2); } } @@ -72,7 +70,7 @@ static int luaB_print (lua_State *L) { lua_rawcall(L, 1, 1); s = lua_tostring(L, -1); /* get result */ if (s == NULL) - luaL_verror(L, "`tostring' must return a string to `print'"); + return luaL_verror(L, "`tostring' must return a string to `print'"); if (i>1) fputs("\t", stdout); fputs(s, stdout); lua_pop(L, 1); /* pop result */ @@ -112,8 +110,7 @@ static int luaB_tonumber (lua_State *L) { static int luaB_error (lua_State *L) { lua_settop(L, 1); - lua_errorobj(L); - return 0; /* to avoid warnings */ + return lua_errorobj(L); } @@ -242,7 +239,7 @@ static int luaB_loadfile (lua_State *L) { static int luaB_assert (lua_State *L) { luaL_check_any(L, 1); if (!lua_toboolean(L, 1)) - luaL_verror(L, "assertion failed! %.90s", luaL_opt_string(L, 2, "")); + return luaL_verror(L, "assertion failed! %s", luaL_opt_string(L, 2, "")); lua_settop(L, 1); return 1; } @@ -335,6 +332,7 @@ static const char *getpath (lua_State *L) { const char *path; lua_getglobal(L, LUA_PATH); /* try global variable */ path = lua_tostring(L, -1); + lua_pop(L, 1); if (path) return path; path = getenv(LUA_PATH); /* else try environment variable */ if (path) return path; @@ -342,7 +340,7 @@ static const char *getpath (lua_State *L) { } -static const char *nextpath (lua_State *L, const char *path) { +static const char *pushnextpath (lua_State *L, const char *path) { const char *l; if (*path == '\0') return NULL; /* no more pathes */ if (*path == LUA_PATH_SEP) path++; /* skip separator */ @@ -353,7 +351,7 @@ static const char *nextpath (lua_State *L, const char *path) { } -static void composename (lua_State *L) { +static void pushcomposename (lua_State *L) { const char *path = lua_tostring(L, -1); const char *wild = strchr(path, '?'); if (wild == NULL) return; /* no wild char; path is the file name */ @@ -372,35 +370,34 @@ static int luaB_require (lua_State *L) { lua_pushvalue(L, 1); lua_setglobal(L, "_REQUIREDNAME"); lua_getglobal(L, REQTAB); - if (!lua_istable(L, 2)) luaL_verror(L, REQTAB " is not a table"); + if (!lua_istable(L, 2)) return luaL_verror(L, REQTAB " is not a table"); path = getpath(L); lua_pushvalue(L, 1); /* check package's name in book-keeping table */ lua_gettable(L, 2); if (!lua_isnil(L, -1)) /* is it there? */ return 0; /* package is already loaded */ else { /* must load it */ - while (status == LUA_ERRFILE && (path = nextpath(L, path)) != NULL) { - composename(L); + while (status == LUA_ERRFILE) { + lua_settop(L, 3); /* reset stack position */ + if ((path = pushnextpath(L, path)) == NULL) break; + pushcomposename(L); status = lua_loadfile(L, lua_tostring(L, -1)); /* try to load it */ - if (status == 0) - status = lua_pcall(L, 0, 0, 0); - lua_settop(L, 3); /* pop string and eventual results from dofile */ } } switch (status) { case 0: { + lua_rawcall(L, 0, 0); /* run loaded module */ lua_pushvalue(L, 1); lua_pushboolean(L, 1); lua_settable(L, 2); /* mark it as loaded */ return 0; } case LUA_ERRFILE: { /* file not found */ - luaL_verror(L, "could not load package `%.20s' from path `%.200s'", - lua_tostring(L, 1), lua_tostring(L, 3)); + return luaL_verror(L, "could not load package `%s' from path `%s'", + lua_tostring(L, 1), getpath(L)); } default: { - luaL_verror(L, "error loading package"); - return 0; /* to avoid warnings */ + return luaL_verror(L, "error loading package\n%s", lua_tostring(L, -1)); } } } @@ -445,7 +442,7 @@ static int luaB_resume (lua_State *L) { lua_State *co = (lua_State *)lua_getfrombox(L, lua_upvalueindex(1)); lua_settop(L, 0); if (lua_resume(L, co) != 0) - lua_errorobj(L); + return lua_errorobj(L); return lua_gettop(L); } @@ -466,7 +463,7 @@ static int luaB_coroutine (lua_State *L) { luaL_arg_check(L, lua_isfunction(L, 1) && !lua_iscfunction(L, 1), 1, "Lua function expected"); NL = lua_newthread(L); - if (NL == NULL) luaL_verror(L, "unable to create new thread"); + if (NL == NULL) return luaL_verror(L, "unable to create new thread"); /* move function and arguments from L to NL */ for (i=0; i LEVELS1 && firstpart) { /* no more than `LEVELS2' more levels? */ if (!lua_getstack(L, level+LEVELS2, &ar)) level--; /* keep going */ else { - luaL_addstring(&b, " ...\n"); /* too many levels */ + lua_pushliteral(L, " ...\n"); /* too many levels */ while (lua_getstack(L, level+LEVELS2, &ar)) /* find last levels */ level++; } @@ -208,40 +207,34 @@ static int errorfb (lua_State *L) { continue; } sprintf(buff, "%4d: ", level-1); - luaL_addstring(&b, buff); + lua_pushstring(L, buff); lua_getinfo(L, "Snl", &ar); switch (*ar.namewhat) { case 'g': case 'l': /* global, local */ - sprintf(buff, "function `%.50s'", ar.name); + luaL_vstr(L, "function `%s'", ar.name); break; case 'f': /* field */ - sprintf(buff, "method `%.50s'", ar.name); - break; - case 't': /* tag method */ - sprintf(buff, "`%.50s' tag method", ar.name); + case 'm': /* method */ + luaL_vstr(L, "method `%s'", ar.name); break; default: { if (*ar.what == 'm') /* main? */ - sprintf(buff, "main of %.70s", ar.short_src); + luaL_vstr(L, "main of %s", ar.short_src); else if (*ar.what == 'C') /* C function? */ - sprintf(buff, "%.70s", ar.short_src); + luaL_vstr(L, "%s", ar.short_src); else - sprintf(buff, "function <%d:%.70s>", ar.linedefined, ar.short_src); + luaL_vstr(L, "function <%d:%s>", ar.linedefined, ar.short_src); ar.source = NULL; /* do not print source again */ } } - luaL_addstring(&b, buff); - if (ar.currentline > 0) { - sprintf(buff, " at line %d", ar.currentline); - luaL_addstring(&b, buff); - } - if (ar.source) { - sprintf(buff, " [%.70s]", ar.short_src); - luaL_addstring(&b, buff); - } - luaL_addstring(&b, "\n"); + if (ar.currentline > 0) + luaL_vstr(L, " at line %d", ar.currentline); + if (ar.source) + luaL_vstr(L, " [%s]", ar.short_src); + lua_pushliteral(L, "\n"); + lua_concat(L, lua_gettop(L)); } - luaL_pushresult(&b); + lua_concat(L, lua_gettop(L)); return 1; } diff --git a/liolib.c b/liolib.c index bdfe380d..6d6024cf 100644 --- a/liolib.c +++ b/liolib.c @@ -1,5 +1,5 @@ /* -** $Id: liolib.c,v 2.3 2002/04/12 19:56:25 roberto Exp roberto $ +** $Id: liolib.c,v 2.4 2002/05/02 17:12:27 roberto Exp roberto $ ** Standard I/O (and system) library ** See Copyright Notice in lua.h */ @@ -257,7 +257,7 @@ static int g_read (lua_State *L, FILE *f, int first) { else { const char *p = lua_tostring(L, n); if (!p || p[0] != '*') - luaL_verror(L, "invalid `read' option"); + return luaL_verror(L, "invalid `read' option"); switch (p[1]) { case 'n': /* number */ success = read_number(L, f); @@ -270,11 +270,10 @@ static int g_read (lua_State *L, FILE *f, int first) { success = 1; /* always success */ break; case 'w': /* word */ - luaL_verror(L, "obsolete option `*w'"); + return luaL_verror(L, "obsolete option `*w'"); break; default: - luaL_argerror(L, n, "invalid format"); - success = 0; /* to avoid warnings */ + return luaL_argerror(L, n, "invalid format"); } } } @@ -430,7 +429,7 @@ static int io_rename (lua_State *L) { static int io_tmpname (lua_State *L) { char buff[L_tmpnam]; if (tmpnam(buff) != buff) - luaL_verror(L, "unable to generate a unique filename"); + return luaL_verror(L, "unable to generate a unique filename"); lua_pushstring(L, buff); return 1; } @@ -471,7 +470,7 @@ static int getfield (lua_State *L, const char *key, int d) { res = (int)(lua_tonumber(L, -1)); else { if (d == -2) - luaL_verror(L, "field `%.20s' missing in date table", key); + return luaL_verror(L, "field `%s' missing in date table", key); res = d; } lua_pop(L, 1); @@ -510,7 +509,7 @@ static int io_date (lua_State *L) { if (strftime(b, sizeof(b), s, stm)) lua_pushstring(L, b); else - luaL_verror(L, "invalid `date' format"); + return luaL_verror(L, "invalid `date' format"); } return 1; } diff --git a/lmathlib.c b/lmathlib.c index 7d91b083..4890f6f9 100644 --- a/lmathlib.c +++ b/lmathlib.c @@ -1,5 +1,5 @@ /* -** $Id: lmathlib.c,v 1.43 2002/04/04 20:20:49 roberto Exp roberto $ +** $Id: lmathlib.c,v 1.44 2002/05/02 17:12:27 roberto Exp roberto $ ** Standard mathematical library ** See Copyright Notice in lua.h */ @@ -187,7 +187,7 @@ static int math_random (lua_State *L) { lua_pushnumber(L, (int)(r*(u-l+1))+l); /* integer between `l' and `u' */ break; } - default: luaL_verror(L, "wrong number of arguments"); + default: return luaL_verror(L, "wrong number of arguments"); } return 1; } diff --git a/lstrlib.c b/lstrlib.c index b308b78e..8a3cd686 100644 --- a/lstrlib.c +++ b/lstrlib.c @@ -1,5 +1,5 @@ /* -** $Id: lstrlib.c,v 1.80 2002/04/02 20:41:59 roberto Exp roberto $ +** $Id: lstrlib.c,v 1.81 2002/05/02 17:12:27 roberto Exp roberto $ ** Standard library for string operations and pattern-matching ** See Copyright Notice in lua.h */ @@ -170,7 +170,7 @@ typedef struct MatchState { static int check_capture (MatchState *ms, int l) { l -= '1'; if (l < 0 || l >= ms->level || ms->capture[l].len == CAP_UNFINISHED) - luaL_verror(ms->L, "invalid capture index"); + return luaL_verror(ms->L, "invalid capture index"); return l; } @@ -179,8 +179,7 @@ static int capture_to_close (MatchState *ms) { int level = ms->level; for (level--; level>=0; level--) if (ms->capture[level].len == CAP_UNFINISHED) return level; - luaL_verror(ms->L, "invalid pattern capture"); - return 0; /* to avoid warnings */ + return luaL_verror(ms->L, "invalid pattern capture"); } @@ -663,7 +662,7 @@ static int str_format (lua_State *L) { char buff[MAX_ITEM]; /* to store the formatted item */ int hasprecision = 0; if (isdigit(uchar(*strfrmt)) && *(strfrmt+1) == '$') - luaL_verror(L, "obsolete `format' option (d$)"); + return luaL_verror(L, "obsolete `format' option (d$)"); arg++; strfrmt = scanformat(L, strfrmt, form, &hasprecision); switch (*strfrmt++) { @@ -696,7 +695,7 @@ static int str_format (lua_State *L) { } } default: /* also treat cases `pnLlh' */ - luaL_verror(L, "invalid option in `format'"); + return luaL_verror(L, "invalid option in `format'"); } luaL_addlstring(&b, buff, strlen(buff)); } diff --git a/ltests.c b/ltests.c index e19cea71..feef9e52 100644 --- a/ltests.c +++ b/ltests.c @@ -1,5 +1,5 @@ /* -** $Id: ltests.c,v 1.118 2002/05/01 20:40:42 roberto Exp roberto $ +** $Id: ltests.c,v 1.119 2002/05/02 13:06:20 roberto Exp roberto $ ** Internal Module for Debugging of the Lua Implementation ** See Copyright Notice in lua.h */ @@ -639,7 +639,7 @@ static int testC (lua_State *L) { else if EQ("type") { lua_pushstring(L, lua_typename(L, lua_type(L, getnum))); } - else luaL_verror(L, "unknown instruction %.30s", buff); + else luaL_verror(L, "unknown instruction %s", buff); } return 0; } diff --git a/lua.h b/lua.h index 9ea98c11..47f50f1f 100644 --- a/lua.h +++ b/lua.h @@ -1,5 +1,5 @@ /* -** $Id: lua.h,v 1.129 2002/05/01 20:40:42 roberto Exp roberto $ +** $Id: lua.h,v 1.130 2002/05/01 20:48:12 roberto Exp roberto $ ** Lua - An Extensible Extension Language ** TeCGraf: Grupo de Tecnologia em Computacao Grafica, PUC-Rio, Brazil ** e-mail: info@lua.org @@ -193,7 +193,7 @@ LUA_API void lua_setgcthreshold (lua_State *L, int newthreshold); ** miscellaneous functions */ -LUA_API void lua_errorobj (lua_State *L); +LUA_API int lua_errorobj (lua_State *L); LUA_API int lua_next (lua_State *L, int index); LUA_API int lua_getn (lua_State *L, int index);