inout.c 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259
  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.28 1996/01/26 16:52:47 roberto Exp roberto $";
  8. #include <stdio.h>
  9. #include <stdlib.h>
  10. #include <string.h>
  11. #include "mem.h"
  12. #include "opcode.h"
  13. #include "hash.h"
  14. #include "inout.h"
  15. #include "table.h"
  16. #include "tree.h"
  17. #include "lua.h"
  18. #ifndef MAXFUNCSTACK
  19. #define MAXFUNCSTACK 100
  20. #endif
  21. #define MAXMESSAGE MAXFUNCSTACK*80
  22. /* Exported variables */
  23. Word lua_linenumber;
  24. char *lua_parsedfile;
  25. static FILE *fp;
  26. static char *st;
  27. /*
  28. ** Function to get the next character from the input file
  29. */
  30. static int fileinput (void)
  31. {
  32. return fgetc (fp);
  33. }
  34. /*
  35. ** Function to get the next character from the input string
  36. */
  37. static int stringinput (void)
  38. {
  39. return *st++;
  40. }
  41. /*
  42. ** Function to open a file to be input unit.
  43. ** Return 0 on success or 1 error.
  44. */
  45. int lua_openfile (char *fn)
  46. {
  47. lua_setinput (fileinput);
  48. if (fn == NULL)
  49. {
  50. fp = stdin;
  51. fn = "(stdin)";
  52. }
  53. else
  54. fp = fopen (fn, "r");
  55. if (fp == NULL)
  56. return 1;
  57. lua_linenumber = 1;
  58. lua_parsedfile = lua_constcreate(fn)->ts.str;
  59. return 0;
  60. }
  61. /*
  62. ** Function to close an opened file
  63. */
  64. void lua_closefile (void)
  65. {
  66. if (fp != NULL && fp != stdin)
  67. {
  68. fclose (fp);
  69. fp = NULL;
  70. }
  71. }
  72. /*
  73. ** Function to open a string to be input unit
  74. */
  75. void lua_openstring (char *s)
  76. {
  77. lua_setinput (stringinput);
  78. st = s;
  79. lua_linenumber = 1;
  80. lua_parsedfile = lua_constcreate("(string)")->ts.str;
  81. }
  82. /*
  83. ** Function to close an opened string
  84. */
  85. void lua_closestring (void)
  86. {
  87. }
  88. /*
  89. ** Internal function: do a string
  90. */
  91. void lua_internaldostring (void)
  92. {
  93. lua_Object obj = lua_getparam (1);
  94. if (lua_isstring(obj) && !lua_dostring(lua_getstring(obj)))
  95. lua_pushnumber(1);
  96. else
  97. lua_pushnil();
  98. }
  99. /*
  100. ** Internal function: do a file
  101. */
  102. void lua_internaldofile (void)
  103. {
  104. lua_Object obj = lua_getparam (1);
  105. char *fname = NULL;
  106. if (lua_isstring(obj))
  107. fname = lua_getstring(obj);
  108. else if (obj != LUA_NOOBJECT)
  109. lua_error("invalid argument to function `dofile'");
  110. /* else fname = NULL */
  111. if (!lua_dofile(fname))
  112. lua_pushnumber(1);
  113. else
  114. lua_pushnil();
  115. }
  116. static char *tostring (lua_Object obj)
  117. {
  118. static char buff[20];
  119. if (lua_isstring(obj))
  120. return lua_getstring(obj);
  121. if (lua_isnumber(obj))
  122. sprintf(buff, "%g", lua_getnumber(obj));
  123. else if (lua_isfunction(obj))
  124. sprintf(buff, "function: %p", (luaI_Address(obj))->value.tf);
  125. else if (lua_iscfunction(obj))
  126. sprintf(buff, "cfunction: %p", lua_getcfunction(obj));
  127. else if (lua_isuserdata(obj))
  128. sprintf(buff, "userdata: %p", lua_getuserdata(obj));
  129. else if (lua_istable(obj))
  130. sprintf(buff, "table: %p", avalue(luaI_Address(obj)));
  131. else if (lua_isnil(obj))
  132. sprintf(buff, "nil");
  133. else buff[0] = 0;
  134. return buff;
  135. }
  136. void luaI_tostring (void)
  137. {
  138. lua_pushstring(tostring(lua_getparam(1)));
  139. }
  140. void luaI_print (void)
  141. {
  142. int i = 1;
  143. lua_Object obj;
  144. while ((obj = lua_getparam(i++)) != LUA_NOOBJECT)
  145. printf("%s\n", tostring(obj));
  146. }
  147. /*
  148. ** Internal function: return an object type.
  149. */
  150. void luaI_type (void)
  151. {
  152. lua_Object o = lua_getparam(1);
  153. int t;
  154. if (o == LUA_NOOBJECT)
  155. lua_error("no parameter to function 'type'");
  156. t = lua_type(o);
  157. switch (t)
  158. {
  159. case LUA_T_NIL :
  160. lua_pushliteral("nil");
  161. break;
  162. case LUA_T_NUMBER :
  163. lua_pushliteral("number");
  164. break;
  165. case LUA_T_STRING :
  166. lua_pushliteral("string");
  167. break;
  168. case LUA_T_ARRAY :
  169. lua_pushliteral("table");
  170. break;
  171. case LUA_T_FUNCTION :
  172. case LUA_T_CFUNCTION :
  173. lua_pushliteral("function");
  174. break;
  175. default :
  176. lua_pushliteral("userdata");
  177. break;
  178. }
  179. lua_pushnumber(t);
  180. }
  181. /*
  182. ** Internal function: convert an object to a number
  183. */
  184. void lua_obj2number (void)
  185. {
  186. lua_Object o = lua_getparam(1);
  187. if (lua_isnumber(o))
  188. lua_pushobject(o);
  189. else if (lua_isstring(o))
  190. {
  191. char c;
  192. float f;
  193. if (sscanf(lua_getstring(o),"%f %c",&f,&c) == 1)
  194. lua_pushnumber(f);
  195. else
  196. lua_pushnil();
  197. }
  198. else
  199. lua_pushnil();
  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. if (!lua_isstring(name))
  218. lua_error("incorrect argument to function `setglobal'");
  219. lua_pushobject(value);
  220. lua_storeglobal(lua_getstring(name));
  221. lua_pushobject(value); /* return given value */
  222. }
  223. void luaI_getglobal (void)
  224. {
  225. lua_Object name = lua_getparam(1);
  226. if (!lua_isstring(name))
  227. lua_error("incorrect argument to function `getglobal'");
  228. lua_pushobject(lua_getglobal(lua_getstring(name)));
  229. }