inout.c 4.8 KB

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