inout.c 3.5 KB

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