inout.c 3.5 KB

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