inout.c 5.3 KB

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