inout.c 8.8 KB

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