inout.c 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293
  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.37 1996/05/28 21:07:32 roberto Exp $";
  8. #include <stdio.h>
  9. #include "lex.h"
  10. #include "opcode.h"
  11. #include "inout.h"
  12. #include "table.h"
  13. #include "tree.h"
  14. #include "lua.h"
  15. #include "mem.h"
  16. /* Exported variables */
  17. Word lua_linenumber;
  18. char *lua_parsedfile;
  19. static FILE *fp;
  20. static char *st;
  21. /*
  22. ** Function to get the next character from the input file
  23. */
  24. static int fileinput (void)
  25. {
  26. return fgetc (fp);
  27. }
  28. /*
  29. ** Function to get the next character from the input string
  30. */
  31. static int stringinput (void)
  32. {
  33. return *st++;
  34. }
  35. /*
  36. ** Function to open a file to be input unit.
  37. ** Return the file.
  38. */
  39. FILE *lua_openfile (char *fn)
  40. {
  41. lua_setinput (fileinput);
  42. if (fn == NULL)
  43. {
  44. fp = stdin;
  45. fn = "(stdin)";
  46. }
  47. else
  48. fp = fopen (fn, "r");
  49. if (fp == NULL)
  50. return NULL;
  51. lua_linenumber = 1;
  52. lua_parsedfile = luaI_createfixedstring(fn)->str;
  53. return fp;
  54. }
  55. /*
  56. ** Function to close an opened file
  57. */
  58. void lua_closefile (void)
  59. {
  60. if (fp != NULL && fp != stdin)
  61. {
  62. fclose (fp);
  63. fp = NULL;
  64. }
  65. }
  66. /*
  67. ** Function to open a string to be input unit
  68. */
  69. void lua_openstring (char *s)
  70. {
  71. lua_setinput (stringinput);
  72. st = s;
  73. lua_linenumber = 1;
  74. lua_parsedfile = luaI_createfixedstring("(string)")->str;
  75. }
  76. /*
  77. ** Function to close an opened string
  78. */
  79. void lua_closestring (void)
  80. {
  81. }
  82. static void check_arg (int cond, char *func)
  83. {
  84. if (!cond)
  85. {
  86. char buff[100];
  87. sprintf(buff, "incorrect argument to function `%s'", func);
  88. lua_error(buff);
  89. }
  90. }
  91. /*
  92. ** Internal function: do a string
  93. */
  94. void lua_internaldostring (void)
  95. {
  96. lua_Object obj = lua_getparam (1);
  97. if (lua_isstring(obj) && !lua_dostring(lua_getstring(obj)))
  98. lua_pushnumber(1);
  99. }
  100. /*
  101. ** Internal function: do a file
  102. */
  103. void lua_internaldofile (void)
  104. {
  105. lua_Object obj = lua_getparam (1);
  106. char *fname = NULL;
  107. if (lua_isstring(obj))
  108. fname = lua_getstring(obj);
  109. else if (obj != LUA_NOOBJECT)
  110. lua_error("invalid argument to function `dofile'");
  111. /* else fname = NULL */
  112. if (!lua_dofile(fname))
  113. lua_pushnumber(1);
  114. }
  115. static char *tostring (lua_Object obj)
  116. {
  117. char *buff = luaI_buffer(20);
  118. if (lua_isstring(obj)) /* get strings and numbers */
  119. return lua_getstring(obj);
  120. else switch(lua_type(obj))
  121. {
  122. case LUA_T_FUNCTION:
  123. sprintf(buff, "function: %p", (luaI_Address(obj))->value.tf);
  124. break;
  125. case LUA_T_CFUNCTION:
  126. sprintf(buff, "cfunction: %p", lua_getcfunction(obj));
  127. break;
  128. case LUA_T_ARRAY:
  129. sprintf(buff, "table: %p", avalue(luaI_Address(obj)));
  130. break;
  131. case LUA_T_NIL:
  132. sprintf(buff, "nil");
  133. break;
  134. default:
  135. sprintf(buff, "userdata: %p", lua_getuserdata(obj));
  136. break;
  137. }
  138. return buff;
  139. }
  140. void luaI_tostring (void)
  141. {
  142. lua_pushstring(tostring(lua_getparam(1)));
  143. }
  144. void luaI_print (void)
  145. {
  146. int i = 1;
  147. lua_Object obj;
  148. while ((obj = lua_getparam(i++)) != LUA_NOOBJECT)
  149. printf("%s\n", tostring(obj));
  150. }
  151. /*
  152. ** Internal function: return an object type.
  153. */
  154. void luaI_type (void)
  155. {
  156. lua_Object o = lua_getparam(1);
  157. int t;
  158. if (o == LUA_NOOBJECT)
  159. lua_error("no parameter to function 'type'");
  160. t = lua_type(o);
  161. switch (t)
  162. {
  163. case LUA_T_NIL :
  164. lua_pushliteral("nil");
  165. break;
  166. case LUA_T_NUMBER :
  167. lua_pushliteral("number");
  168. break;
  169. case LUA_T_STRING :
  170. lua_pushliteral("string");
  171. break;
  172. case LUA_T_ARRAY :
  173. lua_pushliteral("table");
  174. break;
  175. case LUA_T_FUNCTION :
  176. case LUA_T_CFUNCTION :
  177. lua_pushliteral("function");
  178. break;
  179. default :
  180. lua_pushliteral("userdata");
  181. break;
  182. }
  183. lua_pushnumber(t);
  184. }
  185. /*
  186. ** Internal function: convert an object to a number
  187. */
  188. 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. 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. 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. void luaI_setglobal (void)
  207. {
  208. lua_Object name = lua_getparam(1);
  209. lua_Object value = lua_getparam(2);
  210. check_arg(lua_isstring(name), "setglobal");
  211. lua_pushobject(value);
  212. lua_storeglobal(lua_getstring(name));
  213. lua_pushobject(value); /* return given value */
  214. }
  215. void luaI_getglobal (void)
  216. {
  217. lua_Object name = lua_getparam(1);
  218. check_arg(lua_isstring(name), "getglobal");
  219. lua_pushobject(lua_getglobal(lua_getstring(name)));
  220. }
  221. #define MAXPARAMS 256
  222. void luaI_call (void)
  223. {
  224. lua_Object f = lua_getparam(1);
  225. lua_Object arg = lua_getparam(2);
  226. lua_Object temp, params[MAXPARAMS];
  227. int narg, i;
  228. check_arg(lua_istable(arg), "call");
  229. check_arg(lua_isfunction(f), "call");
  230. /* narg = arg.n */
  231. lua_pushobject(arg);
  232. lua_pushstring("n");
  233. temp = lua_getsubscript();
  234. narg = lua_isnumber(temp) ? lua_getnumber(temp) : MAXPARAMS+1;
  235. /* read arg[1...n] */
  236. for (i=0; i<narg; i++)
  237. {
  238. if (i>=MAXPARAMS)
  239. lua_error("argument list too long in function `call'");
  240. lua_pushobject(arg);
  241. lua_pushnumber(i+1);
  242. params[i] = lua_getsubscript();
  243. if (narg == MAXPARAMS+1 && lua_isnil(params[i]))
  244. {
  245. narg = i;
  246. break;
  247. }
  248. }
  249. /* push parameters and do the call */
  250. for (i=0; i<narg; i++)
  251. lua_pushobject(params[i]);
  252. if (lua_callfunction(f))
  253. { /* error */
  254. lua_error(NULL);
  255. }
  256. else
  257. { /* push results */
  258. Object r;
  259. arg = lua_getresult(1);
  260. luaI_packarg((arg == LUA_NOOBJECT)?NULL:luaI_Address(arg), &r);
  261. luaI_pushobject(&r);
  262. }
  263. }