inout.c 8.0 KB

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