inout.c 8.0 KB

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