Imported from ../lua-1.0.tar.gz.
[lua.git] / inout.c
blob3ba32ba7e7584dfbeb6f677321e48191214f854f
1 /*
2 ** inout.c
3 ** Provide function to realise the input/output function and debugger
4 ** facilities.
5 **
6 ** Waldemar Celes Filho
7 ** TeCGraf - PUC-Rio
8 ** 11 May 93
9 */
11 #include <stdio.h>
12 #include <string.h>
14 #include "opcode.h"
15 #include "hash.h"
16 #include "inout.h"
17 #include "table.h"
19 /* Exported variables */
20 int lua_linenumber;
21 int lua_debug;
22 int lua_debugline;
24 /* Internal variables */
25 #ifndef MAXFUNCSTACK
26 #define MAXFUNCSTACK 32
27 #endif
28 static struct { int file; int function; } funcstack[MAXFUNCSTACK];
29 static int nfuncstack=0;
31 static FILE *fp;
32 static char *st;
33 static void (*usererror) (char *s);
36 ** Function to set user function to handle errors.
38 void lua_errorfunction (void (*fn) (char *s))
40 usererror = fn;
44 ** Function to get the next character from the input file
46 static int fileinput (void)
48 int c = fgetc (fp);
49 return (c == EOF ? 0 : c);
53 ** Function to unget the next character from to input file
55 static void fileunput (int c)
57 ungetc (c, fp);
61 ** Function to get the next character from the input string
63 static int stringinput (void)
65 st++;
66 return (*(st-1));
70 ** Function to unget the next character from to input string
72 static void stringunput (int c)
74 st--;
78 ** Function to open a file to be input unit.
79 ** Return 0 on success or 1 on error.
81 int lua_openfile (char *fn)
83 lua_linenumber = 1;
84 lua_setinput (fileinput);
85 lua_setunput (fileunput);
86 fp = fopen (fn, "r");
87 if (fp == NULL) return 1;
88 if (lua_addfile (fn)) return 1;
89 return 0;
93 ** Function to close an opened file
95 void lua_closefile (void)
97 if (fp != NULL)
99 fclose (fp);
100 fp = NULL;
105 ** Function to open a string to be input unit
107 int lua_openstring (char *s)
109 lua_linenumber = 1;
110 lua_setinput (stringinput);
111 lua_setunput (stringunput);
112 st = s;
114 char sn[64];
115 sprintf (sn, "String: %10.10s...", s);
116 if (lua_addfile (sn)) return 1;
118 return 0;
122 ** Call user function to handle error messages, if registred. Or report error
123 ** using standard function (fprintf).
125 void lua_error (char *s)
127 if (usererror != NULL) usererror (s);
128 else fprintf (stderr, "lua: %s\n", s);
132 ** Called to execute SETFUNCTION opcode, this function pushs a function into
133 ** function stack. Return 0 on success or 1 on error.
135 int lua_pushfunction (int file, int function)
137 if (nfuncstack >= MAXFUNCSTACK-1)
139 lua_error ("function stack overflow");
140 return 1;
142 funcstack[nfuncstack].file = file;
143 funcstack[nfuncstack].function = function;
144 nfuncstack++;
145 return 0;
149 ** Called to execute RESET opcode, this function pops a function from
150 ** function stack.
152 void lua_popfunction (void)
154 nfuncstack--;
158 ** Report bug building a message and sending it to lua_error function.
160 void lua_reportbug (char *s)
162 char msg[1024];
163 strcpy (msg, s);
164 if (lua_debugline != 0)
166 int i;
167 if (nfuncstack > 0)
169 sprintf (strchr(msg,0),
170 "\n\tin statement begining at line %d in function \"%s\" of file \"%s\"",
171 lua_debugline, s_name(funcstack[nfuncstack-1].function),
172 lua_file[funcstack[nfuncstack-1].file]);
173 sprintf (strchr(msg,0), "\n\tactive stack\n");
174 for (i=nfuncstack-1; i>=0; i--)
175 sprintf (strchr(msg,0), "\t-> function \"%s\" of file \"%s\"\n",
176 s_name(funcstack[i].function),
177 lua_file[funcstack[i].file]);
179 else
181 sprintf (strchr(msg,0),
182 "\n\tin statement begining at line %d of file \"%s\"",
183 lua_debugline, lua_filename());
186 lua_error (msg);