inout.c 7.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367
  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.56 1997/04/06 14:08:08 roberto Exp roberto $";
  8. #include <stdio.h>
  9. #include <string.h>
  10. #include "auxlib.h"
  11. #include "lex.h"
  12. #include "opcode.h"
  13. #include "inout.h"
  14. #include "table.h"
  15. #include "tree.h"
  16. #include "lua.h"
  17. #include "hash.h"
  18. #include "luamem.h"
  19. #include "fallback.h"
  20. #include "luamem.h"
  21. /* Exported variables */
  22. Word lua_linenumber;
  23. char *lua_parsedfile;
  24. char *luaI_typenames[] = { /* ORDER LUA_T */
  25. "userdata", "line", "cmark", "mark", "function",
  26. "function", "table", "string", "number", "nil",
  27. NULL
  28. };
  29. static FILE *fp;
  30. static char *st;
  31. /*
  32. ** Function to get the next character from the input file
  33. */
  34. static int fileinput (void)
  35. {
  36. int c = fgetc(fp);
  37. return (c == EOF) ? 0 : c;
  38. }
  39. /*
  40. ** Function to get the next character from the input string
  41. */
  42. static int stringinput (void)
  43. {
  44. return *st++;
  45. }
  46. /*
  47. ** Function to open a file to be input unit.
  48. ** Return the file.
  49. */
  50. FILE *lua_openfile (char *fn)
  51. {
  52. lua_setinput (fileinput);
  53. if (fn == NULL)
  54. {
  55. fp = stdin;
  56. fn = "(stdin)";
  57. }
  58. else
  59. fp = fopen (fn, "r");
  60. if (fp == NULL)
  61. return NULL;
  62. lua_parsedfile = luaI_createfixedstring(fn)->str;
  63. return fp;
  64. }
  65. /*
  66. ** Function to close an opened file
  67. */
  68. void lua_closefile (void)
  69. {
  70. if (fp != NULL && fp != stdin)
  71. {
  72. fclose (fp);
  73. fp = NULL;
  74. }
  75. }
  76. /*
  77. ** Function to open a string to be input unit
  78. */
  79. #define SIZE_PREF 20 /* size of string prefix to appear in error messages */
  80. void lua_openstring (char *s)
  81. {
  82. char buff[SIZE_PREF+25];
  83. lua_setinput(stringinput);
  84. st = s;
  85. sprintf(buff, "(dostring) >> %.20s%s", s,
  86. (strlen(s) > SIZE_PREF) ? "..." : "");
  87. lua_parsedfile = luaI_createfixedstring(buff)->str;
  88. }
  89. /*
  90. ** Function to close an opened string
  91. */
  92. void lua_closestring (void)
  93. {
  94. }
  95. static int passresults (void)
  96. {
  97. int arg = 0;
  98. lua_Object obj;
  99. while ((obj = lua_getresult(++arg)) != LUA_NOOBJECT)
  100. lua_pushobject(obj);
  101. return arg-1;
  102. }
  103. /*
  104. ** Internal function: do a string
  105. */
  106. static void lua_internaldostring (void)
  107. {
  108. if (lua_dostring(luaL_check_string(1)) == 0)
  109. if (passresults() == 0)
  110. lua_pushuserdata(NULL); /* at least one result to signal no errors */
  111. }
  112. /*
  113. ** Internal function: do a file
  114. */
  115. static void lua_internaldofile (void)
  116. {
  117. char *fname = luaL_opt_string(1, NULL);
  118. if (lua_dofile(fname) == 0)
  119. if (passresults() == 0)
  120. lua_pushuserdata(NULL); /* at least one result to signal no errors */
  121. }
  122. static char *tostring (lua_Object obj)
  123. {
  124. TObject *o = luaI_Address(obj);
  125. switch (ttype(o)) {
  126. case LUA_T_NUMBER: case LUA_T_STRING:
  127. return lua_getstring(obj);
  128. case LUA_T_ARRAY: case LUA_T_FUNCTION:
  129. case LUA_T_CFUNCTION: case LUA_T_NIL:
  130. return luaI_typenames[-ttype(o)];
  131. case LUA_T_USERDATA: {
  132. char *buff = luaI_buffer(100);
  133. int size = o->value.ts->size;
  134. int i;
  135. strcpy(buff, "userdata: ");
  136. if (size > 10) size = 10;
  137. for (i=0; i<size; i++)
  138. sprintf(buff+strlen(buff), "%.2X",
  139. (int)(unsigned char)o->value.ts->str[i]);
  140. return buff;
  141. }
  142. default: return "<unknown object>";
  143. }
  144. }
  145. static void luaI_tostring (void)
  146. {
  147. lua_pushstring(tostring(lua_getparam(1)));
  148. }
  149. static void luaI_print (void)
  150. {
  151. int i = 1;
  152. lua_Object obj;
  153. while ((obj = lua_getparam(i++)) != LUA_NOOBJECT)
  154. printf("%s\n", tostring(obj));
  155. }
  156. static void luaI_type (void)
  157. {
  158. lua_Object o = lua_getparam(1);
  159. luaL_arg_check(o != LUA_NOOBJECT, 1, "no argument");
  160. lua_pushstring(luaI_typenames[-ttype(luaI_Address(o))]);
  161. lua_pushnumber(lua_tag(o));
  162. }
  163. /*
  164. ** Internal function: convert an object to a number
  165. */
  166. static void lua_obj2number (void)
  167. {
  168. lua_Object o = lua_getparam(1);
  169. if (lua_isnumber(o))
  170. lua_pushnumber(lua_getnumber(o));
  171. }
  172. static void luaI_error (void)
  173. {
  174. char *s = lua_getstring(lua_getparam(1));
  175. if (s == NULL) s = "(no message)";
  176. lua_error(s);
  177. }
  178. static void luaI_assert (void)
  179. {
  180. lua_Object p = lua_getparam(1);
  181. if (p == LUA_NOOBJECT || lua_isnil(p))
  182. lua_error("assertion failed!");
  183. }
  184. static void luaI_setglobal (void)
  185. {
  186. lua_Object value = lua_getparam(2);
  187. luaL_arg_check(value != LUA_NOOBJECT, 2, NULL);
  188. lua_pushobject(value);
  189. lua_setglobal(luaL_check_string(1));
  190. lua_pushobject(value); /* return given value */
  191. }
  192. static void luaI_rawsetglobal (void)
  193. {
  194. lua_Object value = lua_getparam(2);
  195. luaL_arg_check(value != LUA_NOOBJECT, 2, NULL);
  196. lua_pushobject(value);
  197. lua_rawsetglobal(luaL_check_string(1));
  198. lua_pushobject(value); /* return given value */
  199. }
  200. static void luaI_getglobal (void)
  201. {
  202. lua_pushobject(lua_getglobal(luaL_check_string(1)));
  203. }
  204. static void luaI_rawgetglobal (void)
  205. {
  206. lua_pushobject(lua_rawgetglobal(luaL_check_string(1)));
  207. }
  208. static void luatag (void)
  209. {
  210. lua_pushnumber(lua_tag(lua_getparam(1)));
  211. }
  212. #define MAXPARAMS 256
  213. static void luaI_call (void)
  214. {
  215. lua_Object f = lua_getparam(1);
  216. lua_Object arg = lua_getparam(2);
  217. lua_Object temp, params[MAXPARAMS];
  218. int narg, i;
  219. luaL_arg_check(lua_isfunction(f), 1, "function expected");
  220. luaL_arg_check(lua_istable(arg), 2, "table expected");
  221. /* narg = arg.n */
  222. lua_pushobject(arg);
  223. lua_pushstring("n");
  224. temp = lua_getsubscript();
  225. narg = lua_isnumber(temp) ? lua_getnumber(temp) : MAXPARAMS+1;
  226. /* read arg[1...n] */
  227. for (i=0; i<narg; i++) {
  228. if (i>=MAXPARAMS)
  229. lua_error("argument list too long in function `call'");
  230. lua_pushobject(arg);
  231. lua_pushnumber(i+1);
  232. params[i] = lua_getsubscript();
  233. if (narg == MAXPARAMS+1 && lua_isnil(params[i])) {
  234. narg = i;
  235. break;
  236. }
  237. }
  238. /* push parameters and do the call */
  239. for (i=0; i<narg; i++)
  240. lua_pushobject(params[i]);
  241. if (lua_callfunction(f))
  242. lua_error(NULL);
  243. else
  244. passresults();
  245. }
  246. static void luaIl_settag (void)
  247. {
  248. lua_Object o = lua_getparam(1);
  249. luaL_arg_check(o != LUA_NOOBJECT, 1, NULL);
  250. lua_pushobject(o);
  251. lua_settag(luaL_check_number(2));
  252. }
  253. static void luaIl_newtag (void)
  254. {
  255. lua_pushnumber(lua_newtag());
  256. }
  257. static void rawgettable (void)
  258. {
  259. lua_Object t = lua_getparam(1);
  260. lua_Object i = lua_getparam(2);
  261. luaL_arg_check(t != LUA_NOOBJECT, 1, NULL);
  262. luaL_arg_check(i != LUA_NOOBJECT, 2, NULL);
  263. lua_pushobject(t);
  264. lua_pushobject(i);
  265. lua_pushobject(lua_rawgettable());
  266. }
  267. static void rawsettable (void)
  268. {
  269. lua_Object t = lua_getparam(1);
  270. lua_Object i = lua_getparam(2);
  271. lua_Object v = lua_getparam(3);
  272. luaL_arg_check(t != LUA_NOOBJECT && i != LUA_NOOBJECT && v != LUA_NOOBJECT,
  273. 0, NULL);
  274. lua_pushobject(t);
  275. lua_pushobject(i);
  276. lua_pushobject(v);
  277. lua_rawsettable();
  278. }
  279. /*
  280. ** Internal functions
  281. */
  282. static struct {
  283. char *name;
  284. lua_CFunction func;
  285. } int_funcs[] = {
  286. {"assert", luaI_assert},
  287. {"call", luaI_call},
  288. {"dofile", lua_internaldofile},
  289. {"dostring", lua_internaldostring},
  290. {"error", luaI_error},
  291. {"getglobal", luaI_getglobal},
  292. {"newtag", luaIl_newtag},
  293. {"next", lua_next},
  294. {"nextvar", luaI_nextvar},
  295. {"print", luaI_print},
  296. {"rawgetglobal", luaI_rawgetglobal},
  297. {"rawgettable", rawgettable},
  298. {"rawsetglobal", luaI_rawsetglobal},
  299. {"rawsettable", rawsettable},
  300. {"seterrormethod", luaI_seterrormethod},
  301. {"setfallback", luaI_setfallback},
  302. {"setglobal", luaI_setglobal},
  303. {"settagmethod", luaI_settagmethod},
  304. {"gettagmethod", luaI_gettagmethod},
  305. {"settag", luaIl_settag},
  306. {"tonumber", lua_obj2number},
  307. {"tostring", luaI_tostring},
  308. {"tag", luatag},
  309. {"type", luaI_type}
  310. };
  311. #define INTFUNCSIZE (sizeof(int_funcs)/sizeof(int_funcs[0]))
  312. void luaI_predefine (void)
  313. {
  314. int i;
  315. Word n;
  316. for (i=0; i<INTFUNCSIZE; i++) {
  317. n = luaI_findsymbolbyname(int_funcs[i].name);
  318. s_ttype(n) = LUA_T_CFUNCTION; s_fvalue(n) = int_funcs[i].func;
  319. }
  320. n = luaI_findsymbolbyname("_VERSION_");
  321. s_ttype(n) = LUA_T_STRING; s_tsvalue(n) = lua_createstring(LUA_VERSION);
  322. }