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