inout.c 8.7 KB

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