lauxlib.c 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142
  1. /*
  2. ** $Id: lauxlib.c,v 1.30 2000/08/09 19:16:57 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. char buff[100];
  35. const char *rt = lua_type(L, narg);
  36. if (*rt == 'N') rt = "no value";
  37. sprintf(buff, "%.10s expected, got %.10s", type_name, rt);
  38. luaL_argerror(L, narg, buff);
  39. }
  40. /*
  41. ** use the 3rd letter of type names for testing:
  42. ** nuMber, niL, stRing, fuNction, usErdata, taBle, anY
  43. */
  44. void luaL_checktype(lua_State *L, int narg, const char *tname) {
  45. const char *rt = lua_type(L, narg);
  46. if (!(*rt != 'N' && (tname[2] == 'y' || tname[2] == rt[2])))
  47. type_error(L, narg, tname);
  48. }
  49. static const char *checkstr (lua_State *L, int narg, size_t *len) {
  50. const char *s = lua_tostring(L, narg);
  51. if (!s) type_error(L, narg, "string");
  52. if (len) *len = lua_strlen(L, narg);
  53. return s;
  54. }
  55. const char *luaL_check_lstr (lua_State *L, int narg, size_t *len) {
  56. return checkstr(L, narg, len);
  57. }
  58. const char *luaL_opt_lstr (lua_State *L, int narg, const char *def,
  59. size_t *len) {
  60. if (lua_isnull(L, narg)) {
  61. if (len) *len = def ? strlen(def) : 0;
  62. return def;
  63. }
  64. else return checkstr(L, narg, len);
  65. }
  66. double luaL_check_number (lua_State *L, int narg) {
  67. if (!lua_isnumber(L, narg)) type_error(L, narg, "number");
  68. return lua_tonumber(L, narg);
  69. }
  70. double luaL_opt_number (lua_State *L, int narg, double def) {
  71. if (lua_isnull(L, narg)) return def;
  72. else {
  73. if (!lua_isnumber(L, narg)) type_error(L, narg, "number");
  74. return lua_tonumber(L, narg);
  75. }
  76. }
  77. void luaL_openlib (lua_State *L, const struct luaL_reg *l, int n) {
  78. int i;
  79. for (i=0; i<n; i++)
  80. lua_register(L, l[i].name, l[i].func);
  81. }
  82. void luaL_verror (lua_State *L, const char *fmt, ...) {
  83. char buff[500];
  84. va_list argp;
  85. va_start(argp, fmt);
  86. vsprintf(buff, fmt, argp);
  87. va_end(argp);
  88. lua_error(L, buff);
  89. }
  90. #define EXTRALEN sizeof("string \"...\"0")
  91. void luaL_chunkid (char *out, const char *source, int len) {
  92. if (*source == '(') {
  93. strncpy(out, source+1, len-1); /* remove first char */
  94. out[len-1] = '\0'; /* make sure `out' has an end */
  95. out[strlen(out)-1] = '\0'; /* remove last char */
  96. }
  97. else {
  98. len -= EXTRALEN;
  99. if (*source == '@')
  100. sprintf(out, "file `%.*s'", len, source+1);
  101. else {
  102. const char *b = strchr(source , '\n'); /* stop at first new line */
  103. int lim = (b && (b-source)<len) ? b-source : len;
  104. sprintf(out, "string \"%.*s\"", lim, source);
  105. strcpy(out+lim+(EXTRALEN-sizeof("...\"0")), "...\"");
  106. }
  107. }
  108. }
  109. void luaL_filesource (char *out, const char *filename, int len) {
  110. if (filename == NULL) filename = "(stdin)";
  111. sprintf(out, "@%.*s", len-2, filename); /* -2 for '@' and '\0' */
  112. }