inout.c 4.8 KB

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