aboutsummaryrefslogtreecommitdiff
path: root/inout.c
diff options
context:
space:
mode:
authorRoberto Ierusalimschy <roberto@inf.puc-rio.br>1997-09-16 16:25:59 -0300
committerRoberto Ierusalimschy <roberto@inf.puc-rio.br>1997-09-16 16:25:59 -0300
commit26c5f56ad1ac6f0409e638f44af75da09f2b0a0b (patch)
treea4c1cfa52e591f47e54e847ba27fed7ca6e62725 /inout.c
parentdaa858ef278ad136afcb62d7f625b491ffbd5fa3 (diff)
downloadlua-26c5f56ad1ac6f0409e638f44af75da09f2b0a0b.tar.gz
lua-26c5f56ad1ac6f0409e638f44af75da09f2b0a0b.tar.bz2
lua-26c5f56ad1ac6f0409e638f44af75da09f2b0a0b.zip
Built-in functions
Diffstat (limited to 'inout.c')
-rw-r--r--inout.c405
1 files changed, 0 insertions, 405 deletions
diff --git a/inout.c b/inout.c
deleted file mode 100644
index 0a05c9c4..00000000
--- a/inout.c
+++ /dev/null
@@ -1,405 +0,0 @@
1/*
2** inout.c
3** Provide function to realise the input/output function and debugger
4** facilities.
5** Also provides some predefined lua functions.
6*/
7
8char *rcs_inout="$Id: inout.c,v 2.71 1997/07/29 13:33:15 roberto Exp roberto $";
9
10#include <stdio.h>
11#include <string.h>
12
13#include "auxlib.h"
14#include "fallback.h"
15#include "hash.h"
16#include "inout.h"
17#include "lex.h"
18#include "lua.h"
19#include "luamem.h"
20#include "luamem.h"
21#include "opcode.h"
22#include "table.h"
23#include "tree.h"
24#include "undump.h"
25#include "zio.h"
26
27
28/* Exported variables */
29Word lua_linenumber;
30TaggedString *lua_parsedfile;
31
32
33char *luaI_typenames[] = { /* ORDER LUA_T */
34 "userdata", "line", "cmark", "mark", "function",
35 "function", "table", "string", "number", "nil",
36 NULL
37};
38
39
40
41void luaI_setparsedfile (char *name)
42{
43 lua_parsedfile = luaI_createstring(name);
44}
45
46
47int lua_doFILE (FILE *f, int bin)
48{
49 ZIO z;
50 luaZ_Fopen(&z, f);
51 if (bin)
52 return luaI_undump(&z);
53 else {
54 lua_setinput(&z);
55 return lua_domain();
56 }
57}
58
59
60int lua_dofile (char *filename)
61{
62 int status;
63 int c;
64 FILE *f = (filename == NULL) ? stdin : fopen(filename, "r");
65 if (f == NULL)
66 return 2;
67 luaI_setparsedfile(filename?filename:"(stdin)");
68 c = fgetc(f);
69 ungetc(c, f);
70 if (c == ID_CHUNK) {
71 f = freopen(filename, "rb", f); /* set binary mode */
72 status = lua_doFILE(f, 1);
73 }
74 else
75 status = lua_doFILE(f, 0);
76 if (f != stdin)
77 fclose(f);
78 return status;
79}
80
81
82
83#define SIZE_PREF 20 /* size of string prefix to appear in error messages */
84
85
86int lua_dobuffer (char *buff, int size)
87{
88 int status;
89 ZIO z;
90 luaI_setparsedfile("(buffer)");
91 luaZ_mopen(&z, buff, size);
92 status = luaI_undump(&z);
93 return status;
94}
95
96
97int lua_dostring (char *str)
98{
99 int status;
100 char buff[SIZE_PREF+25];
101 char *temp;
102 ZIO z;
103 if (str == NULL) return 1;
104 sprintf(buff, "(dostring) >> %.20s", str);
105 temp = strchr(buff, '\n');
106 if (temp) *temp = 0; /* end string after first line */
107 luaI_setparsedfile(buff);
108 luaZ_sopen(&z, str);
109 lua_setinput(&z);
110 status = lua_domain();
111 return status;
112}
113
114
115
116
117static int passresults (void)
118{
119 int arg = 0;
120 lua_Object obj;
121 while ((obj = lua_getresult(++arg)) != LUA_NOOBJECT)
122 lua_pushobject(obj);
123 return arg-1;
124}
125
126
127static void packresults (void)
128{
129 int arg = 0;
130 lua_Object obj;
131 lua_Object table = lua_createtable();
132 while ((obj = lua_getresult(++arg)) != LUA_NOOBJECT) {
133 lua_pushobject(table);
134 lua_pushnumber(arg);
135 lua_pushobject(obj);
136 lua_rawsettable();
137 }
138 lua_pushobject(table);
139 lua_pushstring("n");
140 lua_pushnumber(arg-1);
141 lua_rawsettable();
142 lua_pushobject(table); /* final result */
143}
144
145/*
146** Internal function: do a string
147*/
148static void lua_internaldostring (void)
149{
150 lua_Object err = lua_getparam(2);
151 if (err != LUA_NOOBJECT) { /* set new error method */
152 luaL_arg_check(lua_isnil(err) || lua_isfunction(err), 2,
153 "must be a valid error handler");
154 lua_pushobject(err);
155 err = lua_seterrormethod();
156 }
157 if (lua_dostring(luaL_check_string(1)) == 0)
158 if (passresults() == 0)
159 lua_pushuserdata(NULL); /* at least one result to signal no errors */
160 if (err != LUA_NOOBJECT) { /* restore old error method */
161 lua_pushobject(err);
162 lua_seterrormethod();
163 }
164}
165
166/*
167** Internal function: do a file
168*/
169static void lua_internaldofile (void)
170{
171 char *fname = luaL_opt_string(1, NULL);
172 if (lua_dofile(fname) == 0)
173 if (passresults() == 0)
174 lua_pushuserdata(NULL); /* at least one result to signal no errors */
175}
176
177
178static char *tostring (lua_Object obj)
179{
180 TObject *o = luaI_Address(obj);
181 switch (ttype(o)) {
182 case LUA_T_NUMBER: case LUA_T_STRING:
183 return lua_getstring(obj);
184 case LUA_T_ARRAY: case LUA_T_FUNCTION:
185 case LUA_T_CFUNCTION: case LUA_T_NIL:
186 return luaI_typenames[-ttype(o)];
187 case LUA_T_USERDATA: {
188 char *buff = luaI_buffer(30);
189 sprintf(buff, "userdata: %p", o->value.ts->u.v);
190 return buff;
191 }
192 default: return "<unknown object>";
193 }
194}
195
196static void luaI_tostring (void)
197{
198 lua_pushstring(tostring(lua_getparam(1)));
199}
200
201static void luaI_print (void)
202{
203 int i = 1;
204 lua_Object obj;
205 while ((obj = lua_getparam(i++)) != LUA_NOOBJECT)
206 printf("%s\n", tostring(obj));
207}
208
209static void luaI_type (void)
210{
211 lua_Object o = lua_getparam(1);
212 luaL_arg_check(o != LUA_NOOBJECT, 1, "no argument");
213 lua_pushstring(luaI_typenames[-ttype(luaI_Address(o))]);
214 lua_pushnumber(lua_tag(o));
215}
216
217/*
218** Internal function: convert an object to a number
219*/
220static void lua_obj2number (void)
221{
222 lua_Object o = lua_getparam(1);
223 if (lua_isnumber(o))
224 lua_pushnumber(lua_getnumber(o));
225}
226
227
228static void luaI_error (void)
229{
230 char *s = lua_getstring(lua_getparam(1));
231 if (s == NULL) s = "(no message)";
232 lua_error(s);
233}
234
235static void luaI_assert (void)
236{
237 lua_Object p = lua_getparam(1);
238 if (p == LUA_NOOBJECT || lua_isnil(p))
239 lua_error("assertion failed!");
240}
241
242static void luaI_setglobal (void)
243{
244 lua_Object value = lua_getparam(2);
245 luaL_arg_check(value != LUA_NOOBJECT, 2, NULL);
246 lua_pushobject(value);
247 lua_setglobal(luaL_check_string(1));
248 lua_pushobject(value); /* return given value */
249}
250
251static void luaI_rawsetglobal (void)
252{
253 lua_Object value = lua_getparam(2);
254 luaL_arg_check(value != LUA_NOOBJECT, 2, NULL);
255 lua_pushobject(value);
256 lua_rawsetglobal(luaL_check_string(1));
257 lua_pushobject(value); /* return given value */
258}
259
260static void luaI_getglobal (void)
261{
262 lua_pushobject(lua_getglobal(luaL_check_string(1)));
263}
264
265static void luaI_rawgetglobal (void)
266{
267 lua_pushobject(lua_rawgetglobal(luaL_check_string(1)));
268}
269
270static void luatag (void)
271{
272 lua_pushnumber(lua_tag(lua_getparam(1)));
273}
274
275
276static int getnarg (lua_Object table)
277{
278 lua_Object temp;
279 /* temp = table.n */
280 lua_pushobject(table); lua_pushstring("n"); temp = lua_gettable();
281 return (lua_isnumber(temp) ? lua_getnumber(temp) : MAX_WORD);
282}
283
284static void luaI_call (void)
285{
286 lua_Object f = lua_getparam(1);
287 lua_Object arg = lua_getparam(2);
288 int withtable = (strcmp(luaL_opt_string(3, ""), "pack") == 0);
289 int narg, i;
290 luaL_arg_check(lua_isfunction(f), 1, "function expected");
291 luaL_arg_check(lua_istable(arg), 2, "table expected");
292 narg = getnarg(arg);
293 /* push arg[1...n] */
294 for (i=0; i<narg; i++) {
295 lua_Object temp;
296 /* temp = arg[i+1] */
297 lua_pushobject(arg); lua_pushnumber(i+1); temp = lua_gettable();
298 if (narg == MAX_WORD && lua_isnil(temp))
299 break;
300 lua_pushobject(temp);
301 }
302 if (lua_callfunction(f))
303 lua_error(NULL);
304 else if (withtable)
305 packresults();
306 else
307 passresults();
308}
309
310static void luaIl_settag (void)
311{
312 lua_Object o = lua_getparam(1);
313 luaL_arg_check(lua_istable(o), 1, "table expected");
314 lua_pushobject(o);
315 lua_settag(luaL_check_number(2));
316}
317
318static void luaIl_newtag (void)
319{
320 lua_pushnumber(lua_newtag());
321}
322
323static void rawgettable (void)
324{
325 lua_Object t = lua_getparam(1);
326 lua_Object i = lua_getparam(2);
327 luaL_arg_check(t != LUA_NOOBJECT, 1, NULL);
328 luaL_arg_check(i != LUA_NOOBJECT, 2, NULL);
329 lua_pushobject(t);
330 lua_pushobject(i);
331 lua_pushobject(lua_rawgettable());
332}
333
334static void rawsettable (void)
335{
336 lua_Object t = lua_getparam(1);
337 lua_Object i = lua_getparam(2);
338 lua_Object v = lua_getparam(3);
339 luaL_arg_check(t != LUA_NOOBJECT && i != LUA_NOOBJECT && v != LUA_NOOBJECT,
340 0, NULL);
341 lua_pushobject(t);
342 lua_pushobject(i);
343 lua_pushobject(v);
344 lua_rawsettable();
345}
346
347
348static void luaI_collectgarbage (void)
349{
350 lua_pushnumber(lua_collectgarbage(luaL_opt_number(1, 0)));
351}
352
353
354/*
355** Internal functions
356*/
357static struct {
358 char *name;
359 lua_CFunction func;
360} int_funcs[] = {
361 {"assert", luaI_assert},
362 {"call", luaI_call},
363 {"collectgarbage", luaI_collectgarbage},
364 {"dofile", lua_internaldofile},
365 {"dostring", lua_internaldostring},
366 {"error", luaI_error},
367 {"getglobal", luaI_getglobal},
368 {"newtag", luaIl_newtag},
369 {"next", lua_next},
370 {"nextvar", luaI_nextvar},
371 {"print", luaI_print},
372 {"rawgetglobal", luaI_rawgetglobal},
373 {"rawgettable", rawgettable},
374 {"rawsetglobal", luaI_rawsetglobal},
375 {"rawsettable", rawsettable},
376 {"seterrormethod", luaI_seterrormethod},
377#if LUA_COMPAT2_5
378 {"setfallback", luaI_setfallback},
379#endif
380 {"setglobal", luaI_setglobal},
381 {"settagmethod", luaI_settagmethod},
382 {"gettagmethod", luaI_gettagmethod},
383 {"settag", luaIl_settag},
384 {"tonumber", lua_obj2number},
385 {"tostring", luaI_tostring},
386 {"tag", luatag},
387 {"type", luaI_type}
388};
389
390#define INTFUNCSIZE (sizeof(int_funcs)/sizeof(int_funcs[0]))
391
392
393void luaI_predefine (void)
394{
395 int i;
396 Word n;
397 for (i=0; i<INTFUNCSIZE; i++) {
398 n = luaI_findsymbolbyname(int_funcs[i].name);
399 s_ttype(n) = LUA_T_CFUNCTION; s_fvalue(n) = int_funcs[i].func;
400 }
401 n = luaI_findsymbolbyname("_VERSION");
402 s_ttype(n) = LUA_T_STRING; s_tsvalue(n) = luaI_createstring(LUA_VERSION);
403}
404
405