inout.c 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180
  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.3 1994/09/05 21:22:43 celes Exp celes $";
  7. #include <stdio.h>
  8. #include <stdlib.h>
  9. #include <string.h>
  10. #include "opcode.h"
  11. #include "hash.h"
  12. #include "inout.h"
  13. #include "table.h"
  14. #include "tree.h"
  15. #include "lua.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 64
  23. #endif
  24. static struct { char *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 get the next character from the input string
  46. */
  47. static int stringinput (void)
  48. {
  49. st++;
  50. return (*(st-1));
  51. }
  52. /*
  53. ** Function to open a file to be input unit.
  54. ** Return 0 on success or 1 on error.
  55. */
  56. int lua_openfile (char *fn)
  57. {
  58. lua_linenumber = 1;
  59. lua_setinput (fileinput);
  60. fp = fopen (fn, "r");
  61. if (fp == NULL) return 1;
  62. if (lua_addfile (fn)) return 1;
  63. return 0;
  64. }
  65. /*
  66. ** Function to close an opened file
  67. */
  68. void lua_closefile (void)
  69. {
  70. if (fp != NULL)
  71. {
  72. lua_delfile();
  73. fclose (fp);
  74. fp = NULL;
  75. }
  76. }
  77. /*
  78. ** Function to open a string to be input unit
  79. */
  80. int lua_openstring (char *s)
  81. {
  82. lua_linenumber = 1;
  83. lua_setinput (stringinput);
  84. st = s;
  85. {
  86. char sn[64];
  87. sprintf (sn, "String: %10.10s...", s);
  88. if (lua_addfile (sn)) return 1;
  89. }
  90. return 0;
  91. }
  92. /*
  93. ** Function to close an opened string
  94. */
  95. void lua_closestring (void)
  96. {
  97. lua_delfile();
  98. }
  99. /*
  100. ** Call user function to handle error messages, if registred. Or report error
  101. ** using standard function (fprintf).
  102. */
  103. void lua_error (char *s)
  104. {
  105. if (usererror != NULL) usererror (s);
  106. else fprintf (stderr, "lua: %s\n", s);
  107. }
  108. /*
  109. ** Called to execute SETFUNCTION opcode, this function pushs a function into
  110. ** function stack. Return 0 on success or 1 on error.
  111. */
  112. int lua_pushfunction (char *file, int function)
  113. {
  114. if (nfuncstack >= MAXFUNCSTACK-1)
  115. {
  116. lua_error ("function stack overflow");
  117. return 1;
  118. }
  119. funcstack[nfuncstack].function = function;
  120. funcstack[nfuncstack].file = file;
  121. nfuncstack++;
  122. return 0;
  123. }
  124. /*
  125. ** Called to execute RESET opcode, this function pops a function from
  126. ** function stack.
  127. */
  128. void lua_popfunction (void)
  129. {
  130. nfuncstack--;
  131. }
  132. /*
  133. ** Report bug building a message and sending it to lua_error function.
  134. */
  135. void lua_reportbug (char *s)
  136. {
  137. char msg[1024];
  138. strcpy (msg, s);
  139. if (lua_debugline != 0)
  140. {
  141. int i;
  142. if (nfuncstack > 0)
  143. {
  144. sprintf (strchr(msg,0),
  145. "\n\tin statement begining at line %d in function \"%s\" of file \"%s\"",
  146. lua_debugline, lua_varname(funcstack[nfuncstack-1].function),
  147. funcstack[nfuncstack-1].file);
  148. sprintf (strchr(msg,0), "\n\tactive stack\n");
  149. for (i=nfuncstack-1; i>=0; i--)
  150. sprintf (strchr(msg,0), "\t-> function \"%s\" of file \"%s\"\n",
  151. lua_varname(funcstack[i].function),
  152. funcstack[i].file);
  153. }
  154. else
  155. {
  156. sprintf (strchr(msg,0),
  157. "\n\tin statement begining at line %d of file \"%s\"",
  158. lua_debugline, lua_filename());
  159. }
  160. }
  161. lua_error (msg);
  162. }