lauxlib.c 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154
  1. /*
  2. ** $Id: lauxlib.c,v 1.28 2000/05/24 13:54:49 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. #define LUA_REENTRANT
  14. #include "lua.h"
  15. #include "lauxlib.h"
  16. #include "luadebug.h"
  17. int luaL_findstring (const char *name, const char *const list[]) {
  18. int i;
  19. for (i=0; list[i]; i++)
  20. if (strcmp(list[i], name) == 0)
  21. return i;
  22. return -1; /* name not found */
  23. }
  24. void luaL_argerror (lua_State *L, int narg, const char *extramsg) {
  25. lua_Debug ar;
  26. lua_getstack(L, 0, &ar);
  27. lua_getinfo(L, "nu", &ar);
  28. narg -= ar.nups;
  29. if (ar.name == NULL)
  30. ar.name = "?";
  31. luaL_verror(L, "bad argument #%d to `%.50s' (%.100s)",
  32. narg, ar.name, extramsg);
  33. }
  34. static void type_error (lua_State *L, int narg, const char *type_name,
  35. lua_Object o) {
  36. char buff[100];
  37. const char *otype = (o == LUA_NOOBJECT) ? "no value" : lua_type(L, o);
  38. sprintf(buff, "%.10s expected, got %.10s", type_name, otype);
  39. luaL_argerror(L, narg, buff);
  40. }
  41. static const char *checkstr (lua_State *L, lua_Object o, int narg,
  42. size_t *len) {
  43. const char *s = lua_getstring(L, o);
  44. if (!s) type_error(L, narg, "string", o);
  45. if (len) *len = lua_strlen(L, o);
  46. return s;
  47. }
  48. const char *luaL_check_lstr (lua_State *L, int narg, size_t *len) {
  49. return checkstr(L, lua_getparam(L, narg), narg, len);
  50. }
  51. const char *luaL_opt_lstr (lua_State *L, int narg, const char *def,
  52. size_t *len) {
  53. lua_Object o = lua_getparam(L, narg);
  54. if (o == LUA_NOOBJECT) {
  55. if (len) *len = def ? strlen(def) : 0;
  56. return def;
  57. }
  58. else return checkstr(L, o, narg, len);
  59. }
  60. double luaL_check_number (lua_State *L, int narg) {
  61. lua_Object o = lua_getparam(L, narg);
  62. if (!lua_isnumber(L, o)) type_error(L, narg, "number", o);
  63. return lua_getnumber(L, o);
  64. }
  65. double luaL_opt_number (lua_State *L, int narg, double def) {
  66. lua_Object o = lua_getparam(L, narg);
  67. if (o == LUA_NOOBJECT) return def;
  68. else {
  69. if (!lua_isnumber(L, o)) type_error(L, narg, "number", o);
  70. return lua_getnumber(L, o);
  71. }
  72. }
  73. lua_Object luaL_tablearg (lua_State *L, int narg) {
  74. lua_Object o = lua_getparam(L, narg);
  75. if (!lua_istable(L, o)) type_error(L, narg, "table", o);
  76. return o;
  77. }
  78. lua_Object luaL_functionarg (lua_State *L, int narg) {
  79. lua_Object o = lua_getparam(L, narg);
  80. if (!lua_isfunction(L, o)) type_error(L, narg, "function", o);
  81. return o;
  82. }
  83. lua_Object luaL_nonnullarg (lua_State *L, int narg) {
  84. lua_Object o = lua_getparam(L, narg);
  85. luaL_arg_check(L, o != LUA_NOOBJECT, narg, "value expected");
  86. return o;
  87. }
  88. void luaL_openlib (lua_State *L, const struct luaL_reg *l, int n) {
  89. int i;
  90. for (i=0; i<n; i++)
  91. lua_register(L, l[i].name, l[i].func);
  92. }
  93. void luaL_verror (lua_State *L, const char *fmt, ...) {
  94. char buff[500];
  95. va_list argp;
  96. va_start(argp, fmt);
  97. vsprintf(buff, fmt, argp);
  98. va_end(argp);
  99. lua_error(L, buff);
  100. }
  101. #define EXTRALEN sizeof("string \"...\"0")
  102. void luaL_chunkid (char *out, const char *source, int len) {
  103. if (*source == '(') {
  104. strncpy(out, source+1, len-1); /* remove first char */
  105. out[len-1] = '\0'; /* make sure `out' has an end */
  106. out[strlen(out)-1] = '\0'; /* remove last char */
  107. }
  108. else {
  109. len -= EXTRALEN;
  110. if (*source == '@')
  111. sprintf(out, "file `%.*s'", len, source+1);
  112. else {
  113. const char *b = strchr(source , '\n'); /* stop at first new line */
  114. int lim = (b && (b-source)<len) ? b-source : len;
  115. sprintf(out, "string \"%.*s\"", lim, source);
  116. strcpy(out+lim+(EXTRALEN-sizeof("...\"0")), "...\"");
  117. }
  118. }
  119. }
  120. void luaL_filesource (char *out, const char *filename, int len) {
  121. if (filename == NULL) filename = "(stdin)";
  122. sprintf(out, "@%.*s", len-2, filename); /* -2 for '@' and '\0' */
  123. }