3 ** Provide function to realise the input/output function and debugger
6 ** Waldemar Celes Filho
19 /* Exported variables */
24 /* Internal variables */
26 #define MAXFUNCSTACK 32
28 static struct { int file
; int function
; } funcstack
[MAXFUNCSTACK
];
29 static int nfuncstack
=0;
33 static void (*usererror
) (char *s
);
36 ** Function to set user function to handle errors.
38 void lua_errorfunction (void (*fn
) (char *s
))
44 ** Function to get the next character from the input file
46 static int fileinput (void)
49 return (c
== EOF
? 0 : c
);
53 ** Function to unget the next character from to input file
55 static void fileunput (int c
)
61 ** Function to get the next character from the input string
63 static int stringinput (void)
70 ** Function to unget the next character from to input string
72 static void stringunput (int c
)
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
)
84 lua_setinput (fileinput
);
85 lua_setunput (fileunput
);
87 if (fp
== NULL
) return 1;
88 if (lua_addfile (fn
)) return 1;
93 ** Function to close an opened file
95 void lua_closefile (void)
105 ** Function to open a string to be input unit
107 int lua_openstring (char *s
)
110 lua_setinput (stringinput
);
111 lua_setunput (stringunput
);
115 sprintf (sn
, "String: %10.10s...", s
);
116 if (lua_addfile (sn
)) return 1;
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");
142 funcstack
[nfuncstack
].file
= file
;
143 funcstack
[nfuncstack
].function
= function
;
149 ** Called to execute RESET opcode, this function pops a function from
152 void lua_popfunction (void)
158 ** Report bug building a message and sending it to lua_error function.
160 void lua_reportbug (char *s
)
164 if (lua_debugline
!= 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
]);
181 sprintf (strchr(msg
,0),
182 "\n\tin statement begining at line %d of file \"%s\"",
183 lua_debugline
, lua_filename());