Imported from ../lua-1.1.tar.gz.
[lua.git] / src / inout.c
blobd985c1b7359cf8964926e53b847cca8332b05193
1 /*
2 ** inout.c
3 ** Provide function to realise the input/output function and debugger
4 ** facilities.
5 */
7 char *rcs_inout="$Id: inout.c,v 1.2 1993/12/22 21:15:16 roberto Exp $";
9 #include <stdio.h>
10 #include <string.h>
12 #include "opcode.h"
13 #include "hash.h"
14 #include "inout.h"
15 #include "table.h"
17 /* Exported variables */
18 int lua_linenumber;
19 int lua_debug;
20 int lua_debugline;
22 /* Internal variables */
23 #ifndef MAXFUNCSTACK
24 #define MAXFUNCSTACK 32
25 #endif
26 static struct { int file; int function; } funcstack[MAXFUNCSTACK];
27 static int nfuncstack=0;
29 static FILE *fp;
30 static char *st;
31 static void (*usererror) (char *s);
34 ** Function to set user function to handle errors.
36 void lua_errorfunction (void (*fn) (char *s))
38 usererror = fn;
42 ** Function to get the next character from the input file
44 static int fileinput (void)
46 int c = fgetc (fp);
47 return (c == EOF ? 0 : c);
51 ** Function to get the next character from the input string
53 static int stringinput (void)
55 st++;
56 return (*(st-1));
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)
65 lua_linenumber = 1;
66 lua_setinput (fileinput);
67 fp = fopen (fn, "r");
68 if (fp == NULL) return 1;
69 if (lua_addfile (fn)) return 1;
70 return 0;
74 ** Function to close an opened file
76 void lua_closefile (void)
78 if (fp != NULL)
80 lua_delfile();
81 fclose (fp);
82 fp = NULL;
87 ** Function to open a string to be input unit
89 int lua_openstring (char *s)
91 lua_linenumber = 1;
92 lua_setinput (stringinput);
93 st = s;
95 char sn[64];
96 sprintf (sn, "String: %10.10s...", s);
97 if (lua_addfile (sn)) return 1;
99 return 0;
103 ** Function to close an opened string
105 void lua_closestring (void)
107 lua_delfile();
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");
129 return 1;
131 funcstack[nfuncstack].file = file;
132 funcstack[nfuncstack].function = function;
133 nfuncstack++;
134 return 0;
138 ** Called to execute RESET opcode, this function pops a function from
139 ** function stack.
141 void lua_popfunction (void)
143 nfuncstack--;
147 ** Report bug building a message and sending it to lua_error function.
149 void lua_reportbug (char *s)
151 char msg[1024];
152 strcpy (msg, s);
153 if (lua_debugline != 0)
155 int i;
156 if (nfuncstack > 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]);
168 else
170 sprintf (strchr(msg,0),
171 "\n\tin statement begining at line %d of file \"%s\"",
172 lua_debugline, lua_filename());
175 lua_error (msg);