lauxlib.c 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151
  1. /*
  2. ** $Id: lauxlib.c,v 1.26 2000/02/08 16:34:31 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 "lauxlib.h"
  15. #include "lua.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, long *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, long *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, long *len) {
  51. lua_Object o = lua_getparam(L, narg);
  52. if (o == LUA_NOOBJECT) {
  53. if (len) *len = def ? strlen(def) : 0;
  54. return def;
  55. }
  56. else return checkstr(L, o, narg, len);
  57. }
  58. double luaL_check_number (lua_State *L, int narg) {
  59. lua_Object o = lua_getparam(L, narg);
  60. if (!lua_isnumber(L, o)) type_error(L, narg, "number", o);
  61. return lua_getnumber(L, o);
  62. }
  63. double luaL_opt_number (lua_State *L, int narg, double def) {
  64. lua_Object o = lua_getparam(L, narg);
  65. if (o == LUA_NOOBJECT) return def;
  66. else {
  67. if (!lua_isnumber(L, o)) type_error(L, narg, "number", o);
  68. return lua_getnumber(L, o);
  69. }
  70. }
  71. lua_Object luaL_tablearg (lua_State *L, int narg) {
  72. lua_Object o = lua_getparam(L, narg);
  73. if (!lua_istable(L, o)) type_error(L, narg, "table", o);
  74. return o;
  75. }
  76. lua_Object luaL_functionarg (lua_State *L, int narg) {
  77. lua_Object o = lua_getparam(L, narg);
  78. if (!lua_isfunction(L, o)) type_error(L, narg, "function", o);
  79. return o;
  80. }
  81. lua_Object luaL_nonnullarg (lua_State *L, int narg) {
  82. lua_Object o = lua_getparam(L, narg);
  83. luaL_arg_check(L, o != LUA_NOOBJECT, narg, "value expected");
  84. return o;
  85. }
  86. void luaL_openlib (lua_State *L, const struct luaL_reg *l, int n) {
  87. int i;
  88. for (i=0; i<n; i++)
  89. lua_register(L, l[i].name, l[i].func);
  90. }
  91. void luaL_verror (lua_State *L, const char *fmt, ...) {
  92. char buff[500];
  93. va_list argp;
  94. va_start(argp, fmt);
  95. vsprintf(buff, fmt, argp);
  96. va_end(argp);
  97. lua_error(L, buff);
  98. }
  99. #define EXTRALEN sizeof("string \"...\"0")
  100. void luaL_chunkid (char *out, const char *source, int len) {
  101. if (*source == '(') {
  102. strncpy(out, source+1, len-1); /* remove first char */
  103. out[len-1] = '\0'; /* make sure `out' has an end */
  104. out[strlen(out)-1] = '\0'; /* remove last char */
  105. }
  106. else {
  107. len -= EXTRALEN;
  108. if (*source == '@')
  109. sprintf(out, "file `%.*s'", len, source+1);
  110. else {
  111. const char *b = strchr(source , '\n'); /* stop at first new line */
  112. int lim = (b && (b-source)<len) ? b-source : len;
  113. sprintf(out, "string \"%.*s\"", lim, source);
  114. strcpy(out+lim+(EXTRALEN-sizeof("...\"0")), "...\"");
  115. }
  116. }
  117. }
  118. void luaL_filesource (char *out, const char *filename, int len) {
  119. if (filename == NULL) filename = "(stdin)";
  120. sprintf(out, "@%.*s", len-2, filename); /* -2 for '@' and '\0' */
  121. }