inout.c 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220
  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.24 1995/10/23 13:54:11 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 = 0;
  25. char *lua_parsedfile;
  26. static FILE *fp;
  27. static char *st;
  28. /*
  29. ** Function to get the next character from the input file
  30. */
  31. static int fileinput (void)
  32. {
  33. return fgetc (fp);
  34. }
  35. /*
  36. ** Function to get the next character from the input string
  37. */
  38. static int stringinput (void)
  39. {
  40. return *st++;
  41. }
  42. /*
  43. ** Function to open a file to be input unit.
  44. ** Return 0 on success or error message on error.
  45. */
  46. char *lua_openfile (char *fn)
  47. {
  48. lua_setinput (fileinput);
  49. if (fn == NULL)
  50. {
  51. fp = stdin;
  52. fn = "(stdin)";
  53. }
  54. else
  55. fp = fopen (fn, "r");
  56. if (fp == NULL)
  57. {
  58. static char buff[255];
  59. sprintf(buff, "unable to open file `%.200s'", fn);
  60. return buff;
  61. }
  62. lua_linenumber = 1;
  63. lua_parsedfile = lua_constcreate(fn)->ts.str;
  64. return NULL;
  65. }
  66. /*
  67. ** Function to close an opened file
  68. */
  69. void lua_closefile (void)
  70. {
  71. if (fp != NULL && fp != stdin)
  72. {
  73. fclose (fp);
  74. fp = NULL;
  75. }
  76. }
  77. /*
  78. ** Function to open a string to be input unit
  79. */
  80. void lua_openstring (char *s)
  81. {
  82. lua_setinput (stringinput);
  83. st = s;
  84. lua_linenumber = 1;
  85. lua_parsedfile = lua_constcreate("(string)")->ts.str;
  86. }
  87. /*
  88. ** Function to close an opened string
  89. */
  90. void lua_closestring (void)
  91. {
  92. }
  93. /*
  94. ** Internal function: do a string
  95. */
  96. void lua_internaldostring (void)
  97. {
  98. lua_Object obj = lua_getparam (1);
  99. if (lua_isstring(obj) && !lua_dostring(lua_getstring(obj)))
  100. lua_pushnumber(1);
  101. else
  102. lua_pushnil();
  103. }
  104. /*
  105. ** Internal function: do a file
  106. */
  107. void lua_internaldofile (void)
  108. {
  109. lua_Object obj = lua_getparam (1);
  110. if (lua_isstring(obj) && !lua_dofile(lua_getstring(obj)))
  111. lua_pushnumber(1);
  112. else
  113. lua_pushnil();
  114. }
  115. /*
  116. ** Internal function: print object values
  117. */
  118. void lua_print (void)
  119. {
  120. int i=1;
  121. lua_Object obj;
  122. while ((obj=lua_getparam (i++)) != LUA_NOOBJECT)
  123. {
  124. if (lua_isnumber(obj)) printf("%g\n",lua_getnumber(obj));
  125. else if (lua_isstring(obj)) printf("%s\n",lua_getstring(obj));
  126. else if (lua_isfunction(obj)) printf("function: %p\n",(luaI_Address(obj))->value.tf);
  127. else if (lua_iscfunction(obj)) printf("cfunction: %p\n",lua_getcfunction(obj)
  128. );
  129. else if (lua_isuserdata(obj)) printf("userdata: %p\n",lua_getuserdata(obj));
  130. else if (lua_istable(obj)) printf("table: %p\n",avalue(luaI_Address(obj)));
  131. else if (lua_isnil(obj)) printf("nil\n");
  132. else printf("invalid value to print\n");
  133. }
  134. }
  135. /*
  136. ** Internal function: return an object type.
  137. */
  138. void luaI_type (void)
  139. {
  140. lua_Object o = lua_getparam(1);
  141. int t;
  142. if (o == LUA_NOOBJECT)
  143. lua_error("no parameter to function 'type'");
  144. t = lua_type(o);
  145. switch (t)
  146. {
  147. case LUA_T_NIL :
  148. lua_pushliteral("nil");
  149. break;
  150. case LUA_T_NUMBER :
  151. lua_pushliteral("number");
  152. break;
  153. case LUA_T_STRING :
  154. lua_pushliteral("string");
  155. break;
  156. case LUA_T_ARRAY :
  157. lua_pushliteral("table");
  158. break;
  159. case LUA_T_FUNCTION :
  160. case LUA_T_CFUNCTION :
  161. lua_pushliteral("function");
  162. break;
  163. default :
  164. lua_pushliteral("userdata");
  165. break;
  166. }
  167. lua_pushnumber(t);
  168. }
  169. /*
  170. ** Internal function: convert an object to a number
  171. */
  172. void lua_obj2number (void)
  173. {
  174. lua_Object o = lua_getparam(1);
  175. if (lua_isnumber(o))
  176. lua_pushobject(o);
  177. else if (lua_isstring(o))
  178. {
  179. char c;
  180. float f;
  181. if (sscanf(lua_getstring(o),"%f %c",&f,&c) == 1)
  182. lua_pushnumber(f);
  183. else
  184. lua_pushnil();
  185. }
  186. else
  187. lua_pushnil();
  188. }
  189. void luaI_error (void)
  190. {
  191. char *s = lua_getstring(lua_getparam(1));
  192. if (s == NULL) s = "(no message)";
  193. lua_error(s);
  194. }