inout.c 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280
  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.10 1994/11/09 18:09:22 roberto Exp roberto $";
  8. #include <stdio.h>
  9. #include <stdlib.h>
  10. #include <string.h>
  11. #include "opcode.h"
  12. #include "hash.h"
  13. #include "inout.h"
  14. #include "table.h"
  15. #include "tree.h"
  16. #include "lua.h"
  17. /* Exported variables */
  18. int lua_linenumber;
  19. int lua_debug;
  20. int lua_debugline = 0;
  21. /* Internal variables */
  22. #ifndef MAXFUNCSTACK
  23. #define MAXFUNCSTACK 64
  24. #endif
  25. static struct {
  26. char *file;
  27. int function;
  28. int line;
  29. } funcstack[MAXFUNCSTACK];
  30. static int nfuncstack=0;
  31. static FILE *fp;
  32. static char *st;
  33. /*
  34. ** Function to get the next character from the input file
  35. */
  36. static int fileinput (void)
  37. {
  38. return fgetc (fp);
  39. }
  40. /*
  41. ** Function to get the next character from the input string
  42. */
  43. static int stringinput (void)
  44. {
  45. return *st++;
  46. }
  47. /*
  48. ** Function to open a file to be input unit.
  49. ** Return 0 on success or error message on error.
  50. */
  51. char *lua_openfile (char *fn)
  52. {
  53. lua_linenumber = 1;
  54. lua_setinput (fileinput);
  55. fp = fopen (fn, "r");
  56. if (fp == NULL)
  57. {
  58. static char buff[32];
  59. sprintf(buff, "unable to open file %.10s", fn);
  60. return buff;
  61. }
  62. return lua_addfile (fn);
  63. }
  64. /*
  65. ** Function to close an opened file
  66. */
  67. void lua_closefile (void)
  68. {
  69. if (fp != NULL)
  70. {
  71. lua_delfile();
  72. fclose (fp);
  73. fp = NULL;
  74. }
  75. }
  76. /*
  77. ** Function to open a string to be input unit
  78. */
  79. char *lua_openstring (char *s)
  80. {
  81. lua_linenumber = 1;
  82. lua_setinput (stringinput);
  83. st = s;
  84. {
  85. char sn[64];
  86. sprintf (sn, "String: %10.10s...", s);
  87. return lua_addfile (sn);
  88. }
  89. }
  90. /*
  91. ** Function to close an opened string
  92. */
  93. void lua_closestring (void)
  94. {
  95. lua_delfile();
  96. }
  97. /*
  98. ** Called to execute SETFUNCTION opcode, this function pushs a function into
  99. ** function stack. Return 0 on success or 1 on error.
  100. */
  101. int lua_pushfunction (char *file, int function)
  102. {
  103. if (nfuncstack >= MAXFUNCSTACK-1)
  104. {
  105. lua_error ("function stack overflow");
  106. return 1;
  107. }
  108. funcstack[nfuncstack].function = function;
  109. funcstack[nfuncstack].file = file;
  110. funcstack[nfuncstack].line= lua_debugline;
  111. nfuncstack++;
  112. return 0;
  113. }
  114. /*
  115. ** Called to execute RESET opcode, this function pops a function from
  116. ** function stack.
  117. */
  118. void lua_popfunction (void)
  119. {
  120. --nfuncstack;
  121. lua_debugline = funcstack[nfuncstack].line;
  122. }
  123. /*
  124. ** Report bug building a message and sending it to lua_error function.
  125. */
  126. void lua_reportbug (char *s)
  127. {
  128. char msg[1024];
  129. strcpy (msg, s);
  130. if (lua_debugline != 0)
  131. {
  132. int i;
  133. if (nfuncstack > 0)
  134. {
  135. sprintf (strchr(msg,0),
  136. "\n\tin statement begining at line %d in function \"%s\" of file \"%s\"",
  137. lua_debugline, lua_constant[funcstack[nfuncstack-1].function],
  138. funcstack[nfuncstack-1].file);
  139. sprintf (strchr(msg,0), "\n\tactive stack\n");
  140. for (i=nfuncstack-1; i>=0; i--)
  141. sprintf (strchr(msg,0), "\t-> function \"%s\" of file \"%s\"\n",
  142. lua_constant[funcstack[i].function],
  143. funcstack[i].file);
  144. }
  145. else
  146. {
  147. sprintf (strchr(msg,0),
  148. "\n\tin statement begining at line %d of file \"%s\"",
  149. lua_debugline, lua_filename());
  150. }
  151. }
  152. lua_error (msg);
  153. }
  154. /*
  155. ** Internal function: do a string
  156. */
  157. void lua_internaldostring (void)
  158. {
  159. lua_Object obj = lua_getparam (1);
  160. if (lua_isstring(obj) && !lua_dostring(lua_getstring(obj)))
  161. lua_pushnumber(1);
  162. else
  163. lua_pushnil();
  164. }
  165. /*
  166. ** Internal function: do a file
  167. */
  168. void lua_internaldofile (void)
  169. {
  170. lua_Object obj = lua_getparam (1);
  171. if (lua_isstring(obj) && !lua_dofile(lua_getstring(obj)))
  172. lua_pushnumber(1);
  173. else
  174. lua_pushnil();
  175. }
  176. /*
  177. ** Internal function: print object values
  178. */
  179. void lua_print (void)
  180. {
  181. int i=1;
  182. lua_Object obj;
  183. while ((obj=lua_getparam (i++)) != 0)
  184. {
  185. if (lua_isnumber(obj)) printf("%g\n",lua_getnumber(obj));
  186. else if (lua_isstring(obj)) printf("%s\n",lua_getstring(obj));
  187. else if (lua_isfunction(obj)) printf("function: %p\n",bvalue(luaI_Address(obj)));
  188. else if (lua_iscfunction(obj)) printf("cfunction: %p\n",lua_getcfunction(obj)
  189. );
  190. else if (lua_isuserdata(obj)) printf("userdata: %p\n",lua_getuserdata(obj));
  191. else if (lua_istable(obj)) printf("table: %p\n",avalue(luaI_Address(obj)));
  192. else if (lua_isnil(obj)) printf("nil\n");
  193. else printf("invalid value to print\n");
  194. }
  195. }
  196. /*
  197. ** Internal function: return an object type.
  198. */
  199. void luaI_type (void)
  200. {
  201. lua_Object o = lua_getparam(1);
  202. if (o == 0)
  203. lua_error("no parameter to function 'type'");
  204. switch (lua_type(o))
  205. {
  206. case LUA_T_NIL :
  207. lua_pushstring("nil");
  208. break;
  209. case LUA_T_NUMBER :
  210. lua_pushstring("number");
  211. break;
  212. case LUA_T_STRING :
  213. lua_pushstring("string");
  214. break;
  215. case LUA_T_ARRAY :
  216. lua_pushstring("table");
  217. break;
  218. case LUA_T_FUNCTION :
  219. lua_pushstring("function");
  220. break;
  221. case LUA_T_CFUNCTION :
  222. lua_pushstring("cfunction");
  223. break;
  224. default :
  225. lua_pushstring("userdata");
  226. break;
  227. }
  228. }
  229. /*
  230. ** Internal function: convert an object to a number
  231. */
  232. void lua_obj2number (void)
  233. {
  234. lua_Object o = lua_getparam(1);
  235. if (lua_isnumber(o))
  236. lua_pushobject(o);
  237. else if (lua_isstring(o))
  238. {
  239. char c;
  240. float f;
  241. if (sscanf(lua_getstring(o),"%f %c",&f,&c) == 1)
  242. lua_pushnumber(f);
  243. else
  244. lua_pushnil();
  245. }
  246. else
  247. lua_pushnil();
  248. }
  249. void luaI_error (void)
  250. {
  251. char *s = lua_getstring(lua_getparam(1));
  252. if (s == NULL) s = "(no message)";
  253. lua_reportbug(s);
  254. }