inout.c 5.6 KB

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