inout.c 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179
  1. /*
  2. ** inout.c
  3. ** Provide function to realise the input/output function and debugger
  4. ** facilities.
  5. */
  6. char *rcs_inout="$Id: inout.c,v 2.2 1994/08/17 22:22:44 roberto Exp celes $";
  7. #include <stdio.h>
  8. #include <string.h>
  9. #include "opcode.h"
  10. #include "hash.h"
  11. #include "inout.h"
  12. #include "table.h"
  13. #include "tree.h"
  14. #include "lua.h"
  15. /* Exported variables */
  16. int lua_linenumber;
  17. int lua_debug;
  18. int lua_debugline;
  19. /* Internal variables */
  20. #ifndef MAXFUNCSTACK
  21. #define MAXFUNCSTACK 64
  22. #endif
  23. static struct { int file; int function; } funcstack[MAXFUNCSTACK];
  24. static int nfuncstack=0;
  25. static FILE *fp;
  26. static char *st;
  27. static void (*usererror) (char *s);
  28. /*
  29. ** Function to set user function to handle errors.
  30. */
  31. void lua_errorfunction (void (*fn) (char *s))
  32. {
  33. usererror = fn;
  34. }
  35. /*
  36. ** Function to get the next character from the input file
  37. */
  38. static int fileinput (void)
  39. {
  40. int c = fgetc (fp);
  41. return (c == EOF ? 0 : c);
  42. }
  43. /*
  44. ** Function to get the next character from the input string
  45. */
  46. static int stringinput (void)
  47. {
  48. st++;
  49. return (*(st-1));
  50. }
  51. /*
  52. ** Function to open a file to be input unit.
  53. ** Return 0 on success or 1 on error.
  54. */
  55. int lua_openfile (char *fn)
  56. {
  57. lua_linenumber = 1;
  58. lua_setinput (fileinput);
  59. fp = fopen (fn, "r");
  60. if (fp == NULL) return 1;
  61. if (lua_addfile (fn)) return 1;
  62. return 0;
  63. }
  64. /*
  65. ** Function to close an opened file
  66. */
  67. void lua_closefile (void)
  68. {
  69. if (fp != NULL)
  70. {
  71. lua_delfile();
  72. fclose (fp);
  73. fp = NULL;
  74. }
  75. }
  76. /*
  77. ** Function to open a string to be input unit
  78. */
  79. int lua_openstring (char *s)
  80. {
  81. lua_linenumber = 1;
  82. lua_setinput (stringinput);
  83. st = s;
  84. {
  85. char sn[64];
  86. sprintf (sn, "String: %10.10s...", s);
  87. if (lua_addfile (sn)) return 1;
  88. }
  89. return 0;
  90. }
  91. /*
  92. ** Function to close an opened string
  93. */
  94. void lua_closestring (void)
  95. {
  96. lua_delfile();
  97. }
  98. /*
  99. ** Call user function to handle error messages, if registred. Or report error
  100. ** using standard function (fprintf).
  101. */
  102. void lua_error (char *s)
  103. {
  104. if (usererror != NULL) usererror (s);
  105. else fprintf (stderr, "lua: %s\n", s);
  106. }
  107. /*
  108. ** Called to execute SETFUNCTION opcode, this function pushs a function into
  109. ** function stack. Return 0 on success or 1 on error.
  110. */
  111. int lua_pushfunction (int file, int function)
  112. {
  113. if (nfuncstack >= MAXFUNCSTACK-1)
  114. {
  115. lua_error ("function stack overflow");
  116. return 1;
  117. }
  118. funcstack[nfuncstack].file = file;
  119. funcstack[nfuncstack].function = function;
  120. nfuncstack++;
  121. return 0;
  122. }
  123. /*
  124. ** Called to execute RESET opcode, this function pops a function from
  125. ** function stack.
  126. */
  127. void lua_popfunction (void)
  128. {
  129. nfuncstack--;
  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[1024];
  137. strcpy (msg, s);
  138. if (lua_debugline != 0)
  139. {
  140. int i;
  141. if (nfuncstack > 0)
  142. {
  143. sprintf (strchr(msg,0),
  144. "\n\tin statement begining at line %d in function \"%s\" of file \"%s\"",
  145. lua_debugline, lua_varname(funcstack[nfuncstack-1].function),
  146. lua_file[funcstack[nfuncstack-1].file]);
  147. sprintf (strchr(msg,0), "\n\tactive stack\n");
  148. for (i=nfuncstack-1; i>=0; i--)
  149. sprintf (strchr(msg,0), "\t-> function \"%s\" of file \"%s\"\n",
  150. lua_varname(funcstack[i].function),
  151. lua_file[funcstack[i].file]);
  152. }
  153. else
  154. {
  155. sprintf (strchr(msg,0),
  156. "\n\tin statement begining at line %d of file \"%s\"",
  157. lua_debugline, lua_filename());
  158. }
  159. }
  160. lua_error (msg);
  161. }