diff options
| author | The Lua team <lua@tecgraf.puc-rio.br> | 1993-07-28 10:18:00 -0300 |
|---|---|---|
| committer | The Lua team <lua@tecgraf.puc-rio.br> | 1993-07-28 10:18:00 -0300 |
| commit | cd05d9c5cb69020c069f037ba7f243f705d0a48a (patch) | |
| tree | cb7f08c0684c10970a528984741047fb3babadd3 /inout.c | |
| download | lua-cd05d9c5cb69020c069f037ba7f243f705d0a48a.tar.gz lua-cd05d9c5cb69020c069f037ba7f243f705d0a48a.tar.bz2 lua-cd05d9c5cb69020c069f037ba7f243f705d0a48a.zip | |
oldest known commit
Diffstat (limited to 'inout.c')
| -rw-r--r-- | inout.c | 188 |
1 files changed, 188 insertions, 0 deletions
diff --git a/inout.c b/inout.c new file mode 100644 index 00000000..3ba32ba7 --- /dev/null +++ b/inout.c | |||
| @@ -0,0 +1,188 @@ | |||
| 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 | */ | ||
| 10 | |||
| 11 | #include <stdio.h> | ||
| 12 | #include <string.h> | ||
| 13 | |||
| 14 | #include "opcode.h" | ||
| 15 | #include "hash.h" | ||
| 16 | #include "inout.h" | ||
| 17 | #include "table.h" | ||
| 18 | |||
| 19 | /* Exported variables */ | ||
| 20 | int lua_linenumber; | ||
| 21 | int lua_debug; | ||
| 22 | int lua_debugline; | ||
| 23 | |||
| 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; | ||
| 30 | |||
| 31 | static FILE *fp; | ||
| 32 | static char *st; | ||
| 33 | static void (*usererror) (char *s); | ||
| 34 | |||
| 35 | /* | ||
| 36 | ** Function to set user function to handle errors. | ||
| 37 | */ | ||
| 38 | void lua_errorfunction (void (*fn) (char *s)) | ||
| 39 | { | ||
| 40 | usererror = fn; | ||
| 41 | } | ||
| 42 | |||
| 43 | /* | ||
| 44 | ** Function to get the next character from the input file | ||
| 45 | */ | ||
| 46 | static int fileinput (void) | ||
| 47 | { | ||
| 48 | int c = fgetc (fp); | ||
| 49 | return (c == EOF ? 0 : c); | ||
| 50 | } | ||
| 51 | |||
| 52 | /* | ||
| 53 | ** Function to unget the next character from to input file | ||
| 54 | */ | ||
| 55 | static void fileunput (int c) | ||
| 56 | { | ||
| 57 | ungetc (c, fp); | ||
| 58 | } | ||
| 59 | |||
| 60 | /* | ||
| 61 | ** Function to get the next character from the input string | ||
| 62 | */ | ||
| 63 | static int stringinput (void) | ||
| 64 | { | ||
| 65 | st++; | ||
| 66 | return (*(st-1)); | ||
| 67 | } | ||
| 68 | |||
| 69 | /* | ||
| 70 | ** Function to unget the next character from to input string | ||
| 71 | */ | ||
| 72 | static void stringunput (int c) | ||
| 73 | { | ||
| 74 | st--; | ||
| 75 | } | ||
| 76 | |||
| 77 | /* | ||
| 78 | ** Function to open a file to be input unit. | ||
| 79 | ** Return 0 on success or 1 on error. | ||
| 80 | */ | ||
| 81 | int lua_openfile (char *fn) | ||
| 82 | { | ||
| 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; | ||
| 90 | } | ||
| 91 | |||
| 92 | /* | ||
| 93 | ** Function to close an opened file | ||
| 94 | */ | ||
| 95 | void lua_closefile (void) | ||
| 96 | { | ||
| 97 | if (fp != NULL) | ||
| 98 | { | ||
| 99 | fclose (fp); | ||
| 100 | fp = NULL; | ||
| 101 | } | ||
| 102 | } | ||
| 103 | |||
| 104 | /* | ||
| 105 | ** Function to open a string to be input unit | ||
| 106 | */ | ||
| 107 | int lua_openstring (char *s) | ||
| 108 | { | ||
| 109 | lua_linenumber = 1; | ||
| 110 | lua_setinput (stringinput); | ||
| 111 | lua_setunput (stringunput); | ||
| 112 | st = s; | ||
| 113 | { | ||
| 114 | char sn[64]; | ||
| 115 | sprintf (sn, "String: %10.10s...", s); | ||
| 116 | if (lua_addfile (sn)) return 1; | ||
| 117 | } | ||
| 118 | return 0; | ||
| 119 | } | ||
| 120 | |||
| 121 | /* | ||
| 122 | ** Call user function to handle error messages, if registred. Or report error | ||
| 123 | ** using standard function (fprintf). | ||
| 124 | */ | ||
| 125 | void lua_error (char *s) | ||
| 126 | { | ||
| 127 | if (usererror != NULL) usererror (s); | ||
| 128 | else fprintf (stderr, "lua: %s\n", s); | ||
| 129 | } | ||
| 130 | |||
| 131 | /* | ||
| 132 | ** Called to execute SETFUNCTION opcode, this function pushs a function into | ||
| 133 | ** function stack. Return 0 on success or 1 on error. | ||
| 134 | */ | ||
| 135 | int lua_pushfunction (int file, int function) | ||
| 136 | { | ||
| 137 | if (nfuncstack >= MAXFUNCSTACK-1) | ||
| 138 | { | ||
| 139 | lua_error ("function stack overflow"); | ||
| 140 | return 1; | ||
| 141 | } | ||
| 142 | funcstack[nfuncstack].file = file; | ||
| 143 | funcstack[nfuncstack].function = function; | ||
| 144 | nfuncstack++; | ||
| 145 | return 0; | ||
| 146 | } | ||
| 147 | |||
| 148 | /* | ||
| 149 | ** Called to execute RESET opcode, this function pops a function from | ||
| 150 | ** function stack. | ||
| 151 | */ | ||
| 152 | void lua_popfunction (void) | ||
| 153 | { | ||
| 154 | nfuncstack--; | ||
| 155 | } | ||
| 156 | |||
| 157 | /* | ||
| 158 | ** Report bug building a message and sending it to lua_error function. | ||
| 159 | */ | ||
| 160 | void lua_reportbug (char *s) | ||
| 161 | { | ||
| 162 | char msg[1024]; | ||
| 163 | strcpy (msg, s); | ||
| 164 | if (lua_debugline != 0) | ||
| 165 | { | ||
| 166 | int i; | ||
| 167 | if (nfuncstack > 0) | ||
| 168 | { | ||
| 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]); | ||
| 178 | } | ||
| 179 | else | ||
| 180 | { | ||
| 181 | sprintf (strchr(msg,0), | ||
| 182 | "\n\tin statement begining at line %d of file \"%s\"", | ||
| 183 | lua_debugline, lua_filename()); | ||
| 184 | } | ||
| 185 | } | ||
| 186 | lua_error (msg); | ||
| 187 | } | ||
| 188 | |||
