inout.c 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308
  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.19 1995/05/02 18:43:03 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;
  25. Word lua_debugline = 0;
  26. /* Internal variables */
  27. typedef struct FuncStackNode {
  28. struct FuncStackNode *next;
  29. char *file;
  30. Word function;
  31. Word line;
  32. } FuncStackNode;
  33. static FuncStackNode *funcStack = NULL;
  34. static Word nfuncstack=0;
  35. static FILE *fp;
  36. static char *st;
  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[255];
  63. sprintf(buff, "unable to open file %.230s", 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.
  104. */
  105. void lua_pushfunction (char *file, Word function)
  106. {
  107. FuncStackNode *newNode;
  108. if (nfuncstack++ >= MAXFUNCSTACK)
  109. {
  110. lua_error("function stack overflow");
  111. }
  112. newNode = new(FuncStackNode);
  113. newNode->function = function;
  114. newNode->file = file;
  115. newNode->line= lua_debugline;
  116. newNode->next = funcStack;
  117. funcStack = newNode;
  118. }
  119. /*
  120. ** Called to execute RESET opcode, this function pops a function from
  121. ** function stack.
  122. */
  123. void lua_popfunction (void)
  124. {
  125. FuncStackNode *temp = funcStack;
  126. if (temp == NULL) return;
  127. --nfuncstack;
  128. lua_debugline = temp->line;
  129. funcStack = temp->next;
  130. luaI_free(temp);
  131. }
  132. /*
  133. ** Report bug building a message and pushing it on the stack.
  134. */
  135. void luaI_reportbug (char *s, int err)
  136. {
  137. char msg[MAXMESSAGE];
  138. strcpy (msg, s);
  139. if (lua_debugline != 0)
  140. {
  141. if (funcStack)
  142. {
  143. FuncStackNode *func = funcStack;
  144. int line = lua_debugline;
  145. sprintf (strchr(msg,0), "\n\tactive stack:\n");
  146. do
  147. {
  148. sprintf (strchr(msg,0),
  149. "\t-> function \"%s\" at file \"%s\":%u\n",
  150. lua_constant[func->function]->str, func->file, line);
  151. line = func->line;
  152. func = func->next;
  153. if (err) lua_popfunction();
  154. } while (func);
  155. }
  156. else
  157. {
  158. sprintf (strchr(msg,0),
  159. "\n\tin statement begining at line %u of file \"%s\"",
  160. lua_debugline, lua_filename());
  161. }
  162. }
  163. lua_pushstring(msg);
  164. }
  165. /*
  166. ** Internal function: do a string
  167. */
  168. void lua_internaldostring (void)
  169. {
  170. lua_Object obj = lua_getparam (1);
  171. if (lua_isstring(obj) && !lua_dostring(lua_getstring(obj)))
  172. lua_pushnumber(1);
  173. else
  174. lua_pushnil();
  175. }
  176. /*
  177. ** Internal function: do a file
  178. */
  179. void lua_internaldofile (void)
  180. {
  181. lua_Object obj = lua_getparam (1);
  182. if (lua_isstring(obj) && !lua_dofile(lua_getstring(obj)))
  183. lua_pushnumber(1);
  184. else
  185. lua_pushnil();
  186. }
  187. /*
  188. ** Internal function: print object values
  189. */
  190. void lua_print (void)
  191. {
  192. int i=1;
  193. lua_Object obj;
  194. while ((obj=lua_getparam (i++)) != LUA_NOOBJECT)
  195. {
  196. if (lua_isnumber(obj)) printf("%g\n",lua_getnumber(obj));
  197. else if (lua_isstring(obj)) printf("%s\n",lua_getstring(obj));
  198. else if (lua_isfunction(obj)) printf("function: %p\n",bvalue(luaI_Address(obj)));
  199. else if (lua_iscfunction(obj)) printf("cfunction: %p\n",lua_getcfunction(obj)
  200. );
  201. else if (lua_isuserdata(obj)) printf("userdata: %p\n",lua_getuserdata(obj));
  202. else if (lua_istable(obj)) printf("table: %p\n",avalue(luaI_Address(obj)));
  203. else if (lua_isnil(obj)) printf("nil\n");
  204. else printf("invalid value to print\n");
  205. }
  206. }
  207. /*
  208. ** Internal function: return an object type.
  209. */
  210. void luaI_type (void)
  211. {
  212. lua_Object o = lua_getparam(1);
  213. int t;
  214. if (o == LUA_NOOBJECT)
  215. lua_error("no parameter to function 'type'");
  216. t = lua_type(o);
  217. switch (t)
  218. {
  219. case LUA_T_NIL :
  220. lua_pushliteral("nil");
  221. break;
  222. case LUA_T_NUMBER :
  223. lua_pushliteral("number");
  224. break;
  225. case LUA_T_STRING :
  226. lua_pushliteral("string");
  227. break;
  228. case LUA_T_ARRAY :
  229. lua_pushliteral("table");
  230. break;
  231. case LUA_T_FUNCTION :
  232. lua_pushliteral("function");
  233. break;
  234. case LUA_T_CFUNCTION :
  235. lua_pushliteral("cfunction");
  236. break;
  237. default :
  238. lua_pushliteral("userdata");
  239. break;
  240. }
  241. lua_pushnumber(t);
  242. }
  243. /*
  244. ** Internal function: convert an object to a number
  245. */
  246. void lua_obj2number (void)
  247. {
  248. lua_Object o = lua_getparam(1);
  249. if (lua_isnumber(o))
  250. lua_pushobject(o);
  251. else if (lua_isstring(o))
  252. {
  253. char c;
  254. float f;
  255. if (sscanf(lua_getstring(o),"%f %c",&f,&c) == 1)
  256. lua_pushnumber(f);
  257. else
  258. lua_pushnil();
  259. }
  260. else
  261. lua_pushnil();
  262. }
  263. void luaI_error (void)
  264. {
  265. char *s = lua_getstring(lua_getparam(1));
  266. if (s == NULL) s = "(no message)";
  267. lua_error(s);
  268. }
  269. void luaI_getstack (void)
  270. {
  271. char *s = lua_getstring(lua_getparam(1));
  272. if (s == NULL) s = "";
  273. luaI_reportbug(s, 0);
  274. }