diff options
author | Roberto Ierusalimschy <roberto@inf.puc-rio.br> | 1997-03-17 14:02:29 -0300 |
---|---|---|
committer | Roberto Ierusalimschy <roberto@inf.puc-rio.br> | 1997-03-17 14:02:29 -0300 |
commit | eea734aa881002e90bd9130171a2b94cd9dc3267 (patch) | |
tree | b2816a614fca723d8c0b06e96cd093438e6e098b | |
parent | b6d91e24e23edfe98ad732660fd456e91658edb9 (diff) | |
download | lua-eea734aa881002e90bd9130171a2b94cd9dc3267.tar.gz lua-eea734aa881002e90bd9130171a2b94cd9dc3267.tar.bz2 lua-eea734aa881002e90bd9130171a2b94cd9dc3267.zip |
new module 'auxlib' centralizes functions to get/check parameters.
-rw-r--r-- | auxlib.c | 47 | ||||
-rw-r--r-- | inout.c | 61 | ||||
-rw-r--r-- | iolib.c | 24 | ||||
-rw-r--r-- | lua.h | 13 | ||||
-rw-r--r-- | lualib.h | 7 | ||||
-rw-r--r-- | makefile | 8 | ||||
-rw-r--r-- | mathlib.c | 50 | ||||
-rw-r--r-- | strlib.c | 93 |
8 files changed, 146 insertions, 157 deletions
diff --git a/auxlib.c b/auxlib.c new file mode 100644 index 00000000..0bee6430 --- /dev/null +++ b/auxlib.c | |||
@@ -0,0 +1,47 @@ | |||
1 | char *rcs_auxlib="$Id: $"; | ||
2 | |||
3 | #include <stdio.h> | ||
4 | |||
5 | #include "lua.h" | ||
6 | |||
7 | |||
8 | void luaL_arg_check(int cond, char *funcname, int numarg, char *extramsg) | ||
9 | { | ||
10 | if (!cond) { | ||
11 | char buff[100]; | ||
12 | if (extramsg == NULL) | ||
13 | sprintf(buff, "bad argument #%d to function `%s'", numarg, funcname); | ||
14 | else | ||
15 | sprintf(buff, "bad argument #%d to function `%s' (%s)", | ||
16 | numarg, funcname, extramsg); | ||
17 | lua_error(buff); | ||
18 | } | ||
19 | } | ||
20 | |||
21 | char *luaL_check_string (int numArg, char *funcname) | ||
22 | { | ||
23 | lua_Object o = lua_getparam(numArg); | ||
24 | luaL_arg_check(lua_isstring(o), funcname, numArg, "string expected"); | ||
25 | return lua_getstring(o); | ||
26 | } | ||
27 | |||
28 | char *luaL_opt_string (int numArg, char *def, char *funcname) | ||
29 | { | ||
30 | return (lua_getparam(numArg) == LUA_NOOBJECT) ? def : | ||
31 | luaL_check_string(numArg, funcname); | ||
32 | } | ||
33 | |||
34 | double luaL_check_number (int numArg, char *funcname) | ||
35 | { | ||
36 | lua_Object o = lua_getparam(numArg); | ||
37 | luaL_arg_check(lua_isnumber(o), funcname, numArg, "number expected"); | ||
38 | return lua_getnumber(o); | ||
39 | } | ||
40 | |||
41 | |||
42 | double luaL_opt_number (int numArg, double def, char *funcname) | ||
43 | { | ||
44 | return (lua_getparam(numArg) == LUA_NOOBJECT) ? def : | ||
45 | luaL_check_number(numArg, funcname); | ||
46 | } | ||
47 | |||
@@ -5,7 +5,7 @@ | |||
5 | ** Also provides some predefined lua functions. | 5 | ** Also provides some predefined lua functions. |
6 | */ | 6 | */ |
7 | 7 | ||
8 | char *rcs_inout="$Id: inout.c,v 2.44 1997/02/26 17:38:41 roberto Unstable roberto $"; | 8 | char *rcs_inout="$Id: inout.c,v 2.45 1997/03/11 18:44:28 roberto Exp roberto $"; |
9 | 9 | ||
10 | #include <stdio.h> | 10 | #include <stdio.h> |
11 | #include <string.h> | 11 | #include <string.h> |
@@ -101,32 +101,6 @@ void lua_closestring (void) | |||
101 | } | 101 | } |
102 | 102 | ||
103 | 103 | ||
104 | static void check_arg (int cond, char *func) | ||
105 | { | ||
106 | if (!cond) | ||
107 | { | ||
108 | char buff[100]; | ||
109 | sprintf(buff, "incorrect argument to function `%s'", func); | ||
110 | lua_error(buff); | ||
111 | } | ||
112 | } | ||
113 | |||
114 | static char *check_string (int numArg, char *funcname) | ||
115 | { | ||
116 | lua_Object o = lua_getparam(numArg); | ||
117 | check_arg(lua_isstring(o), funcname); | ||
118 | return lua_getstring(o); | ||
119 | } | ||
120 | |||
121 | static int check_number (int numArg, char *funcname) | ||
122 | { | ||
123 | lua_Object o = lua_getparam(numArg); | ||
124 | check_arg(lua_isnumber(o), funcname); | ||
125 | return (int)lua_getnumber(o); | ||
126 | } | ||
127 | |||
128 | |||
129 | |||
130 | static int passresults (void) | 104 | static int passresults (void) |
131 | { | 105 | { |
132 | int arg = 0; | 106 | int arg = 0; |
@@ -141,7 +115,7 @@ static int passresults (void) | |||
141 | */ | 115 | */ |
142 | static void lua_internaldostring (void) | 116 | static void lua_internaldostring (void) |
143 | { | 117 | { |
144 | if (lua_dostring(check_string(1, "dostring")) == 0) | 118 | if (lua_dostring(luaL_check_string(1, "dostring")) == 0) |
145 | if (passresults() == 0) | 119 | if (passresults() == 0) |
146 | lua_pushuserdata(NULL); /* at least one result to signal no errors */ | 120 | lua_pushuserdata(NULL); /* at least one result to signal no errors */ |
147 | } | 121 | } |
@@ -151,13 +125,7 @@ static void lua_internaldostring (void) | |||
151 | */ | 125 | */ |
152 | static void lua_internaldofile (void) | 126 | static void lua_internaldofile (void) |
153 | { | 127 | { |
154 | lua_Object obj = lua_getparam (1); | 128 | char *fname = luaL_opt_string(1, NULL, "dofile"); |
155 | char *fname = NULL; | ||
156 | if (lua_isstring(obj)) | ||
157 | fname = lua_getstring(obj); | ||
158 | else if (obj != LUA_NOOBJECT) | ||
159 | lua_error("invalid argument to function `dofile'"); | ||
160 | /* else fname = NULL */ | ||
161 | if (lua_dofile(fname) == 0) | 129 | if (lua_dofile(fname) == 0) |
162 | if (passresults() == 0) | 130 | if (passresults() == 0) |
163 | lua_pushuserdata(NULL); /* at least one result to signal no errors */ | 131 | lua_pushuserdata(NULL); /* at least one result to signal no errors */ |
@@ -247,15 +215,15 @@ static void luaI_assert (void) | |||
247 | static void luaI_setglobal (void) | 215 | static void luaI_setglobal (void) |
248 | { | 216 | { |
249 | lua_Object value = lua_getparam(2); | 217 | lua_Object value = lua_getparam(2); |
250 | check_arg(value != LUA_NOOBJECT, "setglobal"); | 218 | luaL_arg_check(value != LUA_NOOBJECT, "setglobal", 2, NULL); |
251 | lua_pushobject(value); | 219 | lua_pushobject(value); |
252 | lua_storeglobal(check_string(1, "setglobal")); | 220 | lua_storeglobal(luaL_check_string(1, "setglobal")); |
253 | lua_pushobject(value); /* return given value */ | 221 | lua_pushobject(value); /* return given value */ |
254 | } | 222 | } |
255 | 223 | ||
256 | static void luaI_getglobal (void) | 224 | static void luaI_getglobal (void) |
257 | { | 225 | { |
258 | lua_pushobject(lua_getglobal(check_string(1, "getglobal"))); | 226 | lua_pushobject(lua_getglobal(luaL_check_string(1, "getglobal"))); |
259 | } | 227 | } |
260 | 228 | ||
261 | #define MAXPARAMS 256 | 229 | #define MAXPARAMS 256 |
@@ -265,8 +233,8 @@ static void luaI_call (void) | |||
265 | lua_Object arg = lua_getparam(2); | 233 | lua_Object arg = lua_getparam(2); |
266 | lua_Object temp, params[MAXPARAMS]; | 234 | lua_Object temp, params[MAXPARAMS]; |
267 | int narg, i; | 235 | int narg, i; |
268 | check_arg(lua_istable(arg), "call"); | 236 | luaL_arg_check(lua_isfunction(f), "call", 1, "function expected"); |
269 | check_arg(lua_isfunction(f), "call"); | 237 | luaL_arg_check(lua_istable(arg), "call", 2, "table expected"); |
270 | /* narg = arg.n */ | 238 | /* narg = arg.n */ |
271 | lua_pushobject(arg); | 239 | lua_pushobject(arg); |
272 | lua_pushstring("n"); | 240 | lua_pushstring("n"); |
@@ -296,21 +264,22 @@ static void luaI_call (void) | |||
296 | static void luaIl_settag (void) | 264 | static void luaIl_settag (void) |
297 | { | 265 | { |
298 | lua_Object o = lua_getparam(1); | 266 | lua_Object o = lua_getparam(1); |
299 | check_arg(o != LUA_NOOBJECT, "settag"); | 267 | luaL_arg_check(o != LUA_NOOBJECT, "settag", 1, NULL); |
300 | lua_pushobject(o); | 268 | lua_pushobject(o); |
301 | lua_settag(check_number(2, "settag")); | 269 | lua_settag(luaL_check_number(2, "settag")); |
302 | } | 270 | } |
303 | 271 | ||
304 | static void luaIl_newtag (void) | 272 | static void luaIl_newtag (void) |
305 | { | 273 | { |
306 | lua_pushnumber(lua_newtag(check_string(1, "newtag"))); | 274 | lua_pushnumber(lua_newtag(luaL_check_string(1, "newtag"))); |
307 | } | 275 | } |
308 | 276 | ||
309 | static void basicindex (void) | 277 | static void basicindex (void) |
310 | { | 278 | { |
311 | lua_Object t = lua_getparam(1); | 279 | lua_Object t = lua_getparam(1); |
312 | lua_Object i = lua_getparam(2); | 280 | lua_Object i = lua_getparam(2); |
313 | check_arg(t != LUA_NOOBJECT && i != LUA_NOOBJECT, "basicindex"); | 281 | luaL_arg_check(t != LUA_NOOBJECT, "basicindex", 1, NULL); |
282 | luaL_arg_check(i != LUA_NOOBJECT, "basicindex", 2, NULL); | ||
314 | lua_pushobject(t); | 283 | lua_pushobject(t); |
315 | lua_pushobject(i); | 284 | lua_pushobject(i); |
316 | lua_pushobject(lua_basicindex()); | 285 | lua_pushobject(lua_basicindex()); |
@@ -321,8 +290,8 @@ static void basicstoreindex (void) | |||
321 | lua_Object t = lua_getparam(1); | 290 | lua_Object t = lua_getparam(1); |
322 | lua_Object i = lua_getparam(2); | 291 | lua_Object i = lua_getparam(2); |
323 | lua_Object v = lua_getparam(3); | 292 | lua_Object v = lua_getparam(3); |
324 | check_arg(t != LUA_NOOBJECT && i != LUA_NOOBJECT && v != LUA_NOOBJECT, | 293 | luaL_arg_check(t != LUA_NOOBJECT && i != LUA_NOOBJECT && v != LUA_NOOBJECT, |
325 | "basicindex"); | 294 | "basicindex", 0, NULL); |
326 | lua_pushobject(t); | 295 | lua_pushobject(t); |
327 | lua_pushobject(i); | 296 | lua_pushobject(i); |
328 | lua_pushobject(v); | 297 | lua_pushobject(v); |
@@ -58,7 +58,7 @@ static void io_readfrom (void) | |||
58 | else if (lua_isuserdata(f)) | 58 | else if (lua_isuserdata(f)) |
59 | lua_infile = lua_getuserdata(f); | 59 | lua_infile = lua_getuserdata(f); |
60 | else { | 60 | else { |
61 | char *s = lua_check_string(1, "readfrom"); | 61 | char *s = luaL_check_string(1, "readfrom"); |
62 | FILE *fp = (*s == '|') ? popen(s+1, "r") : fopen(s, "r"); | 62 | FILE *fp = (*s == '|') ? popen(s+1, "r") : fopen(s, "r"); |
63 | if (fp) | 63 | if (fp) |
64 | lua_infile = fp; | 64 | lua_infile = fp; |
@@ -79,7 +79,7 @@ static void io_writeto (void) | |||
79 | else if (lua_isuserdata(f)) | 79 | else if (lua_isuserdata(f)) |
80 | lua_outfile = lua_getuserdata(f); | 80 | lua_outfile = lua_getuserdata(f); |
81 | else { | 81 | else { |
82 | char *s = lua_check_string(1, "writeto"); | 82 | char *s = luaL_check_string(1, "writeto"); |
83 | FILE *fp = (*s == '|') ? popen(s+1,"w") : fopen(s,"w"); | 83 | FILE *fp = (*s == '|') ? popen(s+1,"w") : fopen(s,"w"); |
84 | if (fp) | 84 | if (fp) |
85 | lua_outfile = fp; | 85 | lua_outfile = fp; |
@@ -94,7 +94,7 @@ static void io_writeto (void) | |||
94 | 94 | ||
95 | static void io_appendto (void) | 95 | static void io_appendto (void) |
96 | { | 96 | { |
97 | char *s = lua_check_string(1, "appendto"); | 97 | char *s = luaL_check_string(1, "appendto"); |
98 | FILE *fp = fopen (s, "a"); | 98 | FILE *fp = fopen (s, "a"); |
99 | if (fp != NULL) { | 99 | if (fp != NULL) { |
100 | lua_outfile = fp; | 100 | lua_outfile = fp; |
@@ -110,7 +110,7 @@ static void io_appendto (void) | |||
110 | static void io_read (void) | 110 | static void io_read (void) |
111 | { | 111 | { |
112 | char *buff; | 112 | char *buff; |
113 | char *p = lua_opt_string(1, "[^\n]*{\n}", "read"); | 113 | char *p = luaL_opt_string(1, "[^\n]*{\n}", "read"); |
114 | int inskip = 0; /* to control {skips} */ | 114 | int inskip = 0; /* to control {skips} */ |
115 | int c = NEED_OTHER; | 115 | int c = NEED_OTHER; |
116 | luaI_addchar(0); | 116 | luaI_addchar(0); |
@@ -161,7 +161,7 @@ static void io_write (void) | |||
161 | int arg = 1; | 161 | int arg = 1; |
162 | int status = 1; | 162 | int status = 1; |
163 | char *s; | 163 | char *s; |
164 | while ((s = lua_opt_string(arg++, NULL, "write")) != NULL) | 164 | while ((s = luaL_opt_string(arg++, NULL, "write")) != NULL) |
165 | status = status && (fputs(s, lua_outfile) != EOF); | 165 | status = status && (fputs(s, lua_outfile) != EOF); |
166 | pushresult(status); | 166 | pushresult(status); |
167 | } | 167 | } |
@@ -169,20 +169,20 @@ static void io_write (void) | |||
169 | 169 | ||
170 | static void io_execute (void) | 170 | static void io_execute (void) |
171 | { | 171 | { |
172 | lua_pushnumber(system(lua_check_string(1, "execute"))); | 172 | lua_pushnumber(system(luaL_check_string(1, "execute"))); |
173 | } | 173 | } |
174 | 174 | ||
175 | 175 | ||
176 | static void io_remove (void) | 176 | static void io_remove (void) |
177 | { | 177 | { |
178 | pushresult(remove(lua_check_string(1, "remove")) == 0); | 178 | pushresult(remove(luaL_check_string(1, "remove")) == 0); |
179 | } | 179 | } |
180 | 180 | ||
181 | 181 | ||
182 | static void io_rename (void) | 182 | static void io_rename (void) |
183 | { | 183 | { |
184 | pushresult(rename(lua_check_string(1, "rename"), | 184 | pushresult(rename(luaL_check_string(1, "rename"), |
185 | lua_check_string(2, "rename")) == 0); | 185 | luaL_check_string(2, "rename")) == 0); |
186 | } | 186 | } |
187 | 187 | ||
188 | 188 | ||
@@ -195,7 +195,7 @@ static void io_tmpname (void) | |||
195 | 195 | ||
196 | static void io_getenv (void) | 196 | static void io_getenv (void) |
197 | { | 197 | { |
198 | lua_pushstring(getenv(lua_check_string(1, "getenv"))); /* if NULL push nil */ | 198 | lua_pushstring(getenv(luaL_check_string(1, "getenv"))); /* if NULL push nil */ |
199 | } | 199 | } |
200 | 200 | ||
201 | 201 | ||
@@ -203,7 +203,7 @@ static void io_date (void) | |||
203 | { | 203 | { |
204 | time_t t; | 204 | time_t t; |
205 | struct tm *tm; | 205 | struct tm *tm; |
206 | char *s = lua_opt_string(1, "%c", "date"); | 206 | char *s = luaL_opt_string(1, "%c", "date"); |
207 | char b[BUFSIZ]; | 207 | char b[BUFSIZ]; |
208 | time(&t); tm = localtime(&t); | 208 | time(&t); tm = localtime(&t); |
209 | if (strftime(b,sizeof(b),s,tm)) | 209 | if (strftime(b,sizeof(b),s,tm)) |
@@ -269,7 +269,7 @@ static void lua_printstack (FILE *f) | |||
269 | 269 | ||
270 | static void errorfb (void) | 270 | static void errorfb (void) |
271 | { | 271 | { |
272 | char *s = lua_opt_string(1, "(no messsage)", NULL); | 272 | char *s = luaL_opt_string(1, "(no messsage)", NULL); |
273 | fprintf(stderr, "lua: %s\n", s); | 273 | fprintf(stderr, "lua: %s\n", s); |
274 | lua_printstack(stderr); | 274 | lua_printstack(stderr); |
275 | } | 275 | } |
@@ -2,7 +2,7 @@ | |||
2 | ** LUA - Linguagem para Usuarios de Aplicacao | 2 | ** LUA - Linguagem para Usuarios de Aplicacao |
3 | ** Grupo de Tecnologia em Computacao Grafica | 3 | ** Grupo de Tecnologia em Computacao Grafica |
4 | ** TeCGraf - PUC-Rio | 4 | ** TeCGraf - PUC-Rio |
5 | ** $Id: lua.h,v 3.34 1997/02/20 15:51:14 roberto Exp roberto $ | 5 | ** $Id: lua.h,v 3.35 1997/02/26 17:38:41 roberto Unstable roberto $ |
6 | */ | 6 | */ |
7 | 7 | ||
8 | 8 | ||
@@ -80,6 +80,7 @@ void lua_unref (int ref); | |||
80 | lua_Object lua_createtable (void); | 80 | lua_Object lua_createtable (void); |
81 | 81 | ||
82 | 82 | ||
83 | /* =============================================================== */ | ||
83 | /* some useful macros */ | 84 | /* some useful macros */ |
84 | 85 | ||
85 | #define lua_refobject(o,l) (lua_pushobject(o), lua_ref(l)) | 86 | #define lua_refobject(o,l) (lua_pushobject(o), lua_ref(l)) |
@@ -89,7 +90,17 @@ lua_Object lua_createtable (void); | |||
89 | #define lua_pushuserdata(u) lua_pushusertag(u, 0) | 90 | #define lua_pushuserdata(u) lua_pushusertag(u, 0) |
90 | 91 | ||
91 | 92 | ||
93 | /* =============================================================== */ | ||
94 | /* Auxiliar functions for libraries */ | ||
92 | 95 | ||
96 | void luaL_arg_check(int cond, char *funcname, int numarg, char *extramsg); | ||
97 | char *luaL_check_string (int numArg, char *funcname); | ||
98 | char *luaL_opt_string (int numArg, char *def, char *funcname); | ||
99 | double luaL_check_number (int numArg, char *funcname); | ||
100 | double luaL_opt_number (int numArg, double def, char *funcname); | ||
101 | |||
102 | |||
103 | /* =============================================================== */ | ||
93 | /* for compatibility with old versions. Avoid using these macros */ | 104 | /* for compatibility with old versions. Avoid using these macros */ |
94 | 105 | ||
95 | #define lua_type(o) (lua_tag(o)) | 106 | #define lua_type(o) (lua_tag(o)) |
@@ -2,7 +2,7 @@ | |||
2 | ** Libraries to be used in LUA programs | 2 | ** Libraries to be used in LUA programs |
3 | ** Grupo de Tecnologia em Computacao Grafica | 3 | ** Grupo de Tecnologia em Computacao Grafica |
4 | ** TeCGraf - PUC-Rio | 4 | ** TeCGraf - PUC-Rio |
5 | ** $Id: lualib.h,v 1.9 1996/08/01 14:55:33 roberto Exp roberto $ | 5 | ** $Id: lualib.h,v 1.10 1996/08/05 20:55:24 roberto Exp roberto $ |
6 | */ | 6 | */ |
7 | 7 | ||
8 | #ifndef lualib_h | 8 | #ifndef lualib_h |
@@ -23,11 +23,6 @@ struct lua_reg { | |||
23 | }; | 23 | }; |
24 | 24 | ||
25 | void luaI_openlib (struct lua_reg *l, int n); | 25 | void luaI_openlib (struct lua_reg *l, int n); |
26 | void lua_arg_check(int cond, char *funcname); | ||
27 | char *lua_check_string (int numArg, char *funcname); | ||
28 | char *lua_opt_string (int numArg, char *def, char *funcname); | ||
29 | double lua_check_number (int numArg, char *funcname); | ||
30 | long lua_opt_number (int numArg, long def, char *funcname); | ||
31 | char *luaI_addchar (int c); | 26 | char *luaI_addchar (int c); |
32 | void luaI_addquoted (char *s); | 27 | void luaI_addquoted (char *s); |
33 | 28 | ||
@@ -1,4 +1,4 @@ | |||
1 | # $Id: makefile,v 1.27 1996/11/06 20:48:03 roberto Exp roberto $ | 1 | # $Id: makefile,v 1.28 1997/03/05 13:37:04 roberto Exp roberto $ |
2 | 2 | ||
3 | #configuration | 3 | #configuration |
4 | 4 | ||
@@ -30,7 +30,8 @@ LUAOBJS = \ | |||
30 | fallback.o \ | 30 | fallback.o \ |
31 | mem.o \ | 31 | mem.o \ |
32 | func.o \ | 32 | func.o \ |
33 | undump.o | 33 | undump.o \ |
34 | auxlib.o | ||
34 | 35 | ||
35 | LIBOBJS = \ | 36 | LIBOBJS = \ |
36 | iolib.o \ | 37 | iolib.o \ |
@@ -75,6 +76,7 @@ clear : | |||
75 | co $@ | 76 | co $@ |
76 | 77 | ||
77 | 78 | ||
79 | auxlib.o: auxlib.c lua.h | ||
78 | fallback.o: fallback.c mem.h fallback.h lua.h opcode.h types.h tree.h \ | 80 | fallback.o: fallback.c mem.h fallback.h lua.h opcode.h types.h tree.h \ |
79 | func.h table.h hash.h | 81 | func.h table.h hash.h |
80 | func.o: func.c luadebug.h lua.h table.h tree.h types.h opcode.h func.h \ | 82 | func.o: func.c luadebug.h lua.h table.h tree.h types.h opcode.h func.h \ |
@@ -97,6 +99,6 @@ strlib.o: strlib.c lua.h lualib.h | |||
97 | table.o: table.c mem.h opcode.h lua.h types.h tree.h func.h hash.h \ | 99 | table.o: table.c mem.h opcode.h lua.h types.h tree.h func.h hash.h \ |
98 | table.h inout.h fallback.h luadebug.h | 100 | table.h inout.h fallback.h luadebug.h |
99 | tree.o: tree.c mem.h lua.h tree.h types.h lex.h hash.h opcode.h func.h \ | 101 | tree.o: tree.c mem.h lua.h tree.h types.h lex.h hash.h opcode.h func.h \ |
100 | table.h | 102 | table.h fallback.h |
101 | undump.o: undump.c opcode.h lua.h types.h tree.h func.h mem.h table.h \ | 103 | undump.o: undump.c opcode.h lua.h types.h tree.h func.h mem.h table.h \ |
102 | undump.h | 104 | undump.h |
@@ -3,7 +3,7 @@ | |||
3 | ** Mathematics library to LUA | 3 | ** Mathematics library to LUA |
4 | */ | 4 | */ |
5 | 5 | ||
6 | char *rcs_mathlib="$Id: mathlib.c,v 1.17 1996/04/30 21:13:55 roberto Exp roberto $"; | 6 | char *rcs_mathlib="$Id: mathlib.c,v 1.18 1996/08/01 14:55:33 roberto Exp roberto $"; |
7 | 7 | ||
8 | #include <stdlib.h> | 8 | #include <stdlib.h> |
9 | #include <math.h> | 9 | #include <math.h> |
@@ -19,7 +19,7 @@ char *rcs_mathlib="$Id: mathlib.c,v 1.17 1996/04/30 21:13:55 roberto Exp roberto | |||
19 | 19 | ||
20 | static void math_abs (void) | 20 | static void math_abs (void) |
21 | { | 21 | { |
22 | double d = lua_check_number(1, "abs"); | 22 | double d = luaL_check_number(1, "abs"); |
23 | if (d < 0) d = -d; | 23 | if (d < 0) d = -d; |
24 | lua_pushnumber (d); | 24 | lua_pushnumber (d); |
25 | } | 25 | } |
@@ -27,7 +27,7 @@ static void math_abs (void) | |||
27 | 27 | ||
28 | static void math_sin (void) | 28 | static void math_sin (void) |
29 | { | 29 | { |
30 | double d = lua_check_number(1, "sin"); | 30 | double d = luaL_check_number(1, "sin"); |
31 | lua_pushnumber (sin(TORAD(d))); | 31 | lua_pushnumber (sin(TORAD(d))); |
32 | } | 32 | } |
33 | 33 | ||
@@ -35,7 +35,7 @@ static void math_sin (void) | |||
35 | 35 | ||
36 | static void math_cos (void) | 36 | static void math_cos (void) |
37 | { | 37 | { |
38 | double d = lua_check_number(1, "cos"); | 38 | double d = luaL_check_number(1, "cos"); |
39 | lua_pushnumber (cos(TORAD(d))); | 39 | lua_pushnumber (cos(TORAD(d))); |
40 | } | 40 | } |
41 | 41 | ||
@@ -43,64 +43,64 @@ static void math_cos (void) | |||
43 | 43 | ||
44 | static void math_tan (void) | 44 | static void math_tan (void) |
45 | { | 45 | { |
46 | double d = lua_check_number(1, "tan"); | 46 | double d = luaL_check_number(1, "tan"); |
47 | lua_pushnumber (tan(TORAD(d))); | 47 | lua_pushnumber (tan(TORAD(d))); |
48 | } | 48 | } |
49 | 49 | ||
50 | 50 | ||
51 | static void math_asin (void) | 51 | static void math_asin (void) |
52 | { | 52 | { |
53 | double d = lua_check_number(1, "asin"); | 53 | double d = luaL_check_number(1, "asin"); |
54 | lua_pushnumber (TODEGREE(asin(d))); | 54 | lua_pushnumber (TODEGREE(asin(d))); |
55 | } | 55 | } |
56 | 56 | ||
57 | 57 | ||
58 | static void math_acos (void) | 58 | static void math_acos (void) |
59 | { | 59 | { |
60 | double d = lua_check_number(1, "acos"); | 60 | double d = luaL_check_number(1, "acos"); |
61 | lua_pushnumber (TODEGREE(acos(d))); | 61 | lua_pushnumber (TODEGREE(acos(d))); |
62 | } | 62 | } |
63 | 63 | ||
64 | 64 | ||
65 | static void math_atan (void) | 65 | static void math_atan (void) |
66 | { | 66 | { |
67 | double d = lua_check_number(1, "atan"); | 67 | double d = luaL_check_number(1, "atan"); |
68 | lua_pushnumber (TODEGREE(atan(d))); | 68 | lua_pushnumber (TODEGREE(atan(d))); |
69 | } | 69 | } |
70 | 70 | ||
71 | 71 | ||
72 | static void math_atan2 (void) | 72 | static void math_atan2 (void) |
73 | { | 73 | { |
74 | double d1 = lua_check_number(1, "atan2"); | 74 | double d1 = luaL_check_number(1, "atan2"); |
75 | double d2 = lua_check_number(2, "atan2"); | 75 | double d2 = luaL_check_number(2, "atan2"); |
76 | lua_pushnumber (TODEGREE(atan2(d1, d2))); | 76 | lua_pushnumber (TODEGREE(atan2(d1, d2))); |
77 | } | 77 | } |
78 | 78 | ||
79 | 79 | ||
80 | static void math_ceil (void) | 80 | static void math_ceil (void) |
81 | { | 81 | { |
82 | double d = lua_check_number(1, "ceil"); | 82 | double d = luaL_check_number(1, "ceil"); |
83 | lua_pushnumber (ceil(d)); | 83 | lua_pushnumber (ceil(d)); |
84 | } | 84 | } |
85 | 85 | ||
86 | 86 | ||
87 | static void math_floor (void) | 87 | static void math_floor (void) |
88 | { | 88 | { |
89 | double d = lua_check_number(1, "floor"); | 89 | double d = luaL_check_number(1, "floor"); |
90 | lua_pushnumber (floor(d)); | 90 | lua_pushnumber (floor(d)); |
91 | } | 91 | } |
92 | 92 | ||
93 | static void math_mod (void) | 93 | static void math_mod (void) |
94 | { | 94 | { |
95 | float x = lua_check_number(1, "mod"); | 95 | float x = luaL_check_number(1, "mod"); |
96 | float y = lua_check_number(2, "mod"); | 96 | float y = luaL_check_number(2, "mod"); |
97 | lua_pushnumber(fmod(x, y)); | 97 | lua_pushnumber(fmod(x, y)); |
98 | } | 98 | } |
99 | 99 | ||
100 | 100 | ||
101 | static void math_sqrt (void) | 101 | static void math_sqrt (void) |
102 | { | 102 | { |
103 | double d = lua_check_number(1, "sqrt"); | 103 | double d = luaL_check_number(1, "sqrt"); |
104 | lua_pushnumber (sqrt(d)); | 104 | lua_pushnumber (sqrt(d)); |
105 | } | 105 | } |
106 | 106 | ||
@@ -131,10 +131,10 @@ static void math_pow (void) | |||
131 | static void math_min (void) | 131 | static void math_min (void) |
132 | { | 132 | { |
133 | int i=1; | 133 | int i=1; |
134 | double dmin = lua_check_number(i, "min"); | 134 | double dmin = luaL_check_number(i, "min"); |
135 | while (lua_getparam(++i) != LUA_NOOBJECT) | 135 | while (lua_getparam(++i) != LUA_NOOBJECT) |
136 | { | 136 | { |
137 | double d = lua_check_number(i, "min"); | 137 | double d = luaL_check_number(i, "min"); |
138 | if (d < dmin) dmin = d; | 138 | if (d < dmin) dmin = d; |
139 | } | 139 | } |
140 | lua_pushnumber (dmin); | 140 | lua_pushnumber (dmin); |
@@ -143,10 +143,10 @@ static void math_min (void) | |||
143 | static void math_max (void) | 143 | static void math_max (void) |
144 | { | 144 | { |
145 | int i=1; | 145 | int i=1; |
146 | double dmax = lua_check_number(i, "max"); | 146 | double dmax = luaL_check_number(i, "max"); |
147 | while (lua_getparam(++i) != LUA_NOOBJECT) | 147 | while (lua_getparam(++i) != LUA_NOOBJECT) |
148 | { | 148 | { |
149 | double d = lua_check_number(i, "max"); | 149 | double d = luaL_check_number(i, "max"); |
150 | if (d > dmax) dmax = d; | 150 | if (d > dmax) dmax = d; |
151 | } | 151 | } |
152 | lua_pushnumber (dmax); | 152 | lua_pushnumber (dmax); |
@@ -154,33 +154,33 @@ static void math_max (void) | |||
154 | 154 | ||
155 | static void math_log (void) | 155 | static void math_log (void) |
156 | { | 156 | { |
157 | double d = lua_check_number(1, "log"); | 157 | double d = luaL_check_number(1, "log"); |
158 | lua_pushnumber (log(d)); | 158 | lua_pushnumber (log(d)); |
159 | } | 159 | } |
160 | 160 | ||
161 | 161 | ||
162 | static void math_log10 (void) | 162 | static void math_log10 (void) |
163 | { | 163 | { |
164 | double d = lua_check_number(1, "log10"); | 164 | double d = luaL_check_number(1, "log10"); |
165 | lua_pushnumber (log10(d)); | 165 | lua_pushnumber (log10(d)); |
166 | } | 166 | } |
167 | 167 | ||
168 | 168 | ||
169 | static void math_exp (void) | 169 | static void math_exp (void) |
170 | { | 170 | { |
171 | double d = lua_check_number(1, "exp"); | 171 | double d = luaL_check_number(1, "exp"); |
172 | lua_pushnumber (exp(d)); | 172 | lua_pushnumber (exp(d)); |
173 | } | 173 | } |
174 | 174 | ||
175 | static void math_deg (void) | 175 | static void math_deg (void) |
176 | { | 176 | { |
177 | float d = lua_check_number(1, "deg"); | 177 | float d = luaL_check_number(1, "deg"); |
178 | lua_pushnumber (d*180./PI); | 178 | lua_pushnumber (d*180./PI); |
179 | } | 179 | } |
180 | 180 | ||
181 | static void math_rad (void) | 181 | static void math_rad (void) |
182 | { | 182 | { |
183 | float d = lua_check_number(1, "rad"); | 183 | float d = luaL_check_number(1, "rad"); |
184 | lua_pushnumber (d/180.*PI); | 184 | lua_pushnumber (d/180.*PI); |
185 | } | 185 | } |
186 | 186 | ||
@@ -191,7 +191,7 @@ static void math_random (void) | |||
191 | 191 | ||
192 | static void math_randomseed (void) | 192 | static void math_randomseed (void) |
193 | { | 193 | { |
194 | srand(lua_check_number(1, "randomseed")); | 194 | srand(luaL_check_number(1, "randomseed")); |
195 | } | 195 | } |
196 | 196 | ||
197 | 197 | ||
@@ -3,7 +3,7 @@ | |||
3 | ** String library to LUA | 3 | ** String library to LUA |
4 | */ | 4 | */ |
5 | 5 | ||
6 | char *rcs_strlib="$Id: strlib.c,v 1.34 1996/11/22 13:08:02 roberto Exp roberto $"; | 6 | char *rcs_strlib="$Id: strlib.c,v 1.35 1997/02/21 15:21:34 roberto Exp roberto $"; |
7 | 7 | ||
8 | #include <string.h> | 8 | #include <string.h> |
9 | #include <stdio.h> | 9 | #include <stdio.h> |
@@ -42,41 +42,6 @@ static char *openspace (unsigned long size) | |||
42 | return buff+lbuffer.size; | 42 | return buff+lbuffer.size; |
43 | } | 43 | } |
44 | 44 | ||
45 | void lua_arg_check(int cond, char *funcname) | ||
46 | { | ||
47 | if (!cond) { | ||
48 | char buff[100]; | ||
49 | sprintf(buff, "incorrect argument to function `%s'", funcname); | ||
50 | lua_error(buff); | ||
51 | } | ||
52 | } | ||
53 | |||
54 | char *lua_check_string (int numArg, char *funcname) | ||
55 | { | ||
56 | lua_Object o = lua_getparam(numArg); | ||
57 | lua_arg_check(lua_isstring(o), funcname); | ||
58 | return lua_getstring(o); | ||
59 | } | ||
60 | |||
61 | char *lua_opt_string (int numArg, char *def, char *funcname) | ||
62 | { | ||
63 | return (lua_getparam(numArg) == LUA_NOOBJECT) ? def : | ||
64 | lua_check_string(numArg, funcname); | ||
65 | } | ||
66 | |||
67 | double lua_check_number (int numArg, char *funcname) | ||
68 | { | ||
69 | lua_Object o = lua_getparam(numArg); | ||
70 | lua_arg_check(lua_isnumber(o), funcname); | ||
71 | return lua_getnumber(o); | ||
72 | } | ||
73 | |||
74 | long lua_opt_number (int numArg, long def, char *funcname) | ||
75 | { | ||
76 | return (lua_getparam(numArg) == LUA_NOOBJECT) ? def : | ||
77 | (long)lua_check_number(numArg, funcname); | ||
78 | } | ||
79 | |||
80 | char *luaI_addchar (int c) | 45 | char *luaI_addchar (int c) |
81 | { | 46 | { |
82 | if (lbuffer.size >= lbuffer.max) | 47 | if (lbuffer.size >= lbuffer.max) |
@@ -104,8 +69,8 @@ static void addstr (char *s) | |||
104 | */ | 69 | */ |
105 | static void str_tok (void) | 70 | static void str_tok (void) |
106 | { | 71 | { |
107 | char *s1 = lua_check_string(1, "strtok"); | 72 | char *s1 = luaL_check_string(1, "strtok"); |
108 | char *del = lua_check_string(2, "strtok"); | 73 | char *del = luaL_check_string(2, "strtok"); |
109 | lua_Object t = lua_createtable(); | 74 | lua_Object t = lua_createtable(); |
110 | int i = 1; | 75 | int i = 1; |
111 | /* As strtok changes s1, and s1 is "constant", make a copy of it */ | 76 | /* As strtok changes s1, and s1 is "constant", make a copy of it */ |
@@ -127,7 +92,7 @@ static void str_tok (void) | |||
127 | */ | 92 | */ |
128 | static void str_len (void) | 93 | static void str_len (void) |
129 | { | 94 | { |
130 | lua_pushnumber(strlen(lua_check_string(1, "strlen"))); | 95 | lua_pushnumber(strlen(luaL_check_string(1, "strlen"))); |
131 | } | 96 | } |
132 | 97 | ||
133 | /* | 98 | /* |
@@ -135,9 +100,9 @@ static void str_len (void) | |||
135 | */ | 100 | */ |
136 | static void str_sub (void) | 101 | static void str_sub (void) |
137 | { | 102 | { |
138 | char *s = lua_check_string(1, "strsub"); | 103 | char *s = luaL_check_string(1, "strsub"); |
139 | long start = (long)lua_check_number(2, "strsub"); | 104 | long start = (long)luaL_check_number(2, "strsub"); |
140 | long end = lua_opt_number(3, strlen(s), "strsub"); | 105 | long end = (long)luaL_opt_number(3, strlen(s), "strsub"); |
141 | if (1 <= start && start <= end && end <= strlen(s)) { | 106 | if (1 <= start && start <= end && end <= strlen(s)) { |
142 | luaI_addchar(0); | 107 | luaI_addchar(0); |
143 | addnchar(s+start-1, end-start+1); | 108 | addnchar(s+start-1, end-start+1); |
@@ -151,7 +116,7 @@ static void str_sub (void) | |||
151 | */ | 116 | */ |
152 | static void str_lower (void) | 117 | static void str_lower (void) |
153 | { | 118 | { |
154 | char *s = lua_check_string(1, "strlower"); | 119 | char *s = luaL_check_string(1, "strlower"); |
155 | luaI_addchar(0); | 120 | luaI_addchar(0); |
156 | while (*s) | 121 | while (*s) |
157 | luaI_addchar(tolower((unsigned char)*s++)); | 122 | luaI_addchar(tolower((unsigned char)*s++)); |
@@ -163,7 +128,7 @@ static void str_lower (void) | |||
163 | */ | 128 | */ |
164 | static void str_upper (void) | 129 | static void str_upper (void) |
165 | { | 130 | { |
166 | char *s = lua_check_string(1, "strupper"); | 131 | char *s = luaL_check_string(1, "strupper"); |
167 | luaI_addchar(0); | 132 | luaI_addchar(0); |
168 | while (*s) | 133 | while (*s) |
169 | luaI_addchar(toupper((unsigned char)*s++)); | 134 | luaI_addchar(toupper((unsigned char)*s++)); |
@@ -172,8 +137,8 @@ static void str_upper (void) | |||
172 | 137 | ||
173 | static void str_rep (void) | 138 | static void str_rep (void) |
174 | { | 139 | { |
175 | char *s = lua_check_string(1, "strrep"); | 140 | char *s = luaL_check_string(1, "strrep"); |
176 | int n = (int)lua_check_number(2, "strrep"); | 141 | int n = (int)luaL_check_number(2, "strrep"); |
177 | luaI_addchar(0); | 142 | luaI_addchar(0); |
178 | while (n-- > 0) | 143 | while (n-- > 0) |
179 | addstr(s); | 144 | addstr(s); |
@@ -185,9 +150,9 @@ static void str_rep (void) | |||
185 | */ | 150 | */ |
186 | static void str_ascii (void) | 151 | static void str_ascii (void) |
187 | { | 152 | { |
188 | char *s = lua_check_string(1, "ascii"); | 153 | char *s = luaL_check_string(1, "ascii"); |
189 | long pos = lua_opt_number(2, 1, "ascii") - 1; | 154 | long pos = (long)luaL_opt_number(2, 1, "ascii") - 1; |
190 | lua_arg_check(0<=pos && pos<strlen(s), "ascii"); | 155 | luaL_arg_check(0<=pos && pos<strlen(s), "ascii", 2, "out of range"); |
191 | lua_pushnumber((unsigned char)s[pos]); | 156 | lua_pushnumber((unsigned char)s[pos]); |
192 | } | 157 | } |
193 | 158 | ||
@@ -394,10 +359,10 @@ static char *match (char *s, char *p, int level) | |||
394 | 359 | ||
395 | static void str_find (void) | 360 | static void str_find (void) |
396 | { | 361 | { |
397 | char *s = lua_check_string(1, "strfind"); | 362 | char *s = luaL_check_string(1, "strfind"); |
398 | char *p = lua_check_string(2, "strfind"); | 363 | char *p = luaL_check_string(2, "strfind"); |
399 | long init = lua_opt_number(3, 1, "strfind") - 1; | 364 | long init = (long)luaL_opt_number(3, 1, "strfind") - 1; |
400 | lua_arg_check(0 <= init && init <= strlen(s), "strfind"); | 365 | luaL_arg_check(0 <= init && init <= strlen(s), "strfind", 3, "out of range"); |
401 | if (lua_getparam(4) != LUA_NOOBJECT || | 366 | if (lua_getparam(4) != LUA_NOOBJECT || |
402 | strpbrk(p, SPECIALS) == NULL) { /* no special caracters? */ | 367 | strpbrk(p, SPECIALS) == NULL) { /* no special caracters? */ |
403 | char *s2 = strstr(s+init, p); | 368 | char *s2 = strstr(s+init, p); |
@@ -450,15 +415,15 @@ static void add_s (lua_Object newp) | |||
450 | addstr(lua_isstring(res) ? lua_getstring(res) : ""); | 415 | addstr(lua_isstring(res) ? lua_getstring(res) : ""); |
451 | lua_endblock(); | 416 | lua_endblock(); |
452 | } | 417 | } |
453 | else lua_arg_check(0, "gsub"); | 418 | else luaL_arg_check(0, "gsub", 3, NULL); |
454 | } | 419 | } |
455 | 420 | ||
456 | static void str_gsub (void) | 421 | static void str_gsub (void) |
457 | { | 422 | { |
458 | char *src = lua_check_string(1, "gsub"); | 423 | char *src = luaL_check_string(1, "gsub"); |
459 | char *p = lua_check_string(2, "gsub"); | 424 | char *p = luaL_check_string(2, "gsub"); |
460 | lua_Object newp = lua_getparam(3); | 425 | lua_Object newp = lua_getparam(3); |
461 | int max_s = lua_opt_number(4, strlen(src)+1, "gsub"); | 426 | int max_s = (int)luaL_opt_number(4, strlen(src)+1, "gsub"); |
462 | int anchor = (*p == '^') ? (p++, 1) : 0; | 427 | int anchor = (*p == '^') ? (p++, 1) : 0; |
463 | int n = 0; | 428 | int n = 0; |
464 | luaI_addchar(0); | 429 | luaI_addchar(0); |
@@ -482,9 +447,9 @@ static void str_gsub (void) | |||
482 | 447 | ||
483 | static void str_set (void) | 448 | static void str_set (void) |
484 | { | 449 | { |
485 | char *item = lua_check_string(1, "strset"); | 450 | char *item = luaL_check_string(1, "strset"); |
486 | int i; | 451 | int i; |
487 | lua_arg_check(*item_end(item) == 0, "strset"); | 452 | luaL_arg_check(*item_end(item) == 0, "strset", 1, "wrong format"); |
488 | luaI_addchar(0); | 453 | luaI_addchar(0); |
489 | for (i=1; i<256; i++) /* 0 cannot be part of a set */ | 454 | for (i=1; i<256; i++) /* 0 cannot be part of a set */ |
490 | if (singlematch(i, item)) | 455 | if (singlematch(i, item)) |
@@ -509,7 +474,7 @@ void luaI_addquoted (char *s) | |||
509 | static void str_format (void) | 474 | static void str_format (void) |
510 | { | 475 | { |
511 | int arg = 1; | 476 | int arg = 1; |
512 | char *strfrmt = lua_check_string(arg++, "format"); | 477 | char *strfrmt = luaL_check_string(arg++, "format"); |
513 | luaI_addchar(0); /* initialize */ | 478 | luaI_addchar(0); /* initialize */ |
514 | while (*strfrmt) { | 479 | while (*strfrmt) { |
515 | if (*strfrmt != '%') | 480 | if (*strfrmt != '%') |
@@ -528,20 +493,20 @@ static void str_format (void) | |||
528 | buff = openspace(1000); /* to store the formated value */ | 493 | buff = openspace(1000); /* to store the formated value */ |
529 | switch (*strfrmt++) { | 494 | switch (*strfrmt++) { |
530 | case 'q': | 495 | case 'q': |
531 | luaI_addquoted(lua_check_string(arg++, "format")); | 496 | luaI_addquoted(luaL_check_string(arg++, "format")); |
532 | continue; | 497 | continue; |
533 | case 's': { | 498 | case 's': { |
534 | char *s = lua_check_string(arg++, "format"); | 499 | char *s = luaL_check_string(arg++, "format"); |
535 | buff = openspace(strlen(s)); | 500 | buff = openspace(strlen(s)); |
536 | sprintf(buff, form, s); | 501 | sprintf(buff, form, s); |
537 | break; | 502 | break; |
538 | } | 503 | } |
539 | case 'c': case 'd': case 'i': case 'o': | 504 | case 'c': case 'd': case 'i': case 'o': |
540 | case 'u': case 'x': case 'X': | 505 | case 'u': case 'x': case 'X': |
541 | sprintf(buff, form, (int)lua_check_number(arg++, "format")); | 506 | sprintf(buff, form, (int)luaL_check_number(arg++, "format")); |
542 | break; | 507 | break; |
543 | case 'e': case 'E': case 'f': case 'g': | 508 | case 'e': case 'E': case 'f': case 'g': |
544 | sprintf(buff, form, lua_check_number(arg++, "format")); | 509 | sprintf(buff, form, luaL_check_number(arg++, "format")); |
545 | break; | 510 | break; |
546 | default: /* also treat cases 'pnLlh' */ | 511 | default: /* also treat cases 'pnLlh' */ |
547 | lua_error("invalid format option in function `format'"); | 512 | lua_error("invalid format option in function `format'"); |