diff options
author | Roberto Ierusalimschy <roberto@inf.puc-rio.br> | 2002-05-06 16:05:10 -0300 |
---|---|---|
committer | Roberto Ierusalimschy <roberto@inf.puc-rio.br> | 2002-05-06 16:05:10 -0300 |
commit | 71144e3ff0cb81bd9b8bb56d94dc76074c638c64 (patch) | |
tree | 8098675276d0640684898d4302ea98ae91e2c430 /lauxlib.c | |
parent | 0dbf0c5953a3d72deebc7e41840a0e73b46de8bc (diff) | |
download | lua-71144e3ff0cb81bd9b8bb56d94dc76074c638c64.tar.gz lua-71144e3ff0cb81bd9b8bb56d94dc76074c638c64.tar.bz2 lua-71144e3ff0cb81bd9b8bb56d94dc76074c638c64.zip |
errors `return' int, to avoid warnings
+ home-made `sprintf' (first version)
Diffstat (limited to 'lauxlib.c')
-rw-r--r-- | lauxlib.c | 63 |
1 files changed, 48 insertions, 15 deletions
@@ -1,5 +1,5 @@ | |||
1 | /* | 1 | /* |
2 | ** $Id: lauxlib.c,v 1.66 2002/04/16 12:00:02 roberto Exp roberto $ | 2 | ** $Id: lauxlib.c,v 1.67 2002/05/01 20:40:42 roberto Exp roberto $ |
3 | ** Auxiliary functions for building Lua libraries | 3 | ** Auxiliary functions for building Lua libraries |
4 | ** See Copyright Notice in lua.h | 4 | ** See Copyright Notice in lua.h |
5 | */ | 5 | */ |
@@ -30,23 +30,21 @@ LUALIB_API int luaL_findstring (const char *name, const char *const list[]) { | |||
30 | } | 30 | } |
31 | 31 | ||
32 | 32 | ||
33 | LUALIB_API void luaL_argerror (lua_State *L, int narg, const char *extramsg) { | 33 | LUALIB_API int luaL_argerror (lua_State *L, int narg, const char *extramsg) { |
34 | lua_Debug ar; | 34 | lua_Debug ar; |
35 | lua_getstack(L, 0, &ar); | 35 | lua_getstack(L, 0, &ar); |
36 | lua_getinfo(L, "n", &ar); | 36 | lua_getinfo(L, "n", &ar); |
37 | if (strcmp(ar.namewhat, "method") == 0) narg--; /* do not count `self' */ | 37 | if (strcmp(ar.namewhat, "method") == 0) narg--; /* do not count `self' */ |
38 | if (ar.name == NULL) | 38 | if (ar.name == NULL) |
39 | ar.name = "?"; | 39 | ar.name = "?"; |
40 | luaL_verror(L, "bad argument #%d to `%.50s' (%.100s)", | 40 | return luaL_verror(L, "bad argument #%d to `%s' (%s)", |
41 | narg, ar.name, extramsg); | 41 | narg, ar.name, extramsg); |
42 | } | 42 | } |
43 | 43 | ||
44 | 44 | ||
45 | LUALIB_API void luaL_typerror (lua_State *L, int narg, const char *tname) { | 45 | LUALIB_API int luaL_typerror (lua_State *L, int narg, const char *tname) { |
46 | char buff[80]; | 46 | luaL_vstr(L, "%s expected, got %s", tname, lua_typename(L, lua_type(L,narg))); |
47 | sprintf(buff, "%.25s expected, got %.25s", tname, | 47 | return luaL_argerror(L, narg, lua_tostring(L, -1)); |
48 | lua_typename(L, lua_type(L,narg))); | ||
49 | luaL_argerror(L, narg, buff); | ||
50 | } | 48 | } |
51 | 49 | ||
52 | 50 | ||
@@ -57,11 +55,11 @@ static void tag_error (lua_State *L, int narg, int tag) { | |||
57 | 55 | ||
58 | LUALIB_API void luaL_check_stack (lua_State *L, int space, const char *mes) { | 56 | LUALIB_API void luaL_check_stack (lua_State *L, int space, const char *mes) { |
59 | if (!lua_checkstack(L, space)) | 57 | if (!lua_checkstack(L, space)) |
60 | luaL_verror(L, "stack overflow (%.30s)", mes); | 58 | luaL_verror(L, "stack overflow (%s)", mes); |
61 | } | 59 | } |
62 | 60 | ||
63 | 61 | ||
64 | LUALIB_API void luaL_check_type(lua_State *L, int narg, int t) { | 62 | LUALIB_API void luaL_check_type (lua_State *L, int narg, int t) { |
65 | if (lua_type(L, narg) != t) | 63 | if (lua_type(L, narg) != t) |
66 | tag_error(L, narg, t); | 64 | tag_error(L, narg, t); |
67 | } | 65 | } |
@@ -144,13 +142,48 @@ LUALIB_API void luaL_opennamedlib (lua_State *L, const char *libname, | |||
144 | } | 142 | } |
145 | 143 | ||
146 | 144 | ||
147 | LUALIB_API void luaL_verror (lua_State *L, const char *fmt, ...) { | 145 | static void vstr (lua_State *L, const char *fmt, va_list argp) { |
148 | char buff[500]; | 146 | luaL_Buffer b; |
147 | luaL_buffinit(L, &b); | ||
148 | for (;;) { | ||
149 | const char *e = strchr(fmt, '%'); | ||
150 | if (e == NULL) break; | ||
151 | luaL_addlstring(&b, fmt, e-fmt); | ||
152 | switch (*(e+1)) { | ||
153 | case 's': | ||
154 | luaL_addstring(&b, va_arg(argp, char *)); | ||
155 | break; | ||
156 | case 'd': | ||
157 | lua_pushnumber(L, va_arg(argp, int)); | ||
158 | luaL_addvalue (&b); | ||
159 | break; | ||
160 | case '%': | ||
161 | luaL_putchar(&b, '%'); | ||
162 | break; | ||
163 | default: | ||
164 | lua_error(L, "invalid format option"); | ||
165 | } | ||
166 | fmt = e+2; | ||
167 | } | ||
168 | luaL_addstring(&b, fmt); | ||
169 | luaL_pushresult(&b); | ||
170 | } | ||
171 | |||
172 | |||
173 | LUALIB_API void luaL_vstr (lua_State *L, const char *fmt, ...) { | ||
174 | va_list argp; | ||
175 | va_start(argp, fmt); | ||
176 | vstr(L, fmt, argp); | ||
177 | va_end(argp); | ||
178 | } | ||
179 | |||
180 | |||
181 | LUALIB_API int luaL_verror (lua_State *L, const char *fmt, ...) { | ||
149 | va_list argp; | 182 | va_list argp; |
150 | va_start(argp, fmt); | 183 | va_start(argp, fmt); |
151 | vsprintf(buff, fmt, argp); | 184 | vstr(L, fmt, argp); |
152 | va_end(argp); | 185 | va_end(argp); |
153 | lua_error(L, buff); | 186 | return lua_errorobj(L); |
154 | } | 187 | } |
155 | 188 | ||
156 | 189 | ||