inout.c 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342
  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.51 1997/04/01 17:31:42 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. /* Exported variables */
  21. Word lua_linenumber;
  22. char *lua_parsedfile;
  23. static FILE *fp;
  24. static char *st;
  25. /*
  26. ** Function to get the next character from the input file
  27. */
  28. static int fileinput (void)
  29. {
  30. int c = fgetc(fp);
  31. return (c == EOF) ? 0 : c;
  32. }
  33. /*
  34. ** Function to get the next character from the input string
  35. */
  36. static int stringinput (void)
  37. {
  38. return *st++;
  39. }
  40. /*
  41. ** Function to open a file to be input unit.
  42. ** Return the file.
  43. */
  44. FILE *lua_openfile (char *fn)
  45. {
  46. lua_setinput (fileinput);
  47. if (fn == NULL)
  48. {
  49. fp = stdin;
  50. fn = "(stdin)";
  51. }
  52. else
  53. fp = fopen (fn, "r");
  54. if (fp == NULL)
  55. return NULL;
  56. lua_parsedfile = luaI_createfixedstring(fn)->str;
  57. return fp;
  58. }
  59. /*
  60. ** Function to close an opened file
  61. */
  62. void lua_closefile (void)
  63. {
  64. if (fp != NULL && fp != stdin)
  65. {
  66. fclose (fp);
  67. fp = NULL;
  68. }
  69. }
  70. /*
  71. ** Function to open a string to be input unit
  72. */
  73. #define SIZE_PREF 20 /* size of string prefix to appear in error messages */
  74. void lua_openstring (char *s)
  75. {
  76. char buff[SIZE_PREF+25];
  77. lua_setinput(stringinput);
  78. st = s;
  79. strcpy(buff, "(dostring) >> ");
  80. strncat(buff, s, SIZE_PREF);
  81. if (strlen(s) > SIZE_PREF) strcat(buff, "...");
  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. /*
  99. ** Internal function: do a string
  100. */
  101. static void lua_internaldostring (void)
  102. {
  103. if (lua_dostring(luaL_check_string(1, "dostring")) == 0)
  104. if (passresults() == 0)
  105. lua_pushuserdata(NULL); /* at least one result to signal no errors */
  106. }
  107. /*
  108. ** Internal function: do a file
  109. */
  110. static void lua_internaldofile (void)
  111. {
  112. char *fname = luaL_opt_string(1, NULL, "dofile");
  113. if (lua_dofile(fname) == 0)
  114. if (passresults() == 0)
  115. lua_pushuserdata(NULL); /* at least one result to signal no errors */
  116. }
  117. static char *tostring (lua_Object obj)
  118. {
  119. if (lua_isstring(obj)) /* get strings and numbers */
  120. return lua_getstring(obj);
  121. else if (lua_istable(obj))
  122. return "<table>";
  123. else if (lua_isfunction(obj))
  124. return "<function>";
  125. else if (lua_isnil(obj))
  126. return "nil";
  127. else /* if (lua_isuserdata(obj)) */
  128. return "<userdata>";
  129. }
  130. static void luaI_tostring (void)
  131. {
  132. lua_pushstring(tostring(lua_getparam(1)));
  133. }
  134. static void luaI_print (void)
  135. {
  136. int i = 1;
  137. lua_Object obj;
  138. while ((obj = lua_getparam(i++)) != LUA_NOOBJECT)
  139. printf("%s\n", tostring(obj));
  140. }
  141. /*
  142. ** Internal function: convert an object to a number
  143. */
  144. static void lua_obj2number (void)
  145. {
  146. lua_Object o = lua_getparam(1);
  147. if (lua_isnumber(o))
  148. lua_pushnumber(lua_getnumber(o));
  149. }
  150. static void luaI_error (void)
  151. {
  152. char *s = lua_getstring(lua_getparam(1));
  153. if (s == NULL) s = "(no message)";
  154. lua_error(s);
  155. }
  156. static void luaI_assert (void)
  157. {
  158. lua_Object p = lua_getparam(1);
  159. if (p == LUA_NOOBJECT || lua_isnil(p))
  160. lua_error("assertion failed!");
  161. }
  162. static void luaI_setglobal (void)
  163. {
  164. lua_Object value = lua_getparam(2);
  165. luaL_arg_check(value != LUA_NOOBJECT, "setglobal", 2, NULL);
  166. lua_pushobject(value);
  167. lua_setglobal(luaL_check_string(1, "setglobal"));
  168. lua_pushobject(value); /* return given value */
  169. }
  170. static void luaI_basicsetglobal (void)
  171. {
  172. lua_Object value = lua_getparam(2);
  173. luaL_arg_check(value != LUA_NOOBJECT, "basicsetglobal", 2, NULL);
  174. lua_pushobject(value);
  175. lua_basicsetglobal(luaL_check_string(1, "basicsetglobal"));
  176. lua_pushobject(value); /* return given value */
  177. }
  178. static void luaI_getglobal (void)
  179. {
  180. lua_pushobject(lua_getglobal(luaL_check_string(1, "getglobal")));
  181. }
  182. static void luaI_basicgetglobal (void)
  183. {
  184. lua_pushobject(lua_basicgetglobal(luaL_check_string(1, "basicgetglobal")));
  185. }
  186. static void luatag (void)
  187. {
  188. lua_pushnumber(lua_tag(lua_getparam(1)));
  189. }
  190. #define MAXPARAMS 256
  191. static void luaI_call (void)
  192. {
  193. lua_Object f = lua_getparam(1);
  194. lua_Object arg = lua_getparam(2);
  195. lua_Object temp, params[MAXPARAMS];
  196. int narg, i;
  197. luaL_arg_check(lua_isfunction(f), "call", 1, "function expected");
  198. luaL_arg_check(lua_istable(arg), "call", 2, "table expected");
  199. /* narg = arg.n */
  200. lua_pushobject(arg);
  201. lua_pushstring("n");
  202. temp = lua_getsubscript();
  203. narg = lua_isnumber(temp) ? lua_getnumber(temp) : MAXPARAMS+1;
  204. /* read arg[1...n] */
  205. for (i=0; i<narg; i++) {
  206. if (i>=MAXPARAMS)
  207. lua_error("argument list too long in function `call'");
  208. lua_pushobject(arg);
  209. lua_pushnumber(i+1);
  210. params[i] = lua_getsubscript();
  211. if (narg == MAXPARAMS+1 && lua_isnil(params[i])) {
  212. narg = i;
  213. break;
  214. }
  215. }
  216. /* push parameters and do the call */
  217. for (i=0; i<narg; i++)
  218. lua_pushobject(params[i]);
  219. if (lua_callfunction(f))
  220. lua_error(NULL);
  221. else
  222. passresults();
  223. }
  224. static void luaIl_settag (void)
  225. {
  226. lua_Object o = lua_getparam(1);
  227. luaL_arg_check(o != LUA_NOOBJECT, "settag", 1, NULL);
  228. lua_pushobject(o);
  229. lua_settag(luaL_check_number(2, "settag"));
  230. }
  231. static void luaIl_newtag (void)
  232. {
  233. lua_pushnumber(lua_newtag(luaL_check_string(1, "newtag")));
  234. }
  235. static void basicindex (void)
  236. {
  237. lua_Object t = lua_getparam(1);
  238. lua_Object i = lua_getparam(2);
  239. luaL_arg_check(t != LUA_NOOBJECT, "basicindex", 1, NULL);
  240. luaL_arg_check(i != LUA_NOOBJECT, "basicindex", 2, NULL);
  241. lua_pushobject(t);
  242. lua_pushobject(i);
  243. lua_pushobject(lua_basicindex());
  244. }
  245. static void basicstoreindex (void)
  246. {
  247. lua_Object t = lua_getparam(1);
  248. lua_Object i = lua_getparam(2);
  249. lua_Object v = lua_getparam(3);
  250. luaL_arg_check(t != LUA_NOOBJECT && i != LUA_NOOBJECT && v != LUA_NOOBJECT,
  251. "basicindex", 0, NULL);
  252. lua_pushobject(t);
  253. lua_pushobject(i);
  254. lua_pushobject(v);
  255. lua_basicstoreindex();
  256. }
  257. /*
  258. ** Internal functions
  259. */
  260. static struct {
  261. char *name;
  262. lua_CFunction func;
  263. } int_funcs[] = {
  264. {"assert", luaI_assert},
  265. {"basicgetglobal", luaI_basicgetglobal},
  266. {"basicindex", basicindex},
  267. {"basicsetglobal", luaI_basicsetglobal},
  268. {"basicstoreindex", basicstoreindex},
  269. {"call", luaI_call},
  270. {"dofile", lua_internaldofile},
  271. {"dostring", lua_internaldostring},
  272. {"error", luaI_error},
  273. {"getglobal", luaI_getglobal},
  274. {"newtag", luaIl_newtag},
  275. {"next", lua_next},
  276. {"nextvar", luaI_nextvar},
  277. {"print", luaI_print},
  278. {"seterrormethod", luaI_seterrormethod},
  279. {"setfallback", luaI_setfallback},
  280. {"setglobal", luaI_setglobal},
  281. {"setintmethod", luaI_setintmethod},
  282. {"settag", luaIl_settag},
  283. {"tonumber", lua_obj2number},
  284. {"tostring", luaI_tostring},
  285. {"tag", luatag},
  286. {"type", luaI_type}
  287. };
  288. #define INTFUNCSIZE (sizeof(int_funcs)/sizeof(int_funcs[0]))
  289. void luaI_predefine (void)
  290. {
  291. int i;
  292. Word n;
  293. for (i=0; i<INTFUNCSIZE; i++) {
  294. n = luaI_findsymbolbyname(int_funcs[i].name);
  295. s_ttype(n) = LUA_T_CFUNCTION; s_fvalue(n) = int_funcs[i].func;
  296. }
  297. n = luaI_findsymbolbyname("_VERSION_");
  298. s_ttype(n) = LUA_T_STRING; s_tsvalue(n) = lua_createstring(LUA_VERSION);
  299. }