inout.c 5.9 KB

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