diff options
Diffstat (limited to 'liolib.c')
| -rw-r--r-- | liolib.c | 362 |
1 files changed, 182 insertions, 180 deletions
| @@ -1,5 +1,5 @@ | |||
| 1 | /* | 1 | /* |
| 2 | ** $Id: liolib.c,v 1.49 1999/10/26 11:00:12 roberto Exp roberto $ | 2 | ** $Id: liolib.c,v 1.50 1999/11/09 17:59:35 roberto Exp roberto $ |
| 3 | ** Standard I/O (and system) library | 3 | ** Standard I/O (and system) library |
| 4 | ** See Copyright Notice in lua.h | 4 | ** See Copyright Notice in lua.h |
| 5 | */ | 5 | */ |
| @@ -12,6 +12,8 @@ | |||
| 12 | #include <string.h> | 12 | #include <string.h> |
| 13 | #include <time.h> | 13 | #include <time.h> |
| 14 | 14 | ||
| 15 | #define LUA_REENTRANT | ||
| 16 | |||
| 15 | #include "lauxlib.h" | 17 | #include "lauxlib.h" |
| 16 | #include "lua.h" | 18 | #include "lua.h" |
| 17 | #include "luadebug.h" | 19 | #include "luadebug.h" |
| @@ -22,7 +24,7 @@ | |||
| 22 | #include <locale.h> | 24 | #include <locale.h> |
| 23 | #else | 25 | #else |
| 24 | /* no support for locale and for strerror: fake them */ | 26 | /* no support for locale and for strerror: fake them */ |
| 25 | #define setlocale(a,b) 0 | 27 | #define setlocale(a,b) ((void)a, strcmp((b),"C")==0?"C":NULL) |
| 26 | #define LC_ALL 0 | 28 | #define LC_ALL 0 |
| 27 | #define LC_COLLATE 0 | 29 | #define LC_COLLATE 0 |
| 28 | #define LC_CTYPE 0 | 30 | #define LC_CTYPE 0 |
| @@ -37,7 +39,7 @@ | |||
| 37 | 39 | ||
| 38 | #define FIRSTARG 2 /* 1st is upvalue */ | 40 | #define FIRSTARG 2 /* 1st is upvalue */ |
| 39 | 41 | ||
| 40 | #define CLOSEDTAG(tag) ((tag)-1) /* assume that CLOSEDTAG = iotag-1 */ | 42 | #define CLOSEDTAG(L, tag) ((tag)-1) /* assume that CLOSEDTAG = iotag-1 */ |
| 41 | 43 | ||
| 42 | 44 | ||
| 43 | #define FINPUT "_INPUT" | 45 | #define FINPUT "_INPUT" |
| @@ -47,22 +49,22 @@ | |||
| 47 | #ifdef POPEN | 49 | #ifdef POPEN |
| 48 | /* FILE *popen(); | 50 | /* FILE *popen(); |
| 49 | int pclose(); */ | 51 | int pclose(); */ |
| 50 | #define CLOSEFILE(f) ((pclose(f) == -1) ? fclose(f) : 0) | 52 | #define CLOSEFILE(L, f) ((pclose(f) == -1) ? fclose(f) : 0) |
| 51 | #else | 53 | #else |
| 52 | /* no support for popen */ | 54 | /* no support for popen */ |
| 53 | #define popen(x,y) NULL /* that is, popen always fails */ | 55 | #define popen(x,y) NULL /* that is, popen always fails */ |
| 54 | #define CLOSEFILE(f) (fclose(f)) | 56 | #define CLOSEFILE(L, f) (fclose(f)) |
| 55 | #endif | 57 | #endif |
| 56 | 58 | ||
| 57 | 59 | ||
| 58 | 60 | ||
| 59 | static void pushresult (int i) { | 61 | static void pushresult (lua_State *L, int i) { |
| 60 | if (i) | 62 | if (i) |
| 61 | lua_pushuserdata(NULL); | 63 | lua_pushuserdata(L, NULL); |
| 62 | else { | 64 | else { |
| 63 | lua_pushnil(); | 65 | lua_pushnil(L); |
| 64 | lua_pushstring(strerror(errno)); | 66 | lua_pushstring(L, strerror(errno)); |
| 65 | lua_pushnumber(errno); | 67 | lua_pushnumber(L, errno); |
| 66 | } | 68 | } |
| 67 | } | 69 | } |
| 68 | 70 | ||
| @@ -73,144 +75,144 @@ static void pushresult (int i) { | |||
| 73 | ** ======================================================= | 75 | ** ======================================================= |
| 74 | */ | 76 | */ |
| 75 | 77 | ||
| 76 | static int gettag (void) { | 78 | static int gettag (lua_State *L) { |
| 77 | return (int)lua_getnumber(lua_getparam(IOTAG)); | 79 | return (int)lua_getnumber(L, lua_getparam(L, IOTAG)); |
| 78 | } | 80 | } |
| 79 | 81 | ||
| 80 | 82 | ||
| 81 | static int ishandle (lua_Object f) { | 83 | static int ishandle (lua_State *L, lua_Object f) { |
| 82 | if (lua_isuserdata(f)) { | 84 | if (lua_isuserdata(L, f)) { |
| 83 | int tag = gettag(); | 85 | int tag = gettag(L); |
| 84 | if (lua_tag(f) == CLOSEDTAG(tag)) | 86 | if (lua_tag(L, f) == CLOSEDTAG(L, tag)) |
| 85 | lua_error("cannot access a closed file"); | 87 | lua_error(L, "cannot access a closed file"); |
| 86 | return lua_tag(f) == tag; | 88 | return lua_tag(L, f) == tag; |
| 87 | } | 89 | } |
| 88 | else return 0; | 90 | else return 0; |
| 89 | } | 91 | } |
| 90 | 92 | ||
| 91 | 93 | ||
| 92 | static FILE *getfilebyname (const char *name) { | 94 | static FILE *getfilebyname (lua_State *L, const char *name) { |
| 93 | lua_Object f = lua_rawgetglobal(name); | 95 | lua_Object f = lua_rawgetglobal(L, name); |
| 94 | if (!ishandle(f)) | 96 | if (!ishandle(L, f)) |
| 95 | luaL_verror("global variable `%.50s' is not a file handle", name); | 97 | luaL_verror(L, "global variable `%.50s' is not a file handle", name); |
| 96 | return lua_getuserdata(f); | 98 | return lua_getuserdata(L, f); |
| 97 | } | 99 | } |
| 98 | 100 | ||
| 99 | 101 | ||
| 100 | static FILE *getfile (int arg) { | 102 | static FILE *getfile (lua_State *L, int arg) { |
| 101 | lua_Object f = lua_getparam(arg); | 103 | lua_Object f = lua_getparam(L, arg); |
| 102 | return (ishandle(f)) ? lua_getuserdata(f) : NULL; | 104 | return (ishandle(L, f)) ? lua_getuserdata(L, f) : NULL; |
| 103 | } | 105 | } |
| 104 | 106 | ||
| 105 | 107 | ||
| 106 | static FILE *getnonullfile (int arg) { | 108 | static FILE *getnonullfile (lua_State *L, int arg) { |
| 107 | FILE *f = getfile(arg); | 109 | FILE *f = getfile(L, arg); |
| 108 | luaL_arg_check(f, arg, "invalid file handle"); | 110 | luaL_arg_check(L, f, arg, "invalid file handle"); |
| 109 | return f; | 111 | return f; |
| 110 | } | 112 | } |
| 111 | 113 | ||
| 112 | 114 | ||
| 113 | static FILE *getfileparam (const char *name, int *arg) { | 115 | static FILE *getfileparam (lua_State *L, const char *name, int *arg) { |
| 114 | FILE *f = getfile(*arg); | 116 | FILE *f = getfile(L, *arg); |
| 115 | if (f) { | 117 | if (f) { |
| 116 | (*arg)++; | 118 | (*arg)++; |
| 117 | return f; | 119 | return f; |
| 118 | } | 120 | } |
| 119 | else | 121 | else |
| 120 | return getfilebyname(name); | 122 | return getfilebyname(L, name); |
| 121 | } | 123 | } |
| 122 | 124 | ||
| 123 | 125 | ||
| 124 | static int closefile (FILE *f) { | 126 | static int closefile (lua_State *L, FILE *f) { |
| 125 | if (f == stdin || f == stdout) | 127 | if (f == stdin || f == stdout) |
| 126 | return 1; | 128 | return 1; |
| 127 | else { | 129 | else { |
| 128 | int tag = gettag(); | 130 | int tag = gettag(L); |
| 129 | lua_pushusertag(f, tag); | 131 | lua_pushusertag(L, f, tag); |
| 130 | lua_settag(CLOSEDTAG(tag)); | 132 | lua_settag(L, CLOSEDTAG(L, tag)); |
| 131 | return (CLOSEFILE(f) == 0); | 133 | return (CLOSEFILE(L, f) == 0); |
| 132 | } | 134 | } |
| 133 | } | 135 | } |
| 134 | 136 | ||
| 135 | 137 | ||
| 136 | static void io_close (void) { | 138 | static void io_close (lua_State *L) { |
| 137 | pushresult(closefile(getnonullfile(FIRSTARG))); | 139 | pushresult(L, closefile(L, getnonullfile(L, FIRSTARG))); |
| 138 | } | 140 | } |
| 139 | 141 | ||
| 140 | 142 | ||
| 141 | static void gc_close (void) { | 143 | static void gc_close (lua_State *L) { |
| 142 | FILE *f = getnonullfile(FIRSTARG); | 144 | FILE *f = getnonullfile(L, FIRSTARG); |
| 143 | if (f != stdin && f != stdout && f != stderr) { | 145 | if (f != stdin && f != stdout && f != stderr) { |
| 144 | CLOSEFILE(f); | 146 | CLOSEFILE(L, f); |
| 145 | } | 147 | } |
| 146 | } | 148 | } |
| 147 | 149 | ||
| 148 | 150 | ||
| 149 | static void io_open (void) { | 151 | static void io_open (lua_State *L) { |
| 150 | FILE *f = fopen(luaL_check_string(FIRSTARG), luaL_check_string(FIRSTARG+1)); | 152 | FILE *f = fopen(luaL_check_string(L, FIRSTARG), luaL_check_string(L, FIRSTARG+1)); |
| 151 | if (f) lua_pushusertag(f, gettag()); | 153 | if (f) lua_pushusertag(L, f, gettag(L)); |
| 152 | else pushresult(0); | 154 | else pushresult(L, 0); |
| 153 | } | 155 | } |
| 154 | 156 | ||
| 155 | 157 | ||
| 156 | static void setfile (FILE *f, const char *name, int tag) { | 158 | static void setfile (lua_State *L, FILE *f, const char *name, int tag) { |
| 157 | lua_pushusertag(f, tag); | 159 | lua_pushusertag(L, f, tag); |
| 158 | lua_setglobal(name); | 160 | lua_setglobal(L, name); |
| 159 | } | 161 | } |
| 160 | 162 | ||
| 161 | 163 | ||
| 162 | static void setreturn (FILE *f, const char *name) { | 164 | static void setreturn (lua_State *L, FILE *f, const char *name) { |
| 163 | if (f == NULL) | 165 | if (f == NULL) |
| 164 | pushresult(0); | 166 | pushresult(L, 0); |
| 165 | else { | 167 | else { |
| 166 | int tag = gettag(); | 168 | int tag = gettag(L); |
| 167 | setfile(f, name, tag); | 169 | setfile(L, f, name, tag); |
| 168 | lua_pushusertag(f, tag); | 170 | lua_pushusertag(L, f, tag); |
| 169 | } | 171 | } |
| 170 | } | 172 | } |
| 171 | 173 | ||
| 172 | 174 | ||
| 173 | static void io_readfrom (void) { | 175 | static void io_readfrom (lua_State *L) { |
| 174 | FILE *current; | 176 | FILE *current; |
| 175 | lua_Object f = lua_getparam(FIRSTARG); | 177 | lua_Object f = lua_getparam(L, FIRSTARG); |
| 176 | if (f == LUA_NOOBJECT) { | 178 | if (f == LUA_NOOBJECT) { |
| 177 | if (closefile(getfilebyname(FINPUT))) | 179 | if (closefile(L, getfilebyname(L, FINPUT))) |
| 178 | current = stdin; | 180 | current = stdin; |
| 179 | else | 181 | else |
| 180 | current = NULL; /* to signal error */ | 182 | current = NULL; /* to signal error */ |
| 181 | } | 183 | } |
| 182 | else if (lua_tag(f) == gettag()) /* deprecated option */ | 184 | else if (lua_tag(L, f) == gettag(L)) /* deprecated option */ |
| 183 | current = lua_getuserdata(f); | 185 | current = lua_getuserdata(L, f); |
| 184 | else { | 186 | else { |
| 185 | const char *s = luaL_check_string(FIRSTARG); | 187 | const char *s = luaL_check_string(L, FIRSTARG); |
| 186 | current = (*s == '|') ? popen(s+1, "r") : fopen(s, "r"); | 188 | current = (*s == '|') ? popen(s+1, "r") : fopen(s, "r"); |
| 187 | } | 189 | } |
| 188 | setreturn(current, FINPUT); | 190 | setreturn(L, current, FINPUT); |
| 189 | } | 191 | } |
| 190 | 192 | ||
| 191 | 193 | ||
| 192 | static void io_writeto (void) { | 194 | static void io_writeto (lua_State *L) { |
| 193 | FILE *current; | 195 | FILE *current; |
| 194 | lua_Object f = lua_getparam(FIRSTARG); | 196 | lua_Object f = lua_getparam(L, FIRSTARG); |
| 195 | if (f == LUA_NOOBJECT) { | 197 | if (f == LUA_NOOBJECT) { |
| 196 | if (closefile(getfilebyname(FOUTPUT))) | 198 | if (closefile(L, getfilebyname(L, FOUTPUT))) |
| 197 | current = stdout; | 199 | current = stdout; |
| 198 | else | 200 | else |
| 199 | current = NULL; /* to signal error */ | 201 | current = NULL; /* to signal error */ |
| 200 | } | 202 | } |
| 201 | else if (lua_tag(f) == gettag()) /* deprecated option */ | 203 | else if (lua_tag(L, f) == gettag(L)) /* deprecated option */ |
| 202 | current = lua_getuserdata(f); | 204 | current = lua_getuserdata(L, f); |
| 203 | else { | 205 | else { |
| 204 | const char *s = luaL_check_string(FIRSTARG); | 206 | const char *s = luaL_check_string(L, FIRSTARG); |
| 205 | current = (*s == '|') ? popen(s+1,"w") : fopen(s, "w"); | 207 | current = (*s == '|') ? popen(s+1,"w") : fopen(s, "w"); |
| 206 | } | 208 | } |
| 207 | setreturn(current, FOUTPUT); | 209 | setreturn(L, current, FOUTPUT); |
| 208 | } | 210 | } |
| 209 | 211 | ||
| 210 | 212 | ||
| 211 | static void io_appendto (void) { | 213 | static void io_appendto (lua_State *L) { |
| 212 | FILE *current = fopen(luaL_check_string(FIRSTARG), "a"); | 214 | FILE *current = fopen(luaL_check_string(L, FIRSTARG), "a"); |
| 213 | setreturn(current, FOUTPUT); | 215 | setreturn(L, current, FOUTPUT); |
| 214 | } | 216 | } |
| 215 | 217 | ||
| 216 | 218 | ||
| @@ -232,7 +234,7 @@ static void io_appendto (void) { | |||
| 232 | #define NEED_OTHER (EOF-1) /* just some flag different from EOF */ | 234 | #define NEED_OTHER (EOF-1) /* just some flag different from EOF */ |
| 233 | 235 | ||
| 234 | 236 | ||
| 235 | static int read_pattern (FILE *f, const char *p) { | 237 | static int read_pattern (lua_State *L, FILE *f, const char *p) { |
| 236 | int inskip = 0; /* {skip} level */ | 238 | int inskip = 0; /* {skip} level */ |
| 237 | int c = NEED_OTHER; | 239 | int c = NEED_OTHER; |
| 238 | while (*p != '\0') { | 240 | while (*p != '\0') { |
| @@ -242,17 +244,17 @@ static int read_pattern (FILE *f, const char *p) { | |||
| 242 | p++; | 244 | p++; |
| 243 | continue; | 245 | continue; |
| 244 | case '}': | 246 | case '}': |
| 245 | if (!inskip) lua_error("unbalanced braces in read pattern"); | 247 | if (!inskip) lua_error(L, "unbalanced braces in read pattern"); |
| 246 | inskip--; | 248 | inskip--; |
| 247 | p++; | 249 | p++; |
| 248 | continue; | 250 | continue; |
| 249 | default: { | 251 | default: { |
| 250 | const char *ep = luaI_classend(p); /* get what is next */ | 252 | const char *ep = luaI_classend(L, p); /* get what is next */ |
| 251 | int m; /* match result */ | 253 | int m; /* match result */ |
| 252 | if (c == NEED_OTHER) c = getc(f); | 254 | if (c == NEED_OTHER) c = getc(f); |
| 253 | m = (c==EOF) ? 0 : luaI_singlematch(c, p, ep); | 255 | m = (c==EOF) ? 0 : luaI_singlematch(c, p, ep); |
| 254 | if (m) { | 256 | if (m) { |
| 255 | if (!inskip) luaL_addchar(c); | 257 | if (!inskip) luaL_addchar(L, c); |
| 256 | c = NEED_OTHER; | 258 | c = NEED_OTHER; |
| 257 | } | 259 | } |
| 258 | switch (*ep) { | 260 | switch (*ep) { |
| @@ -263,7 +265,7 @@ static int read_pattern (FILE *f, const char *p) { | |||
| 263 | while (m) { /* reads the same item until it fails */ | 265 | while (m) { /* reads the same item until it fails */ |
| 264 | c = getc(f); | 266 | c = getc(f); |
| 265 | m = (c==EOF) ? 0 : luaI_singlematch(c, p, ep); | 267 | m = (c==EOF) ? 0 : luaI_singlematch(c, p, ep); |
| 266 | if (m && !inskip) luaL_addchar(c); | 268 | if (m && !inskip) luaL_addchar(L, c); |
| 267 | } | 269 | } |
| 268 | /* go through to continue reading the pattern */ | 270 | /* go through to continue reading the pattern */ |
| 269 | case '?': /* optional */ | 271 | case '?': /* optional */ |
| @@ -282,26 +284,26 @@ static int read_pattern (FILE *f, const char *p) { | |||
| 282 | 284 | ||
| 283 | #else | 285 | #else |
| 284 | 286 | ||
| 285 | #define read_pattern(f,p) (lua_error("read patterns are deprecated"), 0) | 287 | #define read_pattern(L, f,p) (lua_error(L, "read patterns are deprecated"), 0) |
| 286 | 288 | ||
| 287 | #endif | 289 | #endif |
| 288 | 290 | ||
| 289 | 291 | ||
| 290 | static int read_number (FILE *f) { | 292 | static int read_number (lua_State *L, FILE *f) { |
| 291 | double d; | 293 | double d; |
| 292 | if (fscanf(f, "%lf", &d) == 1) { | 294 | if (fscanf(f, "%lf", &d) == 1) { |
| 293 | lua_pushnumber(d); | 295 | lua_pushnumber(L, d); |
| 294 | return 1; | 296 | return 1; |
| 295 | } | 297 | } |
| 296 | else return 0; /* read fails */ | 298 | else return 0; /* read fails */ |
| 297 | } | 299 | } |
| 298 | 300 | ||
| 299 | 301 | ||
| 300 | static void read_word (FILE *f) { | 302 | static void read_word (lua_State *L, FILE *f) { |
| 301 | int c; | 303 | int c; |
| 302 | do { c = fgetc(f); } while isspace(c); /* skip spaces */ | 304 | do { c = fgetc(f); } while isspace(c); /* skip spaces */ |
| 303 | while (c != EOF && !isspace(c)) { | 305 | while (c != EOF && !isspace(c)) { |
| 304 | luaL_addchar(c); | 306 | luaL_addchar(L, c); |
| 305 | c = fgetc(f); | 307 | c = fgetc(f); |
| 306 | } | 308 | } |
| 307 | ungetc(c, f); | 309 | ungetc(c, f); |
| @@ -311,127 +313,127 @@ static void read_word (FILE *f) { | |||
| 311 | #define HUNK_LINE 256 | 313 | #define HUNK_LINE 256 |
| 312 | #define HUNK_FILE BUFSIZ | 314 | #define HUNK_FILE BUFSIZ |
| 313 | 315 | ||
| 314 | static int read_line (FILE *f) { | 316 | static int read_line (lua_State *L, FILE *f) { |
| 315 | int n; | 317 | int n; |
| 316 | char *b; | 318 | char *b; |
| 317 | do { | 319 | do { |
| 318 | b = luaL_openspace(HUNK_LINE); | 320 | b = luaL_openspace(L, HUNK_LINE); |
| 319 | if (!fgets(b, HUNK_LINE, f)) return 0; /* read fails */ | 321 | if (!fgets(b, HUNK_LINE, f)) return 0; /* read fails */ |
| 320 | n = strlen(b); | 322 | n = strlen(b); |
| 321 | luaL_addsize(n); | 323 | luaL_addsize(L, n); |
| 322 | } while (b[n-1] != '\n'); | 324 | } while (b[n-1] != '\n'); |
| 323 | luaL_addsize(-1); /* remove '\n' */ | 325 | luaL_addsize(L, -1); /* remove '\n' */ |
| 324 | return 1; | 326 | return 1; |
| 325 | } | 327 | } |
| 326 | 328 | ||
| 327 | 329 | ||
| 328 | static void read_file (FILE *f) { | 330 | static void read_file (lua_State *L, FILE *f) { |
| 329 | int n; | 331 | int n; |
| 330 | do { | 332 | do { |
| 331 | char *b = luaL_openspace(HUNK_FILE); | 333 | char *b = luaL_openspace(L, HUNK_FILE); |
| 332 | n = fread(b, sizeof(char), HUNK_FILE, f); | 334 | n = fread(b, sizeof(char), HUNK_FILE, f); |
| 333 | luaL_addsize(n); | 335 | luaL_addsize(L, n); |
| 334 | } while (n==HUNK_FILE); | 336 | } while (n==HUNK_FILE); |
| 335 | } | 337 | } |
| 336 | 338 | ||
| 337 | 339 | ||
| 338 | static int read_chars (FILE *f, int n) { | 340 | static int read_chars (lua_State *L, FILE *f, int n) { |
| 339 | char *b = luaL_openspace(n); | 341 | char *b = luaL_openspace(L, n); |
| 340 | int n1 = fread(b, sizeof(char), n, f); | 342 | int n1 = fread(b, sizeof(char), n, f); |
| 341 | luaL_addsize(n1); | 343 | luaL_addsize(L, n1); |
| 342 | return (n == n1); | 344 | return (n == n1); |
| 343 | } | 345 | } |
| 344 | 346 | ||
| 345 | 347 | ||
| 346 | static void io_read (void) { | 348 | static void io_read (lua_State *L) { |
| 347 | int arg = FIRSTARG; | 349 | int arg = FIRSTARG; |
| 348 | FILE *f = getfileparam(FINPUT, &arg); | 350 | FILE *f = getfileparam(L, FINPUT, &arg); |
| 349 | lua_Object op = lua_getparam(arg); | 351 | lua_Object op = lua_getparam(L, arg); |
| 350 | do { /* repeat for each part */ | 352 | do { /* repeat for each part */ |
| 351 | long l; | 353 | long l; |
| 352 | int success; | 354 | int success; |
| 353 | luaL_resetbuffer(); | 355 | luaL_resetbuffer(L); |
| 354 | if (lua_isnumber(op)) | 356 | if (lua_isnumber(L, op)) |
| 355 | success = read_chars(f, (int)lua_getnumber(op)); | 357 | success = read_chars(L, f, (int)lua_getnumber(L, op)); |
| 356 | else { | 358 | else { |
| 357 | const char *p = luaL_opt_string(arg, "*l"); | 359 | const char *p = luaL_opt_string(L, arg, "*l"); |
| 358 | if (p[0] != '*') | 360 | if (p[0] != '*') |
| 359 | success = read_pattern(f, p); /* deprecated! */ | 361 | success = read_pattern(L, f, p); /* deprecated! */ |
| 360 | else { | 362 | else { |
| 361 | switch (p[1]) { | 363 | switch (p[1]) { |
| 362 | case 'n': /* number */ | 364 | case 'n': /* number */ |
| 363 | if (!read_number(f)) return; /* read fails */ | 365 | if (!read_number(L, f)) return; /* read fails */ |
| 364 | continue; /* number is already pushed; avoid the "pushstring" */ | 366 | continue; /* number is already pushed; avoid the "pushstring" */ |
| 365 | case 'l': /* line */ | 367 | case 'l': /* line */ |
| 366 | success = read_line(f); | 368 | success = read_line(L, f); |
| 367 | break; | 369 | break; |
| 368 | case 'a': /* file */ | 370 | case 'a': /* file */ |
| 369 | read_file(f); | 371 | read_file(L, f); |
| 370 | success = 1; /* always success */ | 372 | success = 1; /* always success */ |
| 371 | break; | 373 | break; |
| 372 | case 'w': /* word */ | 374 | case 'w': /* word */ |
| 373 | read_word(f); | 375 | read_word(L, f); |
| 374 | success = 0; /* must read something to succeed */ | 376 | success = 0; /* must read something to succeed */ |
| 375 | break; | 377 | break; |
| 376 | default: | 378 | default: |
| 377 | luaL_argerror(arg, "invalid format"); | 379 | luaL_argerror(L, arg, "invalid format"); |
| 378 | success = 0; /* to avoid warnings */ | 380 | success = 0; /* to avoid warnings */ |
| 379 | } | 381 | } |
| 380 | } | 382 | } |
| 381 | } | 383 | } |
| 382 | l = luaL_getsize(); | 384 | l = luaL_getsize(L); |
| 383 | if (!success && l==0) return; /* read fails */ | 385 | if (!success && l==0) return; /* read fails */ |
| 384 | lua_pushlstring(luaL_buffer(), l); | 386 | lua_pushlstring(L, luaL_buffer(L), l); |
| 385 | } while ((op = lua_getparam(++arg)) != LUA_NOOBJECT); | 387 | } while ((op = lua_getparam(L, ++arg)) != LUA_NOOBJECT); |
| 386 | } | 388 | } |
| 387 | 389 | ||
| 388 | /* }====================================================== */ | 390 | /* }====================================================== */ |
| 389 | 391 | ||
| 390 | 392 | ||
| 391 | static void io_write (void) { | 393 | static void io_write (lua_State *L) { |
| 392 | int arg = FIRSTARG; | 394 | int arg = FIRSTARG; |
| 393 | FILE *f = getfileparam(FOUTPUT, &arg); | 395 | FILE *f = getfileparam(L, FOUTPUT, &arg); |
| 394 | int status = 1; | 396 | int status = 1; |
| 395 | lua_Object o; | 397 | lua_Object o; |
| 396 | while ((o = lua_getparam(arg++)) != LUA_NOOBJECT) { | 398 | while ((o = lua_getparam(L, arg++)) != LUA_NOOBJECT) { |
| 397 | switch (lua_type(o)[2]) { | 399 | switch (lua_type(L, o)[2]) { |
| 398 | case 'r': { /* stRing? */ | 400 | case 'r': { /* stRing? */ |
| 399 | long l = lua_strlen(o); | 401 | long l = lua_strlen(L, o); |
| 400 | status = status && | 402 | status = status && |
| 401 | ((long)fwrite(lua_getstring(o), sizeof(char), l, f) == l); | 403 | ((long)fwrite(lua_getstring(L, o), sizeof(char), l, f) == l); |
| 402 | break; | 404 | break; |
| 403 | } | 405 | } |
| 404 | case 'm': /* nuMber? */ /* LUA_NUMBER */ | 406 | case 'm': /* nuMber? */ /* LUA_NUMBER */ |
| 405 | /* optimization: could be done exactly as for strings */ | 407 | /* optimization: could be done exactly as for strings */ |
| 406 | status = status && fprintf(f, "%.16g", lua_getnumber(o)) > 0; | 408 | status = status && fprintf(f, "%.16g", lua_getnumber(L, o)) > 0; |
| 407 | break; | 409 | break; |
| 408 | default: luaL_argerror(arg-1, "string expected"); | 410 | default: luaL_argerror(L, arg-1, "string expected"); |
| 409 | } | 411 | } |
| 410 | } | 412 | } |
| 411 | pushresult(status); | 413 | pushresult(L, status); |
| 412 | } | 414 | } |
| 413 | 415 | ||
| 414 | 416 | ||
| 415 | static void io_seek (void) { | 417 | static void io_seek (lua_State *L) { |
| 416 | static const int mode[] = {SEEK_SET, SEEK_CUR, SEEK_END}; | 418 | static const int mode[] = {SEEK_SET, SEEK_CUR, SEEK_END}; |
| 417 | static const char *const modenames[] = {"set", "cur", "end", NULL}; | 419 | static const char *const modenames[] = {"set", "cur", "end", NULL}; |
| 418 | FILE *f = getnonullfile(FIRSTARG); | 420 | FILE *f = getnonullfile(L, FIRSTARG); |
| 419 | int op = luaL_findstring(luaL_opt_string(FIRSTARG+1, "cur"), modenames); | 421 | int op = luaL_findstring(luaL_opt_string(L, FIRSTARG+1, "cur"), modenames); |
| 420 | long offset = luaL_opt_long(FIRSTARG+2, 0); | 422 | long offset = luaL_opt_long(L, FIRSTARG+2, 0); |
| 421 | luaL_arg_check(op != -1, FIRSTARG+1, "invalid mode"); | 423 | luaL_arg_check(L, op != -1, FIRSTARG+1, "invalid mode"); |
| 422 | op = fseek(f, offset, mode[op]); | 424 | op = fseek(f, offset, mode[op]); |
| 423 | if (op) | 425 | if (op) |
| 424 | pushresult(0); /* error */ | 426 | pushresult(L, 0); /* error */ |
| 425 | else | 427 | else |
| 426 | lua_pushnumber(ftell(f)); | 428 | lua_pushnumber(L, ftell(f)); |
| 427 | } | 429 | } |
| 428 | 430 | ||
| 429 | 431 | ||
| 430 | static void io_flush (void) { | 432 | static void io_flush (lua_State *L) { |
| 431 | FILE *f = getfile(FIRSTARG); | 433 | FILE *f = getfile(L, FIRSTARG); |
| 432 | luaL_arg_check(f || lua_getparam(FIRSTARG) == LUA_NOOBJECT, FIRSTARG, | 434 | luaL_arg_check(L, f || lua_getparam(L, FIRSTARG) == LUA_NOOBJECT, FIRSTARG, |
| 433 | "invalid file handle"); | 435 | "invalid file handle"); |
| 434 | pushresult(fflush(f) == 0); | 436 | pushresult(L, fflush(f) == 0); |
| 435 | } | 437 | } |
| 436 | 438 | ||
| 437 | /* }====================================================== */ | 439 | /* }====================================================== */ |
| @@ -443,102 +445,102 @@ static void io_flush (void) { | |||
| 443 | ** ======================================================= | 445 | ** ======================================================= |
| 444 | */ | 446 | */ |
| 445 | 447 | ||
| 446 | static void io_execute (void) { | 448 | static void io_execute (lua_State *L) { |
| 447 | lua_pushnumber(system(luaL_check_string(1))); | 449 | lua_pushnumber(L, system(luaL_check_string(L, 1))); |
| 448 | } | 450 | } |
| 449 | 451 | ||
| 450 | 452 | ||
| 451 | static void io_remove (void) { | 453 | static void io_remove (lua_State *L) { |
| 452 | pushresult(remove(luaL_check_string(1)) == 0); | 454 | pushresult(L, remove(luaL_check_string(L, 1)) == 0); |
| 453 | } | 455 | } |
| 454 | 456 | ||
| 455 | 457 | ||
| 456 | static void io_rename (void) { | 458 | static void io_rename (lua_State *L) { |
| 457 | pushresult(rename(luaL_check_string(1), | 459 | pushresult(L, rename(luaL_check_string(L, 1), |
| 458 | luaL_check_string(2)) == 0); | 460 | luaL_check_string(L, 2)) == 0); |
| 459 | } | 461 | } |
| 460 | 462 | ||
| 461 | 463 | ||
| 462 | static void io_tmpname (void) { | 464 | static void io_tmpname (lua_State *L) { |
| 463 | lua_pushstring(tmpnam(NULL)); | 465 | lua_pushstring(L, tmpnam(NULL)); |
| 464 | } | 466 | } |
| 465 | 467 | ||
| 466 | 468 | ||
| 467 | 469 | ||
| 468 | static void io_getenv (void) { | 470 | static void io_getenv (lua_State *L) { |
| 469 | lua_pushstring(getenv(luaL_check_string(1))); /* if NULL push nil */ | 471 | lua_pushstring(L, getenv(luaL_check_string(L, 1))); /* if NULL push nil */ |
| 470 | } | 472 | } |
| 471 | 473 | ||
| 472 | 474 | ||
| 473 | static void io_clock (void) { | 475 | static void io_clock (lua_State *L) { |
| 474 | lua_pushnumber(((double)clock())/CLOCKS_PER_SEC); | 476 | lua_pushnumber(L, ((double)clock())/CLOCKS_PER_SEC); |
| 475 | } | 477 | } |
| 476 | 478 | ||
| 477 | 479 | ||
| 478 | static void io_date (void) { | 480 | static void io_date (lua_State *L) { |
| 479 | char b[256]; | 481 | char b[256]; |
| 480 | const char *s = luaL_opt_string(1, "%c"); | 482 | const char *s = luaL_opt_string(L, 1, "%c"); |
| 481 | struct tm *tm; | 483 | struct tm *tm; |
| 482 | time_t t; | 484 | time_t t; |
| 483 | time(&t); tm = localtime(&t); | 485 | time(&t); tm = localtime(&t); |
| 484 | if (strftime(b,sizeof(b),s,tm)) | 486 | if (strftime(b,sizeof(b),s,tm)) |
| 485 | lua_pushstring(b); | 487 | lua_pushstring(L, b); |
| 486 | else | 488 | else |
| 487 | lua_error("invalid `date' format"); | 489 | lua_error(L, "invalid `date' format"); |
| 488 | } | 490 | } |
| 489 | 491 | ||
| 490 | 492 | ||
| 491 | static void setloc (void) { | 493 | static void setloc (lua_State *L) { |
| 492 | static const int cat[] = {LC_ALL, LC_COLLATE, LC_CTYPE, LC_MONETARY, | 494 | static const int cat[] = {LC_ALL, LC_COLLATE, LC_CTYPE, LC_MONETARY, |
| 493 | LC_NUMERIC, LC_TIME}; | 495 | LC_NUMERIC, LC_TIME}; |
| 494 | static const char *const catnames[] = {"all", "collate", "ctype", "monetary", | 496 | static const char *const catnames[] = {"all", "collate", "ctype", "monetary", |
| 495 | "numeric", "time", NULL}; | 497 | "numeric", "time", NULL}; |
| 496 | int op = luaL_findstring(luaL_opt_string(2, "all"), catnames); | 498 | int op = luaL_findstring(luaL_opt_string(L, 2, "all"), catnames); |
| 497 | luaL_arg_check(op != -1, 2, "invalid option"); | 499 | luaL_arg_check(L, op != -1, 2, "invalid option"); |
| 498 | lua_pushstring(setlocale(cat[op], luaL_check_string(1))); | 500 | lua_pushstring(L, setlocale(cat[op], luaL_check_string(L, 1))); |
| 499 | } | 501 | } |
| 500 | 502 | ||
| 501 | 503 | ||
| 502 | static void io_exit (void) { | 504 | static void io_exit (lua_State *L) { |
| 503 | exit(luaL_opt_int(1, EXIT_SUCCESS)); | 505 | exit(luaL_opt_int(L, 1, EXIT_SUCCESS)); |
| 504 | } | 506 | } |
| 505 | 507 | ||
| 506 | /* }====================================================== */ | 508 | /* }====================================================== */ |
| 507 | 509 | ||
| 508 | 510 | ||
| 509 | 511 | ||
| 510 | static void io_debug (void) { | 512 | static void io_debug (lua_State *L) { |
| 511 | for (;;) { | 513 | for (;;) { |
| 512 | char buffer[250]; | 514 | char buffer[250]; |
| 513 | fprintf(stderr, "lua_debug> "); | 515 | fprintf(stderr, "lua_debug> "); |
| 514 | if (fgets(buffer, sizeof(buffer), stdin) == 0 || | 516 | if (fgets(buffer, sizeof(buffer), stdin) == 0 || |
| 515 | strcmp(buffer, "cont\n") == 0) | 517 | strcmp(buffer, "cont\n") == 0) |
| 516 | return; | 518 | return; |
| 517 | lua_dostring(buffer); | 519 | lua_dostring(L, buffer); |
| 518 | } | 520 | } |
| 519 | } | 521 | } |
| 520 | 522 | ||
| 521 | 523 | ||
| 522 | 524 | ||
| 523 | #define MESSAGESIZE 150 | 525 | #define MESSAGESIZE 150 |
| 524 | #define MAXMESSAGE (MESSAGESIZE*10) | 526 | #define MAXMESSAGE (MESSAGESIZE*10) |
| 525 | 527 | ||
| 526 | 528 | ||
| 527 | #define MAXSRC 60 | 529 | #define MAXSRC 60 |
| 528 | 530 | ||
| 529 | 531 | ||
| 530 | static void errorfb (void) { | 532 | static void errorfb (lua_State *L) { |
| 531 | char buff[MAXMESSAGE]; | 533 | char buff[MAXMESSAGE]; |
| 532 | int level = 1; /* skip level 0 (it's this function) */ | 534 | int level = 1; /* skip level 0 (it's this function) */ |
| 533 | lua_Object func; | 535 | lua_Object func; |
| 534 | sprintf(buff, "lua error: %.200s\n", lua_getstring(lua_getparam(1))); | 536 | sprintf(buff, "lua error: %.200s\n", lua_getstring(L, lua_getparam(L, 1))); |
| 535 | while ((func = lua_stackedfunction(level++)) != LUA_NOOBJECT) { | 537 | while ((func = lua_stackedfunction(L, level++)) != LUA_NOOBJECT) { |
| 536 | const char *name; | 538 | const char *name; |
| 537 | int currentline; | 539 | int currentline; |
| 538 | const char *chunkname; | 540 | const char *chunkname; |
| 539 | char buffchunk[MAXSRC]; | 541 | char buffchunk[MAXSRC]; |
| 540 | int linedefined; | 542 | int linedefined; |
| 541 | lua_funcinfo(func, &chunkname, &linedefined); | 543 | lua_funcinfo(L, func, &chunkname, &linedefined); |
| 542 | luaL_chunkid(buffchunk, chunkname, sizeof(buffchunk)); | 544 | luaL_chunkid(buffchunk, chunkname, sizeof(buffchunk)); |
| 543 | if (level == 2) strcat(buff, "Active Stack:\n"); | 545 | if (level == 2) strcat(buff, "Active Stack:\n"); |
| 544 | strcat(buff, " "); | 546 | strcat(buff, " "); |
| @@ -546,7 +548,7 @@ static void errorfb (void) { | |||
| 546 | strcat(buff, "...\n"); | 548 | strcat(buff, "...\n"); |
| 547 | break; /* buffer is full */ | 549 | break; /* buffer is full */ |
| 548 | } | 550 | } |
| 549 | switch (*lua_getobjname(func, &name)) { | 551 | switch (*lua_getobjname(L, func, &name)) { |
| 550 | case 'g': | 552 | case 'g': |
| 551 | sprintf(buff+strlen(buff), "function `%.50s'", name); | 553 | sprintf(buff+strlen(buff), "function `%.50s'", name); |
| 552 | break; | 554 | break; |
| @@ -564,16 +566,16 @@ static void errorfb (void) { | |||
| 564 | chunkname = NULL; | 566 | chunkname = NULL; |
| 565 | } | 567 | } |
| 566 | } | 568 | } |
| 567 | if ((currentline = lua_currentline(func)) > 0) | 569 | if ((currentline = lua_currentline(L, func)) > 0) |
| 568 | sprintf(buff+strlen(buff), " at line %d", currentline); | 570 | sprintf(buff+strlen(buff), " at line %d", currentline); |
| 569 | if (chunkname) | 571 | if (chunkname) |
| 570 | sprintf(buff+strlen(buff), " [%.70s]", buffchunk); | 572 | sprintf(buff+strlen(buff), " [%.70s]", buffchunk); |
| 571 | strcat(buff, "\n"); | 573 | strcat(buff, "\n"); |
| 572 | } | 574 | } |
| 573 | func = lua_rawgetglobal("_ALERT"); | 575 | func = lua_rawgetglobal(L, "_ALERT"); |
| 574 | if (lua_isfunction(func)) { /* avoid error loop if _ALERT is not defined */ | 576 | if (lua_isfunction(L, func)) { /* avoid error loop if _ALERT is not defined */ |
| 575 | lua_pushstring(buff); | 577 | lua_pushstring(L, buff); |
| 576 | lua_callfunction(func); | 578 | lua_callfunction(L, func); |
| 577 | } | 579 | } |
| 578 | } | 580 | } |
| 579 | 581 | ||
| @@ -607,31 +609,31 @@ static const struct luaL_reg iolibtag[] = { | |||
| 607 | }; | 609 | }; |
| 608 | 610 | ||
| 609 | 611 | ||
| 610 | static void openwithtags (void) { | 612 | static void openwithtags (lua_State *L) { |
| 611 | unsigned int i; | 613 | unsigned int i; |
| 612 | int iotag = lua_newtag(); | 614 | int iotag = lua_newtag(L); |
| 613 | lua_newtag(); /* alloc CLOSEDTAG: assume that CLOSEDTAG = iotag-1 */ | 615 | lua_newtag(L); /* alloc CLOSEDTAG: assume that CLOSEDTAG = iotag-1 */ |
| 614 | for (i=0; i<sizeof(iolibtag)/sizeof(iolibtag[0]); i++) { | 616 | for (i=0; i<sizeof(iolibtag)/sizeof(iolibtag[0]); i++) { |
| 615 | /* put iotag as upvalue for these functions */ | 617 | /* put iotag as upvalue for these functions */ |
| 616 | lua_pushnumber(iotag); | 618 | lua_pushnumber(L, iotag); |
| 617 | lua_pushcclosure(iolibtag[i].func, 1); | 619 | lua_pushcclosure(L, iolibtag[i].func, 1); |
| 618 | lua_setglobal(iolibtag[i].name); | 620 | lua_setglobal(L, iolibtag[i].name); |
| 619 | } | 621 | } |
| 620 | /* predefined file handles */ | 622 | /* predefined file handles */ |
| 621 | setfile(stdin, FINPUT, iotag); | 623 | setfile(L, stdin, FINPUT, iotag); |
| 622 | setfile(stdout, FOUTPUT, iotag); | 624 | setfile(L, stdout, FOUTPUT, iotag); |
| 623 | setfile(stdin, "_STDIN", iotag); | 625 | setfile(L, stdin, "_STDIN", iotag); |
| 624 | setfile(stdout, "_STDOUT", iotag); | 626 | setfile(L, stdout, "_STDOUT", iotag); |
| 625 | setfile(stderr, "_STDERR", iotag); | 627 | setfile(L, stderr, "_STDERR", iotag); |
| 626 | /* close file when collected */ | 628 | /* close file when collected */ |
| 627 | lua_pushnumber(iotag); | 629 | lua_pushnumber(L, iotag); |
| 628 | lua_pushcclosure(gc_close, 1); | 630 | lua_pushcclosure(L, gc_close, 1); |
| 629 | lua_settagmethod(iotag, "gc"); | 631 | lua_settagmethod(L, iotag, "gc"); |
| 630 | } | 632 | } |
| 631 | 633 | ||
| 632 | void lua_iolibopen (void) { | 634 | void lua_iolibopen (lua_State *L) { |
| 633 | /* register lib functions */ | 635 | /* register lib functions */ |
| 634 | luaL_openlib(iolib, (sizeof(iolib)/sizeof(iolib[0]))); | 636 | luaL_openlib(L, iolib, (sizeof(iolib)/sizeof(iolib[0]))); |
| 635 | openwithtags(); | 637 | openwithtags(L); |
| 636 | } | 638 | } |
| 637 | 639 | ||
