lauxlib.c 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236
  1. /*
  2. ** $Id: lauxlib.c,v 1.33 2000/08/29 20:43:28 roberto Exp roberto $
  3. ** Auxiliary functions for building Lua libraries
  4. ** See Copyright Notice in lua.h
  5. */
  6. #include <stdarg.h>
  7. #include <stdio.h>
  8. #include <string.h>
  9. /* This file uses only the official API of Lua.
  10. ** Any function declared here could be written as an application function.
  11. ** With care, these functions can be used by other libraries.
  12. */
  13. #include "lua.h"
  14. #include "lauxlib.h"
  15. #include "luadebug.h"
  16. int luaL_findstring (const char *name, const char *const list[]) {
  17. int i;
  18. for (i=0; list[i]; i++)
  19. if (strcmp(list[i], name) == 0)
  20. return i;
  21. return -1; /* name not found */
  22. }
  23. void luaL_argerror (lua_State *L, int narg, const char *extramsg) {
  24. lua_Debug ar;
  25. lua_getstack(L, 0, &ar);
  26. lua_getinfo(L, "n", &ar);
  27. if (ar.name == NULL)
  28. ar.name = "?";
  29. luaL_verror(L, "bad argument #%d to `%.50s' (%.100s)",
  30. narg, ar.name, extramsg);
  31. }
  32. static void type_error (lua_State *L, int narg, const char *type_name) {
  33. char buff[100];
  34. const char *rt = lua_type(L, narg);
  35. if (*rt == 'N') rt = "no value";
  36. sprintf(buff, "%.10s expected, got %.10s", type_name, rt);
  37. luaL_argerror(L, narg, buff);
  38. }
  39. void luaL_checkstack (lua_State *L, int space, const char *mes) {
  40. if (space > lua_stackspace(L))
  41. luaL_verror(L, "stack overflow (%.30s)", mes);
  42. }
  43. /*
  44. ** use the 3rd letter of type names for testing:
  45. ** nuMber, niL, stRing, fuNction, usErdata, taBle, anY
  46. */
  47. void luaL_checktype(lua_State *L, int narg, const char *tname) {
  48. const char *rt = lua_type(L, narg);
  49. if (!(*rt != 'N' && (tname[2] == 'y' || tname[2] == rt[2])))
  50. type_error(L, narg, tname);
  51. }
  52. static const char *checkstr (lua_State *L, int narg, size_t *len) {
  53. const char *s = lua_tostring(L, narg);
  54. if (!s) type_error(L, narg, "string");
  55. if (len) *len = lua_strlen(L, narg);
  56. return s;
  57. }
  58. const char *luaL_check_lstr (lua_State *L, int narg, size_t *len) {
  59. return checkstr(L, narg, len);
  60. }
  61. const char *luaL_opt_lstr (lua_State *L, int narg, const char *def,
  62. size_t *len) {
  63. if (lua_isnull(L, narg)) {
  64. if (len) *len = def ? strlen(def) : 0;
  65. return def;
  66. }
  67. else return checkstr(L, narg, len);
  68. }
  69. double luaL_check_number (lua_State *L, int narg) {
  70. if (!lua_isnumber(L, narg)) type_error(L, narg, "number");
  71. return lua_tonumber(L, narg);
  72. }
  73. double luaL_opt_number (lua_State *L, int narg, double def) {
  74. if (lua_isnull(L, narg)) return def;
  75. else {
  76. if (!lua_isnumber(L, narg)) type_error(L, narg, "number");
  77. return lua_tonumber(L, narg);
  78. }
  79. }
  80. void luaL_openlib (lua_State *L, const struct luaL_reg *l, int n) {
  81. int i;
  82. for (i=0; i<n; i++)
  83. lua_register(L, l[i].name, l[i].func);
  84. }
  85. void luaL_verror (lua_State *L, const char *fmt, ...) {
  86. char buff[500];
  87. va_list argp;
  88. va_start(argp, fmt);
  89. vsprintf(buff, fmt, argp);
  90. va_end(argp);
  91. lua_error(L, buff);
  92. }
  93. #define EXTRALEN sizeof("string \"...\"0")
  94. void luaL_chunkid (char *out, const char *source, int len) {
  95. if (*source == '(') {
  96. strncpy(out, source+1, len-1); /* remove first char */
  97. out[len-1] = '\0'; /* make sure `out' has an end */
  98. out[strlen(out)-1] = '\0'; /* remove last char */
  99. }
  100. else {
  101. len -= EXTRALEN;
  102. if (*source == '@')
  103. sprintf(out, "file `%.*s'", len, source+1);
  104. else {
  105. const char *b = strchr(source , '\n'); /* stop at first new line */
  106. int lim = (b && (b-source)<len) ? b-source : len;
  107. sprintf(out, "string \"%.*s\"", lim, source);
  108. strcpy(out+lim+(EXTRALEN-sizeof("...\"0")), "...\"");
  109. }
  110. }
  111. }
  112. /*
  113. ** {======================================================
  114. ** Generic Buffer manipulation
  115. ** =======================================================
  116. */
  117. #define buffempty(B) ((B)->p == (B)->buffer)
  118. #define bufflen(B) ((B)->p - (B)->buffer)
  119. #define bufffree(B) ((size_t)(LUAL_BUFFERSIZE - bufflen(B)))
  120. #define LIMIT (LUA_MINSTACK/2)
  121. static int emptybuffer (luaL_Buffer *B) {
  122. size_t l = bufflen(B);
  123. if (l == 0) return 0; /* put nothing on stack */
  124. else {
  125. lua_pushlstring(B->L, B->buffer, l);
  126. B->p = B->buffer;
  127. B->level++;
  128. return 1;
  129. }
  130. }
  131. static void adjuststack (luaL_Buffer *B) {
  132. if (B->level > 1) {
  133. lua_State *L = B->L;
  134. int toget = 1; /* number of levels to concat */
  135. size_t toplen = lua_strlen(L, -1);
  136. do {
  137. size_t l = lua_strlen(L, -(toget+1));
  138. if (B->level - toget + 1 >= LIMIT || toplen > l) {
  139. toplen += l;
  140. toget++;
  141. }
  142. else break;
  143. } while (toget < B->level);
  144. if (toget >= 2) {
  145. lua_concat(L, toget);
  146. B->level = B->level - toget + 1;
  147. }
  148. }
  149. }
  150. char *luaL_prepbuffer (luaL_Buffer *B) {
  151. if (emptybuffer(B))
  152. adjuststack(B);
  153. return B->buffer;
  154. }
  155. void luaL_addlstring (luaL_Buffer *B, const char *s, size_t l) {
  156. while (l--)
  157. luaL_putchar(B, *s++);
  158. }
  159. void luaL_pushresult (luaL_Buffer *B) {
  160. emptybuffer(B);
  161. if (B->level == 0)
  162. lua_pushlstring(B->L, NULL, 0);
  163. else if (B->level > 1)
  164. lua_concat(B->L, B->level);
  165. B->level = 1;
  166. }
  167. void luaL_addvalue (luaL_Buffer *B) {
  168. lua_State *L = B->L;
  169. size_t vl = lua_strlen(L, -1);
  170. if (vl <= bufffree(B)) { /* fit into buffer? */
  171. memcpy(B->p, lua_tostring(L, -1), vl); /* put it there */
  172. B->p += vl;
  173. lua_pop(L, 1); /* remove from stack */
  174. }
  175. else {
  176. if (emptybuffer(B))
  177. lua_insert(L, -2); /* put buffer before new value */
  178. B->level++; /* add new value into B stack */
  179. adjuststack(B);
  180. }
  181. }
  182. void luaL_buffinit (lua_State *L, luaL_Buffer *B) {
  183. B->L = L;
  184. B->p = B->buffer;
  185. B->level = 0;
  186. }
  187. /* }====================================================== */