lauxlib.c 4.0 KB

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