Lua 5.1.4: lauxlib.c
L0001
L0006
L0007
L0008 #include <ctype.h>
L0009 #include <errno.h>
L0010 #include <stdarg.h>
L0011 #include <stdio.h>
L0012 #include <stdlib.h>
L0013 #include <string.h>
L0014
L0015
L0016
L0019
L0020 #define lauxlib_c
L0021 #define LUA_LIB
L0022
L0023 #include "lua.h"
L0024
L0025 #include "lauxlib.h"
L0026
L0027
L0028 #define FREELIST_REF 0 /* free list of references */
L0029
L0030
L0031
L0032 #define abs_index(L, i) ((i) > 0 || (i) <= LUA_REGISTRYINDEX ? (i) : \
L0033 lua_gettop(L) + (i) + 1)
L0034
L0035
L0036
L0041
L0042
L0043 LUALIB_API int luaL_argerror (lua_State *L, int narg, const char *extramsg) {
L0044 lua_Debug ar;
L0045 if (!lua_getstack(L, 0, &ar))
L0046 return luaL_error(L, "bad argument #%d (%s)", narg, extramsg);
L0047 lua_getinfo(L, "n", &ar);
L0048 if (strcmp(ar.namewhat, "method") == 0) {
L0049 narg--;
L0050 if (narg == 0)
L0051 return luaL_error(L, "calling " LUA_QS " on bad self (%s)",
L0052 ar.name, extramsg);
L0053 }
L0054 if (ar.name == NULL)
L0055 ar.name = "?";
L0056 return luaL_error(L, "bad argument #%d to " LUA_QS " (%s)",
L0057 narg, ar.name, extramsg);
L0058 }
L0059
L0060
L0061 LUALIB_API int luaL_typerror (lua_State *L, int narg, const char *tname) {
L0062 const char *msg = lua_pushfstring(L, "%s expected, got %s",
L0063 tname, luaL_typename(L, narg));
L0064 return luaL_argerror(L, narg, msg);
L0065 }
L0066
L0067
L0068 static void tag_error (lua_State *L, int narg, int tag) {
L0069 luaL_typerror(L, narg, lua_typename(L, tag));
L0070 }
L0071
L0072
L0073 LUALIB_API void luaL_where (lua_State *L, int level) {
L0074 lua_Debug ar;
L0075 if (lua_getstack(L, level, &ar)) {
L0076 lua_getinfo(L, "Sl", &ar);
L0077 if (ar.currentline > 0) {
L0078 lua_pushfstring(L, "%s:%d: ", ar.short_src, ar.currentline);
L0079 return;
L0080 }
L0081 }
L0082 lua_pushliteral(L, "");
L0083 }
L0084
L0085
L0086 LUALIB_API int luaL_error (lua_State *L, const char *fmt, ...) {
L0087 va_list argp;
L0088 va_start(argp, fmt);
L0089 luaL_where(L, 1);
L0090 lua_pushvfstring(L, fmt, argp);
L0091 va_end(argp);
L0092 lua_concat(L, 2);
L0093 return lua_error(L);
L0094 }
L0095
L0096
L0097
L0098
L0099 LUALIB_API int luaL_checkoption (lua_State *L, int narg, const char *def,
L0100 const char *const lst[]) {
L0101 const char *name = (def) ? luaL_optstring(L, narg, def) :
L0102 luaL_checkstring(L, narg);
L0103 int i;
L0104 for (i=0; lst[i]; i++)
L0105 if (strcmp(lst[i], name) == 0)
L0106 return i;
L0107 return luaL_argerror(L, narg,
L0108 lua_pushfstring(L, "invalid option " LUA_QS, name));
L0109 }
L0110
L0111
L0112 LUALIB_API int luaL_newmetatable (lua_State *L, const char *tname) {
L0113 lua_getfield(L, LUA_REGISTRYINDEX, tname);
L0114 if (!lua_isnil(L, -1))
L0115 return 0;
L0116 lua_pop(L, 1);
L0117 lua_newtable(L);
L0118 lua_pushvalue(L, -1);
L0119 lua_setfield(L, LUA_REGISTRYINDEX, tname);
L0120 return 1;
L0121 }
L0122
L0123
L0124 LUALIB_API void *luaL_checkudata (lua_State *L, int ud, const char *tname) {
L0125 void *p = lua_touserdata(L, ud);
L0126 if (p != NULL) {
L0127 if (lua_getmetatable(L, ud)) {
L0128 lua_getfield(L, LUA_REGISTRYINDEX, tname);
L0129 if (lua_rawequal(L, -1, -2)) {
L0130 lua_pop(L, 2);
L0131 return p;
L0132 }
L0133 }
L0134 }
L0135 luaL_typerror(L, ud, tname);
L0136 return NULL;
L0137 }
L0138
L0139
L0140 LUALIB_API void luaL_checkstack (lua_State *L, int space, const char *mes) {
L0141 if (!lua_checkstack(L, space))
L0142 luaL_error(L, "stack overflow (%s)", mes);
L0143 }
L0144
L0145
L0146 LUALIB_API void luaL_checktype (lua_State *L, int narg, int t) {
L0147 if (lua_type(L, narg) != t)
L0148 tag_error(L, narg, t);
L0149 }
L0150
L0151
L0152 LUALIB_API void luaL_checkany (lua_State *L, int narg) {
L0153 if (lua_type(L, narg) == LUA_TNONE)
L0154 luaL_argerror(L, narg, "value expected");
L0155 }
L0156
L0157
L0158 LUALIB_API const char *luaL_checklstring (lua_State *L, int narg, size_t *len) {
L0159 const char *s = lua_tolstring(L, narg, len);
L0160 if (!s) tag_error(L, narg, LUA_TSTRING);
L0161 return s;
L0162 }
L0163
L0164
L0165 LUALIB_API const char *luaL_optlstring (lua_State *L, int narg,
L0166 const char *def, size_t *len) {
L0167 if (lua_isnoneornil(L, narg)) {
L0168 if (len)
L0169 *len = (def ? strlen(def) : 0);
L0170 return def;
L0171 }
L0172 else return luaL_checklstring(L, narg, len);
L0173 }
L0174
L0175
L0176 LUALIB_API lua_Number luaL_checknumber (lua_State *L, int narg) {
L0177 lua_Number d = lua_tonumber(L, narg);
L0178 if (d == 0 && !lua_isnumber(L, narg))
L0179 tag_error(L, narg, LUA_TNUMBER);
L0180 return d;
L0181 }
L0182
L0183
L0184 LUALIB_API lua_Number luaL_optnumber (lua_State *L, int narg, lua_Number def) {
L0185 return luaL_opt(L, luaL_checknumber, narg, def);
L0186 }
L0187
L0188
L0189 LUALIB_API lua_Integer luaL_checkinteger (lua_State *L, int narg) {
L0190 lua_Integer d = lua_tointeger(L, narg);
L0191 if (d == 0 && !lua_isnumber(L, narg))
L0192 tag_error(L, narg, LUA_TNUMBER);
L0193 return d;
L0194 }
L0195
L0196
L0197 LUALIB_API lua_Integer luaL_optinteger (lua_State *L, int narg,
L0198 lua_Integer def) {
L0199 return luaL_opt(L, luaL_checkinteger, narg, def);
L0200 }
L0201
L0202
L0203 LUALIB_API int luaL_getmetafield (lua_State *L, int obj, const char *event) {
L0204 if (!lua_getmetatable(L, obj))
L0205 return 0;
L0206 lua_pushstring(L, event);
L0207 lua_rawget(L, -2);
L0208 if (lua_isnil(L, -1)) {
L0209 lua_pop(L, 2);
L0210 return 0;
L0211 }
L0212 else {
L0213 lua_remove(L, -2);
L0214 return 1;
L0215 }
L0216 }
L0217
L0218
L0219 LUALIB_API int luaL_callmeta (lua_State *L, int obj, const char *event) {
L0220 obj = abs_index(L, obj);
L0221 if (!luaL_getmetafield(L, obj, event))
L0222 return 0;
L0223 lua_pushvalue(L, obj);
L0224 lua_call(L, 1, 1);
L0225 return 1;
L0226 }
L0227
L0228
L0229 LUALIB_API void (luaL_register) (lua_State *L, const char *libname,
L0230 const luaL_Reg *l) {
L0231 luaI_openlib(L, libname, l, 0);
L0232 }
L0233
L0234
L0235 static int libsize (const luaL_Reg *l) {
L0236 int size = 0;
L0237 for (; l->name; l++) size++;
L0238 return size;
L0239 }
L0240
L0241
L0242 LUALIB_API void luaI_openlib (lua_State *L, const char *libname,
L0243 const luaL_Reg *l, int nup) {
L0244 if (libname) {
L0245 int size = libsize(l);
L0246
L0247 luaL_findtable(L, LUA_REGISTRYINDEX, "_LOADED", 1);
L0248 lua_getfield(L, -1, libname);
L0249 if (!lua_istable(L, -1)) {
L0250 lua_pop(L, 1);
L0251
L0252 if (luaL_findtable(L, LUA_GLOBALSINDEX, libname, size) != NULL)
L0253 luaL_error(L, "name conflict for module " LUA_QS, libname);
L0254 lua_pushvalue(L, -1);
L0255 lua_setfield(L, -3, libname);
L0256 }
L0257 lua_remove(L, -2);
L0258 lua_insert(L, -(nup+1));
L0259 }
L0260 for (; l->name; l++) {
L0261 int i;
L0262 for (i=0; i<nup; i++)
L0263 lua_pushvalue(L, -nup);
L0264 lua_pushcclosure(L, l->func, nup);
L0265 lua_setfield(L, -(nup+2), l->name);
L0266 }
L0267 lua_pop(L, nup);
L0268 }
L0269
L0270
L0271
L0272
L0277
L0278 #if defined(LUA_COMPAT_GETN)
L0279
L0280 static int checkint (lua_State *L, int topop) {
L0281 int n = (lua_type(L, -1) == LUA_TNUMBER) ? lua_tointeger(L, -1) : -1;
L0282 lua_pop(L, topop);
L0283 return n;
L0284 }
L0285
L0286
L0287 static void getsizes (lua_State *L) {
L0288 lua_getfield(L, LUA_REGISTRYINDEX, "LUA_SIZES");
L0289 if (lua_isnil(L, -1)) {
L0290 lua_pop(L, 1);
L0291 lua_newtable(L);
L0292 lua_pushvalue(L, -1);
L0293 lua_setmetatable(L, -2);
L0294 lua_pushliteral(L, "kv");
L0295 lua_setfield(L, -2, "__mode");
L0296 lua_pushvalue(L, -1);
L0297 lua_setfield(L, LUA_REGISTRYINDEX, "LUA_SIZES");
L0298 }
L0299 }
L0300
L0301
L0302 LUALIB_API void luaL_setn (lua_State *L, int t, int n) {
L0303 t = abs_index(L, t);
L0304 lua_pushliteral(L, "n");
L0305 lua_rawget(L, t);
L0306 if (checkint(L, 1) >= 0) {
L0307 lua_pushliteral(L, "n");
L0308 lua_pushinteger(L, n);
L0309 lua_rawset(L, t);
L0310 }
L0311 else {
L0312 getsizes(L);
L0313 lua_pushvalue(L, t);
L0314 lua_pushinteger(L, n);
L0315 lua_rawset(L, -3);
L0316 lua_pop(L, 1);
L0317 }
L0318 }
L0319
L0320
L0321 LUALIB_API int luaL_getn (lua_State *L, int t) {
L0322 int n;
L0323 t = abs_index(L, t);
L0324 lua_pushliteral(L, "n");
L0325 lua_rawget(L, t);
L0326 if ((n = checkint(L, 1)) >= 0) return n;
L0327 getsizes(L);
L0328 lua_pushvalue(L, t);
L0329 lua_rawget(L, -2);
L0330 if ((n = checkint(L, 2)) >= 0) return n;
L0331 return (int)lua_objlen(L, t);
L0332 }
L0333
L0334 #endif
L0335
L0336
L0337
L0338
L0339
L0340 LUALIB_API const char *luaL_gsub (lua_State *L, const char *s, const char *p,
L0341 const char *r) {
L0342 const char *wild;
L0343 size_t l = strlen(p);
L0344 luaL_Buffer b;
L0345 luaL_buffinit(L, &b);
L0346 while ((wild = strstr(s, p)) != NULL) {
L0347 luaL_addlstring(&b, s, wild - s);
L0348 luaL_addstring(&b, r);
L0349 s = wild + l;
L0350 }
L0351 luaL_addstring(&b, s);
L0352 luaL_pushresult(&b);
L0353 return lua_tostring(L, -1);
L0354 }
L0355
L0356
L0357 LUALIB_API const char *luaL_findtable (lua_State *L, int idx,
L0358 const char *fname, int szhint) {
L0359 const char *e;
L0360 lua_pushvalue(L, idx);
L0361 do {
L0362 e = strchr(fname, '.');
L0363 if (e == NULL) e = fname + strlen(fname);
L0364 lua_pushlstring(L, fname, e - fname);
L0365 lua_rawget(L, -2);
L0366 if (lua_isnil(L, -1)) {
L0367 lua_pop(L, 1);
L0368 lua_createtable(L, 0, (*e == '.' ? 1 : szhint));
L0369 lua_pushlstring(L, fname, e - fname);
L0370 lua_pushvalue(L, -2);
L0371 lua_settable(L, -4);
L0372 }
L0373 else if (!lua_istable(L, -1)) {
L0374 lua_pop(L, 2);
L0375 return fname;
L0376 }
L0377 lua_remove(L, -2);
L0378 fname = e + 1;
L0379 } while (*e == '.');
L0380 return NULL;
L0381 }
L0382
L0383
L0384
L0385
L0390
L0391
L0392 #define bufflen(B) ((B)->p - (B)->buffer)
L0393 #define bufffree(B) ((size_t)(LUAL_BUFFERSIZE - bufflen(B)))
L0394
L0395 #define LIMIT (LUA_MINSTACK/2)
L0396
L0397
L0398 static int emptybuffer (luaL_Buffer *B) {
L0399 size_t l = bufflen(B);
L0400 if (l == 0) return 0;
L0401 else {
L0402 lua_pushlstring(B->L, B->buffer, l);
L0403 B->p = B->buffer;
L0404 B->lvl++;
L0405 return 1;
L0406 }
L0407 }
L0408
L0409
L0410 static void adjuststack (luaL_Buffer *B) {
L0411 if (B->lvl > 1) {
L0412 lua_State *L = B->L;
L0413 int toget = 1;
L0414 size_t toplen = lua_strlen(L, -1);
L0415 do {
L0416 size_t l = lua_strlen(L, -(toget+1));
L0417 if (B->lvl - toget + 1 >= LIMIT || toplen > l) {
L0418 toplen += l;
L0419 toget++;
L0420 }
L0421 else break;
L0422 } while (toget < B->lvl);
L0423 lua_concat(L, toget);
L0424 B->lvl = B->lvl - toget + 1;
L0425 }
L0426 }
L0427
L0428
L0429 LUALIB_API char *luaL_prepbuffer (luaL_Buffer *B) {
L0430 if (emptybuffer(B))
L0431 adjuststack(B);
L0432 return B->buffer;
L0433 }
L0434
L0435
L0436 LUALIB_API void luaL_addlstring (luaL_Buffer *B, const char *s, size_t l) {
L0437 while (l--)
L0438 luaL_addchar(B, *s++);
L0439 }
L0440
L0441
L0442 LUALIB_API void luaL_addstring (luaL_Buffer *B, const char *s) {
L0443 luaL_addlstring(B, s, strlen(s));
L0444 }
L0445
L0446
L0447 LUALIB_API void luaL_pushresult (luaL_Buffer *B) {
L0448 emptybuffer(B);
L0449 lua_concat(B->L, B->lvl);
L0450 B->lvl = 1;
L0451 }
L0452
L0453
L0454 LUALIB_API void luaL_addvalue (luaL_Buffer *B) {
L0455 lua_State *L = B->L;
L0456 size_t vl;
L0457 const char *s = lua_tolstring(L, -1, &vl);
L0458 if (vl <= bufffree(B)) {
L0459 memcpy(B->p, s, vl);
L0460 B->p += vl;
L0461 lua_pop(L, 1);
L0462 }
L0463 else {
L0464 if (emptybuffer(B))
L0465 lua_insert(L, -2);
L0466 B->lvl++;
L0467 adjuststack(B);
L0468 }
L0469 }
L0470
L0471
L0472 LUALIB_API void luaL_buffinit (lua_State *L, luaL_Buffer *B) {
L0473 B->L = L;
L0474 B->p = B->buffer;
L0475 B->lvl = 0;
L0476 }
L0477
L0478
L0479
L0480
L0481 LUALIB_API int luaL_ref (lua_State *L, int t) {
L0482 int ref;
L0483 t = abs_index(L, t);
L0484 if (lua_isnil(L, -1)) {
L0485 lua_pop(L, 1);
L0486 return LUA_REFNIL;
L0487 }
L0488 lua_rawgeti(L, t, FREELIST_REF);
L0489 ref = (int)lua_tointeger(L, -1);
L0490 lua_pop(L, 1);
L0491 if (ref != 0) {
L0492 lua_rawgeti(L, t, ref);
L0493 lua_rawseti(L, t, FREELIST_REF);
L0494 }
L0495 else {
L0496 ref = (int)lua_objlen(L, t);
L0497 ref++;
L0498 }
L0499 lua_rawseti(L, t, ref);
L0500 return ref;
L0501 }
L0502
L0503
L0504 LUALIB_API void luaL_unref (lua_State *L, int t, int ref) {
L0505 if (ref >= 0) {
L0506 t = abs_index(L, t);
L0507 lua_rawgeti(L, t, FREELIST_REF);
L0508 lua_rawseti(L, t, ref);
L0509 lua_pushinteger(L, ref);
L0510 lua_rawseti(L, t, FREELIST_REF);
L0511 }
L0512 }
L0513
L0514
L0515
L0516
L0521
L0522 typedef struct LoadF {
L0523 int extraline;
L0524 FILE *f;
L0525 char buff[LUAL_BUFFERSIZE];
L0526 } LoadF;
L0527
L0528
L0529 static const char *getF (lua_State *L, void *ud, size_t *size) {
L0530 LoadF *lf = (LoadF *)ud;
L0531 (void)L;
L0532 if (lf->extraline) {
L0533 lf->extraline = 0;
L0534 *size = 1;
L0535 return "\n";
L0536 }
L0537 if (feof(lf->f)) return NULL;
L0538 *size = fread(lf->buff, 1, sizeof(lf->buff), lf->f);
L0539 return (*size > 0) ? lf->buff : NULL;
L0540 }
L0541
L0542
L0543 static int errfile (lua_State *L, const char *what, int fnameindex) {
L0544 const char *serr = strerror(errno);
L0545 const char *filename = lua_tostring(L, fnameindex) + 1;
L0546 lua_pushfstring(L, "cannot %s %s: %s", what, filename, serr);
L0547 lua_remove(L, fnameindex);
L0548 return LUA_ERRFILE;
L0549 }
L0550
L0551
L0552 LUALIB_API int luaL_loadfile (lua_State *L, const char *filename) {
L0553 LoadF lf;
L0554 int status, readstatus;
L0555 int c;
L0556 int fnameindex = lua_gettop(L) + 1;
L0557 lf.extraline = 0;
L0558 if (filename == NULL) {
L0559 lua_pushliteral(L, "=stdin");
L0560 lf.f = stdin;
L0561 }
L0562 else {
L0563 lua_pushfstring(L, "@%s", filename);
L0564 lf.f = fopen(filename, "r");
L0565 if (lf.f == NULL) return errfile(L, "open", fnameindex);
L0566 }
L0567 c = getc(lf.f);
L0568 if (c == '#') {
L0569 lf.extraline = 1;
L0570 while ((c = getc(lf.f)) != EOF && c != '\n') ;
L0571 if (c == '\n') c = getc(lf.f);
L0572 }
L0573 if (c == LUA_SIGNATURE[0] && filename) {
L0574 lf.f = freopen(filename, "rb", lf.f);
L0575 if (lf.f == NULL) return errfile(L, "reopen", fnameindex);
L0576
L0577 while ((c = getc(lf.f)) != EOF && c != LUA_SIGNATURE[0]) ;
L0578 lf.extraline = 0;
L0579 }
L0580 ungetc(c, lf.f);
L0581 status = lua_load(L, getF, &lf, lua_tostring(L, -1));
L0582 readstatus = ferror(lf.f);
L0583 if (filename) fclose(lf.f);
L0584 if (readstatus) {
L0585 lua_settop(L, fnameindex);
L0586 return errfile(L, "read", fnameindex);
L0587 }
L0588 lua_remove(L, fnameindex);
L0589 return status;
L0590 }
L0591
L0592
L0593 typedef struct LoadS {
L0594 const char *s;
L0595 size_t size;
L0596 } LoadS;
L0597
L0598
L0599 static const char *getS (lua_State *L, void *ud, size_t *size) {
L0600 LoadS *ls = (LoadS *)ud;
L0601 (void)L;
L0602 if (ls->size == 0) return NULL;
L0603 *size = ls->size;
L0604 ls->size = 0;
L0605 return ls->s;
L0606 }
L0607
L0608
L0609 LUALIB_API int luaL_loadbuffer (lua_State *L, const char *buff, size_t size,
L0610 const char *name) {
L0611 LoadS ls;
L0612 ls.s = buff;
L0613 ls.size = size;
L0614 return lua_load(L, getS, &ls, name);
L0615 }
L0616
L0617
L0618 LUALIB_API int (luaL_loadstring) (lua_State *L, const char *s) {
L0619 return luaL_loadbuffer(L, s, strlen(s), s);
L0620 }
L0621
L0622
L0623
L0624
L0625
L0626
L0627 static void *l_alloc (void *ud, void *ptr, size_t osize, size_t nsize) {
L0628 (void)ud;
L0629 (void)osize;
L0630 if (nsize == 0) {
L0631 free(ptr);
L0632 return NULL;
L0633 }
L0634 else
L0635 return realloc(ptr, nsize);
L0636 }
L0637
L0638
L0639 static int panic (lua_State *L) {
L0640 (void)L;
L0641 fprintf(stderr, "PANIC: unprotected error in call to Lua API (%s)\n",
L0642 lua_tostring(L, -1));
L0643 return 0;
L0644 }
L0645
L0646
L0647 LUALIB_API lua_State *luaL_newstate (void) {
L0648 lua_State *L = lua_newstate(l_alloc, NULL);
L0649 if (L) lua_atpanic(L, &panic);
L0650 return L;
L0651 }
L0652
Generated by pretty.lua