inout.c 3.3 KB

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