inout.c 4.5 KB

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