3 ** Provide function to realise the input/output function and debugger
7 char *rcs_inout
="$Id: inout.c,v 1.2 1993/12/22 21:15:16 roberto Exp $";
17 /* Exported variables */
22 /* Internal variables */
24 #define MAXFUNCSTACK 32
26 static struct { int file
; int function
; } funcstack
[MAXFUNCSTACK
];
27 static int nfuncstack
=0;
31 static void (*usererror
) (char *s
);
34 ** Function to set user function to handle errors.
36 void lua_errorfunction (void (*fn
) (char *s
))
42 ** Function to get the next character from the input file
44 static int fileinput (void)
47 return (c
== EOF
? 0 : c
);
51 ** Function to get the next character from the input string
53 static int stringinput (void)
60 ** Function to open a file to be input unit.
61 ** Return 0 on success or 1 on error.
63 int lua_openfile (char *fn
)
66 lua_setinput (fileinput
);
68 if (fp
== NULL
) return 1;
69 if (lua_addfile (fn
)) return 1;
74 ** Function to close an opened file
76 void lua_closefile (void)
87 ** Function to open a string to be input unit
89 int lua_openstring (char *s
)
92 lua_setinput (stringinput
);
96 sprintf (sn
, "String: %10.10s...", s
);
97 if (lua_addfile (sn
)) return 1;
103 ** Function to close an opened string
105 void lua_closestring (void)
111 ** Call user function to handle error messages, if registred. Or report error
112 ** using standard function (fprintf).
114 void lua_error (char *s
)
116 if (usererror
!= NULL
) usererror (s
);
117 else fprintf (stderr
, "lua: %s\n", s
);
121 ** Called to execute SETFUNCTION opcode, this function pushs a function into
122 ** function stack. Return 0 on success or 1 on error.
124 int lua_pushfunction (int file
, int function
)
126 if (nfuncstack
>= MAXFUNCSTACK
-1)
128 lua_error ("function stack overflow");
131 funcstack
[nfuncstack
].file
= file
;
132 funcstack
[nfuncstack
].function
= function
;
138 ** Called to execute RESET opcode, this function pops a function from
141 void lua_popfunction (void)
147 ** Report bug building a message and sending it to lua_error function.
149 void lua_reportbug (char *s
)
153 if (lua_debugline
!= 0)
158 sprintf (strchr(msg
,0),
159 "\n\tin statement begining at line %d in function \"%s\" of file \"%s\"",
160 lua_debugline
, s_name(funcstack
[nfuncstack
-1].function
),
161 lua_file
[funcstack
[nfuncstack
-1].file
]);
162 sprintf (strchr(msg
,0), "\n\tactive stack\n");
163 for (i
=nfuncstack
-1; i
>=0; i
--)
164 sprintf (strchr(msg
,0), "\t-> function \"%s\" of file \"%s\"\n",
165 s_name(funcstack
[i
].function
),
166 lua_file
[funcstack
[i
].file
]);
170 sprintf (strchr(msg
,0),
171 "\n\tin statement begining at line %d of file \"%s\"",
172 lua_debugline
, lua_filename());