inout.c 8.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368
  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.53 1997/04/02 22:53:35 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. static char *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. strcpy(buff, "(dostring) >> ");
  86. strncat(buff, s, SIZE_PREF);
  87. if (strlen(s) > SIZE_PREF) strcat(buff, "...");
  88. lua_parsedfile = luaI_createfixedstring(buff)->str;
  89. }
  90. /*
  91. ** Function to close an opened string
  92. */
  93. void lua_closestring (void)
  94. {
  95. }
  96. static int passresults (void)
  97. {
  98. int arg = 0;
  99. lua_Object obj;
  100. while ((obj = lua_getresult(++arg)) != LUA_NOOBJECT)
  101. lua_pushobject(obj);
  102. return arg-1;
  103. }
  104. /*
  105. ** Internal function: do a string
  106. */
  107. static void lua_internaldostring (void)
  108. {
  109. if (lua_dostring(luaL_check_string(1, "dostring")) == 0)
  110. if (passresults() == 0)
  111. lua_pushuserdata(NULL); /* at least one result to signal no errors */
  112. }
  113. /*
  114. ** Internal function: do a file
  115. */
  116. static void lua_internaldofile (void)
  117. {
  118. char *fname = luaL_opt_string(1, NULL, "dofile");
  119. if (lua_dofile(fname) == 0)
  120. if (passresults() == 0)
  121. lua_pushuserdata(NULL); /* at least one result to signal no errors */
  122. }
  123. static char *tostring (lua_Object obj)
  124. {
  125. TObject *o = luaI_Address(obj);
  126. switch (ttype(o)) {
  127. case LUA_T_NUMBER: case LUA_T_STRING:
  128. return lua_getstring(obj);
  129. case LUA_T_ARRAY: case LUA_T_FUNCTION:
  130. case LUA_T_CFUNCTION: case LUA_T_NIL:
  131. return typenames[-ttype(o)];
  132. case LUA_T_USERDATA: {
  133. char *buff = luaI_buffer(100);
  134. int size = o->value.ts->size;
  135. int i;
  136. strcpy(buff, "userdata: ");
  137. if (size > 10) size = 10;
  138. for (i=0; i<size; i++)
  139. sprintf(buff+strlen(buff), "%.2X",
  140. (int)(unsigned char)o->value.ts->str[i]);
  141. return buff;
  142. }
  143. default: return "<unknown object>";
  144. }
  145. }
  146. static void luaI_tostring (void)
  147. {
  148. lua_pushstring(tostring(lua_getparam(1)));
  149. }
  150. static void luaI_print (void)
  151. {
  152. int i = 1;
  153. lua_Object obj;
  154. while ((obj = lua_getparam(i++)) != LUA_NOOBJECT)
  155. printf("%s\n", tostring(obj));
  156. }
  157. static void luaI_type (void)
  158. {
  159. lua_Object o = lua_getparam(1);
  160. luaL_arg_check(o != LUA_NOOBJECT, "type", 1, "no argument");
  161. lua_pushstring(typenames[-ttype(luaI_Address(o))]);
  162. lua_pushnumber(lua_tag(o));
  163. }
  164. /*
  165. ** Internal function: convert an object to a number
  166. */
  167. static void lua_obj2number (void)
  168. {
  169. lua_Object o = lua_getparam(1);
  170. if (lua_isnumber(o))
  171. lua_pushnumber(lua_getnumber(o));
  172. }
  173. static void luaI_error (void)
  174. {
  175. char *s = lua_getstring(lua_getparam(1));
  176. if (s == NULL) s = "(no message)";
  177. lua_error(s);
  178. }
  179. static void luaI_assert (void)
  180. {
  181. lua_Object p = lua_getparam(1);
  182. if (p == LUA_NOOBJECT || lua_isnil(p))
  183. lua_error("assertion failed!");
  184. }
  185. static void luaI_setglobal (void)
  186. {
  187. lua_Object value = lua_getparam(2);
  188. luaL_arg_check(value != LUA_NOOBJECT, "setglobal", 2, NULL);
  189. lua_pushobject(value);
  190. lua_setglobal(luaL_check_string(1, "setglobal"));
  191. lua_pushobject(value); /* return given value */
  192. }
  193. static void luaI_basicsetglobal (void)
  194. {
  195. lua_Object value = lua_getparam(2);
  196. luaL_arg_check(value != LUA_NOOBJECT, "basicsetglobal", 2, NULL);
  197. lua_pushobject(value);
  198. lua_basicsetglobal(luaL_check_string(1, "basicsetglobal"));
  199. lua_pushobject(value); /* return given value */
  200. }
  201. static void luaI_getglobal (void)
  202. {
  203. lua_pushobject(lua_getglobal(luaL_check_string(1, "getglobal")));
  204. }
  205. static void luaI_basicgetglobal (void)
  206. {
  207. lua_pushobject(lua_basicgetglobal(luaL_check_string(1, "basicgetglobal")));
  208. }
  209. static void luatag (void)
  210. {
  211. lua_pushnumber(lua_tag(lua_getparam(1)));
  212. }
  213. #define MAXPARAMS 256
  214. static void luaI_call (void)
  215. {
  216. lua_Object f = lua_getparam(1);
  217. lua_Object arg = lua_getparam(2);
  218. lua_Object temp, params[MAXPARAMS];
  219. int narg, i;
  220. luaL_arg_check(lua_isfunction(f), "call", 1, "function expected");
  221. luaL_arg_check(lua_istable(arg), "call", 2, "table expected");
  222. /* narg = arg.n */
  223. lua_pushobject(arg);
  224. lua_pushstring("n");
  225. temp = lua_getsubscript();
  226. narg = lua_isnumber(temp) ? lua_getnumber(temp) : MAXPARAMS+1;
  227. /* read arg[1...n] */
  228. for (i=0; i<narg; i++) {
  229. if (i>=MAXPARAMS)
  230. lua_error("argument list too long in function `call'");
  231. lua_pushobject(arg);
  232. lua_pushnumber(i+1);
  233. params[i] = lua_getsubscript();
  234. if (narg == MAXPARAMS+1 && lua_isnil(params[i])) {
  235. narg = i;
  236. break;
  237. }
  238. }
  239. /* push parameters and do the call */
  240. for (i=0; i<narg; i++)
  241. lua_pushobject(params[i]);
  242. if (lua_callfunction(f))
  243. lua_error(NULL);
  244. else
  245. passresults();
  246. }
  247. static void luaIl_settag (void)
  248. {
  249. lua_Object o = lua_getparam(1);
  250. luaL_arg_check(o != LUA_NOOBJECT, "settag", 1, NULL);
  251. lua_pushobject(o);
  252. lua_settag(luaL_check_number(2, "settag"));
  253. }
  254. static void luaIl_newtag (void)
  255. {
  256. lua_pushnumber(lua_newtag());
  257. }
  258. static void basicindex (void)
  259. {
  260. lua_Object t = lua_getparam(1);
  261. lua_Object i = lua_getparam(2);
  262. luaL_arg_check(t != LUA_NOOBJECT, "basicindex", 1, NULL);
  263. luaL_arg_check(i != LUA_NOOBJECT, "basicindex", 2, NULL);
  264. lua_pushobject(t);
  265. lua_pushobject(i);
  266. lua_pushobject(lua_basicindex());
  267. }
  268. static void basicstoreindex (void)
  269. {
  270. lua_Object t = lua_getparam(1);
  271. lua_Object i = lua_getparam(2);
  272. lua_Object v = lua_getparam(3);
  273. luaL_arg_check(t != LUA_NOOBJECT && i != LUA_NOOBJECT && v != LUA_NOOBJECT,
  274. "basicindex", 0, NULL);
  275. lua_pushobject(t);
  276. lua_pushobject(i);
  277. lua_pushobject(v);
  278. lua_basicstoreindex();
  279. }
  280. /*
  281. ** Internal functions
  282. */
  283. static struct {
  284. char *name;
  285. lua_CFunction func;
  286. } int_funcs[] = {
  287. {"assert", luaI_assert},
  288. {"basicgetglobal", luaI_basicgetglobal},
  289. {"basicindex", basicindex},
  290. {"basicsetglobal", luaI_basicsetglobal},
  291. {"basicstoreindex", basicstoreindex},
  292. {"call", luaI_call},
  293. {"dofile", lua_internaldofile},
  294. {"dostring", lua_internaldostring},
  295. {"error", luaI_error},
  296. {"getglobal", luaI_getglobal},
  297. {"newtag", luaIl_newtag},
  298. {"next", lua_next},
  299. {"nextvar", luaI_nextvar},
  300. {"print", luaI_print},
  301. {"seterrormethod", luaI_seterrormethod},
  302. {"setfallback", luaI_setfallback},
  303. {"setglobal", luaI_setglobal},
  304. {"setintmethod", luaI_setintmethod},
  305. {"getintmethod", luaI_getintmethod},
  306. {"settag", luaIl_settag},
  307. {"tonumber", lua_obj2number},
  308. {"tostring", luaI_tostring},
  309. {"tag", luatag},
  310. {"type", luaI_type}
  311. };
  312. #define INTFUNCSIZE (sizeof(int_funcs)/sizeof(int_funcs[0]))
  313. void luaI_predefine (void)
  314. {
  315. int i;
  316. Word n;
  317. for (i=0; i<INTFUNCSIZE; i++) {
  318. n = luaI_findsymbolbyname(int_funcs[i].name);
  319. s_ttype(n) = LUA_T_CFUNCTION; s_fvalue(n) = int_funcs[i].func;
  320. }
  321. n = luaI_findsymbolbyname("_VERSION_");
  322. s_ttype(n) = LUA_T_STRING; s_tsvalue(n) = lua_createstring(LUA_VERSION);
  323. }