inout.c 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302
  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.41 1996/09/24 17:30:28 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. static int passresults (void)
  99. {
  100. int arg = 0;
  101. lua_Object obj;
  102. while ((obj = lua_getresult(++arg)) != LUA_NOOBJECT)
  103. lua_pushobject(obj);
  104. return arg-1;
  105. }
  106. /*
  107. ** Internal function: do a string
  108. */
  109. void lua_internaldostring (void)
  110. {
  111. lua_Object obj = lua_getparam (1);
  112. if (lua_isstring(obj) && lua_dostring(lua_getstring(obj)) == 0)
  113. if (passresults() == 0)
  114. lua_pushuserdata(NULL); /* at least one result to signal no errors */
  115. }
  116. /*
  117. ** Internal function: do a file
  118. */
  119. void lua_internaldofile (void)
  120. {
  121. lua_Object obj = lua_getparam (1);
  122. char *fname = NULL;
  123. if (lua_isstring(obj))
  124. fname = lua_getstring(obj);
  125. else if (obj != LUA_NOOBJECT)
  126. lua_error("invalid argument to function `dofile'");
  127. /* else fname = NULL */
  128. if (lua_dofile(fname) == 0)
  129. if (passresults() == 0)
  130. lua_pushuserdata(NULL); /* at least one result to signal no errors */
  131. }
  132. static char *tostring (lua_Object obj)
  133. {
  134. char *buff = luaI_buffer(20);
  135. if (lua_isstring(obj)) /* get strings and numbers */
  136. return lua_getstring(obj);
  137. else switch(lua_type(obj))
  138. {
  139. case LUA_T_FUNCTION:
  140. sprintf(buff, "function: %p", (luaI_Address(obj))->value.tf);
  141. break;
  142. case LUA_T_CFUNCTION:
  143. sprintf(buff, "cfunction: %p", lua_getcfunction(obj));
  144. break;
  145. case LUA_T_ARRAY:
  146. sprintf(buff, "table: %p", avalue(luaI_Address(obj)));
  147. break;
  148. case LUA_T_NIL:
  149. sprintf(buff, "nil");
  150. break;
  151. default:
  152. sprintf(buff, "userdata: %p", lua_getuserdata(obj));
  153. break;
  154. }
  155. return buff;
  156. }
  157. void luaI_tostring (void)
  158. {
  159. lua_pushstring(tostring(lua_getparam(1)));
  160. }
  161. void luaI_print (void)
  162. {
  163. int i = 1;
  164. lua_Object obj;
  165. while ((obj = lua_getparam(i++)) != LUA_NOOBJECT)
  166. printf("%s\n", tostring(obj));
  167. }
  168. /*
  169. ** Internal function: return an object type.
  170. */
  171. void luaI_type (void)
  172. {
  173. lua_Object o = lua_getparam(1);
  174. int t;
  175. if (o == LUA_NOOBJECT)
  176. lua_error("no parameter to function 'type'");
  177. t = lua_type(o);
  178. switch (t)
  179. {
  180. case LUA_T_NIL :
  181. lua_pushliteral("nil");
  182. break;
  183. case LUA_T_NUMBER :
  184. lua_pushliteral("number");
  185. break;
  186. case LUA_T_STRING :
  187. lua_pushliteral("string");
  188. break;
  189. case LUA_T_ARRAY :
  190. lua_pushliteral("table");
  191. break;
  192. case LUA_T_FUNCTION :
  193. case LUA_T_CFUNCTION :
  194. lua_pushliteral("function");
  195. break;
  196. default :
  197. lua_pushliteral("userdata");
  198. break;
  199. }
  200. lua_pushnumber(t);
  201. }
  202. /*
  203. ** Internal function: convert an object to a number
  204. */
  205. void lua_obj2number (void)
  206. {
  207. lua_Object o = lua_getparam(1);
  208. if (lua_isnumber(o))
  209. lua_pushnumber(lua_getnumber(o));
  210. }
  211. void luaI_error (void)
  212. {
  213. char *s = lua_getstring(lua_getparam(1));
  214. if (s == NULL) s = "(no message)";
  215. lua_error(s);
  216. }
  217. void luaI_assert (void)
  218. {
  219. lua_Object p = lua_getparam(1);
  220. if (p == LUA_NOOBJECT || lua_isnil(p))
  221. lua_error("assertion failed!");
  222. }
  223. void luaI_setglobal (void)
  224. {
  225. lua_Object name = lua_getparam(1);
  226. lua_Object value = lua_getparam(2);
  227. check_arg(lua_isstring(name), "setglobal");
  228. lua_pushobject(value);
  229. lua_storeglobal(lua_getstring(name));
  230. lua_pushobject(value); /* return given value */
  231. }
  232. void luaI_getglobal (void)
  233. {
  234. lua_Object name = lua_getparam(1);
  235. check_arg(lua_isstring(name), "getglobal");
  236. lua_pushobject(lua_getglobal(lua_getstring(name)));
  237. }
  238. #define MAXPARAMS 256
  239. void luaI_call (void)
  240. {
  241. lua_Object f = lua_getparam(1);
  242. lua_Object arg = lua_getparam(2);
  243. lua_Object temp, params[MAXPARAMS];
  244. int narg, i;
  245. check_arg(lua_istable(arg), "call");
  246. check_arg(lua_isfunction(f), "call");
  247. /* narg = arg.n */
  248. lua_pushobject(arg);
  249. lua_pushstring("n");
  250. temp = lua_getsubscript();
  251. narg = lua_isnumber(temp) ? lua_getnumber(temp) : MAXPARAMS+1;
  252. /* read arg[1...n] */
  253. for (i=0; i<narg; i++) {
  254. if (i>=MAXPARAMS)
  255. lua_error("argument list too long in function `call'");
  256. lua_pushobject(arg);
  257. lua_pushnumber(i+1);
  258. params[i] = lua_getsubscript();
  259. if (narg == MAXPARAMS+1 && lua_isnil(params[i])) {
  260. narg = i;
  261. break;
  262. }
  263. }
  264. /* push parameters and do the call */
  265. for (i=0; i<narg; i++)
  266. lua_pushobject(params[i]);
  267. if (lua_callfunction(f))
  268. lua_error(NULL);
  269. else
  270. passresults();
  271. }